drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1 | /* DO NOT EDIT! |
| 2 | ** This file is automatically generated by the script in the canonical |
| 3 | ** SQLite source tree at tool/mkshellc.tcl. That script combines source |
| 4 | ** code from various constituent source files of SQLite into this single |
| 5 | ** "shell.c" file used to implement the SQLite command-line shell. |
| 6 | ** |
| 7 | ** Most of the code found below comes from the "src/shell.c.in" file in |
| 8 | ** the canonical SQLite source tree. That main file contains "INCLUDE" |
| 9 | ** lines that specify other files in the canonical source tree that are |
| 10 | ** inserted to getnerate this complete program source file. |
| 11 | ** |
| 12 | ** The code from multiple files is combined into this single "shell.c" |
| 13 | ** source file to help make the command-line program easier to compile. |
| 14 | ** |
| 15 | ** To modify this program, get a copy of the canonical SQLite source tree, |
| 16 | ** edit the src/shell.c.in" and/or some of the other files that are included |
| 17 | ** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script. |
| 18 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 19 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 20 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 21 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 22 | ** The author disclaims copyright to this source code. In place of |
| 23 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 24 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 25 | ** May you do good and not evil. |
| 26 | ** May you find forgiveness for yourself and forgive others. |
| 27 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 28 | ** |
| 29 | ************************************************************************* |
| 30 | ** This file contains code to implement the "sqlite" command line |
| 31 | ** utility for accessing SQLite databases. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 32 | */ |
mistachkin | a3b2ff5 | 2011-09-16 20:16:36 +0000 | [diff] [blame] | 33 | #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS) |
shane | 18e526c | 2008-12-10 22:30:24 +0000 | [diff] [blame] | 34 | /* This needs to come before any includes for MSVC compiler */ |
| 35 | #define _CRT_SECURE_NO_WARNINGS |
| 36 | #endif |
| 37 | |
drh | 36f7dd3 | 2011-10-13 16:02:17 +0000 | [diff] [blame] | 38 | /* |
drh | ce13b99 | 2017-05-23 20:00:00 +0000 | [diff] [blame] | 39 | ** Warning pragmas copied from msvc.h in the core. |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 40 | */ |
drh | ce13b99 | 2017-05-23 20:00:00 +0000 | [diff] [blame] | 41 | #if defined(_MSC_VER) |
| 42 | #pragma warning(disable : 4054) |
| 43 | #pragma warning(disable : 4055) |
| 44 | #pragma warning(disable : 4100) |
| 45 | #pragma warning(disable : 4127) |
| 46 | #pragma warning(disable : 4130) |
| 47 | #pragma warning(disable : 4152) |
| 48 | #pragma warning(disable : 4189) |
| 49 | #pragma warning(disable : 4206) |
| 50 | #pragma warning(disable : 4210) |
| 51 | #pragma warning(disable : 4232) |
| 52 | #pragma warning(disable : 4244) |
| 53 | #pragma warning(disable : 4305) |
| 54 | #pragma warning(disable : 4306) |
| 55 | #pragma warning(disable : 4702) |
| 56 | #pragma warning(disable : 4706) |
| 57 | #endif /* defined(_MSC_VER) */ |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 58 | |
| 59 | /* |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 60 | ** No support for loadable extensions in VxWorks. |
| 61 | */ |
drh | ada3f2b | 2015-03-23 21:32:50 +0000 | [diff] [blame] | 62 | #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 63 | # define SQLITE_OMIT_LOAD_EXTENSION 1 |
| 64 | #endif |
| 65 | |
| 66 | /* |
drh | 36f7dd3 | 2011-10-13 16:02:17 +0000 | [diff] [blame] | 67 | ** Enable large-file support for fopen() and friends on unix. |
| 68 | */ |
| 69 | #ifndef SQLITE_DISABLE_LFS |
| 70 | # define _LARGE_FILE 1 |
| 71 | # ifndef _FILE_OFFSET_BITS |
| 72 | # define _FILE_OFFSET_BITS 64 |
| 73 | # endif |
| 74 | # define _LARGEFILE_SOURCE 1 |
| 75 | #endif |
| 76 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 77 | #include <stdlib.h> |
| 78 | #include <string.h> |
| 79 | #include <stdio.h> |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 80 | #include <assert.h> |
drh | 1d482dd | 2004-05-31 18:23:07 +0000 | [diff] [blame] | 81 | #include "sqlite3.h" |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 82 | #if SQLITE_USER_AUTHENTICATION |
| 83 | # include "sqlite3userauth.h" |
| 84 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 85 | #include <ctype.h> |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 86 | #include <stdarg.h> |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 87 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 88 | #if !defined(_WIN32) && !defined(WIN32) |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 89 | # include <signal.h> |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 90 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 91 | # include <pwd.h> |
| 92 | # endif |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 93 | # include <unistd.h> |
| 94 | # include <sys/types.h> |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 95 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 96 | |
drh | 0ede9eb | 2015-01-10 16:49:23 +0000 | [diff] [blame] | 97 | #if HAVE_READLINE |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 98 | # include <readline/readline.h> |
| 99 | # include <readline/history.h> |
drh | 81d7fd1 | 2010-12-08 00:02:26 +0000 | [diff] [blame] | 100 | #endif |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 101 | |
drh | 0ede9eb | 2015-01-10 16:49:23 +0000 | [diff] [blame] | 102 | #if HAVE_EDITLINE |
drh | aaa21b4 | 2014-02-11 14:37:51 +0000 | [diff] [blame] | 103 | # include <editline/readline.h> |
| 104 | #endif |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 105 | |
| 106 | #if HAVE_EDITLINE || HAVE_READLINE |
| 107 | |
| 108 | # define shell_add_history(X) add_history(X) |
| 109 | # define shell_read_history(X) read_history(X) |
| 110 | # define shell_write_history(X) write_history(X) |
| 111 | # define shell_stifle_history(X) stifle_history(X) |
| 112 | # define shell_readline(X) readline(X) |
| 113 | |
| 114 | #elif HAVE_LINENOISE |
| 115 | |
| 116 | # include "linenoise.h" |
| 117 | # define shell_add_history(X) linenoiseHistoryAdd(X) |
| 118 | # define shell_read_history(X) linenoiseHistoryLoad(X) |
| 119 | # define shell_write_history(X) linenoiseHistorySave(X) |
| 120 | # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) |
| 121 | # define shell_readline(X) linenoise(X) |
| 122 | |
| 123 | #else |
| 124 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 125 | # define shell_read_history(X) |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 126 | # define shell_write_history(X) |
| 127 | # define shell_stifle_history(X) |
| 128 | |
| 129 | # define SHELL_USE_LOCAL_GETLINE 1 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 130 | #endif |
| 131 | |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 132 | |
adamd | 2e8464a | 2006-09-06 21:39:40 +0000 | [diff] [blame] | 133 | #if defined(_WIN32) || defined(WIN32) |
| 134 | # include <io.h> |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 135 | # include <fcntl.h> |
mistachkin | 073664d | 2015-06-17 18:57:37 +0000 | [diff] [blame] | 136 | # define isatty(h) _isatty(h) |
| 137 | # ifndef access |
| 138 | # define access(f,m) _access((f),(m)) |
| 139 | # endif |
| 140 | # undef popen |
| 141 | # define popen _popen |
| 142 | # undef pclose |
| 143 | # define pclose _pclose |
adamd | 2e8464a | 2006-09-06 21:39:40 +0000 | [diff] [blame] | 144 | #else |
mistachkin | 073664d | 2015-06-17 18:57:37 +0000 | [diff] [blame] | 145 | /* Make sure isatty() has a prototype. */ |
| 146 | extern int isatty(int); |
drh | 4328c8b | 2003-04-26 02:50:11 +0000 | [diff] [blame] | 147 | |
mistachkin | 073664d | 2015-06-17 18:57:37 +0000 | [diff] [blame] | 148 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 149 | /* popen and pclose are not C89 functions and so are |
| 150 | ** sometimes omitted from the <stdio.h> header */ |
| 151 | extern FILE *popen(const char*,const char*); |
| 152 | extern int pclose(FILE*); |
| 153 | # else |
| 154 | # define SQLITE_OMIT_POPEN 1 |
| 155 | # endif |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 156 | #endif |
drh | 53371f9 | 2013-07-25 17:07:03 +0000 | [diff] [blame] | 157 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 158 | #if defined(_WIN32_WCE) |
| 159 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() |
| 160 | * thus we always assume that we have a console. That can be |
| 161 | * overridden with the -batch command line option. |
| 162 | */ |
| 163 | #define isatty(x) 1 |
| 164 | #endif |
| 165 | |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 166 | /* ctype macros that work with signed characters */ |
| 167 | #define IsSpace(X) isspace((unsigned char)X) |
| 168 | #define IsDigit(X) isdigit((unsigned char)X) |
| 169 | #define ToLower(X) (char)tolower((unsigned char)X) |
| 170 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 171 | #if defined(_WIN32) || defined(WIN32) |
| 172 | #include <windows.h> |
| 173 | |
| 174 | /* string conversion routines only needed on Win32 */ |
| 175 | extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); |
| 176 | extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int); |
| 177 | extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int); |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 178 | extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 179 | #endif |
| 180 | |
drh | 047d453 | 2015-01-18 20:30:23 +0000 | [diff] [blame] | 181 | /* On Windows, we normally run with output mode of TEXT so that \n characters |
| 182 | ** are automatically translated into \r\n. However, this behavior needs |
| 183 | ** to be disabled in some cases (ex: when generating CSV output and when |
| 184 | ** rendering quoted strings that contain \n characters). The following |
| 185 | ** routines take care of that. |
| 186 | */ |
| 187 | #if defined(_WIN32) || defined(WIN32) |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 188 | static void setBinaryMode(FILE *file, int isOutput){ |
| 189 | if( isOutput ) fflush(file); |
| 190 | _setmode(_fileno(file), _O_BINARY); |
drh | 047d453 | 2015-01-18 20:30:23 +0000 | [diff] [blame] | 191 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 192 | static void setTextMode(FILE *file, int isOutput){ |
| 193 | if( isOutput ) fflush(file); |
| 194 | _setmode(_fileno(file), _O_TEXT); |
drh | 047d453 | 2015-01-18 20:30:23 +0000 | [diff] [blame] | 195 | } |
| 196 | #else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 197 | # define setBinaryMode(X,Y) |
| 198 | # define setTextMode(X,Y) |
drh | 047d453 | 2015-01-18 20:30:23 +0000 | [diff] [blame] | 199 | #endif |
| 200 | |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 201 | |
| 202 | /* True if the timer is enabled */ |
| 203 | static int enableTimer = 0; |
| 204 | |
| 205 | /* Return the current wall-clock time */ |
| 206 | static sqlite3_int64 timeOfDay(void){ |
| 207 | static sqlite3_vfs *clockVfs = 0; |
| 208 | sqlite3_int64 t; |
| 209 | if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
dan | 3fd415b | 2015-11-16 08:54:10 +0000 | [diff] [blame] | 210 | if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 211 | clockVfs->xCurrentTimeInt64(clockVfs, &t); |
| 212 | }else{ |
| 213 | double r; |
| 214 | clockVfs->xCurrentTime(clockVfs, &r); |
| 215 | t = (sqlite3_int64)(r*86400000.0); |
| 216 | } |
| 217 | return t; |
| 218 | } |
| 219 | |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 220 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 221 | #include <sys/time.h> |
| 222 | #include <sys/resource.h> |
| 223 | |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 224 | /* VxWorks does not support getrusage() as far as we can determine */ |
| 225 | #if defined(_WRS_KERNEL) || defined(__RTP__) |
| 226 | struct rusage { |
| 227 | struct timeval ru_utime; /* user CPU time used */ |
| 228 | struct timeval ru_stime; /* system CPU time used */ |
| 229 | }; |
| 230 | #define getrusage(A,B) memset(B,0,sizeof(*B)) |
| 231 | #endif |
| 232 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 233 | /* Saved resource information for the beginning of an operation */ |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 234 | static struct rusage sBegin; /* CPU time at start */ |
| 235 | static sqlite3_int64 iBegin; /* Wall-clock time at start */ |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 236 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 237 | /* |
| 238 | ** Begin timing an operation |
| 239 | */ |
| 240 | static void beginTimer(void){ |
| 241 | if( enableTimer ){ |
| 242 | getrusage(RUSAGE_SELF, &sBegin); |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 243 | iBegin = timeOfDay(); |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | /* Return the difference of two time_structs in seconds */ |
| 248 | static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 249 | return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 250 | (double)(pEnd->tv_sec - pStart->tv_sec); |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | ** Print the timing results. |
| 255 | */ |
| 256 | static void endTimer(void){ |
| 257 | if( enableTimer ){ |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 258 | sqlite3_int64 iEnd = timeOfDay(); |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 259 | struct rusage sEnd; |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 260 | getrusage(RUSAGE_SELF, &sEnd); |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 261 | printf("Run Time: real %.3f user %f sys %f\n", |
| 262 | (iEnd - iBegin)*0.001, |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 263 | timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), |
| 264 | timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); |
| 265 | } |
| 266 | } |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 267 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 268 | #define BEGIN_TIMER beginTimer() |
| 269 | #define END_TIMER endTimer() |
| 270 | #define HAS_TIMER 1 |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 271 | |
| 272 | #elif (defined(_WIN32) || defined(WIN32)) |
| 273 | |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 274 | /* Saved resource information for the beginning of an operation */ |
| 275 | static HANDLE hProcess; |
| 276 | static FILETIME ftKernelBegin; |
| 277 | static FILETIME ftUserBegin; |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 278 | static sqlite3_int64 ftWallBegin; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 279 | typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, |
| 280 | LPFILETIME, LPFILETIME); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 281 | static GETPROCTIMES getProcessTimesAddr = NULL; |
| 282 | |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 283 | /* |
| 284 | ** Check to see if we have timer support. Return 1 if necessary |
| 285 | ** support found (or found previously). |
| 286 | */ |
| 287 | static int hasTimer(void){ |
| 288 | if( getProcessTimesAddr ){ |
| 289 | return 1; |
| 290 | } else { |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 291 | /* GetProcessTimes() isn't supported in WIN95 and some other Windows |
| 292 | ** versions. See if the version we are running on has it, and if it |
| 293 | ** does, save off a pointer to it and the current process handle. |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 294 | */ |
| 295 | hProcess = GetCurrentProcess(); |
| 296 | if( hProcess ){ |
| 297 | HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); |
| 298 | if( NULL != hinstLib ){ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 299 | getProcessTimesAddr = |
| 300 | (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 301 | if( NULL != getProcessTimesAddr ){ |
| 302 | return 1; |
| 303 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 304 | FreeLibrary(hinstLib); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | ** Begin timing an operation |
| 313 | */ |
| 314 | static void beginTimer(void){ |
| 315 | if( enableTimer && getProcessTimesAddr ){ |
| 316 | FILETIME ftCreation, ftExit; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 317 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit, |
| 318 | &ftKernelBegin,&ftUserBegin); |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 319 | ftWallBegin = timeOfDay(); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | /* Return the difference of two FILETIME structs in seconds */ |
| 324 | static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ |
| 325 | sqlite_int64 i64Start = *((sqlite_int64 *) pStart); |
| 326 | sqlite_int64 i64End = *((sqlite_int64 *) pEnd); |
| 327 | return (double) ((i64End - i64Start) / 10000000.0); |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | ** Print the timing results. |
| 332 | */ |
| 333 | static void endTimer(void){ |
| 334 | if( enableTimer && getProcessTimesAddr){ |
| 335 | FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 336 | sqlite3_int64 ftWallEnd = timeOfDay(); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 337 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); |
drh | 4340831 | 2013-10-30 12:43:36 +0000 | [diff] [blame] | 338 | printf("Run Time: real %.3f user %f sys %f\n", |
| 339 | (ftWallEnd - ftWallBegin)*0.001, |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 340 | timeDiff(&ftUserBegin, &ftUserEnd), |
| 341 | timeDiff(&ftKernelBegin, &ftKernelEnd)); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | #define BEGIN_TIMER beginTimer() |
| 346 | #define END_TIMER endTimer() |
| 347 | #define HAS_TIMER hasTimer() |
| 348 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 349 | #else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 350 | #define BEGIN_TIMER |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 351 | #define END_TIMER |
| 352 | #define HAS_TIMER 0 |
| 353 | #endif |
| 354 | |
shane | c0688ea | 2009-03-05 03:48:06 +0000 | [diff] [blame] | 355 | /* |
| 356 | ** Used to prevent warnings about unused parameters |
| 357 | */ |
| 358 | #define UNUSED_PARAMETER(x) (void)(x) |
| 359 | |
drh | e91d16b | 2008-12-08 18:27:31 +0000 | [diff] [blame] | 360 | /* |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 361 | ** If the following flag is set, then command execution stops |
| 362 | ** at an error if we are not interactive. |
| 363 | */ |
| 364 | static int bail_on_error = 0; |
| 365 | |
| 366 | /* |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 367 | ** Threat stdin as an interactive input if the following variable |
| 368 | ** is true. Otherwise, assume stdin is connected to a file or pipe. |
| 369 | */ |
| 370 | static int stdin_is_interactive = 1; |
| 371 | |
| 372 | /* |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 373 | ** On Windows systems we have to know if standard output is a console |
| 374 | ** in order to translate UTF-8 into MBCS. The following variable is |
| 375 | ** true if translation is required. |
| 376 | */ |
| 377 | static int stdout_is_console = 1; |
| 378 | |
| 379 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 380 | ** The following is the open SQLite database. We make a pointer |
| 381 | ** to this database a static variable so that it can be accessed |
| 382 | ** by the SIGINT handler to interrupt database processing. |
| 383 | */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 384 | static sqlite3 *globalDb = 0; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 385 | |
| 386 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 387 | ** True if an interrupt (Control-C) has been received. |
| 388 | */ |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 389 | static volatile int seenInterrupt = 0; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 390 | |
| 391 | /* |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 392 | ** This is the name of our program. It is set in main(), used |
| 393 | ** in a number of other places, mostly for error messages. |
| 394 | */ |
| 395 | static char *Argv0; |
| 396 | |
| 397 | /* |
| 398 | ** Prompt strings. Initialized in main. Settable with |
| 399 | ** .prompt main continue |
| 400 | */ |
| 401 | static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ |
| 402 | static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ |
| 403 | |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 404 | /* |
mistachkin | 710b33b | 2016-01-03 18:59:28 +0000 | [diff] [blame] | 405 | ** Render output like fprintf(). Except, if the output is going to the |
| 406 | ** console and if this is running on a Windows machine, translate the |
| 407 | ** output from UTF-8 into MBCS. |
| 408 | */ |
| 409 | #if defined(_WIN32) || defined(WIN32) |
| 410 | void utf8_printf(FILE *out, const char *zFormat, ...){ |
| 411 | va_list ap; |
| 412 | va_start(ap, zFormat); |
| 413 | if( stdout_is_console && (out==stdout || out==stderr) ){ |
mistachkin | 710b33b | 2016-01-03 18:59:28 +0000 | [diff] [blame] | 414 | char *z1 = sqlite3_vmprintf(zFormat, ap); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 415 | char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0); |
mistachkin | 710b33b | 2016-01-03 18:59:28 +0000 | [diff] [blame] | 416 | sqlite3_free(z1); |
| 417 | fputs(z2, out); |
| 418 | sqlite3_free(z2); |
| 419 | }else{ |
| 420 | vfprintf(out, zFormat, ap); |
| 421 | } |
| 422 | va_end(ap); |
| 423 | } |
| 424 | #elif !defined(utf8_printf) |
| 425 | # define utf8_printf fprintf |
| 426 | #endif |
| 427 | |
| 428 | /* |
| 429 | ** Render output like fprintf(). This should not be used on anything that |
| 430 | ** includes string formatting (e.g. "%s"). |
| 431 | */ |
| 432 | #if !defined(raw_printf) |
| 433 | # define raw_printf fprintf |
| 434 | #endif |
| 435 | |
| 436 | /* |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 437 | ** Write I/O traces to the following stream. |
| 438 | */ |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 439 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 440 | static FILE *iotrace = 0; |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 441 | #endif |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 442 | |
| 443 | /* |
| 444 | ** This routine works like printf in that its first argument is a |
| 445 | ** format string and subsequent arguments are values to be substituted |
| 446 | ** in place of % fields. The result of formatting this string |
| 447 | ** is written to iotrace. |
| 448 | */ |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 449 | #ifdef SQLITE_ENABLE_IOTRACE |
mistachkin | 9871a93 | 2015-03-27 00:21:52 +0000 | [diff] [blame] | 450 | static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 451 | va_list ap; |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 452 | char *z; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 453 | if( iotrace==0 ) return; |
| 454 | va_start(ap, zFormat); |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 455 | z = sqlite3_vmprintf(zFormat, ap); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 456 | va_end(ap); |
mistachkin | 710b33b | 2016-01-03 18:59:28 +0000 | [diff] [blame] | 457 | utf8_printf(iotrace, "%s", z); |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 458 | sqlite3_free(z); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 459 | } |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 460 | #endif |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 461 | |
drh | 6887e8f | 2017-04-17 13:18:42 +0000 | [diff] [blame] | 462 | /* |
| 463 | ** Output string zUtf to stream pOut as w characters. If w is negative, |
| 464 | ** then right-justify the text. W is the width in UTF-8 characters, not |
| 465 | ** in bytes. This is different from the %*.*s specification in printf |
| 466 | ** since with %*.*s the width is measured in bytes, not characters. |
| 467 | */ |
| 468 | static void utf8_width_print(FILE *pOut, int w, const char *zUtf){ |
| 469 | int i; |
| 470 | int n; |
| 471 | int aw = w<0 ? -w : w; |
| 472 | char zBuf[1000]; |
drh | f8a2e8c | 2017-05-06 17:12:52 +0000 | [diff] [blame] | 473 | if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3; |
drh | 6887e8f | 2017-04-17 13:18:42 +0000 | [diff] [blame] | 474 | for(i=n=0; zUtf[i]; i++){ |
| 475 | if( (zUtf[i]&0xc0)!=0x80 ){ |
| 476 | n++; |
| 477 | if( n==aw ){ |
| 478 | do{ i++; }while( (zUtf[i]&0xc0)==0x80 ); |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | if( n>=aw ){ |
| 484 | utf8_printf(pOut, "%.*s", i, zUtf); |
| 485 | }else if( w<0 ){ |
| 486 | utf8_printf(pOut, "%*s%s", aw-n, "", zUtf); |
| 487 | }else{ |
| 488 | utf8_printf(pOut, "%s%*s", zUtf, aw-n, ""); |
| 489 | } |
| 490 | } |
| 491 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 492 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 493 | /* |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 494 | ** Determines if a string is a number of not. |
| 495 | */ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 496 | static int isNumber(const char *z, int *realnum){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 497 | if( *z=='-' || *z=='+' ) z++; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 498 | if( !IsDigit(*z) ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 499 | return 0; |
| 500 | } |
| 501 | z++; |
| 502 | if( realnum ) *realnum = 0; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 503 | while( IsDigit(*z) ){ z++; } |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 504 | if( *z=='.' ){ |
| 505 | z++; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 506 | if( !IsDigit(*z) ) return 0; |
| 507 | while( IsDigit(*z) ){ z++; } |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 508 | if( realnum ) *realnum = 1; |
| 509 | } |
| 510 | if( *z=='e' || *z=='E' ){ |
| 511 | z++; |
| 512 | if( *z=='+' || *z=='-' ) z++; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 513 | if( !IsDigit(*z) ) return 0; |
| 514 | while( IsDigit(*z) ){ z++; } |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 515 | if( realnum ) *realnum = 1; |
| 516 | } |
| 517 | return *z==0; |
| 518 | } |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 519 | |
| 520 | /* |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 521 | ** Compute a string length that is limited to what can be stored in |
| 522 | ** lower 30 bits of a 32-bit signed integer. |
| 523 | */ |
| 524 | static int strlen30(const char *z){ |
| 525 | const char *z2 = z; |
| 526 | while( *z2 ){ z2++; } |
| 527 | return 0x3fffffff & (int)(z2 - z); |
| 528 | } |
| 529 | |
| 530 | /* |
drh | 64bf76d | 2017-06-05 12:29:26 +0000 | [diff] [blame] | 531 | ** Return the length of a string in characters. Multibyte UTF8 characters |
| 532 | ** count as a single character. |
| 533 | */ |
| 534 | static int strlenChar(const char *z){ |
| 535 | int n = 0; |
| 536 | while( *z ){ |
| 537 | if( (0xc0&*(z++))!=0x80 ) n++; |
| 538 | } |
| 539 | return n; |
| 540 | } |
| 541 | |
| 542 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 543 | ** This routine reads a line of text from FILE in, stores |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 544 | ** the text in memory obtained from malloc() and returns a pointer |
| 545 | ** to the text. NULL is returned at end of file, or if malloc() |
| 546 | ** fails. |
| 547 | ** |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 548 | ** If zLine is not NULL then it is a malloced buffer returned from |
| 549 | ** a previous call to this routine that may be reused. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 550 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 551 | static char *local_getline(char *zLine, FILE *in){ |
| 552 | int nLine = zLine==0 ? 0 : 100; |
| 553 | int n = 0; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 554 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 555 | while( 1 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 556 | if( n+100>nLine ){ |
| 557 | nLine = nLine*2 + 100; |
| 558 | zLine = realloc(zLine, nLine); |
| 559 | if( zLine==0 ) return 0; |
| 560 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 561 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 562 | if( n==0 ){ |
| 563 | free(zLine); |
| 564 | return 0; |
| 565 | } |
| 566 | zLine[n] = 0; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 567 | break; |
| 568 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 569 | while( zLine[n] ) n++; |
| 570 | if( n>0 && zLine[n-1]=='\n' ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 571 | n--; |
shaneh | 13b3602 | 2009-12-17 21:07:15 +0000 | [diff] [blame] | 572 | if( n>0 && zLine[n-1]=='\r' ) n--; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 573 | zLine[n] = 0; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 574 | break; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 577 | #if defined(_WIN32) || defined(WIN32) |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 578 | /* For interactive input on Windows systems, translate the |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 579 | ** multi-byte characterset characters into UTF-8. */ |
drh | fc8b40f | 2016-09-07 10:10:18 +0000 | [diff] [blame] | 580 | if( stdin_is_interactive && in==stdin ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 581 | char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 582 | if( zTrans ){ |
| 583 | int nTrans = strlen30(zTrans)+1; |
| 584 | if( nTrans>nLine ){ |
| 585 | zLine = realloc(zLine, nTrans); |
| 586 | if( zLine==0 ){ |
| 587 | sqlite3_free(zTrans); |
| 588 | return 0; |
| 589 | } |
| 590 | } |
| 591 | memcpy(zLine, zTrans, nTrans); |
| 592 | sqlite3_free(zTrans); |
| 593 | } |
| 594 | } |
| 595 | #endif /* defined(_WIN32) || defined(WIN32) */ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 596 | return zLine; |
| 597 | } |
| 598 | |
| 599 | /* |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 600 | ** Retrieve a single line of input text. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 601 | ** |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 602 | ** If in==0 then read from standard input and prompt before each line. |
| 603 | ** If isContinuation is true, then a continuation prompt is appropriate. |
| 604 | ** If isContinuation is zero, then the main prompt should be used. |
| 605 | ** |
| 606 | ** If zPrior is not NULL then it is a buffer from a prior call to this |
| 607 | ** routine that can be reused. |
| 608 | ** |
| 609 | ** The result is stored in space obtained from malloc() and must either |
| 610 | ** be freed by the caller or else passed back into this routine via the |
| 611 | ** zPrior argument for reuse. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 612 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 613 | static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 614 | char *zPrompt; |
| 615 | char *zResult; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 616 | if( in!=0 ){ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 617 | zResult = local_getline(zPrior, in); |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 618 | }else{ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 619 | zPrompt = isContinuation ? continuePrompt : mainPrompt; |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 620 | #if SHELL_USE_LOCAL_GETLINE |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 621 | printf("%s", zPrompt); |
| 622 | fflush(stdout); |
| 623 | zResult = local_getline(zPrior, stdin); |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 624 | #else |
| 625 | free(zPrior); |
| 626 | zResult = shell_readline(zPrompt); |
| 627 | if( zResult && *zResult ) shell_add_history(zResult); |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 628 | #endif |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 629 | } |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 630 | return zResult; |
| 631 | } |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 632 | /* |
| 633 | ** A variable length string to which one can append text. |
| 634 | */ |
| 635 | typedef struct ShellText ShellText; |
| 636 | struct ShellText { |
| 637 | char *z; |
| 638 | int n; |
| 639 | int nAlloc; |
| 640 | }; |
| 641 | |
| 642 | /* |
| 643 | ** Initialize and destroy a ShellText object |
| 644 | */ |
| 645 | static void initText(ShellText *p){ |
| 646 | memset(p, 0, sizeof(*p)); |
| 647 | } |
| 648 | static void freeText(ShellText *p){ |
| 649 | free(p->z); |
| 650 | initText(p); |
| 651 | } |
| 652 | |
| 653 | /* zIn is either a pointer to a NULL-terminated string in memory obtained |
| 654 | ** from malloc(), or a NULL pointer. The string pointed to by zAppend is |
| 655 | ** added to zIn, and the result returned in memory obtained from malloc(). |
| 656 | ** zIn, if it was not NULL, is freed. |
| 657 | ** |
| 658 | ** If the third argument, quote, is not '\0', then it is used as a |
| 659 | ** quote character for zAppend. |
| 660 | */ |
| 661 | static void appendText(ShellText *p, char const *zAppend, char quote){ |
| 662 | int len; |
| 663 | int i; |
| 664 | int nAppend = strlen30(zAppend); |
| 665 | |
| 666 | len = nAppend+p->n+1; |
| 667 | if( quote ){ |
| 668 | len += 2; |
| 669 | for(i=0; i<nAppend; i++){ |
| 670 | if( zAppend[i]==quote ) len++; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | if( p->n+len>=p->nAlloc ){ |
| 675 | p->nAlloc = p->nAlloc*2 + len + 20; |
| 676 | p->z = realloc(p->z, p->nAlloc); |
| 677 | if( p->z==0 ){ |
| 678 | memset(p, 0, sizeof(*p)); |
| 679 | return; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | if( quote ){ |
| 684 | char *zCsr = p->z+p->n; |
| 685 | *zCsr++ = quote; |
| 686 | for(i=0; i<nAppend; i++){ |
| 687 | *zCsr++ = zAppend[i]; |
| 688 | if( zAppend[i]==quote ) *zCsr++ = quote; |
| 689 | } |
| 690 | *zCsr++ = quote; |
| 691 | p->n = (int)(zCsr - p->z); |
| 692 | *zCsr = '\0'; |
| 693 | }else{ |
| 694 | memcpy(p->z+p->n, zAppend, nAppend); |
| 695 | p->n += nAppend; |
| 696 | p->z[p->n] = '\0'; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | ** Attempt to determine if identifier zName needs to be quoted, either |
| 702 | ** because it contains non-alphanumeric characters, or because it is an |
| 703 | ** SQLite keyword. Be conservative in this estimate: When in doubt assume |
| 704 | ** that quoting is required. |
| 705 | ** |
| 706 | ** Return '"' if quoting is required. Return 0 if no quoting is required. |
| 707 | */ |
| 708 | static char quoteChar(const char *zName){ |
| 709 | /* All SQLite keywords, in alphabetical order */ |
| 710 | static const char *azKeywords[] = { |
| 711 | "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS", |
| 712 | "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY", |
| 713 | "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT", |
| 714 | "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE", |
| 715 | "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE", |
| 716 | "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH", |
| 717 | "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN", |
| 718 | "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF", |
| 719 | "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", |
| 720 | "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY", |
| 721 | "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL", |
| 722 | "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA", |
| 723 | "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP", |
| 724 | "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT", |
| 725 | "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP", |
| 726 | "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE", |
| 727 | "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE", |
| 728 | "WITH", "WITHOUT", |
| 729 | }; |
| 730 | int i, lwr, upr, mid, c; |
| 731 | if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"'; |
| 732 | for(i=0; zName[i]; i++){ |
| 733 | if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"'; |
| 734 | } |
| 735 | lwr = 0; |
| 736 | upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1; |
| 737 | while( lwr<=upr ){ |
| 738 | mid = (lwr+upr)/2; |
| 739 | c = sqlite3_stricmp(azKeywords[mid], zName); |
| 740 | if( c==0 ) return '"'; |
| 741 | if( c<0 ){ |
| 742 | lwr = mid+1; |
| 743 | }else{ |
| 744 | upr = mid-1; |
| 745 | } |
| 746 | } |
| 747 | return 0; |
| 748 | } |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 749 | |
drh | 0942559 | 2017-06-15 16:56:05 +0000 | [diff] [blame] | 750 | /* |
| 751 | ** SQL function: shell_add_schema(S,X) |
| 752 | ** |
| 753 | ** Add the schema name X to the CREATE statement in S and return the result. |
| 754 | ** Examples: |
| 755 | ** |
| 756 | ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x); |
| 757 | ** |
| 758 | ** Also works on |
| 759 | ** |
| 760 | ** CREATE INDEX |
| 761 | ** CREATE UNIQUE INDEX |
| 762 | ** CREATE VIEW |
| 763 | ** CREATE TRIGGER |
| 764 | ** CREATE VIRTUAL TABLE |
| 765 | ** |
| 766 | ** This UDF is used by the .schema command to insert the schema name of |
| 767 | ** attached databases into the middle of the sqlite_master.sql field. |
| 768 | */ |
| 769 | static void shellAddSchemaName( |
| 770 | sqlite3_context *pCtx, |
| 771 | int nVal, |
| 772 | sqlite3_value **apVal |
| 773 | ){ |
| 774 | static const char *aPrefix[] = { |
| 775 | "TABLE", |
| 776 | "INDEX", |
| 777 | "UNIQUE INDEX", |
| 778 | "VIEW", |
| 779 | "TRIGGER", |
| 780 | "VIRTUAL TABLE" |
| 781 | }; |
| 782 | int i = 0; |
| 783 | const char *zIn = (const char*)sqlite3_value_text(apVal[0]); |
| 784 | const char *zSchema = (const char*)sqlite3_value_text(apVal[1]); |
| 785 | assert( nVal==2 ); |
| 786 | if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){ |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 787 | for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){ |
drh | 0942559 | 2017-06-15 16:56:05 +0000 | [diff] [blame] | 788 | int n = strlen30(aPrefix[i]); |
| 789 | if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){ |
| 790 | char cQuote = quoteChar(zSchema); |
| 791 | char *z; |
| 792 | if( cQuote ){ |
| 793 | z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8); |
| 794 | }else{ |
| 795 | z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8); |
| 796 | } |
| 797 | sqlite3_result_text(pCtx, z, -1, sqlite3_free); |
| 798 | return; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | sqlite3_result_value(pCtx, apVal[0]); |
| 803 | } |
| 804 | |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 805 | /* |
| 806 | ** The source code for several run-time loadable extensions is inserted |
| 807 | ** below by the ../tool/mkshellc.tcl script. Before processing that included |
| 808 | ** code, we need to override some macros to make the included program code |
| 809 | ** work here in the middle of this regular program. |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 810 | */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 811 | #define SQLITE_EXTENSION_INIT1 |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 812 | #define SQLITE_EXTENSION_INIT2(X) (void)(X) |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 813 | |
| 814 | /************************* Begin ../ext/misc/shathree.c ******************/ |
| 815 | /* |
| 816 | ** 2017-03-08 |
| 817 | ** |
| 818 | ** The author disclaims copyright to this source code. In place of |
| 819 | ** a legal notice, here is a blessing: |
| 820 | ** |
| 821 | ** May you do good and not evil. |
| 822 | ** May you find forgiveness for yourself and forgive others. |
| 823 | ** May you share freely, never taking more than you give. |
| 824 | ** |
| 825 | ****************************************************************************** |
| 826 | ** |
| 827 | ** This SQLite extension implements a functions that compute SHA1 hashes. |
| 828 | ** Two SQL functions are implemented: |
| 829 | ** |
| 830 | ** sha3(X,SIZE) |
| 831 | ** sha3_query(Y,SIZE) |
| 832 | ** |
| 833 | ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if |
| 834 | ** X is NULL. |
| 835 | ** |
| 836 | ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y |
| 837 | ** and returns a hash of their results. |
| 838 | ** |
| 839 | ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm |
| 840 | ** is used. If SIZE is included it must be one of the integers 224, 256, |
| 841 | ** 384, or 512, to determine SHA3 hash variant that is computed. |
| 842 | */ |
| 843 | SQLITE_EXTENSION_INIT1 |
| 844 | #include <assert.h> |
| 845 | #include <string.h> |
| 846 | #include <stdarg.h> |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 847 | typedef sqlite3_uint64 u64; |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 848 | |
| 849 | /****************************************************************************** |
| 850 | ** The Hash Engine |
| 851 | */ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 852 | /* |
| 853 | ** Macros to determine whether the machine is big or little endian, |
| 854 | ** and whether or not that determination is run-time or compile-time. |
| 855 | ** |
| 856 | ** For best performance, an attempt is made to guess at the byte-order |
| 857 | ** using C-preprocessor macros. If that is unsuccessful, or if |
| 858 | ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined |
| 859 | ** at run-time. |
| 860 | */ |
| 861 | #ifndef SHA3_BYTEORDER |
| 862 | # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \ |
| 863 | defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ |
| 864 | defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ |
| 865 | defined(__arm__) |
| 866 | # define SHA3_BYTEORDER 1234 |
| 867 | # elif defined(sparc) || defined(__ppc__) |
| 868 | # define SHA3_BYTEORDER 4321 |
| 869 | # else |
| 870 | # define SHA3_BYTEORDER 0 |
| 871 | # endif |
| 872 | #endif |
| 873 | |
| 874 | |
| 875 | /* |
| 876 | ** State structure for a SHA3 hash in progress |
| 877 | */ |
| 878 | typedef struct SHA3Context SHA3Context; |
| 879 | struct SHA3Context { |
| 880 | union { |
| 881 | u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */ |
| 882 | unsigned char x[1600]; /* ... or 1600 bytes */ |
| 883 | } u; |
| 884 | unsigned nRate; /* Bytes of input accepted per Keccak iteration */ |
| 885 | unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */ |
| 886 | unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */ |
| 887 | }; |
| 888 | |
| 889 | /* |
| 890 | ** A single step of the Keccak mixing function for a 1600-bit state |
| 891 | */ |
| 892 | static void KeccakF1600Step(SHA3Context *p){ |
| 893 | int i; |
| 894 | u64 B0, B1, B2, B3, B4; |
| 895 | u64 C0, C1, C2, C3, C4; |
| 896 | u64 D0, D1, D2, D3, D4; |
| 897 | static const u64 RC[] = { |
| 898 | 0x0000000000000001ULL, 0x0000000000008082ULL, |
| 899 | 0x800000000000808aULL, 0x8000000080008000ULL, |
| 900 | 0x000000000000808bULL, 0x0000000080000001ULL, |
| 901 | 0x8000000080008081ULL, 0x8000000000008009ULL, |
| 902 | 0x000000000000008aULL, 0x0000000000000088ULL, |
| 903 | 0x0000000080008009ULL, 0x000000008000000aULL, |
| 904 | 0x000000008000808bULL, 0x800000000000008bULL, |
| 905 | 0x8000000000008089ULL, 0x8000000000008003ULL, |
| 906 | 0x8000000000008002ULL, 0x8000000000000080ULL, |
| 907 | 0x000000000000800aULL, 0x800000008000000aULL, |
| 908 | 0x8000000080008081ULL, 0x8000000000008080ULL, |
| 909 | 0x0000000080000001ULL, 0x8000000080008008ULL |
| 910 | }; |
| 911 | # define A00 (p->u.s[0]) |
| 912 | # define A01 (p->u.s[1]) |
| 913 | # define A02 (p->u.s[2]) |
| 914 | # define A03 (p->u.s[3]) |
| 915 | # define A04 (p->u.s[4]) |
| 916 | # define A10 (p->u.s[5]) |
| 917 | # define A11 (p->u.s[6]) |
| 918 | # define A12 (p->u.s[7]) |
| 919 | # define A13 (p->u.s[8]) |
| 920 | # define A14 (p->u.s[9]) |
| 921 | # define A20 (p->u.s[10]) |
| 922 | # define A21 (p->u.s[11]) |
| 923 | # define A22 (p->u.s[12]) |
| 924 | # define A23 (p->u.s[13]) |
| 925 | # define A24 (p->u.s[14]) |
| 926 | # define A30 (p->u.s[15]) |
| 927 | # define A31 (p->u.s[16]) |
| 928 | # define A32 (p->u.s[17]) |
| 929 | # define A33 (p->u.s[18]) |
| 930 | # define A34 (p->u.s[19]) |
| 931 | # define A40 (p->u.s[20]) |
| 932 | # define A41 (p->u.s[21]) |
| 933 | # define A42 (p->u.s[22]) |
| 934 | # define A43 (p->u.s[23]) |
| 935 | # define A44 (p->u.s[24]) |
| 936 | # define ROL64(a,x) ((a<<x)|(a>>(64-x))) |
| 937 | |
| 938 | for(i=0; i<24; i+=4){ |
| 939 | C0 = A00^A10^A20^A30^A40; |
| 940 | C1 = A01^A11^A21^A31^A41; |
| 941 | C2 = A02^A12^A22^A32^A42; |
| 942 | C3 = A03^A13^A23^A33^A43; |
| 943 | C4 = A04^A14^A24^A34^A44; |
| 944 | D0 = C4^ROL64(C1, 1); |
| 945 | D1 = C0^ROL64(C2, 1); |
| 946 | D2 = C1^ROL64(C3, 1); |
| 947 | D3 = C2^ROL64(C4, 1); |
| 948 | D4 = C3^ROL64(C0, 1); |
| 949 | |
| 950 | B0 = (A00^D0); |
| 951 | B1 = ROL64((A11^D1), 44); |
| 952 | B2 = ROL64((A22^D2), 43); |
| 953 | B3 = ROL64((A33^D3), 21); |
| 954 | B4 = ROL64((A44^D4), 14); |
| 955 | A00 = B0 ^((~B1)& B2 ); |
| 956 | A00 ^= RC[i]; |
| 957 | A11 = B1 ^((~B2)& B3 ); |
| 958 | A22 = B2 ^((~B3)& B4 ); |
| 959 | A33 = B3 ^((~B4)& B0 ); |
| 960 | A44 = B4 ^((~B0)& B1 ); |
| 961 | |
| 962 | B2 = ROL64((A20^D0), 3); |
| 963 | B3 = ROL64((A31^D1), 45); |
| 964 | B4 = ROL64((A42^D2), 61); |
| 965 | B0 = ROL64((A03^D3), 28); |
| 966 | B1 = ROL64((A14^D4), 20); |
| 967 | A20 = B0 ^((~B1)& B2 ); |
| 968 | A31 = B1 ^((~B2)& B3 ); |
| 969 | A42 = B2 ^((~B3)& B4 ); |
| 970 | A03 = B3 ^((~B4)& B0 ); |
| 971 | A14 = B4 ^((~B0)& B1 ); |
| 972 | |
| 973 | B4 = ROL64((A40^D0), 18); |
| 974 | B0 = ROL64((A01^D1), 1); |
| 975 | B1 = ROL64((A12^D2), 6); |
| 976 | B2 = ROL64((A23^D3), 25); |
| 977 | B3 = ROL64((A34^D4), 8); |
| 978 | A40 = B0 ^((~B1)& B2 ); |
| 979 | A01 = B1 ^((~B2)& B3 ); |
| 980 | A12 = B2 ^((~B3)& B4 ); |
| 981 | A23 = B3 ^((~B4)& B0 ); |
| 982 | A34 = B4 ^((~B0)& B1 ); |
| 983 | |
| 984 | B1 = ROL64((A10^D0), 36); |
| 985 | B2 = ROL64((A21^D1), 10); |
| 986 | B3 = ROL64((A32^D2), 15); |
| 987 | B4 = ROL64((A43^D3), 56); |
| 988 | B0 = ROL64((A04^D4), 27); |
| 989 | A10 = B0 ^((~B1)& B2 ); |
| 990 | A21 = B1 ^((~B2)& B3 ); |
| 991 | A32 = B2 ^((~B3)& B4 ); |
| 992 | A43 = B3 ^((~B4)& B0 ); |
| 993 | A04 = B4 ^((~B0)& B1 ); |
| 994 | |
| 995 | B3 = ROL64((A30^D0), 41); |
| 996 | B4 = ROL64((A41^D1), 2); |
| 997 | B0 = ROL64((A02^D2), 62); |
| 998 | B1 = ROL64((A13^D3), 55); |
| 999 | B2 = ROL64((A24^D4), 39); |
| 1000 | A30 = B0 ^((~B1)& B2 ); |
| 1001 | A41 = B1 ^((~B2)& B3 ); |
| 1002 | A02 = B2 ^((~B3)& B4 ); |
| 1003 | A13 = B3 ^((~B4)& B0 ); |
| 1004 | A24 = B4 ^((~B0)& B1 ); |
| 1005 | |
| 1006 | C0 = A00^A20^A40^A10^A30; |
| 1007 | C1 = A11^A31^A01^A21^A41; |
| 1008 | C2 = A22^A42^A12^A32^A02; |
| 1009 | C3 = A33^A03^A23^A43^A13; |
| 1010 | C4 = A44^A14^A34^A04^A24; |
| 1011 | D0 = C4^ROL64(C1, 1); |
| 1012 | D1 = C0^ROL64(C2, 1); |
| 1013 | D2 = C1^ROL64(C3, 1); |
| 1014 | D3 = C2^ROL64(C4, 1); |
| 1015 | D4 = C3^ROL64(C0, 1); |
| 1016 | |
| 1017 | B0 = (A00^D0); |
| 1018 | B1 = ROL64((A31^D1), 44); |
| 1019 | B2 = ROL64((A12^D2), 43); |
| 1020 | B3 = ROL64((A43^D3), 21); |
| 1021 | B4 = ROL64((A24^D4), 14); |
| 1022 | A00 = B0 ^((~B1)& B2 ); |
| 1023 | A00 ^= RC[i+1]; |
| 1024 | A31 = B1 ^((~B2)& B3 ); |
| 1025 | A12 = B2 ^((~B3)& B4 ); |
| 1026 | A43 = B3 ^((~B4)& B0 ); |
| 1027 | A24 = B4 ^((~B0)& B1 ); |
| 1028 | |
| 1029 | B2 = ROL64((A40^D0), 3); |
| 1030 | B3 = ROL64((A21^D1), 45); |
| 1031 | B4 = ROL64((A02^D2), 61); |
| 1032 | B0 = ROL64((A33^D3), 28); |
| 1033 | B1 = ROL64((A14^D4), 20); |
| 1034 | A40 = B0 ^((~B1)& B2 ); |
| 1035 | A21 = B1 ^((~B2)& B3 ); |
| 1036 | A02 = B2 ^((~B3)& B4 ); |
| 1037 | A33 = B3 ^((~B4)& B0 ); |
| 1038 | A14 = B4 ^((~B0)& B1 ); |
| 1039 | |
| 1040 | B4 = ROL64((A30^D0), 18); |
| 1041 | B0 = ROL64((A11^D1), 1); |
| 1042 | B1 = ROL64((A42^D2), 6); |
| 1043 | B2 = ROL64((A23^D3), 25); |
| 1044 | B3 = ROL64((A04^D4), 8); |
| 1045 | A30 = B0 ^((~B1)& B2 ); |
| 1046 | A11 = B1 ^((~B2)& B3 ); |
| 1047 | A42 = B2 ^((~B3)& B4 ); |
| 1048 | A23 = B3 ^((~B4)& B0 ); |
| 1049 | A04 = B4 ^((~B0)& B1 ); |
| 1050 | |
| 1051 | B1 = ROL64((A20^D0), 36); |
| 1052 | B2 = ROL64((A01^D1), 10); |
| 1053 | B3 = ROL64((A32^D2), 15); |
| 1054 | B4 = ROL64((A13^D3), 56); |
| 1055 | B0 = ROL64((A44^D4), 27); |
| 1056 | A20 = B0 ^((~B1)& B2 ); |
| 1057 | A01 = B1 ^((~B2)& B3 ); |
| 1058 | A32 = B2 ^((~B3)& B4 ); |
| 1059 | A13 = B3 ^((~B4)& B0 ); |
| 1060 | A44 = B4 ^((~B0)& B1 ); |
| 1061 | |
| 1062 | B3 = ROL64((A10^D0), 41); |
| 1063 | B4 = ROL64((A41^D1), 2); |
| 1064 | B0 = ROL64((A22^D2), 62); |
| 1065 | B1 = ROL64((A03^D3), 55); |
| 1066 | B2 = ROL64((A34^D4), 39); |
| 1067 | A10 = B0 ^((~B1)& B2 ); |
| 1068 | A41 = B1 ^((~B2)& B3 ); |
| 1069 | A22 = B2 ^((~B3)& B4 ); |
| 1070 | A03 = B3 ^((~B4)& B0 ); |
| 1071 | A34 = B4 ^((~B0)& B1 ); |
| 1072 | |
| 1073 | C0 = A00^A40^A30^A20^A10; |
| 1074 | C1 = A31^A21^A11^A01^A41; |
| 1075 | C2 = A12^A02^A42^A32^A22; |
| 1076 | C3 = A43^A33^A23^A13^A03; |
| 1077 | C4 = A24^A14^A04^A44^A34; |
| 1078 | D0 = C4^ROL64(C1, 1); |
| 1079 | D1 = C0^ROL64(C2, 1); |
| 1080 | D2 = C1^ROL64(C3, 1); |
| 1081 | D3 = C2^ROL64(C4, 1); |
| 1082 | D4 = C3^ROL64(C0, 1); |
| 1083 | |
| 1084 | B0 = (A00^D0); |
| 1085 | B1 = ROL64((A21^D1), 44); |
| 1086 | B2 = ROL64((A42^D2), 43); |
| 1087 | B3 = ROL64((A13^D3), 21); |
| 1088 | B4 = ROL64((A34^D4), 14); |
| 1089 | A00 = B0 ^((~B1)& B2 ); |
| 1090 | A00 ^= RC[i+2]; |
| 1091 | A21 = B1 ^((~B2)& B3 ); |
| 1092 | A42 = B2 ^((~B3)& B4 ); |
| 1093 | A13 = B3 ^((~B4)& B0 ); |
| 1094 | A34 = B4 ^((~B0)& B1 ); |
| 1095 | |
| 1096 | B2 = ROL64((A30^D0), 3); |
| 1097 | B3 = ROL64((A01^D1), 45); |
| 1098 | B4 = ROL64((A22^D2), 61); |
| 1099 | B0 = ROL64((A43^D3), 28); |
| 1100 | B1 = ROL64((A14^D4), 20); |
| 1101 | A30 = B0 ^((~B1)& B2 ); |
| 1102 | A01 = B1 ^((~B2)& B3 ); |
| 1103 | A22 = B2 ^((~B3)& B4 ); |
| 1104 | A43 = B3 ^((~B4)& B0 ); |
| 1105 | A14 = B4 ^((~B0)& B1 ); |
| 1106 | |
| 1107 | B4 = ROL64((A10^D0), 18); |
| 1108 | B0 = ROL64((A31^D1), 1); |
| 1109 | B1 = ROL64((A02^D2), 6); |
| 1110 | B2 = ROL64((A23^D3), 25); |
| 1111 | B3 = ROL64((A44^D4), 8); |
| 1112 | A10 = B0 ^((~B1)& B2 ); |
| 1113 | A31 = B1 ^((~B2)& B3 ); |
| 1114 | A02 = B2 ^((~B3)& B4 ); |
| 1115 | A23 = B3 ^((~B4)& B0 ); |
| 1116 | A44 = B4 ^((~B0)& B1 ); |
| 1117 | |
| 1118 | B1 = ROL64((A40^D0), 36); |
| 1119 | B2 = ROL64((A11^D1), 10); |
| 1120 | B3 = ROL64((A32^D2), 15); |
| 1121 | B4 = ROL64((A03^D3), 56); |
| 1122 | B0 = ROL64((A24^D4), 27); |
| 1123 | A40 = B0 ^((~B1)& B2 ); |
| 1124 | A11 = B1 ^((~B2)& B3 ); |
| 1125 | A32 = B2 ^((~B3)& B4 ); |
| 1126 | A03 = B3 ^((~B4)& B0 ); |
| 1127 | A24 = B4 ^((~B0)& B1 ); |
| 1128 | |
| 1129 | B3 = ROL64((A20^D0), 41); |
| 1130 | B4 = ROL64((A41^D1), 2); |
| 1131 | B0 = ROL64((A12^D2), 62); |
| 1132 | B1 = ROL64((A33^D3), 55); |
| 1133 | B2 = ROL64((A04^D4), 39); |
| 1134 | A20 = B0 ^((~B1)& B2 ); |
| 1135 | A41 = B1 ^((~B2)& B3 ); |
| 1136 | A12 = B2 ^((~B3)& B4 ); |
| 1137 | A33 = B3 ^((~B4)& B0 ); |
| 1138 | A04 = B4 ^((~B0)& B1 ); |
| 1139 | |
| 1140 | C0 = A00^A30^A10^A40^A20; |
| 1141 | C1 = A21^A01^A31^A11^A41; |
| 1142 | C2 = A42^A22^A02^A32^A12; |
| 1143 | C3 = A13^A43^A23^A03^A33; |
| 1144 | C4 = A34^A14^A44^A24^A04; |
| 1145 | D0 = C4^ROL64(C1, 1); |
| 1146 | D1 = C0^ROL64(C2, 1); |
| 1147 | D2 = C1^ROL64(C3, 1); |
| 1148 | D3 = C2^ROL64(C4, 1); |
| 1149 | D4 = C3^ROL64(C0, 1); |
| 1150 | |
| 1151 | B0 = (A00^D0); |
| 1152 | B1 = ROL64((A01^D1), 44); |
| 1153 | B2 = ROL64((A02^D2), 43); |
| 1154 | B3 = ROL64((A03^D3), 21); |
| 1155 | B4 = ROL64((A04^D4), 14); |
| 1156 | A00 = B0 ^((~B1)& B2 ); |
| 1157 | A00 ^= RC[i+3]; |
| 1158 | A01 = B1 ^((~B2)& B3 ); |
| 1159 | A02 = B2 ^((~B3)& B4 ); |
| 1160 | A03 = B3 ^((~B4)& B0 ); |
| 1161 | A04 = B4 ^((~B0)& B1 ); |
| 1162 | |
| 1163 | B2 = ROL64((A10^D0), 3); |
| 1164 | B3 = ROL64((A11^D1), 45); |
| 1165 | B4 = ROL64((A12^D2), 61); |
| 1166 | B0 = ROL64((A13^D3), 28); |
| 1167 | B1 = ROL64((A14^D4), 20); |
| 1168 | A10 = B0 ^((~B1)& B2 ); |
| 1169 | A11 = B1 ^((~B2)& B3 ); |
| 1170 | A12 = B2 ^((~B3)& B4 ); |
| 1171 | A13 = B3 ^((~B4)& B0 ); |
| 1172 | A14 = B4 ^((~B0)& B1 ); |
| 1173 | |
| 1174 | B4 = ROL64((A20^D0), 18); |
| 1175 | B0 = ROL64((A21^D1), 1); |
| 1176 | B1 = ROL64((A22^D2), 6); |
| 1177 | B2 = ROL64((A23^D3), 25); |
| 1178 | B3 = ROL64((A24^D4), 8); |
| 1179 | A20 = B0 ^((~B1)& B2 ); |
| 1180 | A21 = B1 ^((~B2)& B3 ); |
| 1181 | A22 = B2 ^((~B3)& B4 ); |
| 1182 | A23 = B3 ^((~B4)& B0 ); |
| 1183 | A24 = B4 ^((~B0)& B1 ); |
| 1184 | |
| 1185 | B1 = ROL64((A30^D0), 36); |
| 1186 | B2 = ROL64((A31^D1), 10); |
| 1187 | B3 = ROL64((A32^D2), 15); |
| 1188 | B4 = ROL64((A33^D3), 56); |
| 1189 | B0 = ROL64((A34^D4), 27); |
| 1190 | A30 = B0 ^((~B1)& B2 ); |
| 1191 | A31 = B1 ^((~B2)& B3 ); |
| 1192 | A32 = B2 ^((~B3)& B4 ); |
| 1193 | A33 = B3 ^((~B4)& B0 ); |
| 1194 | A34 = B4 ^((~B0)& B1 ); |
| 1195 | |
| 1196 | B3 = ROL64((A40^D0), 41); |
| 1197 | B4 = ROL64((A41^D1), 2); |
| 1198 | B0 = ROL64((A42^D2), 62); |
| 1199 | B1 = ROL64((A43^D3), 55); |
| 1200 | B2 = ROL64((A44^D4), 39); |
| 1201 | A40 = B0 ^((~B1)& B2 ); |
| 1202 | A41 = B1 ^((~B2)& B3 ); |
| 1203 | A42 = B2 ^((~B3)& B4 ); |
| 1204 | A43 = B3 ^((~B4)& B0 ); |
| 1205 | A44 = B4 ^((~B0)& B1 ); |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | ** Initialize a new hash. iSize determines the size of the hash |
| 1211 | ** in bits and should be one of 224, 256, 384, or 512. Or iSize |
| 1212 | ** can be zero to use the default hash size of 256 bits. |
| 1213 | */ |
| 1214 | static void SHA3Init(SHA3Context *p, int iSize){ |
| 1215 | memset(p, 0, sizeof(*p)); |
| 1216 | if( iSize>=128 && iSize<=512 ){ |
| 1217 | p->nRate = (1600 - ((iSize + 31)&~31)*2)/8; |
| 1218 | }else{ |
| 1219 | p->nRate = (1600 - 2*256)/8; |
| 1220 | } |
| 1221 | #if SHA3_BYTEORDER==1234 |
| 1222 | /* Known to be little-endian at compile-time. No-op */ |
| 1223 | #elif SHA3_BYTEORDER==4321 |
| 1224 | p->ixMask = 7; /* Big-endian */ |
| 1225 | #else |
| 1226 | { |
| 1227 | static unsigned int one = 1; |
| 1228 | if( 1==*(unsigned char*)&one ){ |
| 1229 | /* Little endian. No byte swapping. */ |
| 1230 | p->ixMask = 0; |
| 1231 | }else{ |
| 1232 | /* Big endian. Byte swap. */ |
| 1233 | p->ixMask = 7; |
| 1234 | } |
| 1235 | } |
| 1236 | #endif |
| 1237 | } |
| 1238 | |
| 1239 | /* |
| 1240 | ** Make consecutive calls to the SHA3Update function to add new content |
| 1241 | ** to the hash |
| 1242 | */ |
| 1243 | static void SHA3Update( |
| 1244 | SHA3Context *p, |
| 1245 | const unsigned char *aData, |
| 1246 | unsigned int nData |
| 1247 | ){ |
| 1248 | unsigned int i = 0; |
| 1249 | #if SHA3_BYTEORDER==1234 |
| 1250 | if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){ |
| 1251 | for(; i+7<nData; i+=8){ |
| 1252 | p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i]; |
| 1253 | p->nLoaded += 8; |
| 1254 | if( p->nLoaded>=p->nRate ){ |
| 1255 | KeccakF1600Step(p); |
| 1256 | p->nLoaded = 0; |
| 1257 | } |
| 1258 | } |
| 1259 | } |
| 1260 | #endif |
| 1261 | for(; i<nData; i++){ |
| 1262 | #if SHA3_BYTEORDER==1234 |
| 1263 | p->u.x[p->nLoaded] ^= aData[i]; |
| 1264 | #elif SHA3_BYTEORDER==4321 |
| 1265 | p->u.x[p->nLoaded^0x07] ^= aData[i]; |
| 1266 | #else |
| 1267 | p->u.x[p->nLoaded^p->ixMask] ^= aData[i]; |
| 1268 | #endif |
| 1269 | p->nLoaded++; |
| 1270 | if( p->nLoaded==p->nRate ){ |
| 1271 | KeccakF1600Step(p); |
| 1272 | p->nLoaded = 0; |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | ** After all content has been added, invoke SHA3Final() to compute |
| 1279 | ** the final hash. The function returns a pointer to the binary |
| 1280 | ** hash value. |
| 1281 | */ |
| 1282 | static unsigned char *SHA3Final(SHA3Context *p){ |
| 1283 | unsigned int i; |
| 1284 | if( p->nLoaded==p->nRate-1 ){ |
| 1285 | const unsigned char c1 = 0x86; |
| 1286 | SHA3Update(p, &c1, 1); |
| 1287 | }else{ |
| 1288 | const unsigned char c2 = 0x06; |
| 1289 | const unsigned char c3 = 0x80; |
| 1290 | SHA3Update(p, &c2, 1); |
| 1291 | p->nLoaded = p->nRate - 1; |
| 1292 | SHA3Update(p, &c3, 1); |
| 1293 | } |
| 1294 | for(i=0; i<p->nRate; i++){ |
| 1295 | p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; |
| 1296 | } |
| 1297 | return &p->u.x[p->nRate]; |
| 1298 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1299 | /* End of the hashing logic |
| 1300 | *****************************************************************************/ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 1301 | |
| 1302 | /* |
| 1303 | ** Implementation of the sha3(X,SIZE) function. |
| 1304 | ** |
| 1305 | ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1306 | ** size is 256. If X is a BLOB, it is hashed as is. |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 1307 | ** For all other non-NULL types of input, X is converted into a UTF-8 string |
| 1308 | ** and the string is hashed without the trailing 0x00 terminator. The hash |
| 1309 | ** of a NULL value is NULL. |
| 1310 | */ |
| 1311 | static void sha3Func( |
| 1312 | sqlite3_context *context, |
| 1313 | int argc, |
| 1314 | sqlite3_value **argv |
| 1315 | ){ |
| 1316 | SHA3Context cx; |
| 1317 | int eType = sqlite3_value_type(argv[0]); |
| 1318 | int nByte = sqlite3_value_bytes(argv[0]); |
| 1319 | int iSize; |
| 1320 | if( argc==1 ){ |
| 1321 | iSize = 256; |
| 1322 | }else{ |
| 1323 | iSize = sqlite3_value_int(argv[1]); |
| 1324 | if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ |
| 1325 | sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " |
| 1326 | "384 512", -1); |
| 1327 | return; |
| 1328 | } |
| 1329 | } |
| 1330 | if( eType==SQLITE_NULL ) return; |
| 1331 | SHA3Init(&cx, iSize); |
| 1332 | if( eType==SQLITE_BLOB ){ |
| 1333 | SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte); |
| 1334 | }else{ |
| 1335 | SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte); |
| 1336 | } |
| 1337 | sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT); |
| 1338 | } |
| 1339 | |
| 1340 | /* Compute a string using sqlite3_vsnprintf() with a maximum length |
| 1341 | ** of 50 bytes and add it to the hash. |
| 1342 | */ |
| 1343 | static void hash_step_vformat( |
| 1344 | SHA3Context *p, /* Add content to this context */ |
| 1345 | const char *zFormat, |
| 1346 | ... |
| 1347 | ){ |
| 1348 | va_list ap; |
| 1349 | int n; |
| 1350 | char zBuf[50]; |
| 1351 | va_start(ap, zFormat); |
| 1352 | sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap); |
| 1353 | va_end(ap); |
| 1354 | n = (int)strlen(zBuf); |
| 1355 | SHA3Update(p, (unsigned char*)zBuf, n); |
| 1356 | } |
| 1357 | |
| 1358 | /* |
| 1359 | ** Implementation of the sha3_query(SQL,SIZE) function. |
| 1360 | ** |
| 1361 | ** This function compiles and runs the SQL statement(s) given in the |
| 1362 | ** argument. The results are hashed using a SIZE-bit SHA3. The default |
| 1363 | ** size is 256. |
| 1364 | ** |
| 1365 | ** The format of the byte stream that is hashed is summarized as follows: |
| 1366 | ** |
| 1367 | ** S<n>:<sql> |
| 1368 | ** R |
| 1369 | ** N |
| 1370 | ** I<int> |
| 1371 | ** F<ieee-float> |
| 1372 | ** B<size>:<bytes> |
| 1373 | ** T<size>:<text> |
| 1374 | ** |
| 1375 | ** <sql> is the original SQL text for each statement run and <n> is |
| 1376 | ** the size of that text. The SQL text is UTF-8. A single R character |
| 1377 | ** occurs before the start of each row. N means a NULL value. |
| 1378 | ** I mean an 8-byte little-endian integer <int>. F is a floating point |
| 1379 | ** number with an 8-byte little-endian IEEE floating point value <ieee-float>. |
| 1380 | ** B means blobs of <size> bytes. T means text rendered as <size> |
| 1381 | ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII |
| 1382 | ** text integers. |
| 1383 | ** |
| 1384 | ** For each SQL statement in the X input, there is one S segment. Each |
| 1385 | ** S segment is followed by zero or more R segments, one for each row in the |
| 1386 | ** result set. After each R, there are one or more N, I, F, B, or T segments, |
| 1387 | ** one for each column in the result set. Segments are concatentated directly |
| 1388 | ** with no delimiters of any kind. |
| 1389 | */ |
| 1390 | static void sha3QueryFunc( |
| 1391 | sqlite3_context *context, |
| 1392 | int argc, |
| 1393 | sqlite3_value **argv |
| 1394 | ){ |
| 1395 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 1396 | const char *zSql = (const char*)sqlite3_value_text(argv[0]); |
| 1397 | sqlite3_stmt *pStmt = 0; |
| 1398 | int nCol; /* Number of columns in the result set */ |
| 1399 | int i; /* Loop counter */ |
| 1400 | int rc; |
| 1401 | int n; |
| 1402 | const char *z; |
| 1403 | SHA3Context cx; |
| 1404 | int iSize; |
| 1405 | |
| 1406 | if( argc==1 ){ |
| 1407 | iSize = 256; |
| 1408 | }else{ |
| 1409 | iSize = sqlite3_value_int(argv[1]); |
| 1410 | if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ |
| 1411 | sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " |
| 1412 | "384 512", -1); |
| 1413 | return; |
| 1414 | } |
| 1415 | } |
| 1416 | if( zSql==0 ) return; |
| 1417 | SHA3Init(&cx, iSize); |
| 1418 | while( zSql[0] ){ |
| 1419 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); |
| 1420 | if( rc ){ |
| 1421 | char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s", |
| 1422 | zSql, sqlite3_errmsg(db)); |
| 1423 | sqlite3_finalize(pStmt); |
| 1424 | sqlite3_result_error(context, zMsg, -1); |
| 1425 | sqlite3_free(zMsg); |
| 1426 | return; |
| 1427 | } |
| 1428 | if( !sqlite3_stmt_readonly(pStmt) ){ |
| 1429 | char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); |
| 1430 | sqlite3_finalize(pStmt); |
| 1431 | sqlite3_result_error(context, zMsg, -1); |
| 1432 | sqlite3_free(zMsg); |
| 1433 | return; |
| 1434 | } |
| 1435 | nCol = sqlite3_column_count(pStmt); |
| 1436 | z = sqlite3_sql(pStmt); |
| 1437 | n = (int)strlen(z); |
| 1438 | hash_step_vformat(&cx,"S%d:",n); |
| 1439 | SHA3Update(&cx,(unsigned char*)z,n); |
| 1440 | |
| 1441 | /* Compute a hash over the result of the query */ |
| 1442 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 1443 | SHA3Update(&cx,(const unsigned char*)"R",1); |
| 1444 | for(i=0; i<nCol; i++){ |
| 1445 | switch( sqlite3_column_type(pStmt,i) ){ |
| 1446 | case SQLITE_NULL: { |
| 1447 | SHA3Update(&cx, (const unsigned char*)"N",1); |
| 1448 | break; |
| 1449 | } |
| 1450 | case SQLITE_INTEGER: { |
| 1451 | sqlite3_uint64 u; |
| 1452 | int j; |
| 1453 | unsigned char x[9]; |
| 1454 | sqlite3_int64 v = sqlite3_column_int64(pStmt,i); |
| 1455 | memcpy(&u, &v, 8); |
| 1456 | for(j=8; j>=1; j--){ |
| 1457 | x[j] = u & 0xff; |
| 1458 | u >>= 8; |
| 1459 | } |
| 1460 | x[0] = 'I'; |
| 1461 | SHA3Update(&cx, x, 9); |
| 1462 | break; |
| 1463 | } |
| 1464 | case SQLITE_FLOAT: { |
| 1465 | sqlite3_uint64 u; |
| 1466 | int j; |
| 1467 | unsigned char x[9]; |
| 1468 | double r = sqlite3_column_double(pStmt,i); |
| 1469 | memcpy(&u, &r, 8); |
| 1470 | for(j=8; j>=1; j--){ |
| 1471 | x[j] = u & 0xff; |
| 1472 | u >>= 8; |
| 1473 | } |
| 1474 | x[0] = 'F'; |
| 1475 | SHA3Update(&cx,x,9); |
| 1476 | break; |
| 1477 | } |
| 1478 | case SQLITE_TEXT: { |
| 1479 | int n2 = sqlite3_column_bytes(pStmt, i); |
| 1480 | const unsigned char *z2 = sqlite3_column_text(pStmt, i); |
| 1481 | hash_step_vformat(&cx,"T%d:",n2); |
| 1482 | SHA3Update(&cx, z2, n2); |
| 1483 | break; |
| 1484 | } |
| 1485 | case SQLITE_BLOB: { |
| 1486 | int n2 = sqlite3_column_bytes(pStmt, i); |
| 1487 | const unsigned char *z2 = sqlite3_column_blob(pStmt, i); |
| 1488 | hash_step_vformat(&cx,"B%d:",n2); |
| 1489 | SHA3Update(&cx, z2, n2); |
| 1490 | break; |
| 1491 | } |
| 1492 | } |
| 1493 | } |
| 1494 | } |
| 1495 | sqlite3_finalize(pStmt); |
| 1496 | } |
| 1497 | sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT); |
| 1498 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1499 | |
| 1500 | |
| 1501 | #ifdef _WIN32 |
| 1502 | __declspec(dllexport) |
| 1503 | #endif |
| 1504 | int sqlite3_shathree_init( |
| 1505 | sqlite3 *db, |
| 1506 | char **pzErrMsg, |
| 1507 | const sqlite3_api_routines *pApi |
| 1508 | ){ |
| 1509 | int rc = SQLITE_OK; |
| 1510 | SQLITE_EXTENSION_INIT2(pApi); |
| 1511 | (void)pzErrMsg; /* Unused parameter */ |
| 1512 | rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0, |
| 1513 | sha3Func, 0, 0); |
| 1514 | if( rc==SQLITE_OK ){ |
| 1515 | rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0, |
| 1516 | sha3Func, 0, 0); |
| 1517 | } |
| 1518 | if( rc==SQLITE_OK ){ |
| 1519 | rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0, |
| 1520 | sha3QueryFunc, 0, 0); |
| 1521 | } |
| 1522 | if( rc==SQLITE_OK ){ |
| 1523 | rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0, |
| 1524 | sha3QueryFunc, 0, 0); |
| 1525 | } |
| 1526 | return rc; |
| 1527 | } |
| 1528 | |
| 1529 | /************************* End ../ext/misc/shathree.c ********************/ |
| 1530 | /************************* Begin ../ext/misc/fileio.c ******************/ |
| 1531 | /* |
| 1532 | ** 2014-06-13 |
| 1533 | ** |
| 1534 | ** The author disclaims copyright to this source code. In place of |
| 1535 | ** a legal notice, here is a blessing: |
| 1536 | ** |
| 1537 | ** May you do good and not evil. |
| 1538 | ** May you find forgiveness for yourself and forgive others. |
| 1539 | ** May you share freely, never taking more than you give. |
| 1540 | ** |
| 1541 | ****************************************************************************** |
| 1542 | ** |
| 1543 | ** This SQLite extension implements SQL functions readfile() and |
| 1544 | ** writefile(). |
| 1545 | */ |
| 1546 | SQLITE_EXTENSION_INIT1 |
| 1547 | #include <stdio.h> |
| 1548 | |
| 1549 | /* |
| 1550 | ** Implementation of the "readfile(X)" SQL function. The entire content |
| 1551 | ** of the file named X is read and returned as a BLOB. NULL is returned |
| 1552 | ** if the file does not exist or is unreadable. |
| 1553 | */ |
| 1554 | static void readfileFunc( |
| 1555 | sqlite3_context *context, |
| 1556 | int argc, |
| 1557 | sqlite3_value **argv |
| 1558 | ){ |
| 1559 | const char *zName; |
| 1560 | FILE *in; |
| 1561 | long nIn; |
| 1562 | void *pBuf; |
| 1563 | |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 1564 | (void)(argc); /* Unused parameter */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1565 | zName = (const char*)sqlite3_value_text(argv[0]); |
| 1566 | if( zName==0 ) return; |
| 1567 | in = fopen(zName, "rb"); |
| 1568 | if( in==0 ) return; |
| 1569 | fseek(in, 0, SEEK_END); |
| 1570 | nIn = ftell(in); |
| 1571 | rewind(in); |
| 1572 | pBuf = sqlite3_malloc( nIn ); |
| 1573 | if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ |
| 1574 | sqlite3_result_blob(context, pBuf, nIn, sqlite3_free); |
| 1575 | }else{ |
| 1576 | sqlite3_free(pBuf); |
| 1577 | } |
| 1578 | fclose(in); |
| 1579 | } |
| 1580 | |
| 1581 | /* |
| 1582 | ** Implementation of the "writefile(X,Y)" SQL function. The argument Y |
| 1583 | ** is written into file X. The number of bytes written is returned. Or |
| 1584 | ** NULL is returned if something goes wrong, such as being unable to open |
| 1585 | ** file X for writing. |
| 1586 | */ |
| 1587 | static void writefileFunc( |
| 1588 | sqlite3_context *context, |
| 1589 | int argc, |
| 1590 | sqlite3_value **argv |
| 1591 | ){ |
| 1592 | FILE *out; |
| 1593 | const char *z; |
| 1594 | sqlite3_int64 rc; |
| 1595 | const char *zFile; |
| 1596 | |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 1597 | (void)(argc); /* Unused parameter */ |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 1598 | zFile = (const char*)sqlite3_value_text(argv[0]); |
| 1599 | if( zFile==0 ) return; |
| 1600 | out = fopen(zFile, "wb"); |
| 1601 | if( out==0 ) return; |
| 1602 | z = (const char*)sqlite3_value_blob(argv[1]); |
| 1603 | if( z==0 ){ |
| 1604 | rc = 0; |
| 1605 | }else{ |
| 1606 | rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); |
| 1607 | } |
| 1608 | fclose(out); |
| 1609 | sqlite3_result_int64(context, rc); |
| 1610 | } |
| 1611 | |
| 1612 | |
| 1613 | #ifdef _WIN32 |
| 1614 | __declspec(dllexport) |
| 1615 | #endif |
| 1616 | int sqlite3_fileio_init( |
| 1617 | sqlite3 *db, |
| 1618 | char **pzErrMsg, |
| 1619 | const sqlite3_api_routines *pApi |
| 1620 | ){ |
| 1621 | int rc = SQLITE_OK; |
| 1622 | SQLITE_EXTENSION_INIT2(pApi); |
| 1623 | (void)pzErrMsg; /* Unused parameter */ |
| 1624 | rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0, |
| 1625 | readfileFunc, 0, 0); |
| 1626 | if( rc==SQLITE_OK ){ |
| 1627 | rc = sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0, |
| 1628 | writefileFunc, 0, 0); |
| 1629 | } |
| 1630 | return rc; |
| 1631 | } |
| 1632 | |
| 1633 | /************************* End ../ext/misc/fileio.c ********************/ |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 1634 | /************************* Begin ../ext/misc/completion.c ******************/ |
| 1635 | /* |
| 1636 | ** 2017-07-10 |
| 1637 | ** |
| 1638 | ** The author disclaims copyright to this source code. In place of |
| 1639 | ** a legal notice, here is a blessing: |
| 1640 | ** |
| 1641 | ** May you do good and not evil. |
| 1642 | ** May you find forgiveness for yourself and forgive others. |
| 1643 | ** May you share freely, never taking more than you give. |
| 1644 | ** |
| 1645 | ************************************************************************* |
| 1646 | ** |
| 1647 | ** This file implements an eponymous virtual table that returns suggested |
| 1648 | ** completions for a partial SQL input. |
| 1649 | ** |
| 1650 | ** Suggested usage: |
| 1651 | ** |
| 1652 | ** SELECT DISTINCT candidate COLLATE nocase |
| 1653 | ** FROM completion($prefix,$wholeline) |
| 1654 | ** ORDER BY 1; |
| 1655 | ** |
| 1656 | ** The two query parameters are optional. $prefix is the text of the |
| 1657 | ** current word being typed and that is to be completed. $wholeline is |
| 1658 | ** the complete input line, used for context. |
| 1659 | ** |
| 1660 | ** The raw completion() table might return the same candidate multiple |
| 1661 | ** times, for example if the same column name is used to two or more |
| 1662 | ** tables. And the candidates are returned in an arbitrary order. Hence, |
| 1663 | ** the DISTINCT and ORDER BY are recommended. |
| 1664 | ** |
| 1665 | ** This virtual table operates at the speed of human typing, and so there |
| 1666 | ** is no attempt to make it fast. Even a slow implementation will be much |
| 1667 | ** faster than any human can type. |
| 1668 | ** |
| 1669 | */ |
| 1670 | SQLITE_EXTENSION_INIT1 |
| 1671 | #include <assert.h> |
| 1672 | #include <string.h> |
| 1673 | #include <ctype.h> |
| 1674 | |
| 1675 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1676 | |
| 1677 | /* completion_vtab is a subclass of sqlite3_vtab which will |
| 1678 | ** serve as the underlying representation of a completion virtual table |
| 1679 | */ |
| 1680 | typedef struct completion_vtab completion_vtab; |
| 1681 | struct completion_vtab { |
| 1682 | sqlite3_vtab base; /* Base class - must be first */ |
| 1683 | sqlite3 *db; /* Database connection for this completion vtab */ |
| 1684 | }; |
| 1685 | |
| 1686 | /* completion_cursor is a subclass of sqlite3_vtab_cursor which will |
| 1687 | ** serve as the underlying representation of a cursor that scans |
| 1688 | ** over rows of the result |
| 1689 | */ |
| 1690 | typedef struct completion_cursor completion_cursor; |
| 1691 | struct completion_cursor { |
| 1692 | sqlite3_vtab_cursor base; /* Base class - must be first */ |
| 1693 | sqlite3 *db; /* Database connection for this cursor */ |
| 1694 | int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */ |
| 1695 | char *zPrefix; /* The prefix for the word we want to complete */ |
| 1696 | char *zLine; /* The whole that we want to complete */ |
| 1697 | const char *zCurrentRow; /* Current output row */ |
| 1698 | sqlite3_stmt *pStmt; /* Current statement */ |
| 1699 | sqlite3_int64 iRowid; /* The rowid */ |
| 1700 | int ePhase; /* Current phase */ |
| 1701 | int j; /* inter-phase counter */ |
| 1702 | }; |
| 1703 | |
| 1704 | /* Values for ePhase: |
| 1705 | */ |
| 1706 | #define COMPLETION_FIRST_PHASE 1 |
| 1707 | #define COMPLETION_KEYWORDS 1 |
| 1708 | #define COMPLETION_PRAGMAS 2 |
| 1709 | #define COMPLETION_FUNCTIONS 3 |
| 1710 | #define COMPLETION_COLLATIONS 4 |
| 1711 | #define COMPLETION_INDEXES 5 |
| 1712 | #define COMPLETION_TRIGGERS 6 |
| 1713 | #define COMPLETION_DATABASES 7 |
| 1714 | #define COMPLETION_TABLES 8 |
| 1715 | #define COMPLETION_COLUMNS 9 |
| 1716 | #define COMPLETION_MODULES 10 |
| 1717 | #define COMPLETION_EOF 11 |
| 1718 | |
| 1719 | /* |
| 1720 | ** The completionConnect() method is invoked to create a new |
| 1721 | ** completion_vtab that describes the completion virtual table. |
| 1722 | ** |
| 1723 | ** Think of this routine as the constructor for completion_vtab objects. |
| 1724 | ** |
| 1725 | ** All this routine needs to do is: |
| 1726 | ** |
| 1727 | ** (1) Allocate the completion_vtab object and initialize all fields. |
| 1728 | ** |
| 1729 | ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the |
| 1730 | ** result set of queries against completion will look like. |
| 1731 | */ |
| 1732 | static int completionConnect( |
| 1733 | sqlite3 *db, |
| 1734 | void *pAux, |
| 1735 | int argc, const char *const*argv, |
| 1736 | sqlite3_vtab **ppVtab, |
| 1737 | char **pzErr |
| 1738 | ){ |
| 1739 | completion_vtab *pNew; |
| 1740 | int rc; |
| 1741 | |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 1742 | (void)(pAux); /* Unused parameter */ |
| 1743 | (void)(argc); /* Unused parameter */ |
| 1744 | (void)(argv); /* Unused parameter */ |
| 1745 | (void)(pzErr); /* Unused parameter */ |
| 1746 | |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 1747 | /* Column numbers */ |
| 1748 | #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */ |
| 1749 | #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */ |
| 1750 | #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */ |
| 1751 | #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */ |
| 1752 | |
| 1753 | rc = sqlite3_declare_vtab(db, |
| 1754 | "CREATE TABLE x(" |
| 1755 | " candidate TEXT," |
| 1756 | " prefix TEXT HIDDEN," |
| 1757 | " wholeline TEXT HIDDEN," |
| 1758 | " phase INT HIDDEN" /* Used for debugging only */ |
| 1759 | ")"); |
| 1760 | if( rc==SQLITE_OK ){ |
| 1761 | pNew = sqlite3_malloc( sizeof(*pNew) ); |
| 1762 | *ppVtab = (sqlite3_vtab*)pNew; |
| 1763 | if( pNew==0 ) return SQLITE_NOMEM; |
| 1764 | memset(pNew, 0, sizeof(*pNew)); |
| 1765 | pNew->db = db; |
| 1766 | } |
| 1767 | return rc; |
| 1768 | } |
| 1769 | |
| 1770 | /* |
| 1771 | ** This method is the destructor for completion_cursor objects. |
| 1772 | */ |
| 1773 | static int completionDisconnect(sqlite3_vtab *pVtab){ |
| 1774 | sqlite3_free(pVtab); |
| 1775 | return SQLITE_OK; |
| 1776 | } |
| 1777 | |
| 1778 | /* |
| 1779 | ** Constructor for a new completion_cursor object. |
| 1780 | */ |
| 1781 | static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 1782 | completion_cursor *pCur; |
| 1783 | pCur = sqlite3_malloc( sizeof(*pCur) ); |
| 1784 | if( pCur==0 ) return SQLITE_NOMEM; |
| 1785 | memset(pCur, 0, sizeof(*pCur)); |
| 1786 | pCur->db = ((completion_vtab*)p)->db; |
| 1787 | *ppCursor = &pCur->base; |
| 1788 | return SQLITE_OK; |
| 1789 | } |
| 1790 | |
| 1791 | /* |
| 1792 | ** Reset the completion_cursor. |
| 1793 | */ |
| 1794 | static void completionCursorReset(completion_cursor *pCur){ |
| 1795 | sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0; |
| 1796 | sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0; |
| 1797 | sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0; |
| 1798 | pCur->j = 0; |
| 1799 | } |
| 1800 | |
| 1801 | /* |
| 1802 | ** Destructor for a completion_cursor. |
| 1803 | */ |
| 1804 | static int completionClose(sqlite3_vtab_cursor *cur){ |
| 1805 | completionCursorReset((completion_cursor*)cur); |
| 1806 | sqlite3_free(cur); |
| 1807 | return SQLITE_OK; |
| 1808 | } |
| 1809 | |
| 1810 | /* |
| 1811 | ** All SQL keywords understood by SQLite |
| 1812 | */ |
| 1813 | static const char *completionKwrds[] = { |
| 1814 | "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS", |
| 1815 | "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY", |
| 1816 | "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT", |
| 1817 | "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE", |
| 1818 | "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE", |
| 1819 | "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH", |
| 1820 | "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN", |
| 1821 | "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF", |
| 1822 | "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", |
| 1823 | "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY", |
| 1824 | "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL", |
| 1825 | "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA", |
| 1826 | "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP", |
| 1827 | "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT", |
| 1828 | "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP", |
| 1829 | "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE", |
| 1830 | "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE", |
| 1831 | "WITH", "WITHOUT", |
| 1832 | }; |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 1833 | #define completionKwCount \ |
| 1834 | (int)(sizeof(completionKwrds)/sizeof(completionKwrds[0])) |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 1835 | |
| 1836 | /* |
| 1837 | ** Advance a completion_cursor to its next row of output. |
| 1838 | ** |
| 1839 | ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object |
| 1840 | ** record the current state of the scan. This routine sets ->zCurrentRow |
| 1841 | ** to the current row of output and then returns. If no more rows remain, |
| 1842 | ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual |
| 1843 | ** table that has reached the end of its scan. |
| 1844 | ** |
| 1845 | ** The current implementation just lists potential identifiers and |
| 1846 | ** keywords and filters them by zPrefix. Future enhancements should |
| 1847 | ** take zLine into account to try to restrict the set of identifiers and |
| 1848 | ** keywords based on what would be legal at the current point of input. |
| 1849 | */ |
| 1850 | static int completionNext(sqlite3_vtab_cursor *cur){ |
| 1851 | completion_cursor *pCur = (completion_cursor*)cur; |
| 1852 | int eNextPhase = 0; /* Next phase to try if current phase reaches end */ |
| 1853 | int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */ |
| 1854 | pCur->iRowid++; |
| 1855 | while( pCur->ePhase!=COMPLETION_EOF ){ |
| 1856 | switch( pCur->ePhase ){ |
| 1857 | case COMPLETION_KEYWORDS: { |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 1858 | if( pCur->j >= completionKwCount ){ |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 1859 | pCur->zCurrentRow = 0; |
| 1860 | pCur->ePhase = COMPLETION_DATABASES; |
| 1861 | }else{ |
| 1862 | pCur->zCurrentRow = completionKwrds[pCur->j++]; |
| 1863 | } |
| 1864 | iCol = -1; |
| 1865 | break; |
| 1866 | } |
| 1867 | case COMPLETION_DATABASES: { |
| 1868 | if( pCur->pStmt==0 ){ |
| 1869 | sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, |
| 1870 | &pCur->pStmt, 0); |
| 1871 | } |
| 1872 | iCol = 1; |
| 1873 | eNextPhase = COMPLETION_TABLES; |
| 1874 | break; |
| 1875 | } |
| 1876 | case COMPLETION_TABLES: { |
| 1877 | if( pCur->pStmt==0 ){ |
| 1878 | sqlite3_stmt *pS2; |
| 1879 | char *zSql = 0; |
| 1880 | const char *zSep = ""; |
| 1881 | sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); |
| 1882 | while( sqlite3_step(pS2)==SQLITE_ROW ){ |
| 1883 | const char *zDb = (const char*)sqlite3_column_text(pS2, 1); |
| 1884 | zSql = sqlite3_mprintf( |
| 1885 | "%z%s" |
| 1886 | "SELECT name FROM \"%w\".sqlite_master" |
| 1887 | " WHERE type='table'", |
| 1888 | zSql, zSep, zDb |
| 1889 | ); |
| 1890 | if( zSql==0 ) return SQLITE_NOMEM; |
| 1891 | zSep = " UNION "; |
| 1892 | } |
| 1893 | sqlite3_finalize(pS2); |
| 1894 | sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); |
| 1895 | sqlite3_free(zSql); |
| 1896 | } |
| 1897 | iCol = 0; |
| 1898 | eNextPhase = COMPLETION_COLUMNS; |
| 1899 | break; |
| 1900 | } |
| 1901 | case COMPLETION_COLUMNS: { |
| 1902 | if( pCur->pStmt==0 ){ |
| 1903 | sqlite3_stmt *pS2; |
| 1904 | char *zSql = 0; |
| 1905 | const char *zSep = ""; |
| 1906 | sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); |
| 1907 | while( sqlite3_step(pS2)==SQLITE_ROW ){ |
| 1908 | const char *zDb = (const char*)sqlite3_column_text(pS2, 1); |
| 1909 | zSql = sqlite3_mprintf( |
| 1910 | "%z%s" |
| 1911 | "SELECT pti.name FROM \"%w\".sqlite_master AS sm" |
| 1912 | " JOIN pragma_table_info(sm.name,%Q) AS pti" |
| 1913 | " WHERE sm.type='table'", |
| 1914 | zSql, zSep, zDb, zDb |
| 1915 | ); |
| 1916 | if( zSql==0 ) return SQLITE_NOMEM; |
| 1917 | zSep = " UNION "; |
| 1918 | } |
| 1919 | sqlite3_finalize(pS2); |
| 1920 | sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); |
| 1921 | sqlite3_free(zSql); |
| 1922 | } |
| 1923 | iCol = 0; |
| 1924 | eNextPhase = COMPLETION_EOF; |
| 1925 | break; |
| 1926 | } |
| 1927 | } |
| 1928 | if( iCol<0 ){ |
| 1929 | /* This case is when the phase presets zCurrentRow */ |
| 1930 | if( pCur->zCurrentRow==0 ) continue; |
| 1931 | }else{ |
| 1932 | if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){ |
| 1933 | /* Extract the next row of content */ |
| 1934 | pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol); |
| 1935 | }else{ |
| 1936 | /* When all rows are finished, advance to the next phase */ |
| 1937 | sqlite3_finalize(pCur->pStmt); |
| 1938 | pCur->pStmt = 0; |
| 1939 | pCur->ePhase = eNextPhase; |
| 1940 | continue; |
| 1941 | } |
| 1942 | } |
| 1943 | if( pCur->nPrefix==0 ) break; |
| 1944 | if( sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 ){ |
| 1945 | break; |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | return SQLITE_OK; |
| 1950 | } |
| 1951 | |
| 1952 | /* |
| 1953 | ** Return values of columns for the row at which the completion_cursor |
| 1954 | ** is currently pointing. |
| 1955 | */ |
| 1956 | static int completionColumn( |
| 1957 | sqlite3_vtab_cursor *cur, /* The cursor */ |
| 1958 | sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
| 1959 | int i /* Which column to return */ |
| 1960 | ){ |
| 1961 | completion_cursor *pCur = (completion_cursor*)cur; |
| 1962 | switch( i ){ |
| 1963 | case COMPLETION_COLUMN_CANDIDATE: { |
| 1964 | sqlite3_result_text(ctx, pCur->zCurrentRow, -1, SQLITE_TRANSIENT); |
| 1965 | break; |
| 1966 | } |
| 1967 | case COMPLETION_COLUMN_PREFIX: { |
| 1968 | sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT); |
| 1969 | break; |
| 1970 | } |
| 1971 | case COMPLETION_COLUMN_WHOLELINE: { |
| 1972 | sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT); |
| 1973 | break; |
| 1974 | } |
| 1975 | case COMPLETION_COLUMN_PHASE: { |
| 1976 | sqlite3_result_int(ctx, pCur->ePhase); |
| 1977 | break; |
| 1978 | } |
| 1979 | } |
| 1980 | return SQLITE_OK; |
| 1981 | } |
| 1982 | |
| 1983 | /* |
| 1984 | ** Return the rowid for the current row. In this implementation, the |
| 1985 | ** rowid is the same as the output value. |
| 1986 | */ |
| 1987 | static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 1988 | completion_cursor *pCur = (completion_cursor*)cur; |
| 1989 | *pRowid = pCur->iRowid; |
| 1990 | return SQLITE_OK; |
| 1991 | } |
| 1992 | |
| 1993 | /* |
| 1994 | ** Return TRUE if the cursor has been moved off of the last |
| 1995 | ** row of output. |
| 1996 | */ |
| 1997 | static int completionEof(sqlite3_vtab_cursor *cur){ |
| 1998 | completion_cursor *pCur = (completion_cursor*)cur; |
| 1999 | return pCur->ePhase >= COMPLETION_EOF; |
| 2000 | } |
| 2001 | |
| 2002 | /* |
| 2003 | ** This method is called to "rewind" the completion_cursor object back |
| 2004 | ** to the first row of output. This method is always called at least |
| 2005 | ** once prior to any call to completionColumn() or completionRowid() or |
| 2006 | ** completionEof(). |
| 2007 | */ |
| 2008 | static int completionFilter( |
| 2009 | sqlite3_vtab_cursor *pVtabCursor, |
| 2010 | int idxNum, const char *idxStr, |
| 2011 | int argc, sqlite3_value **argv |
| 2012 | ){ |
| 2013 | completion_cursor *pCur = (completion_cursor *)pVtabCursor; |
| 2014 | int iArg = 0; |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 2015 | (void)(idxStr); /* Unused parameter */ |
| 2016 | (void)(argc); /* Unused parameter */ |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 2017 | completionCursorReset(pCur); |
| 2018 | if( idxNum & 1 ){ |
| 2019 | pCur->nPrefix = sqlite3_value_bytes(argv[iArg]); |
| 2020 | if( pCur->nPrefix>0 ){ |
| 2021 | pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); |
| 2022 | if( pCur->zPrefix==0 ) return SQLITE_NOMEM; |
| 2023 | } |
| 2024 | iArg++; |
| 2025 | } |
| 2026 | if( idxNum & 2 ){ |
| 2027 | pCur->nLine = sqlite3_value_bytes(argv[iArg]); |
| 2028 | if( pCur->nLine>0 ){ |
| 2029 | pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); |
| 2030 | if( pCur->zLine==0 ) return SQLITE_NOMEM; |
| 2031 | } |
| 2032 | iArg++; |
| 2033 | } |
| 2034 | if( pCur->zLine!=0 && pCur->zPrefix==0 ){ |
| 2035 | int i = pCur->nLine; |
| 2036 | while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){ |
| 2037 | i--; |
| 2038 | } |
| 2039 | pCur->nPrefix = pCur->nLine - i; |
| 2040 | if( pCur->nPrefix>0 ){ |
| 2041 | pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i); |
| 2042 | if( pCur->zPrefix==0 ) return SQLITE_NOMEM; |
| 2043 | } |
| 2044 | } |
| 2045 | pCur->iRowid = 0; |
| 2046 | pCur->ePhase = COMPLETION_FIRST_PHASE; |
| 2047 | return completionNext(pVtabCursor); |
| 2048 | } |
| 2049 | |
| 2050 | /* |
| 2051 | ** SQLite will invoke this method one or more times while planning a query |
| 2052 | ** that uses the completion virtual table. This routine needs to create |
| 2053 | ** a query plan for each invocation and compute an estimated cost for that |
| 2054 | ** plan. |
| 2055 | ** |
| 2056 | ** There are two hidden parameters that act as arguments to the table-valued |
| 2057 | ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix" |
| 2058 | ** is available and bit 1 is set if "wholeline" is available. |
| 2059 | */ |
| 2060 | static int completionBestIndex( |
| 2061 | sqlite3_vtab *tab, |
| 2062 | sqlite3_index_info *pIdxInfo |
| 2063 | ){ |
| 2064 | int i; /* Loop over constraints */ |
| 2065 | int idxNum = 0; /* The query plan bitmask */ |
| 2066 | int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */ |
| 2067 | int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */ |
| 2068 | int nArg = 0; /* Number of arguments that completeFilter() expects */ |
| 2069 | const struct sqlite3_index_constraint *pConstraint; |
| 2070 | |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 2071 | (void)(tab); /* Unused parameter */ |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 2072 | pConstraint = pIdxInfo->aConstraint; |
| 2073 | for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
| 2074 | if( pConstraint->usable==0 ) continue; |
| 2075 | if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 2076 | switch( pConstraint->iColumn ){ |
| 2077 | case COMPLETION_COLUMN_PREFIX: |
| 2078 | prefixIdx = i; |
| 2079 | idxNum |= 1; |
| 2080 | break; |
| 2081 | case COMPLETION_COLUMN_WHOLELINE: |
| 2082 | wholelineIdx = i; |
| 2083 | idxNum |= 2; |
| 2084 | break; |
| 2085 | } |
| 2086 | } |
| 2087 | if( prefixIdx>=0 ){ |
| 2088 | pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg; |
| 2089 | pIdxInfo->aConstraintUsage[prefixIdx].omit = 1; |
| 2090 | } |
| 2091 | if( wholelineIdx>=0 ){ |
| 2092 | pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg; |
| 2093 | pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1; |
| 2094 | } |
| 2095 | pIdxInfo->idxNum = idxNum; |
| 2096 | pIdxInfo->estimatedCost = (double)5000 - 1000*nArg; |
| 2097 | pIdxInfo->estimatedRows = 500 - 100*nArg; |
| 2098 | return SQLITE_OK; |
| 2099 | } |
| 2100 | |
| 2101 | /* |
| 2102 | ** This following structure defines all the methods for the |
| 2103 | ** completion virtual table. |
| 2104 | */ |
| 2105 | static sqlite3_module completionModule = { |
| 2106 | 0, /* iVersion */ |
| 2107 | 0, /* xCreate */ |
| 2108 | completionConnect, /* xConnect */ |
| 2109 | completionBestIndex, /* xBestIndex */ |
| 2110 | completionDisconnect, /* xDisconnect */ |
| 2111 | 0, /* xDestroy */ |
| 2112 | completionOpen, /* xOpen - open a cursor */ |
| 2113 | completionClose, /* xClose - close a cursor */ |
| 2114 | completionFilter, /* xFilter - configure scan constraints */ |
| 2115 | completionNext, /* xNext - advance a cursor */ |
| 2116 | completionEof, /* xEof - check for end of scan */ |
| 2117 | completionColumn, /* xColumn - read data */ |
| 2118 | completionRowid, /* xRowid - read data */ |
| 2119 | 0, /* xUpdate */ |
| 2120 | 0, /* xBegin */ |
| 2121 | 0, /* xSync */ |
| 2122 | 0, /* xCommit */ |
| 2123 | 0, /* xRollback */ |
| 2124 | 0, /* xFindMethod */ |
| 2125 | 0, /* xRename */ |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 2126 | 0, /* xSavepoint */ |
| 2127 | 0, /* xRelease */ |
| 2128 | 0 /* xRollbackTo */ |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 2129 | }; |
| 2130 | |
| 2131 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 2132 | |
| 2133 | int sqlite3CompletionVtabInit(sqlite3 *db){ |
| 2134 | int rc = SQLITE_OK; |
| 2135 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2136 | rc = sqlite3_create_module(db, "completion", &completionModule, 0); |
| 2137 | #endif |
| 2138 | return rc; |
| 2139 | } |
| 2140 | |
| 2141 | #ifdef _WIN32 |
| 2142 | __declspec(dllexport) |
| 2143 | #endif |
| 2144 | int sqlite3_completion_init( |
| 2145 | sqlite3 *db, |
| 2146 | char **pzErrMsg, |
| 2147 | const sqlite3_api_routines *pApi |
| 2148 | ){ |
| 2149 | int rc = SQLITE_OK; |
| 2150 | SQLITE_EXTENSION_INIT2(pApi); |
drh | 8999798 | 2017-07-11 18:11:33 +0000 | [diff] [blame] | 2151 | (void)(pzErrMsg); /* Unused parameter */ |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 2152 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2153 | rc = sqlite3CompletionVtabInit(db); |
| 2154 | #endif |
| 2155 | return rc; |
| 2156 | } |
| 2157 | |
| 2158 | /************************* End ../ext/misc/completion.c ********************/ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 2159 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 2160 | #if defined(SQLITE_ENABLE_SESSION) |
| 2161 | /* |
| 2162 | ** State information for a single open session |
| 2163 | */ |
| 2164 | typedef struct OpenSession OpenSession; |
| 2165 | struct OpenSession { |
| 2166 | char *zName; /* Symbolic name for this session */ |
| 2167 | int nFilter; /* Number of xFilter rejection GLOB patterns */ |
| 2168 | char **azFilter; /* Array of xFilter rejection GLOB patterns */ |
| 2169 | sqlite3_session *p; /* The open session */ |
| 2170 | }; |
| 2171 | #endif |
| 2172 | |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2173 | /* |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2174 | ** Shell output mode information from before ".explain on", |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2175 | ** saved so that it can be restored by ".explain off" |
| 2176 | */ |
| 2177 | typedef struct SavedModeInfo SavedModeInfo; |
| 2178 | struct SavedModeInfo { |
| 2179 | int valid; /* Is there legit data in here? */ |
| 2180 | int mode; /* Mode prior to ".explain on" */ |
| 2181 | int showHeader; /* The ".header" setting prior to ".explain on" */ |
| 2182 | int colWidth[100]; /* Column widths prior to ".explain on" */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2183 | }; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2184 | |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 2185 | /* |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2186 | ** State information about the database connection is contained in an |
| 2187 | ** instance of the following structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2188 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2189 | typedef struct ShellState ShellState; |
| 2190 | struct ShellState { |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2191 | sqlite3 *db; /* The database */ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2192 | int autoExplain; /* Automatically turn on .explain mode */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2193 | int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 2194 | int statsOn; /* True to display memory stats before each finalize */ |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 2195 | int scanstatsOn; /* True to display scan stats before each finalize */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 2196 | int outCount; /* Revert to stdout when reaching zero */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2197 | int cnt; /* Number of records displayed so far */ |
| 2198 | FILE *out; /* Write results here */ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 2199 | FILE *traceOut; /* Output for sqlite3_trace() */ |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 2200 | int nErr; /* Number of errors seen */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2201 | int mode; /* An output mode setting */ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2202 | int cMode; /* temporary output mode for the current query */ |
| 2203 | int normalMode; /* Output mode before ".explain on" */ |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2204 | int writableSchema; /* True if PRAGMA writable_schema=ON */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2205 | int showHeader; /* True to show column names in List or Column mode */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 2206 | int nCheck; /* Number of ".check" commands run */ |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2207 | unsigned shellFlgs; /* Various flags */ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2208 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 2209 | char zTestcase[30]; /* Name of current test case */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2210 | char colSeparator[20]; /* Column separator character for several modes */ |
| 2211 | char rowSeparator[20]; /* Row separator character for MODE_Ascii */ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2212 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 2213 | int actualWidth[100]; /* Actual width of each column */ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2214 | char nullValue[20]; /* The text to print when a NULL comes back from |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 2215 | ** the database */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2216 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
| 2217 | const char *zDbFilename; /* name of the database file */ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 2218 | char *zFreeOnClose; /* Filename to free when closing */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 2219 | const char *zVfs; /* Name of VFS to use */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2220 | sqlite3_stmt *pStmt; /* Current statement if any. */ |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 2221 | FILE *pLog; /* Write log output here */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2222 | int *aiIndent; /* Array of indents used in MODE_Explain */ |
| 2223 | int nIndent; /* Size of array aiIndent[] */ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2224 | int iIndent; /* Index of current op in aiIndent[] */ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 2225 | #if defined(SQLITE_ENABLE_SESSION) |
| 2226 | int nSession; /* Number of active sessions */ |
| 2227 | OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ |
| 2228 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2229 | }; |
| 2230 | |
| 2231 | /* |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2232 | ** These are the allowed shellFlgs values |
| 2233 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 2234 | #define SHFLG_Scratch 0x00000001 /* The --scratch option is used */ |
| 2235 | #define SHFLG_Pagecache 0x00000002 /* The --pagecache option is used */ |
| 2236 | #define SHFLG_Lookaside 0x00000004 /* Lookaside memory is used */ |
| 2237 | #define SHFLG_Backslash 0x00000008 /* The --backslash option is used */ |
| 2238 | #define SHFLG_PreserveRowid 0x00000010 /* .dump preserves rowid values */ |
drh | dbc2672 | 2017-07-10 18:04:41 +0000 | [diff] [blame] | 2239 | #define SHFLG_Newlines 0x00000020 /* .dump --newline flag */ |
| 2240 | #define SHFLG_CountChanges 0x00000040 /* .changes setting */ |
| 2241 | #define SHFLG_Echo 0x00000080 /* .echo or --echo setting */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 2242 | |
| 2243 | /* |
| 2244 | ** Macros for testing and setting shellFlgs |
| 2245 | */ |
| 2246 | #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) |
| 2247 | #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) |
| 2248 | #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 2249 | |
| 2250 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2251 | ** These are the allowed modes. |
| 2252 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 2253 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2254 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 2255 | #define MODE_List 2 /* One record per line with a separator */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2256 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 2257 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 2258 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 2259 | #define MODE_Quote 6 /* Quote values as for SQL */ |
| 2260 | #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ |
| 2261 | #define MODE_Csv 8 /* Quote strings, numbers are plain */ |
| 2262 | #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ |
| 2263 | #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ |
| 2264 | #define MODE_Pretty 11 /* Pretty-print schemas */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2265 | |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 2266 | static const char *modeDescr[] = { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2267 | "line", |
| 2268 | "column", |
| 2269 | "list", |
| 2270 | "semi", |
| 2271 | "html", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2272 | "insert", |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 2273 | "quote", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2274 | "tcl", |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2275 | "csv", |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 2276 | "explain", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2277 | "ascii", |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2278 | "prettyprint", |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2279 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2280 | |
| 2281 | /* |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 2282 | ** These are the column/row/line separators used by the various |
| 2283 | ** import/export modes. |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2284 | */ |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 2285 | #define SEP_Column "|" |
| 2286 | #define SEP_Row "\n" |
| 2287 | #define SEP_Tab "\t" |
| 2288 | #define SEP_Space " " |
| 2289 | #define SEP_Comma "," |
| 2290 | #define SEP_CrLf "\r\n" |
| 2291 | #define SEP_Unit "\x1F" |
| 2292 | #define SEP_Record "\x1E" |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2293 | |
| 2294 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2295 | ** Number of elements in an array |
| 2296 | */ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 2297 | #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2298 | |
| 2299 | /* |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 2300 | ** A callback for the sqlite3_log() interface. |
| 2301 | */ |
| 2302 | static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2303 | ShellState *p = (ShellState*)pArg; |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 2304 | if( p->pLog==0 ) return; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2305 | utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 2306 | fflush(p->pLog); |
| 2307 | } |
| 2308 | |
| 2309 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2310 | ** Output the given string as a hex-encoded blob (eg. X'1234' ) |
| 2311 | */ |
| 2312 | static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
| 2313 | int i; |
| 2314 | char *zBlob = (char *)pBlob; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2315 | raw_printf(out,"X'"); |
| 2316 | for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); } |
| 2317 | raw_printf(out,"'"); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | /* |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 2321 | ** Find a string that is not found anywhere in z[]. Return a pointer |
| 2322 | ** to that string. |
| 2323 | ** |
| 2324 | ** Try to use zA and zB first. If both of those are already found in z[] |
| 2325 | ** then make up some string and store it in the buffer zBuf. |
| 2326 | */ |
| 2327 | static const char *unused_string( |
| 2328 | const char *z, /* Result must not appear anywhere in z */ |
| 2329 | const char *zA, const char *zB, /* Try these first */ |
| 2330 | char *zBuf /* Space to store a generated string */ |
| 2331 | ){ |
| 2332 | unsigned i = 0; |
| 2333 | if( strstr(z, zA)==0 ) return zA; |
| 2334 | if( strstr(z, zB)==0 ) return zB; |
| 2335 | do{ |
| 2336 | sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); |
| 2337 | }while( strstr(z,zBuf)!=0 ); |
| 2338 | return zBuf; |
| 2339 | } |
| 2340 | |
| 2341 | /* |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2342 | ** Output the given string as a quoted string using SQL quoting conventions. |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 2343 | ** |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2344 | ** See also: output_quoted_escaped_string() |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2345 | */ |
| 2346 | static void output_quoted_string(FILE *out, const char *z){ |
| 2347 | int i; |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 2348 | char c; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2349 | setBinaryMode(out, 1); |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2350 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 2351 | if( c==0 ){ |
| 2352 | utf8_printf(out,"'%s'",z); |
| 2353 | }else{ |
| 2354 | raw_printf(out, "'"); |
| 2355 | while( *z ){ |
| 2356 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 2357 | if( c=='\'' ) i++; |
| 2358 | if( i ){ |
| 2359 | utf8_printf(out, "%.*s", i, z); |
| 2360 | z += i; |
| 2361 | } |
| 2362 | if( c=='\'' ){ |
| 2363 | raw_printf(out, "'"); |
| 2364 | continue; |
| 2365 | } |
| 2366 | if( c==0 ){ |
| 2367 | break; |
| 2368 | } |
| 2369 | z++; |
| 2370 | } |
| 2371 | raw_printf(out, "'"); |
| 2372 | } |
| 2373 | setTextMode(out, 1); |
| 2374 | } |
| 2375 | |
| 2376 | /* |
| 2377 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 2378 | ** Additionallly , escape the "\n" and "\r" characters so that they do not |
| 2379 | ** get corrupted by end-of-line translation facilities in some operating |
| 2380 | ** systems. |
| 2381 | ** |
| 2382 | ** This is like output_quoted_string() but with the addition of the \r\n |
| 2383 | ** escape mechanism. |
| 2384 | */ |
| 2385 | static void output_quoted_escaped_string(FILE *out, const char *z){ |
| 2386 | int i; |
| 2387 | char c; |
| 2388 | setBinaryMode(out, 1); |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 2389 | for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} |
| 2390 | if( c==0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2391 | utf8_printf(out,"'%s'",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2392 | }else{ |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 2393 | const char *zNL = 0; |
| 2394 | const char *zCR = 0; |
| 2395 | int nNL = 0; |
| 2396 | int nCR = 0; |
| 2397 | char zBuf1[20], zBuf2[20]; |
| 2398 | for(i=0; z[i]; i++){ |
| 2399 | if( z[i]=='\n' ) nNL++; |
| 2400 | if( z[i]=='\r' ) nCR++; |
| 2401 | } |
| 2402 | if( nNL ){ |
| 2403 | raw_printf(out, "replace("); |
| 2404 | zNL = unused_string(z, "\\n", "\\012", zBuf1); |
| 2405 | } |
| 2406 | if( nCR ){ |
| 2407 | raw_printf(out, "replace("); |
| 2408 | zCR = unused_string(z, "\\r", "\\015", zBuf2); |
| 2409 | } |
| 2410 | raw_printf(out, "'"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2411 | while( *z ){ |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 2412 | for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} |
| 2413 | if( c=='\'' ) i++; |
| 2414 | if( i ){ |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 2415 | utf8_printf(out, "%.*s", i, z); |
| 2416 | z += i; |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 2417 | } |
| 2418 | if( c=='\'' ){ |
| 2419 | raw_printf(out, "'"); |
| 2420 | continue; |
| 2421 | } |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 2422 | if( c==0 ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2423 | break; |
| 2424 | } |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 2425 | z++; |
| 2426 | if( c=='\n' ){ |
| 2427 | raw_printf(out, "%s", zNL); |
| 2428 | continue; |
drh | 708b22b | 2017-03-11 01:56:41 +0000 | [diff] [blame] | 2429 | } |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 2430 | raw_printf(out, "%s", zCR); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2431 | } |
drh | 6193d49 | 2017-04-07 11:45:58 +0000 | [diff] [blame] | 2432 | raw_printf(out, "'"); |
| 2433 | if( nCR ){ |
| 2434 | raw_printf(out, ",'%s',char(13))", zCR); |
| 2435 | } |
| 2436 | if( nNL ){ |
| 2437 | raw_printf(out, ",'%s',char(10))", zNL); |
| 2438 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2439 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2440 | setTextMode(out, 1); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
| 2443 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2444 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 2445 | */ |
| 2446 | static void output_c_string(FILE *out, const char *z){ |
| 2447 | unsigned int c; |
| 2448 | fputc('"', out); |
| 2449 | while( (c = *(z++))!=0 ){ |
| 2450 | if( c=='\\' ){ |
| 2451 | fputc(c, out); |
| 2452 | fputc(c, out); |
mistachkin | 585dcb2 | 2012-12-04 00:23:43 +0000 | [diff] [blame] | 2453 | }else if( c=='"' ){ |
| 2454 | fputc('\\', out); |
| 2455 | fputc('"', out); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2456 | }else if( c=='\t' ){ |
| 2457 | fputc('\\', out); |
| 2458 | fputc('t', out); |
| 2459 | }else if( c=='\n' ){ |
| 2460 | fputc('\\', out); |
| 2461 | fputc('n', out); |
| 2462 | }else if( c=='\r' ){ |
| 2463 | fputc('\\', out); |
| 2464 | fputc('r', out); |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 2465 | }else if( !isprint(c&0xff) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2466 | raw_printf(out, "\\%03o", c&0xff); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2467 | }else{ |
| 2468 | fputc(c, out); |
| 2469 | } |
| 2470 | } |
| 2471 | fputc('"', out); |
| 2472 | } |
| 2473 | |
| 2474 | /* |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 2475 | ** Output the given string with characters that are special to |
| 2476 | ** HTML escaped. |
| 2477 | */ |
| 2478 | static void output_html_string(FILE *out, const char *z){ |
| 2479 | int i; |
drh | c3d6ba4 | 2014-01-13 20:38:35 +0000 | [diff] [blame] | 2480 | if( z==0 ) z = ""; |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 2481 | while( *z ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2482 | for(i=0; z[i] |
| 2483 | && z[i]!='<' |
| 2484 | && z[i]!='&' |
| 2485 | && z[i]!='>' |
| 2486 | && z[i]!='\"' |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 2487 | && z[i]!='\''; |
| 2488 | i++){} |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 2489 | if( i>0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2490 | utf8_printf(out,"%.*s",i,z); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 2491 | } |
| 2492 | if( z[i]=='<' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2493 | raw_printf(out,"<"); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 2494 | }else if( z[i]=='&' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2495 | raw_printf(out,"&"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 2496 | }else if( z[i]=='>' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2497 | raw_printf(out,">"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 2498 | }else if( z[i]=='\"' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2499 | raw_printf(out,"""); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 2500 | }else if( z[i]=='\'' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2501 | raw_printf(out,"'"); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 2502 | }else{ |
| 2503 | break; |
| 2504 | } |
| 2505 | z += i + 1; |
| 2506 | } |
| 2507 | } |
| 2508 | |
| 2509 | /* |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2510 | ** If a field contains any character identified by a 1 in the following |
| 2511 | ** array, then the string must be quoted for CSV. |
| 2512 | */ |
| 2513 | static const char needCsvQuote[] = { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2514 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2515 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2516 | 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2517 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2518 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2519 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2520 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2521 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 2522 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2523 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2524 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2525 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2526 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2527 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2528 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2529 | 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] | 2530 | }; |
| 2531 | |
| 2532 | /* |
mistachkin | dd11f2d | 2014-12-11 04:49:46 +0000 | [diff] [blame] | 2533 | ** Output a single term of CSV. Actually, p->colSeparator is used for |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2534 | ** the separator, which may or may not be a comma. p->nullValue is |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 2535 | ** the null value. Strings are quoted if necessary. The separator |
| 2536 | ** is only issued if bSep is true. |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2537 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2538 | static void output_csv(ShellState *p, const char *z, int bSep){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2539 | FILE *out = p->out; |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2540 | if( z==0 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2541 | utf8_printf(out,"%s",p->nullValue); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2542 | }else{ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2543 | int i; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2544 | int nSep = strlen30(p->colSeparator); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2545 | for(i=0; z[i]; i++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2546 | if( needCsvQuote[((unsigned char*)z)[i]] |
| 2547 | || (z[i]==p->colSeparator[0] && |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2548 | (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2549 | i = 0; |
| 2550 | break; |
| 2551 | } |
| 2552 | } |
| 2553 | if( i==0 ){ |
| 2554 | putc('"', out); |
| 2555 | for(i=0; z[i]; i++){ |
| 2556 | if( z[i]=='"' ) putc('"', out); |
| 2557 | putc(z[i], out); |
| 2558 | } |
| 2559 | putc('"', out); |
| 2560 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2561 | utf8_printf(out, "%s", z); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2562 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2563 | } |
| 2564 | if( bSep ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2565 | utf8_printf(p->out, "%s", p->colSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2566 | } |
| 2567 | } |
| 2568 | |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 2569 | #ifdef SIGINT |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2570 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 2571 | ** This routine runs when the user presses Ctrl-C |
| 2572 | */ |
| 2573 | static void interrupt_handler(int NotUsed){ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 2574 | UNUSED_PARAMETER(NotUsed); |
drh | 43ae8f6 | 2014-05-23 12:03:47 +0000 | [diff] [blame] | 2575 | seenInterrupt++; |
| 2576 | if( seenInterrupt>2 ) exit(1); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 2577 | if( globalDb ) sqlite3_interrupt(globalDb); |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 2578 | } |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 2579 | #endif |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 2580 | |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 2581 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 2582 | /* |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 2583 | ** When the ".auth ON" is set, the following authorizer callback is |
| 2584 | ** invoked. It always returns SQLITE_OK. |
| 2585 | */ |
| 2586 | static int shellAuth( |
| 2587 | void *pClientData, |
| 2588 | int op, |
| 2589 | const char *zA1, |
| 2590 | const char *zA2, |
| 2591 | const char *zA3, |
| 2592 | const char *zA4 |
| 2593 | ){ |
| 2594 | ShellState *p = (ShellState*)pClientData; |
| 2595 | static const char *azAction[] = { 0, |
| 2596 | "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", |
| 2597 | "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", |
| 2598 | "CREATE_TRIGGER", "CREATE_VIEW", "DELETE", |
| 2599 | "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", |
| 2600 | "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", |
| 2601 | "DROP_TRIGGER", "DROP_VIEW", "INSERT", |
| 2602 | "PRAGMA", "READ", "SELECT", |
| 2603 | "TRANSACTION", "UPDATE", "ATTACH", |
| 2604 | "DETACH", "ALTER_TABLE", "REINDEX", |
| 2605 | "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", |
| 2606 | "FUNCTION", "SAVEPOINT", "RECURSIVE" |
| 2607 | }; |
| 2608 | int i; |
| 2609 | const char *az[4]; |
| 2610 | az[0] = zA1; |
| 2611 | az[1] = zA2; |
| 2612 | az[2] = zA3; |
| 2613 | az[3] = zA4; |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 2614 | utf8_printf(p->out, "authorizer: %s", azAction[op]); |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 2615 | for(i=0; i<4; i++){ |
| 2616 | raw_printf(p->out, " "); |
| 2617 | if( az[i] ){ |
| 2618 | output_c_string(p->out, az[i]); |
| 2619 | }else{ |
| 2620 | raw_printf(p->out, "NULL"); |
| 2621 | } |
| 2622 | } |
| 2623 | raw_printf(p->out, "\n"); |
| 2624 | return SQLITE_OK; |
| 2625 | } |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 2626 | #endif |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 2627 | |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2628 | /* |
| 2629 | ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. |
| 2630 | ** |
| 2631 | ** This routine converts some CREATE TABLE statements for shadow tables |
| 2632 | ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. |
| 2633 | */ |
| 2634 | static void printSchemaLine(FILE *out, const char *z, const char *zTail){ |
| 2635 | if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ |
| 2636 | utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); |
| 2637 | }else{ |
| 2638 | utf8_printf(out, "%s%s", z, zTail); |
| 2639 | } |
| 2640 | } |
| 2641 | static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ |
| 2642 | char c = z[n]; |
| 2643 | z[n] = 0; |
| 2644 | printSchemaLine(out, z, zTail); |
| 2645 | z[n] = c; |
| 2646 | } |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 2647 | |
| 2648 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2649 | ** This is the callback routine that the shell |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2650 | ** invokes for each row of a query result. |
| 2651 | */ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 2652 | static int shell_callback( |
| 2653 | void *pArg, |
| 2654 | int nArg, /* Number of result columns */ |
| 2655 | char **azArg, /* Text of each result column */ |
| 2656 | char **azCol, /* Column names */ |
| 2657 | int *aiType /* Column types */ |
| 2658 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2659 | int i; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 2660 | ShellState *p = (ShellState*)pArg; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 2661 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2662 | switch( p->cMode ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2663 | case MODE_Line: { |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2664 | int w = 5; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2665 | if( azArg==0 ) break; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2666 | for(i=0; i<nArg; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2667 | int len = strlen30(azCol[i] ? azCol[i] : ""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2668 | if( len>w ) w = len; |
| 2669 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2670 | if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2671 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2672 | utf8_printf(p->out,"%*s = %s%s", w, azCol[i], |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2673 | azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2674 | } |
| 2675 | break; |
| 2676 | } |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 2677 | case MODE_Explain: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2678 | case MODE_Column: { |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2679 | static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
| 2680 | const int *colWidth; |
| 2681 | int showHdr; |
| 2682 | char *rowSep; |
| 2683 | if( p->cMode==MODE_Column ){ |
| 2684 | colWidth = p->colWidth; |
| 2685 | showHdr = p->showHeader; |
| 2686 | rowSep = p->rowSeparator; |
| 2687 | }else{ |
| 2688 | colWidth = aExplainWidths; |
| 2689 | showHdr = 1; |
mistachkin | 6d94555 | 2016-02-09 20:31:50 +0000 | [diff] [blame] | 2690 | rowSep = SEP_Row; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2691 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2692 | if( p->cnt++==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2693 | for(i=0; i<nArg; i++){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2694 | int w, n; |
| 2695 | if( i<ArraySize(p->colWidth) ){ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2696 | w = colWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2697 | }else{ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 2698 | w = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2699 | } |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 2700 | if( w==0 ){ |
drh | 64bf76d | 2017-06-05 12:29:26 +0000 | [diff] [blame] | 2701 | w = strlenChar(azCol[i] ? azCol[i] : ""); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2702 | if( w<10 ) w = 10; |
drh | 64bf76d | 2017-06-05 12:29:26 +0000 | [diff] [blame] | 2703 | n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2704 | if( w<n ) w = n; |
| 2705 | } |
| 2706 | if( i<ArraySize(p->actualWidth) ){ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 2707 | p->actualWidth[i] = w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2708 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2709 | if( showHdr ){ |
drh | 6887e8f | 2017-04-17 13:18:42 +0000 | [diff] [blame] | 2710 | utf8_width_print(p->out, w, azCol[i]); |
| 2711 | utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " "); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2712 | } |
| 2713 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2714 | if( showHdr ){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2715 | for(i=0; i<nArg; i++){ |
| 2716 | int w; |
| 2717 | if( i<ArraySize(p->actualWidth) ){ |
| 2718 | w = p->actualWidth[i]; |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 2719 | if( w<0 ) w = -w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2720 | }else{ |
| 2721 | w = 10; |
| 2722 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2723 | utf8_printf(p->out,"%-*.*s%s",w,w, |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2724 | "----------------------------------------------------------" |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2725 | "----------------------------------------------------------", |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 2726 | i==nArg-1 ? rowSep : " "); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2727 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2728 | } |
| 2729 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2730 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2731 | for(i=0; i<nArg; i++){ |
| 2732 | int w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 2733 | if( i<ArraySize(p->actualWidth) ){ |
| 2734 | w = p->actualWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2735 | }else{ |
| 2736 | w = 10; |
| 2737 | } |
drh | 64bf76d | 2017-06-05 12:29:26 +0000 | [diff] [blame] | 2738 | if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){ |
| 2739 | w = strlenChar(azArg[i]); |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 2740 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2741 | if( i==1 && p->aiIndent && p->pStmt ){ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2742 | if( p->iIndent<p->nIndent ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2743 | utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2744 | } |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 2745 | p->iIndent++; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 2746 | } |
drh | 6887e8f | 2017-04-17 13:18:42 +0000 | [diff] [blame] | 2747 | utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); |
| 2748 | utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " "); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2749 | } |
| 2750 | break; |
| 2751 | } |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2752 | case MODE_Semi: { /* .schema and .fullschema output */ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2753 | printSchemaLine(p->out, azArg[0], ";\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2754 | break; |
| 2755 | } |
| 2756 | case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
| 2757 | char *z; |
drh | 07d683f | 2016-04-13 21:00:36 +0000 | [diff] [blame] | 2758 | int j; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2759 | int nParen = 0; |
| 2760 | char cEnd = 0; |
| 2761 | char c; |
| 2762 | int nLine = 0; |
| 2763 | assert( nArg==1 ); |
| 2764 | if( azArg[0]==0 ) break; |
| 2765 | if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
| 2766 | || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
| 2767 | ){ |
| 2768 | utf8_printf(p->out, "%s;\n", azArg[0]); |
| 2769 | break; |
| 2770 | } |
| 2771 | z = sqlite3_mprintf("%s", azArg[0]); |
| 2772 | j = 0; |
| 2773 | for(i=0; IsSpace(z[i]); i++){} |
| 2774 | for(; (c = z[i])!=0; i++){ |
| 2775 | if( IsSpace(c) ){ |
| 2776 | if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; |
| 2777 | }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ |
| 2778 | j--; |
| 2779 | } |
| 2780 | z[j++] = c; |
| 2781 | } |
| 2782 | while( j>0 && IsSpace(z[j-1]) ){ j--; } |
| 2783 | z[j] = 0; |
| 2784 | if( strlen30(z)>=79 ){ |
drh | 07d683f | 2016-04-13 21:00:36 +0000 | [diff] [blame] | 2785 | for(i=j=0; (c = z[i])!=0; i++){ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2786 | if( c==cEnd ){ |
| 2787 | cEnd = 0; |
| 2788 | }else if( c=='"' || c=='\'' || c=='`' ){ |
| 2789 | cEnd = c; |
| 2790 | }else if( c=='[' ){ |
| 2791 | cEnd = ']'; |
| 2792 | }else if( c=='(' ){ |
| 2793 | nParen++; |
| 2794 | }else if( c==')' ){ |
| 2795 | nParen--; |
| 2796 | if( nLine>0 && nParen==0 && j>0 ){ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2797 | printSchemaLineN(p->out, z, j, "\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2798 | j = 0; |
| 2799 | } |
| 2800 | } |
| 2801 | z[j++] = c; |
| 2802 | if( nParen==1 && (c=='(' || c==',' || c=='\n') ){ |
| 2803 | if( c=='\n' ) j--; |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2804 | printSchemaLineN(p->out, z, j, "\n "); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2805 | j = 0; |
| 2806 | nLine++; |
| 2807 | while( IsSpace(z[i+1]) ){ i++; } |
| 2808 | } |
| 2809 | } |
| 2810 | z[j] = 0; |
| 2811 | } |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 2812 | printSchemaLine(p->out, z, ";\n"); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 2813 | sqlite3_free(z); |
| 2814 | break; |
| 2815 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2816 | case MODE_List: { |
| 2817 | if( p->cnt++==0 && p->showHeader ){ |
| 2818 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2819 | utf8_printf(p->out,"%s%s",azCol[i], |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2820 | i==nArg-1 ? p->rowSeparator : p->colSeparator); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2821 | } |
| 2822 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2823 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2824 | for(i=0; i<nArg; i++){ |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2825 | char *z = azArg[i]; |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2826 | if( z==0 ) z = p->nullValue; |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2827 | utf8_printf(p->out, "%s", z); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2828 | if( i<nArg-1 ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2829 | utf8_printf(p->out, "%s", p->colSeparator); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2830 | }else{ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2831 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2832 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2833 | } |
| 2834 | break; |
| 2835 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2836 | case MODE_Html: { |
| 2837 | if( p->cnt++==0 && p->showHeader ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2838 | raw_printf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2839 | for(i=0; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2840 | raw_printf(p->out,"<TH>"); |
shane | 43d9cb2 | 2009-10-21 14:11:48 +0000 | [diff] [blame] | 2841 | output_html_string(p->out, azCol[i]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2842 | raw_printf(p->out,"</TH>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2843 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2844 | raw_printf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2845 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2846 | if( azArg==0 ) break; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2847 | raw_printf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2848 | for(i=0; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2849 | raw_printf(p->out,"<TD>"); |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2850 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2851 | raw_printf(p->out,"</TD>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2852 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 2853 | raw_printf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2854 | break; |
| 2855 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2856 | case MODE_Tcl: { |
| 2857 | if( p->cnt++==0 && p->showHeader ){ |
| 2858 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 2859 | output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2860 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2861 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2862 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2863 | } |
| 2864 | if( azArg==0 ) break; |
| 2865 | for(i=0; i<nArg; i++){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 2866 | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2867 | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2868 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2869 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2870 | break; |
| 2871 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2872 | case MODE_Csv: { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2873 | setBinaryMode(p->out, 1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2874 | if( p->cnt++==0 && p->showHeader ){ |
| 2875 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 2876 | output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2877 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2878 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2879 | } |
drh | 4025326 | 2014-10-17 21:35:05 +0000 | [diff] [blame] | 2880 | if( nArg>0 ){ |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 2881 | for(i=0; i<nArg; i++){ |
| 2882 | output_csv(p, azArg[i], i<nArg-1); |
| 2883 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2884 | utf8_printf(p->out, "%s", p->rowSeparator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2885 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 2886 | setTextMode(p->out, 1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2887 | break; |
| 2888 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2889 | case MODE_Insert: { |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2890 | if( azArg==0 ) break; |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2891 | utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); |
| 2892 | if( p->showHeader ){ |
| 2893 | raw_printf(p->out,"("); |
| 2894 | for(i=0; i<nArg; i++){ |
| 2895 | if( i>0 ) raw_printf(p->out, ","); |
| 2896 | if( quoteChar(azCol[i]) ){ |
| 2897 | char *z = sqlite3_mprintf("\"%w\"", azCol[i]); |
| 2898 | utf8_printf(p->out, "%s", z); |
| 2899 | sqlite3_free(z); |
| 2900 | }else{ |
| 2901 | raw_printf(p->out, "%s", azCol[i]); |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 2902 | } |
mistachkin | 151c75a | 2015-04-07 21:16:40 +0000 | [diff] [blame] | 2903 | } |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2904 | raw_printf(p->out,")"); |
| 2905 | } |
| 2906 | p->cnt++; |
| 2907 | for(i=0; i<nArg; i++){ |
| 2908 | raw_printf(p->out, i>0 ? "," : " VALUES("); |
| 2909 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 2910 | utf8_printf(p->out,"NULL"); |
| 2911 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
drh | dbc2672 | 2017-07-10 18:04:41 +0000 | [diff] [blame] | 2912 | if( ShellHasFlag(p, SHFLG_Newlines) ){ |
| 2913 | output_quoted_string(p->out, azArg[i]); |
| 2914 | }else{ |
| 2915 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2916 | } |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2917 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
| 2918 | utf8_printf(p->out,"%s", azArg[i]); |
| 2919 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2920 | char z[50]; |
| 2921 | double r = sqlite3_column_double(p->pStmt, i); |
| 2922 | sqlite3_snprintf(50,z,"%!.20g", r); |
| 2923 | raw_printf(p->out, "%s", z); |
| 2924 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2925 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2926 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 2927 | output_hex_blob(p->out, pBlob, nBlob); |
| 2928 | }else if( isNumber(azArg[i], 0) ){ |
| 2929 | utf8_printf(p->out,"%s", azArg[i]); |
drh | dbc2672 | 2017-07-10 18:04:41 +0000 | [diff] [blame] | 2930 | }else if( ShellHasFlag(p, SHFLG_Newlines) ){ |
| 2931 | output_quoted_string(p->out, azArg[i]); |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2932 | }else{ |
| 2933 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2934 | } |
| 2935 | } |
| 2936 | raw_printf(p->out,");\n"); |
| 2937 | break; |
| 2938 | } |
| 2939 | case MODE_Quote: { |
| 2940 | if( azArg==0 ) break; |
| 2941 | if( p->cnt==0 && p->showHeader ){ |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2942 | for(i=0; i<nArg; i++){ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 2943 | if( i>0 ) raw_printf(p->out, ","); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2944 | output_quoted_string(p->out, azCol[i]); |
| 2945 | } |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 2946 | raw_printf(p->out,"\n"); |
mistachkin | 151c75a | 2015-04-07 21:16:40 +0000 | [diff] [blame] | 2947 | } |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 2948 | p->cnt++; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2949 | for(i=0; i<nArg; i++){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2950 | if( i>0 ) raw_printf(p->out, ","); |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2951 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2952 | utf8_printf(p->out,"NULL"); |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2953 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
shane | ad6b8d0 | 2009-10-22 18:12:58 +0000 | [diff] [blame] | 2954 | output_quoted_string(p->out, azArg[i]); |
drh | 891d6b4 | 2017-03-11 00:46:57 +0000 | [diff] [blame] | 2955 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2956 | utf8_printf(p->out,"%s", azArg[i]); |
drh | 891d6b4 | 2017-03-11 00:46:57 +0000 | [diff] [blame] | 2957 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2958 | char z[50]; |
| 2959 | double r = sqlite3_column_double(p->pStmt, i); |
| 2960 | sqlite3_snprintf(50,z,"%!.20g", r); |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2961 | raw_printf(p->out, "%s", z); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2962 | }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
| 2963 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2964 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2965 | output_hex_blob(p->out, pBlob, nBlob); |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 2966 | }else if( isNumber(azArg[i], 0) ){ |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2967 | utf8_printf(p->out,"%s", azArg[i]); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2968 | }else{ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2969 | output_quoted_string(p->out, azArg[i]); |
| 2970 | } |
| 2971 | } |
drh | 13fe138 | 2017-04-08 13:42:55 +0000 | [diff] [blame] | 2972 | raw_printf(p->out,"\n"); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2973 | break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2974 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2975 | case MODE_Ascii: { |
| 2976 | if( p->cnt++==0 && p->showHeader ){ |
| 2977 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2978 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2979 | utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2980 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2981 | utf8_printf(p->out, "%s", p->rowSeparator); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2982 | } |
| 2983 | if( azArg==0 ) break; |
| 2984 | for(i=0; i<nArg; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2985 | if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
| 2986 | utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2987 | } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 2988 | utf8_printf(p->out, "%s", p->rowSeparator); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 2989 | break; |
| 2990 | } |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 2991 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2992 | return 0; |
| 2993 | } |
| 2994 | |
| 2995 | /* |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 2996 | ** This is the callback routine that the SQLite library |
| 2997 | ** invokes for each row of a query result. |
| 2998 | */ |
| 2999 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 3000 | /* since we don't have type info, call the shell_callback with a NULL value */ |
| 3001 | return shell_callback(pArg, nArg, azArg, azCol, NULL); |
| 3002 | } |
| 3003 | |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 3004 | /* |
| 3005 | ** This is the callback routine from sqlite3_exec() that appends all |
| 3006 | ** output onto the end of a ShellText object. |
| 3007 | */ |
| 3008 | static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ |
| 3009 | ShellText *p = (ShellText*)pArg; |
| 3010 | int i; |
drh | 2fb79e9 | 2017-03-25 12:08:11 +0000 | [diff] [blame] | 3011 | UNUSED_PARAMETER(az); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 3012 | if( p->n ) appendText(p, "|", 0); |
| 3013 | for(i=0; i<nArg; i++){ |
| 3014 | if( i ) appendText(p, ",", 0); |
| 3015 | if( azArg[i] ) appendText(p, azArg[i], 0); |
| 3016 | } |
| 3017 | return 0; |
| 3018 | } |
| 3019 | |
| 3020 | /* |
| 3021 | ** Generate an appropriate SELFTEST table in the main database. |
| 3022 | */ |
| 3023 | static void createSelftestTable(ShellState *p){ |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 3024 | char *zErrMsg = 0; |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 3025 | sqlite3_exec(p->db, |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 3026 | "SAVEPOINT selftest_init;\n" |
| 3027 | "CREATE TABLE IF NOT EXISTS selftest(\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 3028 | " tno INTEGER PRIMARY KEY,\n" /* Test number */ |
| 3029 | " op TEXT,\n" /* Operator: memo run */ |
| 3030 | " cmd TEXT,\n" /* Command text */ |
| 3031 | " ans TEXT\n" /* Desired answer */ |
| 3032 | ");" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 3033 | "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" |
| 3034 | "INSERT INTO [_shell$self](rowid,op,cmd)\n" |
| 3035 | " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" |
| 3036 | " 'memo','Tests generated by --init');\n" |
| 3037 | "INSERT INTO [_shell$self]\n" |
| 3038 | " SELECT 'run',\n" |
| 3039 | " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " |
| 3040 | "FROM sqlite_master ORDER BY 2'',224))',\n" |
| 3041 | " hex(sha3_query('SELECT type,name,tbl_name,sql " |
| 3042 | "FROM sqlite_master ORDER BY 2',224));\n" |
| 3043 | "INSERT INTO [_shell$self]\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 3044 | " SELECT 'run'," |
| 3045 | " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" |
| 3046 | " printf('%w',name) || '\" NOT INDEXED'',224))',\n" |
| 3047 | " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" |
| 3048 | " FROM (\n" |
| 3049 | " SELECT name FROM sqlite_master\n" |
| 3050 | " WHERE type='table'\n" |
| 3051 | " AND name<>'selftest'\n" |
| 3052 | " AND coalesce(rootpage,0)>0\n" |
| 3053 | " )\n" |
| 3054 | " ORDER BY name;\n" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 3055 | "INSERT INTO [_shell$self]\n" |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 3056 | " VALUES('run','PRAGMA integrity_check','ok');\n" |
drh | f157d10 | 2017-03-10 01:05:38 +0000 | [diff] [blame] | 3057 | "INSERT INTO selftest(tno,op,cmd,ans)" |
| 3058 | " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" |
| 3059 | "DROP TABLE [_shell$self];" |
| 3060 | ,0,0,&zErrMsg); |
| 3061 | if( zErrMsg ){ |
| 3062 | utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); |
| 3063 | sqlite3_free(zErrMsg); |
| 3064 | } |
| 3065 | sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3068 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3069 | /* |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3070 | ** Set the destination table field of the ShellState structure to |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 3071 | ** the name of the table given. Escape any quote characters in the |
| 3072 | ** table name. |
| 3073 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3074 | static void set_table_name(ShellState *p, const char *zName){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 3075 | int i, n; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3076 | int cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 3077 | char *z; |
| 3078 | |
| 3079 | if( p->zDestTable ){ |
| 3080 | free(p->zDestTable); |
| 3081 | p->zDestTable = 0; |
| 3082 | } |
| 3083 | if( zName==0 ) return; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3084 | cQuote = quoteChar(zName); |
| 3085 | n = strlen30(zName); |
drh | 45e7d7d | 2017-06-24 13:31:40 +0000 | [diff] [blame] | 3086 | if( cQuote ) n += n+2; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 3087 | z = p->zDestTable = malloc( n+1 ); |
| 3088 | if( z==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3089 | raw_printf(stderr,"Error: out of memory\n"); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 3090 | exit(1); |
| 3091 | } |
| 3092 | n = 0; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3093 | if( cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 3094 | for(i=0; zName[i]; i++){ |
| 3095 | z[n++] = zName[i]; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3096 | if( zName[i]==cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 3097 | } |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3098 | if( cQuote ) z[n++] = cQuote; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 3099 | z[n] = 0; |
| 3100 | } |
| 3101 | |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3102 | |
| 3103 | /* |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 3104 | ** Execute a query statement that will generate SQL output. Print |
| 3105 | ** the result columns, comma-separated, on a line and then add a |
| 3106 | ** semicolon terminator to the end of that line. |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3107 | ** |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 3108 | ** If the number of columns is 1 and that column contains text "--" |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3109 | ** then write the semicolon on a separate line. That way, if a |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 3110 | ** "--" comment occurs at the end of the statement, the comment |
| 3111 | ** won't consume the semicolon terminator. |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3112 | */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 3113 | static int run_table_dump_query( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3114 | ShellState *p, /* Query context */ |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3115 | const char *zSelect, /* SELECT statement to extract content */ |
| 3116 | const char *zFirstRow /* Print before first row, if not NULL */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 3117 | ){ |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3118 | sqlite3_stmt *pSelect; |
| 3119 | int rc; |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 3120 | int nResult; |
| 3121 | int i; |
| 3122 | const char *z; |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 3123 | rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3124 | if( rc!=SQLITE_OK || !pSelect ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3125 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 3126 | sqlite3_errmsg(p->db)); |
drh | 4384e98 | 2013-10-01 15:30:05 +0000 | [diff] [blame] | 3127 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3128 | return rc; |
| 3129 | } |
| 3130 | rc = sqlite3_step(pSelect); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 3131 | nResult = sqlite3_column_count(pSelect); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3132 | while( rc==SQLITE_ROW ){ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 3133 | if( zFirstRow ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3134 | utf8_printf(p->out, "%s", zFirstRow); |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 3135 | zFirstRow = 0; |
| 3136 | } |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 3137 | z = (const char*)sqlite3_column_text(pSelect, 0); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3138 | utf8_printf(p->out, "%s", z); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3139 | for(i=1; i<nResult; i++){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3140 | utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 3141 | } |
| 3142 | if( z==0 ) z = ""; |
| 3143 | while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
| 3144 | if( z[0] ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3145 | raw_printf(p->out, "\n;\n"); |
drh | b21a8e4 | 2012-01-28 21:08:51 +0000 | [diff] [blame] | 3146 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3147 | raw_printf(p->out, ";\n"); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3148 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3149 | rc = sqlite3_step(pSelect); |
| 3150 | } |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3151 | rc = sqlite3_finalize(pSelect); |
| 3152 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3153 | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
| 3154 | sqlite3_errmsg(p->db)); |
drh | 4384e98 | 2013-10-01 15:30:05 +0000 | [diff] [blame] | 3155 | if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3156 | } |
| 3157 | return rc; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3160 | /* |
| 3161 | ** Allocate space and save off current error string. |
| 3162 | */ |
| 3163 | static char *save_err_msg( |
| 3164 | sqlite3 *db /* Database to query */ |
| 3165 | ){ |
| 3166 | int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3167 | char *zErrMsg = sqlite3_malloc64(nErrMsg); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3168 | if( zErrMsg ){ |
| 3169 | memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); |
| 3170 | } |
| 3171 | return zErrMsg; |
| 3172 | } |
| 3173 | |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 3174 | #ifdef __linux__ |
| 3175 | /* |
| 3176 | ** Attempt to display I/O stats on Linux using /proc/PID/io |
| 3177 | */ |
| 3178 | static void displayLinuxIoStats(FILE *out){ |
| 3179 | FILE *in; |
| 3180 | char z[200]; |
| 3181 | sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
| 3182 | in = fopen(z, "rb"); |
| 3183 | if( in==0 ) return; |
| 3184 | while( fgets(z, sizeof(z), in)!=0 ){ |
| 3185 | static const struct { |
| 3186 | const char *zPattern; |
| 3187 | const char *zDesc; |
| 3188 | } aTrans[] = { |
drh | fc1a84c | 2016-02-27 19:19:22 +0000 | [diff] [blame] | 3189 | { "rchar: ", "Bytes received by read():" }, |
| 3190 | { "wchar: ", "Bytes sent to write():" }, |
| 3191 | { "syscr: ", "Read() system calls:" }, |
| 3192 | { "syscw: ", "Write() system calls:" }, |
| 3193 | { "read_bytes: ", "Bytes read from storage:" }, |
| 3194 | { "write_bytes: ", "Bytes written to storage:" }, |
| 3195 | { "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 3196 | }; |
| 3197 | int i; |
| 3198 | for(i=0; i<ArraySize(aTrans); i++){ |
| 3199 | int n = (int)strlen(aTrans[i].zPattern); |
| 3200 | if( strncmp(aTrans[i].zPattern, z, n)==0 ){ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 3201 | utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 3202 | break; |
| 3203 | } |
| 3204 | } |
| 3205 | } |
| 3206 | fclose(in); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3207 | } |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 3208 | #endif |
| 3209 | |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 3210 | /* |
| 3211 | ** Display a single line of status using 64-bit values. |
| 3212 | */ |
| 3213 | static void displayStatLine( |
| 3214 | ShellState *p, /* The shell context */ |
| 3215 | char *zLabel, /* Label for this one line */ |
| 3216 | char *zFormat, /* Format for the result */ |
| 3217 | int iStatusCtrl, /* Which status to display */ |
| 3218 | int bReset /* True to reset the stats */ |
| 3219 | ){ |
| 3220 | sqlite3_int64 iCur = -1; |
| 3221 | sqlite3_int64 iHiwtr = -1; |
| 3222 | int i, nPercent; |
| 3223 | char zLine[200]; |
| 3224 | sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset); |
| 3225 | for(i=0, nPercent=0; zFormat[i]; i++){ |
| 3226 | if( zFormat[i]=='%' ) nPercent++; |
| 3227 | } |
| 3228 | if( nPercent>1 ){ |
| 3229 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); |
| 3230 | }else{ |
| 3231 | sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); |
| 3232 | } |
| 3233 | raw_printf(p->out, "%-36s %s\n", zLabel, zLine); |
| 3234 | } |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 3235 | |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3236 | /* |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3237 | ** Display memory stats. |
| 3238 | */ |
| 3239 | static int display_stats( |
| 3240 | sqlite3 *db, /* Database to query */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3241 | ShellState *pArg, /* Pointer to ShellState */ |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3242 | int bReset /* True to reset the stats */ |
| 3243 | ){ |
| 3244 | int iCur; |
| 3245 | int iHiwtr; |
| 3246 | |
| 3247 | if( pArg && pArg->out ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 3248 | displayStatLine(pArg, "Memory Used:", |
| 3249 | "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); |
| 3250 | displayStatLine(pArg, "Number of Outstanding Allocations:", |
| 3251 | "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 3252 | if( pArg->shellFlgs & SHFLG_Pagecache ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 3253 | displayStatLine(pArg, "Number of Pcache Pages Used:", |
| 3254 | "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 3255 | } |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 3256 | displayStatLine(pArg, "Number of Pcache Overflow Bytes:", |
| 3257 | "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 3258 | if( pArg->shellFlgs & SHFLG_Scratch ){ |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 3259 | displayStatLine(pArg, "Number of Scratch Allocations Used:", |
| 3260 | "%lld (max %lld)", SQLITE_STATUS_SCRATCH_USED, bReset); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 3261 | } |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 3262 | displayStatLine(pArg, "Number of Scratch Overflow Bytes:", |
| 3263 | "%lld (max %lld) bytes", SQLITE_STATUS_SCRATCH_OVERFLOW, bReset); |
| 3264 | displayStatLine(pArg, "Largest Allocation:", |
| 3265 | "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); |
| 3266 | displayStatLine(pArg, "Largest Pcache Allocation:", |
| 3267 | "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); |
| 3268 | displayStatLine(pArg, "Largest Scratch Allocation:", |
| 3269 | "%lld bytes", SQLITE_STATUS_SCRATCH_SIZE, bReset); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3270 | #ifdef YYTRACKMAXSTACKDEPTH |
drh | a2df53b | 2017-03-10 14:36:10 +0000 | [diff] [blame] | 3271 | displayStatLine(pArg, "Deepest Parser Stack:", |
| 3272 | "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3273 | #endif |
| 3274 | } |
| 3275 | |
| 3276 | if( pArg && pArg->out && db ){ |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 3277 | if( pArg->shellFlgs & SHFLG_Lookaside ){ |
| 3278 | iHiwtr = iCur = -1; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 3279 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, |
| 3280 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3281 | raw_printf(pArg->out, |
| 3282 | "Lookaside Slots Used: %d (max %d)\n", |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 3283 | iCur, iHiwtr); |
| 3284 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, |
| 3285 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3286 | raw_printf(pArg->out, "Successful lookaside attempts: %d\n", |
| 3287 | iHiwtr); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 3288 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, |
| 3289 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3290 | raw_printf(pArg->out, "Lookaside failures due to size: %d\n", |
| 3291 | iHiwtr); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 3292 | sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, |
| 3293 | &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3294 | raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", |
| 3295 | iHiwtr); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 3296 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3297 | iHiwtr = iCur = -1; |
| 3298 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3299 | raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", |
| 3300 | iCur); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 3301 | iHiwtr = iCur = -1; |
drh | c78e6e4 | 2011-09-23 18:58:23 +0000 | [diff] [blame] | 3302 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3303 | raw_printf(pArg->out, "Page cache hits: %d\n", iCur); |
drh | c78e6e4 | 2011-09-23 18:58:23 +0000 | [diff] [blame] | 3304 | iHiwtr = iCur = -1; |
| 3305 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3306 | raw_printf(pArg->out, "Page cache misses: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3307 | iHiwtr = iCur = -1; |
drh | fbbcd5d | 2012-03-24 20:09:33 +0000 | [diff] [blame] | 3308 | sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3309 | raw_printf(pArg->out, "Page cache writes: %d\n", iCur); |
drh | fbbcd5d | 2012-03-24 20:09:33 +0000 | [diff] [blame] | 3310 | iHiwtr = iCur = -1; |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3311 | sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3312 | raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3313 | iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3314 | iHiwtr = iCur = -1; |
| 3315 | sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3316 | raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3317 | iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | if( pArg && pArg->out && db && pArg->pStmt ){ |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 3321 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, |
| 3322 | bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3323 | raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3324 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3325 | raw_printf(pArg->out, "Sort Operations: %d\n", iCur); |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 3326 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3327 | raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); |
drh | bf159fa | 2013-06-25 22:01:22 +0000 | [diff] [blame] | 3328 | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3329 | raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3330 | } |
| 3331 | |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 3332 | #ifdef __linux__ |
| 3333 | displayLinuxIoStats(pArg->out); |
| 3334 | #endif |
| 3335 | |
dan | 5a79028 | 2015-08-07 20:06:14 +0000 | [diff] [blame] | 3336 | /* Do not remove this machine readable comment: extra-stats-output-here */ |
| 3337 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3338 | return 0; |
| 3339 | } |
| 3340 | |
| 3341 | /* |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 3342 | ** Display scan stats. |
| 3343 | */ |
| 3344 | static void display_scanstats( |
| 3345 | sqlite3 *db, /* Database to query */ |
| 3346 | ShellState *pArg /* Pointer to ShellState */ |
| 3347 | ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 3348 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 3349 | UNUSED_PARAMETER(db); |
| 3350 | UNUSED_PARAMETER(pArg); |
| 3351 | #else |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 3352 | int i, k, n, mx; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3353 | raw_printf(pArg->out, "-------- scanstats --------\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 3354 | mx = 0; |
| 3355 | for(k=0; k<=mx; k++){ |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 3356 | double rEstLoop = 1.0; |
| 3357 | for(i=n=0; 1; i++){ |
| 3358 | sqlite3_stmt *p = pArg->pStmt; |
| 3359 | sqlite3_int64 nLoop, nVisit; |
| 3360 | double rEst; |
| 3361 | int iSid; |
| 3362 | const char *zExplain; |
| 3363 | if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ |
| 3364 | break; |
| 3365 | } |
| 3366 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 3367 | if( iSid>mx ) mx = iSid; |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 3368 | if( iSid!=k ) continue; |
drh | 179bac3 | 2014-11-06 12:17:24 +0000 | [diff] [blame] | 3369 | if( n==0 ){ |
| 3370 | rEstLoop = (double)nLoop; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3371 | if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); |
drh | 179bac3 | 2014-11-06 12:17:24 +0000 | [diff] [blame] | 3372 | } |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 3373 | n++; |
| 3374 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); |
| 3375 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); |
| 3376 | sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3377 | utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 3378 | rEstLoop *= rEst; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3379 | raw_printf(pArg->out, |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 3380 | " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", |
drh | 9a06d30 | 2014-11-07 13:52:44 +0000 | [diff] [blame] | 3381 | nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst |
drh | 42f30bc | 2014-11-06 12:08:21 +0000 | [diff] [blame] | 3382 | ); |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 3383 | } |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 3384 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3385 | raw_printf(pArg->out, "---------------------------\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 3386 | #endif |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 3387 | } |
| 3388 | |
| 3389 | /* |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3390 | ** Parameter azArray points to a zero-terminated array of strings. zStr |
| 3391 | ** points to a single nul-terminated string. Return non-zero if zStr |
| 3392 | ** is equal, according to strcmp(), to any of the strings in the array. |
| 3393 | ** Otherwise, return zero. |
| 3394 | */ |
| 3395 | static int str_in_array(const char *zStr, const char **azArray){ |
| 3396 | int i; |
| 3397 | for(i=0; azArray[i]; i++){ |
| 3398 | if( 0==strcmp(zStr, azArray[i]) ) return 1; |
| 3399 | } |
| 3400 | return 0; |
| 3401 | } |
| 3402 | |
| 3403 | /* |
| 3404 | ** If compiled statement pSql appears to be an EXPLAIN statement, allocate |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3405 | ** and populate the ShellState.aiIndent[] array with the number of |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3406 | ** spaces each opcode should be indented before it is output. |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3407 | ** |
| 3408 | ** The indenting rules are: |
| 3409 | ** |
| 3410 | ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
| 3411 | ** all opcodes that occur between the p2 jump destination and the opcode |
| 3412 | ** itself by 2 spaces. |
| 3413 | ** |
drh | 01752bc | 2013-11-14 23:59:33 +0000 | [diff] [blame] | 3414 | ** * For each "Goto", if the jump destination is earlier in the program |
| 3415 | ** and ends on one of: |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 3416 | ** Yield SeekGt SeekLt RowSetRead Rewind |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 3417 | ** or if the P1 parameter is one instead of zero, |
drh | 01752bc | 2013-11-14 23:59:33 +0000 | [diff] [blame] | 3418 | ** then indent all opcodes between the earlier instruction |
drh | d244744 | 2013-11-13 19:01:41 +0000 | [diff] [blame] | 3419 | ** and "Goto" by 2 spaces. |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3420 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3421 | static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3422 | const char *zSql; /* The text of the SQL statement */ |
| 3423 | const char *z; /* Used to check if this is an EXPLAIN */ |
| 3424 | int *abYield = 0; /* True if op is an OP_Yield */ |
| 3425 | int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 3426 | int iOp; /* Index of operation in p->aiIndent[] */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3427 | |
drh | 8ad0de3 | 2014-03-20 18:45:27 +0000 | [diff] [blame] | 3428 | const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", |
| 3429 | "NextIfOpen", "PrevIfOpen", 0 }; |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 3430 | const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
| 3431 | "Rewind", 0 }; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3432 | const char *azGoto[] = { "Goto", 0 }; |
| 3433 | |
| 3434 | /* Try to figure out if this is really an EXPLAIN statement. If this |
| 3435 | ** cannot be verified, return early. */ |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 3436 | if( sqlite3_column_count(pSql)!=8 ){ |
| 3437 | p->cMode = p->mode; |
| 3438 | return; |
| 3439 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3440 | zSql = sqlite3_sql(pSql); |
| 3441 | if( zSql==0 ) return; |
| 3442 | for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 3443 | if( sqlite3_strnicmp(z, "explain", 7) ){ |
| 3444 | p->cMode = p->mode; |
| 3445 | return; |
| 3446 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3447 | |
| 3448 | for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ |
| 3449 | int i; |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 3450 | int iAddr = sqlite3_column_int(pSql, 0); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3451 | const char *zOp = (const char*)sqlite3_column_text(pSql, 1); |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 3452 | |
| 3453 | /* Set p2 to the P2 field of the current opcode. Then, assuming that |
| 3454 | ** p2 is an instruction address, set variable p2op to the index of that |
| 3455 | ** instruction in the aiIndent[] array. p2 and p2op may be different if |
| 3456 | ** the current instruction is part of a sub-program generated by an |
| 3457 | ** SQL trigger or foreign key. */ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3458 | int p2 = sqlite3_column_int(pSql, 3); |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 3459 | int p2op = (p2 + (iOp-iAddr)); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3460 | |
| 3461 | /* Grow the p->aiIndent array as required */ |
| 3462 | if( iOp>=nAlloc ){ |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 3463 | if( iOp==0 ){ |
| 3464 | /* Do further verfication that this is explain output. Abort if |
| 3465 | ** it is not */ |
| 3466 | static const char *explainCols[] = { |
| 3467 | "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; |
| 3468 | int jj; |
| 3469 | for(jj=0; jj<ArraySize(explainCols); jj++){ |
| 3470 | if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ |
| 3471 | p->cMode = p->mode; |
| 3472 | sqlite3_reset(pSql); |
| 3473 | return; |
| 3474 | } |
| 3475 | } |
| 3476 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3477 | nAlloc += 100; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3478 | p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); |
| 3479 | abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3480 | } |
| 3481 | abYield[iOp] = str_in_array(zOp, azYield); |
| 3482 | p->aiIndent[iOp] = 0; |
| 3483 | p->nIndent = iOp+1; |
| 3484 | |
| 3485 | if( str_in_array(zOp, azNext) ){ |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 3486 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3487 | } |
drh | fe70510 | 2014-03-06 13:38:37 +0000 | [diff] [blame] | 3488 | if( str_in_array(zOp, azGoto) && p2op<p->nIndent |
| 3489 | && (abYield[p2op] || sqlite3_column_int(pSql, 2)) |
| 3490 | ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3491 | for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3492 | } |
| 3493 | } |
| 3494 | |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 3495 | p->iIndent = 0; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3496 | sqlite3_free(abYield); |
| 3497 | sqlite3_reset(pSql); |
| 3498 | } |
| 3499 | |
| 3500 | /* |
| 3501 | ** Free the array allocated by explain_data_prepare(). |
| 3502 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3503 | static void explain_data_delete(ShellState *p){ |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3504 | sqlite3_free(p->aiIndent); |
| 3505 | p->aiIndent = 0; |
| 3506 | p->nIndent = 0; |
dan | c4650bb | 2013-11-18 08:41:06 +0000 | [diff] [blame] | 3507 | p->iIndent = 0; |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | /* |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3511 | ** Disable and restore .wheretrace and .selecttrace settings. |
| 3512 | */ |
| 3513 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 3514 | extern int sqlite3SelectTrace; |
| 3515 | static int savedSelectTrace; |
| 3516 | #endif |
| 3517 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 3518 | extern int sqlite3WhereTrace; |
| 3519 | static int savedWhereTrace; |
| 3520 | #endif |
| 3521 | static void disable_debug_trace_modes(void){ |
| 3522 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 3523 | savedSelectTrace = sqlite3SelectTrace; |
| 3524 | sqlite3SelectTrace = 0; |
| 3525 | #endif |
| 3526 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 3527 | savedWhereTrace = sqlite3WhereTrace; |
| 3528 | sqlite3WhereTrace = 0; |
| 3529 | #endif |
| 3530 | } |
| 3531 | static void restore_debug_trace_modes(void){ |
| 3532 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 3533 | sqlite3SelectTrace = savedSelectTrace; |
| 3534 | #endif |
| 3535 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 3536 | sqlite3WhereTrace = savedWhereTrace; |
| 3537 | #endif |
| 3538 | } |
| 3539 | |
| 3540 | /* |
| 3541 | ** Run a prepared statement |
| 3542 | */ |
| 3543 | static void exec_prepared_stmt( |
| 3544 | ShellState *pArg, /* Pointer to ShellState */ |
| 3545 | sqlite3_stmt *pStmt, /* Statment to run */ |
| 3546 | int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */ |
| 3547 | ){ |
| 3548 | int rc; |
| 3549 | |
| 3550 | /* perform the first step. this will tell us if we |
| 3551 | ** have a result set or not and how wide it is. |
| 3552 | */ |
| 3553 | rc = sqlite3_step(pStmt); |
| 3554 | /* if we have a result set... */ |
| 3555 | if( SQLITE_ROW == rc ){ |
| 3556 | /* if we have a callback... */ |
| 3557 | if( xCallback ){ |
| 3558 | /* allocate space for col name ptr, value ptr, and type */ |
| 3559 | int nCol = sqlite3_column_count(pStmt); |
| 3560 | void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); |
| 3561 | if( !pData ){ |
| 3562 | rc = SQLITE_NOMEM; |
| 3563 | }else{ |
| 3564 | char **azCols = (char **)pData; /* Names of result columns */ |
| 3565 | char **azVals = &azCols[nCol]; /* Results */ |
| 3566 | int *aiTypes = (int *)&azVals[nCol]; /* Result types */ |
| 3567 | int i, x; |
| 3568 | assert(sizeof(int) <= sizeof(char *)); |
| 3569 | /* save off ptrs to column names */ |
| 3570 | for(i=0; i<nCol; i++){ |
| 3571 | azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| 3572 | } |
| 3573 | do{ |
| 3574 | /* extract the data and data types */ |
| 3575 | for(i=0; i<nCol; i++){ |
| 3576 | aiTypes[i] = x = sqlite3_column_type(pStmt, i); |
| 3577 | if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ |
| 3578 | azVals[i] = ""; |
| 3579 | }else{ |
| 3580 | azVals[i] = (char*)sqlite3_column_text(pStmt, i); |
| 3581 | } |
| 3582 | if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ |
| 3583 | rc = SQLITE_NOMEM; |
| 3584 | break; /* from for */ |
| 3585 | } |
| 3586 | } /* end for */ |
| 3587 | |
| 3588 | /* if data and types extracted successfully... */ |
| 3589 | if( SQLITE_ROW == rc ){ |
| 3590 | /* call the supplied callback with the result row data */ |
| 3591 | if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){ |
| 3592 | rc = SQLITE_ABORT; |
| 3593 | }else{ |
| 3594 | rc = sqlite3_step(pStmt); |
| 3595 | } |
| 3596 | } |
| 3597 | } while( SQLITE_ROW == rc ); |
| 3598 | sqlite3_free(pData); |
| 3599 | } |
| 3600 | }else{ |
| 3601 | do{ |
| 3602 | rc = sqlite3_step(pStmt); |
| 3603 | } while( rc == SQLITE_ROW ); |
| 3604 | } |
| 3605 | } |
| 3606 | } |
| 3607 | |
| 3608 | /* |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3609 | ** Execute a statement or set of statements. Print |
| 3610 | ** any result rows/columns depending on the current mode |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3611 | ** set via the supplied callback. |
| 3612 | ** |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3613 | ** This is very similar to SQLite's built-in sqlite3_exec() |
| 3614 | ** function except it takes a slightly different callback |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3615 | ** and callback data argument. |
| 3616 | */ |
| 3617 | static int shell_exec( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3618 | sqlite3 *db, /* An open database */ |
| 3619 | const char *zSql, /* SQL to be evaluated */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3620 | int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3621 | /* (not the same as sqlite3_exec) */ |
| 3622 | ShellState *pArg, /* Pointer to ShellState */ |
| 3623 | char **pzErrMsg /* Error msg written here */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3624 | ){ |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 3625 | sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
| 3626 | int rc = SQLITE_OK; /* Return Code */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 3627 | int rc2; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 3628 | const char *zLeftover; /* Tail of unprocessed SQL */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3629 | |
| 3630 | if( pzErrMsg ){ |
| 3631 | *pzErrMsg = NULL; |
| 3632 | } |
| 3633 | |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 3634 | while( zSql[0] && (SQLITE_OK == rc) ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3635 | static const char *zStmtSql; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 3636 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 3637 | if( SQLITE_OK != rc ){ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3638 | if( pzErrMsg ){ |
| 3639 | *pzErrMsg = save_err_msg(db); |
| 3640 | } |
| 3641 | }else{ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 3642 | if( !pStmt ){ |
| 3643 | /* this happens for a comment or white-space */ |
| 3644 | zSql = zLeftover; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 3645 | while( IsSpace(zSql[0]) ) zSql++; |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 3646 | continue; |
| 3647 | } |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3648 | zStmtSql = sqlite3_sql(pStmt); |
drh | 6027561 | 2016-11-03 02:25:30 +0000 | [diff] [blame] | 3649 | if( zStmtSql==0 ) zStmtSql = ""; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3650 | while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3651 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3652 | /* save off the prepared statment handle and reset row count */ |
| 3653 | if( pArg ){ |
| 3654 | pArg->pStmt = pStmt; |
| 3655 | pArg->cnt = 0; |
| 3656 | } |
| 3657 | |
shaneh | b7977c5 | 2010-01-18 18:17:10 +0000 | [diff] [blame] | 3658 | /* echo the sql statement if echo on */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 3659 | if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){ |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3660 | utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); |
drh | a8c62df | 2010-02-15 15:47:18 +0000 | [diff] [blame] | 3661 | } |
shaneh | b7977c5 | 2010-01-18 18:17:10 +0000 | [diff] [blame] | 3662 | |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 3663 | /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3664 | if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){ |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 3665 | sqlite3_stmt *pExplain; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3666 | char *zEQP; |
| 3667 | disable_debug_trace_modes(); |
| 3668 | zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 3669 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 3670 | if( rc==SQLITE_OK ){ |
| 3671 | while( sqlite3_step(pExplain)==SQLITE_ROW ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3672 | raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0)); |
| 3673 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1)); |
| 3674 | raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2)); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3675 | utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3)); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 3676 | } |
| 3677 | } |
| 3678 | sqlite3_finalize(pExplain); |
| 3679 | sqlite3_free(zEQP); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3680 | if( pArg->autoEQP>=2 ){ |
| 3681 | /* Also do an EXPLAIN for ".eqp full" mode */ |
| 3682 | zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); |
| 3683 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 3684 | if( rc==SQLITE_OK ){ |
| 3685 | pArg->cMode = MODE_Explain; |
| 3686 | explain_data_prepare(pArg, pExplain); |
| 3687 | exec_prepared_stmt(pArg, pExplain, xCallback); |
| 3688 | explain_data_delete(pArg); |
| 3689 | } |
| 3690 | sqlite3_finalize(pExplain); |
| 3691 | sqlite3_free(zEQP); |
| 3692 | } |
| 3693 | restore_debug_trace_modes(); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 3696 | if( pArg ){ |
| 3697 | pArg->cMode = pArg->mode; |
drh | 87a24aa | 2016-02-09 20:04:07 +0000 | [diff] [blame] | 3698 | if( pArg->autoExplain |
| 3699 | && sqlite3_column_count(pStmt)==8 |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3700 | && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0 |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 3701 | ){ |
| 3702 | pArg->cMode = MODE_Explain; |
| 3703 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3704 | |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 3705 | /* If the shell is currently in ".explain" mode, gather the extra |
| 3706 | ** data required to add indents to the output.*/ |
| 3707 | if( pArg->cMode==MODE_Explain ){ |
| 3708 | explain_data_prepare(pArg, pStmt); |
| 3709 | } |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 3712 | exec_prepared_stmt(pArg, pStmt, xCallback); |
dan | a98bf36 | 2013-11-13 18:35:01 +0000 | [diff] [blame] | 3713 | explain_data_delete(pArg); |
| 3714 | |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3715 | /* print usage stats if stats on */ |
| 3716 | if( pArg && pArg->statsOn ){ |
| 3717 | display_stats(db, pArg, 0); |
| 3718 | } |
| 3719 | |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 3720 | /* print loop-counters if required */ |
| 3721 | if( pArg && pArg->scanstatsOn ){ |
| 3722 | display_scanstats(db, pArg); |
| 3723 | } |
| 3724 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3725 | /* Finalize the statement just executed. If this fails, save a |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 3726 | ** copy of the error message. Otherwise, set zSql to point to the |
| 3727 | ** next statement to execute. */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 3728 | rc2 = sqlite3_finalize(pStmt); |
| 3729 | if( rc!=SQLITE_NOMEM ) rc = rc2; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 3730 | if( rc==SQLITE_OK ){ |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 3731 | zSql = zLeftover; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 3732 | while( IsSpace(zSql[0]) ) zSql++; |
dan | 4564ced | 2010-01-05 04:59:56 +0000 | [diff] [blame] | 3733 | }else if( pzErrMsg ){ |
| 3734 | *pzErrMsg = save_err_msg(db); |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3735 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 3736 | |
| 3737 | /* clear saved stmt handle */ |
| 3738 | if( pArg ){ |
| 3739 | pArg->pStmt = NULL; |
| 3740 | } |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3741 | } |
shane | b9fc17d | 2009-10-22 21:23:35 +0000 | [diff] [blame] | 3742 | } /* end while */ |
shane | 626a6e4 | 2009-10-22 17:30:15 +0000 | [diff] [blame] | 3743 | |
| 3744 | return rc; |
| 3745 | } |
| 3746 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3747 | /* |
| 3748 | ** Release memory previously allocated by tableColumnList(). |
| 3749 | */ |
| 3750 | static void freeColumnList(char **azCol){ |
| 3751 | int i; |
| 3752 | for(i=1; azCol[i]; i++){ |
| 3753 | sqlite3_free(azCol[i]); |
| 3754 | } |
| 3755 | /* azCol[0] is a static string */ |
| 3756 | sqlite3_free(azCol); |
| 3757 | } |
| 3758 | |
| 3759 | /* |
| 3760 | ** Return a list of pointers to strings which are the names of all |
| 3761 | ** columns in table zTab. The memory to hold the names is dynamically |
| 3762 | ** allocated and must be released by the caller using a subsequent call |
| 3763 | ** to freeColumnList(). |
| 3764 | ** |
| 3765 | ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid |
| 3766 | ** value that needs to be preserved, then azCol[0] is filled in with the |
| 3767 | ** name of the rowid column. |
| 3768 | ** |
| 3769 | ** The first regular column in the table is azCol[1]. The list is terminated |
| 3770 | ** by an entry with azCol[i]==0. |
| 3771 | */ |
| 3772 | static char **tableColumnList(ShellState *p, const char *zTab){ |
| 3773 | char **azCol = 0; |
| 3774 | sqlite3_stmt *pStmt; |
| 3775 | char *zSql; |
| 3776 | int nCol = 0; |
| 3777 | int nAlloc = 0; |
| 3778 | int nPK = 0; /* Number of PRIMARY KEY columns seen */ |
| 3779 | int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 3780 | int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3781 | int rc; |
| 3782 | |
| 3783 | zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); |
| 3784 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3785 | sqlite3_free(zSql); |
| 3786 | if( rc ) return 0; |
| 3787 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3788 | if( nCol>=nAlloc-2 ){ |
| 3789 | nAlloc = nAlloc*2 + nCol + 10; |
| 3790 | azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); |
| 3791 | if( azCol==0 ){ |
| 3792 | raw_printf(stderr, "Error: out of memory\n"); |
| 3793 | exit(1); |
| 3794 | } |
| 3795 | } |
| 3796 | azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); |
| 3797 | if( sqlite3_column_int(pStmt, 5) ){ |
| 3798 | nPK++; |
| 3799 | if( nPK==1 |
| 3800 | && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 3801 | "INTEGER")==0 |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3802 | ){ |
| 3803 | isIPK = 1; |
| 3804 | }else{ |
| 3805 | isIPK = 0; |
| 3806 | } |
| 3807 | } |
| 3808 | } |
| 3809 | sqlite3_finalize(pStmt); |
| 3810 | azCol[0] = 0; |
| 3811 | azCol[nCol+1] = 0; |
| 3812 | |
| 3813 | /* The decision of whether or not a rowid really needs to be preserved |
| 3814 | ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table |
| 3815 | ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve |
| 3816 | ** rowids on tables where the rowid is inaccessible because there are other |
| 3817 | ** columns in the table named "rowid", "_rowid_", and "oid". |
| 3818 | */ |
| 3819 | if( preserveRowid && isIPK ){ |
| 3820 | /* If a single PRIMARY KEY column with type INTEGER was seen, then it |
| 3821 | ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID |
| 3822 | ** table or a INTEGER PRIMARY KEY DESC column, neither of which are |
| 3823 | ** ROWID aliases. To distinguish these cases, check to see if |
| 3824 | ** there is a "pk" entry in "PRAGMA index_list". There will be |
| 3825 | ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. |
| 3826 | */ |
| 3827 | zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" |
| 3828 | " WHERE origin='pk'", zTab); |
| 3829 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 3830 | sqlite3_free(zSql); |
| 3831 | if( rc ){ |
| 3832 | freeColumnList(azCol); |
| 3833 | return 0; |
| 3834 | } |
| 3835 | rc = sqlite3_step(pStmt); |
| 3836 | sqlite3_finalize(pStmt); |
| 3837 | preserveRowid = rc==SQLITE_ROW; |
| 3838 | } |
| 3839 | if( preserveRowid ){ |
| 3840 | /* Only preserve the rowid if we can find a name to use for the |
| 3841 | ** rowid */ |
| 3842 | static char *azRowid[] = { "rowid", "_rowid_", "oid" }; |
| 3843 | int i, j; |
| 3844 | for(j=0; j<3; j++){ |
| 3845 | for(i=1; i<=nCol; i++){ |
| 3846 | if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; |
| 3847 | } |
| 3848 | if( i>nCol ){ |
| 3849 | /* At this point, we know that azRowid[j] is not the name of any |
| 3850 | ** ordinary column in the table. Verify that azRowid[j] is a valid |
| 3851 | ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID |
| 3852 | ** tables will fail this last check */ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3853 | rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); |
| 3854 | if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; |
| 3855 | break; |
| 3856 | } |
| 3857 | } |
| 3858 | } |
| 3859 | return azCol; |
| 3860 | } |
| 3861 | |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 3862 | /* |
drh | f8563c0 | 2017-03-09 18:13:52 +0000 | [diff] [blame] | 3863 | ** Toggle the reverse_unordered_selects setting. |
| 3864 | */ |
| 3865 | static void toggleSelectOrder(sqlite3 *db){ |
| 3866 | sqlite3_stmt *pStmt = 0; |
| 3867 | int iSetting = 0; |
| 3868 | char zStmt[100]; |
| 3869 | sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); |
| 3870 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 3871 | iSetting = sqlite3_column_int(pStmt, 0); |
| 3872 | } |
| 3873 | sqlite3_finalize(pStmt); |
| 3874 | sqlite3_snprintf(sizeof(zStmt), zStmt, |
| 3875 | "PRAGMA reverse_unordered_selects(%d)", !iSetting); |
| 3876 | sqlite3_exec(db, zStmt, 0, 0, 0); |
| 3877 | } |
| 3878 | |
| 3879 | /* |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3880 | ** This is a different callback routine used for dumping the database. |
| 3881 | ** Each row received by this callback consists of a table name, |
| 3882 | ** the table type ("index" or "table") and SQL to create the table. |
| 3883 | ** This routine should print text sufficient to recreate the table. |
| 3884 | */ |
drh | 701ff6a | 2017-03-22 12:51:34 +0000 | [diff] [blame] | 3885 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3886 | int rc; |
| 3887 | const char *zTable; |
| 3888 | const char *zType; |
| 3889 | const char *zSql; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 3890 | ShellState *p = (ShellState *)pArg; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3891 | |
drh | 701ff6a | 2017-03-22 12:51:34 +0000 | [diff] [blame] | 3892 | UNUSED_PARAMETER(azNotUsed); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3893 | if( nArg!=3 ) return 1; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3894 | zTable = azArg[0]; |
| 3895 | zType = azArg[1]; |
| 3896 | zSql = azArg[2]; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3897 | |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 3898 | if( strcmp(zTable, "sqlite_sequence")==0 ){ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3899 | raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); |
drh | 7ed1032 | 2013-08-07 16:04:27 +0000 | [diff] [blame] | 3900 | }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3901 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 3902 | }else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
| 3903 | return 0; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3904 | }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
| 3905 | char *zIns; |
| 3906 | if( !p->writableSchema ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 3907 | raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3908 | p->writableSchema = 1; |
| 3909 | } |
| 3910 | zIns = sqlite3_mprintf( |
| 3911 | "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" |
| 3912 | "VALUES('table','%q','%q',0,'%q');", |
| 3913 | zTable, zTable, zSql); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 3914 | utf8_printf(p->out, "%s\n", zIns); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3915 | sqlite3_free(zIns); |
| 3916 | return 0; |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 3917 | }else{ |
drh | 79f20e9 | 2016-12-13 23:22:39 +0000 | [diff] [blame] | 3918 | printSchemaLine(p->out, zSql, ";\n"); |
drh | f8eb96a | 2005-02-03 00:42:34 +0000 | [diff] [blame] | 3919 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3920 | |
| 3921 | if( strcmp(zType, "table")==0 ){ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3922 | ShellText sSelect; |
| 3923 | ShellText sTable; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3924 | char **azCol; |
| 3925 | int i; |
| 3926 | char *savedDestTable; |
| 3927 | int savedMode; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3928 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3929 | azCol = tableColumnList(p, zTable); |
| 3930 | if( azCol==0 ){ |
| 3931 | p->nErr++; |
| 3932 | return 0; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3933 | } |
| 3934 | |
drh | bf92ec0 | 2012-03-22 12:50:34 +0000 | [diff] [blame] | 3935 | /* Always quote the table name, even if it appears to be pure ascii, |
| 3936 | ** in case it is a keyword. Ex: INSERT INTO "table" ... */ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3937 | initText(&sTable); |
| 3938 | appendText(&sTable, zTable, quoteChar(zTable)); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3939 | /* If preserving the rowid, add a column list after the table name. |
| 3940 | ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" |
| 3941 | ** instead of the usual "INSERT INTO tab VALUES(...)". |
| 3942 | */ |
| 3943 | if( azCol[0] ){ |
| 3944 | appendText(&sTable, "(", 0); |
| 3945 | appendText(&sTable, azCol[0], 0); |
| 3946 | for(i=1; azCol[i]; i++){ |
| 3947 | appendText(&sTable, ",", 0); |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3948 | appendText(&sTable, azCol[i], quoteChar(azCol[i])); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3949 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3950 | appendText(&sTable, ")", 0); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3951 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 3952 | |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3953 | /* Build an appropriate SELECT statement */ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3954 | initText(&sSelect); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3955 | appendText(&sSelect, "SELECT ", 0); |
| 3956 | if( azCol[0] ){ |
| 3957 | appendText(&sSelect, azCol[0], 0); |
| 3958 | appendText(&sSelect, ",", 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3959 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3960 | for(i=1; azCol[i]; i++){ |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3961 | appendText(&sSelect, azCol[i], quoteChar(azCol[i])); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3962 | if( azCol[i+1] ){ |
| 3963 | appendText(&sSelect, ",", 0); |
| 3964 | } |
| 3965 | } |
| 3966 | freeColumnList(azCol); |
| 3967 | appendText(&sSelect, " FROM ", 0); |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3968 | appendText(&sSelect, zTable, quoteChar(zTable)); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3969 | |
| 3970 | savedDestTable = p->zDestTable; |
| 3971 | savedMode = p->mode; |
| 3972 | p->zDestTable = sTable.z; |
| 3973 | p->mode = p->cMode = MODE_Insert; |
| 3974 | rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0); |
drh | f8563c0 | 2017-03-09 18:13:52 +0000 | [diff] [blame] | 3975 | if( (rc&0xff)==SQLITE_CORRUPT ){ |
| 3976 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
| 3977 | toggleSelectOrder(p->db); |
| 3978 | shell_exec(p->db, sSelect.z, shell_callback, p, 0); |
| 3979 | toggleSelectOrder(p->db); |
| 3980 | } |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3981 | p->zDestTable = savedDestTable; |
| 3982 | p->mode = savedMode; |
drh | f42d318 | 2017-03-08 12:25:18 +0000 | [diff] [blame] | 3983 | freeText(&sTable); |
| 3984 | freeText(&sSelect); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 3985 | if( rc ) p->nErr++; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3986 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 3987 | return 0; |
| 3988 | } |
| 3989 | |
| 3990 | /* |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 3991 | ** Run zQuery. Use dump_callback() as the callback routine so that |
| 3992 | ** the contents of the query are output as SQL statements. |
| 3993 | ** |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 3994 | ** If we get a SQLITE_CORRUPT error, rerun the query after appending |
| 3995 | ** "ORDER BY rowid DESC" to the end. |
| 3996 | */ |
| 3997 | static int run_schema_dump_query( |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 3998 | ShellState *p, |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 3999 | const char *zQuery |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 4000 | ){ |
| 4001 | int rc; |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4002 | char *zErr = 0; |
| 4003 | rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 4004 | if( rc==SQLITE_CORRUPT ){ |
| 4005 | char *zQ2; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 4006 | int len = strlen30(zQuery); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4007 | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4008 | if( zErr ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4009 | utf8_printf(p->out, "/****** %s ******/\n", zErr); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4010 | sqlite3_free(zErr); |
| 4011 | zErr = 0; |
| 4012 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 4013 | zQ2 = malloc( len+100 ); |
| 4014 | if( zQ2==0 ) return rc; |
drh | 8c5058b | 2012-04-16 17:22:30 +0000 | [diff] [blame] | 4015 | sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4016 | rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
| 4017 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4018 | utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 4019 | }else{ |
| 4020 | rc = SQLITE_CORRUPT; |
| 4021 | } |
| 4022 | sqlite3_free(zErr); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 4023 | free(zQ2); |
| 4024 | } |
| 4025 | return rc; |
| 4026 | } |
| 4027 | |
| 4028 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4029 | ** Text of a help message |
| 4030 | */ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 4031 | static char zHelp[] = |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 4032 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 4033 | ".auth ON|OFF Show authorizer callbacks\n" |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 4034 | #endif |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4035 | ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4036 | ".bail on|off Stop after hitting an error. Default OFF\n" |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4037 | ".binary on|off Turn binary output on or off. Default OFF\n" |
drh | 453ca04 | 2017-05-22 18:00:34 +0000 | [diff] [blame] | 4038 | ".cd DIRECTORY Change the working directory to DIRECTORY\n" |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 4039 | ".changes on|off Show number of rows changed by SQL\n" |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4040 | ".check GLOB Fail if output since .testcase does not match\n" |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4041 | ".clone NEWDB Clone data into NEWDB from the existing database\n" |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 4042 | ".databases List names and files of attached databases\n" |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4043 | ".dbinfo ?DB? Show status information about the database\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 4044 | ".dump ?TABLE? ... Dump the database in an SQL text format\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 4045 | " If TABLE specified, only dump tables matching\n" |
| 4046 | " LIKE pattern TABLE.\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4047 | ".echo on|off Turn command echo on or off\n" |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 4048 | ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4049 | ".exit Exit this program\n" |
drh | c31b79d | 2017-06-29 21:11:27 +0000 | [diff] [blame] | 4050 | /* Because explain mode comes on automatically now, the ".explain" mode |
| 4051 | ** is removed from the help screen. It is still supported for legacy, however */ |
| 4052 | /*".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] | 4053 | ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4054 | ".headers on|off Turn display of headers on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4055 | ".help Show this message\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 4056 | ".import FILE TABLE Import data from FILE into TABLE\n" |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 4057 | #ifndef SQLITE_OMIT_TEST_CONTROL |
| 4058 | ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n" |
| 4059 | #endif |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4060 | ".indexes ?TABLE? Show names of all indexes\n" |
| 4061 | " If TABLE specified, only show indexes for tables\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 4062 | " matching LIKE pattern TABLE.\n" |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 4063 | #ifdef SQLITE_ENABLE_IOTRACE |
| 4064 | ".iotrace FILE Enable I/O diagnostic logging to FILE\n" |
| 4065 | #endif |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 4066 | ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n" |
drh | 3fd9f33 | 2016-12-16 18:41:11 +0000 | [diff] [blame] | 4067 | ".lint OPTIONS Report potential schema issues. Options:\n" |
| 4068 | " fkey-indexes Find missing foreign key indexes\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 4069 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 4070 | ".load FILE ?ENTRY? Load an extension library\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 4071 | #endif |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 4072 | ".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] | 4073 | ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 4074 | " ascii Columns/rows delimited by 0x1F and 0x1E\n" |
drh | 3b584fa | 2004-09-24 12:50:03 +0000 | [diff] [blame] | 4075 | " csv Comma-separated values\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 4076 | " column Left-aligned columns. (See .width)\n" |
| 4077 | " html HTML <table> code\n" |
| 4078 | " insert SQL insert statements for TABLE\n" |
| 4079 | " line One value per line\n" |
drh | 0c5cd96 | 2017-02-14 21:47:46 +0000 | [diff] [blame] | 4080 | " list Values delimited by \"|\"\n" |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 4081 | " quote Escape answers as for SQL\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 4082 | " tabs Tab-separated values\n" |
| 4083 | " tcl TCL list elements\n" |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 4084 | ".nullvalue STRING Use STRING in place of NULL values\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4085 | ".once FILENAME Output for the next SQL command only to FILENAME\n" |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4086 | ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n" |
| 4087 | " The --new option starts with an empty file\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4088 | ".output ?FILENAME? Send output to FILENAME or stdout\n" |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 4089 | ".print STRING... Print literal STRING\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4090 | ".prompt MAIN CONTINUE Replace the standard prompts\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 4091 | ".quit Exit this program\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 4092 | ".read FILENAME Execute SQL in FILENAME\n" |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 4093 | ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 4094 | ".save FILE Write in-memory database into FILE\n" |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 4095 | ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n" |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 4096 | ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n" |
| 4097 | " Add --indent for pretty-printing\n" |
drh | a5d75ba | 2017-03-15 14:20:34 +0000 | [diff] [blame] | 4098 | ".selftest ?--init? Run tests defined in the SELFTEST table\n" |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 4099 | ".separator COL ?ROW? Change the column separator and optionally the row\n" |
| 4100 | " separator for both the output mode and .import\n" |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4101 | #if defined(SQLITE_ENABLE_SESSION) |
| 4102 | ".session CMD ... Create or control sessions\n" |
| 4103 | #endif |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 4104 | ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 4105 | ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 4106 | ".show Show the current values for various settings\n" |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 4107 | ".stats ?on|off? Show stats or turn stats on or off\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 4108 | ".system CMD ARGS... Run CMD ARGS... in a system shell\n" |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 4109 | ".tables ?TABLE? List names of tables\n" |
| 4110 | " If TABLE specified, only list tables matching\n" |
| 4111 | " LIKE pattern TABLE.\n" |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 4112 | ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n" |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 4113 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4114 | ".timer on|off Turn SQL timer on or off\n" |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 4115 | ".trace FILE|off Output each SQL statement as it is run\n" |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 4116 | ".vfsinfo ?AUX? Information about the top-level VFS\n" |
drh | b19e735 | 2016-01-12 19:37:20 +0000 | [diff] [blame] | 4117 | ".vfslist List all available VFSes\n" |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 4118 | ".vfsname ?AUX? Print the name of the VFS stack\n" |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 4119 | ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 4120 | " Negative values right-justify\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4121 | ; |
| 4122 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4123 | #if defined(SQLITE_ENABLE_SESSION) |
| 4124 | /* |
| 4125 | ** Print help information for the ".sessions" command |
| 4126 | */ |
| 4127 | void session_help(ShellState *p){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 4128 | raw_printf(p->out, |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4129 | ".session ?NAME? SUBCOMMAND ?ARGS...?\n" |
| 4130 | "If ?NAME? is omitted, the first defined session is used.\n" |
| 4131 | "Subcommands:\n" |
| 4132 | " attach TABLE Attach TABLE\n" |
| 4133 | " changeset FILE Write a changeset into FILE\n" |
| 4134 | " close Close one session\n" |
| 4135 | " enable ?BOOLEAN? Set or query the enable bit\n" |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4136 | " filter GLOB... Reject tables matching GLOBs\n" |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4137 | " indirect ?BOOLEAN? Mark or query the indirect status\n" |
| 4138 | " isempty Query whether the session is empty\n" |
| 4139 | " list List currently open session names\n" |
| 4140 | " open DB NAME Open a new session on DB\n" |
| 4141 | " patchset FILE Write a patchset into FILE\n" |
| 4142 | ); |
| 4143 | } |
| 4144 | #endif |
| 4145 | |
| 4146 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 4147 | /* Forward reference */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4148 | static int process_input(ShellState *p, FILE *in); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4149 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4150 | /* |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 4151 | ** Read the content of file zName into memory obtained from sqlite3_malloc64() |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 4152 | ** and return a pointer to the buffer. The caller is responsible for freeing |
| 4153 | ** the memory. |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4154 | ** |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 4155 | ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes |
| 4156 | ** read. |
| 4157 | ** |
| 4158 | ** For convenience, a nul-terminator byte is always appended to the data read |
| 4159 | ** from the file before the buffer is returned. This byte is not included in |
| 4160 | ** the final value of (*pnByte), if applicable. |
| 4161 | ** |
| 4162 | ** NULL is returned if any error is encountered. The final value of *pnByte |
| 4163 | ** is undefined in this case. |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4164 | */ |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 4165 | static char *readFile(const char *zName, int *pnByte){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4166 | FILE *in = fopen(zName, "rb"); |
| 4167 | long nIn; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 4168 | size_t nRead; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4169 | char *pBuf; |
| 4170 | if( in==0 ) return 0; |
| 4171 | fseek(in, 0, SEEK_END); |
| 4172 | nIn = ftell(in); |
| 4173 | rewind(in); |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 4174 | pBuf = sqlite3_malloc64( nIn+1 ); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4175 | if( pBuf==0 ) return 0; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 4176 | nRead = fread(pBuf, nIn, 1, in); |
| 4177 | fclose(in); |
| 4178 | if( nRead!=1 ){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4179 | sqlite3_free(pBuf); |
| 4180 | return 0; |
| 4181 | } |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 4182 | pBuf[nIn] = 0; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 4183 | if( pnByte ) *pnByte = nIn; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 4184 | return pBuf; |
| 4185 | } |
| 4186 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4187 | #if defined(SQLITE_ENABLE_SESSION) |
| 4188 | /* |
| 4189 | ** Close a single OpenSession object and release all of its associated |
| 4190 | ** resources. |
| 4191 | */ |
| 4192 | static void session_close(OpenSession *pSession){ |
| 4193 | int i; |
| 4194 | sqlite3session_delete(pSession->p); |
| 4195 | sqlite3_free(pSession->zName); |
| 4196 | for(i=0; i<pSession->nFilter; i++){ |
| 4197 | sqlite3_free(pSession->azFilter[i]); |
| 4198 | } |
| 4199 | sqlite3_free(pSession->azFilter); |
| 4200 | memset(pSession, 0, sizeof(OpenSession)); |
| 4201 | } |
| 4202 | #endif |
| 4203 | |
| 4204 | /* |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 4205 | ** Close all OpenSession objects and release all associated resources. |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4206 | */ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4207 | #if defined(SQLITE_ENABLE_SESSION) |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 4208 | static void session_close_all(ShellState *p){ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4209 | int i; |
| 4210 | for(i=0; i<p->nSession; i++){ |
| 4211 | session_close(&p->aSession[i]); |
| 4212 | } |
| 4213 | p->nSession = 0; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4214 | } |
drh | 51b55a3 | 2016-04-04 12:38:05 +0000 | [diff] [blame] | 4215 | #else |
| 4216 | # define session_close_all(X) |
| 4217 | #endif |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 4218 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4219 | /* |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 4220 | ** Implementation of the xFilter function for an open session. Omit |
| 4221 | ** any tables named by ".session filter" but let all other table through. |
| 4222 | */ |
| 4223 | #if defined(SQLITE_ENABLE_SESSION) |
| 4224 | static int session_filter(void *pCtx, const char *zTab){ |
| 4225 | OpenSession *pSession = (OpenSession*)pCtx; |
| 4226 | int i; |
| 4227 | for(i=0; i<pSession->nFilter; i++){ |
| 4228 | if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; |
| 4229 | } |
| 4230 | return 1; |
| 4231 | } |
| 4232 | #endif |
| 4233 | |
| 4234 | /* |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 4235 | ** Make sure the database is open. If it is not, then open it. If |
| 4236 | ** the database fails to open, print an error message and exit. |
| 4237 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4238 | static void open_db(ShellState *p, int keepAlive){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 4239 | if( p->db==0 ){ |
drh | bbb0be8 | 2012-06-27 16:12:27 +0000 | [diff] [blame] | 4240 | sqlite3_initialize(); |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 4241 | sqlite3_open(p->zDbFilename, &p->db); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4242 | globalDb = p->db; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4243 | if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4244 | utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4245 | p->zDbFilename, sqlite3_errmsg(p->db)); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 4246 | if( keepAlive ) return; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 4247 | exit(1); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 4248 | } |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 4249 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 4250 | sqlite3_enable_load_extension(p->db, 1); |
| 4251 | #endif |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 4252 | sqlite3_fileio_init(p->db, 0, 0); |
| 4253 | sqlite3_shathree_init(p->db, 0, 0); |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 4254 | sqlite3_completion_init(p->db, 0, 0); |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 4255 | sqlite3_create_function(p->db, "shell_add_schema", 2, SQLITE_UTF8, 0, |
| 4256 | shellAddSchemaName, 0, 0); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 4257 | } |
| 4258 | } |
| 4259 | |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 4260 | #if HAVE_READLINE || HAVE_EDITLINE |
| 4261 | /* |
| 4262 | ** Readline completion callbacks |
| 4263 | */ |
| 4264 | static char *readline_completion_generator(const char *text, int state){ |
| 4265 | static sqlite3_stmt *pStmt = 0; |
| 4266 | char *zRet; |
| 4267 | if( state==0 ){ |
| 4268 | char *zSql; |
| 4269 | int rc; |
| 4270 | sqlite3_finalize(pStmt); |
| 4271 | zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" |
| 4272 | " FROM completion(%Q) ORDER BY 1", text); |
| 4273 | sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); |
| 4274 | sqlite3_free(zSql); |
| 4275 | } |
| 4276 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4277 | zRet = strdup(sqlite3_column_text(pStmt, 0)); |
| 4278 | }else{ |
| 4279 | sqlite3_finalize(pStmt); |
| 4280 | pStmt = 0; |
| 4281 | zRet = 0; |
| 4282 | } |
| 4283 | return zRet; |
| 4284 | } |
| 4285 | static char **readline_completion(const char *zText, int iStart, int iEnd){ |
| 4286 | rl_attempted_completion_over = 1; |
| 4287 | return rl_completion_matches(zText, readline_completion_generator); |
| 4288 | } |
| 4289 | |
| 4290 | #elif HAVE_LINENOISE |
| 4291 | /* |
| 4292 | ** Linenoise completion callback |
| 4293 | */ |
| 4294 | static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){ |
| 4295 | int nLine = (int)strlen(zLine); |
| 4296 | int i, iStart; |
| 4297 | sqlite3_stmt *pStmt = 0; |
| 4298 | char *zSql; |
| 4299 | char zBuf[1000]; |
| 4300 | |
| 4301 | if( nLine>sizeof(zBuf)-30 ) return; |
| 4302 | if( zLine[0]=='.' ) return; |
| 4303 | for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){} |
| 4304 | if( i==nLine-1 ) return; |
| 4305 | iStart = i+1; |
| 4306 | memcpy(zBuf, zLine, iStart); |
| 4307 | zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" |
| 4308 | " FROM completion(%Q,%Q) ORDER BY 1", |
| 4309 | &zLine[iStart], zLine); |
| 4310 | sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); |
| 4311 | sqlite3_free(zSql); |
| 4312 | sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */ |
| 4313 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4314 | const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0); |
| 4315 | int nCompletion = sqlite3_column_bytes(pStmt, 0); |
| 4316 | if( iStart+nCompletion < sizeof(zBuf)-1 ){ |
| 4317 | memcpy(zBuf+iStart, zCompletion, nCompletion+1); |
| 4318 | linenoiseAddCompletion(lc, zBuf); |
| 4319 | } |
| 4320 | } |
| 4321 | sqlite3_finalize(pStmt); |
| 4322 | } |
| 4323 | #endif |
| 4324 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 4325 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4326 | ** Do C-language style dequoting. |
| 4327 | ** |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4328 | ** \a -> alarm |
| 4329 | ** \b -> backspace |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4330 | ** \t -> tab |
| 4331 | ** \n -> newline |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4332 | ** \v -> vertical tab |
| 4333 | ** \f -> form feed |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4334 | ** \r -> carriage return |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4335 | ** \s -> space |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 4336 | ** \" -> " |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4337 | ** \' -> ' |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4338 | ** \\ -> backslash |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4339 | ** \NNN -> ascii character NNN in octal |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4340 | */ |
| 4341 | static void resolve_backslashes(char *z){ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 4342 | int i, j; |
| 4343 | char c; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4344 | while( *z && *z!='\\' ) z++; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4345 | for(i=j=0; (c = z[i])!=0; i++, j++){ |
drh | 4b60803 | 2015-04-15 19:25:25 +0000 | [diff] [blame] | 4346 | if( c=='\\' && z[i+1]!=0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4347 | c = z[++i]; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4348 | if( c=='a' ){ |
| 4349 | c = '\a'; |
| 4350 | }else if( c=='b' ){ |
| 4351 | c = '\b'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4352 | }else if( c=='t' ){ |
| 4353 | c = '\t'; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4354 | }else if( c=='n' ){ |
| 4355 | c = '\n'; |
| 4356 | }else if( c=='v' ){ |
| 4357 | c = '\v'; |
| 4358 | }else if( c=='f' ){ |
| 4359 | c = '\f'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4360 | }else if( c=='r' ){ |
| 4361 | c = '\r'; |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 4362 | }else if( c=='"' ){ |
| 4363 | c = '"'; |
| 4364 | }else if( c=='\'' ){ |
| 4365 | c = '\''; |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 4366 | }else if( c=='\\' ){ |
| 4367 | c = '\\'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4368 | }else if( c>='0' && c<='7' ){ |
drh | aa81608 | 2005-12-29 12:53:09 +0000 | [diff] [blame] | 4369 | c -= '0'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4370 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 4371 | i++; |
| 4372 | c = (c<<3) + z[i] - '0'; |
| 4373 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 4374 | i++; |
| 4375 | c = (c<<3) + z[i] - '0'; |
| 4376 | } |
| 4377 | } |
| 4378 | } |
| 4379 | } |
| 4380 | z[j] = c; |
| 4381 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4382 | if( j<i ) z[j] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | /* |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4386 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 4387 | ** is not a hex digit. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 4388 | */ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4389 | static int hexDigitValue(char c){ |
| 4390 | if( c>='0' && c<='9' ) return c - '0'; |
| 4391 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 4392 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 4393 | return -1; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | /* |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 4397 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 4398 | */ |
| 4399 | static sqlite3_int64 integerValue(const char *zArg){ |
| 4400 | sqlite3_int64 v = 0; |
| 4401 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 4402 | { "KiB", 1024 }, |
| 4403 | { "MiB", 1024*1024 }, |
| 4404 | { "GiB", 1024*1024*1024 }, |
| 4405 | { "KB", 1000 }, |
| 4406 | { "MB", 1000000 }, |
| 4407 | { "GB", 1000000000 }, |
| 4408 | { "K", 1000 }, |
| 4409 | { "M", 1000000 }, |
| 4410 | { "G", 1000000000 }, |
| 4411 | }; |
| 4412 | int i; |
| 4413 | int isNeg = 0; |
| 4414 | if( zArg[0]=='-' ){ |
| 4415 | isNeg = 1; |
| 4416 | zArg++; |
| 4417 | }else if( zArg[0]=='+' ){ |
| 4418 | zArg++; |
| 4419 | } |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4420 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 4421 | int x; |
| 4422 | zArg += 2; |
| 4423 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 4424 | v = (v<<4) + x; |
| 4425 | zArg++; |
| 4426 | } |
| 4427 | }else{ |
| 4428 | while( IsDigit(zArg[0]) ){ |
| 4429 | v = v*10 + zArg[0] - '0'; |
| 4430 | zArg++; |
| 4431 | } |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 4432 | } |
drh | c2bed0a | 2013-05-24 11:57:50 +0000 | [diff] [blame] | 4433 | for(i=0; i<ArraySize(aMult); i++){ |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 4434 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 4435 | v *= aMult[i].iMult; |
| 4436 | break; |
| 4437 | } |
| 4438 | } |
| 4439 | return isNeg? -v : v; |
| 4440 | } |
| 4441 | |
| 4442 | /* |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4443 | ** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
| 4444 | ** for TRUE and FALSE. Return the integer value if appropriate. |
| 4445 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4446 | static int booleanValue(const char *zArg){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4447 | int i; |
| 4448 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 4449 | for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
| 4450 | }else{ |
| 4451 | for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
| 4452 | } |
| 4453 | if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); |
| 4454 | if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ |
| 4455 | return 1; |
| 4456 | } |
| 4457 | if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ |
| 4458 | return 0; |
| 4459 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4460 | utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 4461 | zArg); |
| 4462 | return 0; |
| 4463 | } |
| 4464 | |
| 4465 | /* |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 4466 | ** Set or clear a shell flag according to a boolean value. |
| 4467 | */ |
| 4468 | static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){ |
| 4469 | if( booleanValue(zArg) ){ |
| 4470 | ShellSetFlag(p, mFlag); |
| 4471 | }else{ |
| 4472 | ShellClearFlag(p, mFlag); |
| 4473 | } |
| 4474 | } |
| 4475 | |
| 4476 | /* |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 4477 | ** Close an output file, assuming it is not stderr or stdout |
| 4478 | */ |
| 4479 | static void output_file_close(FILE *f){ |
| 4480 | if( f && f!=stdout && f!=stderr ) fclose(f); |
| 4481 | } |
| 4482 | |
| 4483 | /* |
| 4484 | ** Try to open an output file. The names "stdout" and "stderr" are |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4485 | ** recognized and do the right thing. NULL is returned if the output |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 4486 | ** filename is "off". |
| 4487 | */ |
| 4488 | static FILE *output_file_open(const char *zFile){ |
| 4489 | FILE *f; |
| 4490 | if( strcmp(zFile,"stdout")==0 ){ |
| 4491 | f = stdout; |
| 4492 | }else if( strcmp(zFile, "stderr")==0 ){ |
| 4493 | f = stderr; |
| 4494 | }else if( strcmp(zFile, "off")==0 ){ |
| 4495 | f = 0; |
| 4496 | }else{ |
| 4497 | f = fopen(zFile, "wb"); |
| 4498 | if( f==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4499 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 4500 | } |
| 4501 | } |
| 4502 | return f; |
| 4503 | } |
| 4504 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 4505 | #if !defined(SQLITE_UNTESTABLE) |
drh | c10b9da | 2016-11-20 17:59:59 +0000 | [diff] [blame] | 4506 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 4507 | /* |
| 4508 | ** A routine for handling output from sqlite3_trace(). |
| 4509 | */ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 4510 | static int sql_trace_callback( |
| 4511 | unsigned mType, |
| 4512 | void *pArg, |
| 4513 | void *pP, |
| 4514 | void *pX |
| 4515 | ){ |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 4516 | FILE *f = (FILE*)pArg; |
drh | b0df540 | 2016-08-01 17:06:44 +0000 | [diff] [blame] | 4517 | UNUSED_PARAMETER(mType); |
| 4518 | UNUSED_PARAMETER(pP); |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 4519 | if( f ){ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 4520 | const char *z = (const char*)pX; |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 4521 | int i = (int)strlen(z); |
| 4522 | while( i>0 && z[i-1]==';' ){ i--; } |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 4523 | utf8_printf(f, "%.*s;\n", i, z); |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 4524 | } |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 4525 | return 0; |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 4526 | } |
drh | c10b9da | 2016-11-20 17:59:59 +0000 | [diff] [blame] | 4527 | #endif |
| 4528 | #endif |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 4529 | |
| 4530 | /* |
drh | d8621b9 | 2012-04-17 09:09:33 +0000 | [diff] [blame] | 4531 | ** A no-op routine that runs with the ".breakpoint" doc-command. This is |
| 4532 | ** a useful spot to set a debugger breakpoint. |
| 4533 | */ |
| 4534 | static void test_breakpoint(void){ |
| 4535 | static int nCall = 0; |
| 4536 | nCall++; |
| 4537 | } |
| 4538 | |
| 4539 | /* |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4540 | ** An object used to read a CSV and other files for import. |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4541 | */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4542 | typedef struct ImportCtx ImportCtx; |
| 4543 | struct ImportCtx { |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4544 | const char *zFile; /* Name of the input file */ |
| 4545 | FILE *in; /* Read the CSV text from this input stream */ |
| 4546 | char *z; /* Accumulated text for a field */ |
| 4547 | int n; /* Number of bytes in z */ |
| 4548 | int nAlloc; /* Space allocated for z[] */ |
| 4549 | int nLine; /* Current line number */ |
drh | d5fbde8 | 2017-06-26 18:42:23 +0000 | [diff] [blame] | 4550 | int bNotFirst; /* True if one or more bytes already read */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4551 | int cTerm; /* Character that terminated the most recent field */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4552 | int cColSep; /* The column separator character. (Usually ",") */ |
| 4553 | int cRowSep; /* The row separator character. (Usually "\n") */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4554 | }; |
| 4555 | |
| 4556 | /* Append a single byte to z[] */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4557 | static void import_append_char(ImportCtx *p, int c){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4558 | if( p->n+1>=p->nAlloc ){ |
| 4559 | p->nAlloc += p->nAlloc + 100; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4560 | p->z = sqlite3_realloc64(p->z, p->nAlloc); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4561 | if( p->z==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4562 | raw_printf(stderr, "out of memory\n"); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4563 | exit(1); |
| 4564 | } |
| 4565 | } |
| 4566 | p->z[p->n++] = (char)c; |
| 4567 | } |
| 4568 | |
| 4569 | /* Read a single field of CSV text. Compatible with rfc4180 and extended |
| 4570 | ** with the option of having a separator other than ",". |
| 4571 | ** |
| 4572 | ** + Input comes from p->in. |
| 4573 | ** + 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] | 4574 | ** from sqlite3_malloc64(). |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4575 | ** + Use p->cSep as the column separator. The default is ",". |
| 4576 | ** + Use p->rSep as the row separator. The default is "\n". |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4577 | ** + Keep track of the line number in p->nLine. |
| 4578 | ** + Store the character that terminates the field in p->cTerm. Store |
| 4579 | ** EOF on end-of-file. |
| 4580 | ** + Report syntax errors on stderr |
| 4581 | */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 4582 | static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4583 | int c; |
| 4584 | int cSep = p->cColSep; |
| 4585 | int rSep = p->cRowSep; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4586 | p->n = 0; |
| 4587 | c = fgetc(p->in); |
| 4588 | if( c==EOF || seenInterrupt ){ |
| 4589 | p->cTerm = EOF; |
| 4590 | return 0; |
| 4591 | } |
| 4592 | if( c=='"' ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4593 | int pc, ppc; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4594 | int startLine = p->nLine; |
| 4595 | int cQuote = c; |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 4596 | pc = ppc = 0; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4597 | while( 1 ){ |
| 4598 | c = fgetc(p->in); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4599 | if( c==rSep ) p->nLine++; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4600 | if( c==cQuote ){ |
| 4601 | if( pc==cQuote ){ |
| 4602 | pc = 0; |
| 4603 | continue; |
| 4604 | } |
| 4605 | } |
| 4606 | if( (c==cSep && pc==cQuote) |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4607 | || (c==rSep && pc==cQuote) |
| 4608 | || (c==rSep && pc=='\r' && ppc==cQuote) |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4609 | || (c==EOF && pc==cQuote) |
| 4610 | ){ |
| 4611 | do{ p->n--; }while( p->z[p->n]!=cQuote ); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4612 | p->cTerm = c; |
| 4613 | break; |
| 4614 | } |
| 4615 | if( pc==cQuote && c!='\r' ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4616 | utf8_printf(stderr, "%s:%d: unescaped %c character\n", |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4617 | p->zFile, p->nLine, cQuote); |
| 4618 | } |
| 4619 | if( c==EOF ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4620 | utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4621 | p->zFile, startLine, cQuote); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4622 | p->cTerm = c; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4623 | break; |
| 4624 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4625 | import_append_char(p, c); |
drh | a81ad17 | 2013-12-11 14:00:04 +0000 | [diff] [blame] | 4626 | ppc = pc; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4627 | pc = c; |
drh | d0a64dc | 2013-06-30 20:24:26 +0000 | [diff] [blame] | 4628 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4629 | }else{ |
drh | d5fbde8 | 2017-06-26 18:42:23 +0000 | [diff] [blame] | 4630 | /* If this is the first field being parsed and it begins with the |
| 4631 | ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ |
| 4632 | if( (c&0xff)==0xef && p->bNotFirst==0 ){ |
| 4633 | import_append_char(p, c); |
| 4634 | c = fgetc(p->in); |
| 4635 | if( (c&0xff)==0xbb ){ |
| 4636 | import_append_char(p, c); |
| 4637 | c = fgetc(p->in); |
| 4638 | if( (c&0xff)==0xbf ){ |
| 4639 | p->bNotFirst = 1; |
| 4640 | p->n = 0; |
| 4641 | return csv_read_one_field(p); |
| 4642 | } |
| 4643 | } |
| 4644 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4645 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 4646 | import_append_char(p, c); |
drh | d0a64dc | 2013-06-30 20:24:26 +0000 | [diff] [blame] | 4647 | c = fgetc(p->in); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4648 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4649 | if( c==rSep ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4650 | p->nLine++; |
drh | 3852b68 | 2014-02-26 13:53:34 +0000 | [diff] [blame] | 4651 | if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4652 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4653 | p->cTerm = c; |
| 4654 | } |
drh | 8dd675e | 2013-07-12 21:09:24 +0000 | [diff] [blame] | 4655 | if( p->z ) p->z[p->n] = 0; |
drh | d5fbde8 | 2017-06-26 18:42:23 +0000 | [diff] [blame] | 4656 | p->bNotFirst = 1; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4657 | return p->z; |
| 4658 | } |
| 4659 | |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4660 | /* Read a single field of ASCII delimited text. |
| 4661 | ** |
| 4662 | ** + Input comes from p->in. |
| 4663 | ** + 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] | 4664 | ** from sqlite3_malloc64(). |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4665 | ** + Use p->cSep as the column separator. The default is "\x1F". |
| 4666 | ** + Use p->rSep as the row separator. The default is "\x1E". |
| 4667 | ** + Keep track of the row number in p->nLine. |
| 4668 | ** + Store the character that terminates the field in p->cTerm. Store |
| 4669 | ** EOF on end-of-file. |
| 4670 | ** + Report syntax errors on stderr |
| 4671 | */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 4672 | static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 4673 | int c; |
| 4674 | int cSep = p->cColSep; |
| 4675 | int rSep = p->cRowSep; |
| 4676 | p->n = 0; |
| 4677 | c = fgetc(p->in); |
| 4678 | if( c==EOF || seenInterrupt ){ |
| 4679 | p->cTerm = EOF; |
| 4680 | return 0; |
| 4681 | } |
| 4682 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 4683 | import_append_char(p, c); |
| 4684 | c = fgetc(p->in); |
| 4685 | } |
| 4686 | if( c==rSep ){ |
| 4687 | p->nLine++; |
| 4688 | } |
| 4689 | p->cTerm = c; |
| 4690 | if( p->z ) p->z[p->n] = 0; |
| 4691 | return p->z; |
| 4692 | } |
| 4693 | |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 4694 | /* |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4695 | ** Try to transfer data for table zTable. If an error is seen while |
| 4696 | ** moving forward, try to go backwards. The backwards movement won't |
| 4697 | ** work for WITHOUT ROWID tables. |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4698 | */ |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 4699 | static void tryToCloneData( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4700 | ShellState *p, |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4701 | sqlite3 *newDb, |
| 4702 | const char *zTable |
| 4703 | ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 4704 | sqlite3_stmt *pQuery = 0; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4705 | sqlite3_stmt *pInsert = 0; |
| 4706 | char *zQuery = 0; |
| 4707 | char *zInsert = 0; |
| 4708 | int rc; |
| 4709 | int i, j, n; |
| 4710 | int nTable = (int)strlen(zTable); |
| 4711 | int k = 0; |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4712 | int cnt = 0; |
| 4713 | const int spinRate = 10000; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4714 | |
| 4715 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
| 4716 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4717 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4718 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4719 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 4720 | zQuery); |
| 4721 | goto end_data_xfer; |
| 4722 | } |
| 4723 | n = sqlite3_column_count(pQuery); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4724 | zInsert = sqlite3_malloc64(200 + nTable + n*3); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4725 | if( zInsert==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4726 | raw_printf(stderr, "out of memory\n"); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4727 | goto end_data_xfer; |
| 4728 | } |
| 4729 | sqlite3_snprintf(200+nTable,zInsert, |
| 4730 | "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); |
| 4731 | i = (int)strlen(zInsert); |
| 4732 | for(j=1; j<n; j++){ |
| 4733 | memcpy(zInsert+i, ",?", 2); |
| 4734 | i += 2; |
| 4735 | } |
| 4736 | memcpy(zInsert+i, ");", 3); |
| 4737 | rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); |
| 4738 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4739 | utf8_printf(stderr, "Error %d: %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4740 | sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), |
| 4741 | zQuery); |
| 4742 | goto end_data_xfer; |
| 4743 | } |
| 4744 | for(k=0; k<2; k++){ |
| 4745 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4746 | for(i=0; i<n; i++){ |
| 4747 | switch( sqlite3_column_type(pQuery, i) ){ |
| 4748 | case SQLITE_NULL: { |
| 4749 | sqlite3_bind_null(pInsert, i+1); |
| 4750 | break; |
| 4751 | } |
| 4752 | case SQLITE_INTEGER: { |
| 4753 | sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); |
| 4754 | break; |
| 4755 | } |
| 4756 | case SQLITE_FLOAT: { |
| 4757 | sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); |
| 4758 | break; |
| 4759 | } |
| 4760 | case SQLITE_TEXT: { |
| 4761 | sqlite3_bind_text(pInsert, i+1, |
| 4762 | (const char*)sqlite3_column_text(pQuery,i), |
| 4763 | -1, SQLITE_STATIC); |
| 4764 | break; |
| 4765 | } |
| 4766 | case SQLITE_BLOB: { |
| 4767 | sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), |
| 4768 | sqlite3_column_bytes(pQuery,i), |
| 4769 | SQLITE_STATIC); |
| 4770 | break; |
| 4771 | } |
| 4772 | } |
| 4773 | } /* End for */ |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4774 | rc = sqlite3_step(pInsert); |
| 4775 | if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4776 | utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4777 | sqlite3_errmsg(newDb)); |
| 4778 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4779 | sqlite3_reset(pInsert); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4780 | cnt++; |
| 4781 | if( (cnt%spinRate)==0 ){ |
| 4782 | printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); |
| 4783 | fflush(stdout); |
| 4784 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4785 | } /* End while */ |
| 4786 | if( rc==SQLITE_DONE ) break; |
| 4787 | sqlite3_finalize(pQuery); |
| 4788 | sqlite3_free(zQuery); |
| 4789 | zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", |
| 4790 | zTable); |
| 4791 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4792 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4793 | utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4794 | break; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4795 | } |
| 4796 | } /* End for(k=0...) */ |
| 4797 | |
| 4798 | end_data_xfer: |
| 4799 | sqlite3_finalize(pQuery); |
| 4800 | sqlite3_finalize(pInsert); |
| 4801 | sqlite3_free(zQuery); |
| 4802 | sqlite3_free(zInsert); |
| 4803 | } |
| 4804 | |
| 4805 | |
| 4806 | /* |
| 4807 | ** Try to transfer all rows of the schema that match zWhere. For |
| 4808 | ** each row, invoke xForEach() on the object defined by that row. |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4809 | ** If an error is encountered while moving forward through the |
| 4810 | ** sqlite_master table, try again moving backwards. |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4811 | */ |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 4812 | static void tryToCloneSchema( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4813 | ShellState *p, |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4814 | sqlite3 *newDb, |
| 4815 | const char *zWhere, |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4816 | void (*xForEach)(ShellState*,sqlite3*,const char*) |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4817 | ){ |
| 4818 | sqlite3_stmt *pQuery = 0; |
| 4819 | char *zQuery = 0; |
| 4820 | int rc; |
| 4821 | const unsigned char *zName; |
| 4822 | const unsigned char *zSql; |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4823 | char *zErrMsg = 0; |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4824 | |
| 4825 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 4826 | " WHERE %s", zWhere); |
| 4827 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4828 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4829 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4830 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 4831 | zQuery); |
| 4832 | goto end_schema_xfer; |
| 4833 | } |
| 4834 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4835 | zName = sqlite3_column_text(pQuery, 0); |
| 4836 | zSql = sqlite3_column_text(pQuery, 1); |
| 4837 | printf("%s... ", zName); fflush(stdout); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4838 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 4839 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4840 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4841 | sqlite3_free(zErrMsg); |
| 4842 | zErrMsg = 0; |
| 4843 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4844 | if( xForEach ){ |
| 4845 | xForEach(p, newDb, (const char*)zName); |
| 4846 | } |
| 4847 | printf("done\n"); |
| 4848 | } |
| 4849 | if( rc!=SQLITE_DONE ){ |
| 4850 | sqlite3_finalize(pQuery); |
| 4851 | sqlite3_free(zQuery); |
| 4852 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
| 4853 | " WHERE %s ORDER BY rowid DESC", zWhere); |
| 4854 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 4855 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4856 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4857 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 4858 | zQuery); |
| 4859 | goto end_schema_xfer; |
| 4860 | } |
| 4861 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 4862 | zName = sqlite3_column_text(pQuery, 0); |
| 4863 | zSql = sqlite3_column_text(pQuery, 1); |
| 4864 | printf("%s... ", zName); fflush(stdout); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4865 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 4866 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4867 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
drh | 4bbcf10 | 2014-02-06 02:46:08 +0000 | [diff] [blame] | 4868 | sqlite3_free(zErrMsg); |
| 4869 | zErrMsg = 0; |
| 4870 | } |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4871 | if( xForEach ){ |
| 4872 | xForEach(p, newDb, (const char*)zName); |
| 4873 | } |
| 4874 | printf("done\n"); |
| 4875 | } |
| 4876 | } |
| 4877 | end_schema_xfer: |
| 4878 | sqlite3_finalize(pQuery); |
| 4879 | sqlite3_free(zQuery); |
| 4880 | } |
| 4881 | |
| 4882 | /* |
| 4883 | ** Open a new database file named "zNewDb". Try to recover as much information |
| 4884 | ** as possible out of the main database (which might be corrupt) and write it |
| 4885 | ** into zNewDb. |
| 4886 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4887 | static void tryToClone(ShellState *p, const char *zNewDb){ |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4888 | int rc; |
| 4889 | sqlite3 *newDb = 0; |
| 4890 | if( access(zNewDb,0)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4891 | utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4892 | return; |
| 4893 | } |
| 4894 | rc = sqlite3_open(zNewDb, &newDb); |
| 4895 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4896 | utf8_printf(stderr, "Cannot create output database: %s\n", |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4897 | sqlite3_errmsg(newDb)); |
| 4898 | }else{ |
drh | 54d0d2d | 2014-04-03 00:32:13 +0000 | [diff] [blame] | 4899 | sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4900 | sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 4901 | tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); |
| 4902 | tryToCloneSchema(p, newDb, "type!='table'", 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4903 | sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); |
drh | 54d0d2d | 2014-04-03 00:32:13 +0000 | [diff] [blame] | 4904 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
drh | 3350ce9 | 2014-02-06 00:49:12 +0000 | [diff] [blame] | 4905 | } |
| 4906 | sqlite3_close(newDb); |
| 4907 | } |
| 4908 | |
| 4909 | /* |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4910 | ** Change the output file back to stdout |
| 4911 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 4912 | static void output_reset(ShellState *p){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4913 | if( p->outfile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4914 | #ifndef SQLITE_OMIT_POPEN |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4915 | pclose(p->out); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4916 | #endif |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 4917 | }else{ |
| 4918 | output_file_close(p->out); |
| 4919 | } |
| 4920 | p->outfile[0] = 0; |
| 4921 | p->out = stdout; |
| 4922 | } |
| 4923 | |
| 4924 | /* |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4925 | ** Run an SQL command and return the single integer result. |
| 4926 | */ |
| 4927 | static int db_int(ShellState *p, const char *zSql){ |
| 4928 | sqlite3_stmt *pStmt; |
| 4929 | int res = 0; |
| 4930 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 4931 | if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4932 | res = sqlite3_column_int(pStmt,0); |
| 4933 | } |
| 4934 | sqlite3_finalize(pStmt); |
| 4935 | return res; |
| 4936 | } |
| 4937 | |
| 4938 | /* |
| 4939 | ** Convert a 2-byte or 4-byte big-endian integer into a native integer |
| 4940 | */ |
drh | a0620ac | 2016-07-13 13:05:13 +0000 | [diff] [blame] | 4941 | static unsigned int get2byteInt(unsigned char *a){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4942 | return (a[0]<<8) + a[1]; |
| 4943 | } |
drh | a0620ac | 2016-07-13 13:05:13 +0000 | [diff] [blame] | 4944 | static unsigned int get4byteInt(unsigned char *a){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4945 | return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 4946 | } |
| 4947 | |
| 4948 | /* |
| 4949 | ** Implementation of the ".info" command. |
| 4950 | ** |
| 4951 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 4952 | */ |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4953 | static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4954 | static const struct { const char *zName; int ofst; } aField[] = { |
| 4955 | { "file change counter:", 24 }, |
| 4956 | { "database page count:", 28 }, |
| 4957 | { "freelist page count:", 36 }, |
| 4958 | { "schema cookie:", 40 }, |
| 4959 | { "schema format:", 44 }, |
| 4960 | { "default cache size:", 48 }, |
| 4961 | { "autovacuum top root:", 52 }, |
| 4962 | { "incremental vacuum:", 64 }, |
| 4963 | { "text encoding:", 56 }, |
| 4964 | { "user version:", 60 }, |
| 4965 | { "application id:", 68 }, |
| 4966 | { "software version:", 96 }, |
| 4967 | }; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4968 | static const struct { const char *zName; const char *zSql; } aQuery[] = { |
| 4969 | { "number of tables:", |
| 4970 | "SELECT count(*) FROM %s WHERE type='table'" }, |
| 4971 | { "number of indexes:", |
| 4972 | "SELECT count(*) FROM %s WHERE type='index'" }, |
| 4973 | { "number of triggers:", |
| 4974 | "SELECT count(*) FROM %s WHERE type='trigger'" }, |
| 4975 | { "number of views:", |
| 4976 | "SELECT count(*) FROM %s WHERE type='view'" }, |
| 4977 | { "schema size:", |
| 4978 | "SELECT total(length(sql)) FROM %s" }, |
| 4979 | }; |
mistachkin | bfe8bd5 | 2015-11-17 19:17:14 +0000 | [diff] [blame] | 4980 | sqlite3_file *pFile = 0; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4981 | int i; |
| 4982 | char *zSchemaTab; |
| 4983 | char *zDb = nArg>=2 ? azArg[1] : "main"; |
| 4984 | unsigned char aHdr[100]; |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4985 | open_db(p, 0); |
| 4986 | if( p->db==0 ) return 1; |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 4987 | sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4988 | if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){ |
| 4989 | return 1; |
| 4990 | } |
| 4991 | i = pFile->pMethods->xRead(pFile, aHdr, 100, 0); |
| 4992 | if( i!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4993 | raw_printf(stderr, "unable to read database header\n"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 4994 | return 1; |
| 4995 | } |
| 4996 | i = get2byteInt(aHdr+16); |
| 4997 | if( i==1 ) i = 65536; |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 4998 | utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
| 4999 | utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
| 5000 | utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); |
| 5001 | utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 5002 | for(i=0; i<ArraySize(aField); i++){ |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 5003 | int ofst = aField[i].ofst; |
| 5004 | unsigned int val = get4byteInt(aHdr + ofst); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5005 | utf8_printf(p->out, "%-20s %u", aField[i].zName, val); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 5006 | switch( ofst ){ |
| 5007 | case 56: { |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5008 | if( val==1 ) raw_printf(p->out, " (utf8)"); |
| 5009 | if( val==2 ) raw_printf(p->out, " (utf16le)"); |
| 5010 | if( val==3 ) raw_printf(p->out, " (utf16be)"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 5011 | } |
| 5012 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5013 | raw_printf(p->out, "\n"); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 5014 | } |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 5015 | if( zDb==0 ){ |
| 5016 | zSchemaTab = sqlite3_mprintf("main.sqlite_master"); |
| 5017 | }else if( strcmp(zDb,"temp")==0 ){ |
| 5018 | zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); |
| 5019 | }else{ |
| 5020 | zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); |
| 5021 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 5022 | for(i=0; i<ArraySize(aQuery); i++){ |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 5023 | char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); |
| 5024 | int val = db_int(p, zSql); |
| 5025 | sqlite3_free(zSql); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 5026 | utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 5027 | } |
| 5028 | sqlite3_free(zSchemaTab); |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 5029 | return 0; |
| 5030 | } |
| 5031 | |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 5032 | /* |
| 5033 | ** Print the current sqlite3_errmsg() value to stderr and return 1. |
| 5034 | */ |
| 5035 | static int shellDatabaseError(sqlite3 *db){ |
| 5036 | const char *zErr = sqlite3_errmsg(db); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5037 | utf8_printf(stderr, "Error: %s\n", zErr); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 5038 | return 1; |
| 5039 | } |
| 5040 | |
| 5041 | /* |
| 5042 | ** Print an out-of-memory message to stderr and return 1. |
| 5043 | */ |
| 5044 | static int shellNomemError(void){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5045 | raw_printf(stderr, "Error: out of memory\n"); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 5046 | return 1; |
| 5047 | } |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 5048 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 5049 | /* |
| 5050 | ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE |
| 5051 | ** if they match and FALSE (0) if they do not match. |
| 5052 | ** |
| 5053 | ** Globbing rules: |
| 5054 | ** |
| 5055 | ** '*' Matches any sequence of zero or more characters. |
| 5056 | ** |
| 5057 | ** '?' Matches exactly one character. |
| 5058 | ** |
| 5059 | ** [...] Matches one character from the enclosed list of |
| 5060 | ** characters. |
| 5061 | ** |
| 5062 | ** [^...] Matches one character not in the enclosed list. |
| 5063 | ** |
| 5064 | ** '#' Matches any sequence of one or more digits with an |
| 5065 | ** optional + or - sign in front |
| 5066 | ** |
| 5067 | ** ' ' Any span of whitespace matches any other span of |
| 5068 | ** whitespace. |
| 5069 | ** |
| 5070 | ** Extra whitespace at the end of z[] is ignored. |
| 5071 | */ |
| 5072 | static int testcase_glob(const char *zGlob, const char *z){ |
| 5073 | int c, c2; |
| 5074 | int invert; |
| 5075 | int seen; |
| 5076 | |
| 5077 | while( (c = (*(zGlob++)))!=0 ){ |
| 5078 | if( IsSpace(c) ){ |
| 5079 | if( !IsSpace(*z) ) return 0; |
| 5080 | while( IsSpace(*zGlob) ) zGlob++; |
| 5081 | while( IsSpace(*z) ) z++; |
| 5082 | }else if( c=='*' ){ |
| 5083 | while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
| 5084 | if( c=='?' && (*(z++))==0 ) return 0; |
| 5085 | } |
| 5086 | if( c==0 ){ |
| 5087 | return 1; |
| 5088 | }else if( c=='[' ){ |
| 5089 | while( *z && testcase_glob(zGlob-1,z)==0 ){ |
| 5090 | z++; |
| 5091 | } |
| 5092 | return (*z)!=0; |
| 5093 | } |
| 5094 | while( (c2 = (*(z++)))!=0 ){ |
| 5095 | while( c2!=c ){ |
| 5096 | c2 = *(z++); |
| 5097 | if( c2==0 ) return 0; |
| 5098 | } |
| 5099 | if( testcase_glob(zGlob,z) ) return 1; |
| 5100 | } |
| 5101 | return 0; |
| 5102 | }else if( c=='?' ){ |
| 5103 | if( (*(z++))==0 ) return 0; |
| 5104 | }else if( c=='[' ){ |
| 5105 | int prior_c = 0; |
| 5106 | seen = 0; |
| 5107 | invert = 0; |
| 5108 | c = *(z++); |
| 5109 | if( c==0 ) return 0; |
| 5110 | c2 = *(zGlob++); |
| 5111 | if( c2=='^' ){ |
| 5112 | invert = 1; |
| 5113 | c2 = *(zGlob++); |
| 5114 | } |
| 5115 | if( c2==']' ){ |
| 5116 | if( c==']' ) seen = 1; |
| 5117 | c2 = *(zGlob++); |
| 5118 | } |
| 5119 | while( c2 && c2!=']' ){ |
| 5120 | if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
| 5121 | c2 = *(zGlob++); |
| 5122 | if( c>=prior_c && c<=c2 ) seen = 1; |
| 5123 | prior_c = 0; |
| 5124 | }else{ |
| 5125 | if( c==c2 ){ |
| 5126 | seen = 1; |
| 5127 | } |
| 5128 | prior_c = c2; |
| 5129 | } |
| 5130 | c2 = *(zGlob++); |
| 5131 | } |
| 5132 | if( c2==0 || (seen ^ invert)==0 ) return 0; |
| 5133 | }else if( c=='#' ){ |
| 5134 | if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; |
| 5135 | if( !IsDigit(z[0]) ) return 0; |
| 5136 | z++; |
| 5137 | while( IsDigit(z[0]) ){ z++; } |
| 5138 | }else{ |
| 5139 | if( c!=(*(z++)) ) return 0; |
| 5140 | } |
| 5141 | } |
| 5142 | while( IsSpace(*z) ){ z++; } |
| 5143 | return *z==0; |
| 5144 | } |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 5145 | |
| 5146 | |
drh | f7502f0 | 2015-02-06 14:19:44 +0000 | [diff] [blame] | 5147 | /* |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5148 | ** Compare the string as a command-line option with either one or two |
| 5149 | ** initial "-" characters. |
| 5150 | */ |
| 5151 | static int optionMatch(const char *zStr, const char *zOpt){ |
| 5152 | if( zStr[0]!='-' ) return 0; |
| 5153 | zStr++; |
| 5154 | if( zStr[0]=='-' ) zStr++; |
| 5155 | return strcmp(zStr, zOpt)==0; |
| 5156 | } |
| 5157 | |
| 5158 | /* |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5159 | ** Delete a file. |
| 5160 | */ |
| 5161 | int shellDeleteFile(const char *zFilename){ |
| 5162 | int rc; |
| 5163 | #ifdef _WIN32 |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 5164 | wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5165 | rc = _wunlink(z); |
| 5166 | sqlite3_free(z); |
| 5167 | #else |
| 5168 | rc = unlink(zFilename); |
| 5169 | #endif |
| 5170 | return rc; |
| 5171 | } |
| 5172 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5173 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5174 | /* |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 5175 | ** The implementation of SQL scalar function fkey_collate_clause(), used |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5176 | ** by the ".lint fkey-indexes" command. This scalar function is always |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 5177 | ** called with four arguments - the parent table name, the parent column name, |
| 5178 | ** the child table name and the child column name. |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5179 | ** |
| 5180 | ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') |
| 5181 | ** |
| 5182 | ** If either of the named tables or columns do not exist, this function |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 5183 | ** returns an empty string. An empty string is also returned if both tables |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5184 | ** and columns exist but have the same default collation sequence. Or, |
| 5185 | ** if both exist but the default collation sequences are different, this |
| 5186 | ** function returns the string " COLLATE <parent-collation>", where |
| 5187 | ** <parent-collation> is the default collation sequence of the parent column. |
| 5188 | */ |
| 5189 | static void shellFkeyCollateClause( |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 5190 | sqlite3_context *pCtx, |
| 5191 | int nVal, |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5192 | sqlite3_value **apVal |
| 5193 | ){ |
| 5194 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 5195 | const char *zParent; |
| 5196 | const char *zParentCol; |
| 5197 | const char *zParentSeq; |
| 5198 | const char *zChild; |
| 5199 | const char *zChildCol; |
drh | 96ada59 | 2016-12-29 19:48:46 +0000 | [diff] [blame] | 5200 | const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5201 | int rc; |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 5202 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5203 | assert( nVal==4 ); |
| 5204 | zParent = (const char*)sqlite3_value_text(apVal[0]); |
| 5205 | zParentCol = (const char*)sqlite3_value_text(apVal[1]); |
| 5206 | zChild = (const char*)sqlite3_value_text(apVal[2]); |
| 5207 | zChildCol = (const char*)sqlite3_value_text(apVal[3]); |
| 5208 | |
| 5209 | sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); |
| 5210 | rc = sqlite3_table_column_metadata( |
| 5211 | db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 |
| 5212 | ); |
| 5213 | if( rc==SQLITE_OK ){ |
| 5214 | rc = sqlite3_table_column_metadata( |
| 5215 | db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 |
| 5216 | ); |
| 5217 | } |
| 5218 | |
| 5219 | if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ |
| 5220 | char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); |
| 5221 | sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); |
| 5222 | sqlite3_free(z); |
| 5223 | } |
| 5224 | } |
| 5225 | |
| 5226 | |
| 5227 | /* |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5228 | ** The implementation of dot-command ".lint fkey-indexes". |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5229 | */ |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5230 | static int lintFkeyIndexes( |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5231 | ShellState *pState, /* Current shell tool state */ |
| 5232 | char **azArg, /* Array of arguments passed to dot command */ |
| 5233 | int nArg /* Number of entries in azArg[] */ |
| 5234 | ){ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 5235 | sqlite3 *db = pState->db; /* Database handle to query "main" db of */ |
| 5236 | FILE *out = pState->out; /* Stream to write non-error output to */ |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 5237 | int bVerbose = 0; /* If -verbose is present */ |
| 5238 | int bGroupByParent = 0; /* If -groupbyparent is present */ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 5239 | int i; /* To iterate through azArg[] */ |
| 5240 | const char *zIndent = ""; /* How much to indent CREATE INDEX by */ |
| 5241 | int rc; /* Return code */ |
| 5242 | sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5243 | |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 5244 | /* |
| 5245 | ** This SELECT statement returns one row for each foreign key constraint |
| 5246 | ** in the schema of the main database. The column values are: |
| 5247 | ** |
| 5248 | ** 0. The text of an SQL statement similar to: |
| 5249 | ** |
| 5250 | ** "EXPLAIN QUERY PLAN SELECT rowid FROM child_table WHERE child_key=?" |
| 5251 | ** |
| 5252 | ** This is the same SELECT that the foreign keys implementation needs |
| 5253 | ** to run internally on child tables. If there is an index that can |
| 5254 | ** be used to optimize this query, then it can also be used by the FK |
| 5255 | ** implementation to optimize DELETE or UPDATE statements on the parent |
| 5256 | ** table. |
| 5257 | ** |
| 5258 | ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by |
| 5259 | ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema |
| 5260 | ** contains an index that can be used to optimize the query. |
| 5261 | ** |
| 5262 | ** 2. Human readable text that describes the child table and columns. e.g. |
| 5263 | ** |
| 5264 | ** "child_table(child_key1, child_key2)" |
| 5265 | ** |
| 5266 | ** 3. Human readable text that describes the parent table and columns. e.g. |
| 5267 | ** |
| 5268 | ** "parent_table(parent_key1, parent_key2)" |
| 5269 | ** |
| 5270 | ** 4. A full CREATE INDEX statement for an index that could be used to |
| 5271 | ** optimize DELETE or UPDATE statements on the parent table. e.g. |
| 5272 | ** |
| 5273 | ** "CREATE INDEX child_table_child_key ON child_table(child_key)" |
| 5274 | ** |
| 5275 | ** 5. The name of the parent table. |
| 5276 | ** |
| 5277 | ** These six values are used by the C logic below to generate the report. |
| 5278 | */ |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5279 | const char *zSql = |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 5280 | "SELECT " |
| 5281 | " 'EXPLAIN QUERY PLAN SELECT rowid FROM ' || quote(s.name) || ' WHERE '" |
| 5282 | " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 5283 | " || fkey_collate_clause(" |
| 5284 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5285 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 5286 | " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5287 | " || group_concat('*=?', ' AND ') || ')'" |
| 5288 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 5289 | " s.name || '(' || group_concat(f.[from], ', ') || ')'" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5290 | ", " |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 5291 | " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5292 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 5293 | " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" |
| 5294 | " || ' ON ' || quote(s.name) || '('" |
| 5295 | " || group_concat(quote(f.[from]) ||" |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 5296 | " fkey_collate_clause(" |
| 5297 | " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5298 | " || ');'" |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 5299 | ", " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 5300 | " f.[table] " |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 5301 | "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f " |
dan | 50da938 | 2017-04-06 12:06:56 +0000 | [diff] [blame] | 5302 | "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] | 5303 | "GROUP BY s.name, f.id " |
| 5304 | "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5305 | ; |
dan | 54e2efc | 2017-04-06 14:56:26 +0000 | [diff] [blame] | 5306 | const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)"; |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5307 | |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5308 | for(i=2; i<nArg; i++){ |
drh | 344a1bf | 2016-12-22 14:53:25 +0000 | [diff] [blame] | 5309 | int n = (int)strlen(azArg[i]); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 5310 | if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ |
| 5311 | bVerbose = 1; |
| 5312 | } |
| 5313 | else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ |
| 5314 | bGroupByParent = 1; |
| 5315 | zIndent = " "; |
| 5316 | } |
| 5317 | else{ |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5318 | raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", |
| 5319 | azArg[0], azArg[1] |
| 5320 | ); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 5321 | return SQLITE_ERROR; |
| 5322 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5323 | } |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 5324 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5325 | /* Register the fkey_collate_clause() SQL function */ |
dan | dd9e0be | 2016-12-16 16:44:27 +0000 | [diff] [blame] | 5326 | rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, |
| 5327 | 0, shellFkeyCollateClause, 0, 0 |
| 5328 | ); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5329 | |
| 5330 | |
| 5331 | if( rc==SQLITE_OK ){ |
| 5332 | rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); |
| 5333 | } |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 5334 | if( rc==SQLITE_OK ){ |
| 5335 | sqlite3_bind_int(pSql, 1, bGroupByParent); |
| 5336 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5337 | |
| 5338 | if( rc==SQLITE_OK ){ |
| 5339 | int rc2; |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 5340 | char *zPrev = 0; |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5341 | while( SQLITE_ROW==sqlite3_step(pSql) ){ |
| 5342 | int res = -1; |
| 5343 | sqlite3_stmt *pExplain = 0; |
| 5344 | const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); |
| 5345 | const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); |
| 5346 | const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); |
| 5347 | const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); |
| 5348 | const char *zCI = (const char*)sqlite3_column_text(pSql, 4); |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 5349 | const char *zParent = (const char*)sqlite3_column_text(pSql, 5); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5350 | |
| 5351 | rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| 5352 | if( rc!=SQLITE_OK ) break; |
| 5353 | if( SQLITE_ROW==sqlite3_step(pExplain) ){ |
| 5354 | const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); |
dan | 54e2efc | 2017-04-06 14:56:26 +0000 | [diff] [blame] | 5355 | res = ( |
| 5356 | 0==sqlite3_strglob(zGlob, zPlan) |
| 5357 | || 0==sqlite3_strglob(zGlobIPK, zPlan) |
| 5358 | ); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5359 | } |
| 5360 | rc = sqlite3_finalize(pExplain); |
| 5361 | if( rc!=SQLITE_OK ) break; |
| 5362 | |
| 5363 | if( res<0 ){ |
| 5364 | raw_printf(stderr, "Error: internal error"); |
| 5365 | break; |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 5366 | }else{ |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 5367 | if( bGroupByParent |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 5368 | && (bVerbose || res==0) |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 5369 | && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 5370 | ){ |
| 5371 | raw_printf(out, "-- Parent table %s\n", zParent); |
| 5372 | sqlite3_free(zPrev); |
| 5373 | zPrev = sqlite3_mprintf("%s", zParent); |
| 5374 | } |
| 5375 | |
| 5376 | if( res==0 ){ |
| 5377 | raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); |
| 5378 | }else if( bVerbose ){ |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 5379 | raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 5380 | zIndent, zFrom, zTarget |
| 5381 | ); |
| 5382 | } |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5383 | } |
| 5384 | } |
dan | f9647b6 | 2016-12-15 06:01:40 +0000 | [diff] [blame] | 5385 | sqlite3_free(zPrev); |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5386 | |
| 5387 | if( rc!=SQLITE_OK ){ |
| 5388 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 5389 | } |
| 5390 | |
| 5391 | rc2 = sqlite3_finalize(pSql); |
| 5392 | if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ |
| 5393 | rc = rc2; |
| 5394 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 5395 | } |
| 5396 | }else{ |
| 5397 | raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
| 5398 | } |
| 5399 | |
| 5400 | return rc; |
| 5401 | } |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5402 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5403 | /* |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5404 | ** Implementation of ".lint" dot command. |
| 5405 | */ |
| 5406 | static int lintDotCommand( |
| 5407 | ShellState *pState, /* Current shell tool state */ |
| 5408 | char **azArg, /* Array of arguments passed to dot command */ |
| 5409 | int nArg /* Number of entries in azArg[] */ |
| 5410 | ){ |
| 5411 | int n; |
drh | 96ada59 | 2016-12-29 19:48:46 +0000 | [diff] [blame] | 5412 | n = (nArg>=2 ? (int)strlen(azArg[1]) : 0); |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 5413 | if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; |
| 5414 | return lintFkeyIndexes(pState, azArg, nArg); |
| 5415 | |
| 5416 | usage: |
| 5417 | raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); |
| 5418 | raw_printf(stderr, "Where sub-commands are:\n"); |
| 5419 | raw_printf(stderr, " fkey-indexes\n"); |
| 5420 | return SQLITE_ERROR; |
| 5421 | } |
| 5422 | |
dan | 35ac58e | 2016-12-14 19:28:27 +0000 | [diff] [blame] | 5423 | |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 5424 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5425 | ** If an input line begins with "." then invoke this routine to |
| 5426 | ** process that line. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5427 | ** |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5428 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5429 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 5430 | static int do_meta_command(char *zLine, ShellState *p){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5431 | int h = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5432 | int nArg = 0; |
| 5433 | int n, c; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 5434 | int rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5435 | char *azArg[50]; |
| 5436 | |
| 5437 | /* Parse the input line into tokens. |
| 5438 | */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5439 | while( zLine[h] && nArg<ArraySize(azArg) ){ |
| 5440 | while( IsSpace(zLine[h]) ){ h++; } |
| 5441 | if( zLine[h]==0 ) break; |
| 5442 | if( zLine[h]=='\'' || zLine[h]=='"' ){ |
| 5443 | int delim = zLine[h++]; |
| 5444 | azArg[nArg++] = &zLine[h]; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5445 | while( zLine[h] && zLine[h]!=delim ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5446 | if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5447 | h++; |
drh | 4c56b99 | 2013-06-27 13:26:55 +0000 | [diff] [blame] | 5448 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5449 | if( zLine[h]==delim ){ |
| 5450 | zLine[h++] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5451 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5452 | if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5453 | }else{ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5454 | azArg[nArg++] = &zLine[h]; |
| 5455 | while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } |
| 5456 | if( zLine[h] ) zLine[h++] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5457 | resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5458 | } |
| 5459 | } |
| 5460 | |
| 5461 | /* Process the input line. |
| 5462 | */ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5463 | if( nArg==0 ) return 0; /* no tokens, no error */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5464 | n = strlen30(azArg[0]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5465 | c = azArg[0][0]; |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 5466 | |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 5467 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 5468 | if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ |
| 5469 | if( nArg!=2 ){ |
| 5470 | raw_printf(stderr, "Usage: .auth ON|OFF\n"); |
| 5471 | rc = 1; |
| 5472 | goto meta_command_exit; |
| 5473 | } |
| 5474 | open_db(p, 0); |
| 5475 | if( booleanValue(azArg[1]) ){ |
| 5476 | sqlite3_set_authorizer(p->db, shellAuth, p); |
| 5477 | }else{ |
| 5478 | sqlite3_set_authorizer(p->db, 0, 0); |
| 5479 | } |
| 5480 | }else |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 5481 | #endif |
drh | de613c6 | 2016-04-04 17:23:10 +0000 | [diff] [blame] | 5482 | |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 5483 | if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) |
| 5484 | || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) |
| 5485 | ){ |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 5486 | const char *zDestFile = 0; |
| 5487 | const char *zDb = 0; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5488 | sqlite3 *pDest; |
| 5489 | sqlite3_backup *pBackup; |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 5490 | int j; |
| 5491 | for(j=1; j<nArg; j++){ |
| 5492 | const char *z = azArg[j]; |
| 5493 | if( z[0]=='-' ){ |
| 5494 | while( z[0]=='-' ) z++; |
drh | af66433 | 2013-07-18 20:28:29 +0000 | [diff] [blame] | 5495 | /* No options to process at this time */ |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 5496 | { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5497 | utf8_printf(stderr, "unknown option: %s\n", azArg[j]); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 5498 | return 1; |
| 5499 | } |
| 5500 | }else if( zDestFile==0 ){ |
| 5501 | zDestFile = azArg[j]; |
| 5502 | }else if( zDb==0 ){ |
| 5503 | zDb = zDestFile; |
| 5504 | zDestFile = azArg[j]; |
| 5505 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5506 | raw_printf(stderr, "too many arguments to .backup\n"); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 5507 | return 1; |
| 5508 | } |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5509 | } |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 5510 | if( zDestFile==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5511 | raw_printf(stderr, "missing FILENAME argument on .backup\n"); |
drh | bc46f02 | 2013-01-23 18:53:23 +0000 | [diff] [blame] | 5512 | return 1; |
| 5513 | } |
| 5514 | if( zDb==0 ) zDb = "main"; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5515 | rc = sqlite3_open(zDestFile, &pDest); |
| 5516 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5517 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5518 | sqlite3_close(pDest); |
| 5519 | return 1; |
| 5520 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5521 | open_db(p, 0); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5522 | pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
| 5523 | if( pBackup==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5524 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5525 | sqlite3_close(pDest); |
| 5526 | return 1; |
| 5527 | } |
| 5528 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
| 5529 | sqlite3_backup_finish(pBackup); |
| 5530 | if( rc==SQLITE_DONE ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5531 | rc = 0; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5532 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5533 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5534 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 5535 | } |
| 5536 | sqlite3_close(pDest); |
| 5537 | }else |
| 5538 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5539 | if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ |
| 5540 | if( nArg==2 ){ |
| 5541 | bail_on_error = booleanValue(azArg[1]); |
| 5542 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5543 | raw_printf(stderr, "Usage: .bail on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5544 | rc = 1; |
| 5545 | } |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 5546 | }else |
| 5547 | |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 5548 | if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ |
| 5549 | if( nArg==2 ){ |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 5550 | if( booleanValue(azArg[1]) ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5551 | setBinaryMode(p->out, 1); |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 5552 | }else{ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5553 | setTextMode(p->out, 1); |
mistachkin | bdffff9 | 2015-01-19 20:22:33 +0000 | [diff] [blame] | 5554 | } |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 5555 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5556 | raw_printf(stderr, "Usage: .binary on|off\n"); |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 5557 | rc = 1; |
| 5558 | } |
| 5559 | }else |
| 5560 | |
drh | 453ca04 | 2017-05-22 18:00:34 +0000 | [diff] [blame] | 5561 | if( c=='c' && strcmp(azArg[0],"cd")==0 ){ |
| 5562 | if( nArg==2 ){ |
| 5563 | #if defined(_WIN32) || defined(WIN32) |
| 5564 | wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); |
| 5565 | rc = !SetCurrentDirectoryW(z); |
| 5566 | sqlite3_free(z); |
| 5567 | #else |
| 5568 | rc = chdir(azArg[1]); |
| 5569 | #endif |
| 5570 | if( rc ){ |
| 5571 | utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]); |
| 5572 | rc = 1; |
| 5573 | } |
| 5574 | }else{ |
| 5575 | raw_printf(stderr, "Usage: .cd DIRECTORY\n"); |
| 5576 | rc = 1; |
| 5577 | } |
| 5578 | }else |
| 5579 | |
drh | d8621b9 | 2012-04-17 09:09:33 +0000 | [diff] [blame] | 5580 | /* The undocumented ".breakpoint" command causes a call to the no-op |
| 5581 | ** routine named test_breakpoint(). |
| 5582 | */ |
| 5583 | if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ |
| 5584 | test_breakpoint(); |
| 5585 | }else |
| 5586 | |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 5587 | if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
| 5588 | if( nArg==2 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 5589 | setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 5590 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5591 | raw_printf(stderr, "Usage: .changes on|off\n"); |
drh | df12f1c | 2015-12-07 21:46:19 +0000 | [diff] [blame] | 5592 | rc = 1; |
| 5593 | } |
| 5594 | }else |
| 5595 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 5596 | /* Cancel output redirection, if it is currently set (by .testcase) |
| 5597 | ** Then read the content of the testcase-out.txt file and compare against |
| 5598 | ** azArg[1]. If there are differences, report an error and exit. |
| 5599 | */ |
| 5600 | if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ |
| 5601 | char *zRes = 0; |
| 5602 | output_reset(p); |
| 5603 | if( nArg!=2 ){ |
| 5604 | raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5605 | rc = 2; |
dan | 11da002 | 2016-12-17 08:18:05 +0000 | [diff] [blame] | 5606 | }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 5607 | raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); |
| 5608 | rc = 2; |
| 5609 | }else if( testcase_glob(azArg[1],zRes)==0 ){ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 5610 | utf8_printf(stderr, |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5611 | "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", |
| 5612 | p->zTestcase, azArg[1], zRes); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 5613 | rc = 2; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5614 | }else{ |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 5615 | utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 5616 | p->nCheck++; |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 5617 | } |
| 5618 | sqlite3_free(zRes); |
| 5619 | }else |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 5620 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5621 | if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ |
| 5622 | if( nArg==2 ){ |
| 5623 | tryToClone(p, azArg[1]); |
| 5624 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5625 | raw_printf(stderr, "Usage: .clone FILENAME\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5626 | rc = 1; |
| 5627 | } |
mistachkin | e31ae90 | 2014-02-06 01:15:29 +0000 | [diff] [blame] | 5628 | }else |
| 5629 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5630 | if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 5631 | ShellState data; |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 5632 | char *zErrMsg = 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5633 | open_db(p, 0); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 5634 | memcpy(&data, p, sizeof(data)); |
drh | b20a61b | 2016-12-24 18:18:58 +0000 | [diff] [blame] | 5635 | data.showHeader = 0; |
| 5636 | data.cMode = data.mode = MODE_List; |
| 5637 | sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": "); |
drh | 0b2110c | 2004-10-26 00:08:10 +0000 | [diff] [blame] | 5638 | data.cnt = 0; |
drh | b20a61b | 2016-12-24 18:18:58 +0000 | [diff] [blame] | 5639 | sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list", |
| 5640 | callback, &data, &zErrMsg); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 5641 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5642 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 5643 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5644 | rc = 1; |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 5645 | } |
| 5646 | }else |
| 5647 | |
drh | 0e55db1 | 2015-02-06 14:51:13 +0000 | [diff] [blame] | 5648 | if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){ |
| 5649 | rc = shell_dbinfo_command(p, nArg, azArg); |
| 5650 | }else |
| 5651 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5652 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5653 | const char *zLike = 0; |
| 5654 | int i; |
drh | 72507d4 | 2017-04-08 00:55:13 +0000 | [diff] [blame] | 5655 | int savedShowHeader = p->showHeader; |
drh | dbc2672 | 2017-07-10 18:04:41 +0000 | [diff] [blame] | 5656 | ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5657 | for(i=1; i<nArg; i++){ |
| 5658 | if( azArg[i][0]=='-' ){ |
| 5659 | const char *z = azArg[i]+1; |
| 5660 | if( z[0]=='-' ) z++; |
| 5661 | if( strcmp(z,"preserve-rowids")==0 ){ |
drh | 34ad36b | 2017-03-25 18:15:05 +0000 | [diff] [blame] | 5662 | #ifdef SQLITE_OMIT_VIRTUALTABLE |
| 5663 | raw_printf(stderr, "The --preserve-rowids option is not compatible" |
| 5664 | " with SQLITE_OMIT_VIRTUALTABLE\n"); |
| 5665 | rc = 1; |
| 5666 | goto meta_command_exit; |
| 5667 | #else |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 5668 | ShellSetFlag(p, SHFLG_PreserveRowid); |
drh | 34ad36b | 2017-03-25 18:15:05 +0000 | [diff] [blame] | 5669 | #endif |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5670 | }else |
drh | dbc2672 | 2017-07-10 18:04:41 +0000 | [diff] [blame] | 5671 | if( strcmp(z,"newlines")==0 ){ |
| 5672 | ShellSetFlag(p, SHFLG_Newlines); |
| 5673 | }else |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5674 | { |
| 5675 | raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); |
| 5676 | rc = 1; |
| 5677 | goto meta_command_exit; |
| 5678 | } |
| 5679 | }else if( zLike ){ |
drh | dbc2672 | 2017-07-10 18:04:41 +0000 | [diff] [blame] | 5680 | raw_printf(stderr, "Usage: .dump ?--preserve-rowids? " |
| 5681 | "?--newlines? ?LIKE-PATTERN?\n"); |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5682 | rc = 1; |
| 5683 | goto meta_command_exit; |
| 5684 | }else{ |
| 5685 | zLike = azArg[i]; |
| 5686 | } |
| 5687 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5688 | open_db(p, 0); |
drh | f1dfc4f | 2009-09-23 15:51:35 +0000 | [diff] [blame] | 5689 | /* When playing back a "dump", the content might appear in an order |
| 5690 | ** which causes immediate foreign key constraints to be violated. |
| 5691 | ** So disable foreign-key constraint enforcement to prevent problems. */ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5692 | raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
| 5693 | raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 5694 | p->writableSchema = 0; |
drh | 72507d4 | 2017-04-08 00:55:13 +0000 | [diff] [blame] | 5695 | p->showHeader = 0; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5696 | /* Set writable_schema=ON since doing so forces SQLite to initialize |
| 5697 | ** as much of the schema as it can even if the sqlite_master table is |
| 5698 | ** corrupt. */ |
drh | 5619795 | 2011-10-13 16:30:13 +0000 | [diff] [blame] | 5699 | sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 5700 | p->nErr = 0; |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5701 | if( zLike==0 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5702 | run_schema_dump_query(p, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5703 | "SELECT name, type, sql FROM sqlite_master " |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 5704 | "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 5705 | ); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5706 | run_schema_dump_query(p, |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 5707 | "SELECT name, type, sql FROM sqlite_master " |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 5708 | "WHERE name=='sqlite_sequence'" |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 5709 | ); |
drh | 2f464a0 | 2011-10-13 00:41:49 +0000 | [diff] [blame] | 5710 | run_table_dump_query(p, |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 5711 | "SELECT sql FROM sqlite_master " |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 5712 | "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 5713 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 5714 | }else{ |
drh | e611f14 | 2017-03-08 11:44:00 +0000 | [diff] [blame] | 5715 | char *zSql; |
| 5716 | zSql = sqlite3_mprintf( |
| 5717 | "SELECT name, type, sql FROM sqlite_master " |
| 5718 | "WHERE tbl_name LIKE %Q AND type=='table'" |
| 5719 | " AND sql NOT NULL", zLike); |
| 5720 | run_schema_dump_query(p,zSql); |
| 5721 | sqlite3_free(zSql); |
| 5722 | zSql = sqlite3_mprintf( |
| 5723 | "SELECT sql FROM sqlite_master " |
| 5724 | "WHERE sql NOT NULL" |
| 5725 | " AND type IN ('index','trigger','view')" |
| 5726 | " AND tbl_name LIKE %Q", zLike); |
| 5727 | run_table_dump_query(p, zSql, 0); |
| 5728 | sqlite3_free(zSql); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 5729 | } |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 5730 | if( p->writableSchema ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5731 | raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 5732 | p->writableSchema = 0; |
| 5733 | } |
drh | 5619795 | 2011-10-13 16:30:13 +0000 | [diff] [blame] | 5734 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
| 5735 | sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5736 | raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); |
drh | 72507d4 | 2017-04-08 00:55:13 +0000 | [diff] [blame] | 5737 | p->showHeader = savedShowHeader; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 5738 | }else |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5739 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5740 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ |
| 5741 | if( nArg==2 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 5742 | setOrClearFlag(p, SHFLG_Echo, azArg[1]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5743 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5744 | raw_printf(stderr, "Usage: .echo on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5745 | rc = 1; |
| 5746 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 5747 | }else |
| 5748 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5749 | if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ |
| 5750 | if( nArg==2 ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 5751 | if( strcmp(azArg[1],"full")==0 ){ |
| 5752 | p->autoEQP = 2; |
| 5753 | }else{ |
| 5754 | p->autoEQP = booleanValue(azArg[1]); |
| 5755 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5756 | }else{ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 5757 | raw_printf(stderr, "Usage: .eqp on|off|full\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5758 | rc = 1; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 5759 | } |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 5760 | }else |
| 5761 | |
drh | d3ac7d9 | 2013-01-25 18:33:43 +0000 | [diff] [blame] | 5762 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 5763 | if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 5764 | rc = 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5765 | }else |
| 5766 | |
drh | c31b79d | 2017-06-29 21:11:27 +0000 | [diff] [blame] | 5767 | /* The ".explain" command is automatic now. It is largely pointless. It |
| 5768 | ** retained purely for backwards compatibility */ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5769 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5770 | int val = 1; |
| 5771 | if( nArg>=2 ){ |
| 5772 | if( strcmp(azArg[1],"auto")==0 ){ |
| 5773 | val = 99; |
| 5774 | }else{ |
| 5775 | val = booleanValue(azArg[1]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5776 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5777 | } |
| 5778 | if( val==1 && p->mode!=MODE_Explain ){ |
| 5779 | p->normalMode = p->mode; |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 5780 | p->mode = MODE_Explain; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5781 | p->autoExplain = 0; |
| 5782 | }else if( val==0 ){ |
| 5783 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 5784 | p->autoExplain = 0; |
| 5785 | }else if( val==99 ){ |
| 5786 | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
| 5787 | p->autoExplain = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 5788 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5789 | }else |
| 5790 | |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5791 | if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 5792 | ShellState data; |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5793 | char *zErrMsg = 0; |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5794 | int doStats = 0; |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5795 | memcpy(&data, p, sizeof(data)); |
| 5796 | data.showHeader = 0; |
| 5797 | data.cMode = data.mode = MODE_Semi; |
| 5798 | if( nArg==2 && optionMatch(azArg[1], "indent") ){ |
| 5799 | data.cMode = data.mode = MODE_Pretty; |
| 5800 | nArg = 1; |
| 5801 | } |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5802 | if( nArg!=1 ){ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 5803 | raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5804 | rc = 1; |
| 5805 | goto meta_command_exit; |
| 5806 | } |
| 5807 | open_db(p, 0); |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5808 | rc = sqlite3_exec(p->db, |
| 5809 | "SELECT sql FROM" |
| 5810 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| 5811 | " FROM sqlite_master UNION ALL" |
| 5812 | " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
drh | 4b2590e | 2014-08-19 19:28:00 +0000 | [diff] [blame] | 5813 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5814 | "ORDER BY rowid", |
| 5815 | callback, &data, &zErrMsg |
| 5816 | ); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5817 | if( rc==SQLITE_OK ){ |
| 5818 | sqlite3_stmt *pStmt; |
| 5819 | rc = sqlite3_prepare_v2(p->db, |
| 5820 | "SELECT rowid FROM sqlite_master" |
| 5821 | " WHERE name GLOB 'sqlite_stat[134]'", |
| 5822 | -1, &pStmt, 0); |
| 5823 | doStats = sqlite3_step(pStmt)==SQLITE_ROW; |
| 5824 | sqlite3_finalize(pStmt); |
| 5825 | } |
| 5826 | if( doStats==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5827 | raw_printf(p->out, "/* No STAT tables available */\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5828 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5829 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5830 | sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", |
| 5831 | callback, &data, &zErrMsg); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 5832 | data.cMode = data.mode = MODE_Insert; |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5833 | data.zDestTable = "sqlite_stat1"; |
| 5834 | shell_exec(p->db, "SELECT * FROM sqlite_stat1", |
| 5835 | shell_callback, &data,&zErrMsg); |
| 5836 | data.zDestTable = "sqlite_stat3"; |
| 5837 | shell_exec(p->db, "SELECT * FROM sqlite_stat3", |
| 5838 | shell_callback, &data,&zErrMsg); |
| 5839 | data.zDestTable = "sqlite_stat4"; |
| 5840 | shell_exec(p->db, "SELECT * FROM sqlite_stat4", |
| 5841 | shell_callback, &data, &zErrMsg); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5842 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
drh | 56f674c | 2014-07-18 14:43:29 +0000 | [diff] [blame] | 5843 | } |
drh | c197154 | 2014-06-23 23:28:13 +0000 | [diff] [blame] | 5844 | }else |
| 5845 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5846 | if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ |
| 5847 | if( nArg==2 ){ |
| 5848 | p->showHeader = booleanValue(azArg[1]); |
| 5849 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5850 | raw_printf(stderr, "Usage: .headers on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5851 | rc = 1; |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame] | 5852 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5853 | }else |
| 5854 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5855 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5856 | utf8_printf(p->out, "%s", zHelp); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5857 | }else |
| 5858 | |
| 5859 | if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ |
drh | 01f3754 | 2014-05-31 15:43:33 +0000 | [diff] [blame] | 5860 | char *zTable; /* Insert data into this table */ |
| 5861 | char *zFile; /* Name of file to extra content from */ |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5862 | sqlite3_stmt *pStmt = NULL; /* A statement */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5863 | int nCol; /* Number of columns in the table */ |
| 5864 | int nByte; /* Number of bytes in an SQL string */ |
| 5865 | int i, j; /* Loop counters */ |
drh | 2d46311 | 2013-08-06 14:36:36 +0000 | [diff] [blame] | 5866 | int needCommit; /* True to COMMIT or ROLLBACK at end */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5867 | int nSep; /* Number of bytes in p->colSeparator[] */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5868 | char *zSql; /* An SQL statement */ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5869 | ImportCtx sCtx; /* Reader context */ |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 5870 | char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
| 5871 | int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5872 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5873 | if( nArg!=3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5874 | raw_printf(stderr, "Usage: .import FILE TABLE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 5875 | goto meta_command_exit; |
| 5876 | } |
drh | 01f3754 | 2014-05-31 15:43:33 +0000 | [diff] [blame] | 5877 | zFile = azArg[1]; |
| 5878 | zTable = azArg[2]; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5879 | seenInterrupt = 0; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5880 | memset(&sCtx, 0, sizeof(sCtx)); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 5881 | open_db(p, 0); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5882 | nSep = strlen30(p->colSeparator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5883 | if( nSep==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5884 | raw_printf(stderr, |
| 5885 | "Error: non-null column separator required for import\n"); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5886 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5887 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5888 | if( nSep>1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5889 | raw_printf(stderr, "Error: multi-character column separators not allowed" |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5890 | " for import\n"); |
| 5891 | return 1; |
| 5892 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5893 | nSep = strlen30(p->rowSeparator); |
| 5894 | if( nSep==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5895 | raw_printf(stderr, "Error: non-null row separator required for import\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5896 | return 1; |
| 5897 | } |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 5898 | if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ |
| 5899 | /* When importing CSV (only), if the row separator is set to the |
| 5900 | ** default output row separator, change it to the default input |
| 5901 | ** row separator. This avoids having to maintain different input |
| 5902 | ** and output row separators. */ |
| 5903 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
| 5904 | nSep = strlen30(p->rowSeparator); |
| 5905 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5906 | if( nSep>1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5907 | raw_printf(stderr, "Error: multi-character row separators not allowed" |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5908 | " for import\n"); |
| 5909 | return 1; |
| 5910 | } |
| 5911 | sCtx.zFile = zFile; |
| 5912 | sCtx.nLine = 1; |
| 5913 | if( sCtx.zFile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5914 | #ifdef SQLITE_OMIT_POPEN |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5915 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5916 | return 1; |
| 5917 | #else |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5918 | sCtx.in = popen(sCtx.zFile+1, "r"); |
| 5919 | sCtx.zFile = "<pipe>"; |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5920 | xCloser = pclose; |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5921 | #endif |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5922 | }else{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5923 | sCtx.in = fopen(sCtx.zFile, "rb"); |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5924 | xCloser = fclose; |
| 5925 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5926 | if( p->mode==MODE_Ascii ){ |
| 5927 | xRead = ascii_read_one_field; |
| 5928 | }else{ |
| 5929 | xRead = csv_read_one_field; |
| 5930 | } |
| 5931 | if( sCtx.in==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5932 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5933 | return 1; |
| 5934 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5935 | sCtx.cColSep = p->colSeparator[0]; |
| 5936 | sCtx.cRowSep = p->rowSeparator[0]; |
drh | 7b075e3 | 2011-09-28 01:10:00 +0000 | [diff] [blame] | 5937 | zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5938 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5939 | raw_printf(stderr, "Error: out of memory\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5940 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5941 | return 1; |
| 5942 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5943 | nByte = strlen30(zSql); |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 5944 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5945 | import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5946 | if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5947 | char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); |
| 5948 | char cSep = '('; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5949 | while( xRead(&sCtx) ){ |
drh | d8c22ac | 2016-02-25 13:33:02 +0000 | [diff] [blame] | 5950 | zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5951 | cSep = ','; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5952 | if( sCtx.cTerm!=sCtx.cColSep ) break; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5953 | } |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5954 | if( cSep=='(' ){ |
| 5955 | sqlite3_free(zCreate); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5956 | sqlite3_free(sCtx.z); |
| 5957 | xCloser(sCtx.in); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5958 | utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); |
drh | 5bde816 | 2013-06-27 14:07:53 +0000 | [diff] [blame] | 5959 | return 1; |
| 5960 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5961 | zCreate = sqlite3_mprintf("%z\n)", zCreate); |
| 5962 | rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); |
| 5963 | sqlite3_free(zCreate); |
| 5964 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5965 | utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5966 | sqlite3_errmsg(p->db)); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5967 | sqlite3_free(sCtx.z); |
| 5968 | xCloser(sCtx.in); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5969 | return 1; |
| 5970 | } |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 5971 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5972 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5973 | sqlite3_free(zSql); |
| 5974 | if( rc ){ |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5975 | if (pStmt) sqlite3_finalize(pStmt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5976 | utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5977 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5978 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5979 | } |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5980 | nCol = sqlite3_column_count(pStmt); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5981 | sqlite3_finalize(pStmt); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5982 | pStmt = 0; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 5983 | if( nCol==0 ) return 0; /* no columns, no error */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5984 | zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5985 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 5986 | raw_printf(stderr, "Error: out of memory\n"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 5987 | xCloser(sCtx.in); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 5988 | return 1; |
| 5989 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5990 | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 5991 | j = strlen30(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 5992 | for(i=1; i<nCol; i++){ |
| 5993 | zSql[j++] = ','; |
| 5994 | zSql[j++] = '?'; |
| 5995 | } |
| 5996 | zSql[j++] = ')'; |
| 5997 | zSql[j] = 0; |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 5998 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 5999 | sqlite3_free(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 6000 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6001 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 6002 | if (pStmt) sqlite3_finalize(pStmt); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6003 | xCloser(sCtx.in); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 6004 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 6005 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6006 | needCommit = sqlite3_get_autocommit(p->db); |
| 6007 | if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 6008 | do{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6009 | int startLine = sCtx.nLine; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 6010 | for(i=0; i<nCol; i++){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6011 | char *z = xRead(&sCtx); |
| 6012 | /* |
| 6013 | ** Did we reach end-of-file before finding any columns? |
| 6014 | ** If so, stop instead of NULL filling the remaining columns. |
| 6015 | */ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 6016 | if( z==0 && i==0 ) break; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6017 | /* |
| 6018 | ** Did we reach end-of-file OR end-of-line before finding any |
| 6019 | ** columns in ASCII mode? If so, stop instead of NULL filling |
| 6020 | ** the remaining columns. |
| 6021 | */ |
| 6022 | if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 6023 | sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6024 | if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6025 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 6026 | "filling the rest with NULL\n", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6027 | sCtx.zFile, startLine, nCol, i+1); |
mistachkin | a0efb1a | 2015-02-12 22:45:25 +0000 | [diff] [blame] | 6028 | i += 2; |
mistachkin | 6fe0338 | 2014-06-16 22:45:28 +0000 | [diff] [blame] | 6029 | while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
drh | 18f52e0 | 2012-01-16 16:56:31 +0000 | [diff] [blame] | 6030 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 6031 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6032 | if( sCtx.cTerm==sCtx.cColSep ){ |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 6033 | do{ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6034 | xRead(&sCtx); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 6035 | i++; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6036 | }while( sCtx.cTerm==sCtx.cColSep ); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6037 | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 6038 | "extras ignored\n", |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6039 | sCtx.zFile, startLine, nCol, i); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 6040 | } |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 6041 | if( i>=nCol ){ |
| 6042 | sqlite3_step(pStmt); |
| 6043 | rc = sqlite3_reset(pStmt); |
| 6044 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6045 | utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, |
| 6046 | startLine, sqlite3_errmsg(p->db)); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 6047 | } |
| 6048 | } |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6049 | }while( sCtx.cTerm!=EOF ); |
drh | db95f68 | 2013-06-26 22:46:00 +0000 | [diff] [blame] | 6050 | |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6051 | xCloser(sCtx.in); |
| 6052 | sqlite3_free(sCtx.z); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 6053 | sqlite3_finalize(pStmt); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 6054 | if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 6055 | }else |
| 6056 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 6057 | #ifndef SQLITE_UNTESTABLE |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 6058 | if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ |
| 6059 | char *zSql; |
| 6060 | char *zCollist = 0; |
| 6061 | sqlite3_stmt *pStmt; |
| 6062 | int tnum = 0; |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 6063 | int i; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 6064 | if( nArg!=3 ){ |
| 6065 | utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"); |
| 6066 | rc = 1; |
| 6067 | goto meta_command_exit; |
| 6068 | } |
| 6069 | open_db(p, 0); |
| 6070 | zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master" |
| 6071 | " WHERE name='%q' AND type='index'", azArg[1]); |
| 6072 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6073 | sqlite3_free(zSql); |
| 6074 | if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 6075 | tnum = sqlite3_column_int(pStmt, 0); |
| 6076 | } |
| 6077 | sqlite3_finalize(pStmt); |
| 6078 | if( tnum==0 ){ |
| 6079 | utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); |
| 6080 | rc = 1; |
| 6081 | goto meta_command_exit; |
| 6082 | } |
| 6083 | zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); |
| 6084 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 6085 | sqlite3_free(zSql); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 6086 | i = 0; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 6087 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 6088 | char zLabel[20]; |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 6089 | const char *zCol = (const char*)sqlite3_column_text(pStmt,2); |
drh | 59ce2c4 | 2016-11-03 13:12:28 +0000 | [diff] [blame] | 6090 | i++; |
| 6091 | if( zCol==0 ){ |
| 6092 | if( sqlite3_column_int(pStmt,1)==-1 ){ |
| 6093 | zCol = "_ROWID_"; |
| 6094 | }else{ |
| 6095 | sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); |
| 6096 | zCol = zLabel; |
| 6097 | } |
| 6098 | } |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 6099 | if( zCollist==0 ){ |
| 6100 | zCollist = sqlite3_mprintf("\"%w\"", zCol); |
| 6101 | }else{ |
| 6102 | zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); |
| 6103 | } |
| 6104 | } |
| 6105 | sqlite3_finalize(pStmt); |
| 6106 | zSql = sqlite3_mprintf( |
| 6107 | "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID", |
| 6108 | azArg[2], zCollist, zCollist); |
| 6109 | sqlite3_free(zCollist); |
| 6110 | rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); |
| 6111 | if( rc==SQLITE_OK ){ |
| 6112 | rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
| 6113 | sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); |
| 6114 | if( rc ){ |
| 6115 | utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); |
| 6116 | }else{ |
| 6117 | utf8_printf(stdout, "%s;\n", zSql); |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 6118 | raw_printf(stdout, |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 6119 | "WARNING: writing to an imposter table will corrupt the index!\n" |
| 6120 | ); |
| 6121 | } |
| 6122 | }else{ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 6123 | raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 6124 | rc = 1; |
| 6125 | } |
| 6126 | sqlite3_free(zSql); |
| 6127 | }else |
| 6128 | #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ |
| 6129 | |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 6130 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 6131 | if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
mistachkin | 9871a93 | 2015-03-27 00:21:52 +0000 | [diff] [blame] | 6132 | SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 6133 | if( iotrace && iotrace!=stdout ) fclose(iotrace); |
| 6134 | iotrace = 0; |
| 6135 | if( nArg<2 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 6136 | sqlite3IoTrace = 0; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 6137 | }else if( strcmp(azArg[1], "-")==0 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 6138 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 6139 | iotrace = stdout; |
| 6140 | }else{ |
| 6141 | iotrace = fopen(azArg[1], "w"); |
| 6142 | if( iotrace==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6143 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 6144 | sqlite3IoTrace = 0; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6145 | rc = 1; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 6146 | }else{ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 6147 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 6148 | } |
| 6149 | } |
| 6150 | }else |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 6151 | #endif |
drh | 16eb594 | 2016-11-03 13:01:38 +0000 | [diff] [blame] | 6152 | |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 6153 | if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ |
| 6154 | static const struct { |
| 6155 | const char *zLimitName; /* Name of a limit */ |
| 6156 | int limitCode; /* Integer code for that limit */ |
| 6157 | } aLimit[] = { |
| 6158 | { "length", SQLITE_LIMIT_LENGTH }, |
| 6159 | { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, |
| 6160 | { "column", SQLITE_LIMIT_COLUMN }, |
| 6161 | { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, |
| 6162 | { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, |
| 6163 | { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, |
| 6164 | { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, |
| 6165 | { "attached", SQLITE_LIMIT_ATTACHED }, |
| 6166 | { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
| 6167 | { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, |
| 6168 | { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, |
| 6169 | { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, |
| 6170 | }; |
| 6171 | int i, n2; |
| 6172 | open_db(p, 0); |
| 6173 | if( nArg==1 ){ |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 6174 | for(i=0; i<ArraySize(aLimit); i++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6175 | printf("%20s %d\n", aLimit[i].zLimitName, |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 6176 | sqlite3_limit(p->db, aLimit[i].limitCode, -1)); |
| 6177 | } |
| 6178 | }else if( nArg>3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6179 | raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 6180 | rc = 1; |
| 6181 | goto meta_command_exit; |
| 6182 | }else{ |
| 6183 | int iLimit = -1; |
| 6184 | n2 = strlen30(azArg[1]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 6185 | for(i=0; i<ArraySize(aLimit); i++){ |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 6186 | if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ |
| 6187 | if( iLimit<0 ){ |
| 6188 | iLimit = i; |
| 6189 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6190 | utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 6191 | rc = 1; |
| 6192 | goto meta_command_exit; |
| 6193 | } |
| 6194 | } |
| 6195 | } |
| 6196 | if( iLimit<0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6197 | utf8_printf(stderr, "unknown limit: \"%s\"\n" |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 6198 | "enter \".limits\" with no arguments for a list.\n", |
| 6199 | azArg[1]); |
| 6200 | rc = 1; |
| 6201 | goto meta_command_exit; |
| 6202 | } |
| 6203 | if( nArg==3 ){ |
mistachkin | 2c1820c | 2015-05-08 01:04:39 +0000 | [diff] [blame] | 6204 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, |
| 6205 | (int)integerValue(azArg[2])); |
drh | 1a51337 | 2015-05-02 17:40:23 +0000 | [diff] [blame] | 6206 | } |
| 6207 | printf("%20s %d\n", aLimit[iLimit].zLimitName, |
| 6208 | sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); |
| 6209 | } |
| 6210 | }else |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 6211 | |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 6212 | if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ |
drh | 3fd9f33 | 2016-12-16 18:41:11 +0000 | [diff] [blame] | 6213 | open_db(p, 0); |
dan | 3c7ebeb | 2016-12-16 17:28:56 +0000 | [diff] [blame] | 6214 | lintDotCommand(p, azArg, nArg); |
| 6215 | }else |
| 6216 | |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 6217 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6218 | if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 6219 | const char *zFile, *zProc; |
| 6220 | char *zErrMsg = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6221 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6222 | raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6223 | rc = 1; |
| 6224 | goto meta_command_exit; |
| 6225 | } |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 6226 | zFile = azArg[1]; |
| 6227 | zProc = nArg>=3 ? azArg[2] : 0; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6228 | open_db(p, 0); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 6229 | rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
| 6230 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6231 | utf8_printf(stderr, "Error: %s\n", zErrMsg); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 6232 | sqlite3_free(zErrMsg); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 6233 | rc = 1; |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 6234 | } |
| 6235 | }else |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 6236 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 6237 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6238 | if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ |
| 6239 | if( nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6240 | raw_printf(stderr, "Usage: .log FILENAME\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6241 | rc = 1; |
| 6242 | }else{ |
| 6243 | const char *zFile = azArg[1]; |
| 6244 | output_file_close(p->pLog); |
| 6245 | p->pLog = output_file_open(zFile); |
| 6246 | } |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 6247 | }else |
| 6248 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6249 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ |
| 6250 | const char *zMode = nArg>=2 ? azArg[1] : ""; |
| 6251 | int n2 = (int)strlen(zMode); |
| 6252 | int c2 = zMode[0]; |
| 6253 | if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6254 | p->mode = MODE_Line; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 6255 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6256 | }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6257 | p->mode = MODE_Column; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 6258 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6259 | }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6260 | p->mode = MODE_List; |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 6261 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); |
| 6262 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6263 | }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 6264 | p->mode = MODE_Html; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6265 | }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 6266 | p->mode = MODE_Tcl; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 6267 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); |
drh | 7aee83b | 2017-01-27 01:52:42 +0000 | [diff] [blame] | 6268 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6269 | }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 6270 | p->mode = MODE_Csv; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 6271 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 6272 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6273 | }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 6274 | p->mode = MODE_List; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 6275 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6276 | }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 6277 | p->mode = MODE_Insert; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6278 | set_table_name(p, nArg>=3 ? azArg[2] : "table"); |
drh | 41f5f6e | 2016-10-21 17:39:30 +0000 | [diff] [blame] | 6279 | }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){ |
| 6280 | p->mode = MODE_Quote; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6281 | }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ |
| 6282 | p->mode = MODE_Ascii; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 6283 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
| 6284 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
drh | a501f7d | 2017-06-29 21:33:25 +0000 | [diff] [blame] | 6285 | }else if( nArg==1 ){ |
| 6286 | raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]); |
| 6287 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6288 | raw_printf(stderr, "Error: mode should be one of: " |
drh | 36f49d0 | 2016-11-23 23:18:45 +0000 | [diff] [blame] | 6289 | "ascii column csv html insert line list quote tabs tcl\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6290 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6291 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 6292 | p->cMode = p->mode; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6293 | }else |
| 6294 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6295 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ |
| 6296 | if( nArg==2 ){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 6297 | sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
| 6298 | "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6299 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6300 | raw_printf(stderr, "Usage: .nullvalue STRING\n"); |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 6301 | rc = 1; |
| 6302 | } |
| 6303 | }else |
| 6304 | |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6305 | if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 6306 | char *zNewFilename; /* Name of the database file to open */ |
| 6307 | int iName = 1; /* Index in azArg[] of the filename */ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 6308 | int newFlag = 0; /* True to delete file before opening */ |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 6309 | /* Close the existing database */ |
| 6310 | session_close_all(p); |
| 6311 | sqlite3_close(p->db); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6312 | p->db = 0; |
dan | 2147221 | 2017-03-01 11:30:27 +0000 | [diff] [blame] | 6313 | p->zDbFilename = 0; |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 6314 | sqlite3_free(p->zFreeOnClose); |
| 6315 | p->zFreeOnClose = 0; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 6316 | /* Check for command-line arguments */ |
| 6317 | for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){ |
| 6318 | const char *z = azArg[iName]; |
| 6319 | if( optionMatch(z,"new") ){ |
| 6320 | newFlag = 1; |
| 6321 | }else if( z[0]=='-' ){ |
| 6322 | utf8_printf(stderr, "unknown option: %s\n", z); |
| 6323 | rc = 1; |
mistachkin | 8145fc6 | 2016-09-16 20:39:21 +0000 | [diff] [blame] | 6324 | goto meta_command_exit; |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 6325 | } |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 6326 | } |
| 6327 | /* If a filename is specified, try to open it first */ |
| 6328 | zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0; |
| 6329 | if( zNewFilename ){ |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 6330 | if( newFlag ) shellDeleteFile(zNewFilename); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 6331 | p->zDbFilename = zNewFilename; |
| 6332 | open_db(p, 1); |
| 6333 | if( p->db==0 ){ |
| 6334 | utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); |
| 6335 | sqlite3_free(zNewFilename); |
| 6336 | }else{ |
| 6337 | p->zFreeOnClose = zNewFilename; |
| 6338 | } |
| 6339 | } |
| 6340 | if( p->db==0 ){ |
| 6341 | /* As a fall-back open a TEMP database */ |
| 6342 | p->zDbFilename = 0; |
| 6343 | open_db(p, 0); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6344 | } |
| 6345 | }else |
| 6346 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6347 | if( c=='o' |
| 6348 | && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0) |
| 6349 | ){ |
| 6350 | const char *zFile = nArg>=2 ? azArg[1] : "stdout"; |
| 6351 | if( nArg>2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6352 | utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6353 | rc = 1; |
| 6354 | goto meta_command_exit; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6355 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6356 | if( n>1 && strncmp(azArg[0], "once", n)==0 ){ |
| 6357 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6358 | raw_printf(stderr, "Usage: .once FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6359 | rc = 1; |
| 6360 | goto meta_command_exit; |
| 6361 | } |
| 6362 | p->outCount = 2; |
| 6363 | }else{ |
| 6364 | p->outCount = 0; |
| 6365 | } |
| 6366 | output_reset(p); |
| 6367 | if( zFile[0]=='|' ){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 6368 | #ifdef SQLITE_OMIT_POPEN |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6369 | raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 6370 | rc = 1; |
| 6371 | p->out = stdout; |
| 6372 | #else |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6373 | p->out = popen(zFile + 1, "w"); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 6374 | if( p->out==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6375 | utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 6376 | p->out = stdout; |
| 6377 | rc = 1; |
| 6378 | }else{ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6379 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
drh | e1da8fa | 2012-03-30 00:05:57 +0000 | [diff] [blame] | 6380 | } |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 6381 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6382 | }else{ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6383 | p->out = output_file_open(zFile); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6384 | if( p->out==0 ){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6385 | if( strcmp(zFile,"off")!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6386 | utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 6387 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6388 | p->out = stdout; |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6389 | rc = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6390 | } else { |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6391 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6392 | } |
| 6393 | } |
| 6394 | }else |
| 6395 | |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 6396 | if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
| 6397 | int i; |
| 6398 | for(i=1; i<nArg; i++){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6399 | if( i>1 ) raw_printf(p->out, " "); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 6400 | utf8_printf(p->out, "%s", azArg[i]); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 6401 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6402 | raw_printf(p->out, "\n"); |
drh | 078b1fd | 2012-09-21 13:40:02 +0000 | [diff] [blame] | 6403 | }else |
| 6404 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6405 | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6406 | if( nArg >= 2) { |
| 6407 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 6408 | } |
| 6409 | if( nArg >= 3) { |
| 6410 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 6411 | } |
| 6412 | }else |
| 6413 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6414 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 6415 | rc = 2; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 6416 | }else |
| 6417 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6418 | if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ |
| 6419 | FILE *alt; |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6420 | if( nArg!=2 ){ |
| 6421 | raw_printf(stderr, "Usage: .read FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6422 | rc = 1; |
| 6423 | goto meta_command_exit; |
| 6424 | } |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6425 | alt = fopen(azArg[1], "rb"); |
| 6426 | if( alt==0 ){ |
| 6427 | utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); |
| 6428 | rc = 1; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6429 | }else{ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 6430 | rc = process_input(p, alt); |
| 6431 | fclose(alt); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6432 | } |
| 6433 | }else |
| 6434 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6435 | if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 6436 | const char *zSrcFile; |
| 6437 | const char *zDb; |
| 6438 | sqlite3 *pSrc; |
| 6439 | sqlite3_backup *pBackup; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 6440 | int nTimeout = 0; |
| 6441 | |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 6442 | if( nArg==2 ){ |
| 6443 | zSrcFile = azArg[1]; |
| 6444 | zDb = "main"; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6445 | }else if( nArg==3 ){ |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 6446 | zSrcFile = azArg[2]; |
| 6447 | zDb = azArg[1]; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6448 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6449 | raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6450 | rc = 1; |
| 6451 | goto meta_command_exit; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 6452 | } |
| 6453 | rc = sqlite3_open(zSrcFile, &pSrc); |
| 6454 | if( rc!=SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6455 | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 6456 | sqlite3_close(pSrc); |
| 6457 | return 1; |
| 6458 | } |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6459 | open_db(p, 0); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 6460 | pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
| 6461 | if( pBackup==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6462 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 6463 | sqlite3_close(pSrc); |
| 6464 | return 1; |
| 6465 | } |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 6466 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
| 6467 | || rc==SQLITE_BUSY ){ |
| 6468 | if( rc==SQLITE_BUSY ){ |
| 6469 | if( nTimeout++ >= 3 ) break; |
| 6470 | sqlite3_sleep(100); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 6471 | } |
| 6472 | } |
| 6473 | sqlite3_backup_finish(pBackup); |
| 6474 | if( rc==SQLITE_DONE ){ |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6475 | rc = 0; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 6476 | }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6477 | raw_printf(stderr, "Error: source database is busy\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6478 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 6479 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6480 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6481 | rc = 1; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 6482 | } |
| 6483 | sqlite3_close(pSrc); |
| 6484 | }else |
| 6485 | |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 6486 | |
| 6487 | if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ |
| 6488 | if( nArg==2 ){ |
| 6489 | p->scanstatsOn = booleanValue(azArg[1]); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 6490 | #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6491 | raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); |
drh | 15f23c2 | 2014-11-06 12:46:16 +0000 | [diff] [blame] | 6492 | #endif |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 6493 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6494 | raw_printf(stderr, "Usage: .scanstats on|off\n"); |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 6495 | rc = 1; |
| 6496 | } |
| 6497 | }else |
| 6498 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6499 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 6500 | ShellText sSelect; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 6501 | ShellState data; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6502 | char *zErrMsg = 0; |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 6503 | const char *zDiv = 0; |
| 6504 | int iSchema = 0; |
| 6505 | |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 6506 | open_db(p, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6507 | memcpy(&data, p, sizeof(data)); |
| 6508 | data.showHeader = 0; |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 6509 | data.cMode = data.mode = MODE_Semi; |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 6510 | initText(&sSelect); |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 6511 | if( nArg>=2 && optionMatch(azArg[1], "indent") ){ |
| 6512 | data.cMode = data.mode = MODE_Pretty; |
| 6513 | nArg--; |
| 6514 | if( nArg==2 ) azArg[1] = azArg[2]; |
| 6515 | } |
| 6516 | if( nArg==2 && azArg[1][0]!='-' ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 6517 | int i; |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 6518 | 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] | 6519 | if( strcmp(azArg[1],"sqlite_master")==0 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 6520 | char *new_argv[2], *new_colv[2]; |
| 6521 | new_argv[0] = "CREATE TABLE sqlite_master (\n" |
| 6522 | " type text,\n" |
| 6523 | " name text,\n" |
| 6524 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 6525 | " rootpage integer,\n" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 6526 | " sql text\n" |
| 6527 | ")"; |
| 6528 | new_argv[1] = 0; |
| 6529 | new_colv[0] = "sql"; |
| 6530 | new_colv[1] = 0; |
| 6531 | callback(&data, 1, new_argv, new_colv); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6532 | rc = SQLITE_OK; |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 6533 | }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 6534 | char *new_argv[2], *new_colv[2]; |
| 6535 | new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" |
| 6536 | " type text,\n" |
| 6537 | " name text,\n" |
| 6538 | " tbl_name text,\n" |
| 6539 | " rootpage integer,\n" |
| 6540 | " sql text\n" |
| 6541 | ")"; |
| 6542 | new_argv[1] = 0; |
| 6543 | new_colv[0] = "sql"; |
| 6544 | new_colv[1] = 0; |
| 6545 | callback(&data, 1, new_argv, new_colv); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6546 | rc = SQLITE_OK; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 6547 | }else{ |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 6548 | zDiv = "("; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 6549 | } |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6550 | }else if( nArg==1 ){ |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 6551 | zDiv = "("; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6552 | }else{ |
drh | 4926fec | 2016-04-13 15:33:42 +0000 | [diff] [blame] | 6553 | raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6554 | rc = 1; |
| 6555 | goto meta_command_exit; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6556 | } |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 6557 | if( zDiv ){ |
| 6558 | sqlite3_stmt *pStmt = 0; |
drh | a7a0bfa | 2017-07-07 18:06:49 +0000 | [diff] [blame] | 6559 | rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", |
| 6560 | -1, &pStmt, 0); |
| 6561 | if( rc ){ |
| 6562 | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 6563 | sqlite3_finalize(pStmt); |
| 6564 | rc = 1; |
| 6565 | goto meta_command_exit; |
| 6566 | } |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 6567 | appendText(&sSelect, "SELECT sql FROM", 0); |
| 6568 | iSchema = 0; |
| 6569 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 6570 | const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); |
| 6571 | char zScNum[30]; |
| 6572 | sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); |
| 6573 | appendText(&sSelect, zDiv, 0); |
| 6574 | zDiv = " UNION ALL "; |
| 6575 | if( strcmp(zDb, "main")!=0 ){ |
| 6576 | appendText(&sSelect, "SELECT shell_add_schema(sql,", 0); |
drh | 90cdec0 | 2017-06-15 13:07:56 +0000 | [diff] [blame] | 6577 | appendText(&sSelect, zDb, '"'); |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 6578 | appendText(&sSelect, ") AS sql, type, tbl_name, name, rowid,", 0); |
| 6579 | appendText(&sSelect, zScNum, 0); |
| 6580 | appendText(&sSelect, " AS snum, ", 0); |
| 6581 | appendText(&sSelect, zDb, '\''); |
| 6582 | appendText(&sSelect, " AS sname FROM ", 0); |
drh | 90cdec0 | 2017-06-15 13:07:56 +0000 | [diff] [blame] | 6583 | appendText(&sSelect, zDb, '"'); |
drh | 20c9c3f | 2017-06-15 12:21:09 +0000 | [diff] [blame] | 6584 | appendText(&sSelect, ".sqlite_master", 0); |
| 6585 | }else{ |
| 6586 | appendText(&sSelect, "SELECT sql, type, tbl_name, name, rowid, ", 0); |
| 6587 | appendText(&sSelect, zScNum, 0); |
| 6588 | appendText(&sSelect, " AS snum, 'main' AS sname FROM sqlite_master",0); |
| 6589 | } |
| 6590 | } |
| 6591 | sqlite3_finalize(pStmt); |
| 6592 | appendText(&sSelect, ") WHERE ", 0); |
| 6593 | if( nArg>1 ){ |
| 6594 | char *zQarg = sqlite3_mprintf("%Q", azArg[1]); |
| 6595 | if( strchr(azArg[1], '.') ){ |
| 6596 | appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0); |
| 6597 | }else{ |
| 6598 | appendText(&sSelect, "lower(tbl_name)", 0); |
| 6599 | } |
| 6600 | appendText(&sSelect, strchr(azArg[1], '*') ? " GLOB " : " LIKE ", 0); |
| 6601 | appendText(&sSelect, zQarg, 0); |
| 6602 | appendText(&sSelect, " AND ", 0); |
| 6603 | sqlite3_free(zQarg); |
| 6604 | } |
| 6605 | appendText(&sSelect, "type!='meta' AND sql IS NOT NULL" |
| 6606 | " ORDER BY snum, rowid", 0); |
| 6607 | rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); |
| 6608 | freeText(&sSelect); |
| 6609 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6610 | if( zErrMsg ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6611 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 6612 | sqlite3_free(zErrMsg); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6613 | rc = 1; |
| 6614 | }else if( rc != SQLITE_OK ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6615 | raw_printf(stderr,"Error: querying schema information\n"); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 6616 | rc = 1; |
| 6617 | }else{ |
| 6618 | rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6619 | } |
| 6620 | }else |
| 6621 | |
drh | abd4c72 | 2014-09-20 18:18:33 +0000 | [diff] [blame] | 6622 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| 6623 | if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ |
drh | 2b44fd9 | 2017-02-17 01:43:51 +0000 | [diff] [blame] | 6624 | sqlite3SelectTrace = (int)integerValue(azArg[1]); |
drh | abd4c72 | 2014-09-20 18:18:33 +0000 | [diff] [blame] | 6625 | }else |
| 6626 | #endif |
| 6627 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6628 | #if defined(SQLITE_ENABLE_SESSION) |
| 6629 | if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ |
| 6630 | OpenSession *pSession = &p->aSession[0]; |
| 6631 | char **azCmd = &azArg[1]; |
| 6632 | int iSes = 0; |
| 6633 | int nCmd = nArg - 1; |
| 6634 | int i; |
| 6635 | if( nArg<=1 ) goto session_syntax_error; |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 6636 | open_db(p, 0); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6637 | if( nArg>=3 ){ |
| 6638 | for(iSes=0; iSes<p->nSession; iSes++){ |
| 6639 | if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; |
| 6640 | } |
| 6641 | if( iSes<p->nSession ){ |
| 6642 | pSession = &p->aSession[iSes]; |
| 6643 | azCmd++; |
| 6644 | nCmd--; |
| 6645 | }else{ |
| 6646 | pSession = &p->aSession[0]; |
| 6647 | iSes = 0; |
| 6648 | } |
| 6649 | } |
| 6650 | |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 6651 | /* .session attach TABLE |
| 6652 | ** Invoke the sqlite3session_attach() interface to attach a particular |
| 6653 | ** table so that it is never filtered. |
| 6654 | */ |
| 6655 | if( strcmp(azCmd[0],"attach")==0 ){ |
| 6656 | if( nCmd!=2 ) goto session_syntax_error; |
| 6657 | if( pSession->p==0 ){ |
| 6658 | session_not_open: |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6659 | raw_printf(stderr, "ERROR: No sessions are open\n"); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 6660 | }else{ |
| 6661 | rc = sqlite3session_attach(pSession->p, azCmd[1]); |
| 6662 | if( rc ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6663 | raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 6664 | rc = 0; |
| 6665 | } |
| 6666 | } |
| 6667 | }else |
| 6668 | |
| 6669 | /* .session changeset FILE |
| 6670 | ** .session patchset FILE |
| 6671 | ** Write a changeset or patchset into a file. The file is overwritten. |
| 6672 | */ |
| 6673 | if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ |
| 6674 | FILE *out = 0; |
| 6675 | if( nCmd!=2 ) goto session_syntax_error; |
| 6676 | if( pSession->p==0 ) goto session_not_open; |
| 6677 | out = fopen(azCmd[1], "wb"); |
| 6678 | if( out==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6679 | utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 6680 | }else{ |
| 6681 | int szChng; |
| 6682 | void *pChng; |
| 6683 | if( azCmd[0][0]=='c' ){ |
drh | 2967e0c | 2014-08-19 00:26:17 +0000 | [diff] [blame] | 6684 | rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 6685 | }else{ |
drh | 2967e0c | 2014-08-19 00:26:17 +0000 | [diff] [blame] | 6686 | rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); |
| 6687 | } |
| 6688 | if( rc ){ |
| 6689 | printf("Error: error code %d\n", rc); |
| 6690 | rc = 0; |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 6691 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6692 | if( pChng |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 6693 | && fwrite(pChng, szChng, 1, out)!=1 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6694 | raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 6695 | szChng); |
| 6696 | } |
| 6697 | sqlite3_free(pChng); |
| 6698 | fclose(out); |
| 6699 | } |
| 6700 | }else |
| 6701 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6702 | /* .session close |
| 6703 | ** Close the identified session |
| 6704 | */ |
| 6705 | if( strcmp(azCmd[0], "close")==0 ){ |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 6706 | if( nCmd!=1 ) goto session_syntax_error; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6707 | if( p->nSession ){ |
| 6708 | session_close(pSession); |
| 6709 | p->aSession[iSes] = p->aSession[--p->nSession]; |
| 6710 | } |
| 6711 | }else |
| 6712 | |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 6713 | /* .session enable ?BOOLEAN? |
| 6714 | ** Query or set the enable flag |
| 6715 | */ |
| 6716 | if( strcmp(azCmd[0], "enable")==0 ){ |
| 6717 | int ii; |
| 6718 | if( nCmd>2 ) goto session_syntax_error; |
| 6719 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 6720 | if( p->nSession ){ |
| 6721 | ii = sqlite3session_enable(pSession->p, ii); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6722 | utf8_printf(p->out, "session %s enable flag = %d\n", |
| 6723 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 6724 | } |
| 6725 | }else |
| 6726 | |
| 6727 | /* .session filter GLOB .... |
| 6728 | ** Set a list of GLOB patterns of table names to be excluded. |
| 6729 | */ |
| 6730 | if( strcmp(azCmd[0], "filter")==0 ){ |
| 6731 | int ii, nByte; |
| 6732 | if( nCmd<2 ) goto session_syntax_error; |
| 6733 | if( p->nSession ){ |
| 6734 | for(ii=0; ii<pSession->nFilter; ii++){ |
| 6735 | sqlite3_free(pSession->azFilter[ii]); |
| 6736 | } |
| 6737 | sqlite3_free(pSession->azFilter); |
| 6738 | nByte = sizeof(pSession->azFilter[0])*(nCmd-1); |
| 6739 | pSession->azFilter = sqlite3_malloc( nByte ); |
| 6740 | if( pSession->azFilter==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6741 | raw_printf(stderr, "Error: out or memory\n"); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 6742 | exit(1); |
| 6743 | } |
| 6744 | for(ii=1; ii<nCmd; ii++){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 6745 | pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 6746 | } |
| 6747 | pSession->nFilter = ii-1; |
| 6748 | } |
| 6749 | }else |
| 6750 | |
| 6751 | /* .session indirect ?BOOLEAN? |
| 6752 | ** Query or set the indirect flag |
| 6753 | */ |
| 6754 | if( strcmp(azCmd[0], "indirect")==0 ){ |
| 6755 | int ii; |
| 6756 | if( nCmd>2 ) goto session_syntax_error; |
| 6757 | ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| 6758 | if( p->nSession ){ |
| 6759 | ii = sqlite3session_indirect(pSession->p, ii); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6760 | utf8_printf(p->out, "session %s indirect flag = %d\n", |
| 6761 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 6762 | } |
| 6763 | }else |
| 6764 | |
| 6765 | /* .session isempty |
| 6766 | ** Determine if the session is empty |
| 6767 | */ |
| 6768 | if( strcmp(azCmd[0], "isempty")==0 ){ |
| 6769 | int ii; |
| 6770 | if( nCmd!=1 ) goto session_syntax_error; |
| 6771 | if( p->nSession ){ |
| 6772 | ii = sqlite3session_isempty(pSession->p); |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6773 | utf8_printf(p->out, "session %s isempty flag = %d\n", |
| 6774 | pSession->zName, ii); |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 6775 | } |
| 6776 | }else |
| 6777 | |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6778 | /* .session list |
| 6779 | ** List all currently open sessions |
| 6780 | */ |
| 6781 | if( strcmp(azCmd[0],"list")==0 ){ |
| 6782 | for(i=0; i<p->nSession; i++){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6783 | utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6784 | } |
| 6785 | }else |
| 6786 | |
| 6787 | /* .session open DB NAME |
| 6788 | ** Open a new session called NAME on the attached database DB. |
| 6789 | ** DB is normally "main". |
| 6790 | */ |
| 6791 | if( strcmp(azCmd[0],"open")==0 ){ |
| 6792 | char *zName; |
| 6793 | if( nCmd!=3 ) goto session_syntax_error; |
| 6794 | zName = azCmd[2]; |
| 6795 | if( zName[0]==0 ) goto session_syntax_error; |
| 6796 | for(i=0; i<p->nSession; i++){ |
| 6797 | if( strcmp(p->aSession[i].zName,zName)==0 ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6798 | utf8_printf(stderr, "Session \"%s\" already exists\n", zName); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6799 | goto meta_command_exit; |
| 6800 | } |
| 6801 | } |
| 6802 | if( p->nSession>=ArraySize(p->aSession) ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6803 | raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6804 | goto meta_command_exit; |
| 6805 | } |
| 6806 | pSession = &p->aSession[p->nSession]; |
| 6807 | rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); |
| 6808 | if( rc ){ |
mistachkin | 899c5c9 | 2016-04-03 20:50:02 +0000 | [diff] [blame] | 6809 | raw_printf(stderr, "Cannot open session: error code=%d\n", rc); |
drh | 3a67b04 | 2014-08-18 17:56:31 +0000 | [diff] [blame] | 6810 | rc = 0; |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6811 | goto meta_command_exit; |
| 6812 | } |
drh | 03168ca | 2014-08-18 20:01:31 +0000 | [diff] [blame] | 6813 | pSession->nFilter = 0; |
| 6814 | sqlite3session_table_filter(pSession->p, session_filter, pSession); |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 6815 | p->nSession++; |
| 6816 | pSession->zName = sqlite3_mprintf("%s", zName); |
| 6817 | }else |
| 6818 | /* If no command name matches, show a syntax error */ |
| 6819 | session_syntax_error: |
| 6820 | session_help(p); |
| 6821 | }else |
| 6822 | #endif |
| 6823 | |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 6824 | #ifdef SQLITE_DEBUG |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6825 | /* Undocumented commands for internal testing. Subject to change |
| 6826 | ** without notice. */ |
| 6827 | if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ |
| 6828 | if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ |
| 6829 | int i, v; |
| 6830 | for(i=1; i<nArg; i++){ |
| 6831 | v = booleanValue(azArg[i]); |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 6832 | utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6833 | } |
| 6834 | } |
| 6835 | if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ |
| 6836 | int i; sqlite3_int64 v; |
| 6837 | for(i=1; i<nArg; i++){ |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 6838 | char zBuf[200]; |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6839 | v = integerValue(azArg[i]); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6840 | 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] | 6841 | utf8_printf(p->out, "%s", zBuf); |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6842 | } |
| 6843 | } |
| 6844 | }else |
drh | 340f582 | 2013-06-27 13:01:21 +0000 | [diff] [blame] | 6845 | #endif |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 6846 | |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6847 | if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){ |
| 6848 | int bIsInit = 0; /* True to initialize the SELFTEST table */ |
| 6849 | int bVerbose = 0; /* Verbose output */ |
| 6850 | int bSelftestExists; /* True if SELFTEST already exists */ |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6851 | int i, k; /* Loop counters */ |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6852 | int nTest = 0; /* Number of tests runs */ |
| 6853 | int nErr = 0; /* Number of errors seen */ |
| 6854 | ShellText str; /* Answer for a query */ |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6855 | sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6856 | |
| 6857 | open_db(p,0); |
| 6858 | for(i=1; i<nArg; i++){ |
| 6859 | const char *z = azArg[i]; |
| 6860 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 6861 | if( strcmp(z,"-init")==0 ){ |
| 6862 | bIsInit = 1; |
| 6863 | }else |
| 6864 | if( strcmp(z,"-v")==0 ){ |
| 6865 | bVerbose++; |
| 6866 | }else |
| 6867 | { |
| 6868 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
| 6869 | azArg[i], azArg[0]); |
| 6870 | raw_printf(stderr, "Should be one of: --init -v\n"); |
| 6871 | rc = 1; |
| 6872 | goto meta_command_exit; |
| 6873 | } |
| 6874 | } |
| 6875 | if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) |
| 6876 | != SQLITE_OK ){ |
| 6877 | bSelftestExists = 0; |
| 6878 | }else{ |
| 6879 | bSelftestExists = 1; |
| 6880 | } |
| 6881 | if( bIsInit ){ |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6882 | createSelftestTable(p); |
| 6883 | bSelftestExists = 1; |
| 6884 | } |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6885 | initText(&str); |
| 6886 | appendText(&str, "x", 0); |
| 6887 | for(k=bSelftestExists; k>=0; k--){ |
| 6888 | if( k==1 ){ |
| 6889 | rc = sqlite3_prepare_v2(p->db, |
| 6890 | "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", |
| 6891 | -1, &pStmt, 0); |
| 6892 | }else{ |
| 6893 | rc = sqlite3_prepare_v2(p->db, |
| 6894 | "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," |
| 6895 | " (1,'run','PRAGMA integrity_check','ok')", |
| 6896 | -1, &pStmt, 0); |
| 6897 | } |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6898 | if( rc ){ |
| 6899 | raw_printf(stderr, "Error querying the selftest table\n"); |
| 6900 | rc = 1; |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6901 | sqlite3_finalize(pStmt); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6902 | goto meta_command_exit; |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6903 | } |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6904 | for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ |
| 6905 | int tno = sqlite3_column_int(pStmt, 0); |
| 6906 | const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); |
| 6907 | const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); |
| 6908 | const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 6909 | |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6910 | k = 0; |
| 6911 | if( bVerbose>0 ){ |
| 6912 | char *zQuote = sqlite3_mprintf("%q", zSql); |
| 6913 | printf("%d: %s %s\n", tno, zOp, zSql); |
| 6914 | sqlite3_free(zQuote); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6915 | } |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6916 | if( strcmp(zOp,"memo")==0 ){ |
| 6917 | utf8_printf(p->out, "%s\n", zSql); |
| 6918 | }else |
| 6919 | if( strcmp(zOp,"run")==0 ){ |
| 6920 | char *zErrMsg = 0; |
| 6921 | str.n = 0; |
| 6922 | str.z[0] = 0; |
| 6923 | rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); |
| 6924 | nTest++; |
| 6925 | if( bVerbose ){ |
| 6926 | utf8_printf(p->out, "Result: %s\n", str.z); |
| 6927 | } |
| 6928 | if( rc || zErrMsg ){ |
| 6929 | nErr++; |
| 6930 | rc = 1; |
| 6931 | utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); |
| 6932 | sqlite3_free(zErrMsg); |
| 6933 | }else if( strcmp(zAns,str.z)!=0 ){ |
| 6934 | nErr++; |
| 6935 | rc = 1; |
| 6936 | utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); |
| 6937 | utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); |
| 6938 | } |
| 6939 | }else |
| 6940 | { |
| 6941 | utf8_printf(stderr, |
| 6942 | "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6943 | rc = 1; |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6944 | break; |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6945 | } |
drh | c5d353f | 2017-06-09 02:27:49 +0000 | [diff] [blame] | 6946 | } /* End loop over rows of content from SELFTEST */ |
| 6947 | sqlite3_finalize(pStmt); |
| 6948 | } /* End loop over k */ |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6949 | freeText(&str); |
drh | fb546af | 2017-03-09 22:00:33 +0000 | [diff] [blame] | 6950 | utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); |
| 6951 | }else |
| 6952 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6953 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6954 | if( nArg<2 || nArg>3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 6955 | raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 6956 | rc = 1; |
| 6957 | } |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6958 | if( nArg>=2 ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 6959 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, |
mistachkin | 22c9638 | 2014-07-24 22:51:18 +0000 | [diff] [blame] | 6960 | "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6961 | } |
| 6962 | if( nArg>=3 ){ |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 6963 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
| 6964 | "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 6965 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6966 | }else |
| 6967 | |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6968 | if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){ |
| 6969 | const char *zLike = 0; /* Which table to checksum. 0 means everything */ |
| 6970 | int i; /* Loop counter */ |
| 6971 | int bSchema = 0; /* Also hash the schema */ |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6972 | int bSeparate = 0; /* Hash each table separately */ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6973 | int iSize = 224; /* Hash algorithm to use */ |
| 6974 | int bDebug = 0; /* Only show the query that would have run */ |
| 6975 | sqlite3_stmt *pStmt; /* For querying tables names */ |
| 6976 | char *zSql; /* SQL to be run */ |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6977 | char *zSep; /* Separator */ |
| 6978 | ShellText sSql; /* Complete SQL for the query to run the hash */ |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6979 | ShellText sQuery; /* Set of queries used to read all content */ |
drh | f80d4ff | 2017-03-08 18:06:20 +0000 | [diff] [blame] | 6980 | open_db(p, 0); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6981 | for(i=1; i<nArg; i++){ |
| 6982 | const char *z = azArg[i]; |
| 6983 | if( z[0]=='-' ){ |
| 6984 | z++; |
| 6985 | if( z[0]=='-' ) z++; |
| 6986 | if( strcmp(z,"schema")==0 ){ |
| 6987 | bSchema = 1; |
| 6988 | }else |
mistachkin | e16a350 | 2017-05-29 03:48:13 +0000 | [diff] [blame] | 6989 | if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 |
| 6990 | || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 6991 | ){ |
| 6992 | iSize = atoi(&z[5]); |
| 6993 | }else |
| 6994 | if( strcmp(z,"debug")==0 ){ |
| 6995 | bDebug = 1; |
| 6996 | }else |
| 6997 | { |
| 6998 | utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 6999 | azArg[i], azArg[0]); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 7000 | raw_printf(stderr, "Should be one of: --schema" |
| 7001 | " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n"); |
| 7002 | rc = 1; |
| 7003 | goto meta_command_exit; |
| 7004 | } |
| 7005 | }else if( zLike ){ |
| 7006 | raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); |
| 7007 | rc = 1; |
| 7008 | goto meta_command_exit; |
| 7009 | }else{ |
| 7010 | zLike = z; |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 7011 | bSeparate = 1; |
| 7012 | if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 7013 | } |
| 7014 | } |
| 7015 | if( bSchema ){ |
| 7016 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 7017 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 7018 | " UNION ALL SELECT 'sqlite_master'" |
| 7019 | " ORDER BY 1 collate nocase"; |
| 7020 | }else{ |
| 7021 | zSql = "SELECT lower(name) FROM sqlite_master" |
| 7022 | " WHERE type='table' AND coalesce(rootpage,0)>1" |
| 7023 | " AND name NOT LIKE 'sqlite_%'" |
| 7024 | " ORDER BY 1 collate nocase"; |
| 7025 | } |
| 7026 | sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 7027 | initText(&sQuery); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 7028 | initText(&sSql); |
| 7029 | appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); |
| 7030 | zSep = "VALUES("; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 7031 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 7032 | const char *zTab = (const char*)sqlite3_column_text(pStmt,0); |
| 7033 | if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; |
| 7034 | if( strncmp(zTab, "sqlite_",7)!=0 ){ |
| 7035 | appendText(&sQuery,"SELECT * FROM ", 0); |
| 7036 | appendText(&sQuery,zTab,'"'); |
| 7037 | appendText(&sQuery," NOT INDEXED;", 0); |
| 7038 | }else if( strcmp(zTab, "sqlite_master")==0 ){ |
| 7039 | appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master" |
| 7040 | " ORDER BY name;", 0); |
| 7041 | }else if( strcmp(zTab, "sqlite_sequence")==0 ){ |
| 7042 | appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" |
| 7043 | " ORDER BY name;", 0); |
| 7044 | }else if( strcmp(zTab, "sqlite_stat1")==0 ){ |
| 7045 | appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" |
| 7046 | " ORDER BY tbl,idx;", 0); |
| 7047 | }else if( strcmp(zTab, "sqlite_stat3")==0 |
| 7048 | || strcmp(zTab, "sqlite_stat4")==0 ){ |
| 7049 | appendText(&sQuery, "SELECT * FROM ", 0); |
| 7050 | appendText(&sQuery, zTab, 0); |
| 7051 | appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); |
| 7052 | } |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 7053 | appendText(&sSql, zSep, 0); |
| 7054 | appendText(&sSql, sQuery.z, '\''); |
| 7055 | sQuery.n = 0; |
| 7056 | appendText(&sSql, ",", 0); |
| 7057 | appendText(&sSql, zTab, '\''); |
| 7058 | zSep = "),("; |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 7059 | } |
| 7060 | sqlite3_finalize(pStmt); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 7061 | if( bSeparate ){ |
| 7062 | zSql = sqlite3_mprintf( |
| 7063 | "%s))" |
| 7064 | " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" |
| 7065 | " FROM [sha3sum$query]", |
| 7066 | sSql.z, iSize); |
| 7067 | }else{ |
| 7068 | zSql = sqlite3_mprintf( |
| 7069 | "%s))" |
| 7070 | " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" |
| 7071 | " FROM [sha3sum$query]", |
| 7072 | sSql.z, iSize); |
| 7073 | } |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 7074 | freeText(&sQuery); |
drh | 3ee83ef | 2017-03-08 17:56:54 +0000 | [diff] [blame] | 7075 | freeText(&sSql); |
drh | 1554bc8 | 2017-03-08 16:10:34 +0000 | [diff] [blame] | 7076 | if( bDebug ){ |
| 7077 | utf8_printf(p->out, "%s\n", zSql); |
| 7078 | }else{ |
| 7079 | shell_exec(p->db, zSql, shell_callback, p, 0); |
| 7080 | } |
| 7081 | sqlite3_free(zSql); |
| 7082 | }else |
| 7083 | |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 7084 | if( c=='s' |
| 7085 | && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 7086 | ){ |
| 7087 | char *zCmd; |
drh | 5402710 | 2014-08-06 14:36:53 +0000 | [diff] [blame] | 7088 | int i, x; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7089 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7090 | raw_printf(stderr, "Usage: .system COMMAND\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7091 | rc = 1; |
| 7092 | goto meta_command_exit; |
| 7093 | } |
drh | dcb3e3d | 2014-05-29 03:17:29 +0000 | [diff] [blame] | 7094 | zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 7095 | for(i=2; i<nArg; i++){ |
drh | dcb3e3d | 2014-05-29 03:17:29 +0000 | [diff] [blame] | 7096 | zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", |
| 7097 | zCmd, azArg[i]); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 7098 | } |
drh | 5402710 | 2014-08-06 14:36:53 +0000 | [diff] [blame] | 7099 | x = system(zCmd); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 7100 | sqlite3_free(zCmd); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7101 | if( x ) raw_printf(stderr, "System command returns %d\n", x); |
drh | 62cdde5 | 2014-05-28 20:22:28 +0000 | [diff] [blame] | 7102 | }else |
| 7103 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7104 | if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 7105 | static const char *azBool[] = { "off", "on", "full", "unk" }; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7106 | int i; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7107 | if( nArg!=1 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7108 | raw_printf(stderr, "Usage: .show\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7109 | rc = 1; |
| 7110 | goto meta_command_exit; |
| 7111 | } |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7112 | utf8_printf(p->out, "%12.12s: %s\n","echo", |
| 7113 | azBool[ShellHasFlag(p, SHFLG_Echo)]); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 7114 | utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 7115 | utf8_printf(p->out, "%12.12s: %s\n","explain", |
| 7116 | p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 7117 | utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7118 | utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
| 7119 | utf8_printf(p->out, "%12.12s: ", "nullvalue"); |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 7120 | output_c_string(p->out, p->nullValue); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7121 | raw_printf(p->out, "\n"); |
| 7122 | utf8_printf(p->out,"%12.12s: %s\n","output", |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 7123 | strlen30(p->outfile) ? p->outfile : "stdout"); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7124 | utf8_printf(p->out,"%12.12s: ", "colseparator"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7125 | output_c_string(p->out, p->colSeparator); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7126 | raw_printf(p->out, "\n"); |
| 7127 | utf8_printf(p->out,"%12.12s: ", "rowseparator"); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7128 | output_c_string(p->out, p->rowSeparator); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7129 | raw_printf(p->out, "\n"); |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 7130 | utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7131 | utf8_printf(p->out, "%12.12s: ", "width"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7132 | for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7133 | raw_printf(p->out, "%d ", p->colWidth[i]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7134 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7135 | raw_printf(p->out, "\n"); |
drh | cd0509e | 2016-09-16 00:26:08 +0000 | [diff] [blame] | 7136 | utf8_printf(p->out, "%12.12s: %s\n", "filename", |
| 7137 | p->zDbFilename ? p->zDbFilename : ""); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7138 | }else |
| 7139 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7140 | if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ |
| 7141 | if( nArg==2 ){ |
| 7142 | p->statsOn = booleanValue(azArg[1]); |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 7143 | }else if( nArg==1 ){ |
| 7144 | display_stats(p->db, p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7145 | }else{ |
drh | 3478490 | 2016-02-27 17:12:36 +0000 | [diff] [blame] | 7146 | raw_printf(stderr, "Usage: .stats ?on|off?\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7147 | rc = 1; |
| 7148 | } |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 7149 | }else |
| 7150 | |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 7151 | if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) |
| 7152 | || (c=='i' && (strncmp(azArg[0], "indices", n)==0 |
| 7153 | || strncmp(azArg[0], "indexes", n)==0) ) |
| 7154 | ){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7155 | sqlite3_stmt *pStmt; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 7156 | char **azResult; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7157 | int nRow, nAlloc; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7158 | int ii; |
drh | 594ccd0 | 2017-06-15 12:50:47 +0000 | [diff] [blame] | 7159 | ShellText s; |
| 7160 | initText(&s); |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 7161 | open_db(p, 0); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7162 | rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 7163 | if( rc ) return shellDatabaseError(p->db); |
| 7164 | |
drh | 594ccd0 | 2017-06-15 12:50:47 +0000 | [diff] [blame] | 7165 | if( nArg>2 && c=='i' ){ |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 7166 | /* It is an historical accident that the .indexes command shows an error |
| 7167 | ** when called with the wrong number of arguments whereas the .tables |
| 7168 | ** command does not. */ |
| 7169 | raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); |
| 7170 | rc = 1; |
| 7171 | goto meta_command_exit; |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 7172 | } |
drh | 594ccd0 | 2017-06-15 12:50:47 +0000 | [diff] [blame] | 7173 | for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7174 | const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
drh | 594ccd0 | 2017-06-15 12:50:47 +0000 | [diff] [blame] | 7175 | if( zDbName==0 ) continue; |
| 7176 | if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0); |
| 7177 | if( sqlite3_stricmp(zDbName, "main")==0 ){ |
| 7178 | appendText(&s, "SELECT name FROM ", 0); |
drh | 6a5a420 | 2016-12-24 21:32:40 +0000 | [diff] [blame] | 7179 | }else{ |
drh | 594ccd0 | 2017-06-15 12:50:47 +0000 | [diff] [blame] | 7180 | appendText(&s, "SELECT ", 0); |
| 7181 | appendText(&s, zDbName, '\''); |
| 7182 | appendText(&s, "||'.'||name FROM ", 0); |
| 7183 | } |
| 7184 | appendText(&s, zDbName, '"'); |
| 7185 | appendText(&s, ".sqlite_master ", 0); |
| 7186 | if( c=='t' ){ |
| 7187 | appendText(&s," WHERE type IN ('table','view')" |
| 7188 | " AND name NOT LIKE 'sqlite_%'" |
| 7189 | " AND name LIKE ?1", 0); |
| 7190 | }else{ |
| 7191 | appendText(&s," WHERE type='index'" |
| 7192 | " AND tbl_name LIKE ?1", 0); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7193 | } |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 7194 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 7195 | rc = sqlite3_finalize(pStmt); |
drh | 594ccd0 | 2017-06-15 12:50:47 +0000 | [diff] [blame] | 7196 | appendText(&s, " ORDER BY 1", 0); |
| 7197 | rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); |
| 7198 | freeText(&s); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 7199 | if( rc ) return shellDatabaseError(p->db); |
| 7200 | |
| 7201 | /* Run the SQL statement prepared by the above block. Store the results |
| 7202 | ** as an array of nul-terminated strings in azResult[]. */ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7203 | nRow = nAlloc = 0; |
| 7204 | azResult = 0; |
| 7205 | if( nArg>1 ){ |
| 7206 | sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 7207 | }else{ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7208 | sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); |
| 7209 | } |
| 7210 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 7211 | if( nRow>=nAlloc ){ |
| 7212 | char **azNew; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 7213 | int n2 = nAlloc*2 + 10; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7214 | azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7215 | if( azNew==0 ){ |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 7216 | rc = shellNomemError(); |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7217 | break; |
| 7218 | } |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 7219 | nAlloc = n2; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7220 | azResult = azNew; |
| 7221 | } |
| 7222 | azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 7223 | if( 0==azResult[nRow] ){ |
| 7224 | rc = shellNomemError(); |
| 7225 | break; |
| 7226 | } |
| 7227 | nRow++; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7228 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 7229 | if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ |
| 7230 | rc = shellDatabaseError(p->db); |
| 7231 | } |
| 7232 | |
| 7233 | /* Pretty-print the contents of array azResult[] to the output */ |
| 7234 | if( rc==0 && nRow>0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 7235 | int len, maxlen = 0; |
| 7236 | int i, j; |
| 7237 | int nPrintCol, nPrintRow; |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7238 | for(i=0; i<nRow; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 7239 | len = strlen30(azResult[i]); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 7240 | if( len>maxlen ) maxlen = len; |
| 7241 | } |
| 7242 | nPrintCol = 80/(maxlen+2); |
| 7243 | if( nPrintCol<1 ) nPrintCol = 1; |
| 7244 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 7245 | for(i=0; i<nPrintRow; i++){ |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7246 | for(j=i; j<nRow; j+=nPrintRow){ |
| 7247 | char *zSp = j<nPrintRow ? "" : " "; |
drh | e05461c | 2015-12-30 13:36:57 +0000 | [diff] [blame] | 7248 | utf8_printf(p->out, "%s%-*s", zSp, maxlen, |
| 7249 | azResult[j] ? azResult[j]:""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 7250 | } |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7251 | raw_printf(p->out, "\n"); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 7252 | } |
| 7253 | } |
dan | d95bb39 | 2015-09-30 11:19:05 +0000 | [diff] [blame] | 7254 | |
drh | 9878123 | 2012-04-23 12:38:05 +0000 | [diff] [blame] | 7255 | for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
| 7256 | sqlite3_free(azResult); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7257 | }else |
| 7258 | |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 7259 | /* Begin redirecting output to the file "testcase-out.txt" */ |
| 7260 | if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ |
| 7261 | output_reset(p); |
| 7262 | p->out = output_file_open("testcase-out.txt"); |
| 7263 | if( p->out==0 ){ |
mistachkin | 2f9a613 | 2016-11-11 05:19:45 +0000 | [diff] [blame] | 7264 | raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 7265 | } |
drh | 760c816 | 2016-09-16 02:52:22 +0000 | [diff] [blame] | 7266 | if( nArg>=2 ){ |
| 7267 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); |
| 7268 | }else{ |
| 7269 | sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); |
| 7270 | } |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 7271 | }else |
drh | 2db8211 | 2016-09-15 21:35:24 +0000 | [diff] [blame] | 7272 | |
drh | d12602a | 2016-12-07 15:49:02 +0000 | [diff] [blame] | 7273 | #ifndef SQLITE_UNTESTABLE |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7274 | if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){ |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 7275 | static const struct { |
| 7276 | const char *zCtrlName; /* Name of a test-control option */ |
| 7277 | int ctrlCode; /* Integer code for that option */ |
| 7278 | } aCtrl[] = { |
| 7279 | { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE }, |
| 7280 | { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE }, |
| 7281 | { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET }, |
| 7282 | { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST }, |
| 7283 | { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL }, |
| 7284 | { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS }, |
| 7285 | { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE }, |
| 7286 | { "assert", SQLITE_TESTCTRL_ASSERT }, |
| 7287 | { "always", SQLITE_TESTCTRL_ALWAYS }, |
| 7288 | { "reserve", SQLITE_TESTCTRL_RESERVE }, |
| 7289 | { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS }, |
| 7290 | { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD }, |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 7291 | { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC }, |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 7292 | { "byteorder", SQLITE_TESTCTRL_BYTEORDER }, |
drh | e4bb23a | 2015-01-19 15:05:54 +0000 | [diff] [blame] | 7293 | { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT }, |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 7294 | { "imposter", SQLITE_TESTCTRL_IMPOSTER }, |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 7295 | }; |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7296 | int testctrl = -1; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 7297 | int rc2 = 0; |
| 7298 | int i, n2; |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 7299 | open_db(p, 0); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7300 | |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 7301 | /* convert testctrl text option to value. allow any unique prefix |
| 7302 | ** of the option name, or a numerical value. */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 7303 | n2 = strlen30(azArg[1]); |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 7304 | for(i=0; i<ArraySize(aCtrl); i++){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 7305 | if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){ |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 7306 | if( testctrl<0 ){ |
| 7307 | testctrl = aCtrl[i].ctrlCode; |
| 7308 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7309 | utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]); |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 7310 | testctrl = -1; |
| 7311 | break; |
| 7312 | } |
| 7313 | } |
| 7314 | } |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 7315 | if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7316 | if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7317 | utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7318 | }else{ |
| 7319 | switch(testctrl){ |
| 7320 | |
| 7321 | /* sqlite3_test_control(int, db, int) */ |
| 7322 | case SQLITE_TESTCTRL_OPTIMIZATIONS: |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7323 | case SQLITE_TESTCTRL_RESERVE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7324 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7325 | int opt = (int)strtol(azArg[2], 0, 0); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 7326 | rc2 = sqlite3_test_control(testctrl, p->db, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7327 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7328 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7329 | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 7330 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7331 | } |
| 7332 | break; |
| 7333 | |
| 7334 | /* sqlite3_test_control(int) */ |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 7335 | case SQLITE_TESTCTRL_PRNG_SAVE: |
| 7336 | case SQLITE_TESTCTRL_PRNG_RESTORE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7337 | case SQLITE_TESTCTRL_PRNG_RESET: |
drh | 2cf4acb | 2014-04-18 00:06:02 +0000 | [diff] [blame] | 7338 | case SQLITE_TESTCTRL_BYTEORDER: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7339 | if( nArg==2 ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 7340 | rc2 = sqlite3_test_control(testctrl); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7341 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7342 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7343 | utf8_printf(stderr,"Error: testctrl %s takes no options\n", |
| 7344 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7345 | } |
| 7346 | break; |
| 7347 | |
| 7348 | /* sqlite3_test_control(int, uint) */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7349 | case SQLITE_TESTCTRL_PENDING_BYTE: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7350 | if( nArg==3 ){ |
drh | af66433 | 2013-07-18 20:28:29 +0000 | [diff] [blame] | 7351 | unsigned int opt = (unsigned int)integerValue(azArg[2]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 7352 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7353 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7354 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7355 | utf8_printf(stderr,"Error: testctrl %s takes a single unsigned" |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 7356 | " int option\n", azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7357 | } |
| 7358 | break; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7359 | |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7360 | /* sqlite3_test_control(int, int) */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7361 | case SQLITE_TESTCTRL_ASSERT: |
| 7362 | case SQLITE_TESTCTRL_ALWAYS: |
| 7363 | case SQLITE_TESTCTRL_NEVER_CORRUPT: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7364 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7365 | int opt = booleanValue(azArg[2]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 7366 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7367 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7368 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7369 | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
drh | d416fe7 | 2011-03-17 16:45:50 +0000 | [diff] [blame] | 7370 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7371 | } |
| 7372 | break; |
| 7373 | |
| 7374 | /* sqlite3_test_control(int, char *) */ |
| 7375 | #ifdef SQLITE_N_KEYWORD |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7376 | case SQLITE_TESTCTRL_ISKEYWORD: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7377 | if( nArg==3 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7378 | const char *opt = azArg[2]; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 7379 | rc2 = sqlite3_test_control(testctrl, opt); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7380 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7381 | } else { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7382 | utf8_printf(stderr, |
| 7383 | "Error: testctrl %s takes a single char * option\n", |
| 7384 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7385 | } |
| 7386 | break; |
| 7387 | #endif |
| 7388 | |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 7389 | case SQLITE_TESTCTRL_IMPOSTER: |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 7390 | if( nArg==5 ){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7391 | rc2 = sqlite3_test_control(testctrl, p->db, |
drh | 1ffede8 | 2015-01-30 20:59:27 +0000 | [diff] [blame] | 7392 | azArg[2], |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 7393 | integerValue(azArg[3]), |
| 7394 | integerValue(azArg[4])); |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7395 | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 7396 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7397 | raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n"); |
drh | 8964b34 | 2015-01-29 17:54:52 +0000 | [diff] [blame] | 7398 | } |
| 7399 | break; |
| 7400 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7401 | case SQLITE_TESTCTRL_BITVEC_TEST: |
| 7402 | case SQLITE_TESTCTRL_FAULT_INSTALL: |
| 7403 | case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: |
| 7404 | case SQLITE_TESTCTRL_SCRATCHMALLOC: |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7405 | default: |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7406 | utf8_printf(stderr, |
| 7407 | "Error: CLI support for testctrl %s not implemented\n", |
| 7408 | azArg[1]); |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7409 | break; |
| 7410 | } |
| 7411 | } |
| 7412 | }else |
drh | f196972 | 2017-02-17 23:52:00 +0000 | [diff] [blame] | 7413 | #endif /* !defined(SQLITE_UNTESTABLE) */ |
shaneh | 96887e1 | 2011-02-10 21:08:58 +0000 | [diff] [blame] | 7414 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7415 | if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 7416 | open_db(p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7417 | sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 7418 | }else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7419 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7420 | if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ |
| 7421 | if( nArg==2 ){ |
| 7422 | enableTimer = booleanValue(azArg[1]); |
| 7423 | if( enableTimer && !HAS_TIMER ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7424 | raw_printf(stderr, "Error: timer not available on this system.\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7425 | enableTimer = 0; |
| 7426 | } |
| 7427 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7428 | raw_printf(stderr, "Usage: .timer on|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7429 | rc = 1; |
| 7430 | } |
shaneh | e2aa9d7 | 2009-11-06 17:20:17 +0000 | [diff] [blame] | 7431 | }else |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7432 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7433 | if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 7434 | open_db(p, 0); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7435 | if( nArg!=2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7436 | raw_printf(stderr, "Usage: .trace FILE|off\n"); |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7437 | rc = 1; |
| 7438 | goto meta_command_exit; |
| 7439 | } |
drh | 657b4a8 | 2015-03-19 13:30:41 +0000 | [diff] [blame] | 7440 | output_file_close(p->traceOut); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 7441 | p->traceOut = output_file_open(azArg[1]); |
drh | bbb0be8 | 2012-06-27 16:12:27 +0000 | [diff] [blame] | 7442 | #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 7443 | if( p->traceOut==0 ){ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 7444 | sqlite3_trace_v2(p->db, 0, 0, 0); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 7445 | }else{ |
drh | 4b363a5 | 2016-07-23 20:27:41 +0000 | [diff] [blame] | 7446 | sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut); |
drh | 42f64e5 | 2012-04-04 16:56:23 +0000 | [diff] [blame] | 7447 | } |
| 7448 | #endif |
| 7449 | }else |
| 7450 | |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7451 | #if SQLITE_USER_AUTHENTICATION |
| 7452 | if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ |
| 7453 | if( nArg<2 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7454 | raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7455 | rc = 1; |
| 7456 | goto meta_command_exit; |
| 7457 | } |
drh | 7883ecf | 2014-09-11 16:19:31 +0000 | [diff] [blame] | 7458 | open_db(p, 0); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7459 | if( strcmp(azArg[1],"login")==0 ){ |
| 7460 | if( nArg!=4 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7461 | raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7462 | rc = 1; |
| 7463 | goto meta_command_exit; |
| 7464 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 7465 | rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], |
| 7466 | (int)strlen(azArg[3])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7467 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7468 | utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7469 | rc = 1; |
| 7470 | } |
| 7471 | }else if( strcmp(azArg[1],"add")==0 ){ |
| 7472 | if( nArg!=5 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7473 | raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7474 | rc = 1; |
| 7475 | goto meta_command_exit; |
| 7476 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 7477 | rc = sqlite3_user_add(p->db, azArg[2], |
| 7478 | azArg[3], (int)strlen(azArg[3]), |
| 7479 | booleanValue(azArg[4])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7480 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7481 | raw_printf(stderr, "User-Add failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7482 | rc = 1; |
| 7483 | } |
| 7484 | }else if( strcmp(azArg[1],"edit")==0 ){ |
| 7485 | if( nArg!=5 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7486 | raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7487 | rc = 1; |
| 7488 | goto meta_command_exit; |
| 7489 | } |
drh | d39c40f | 2014-09-11 00:27:53 +0000 | [diff] [blame] | 7490 | rc = sqlite3_user_change(p->db, azArg[2], |
| 7491 | azArg[3], (int)strlen(azArg[3]), |
| 7492 | booleanValue(azArg[4])); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7493 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7494 | raw_printf(stderr, "User-Edit failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7495 | rc = 1; |
| 7496 | } |
| 7497 | }else if( strcmp(azArg[1],"delete")==0 ){ |
| 7498 | if( nArg!=3 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7499 | raw_printf(stderr, "Usage: .user delete USER\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7500 | rc = 1; |
| 7501 | goto meta_command_exit; |
| 7502 | } |
| 7503 | rc = sqlite3_user_delete(p->db, azArg[2]); |
| 7504 | if( rc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7505 | raw_printf(stderr, "User-Delete failed: %d\n", rc); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7506 | rc = 1; |
| 7507 | } |
| 7508 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7509 | raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7510 | rc = 1; |
| 7511 | goto meta_command_exit; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7512 | } |
drh | f442e33 | 2014-09-10 19:01:14 +0000 | [diff] [blame] | 7513 | }else |
| 7514 | #endif /* SQLITE_USER_AUTHENTICATION */ |
| 7515 | |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 7516 | if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7517 | utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 7518 | sqlite3_libversion(), sqlite3_sourceid()); |
| 7519 | }else |
| 7520 | |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 7521 | if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ |
| 7522 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
drh | 38305ab | 2017-01-22 16:34:35 +0000 | [diff] [blame] | 7523 | sqlite3_vfs *pVfs = 0; |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 7524 | if( p->db ){ |
| 7525 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); |
| 7526 | if( pVfs ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7527 | utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); |
| 7528 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 7529 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 7530 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
drh | 790f287 | 2015-11-28 18:06:36 +0000 | [diff] [blame] | 7531 | } |
| 7532 | } |
| 7533 | }else |
| 7534 | |
drh | b19e735 | 2016-01-12 19:37:20 +0000 | [diff] [blame] | 7535 | if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ |
| 7536 | sqlite3_vfs *pVfs; |
| 7537 | sqlite3_vfs *pCurrent = 0; |
| 7538 | if( p->db ){ |
| 7539 | sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); |
| 7540 | } |
| 7541 | for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
| 7542 | utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, |
| 7543 | pVfs==pCurrent ? " <--- CURRENT" : ""); |
| 7544 | raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
| 7545 | raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
| 7546 | raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
| 7547 | if( pVfs->pNext ){ |
| 7548 | raw_printf(p->out, "-----------------------------------\n"); |
| 7549 | } |
| 7550 | } |
| 7551 | }else |
| 7552 | |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 7553 | if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ |
| 7554 | const char *zDbName = nArg==2 ? azArg[1] : "main"; |
| 7555 | char *zVfsName = 0; |
| 7556 | if( p->db ){ |
| 7557 | sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
| 7558 | if( zVfsName ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7559 | utf8_printf(p->out, "%s\n", zVfsName); |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 7560 | sqlite3_free(zVfsName); |
| 7561 | } |
| 7562 | } |
| 7563 | }else |
| 7564 | |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 7565 | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| 7566 | if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7567 | sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 7568 | }else |
| 7569 | #endif |
| 7570 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7571 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7572 | int j; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 7573 | assert( nArg<=ArraySize(azArg) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7574 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
drh | 348d19c | 2013-06-03 12:47:43 +0000 | [diff] [blame] | 7575 | p->colWidth[j-1] = (int)integerValue(azArg[j]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7576 | } |
| 7577 | }else |
| 7578 | |
| 7579 | { |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7580 | utf8_printf(stderr, "Error: unknown command or invalid arguments: " |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7581 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 7582 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7583 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7584 | |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7585 | meta_command_exit: |
| 7586 | if( p->outCount ){ |
| 7587 | p->outCount--; |
| 7588 | if( p->outCount==0 ) output_reset(p); |
| 7589 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7590 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7591 | } |
| 7592 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7593 | /* |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 7594 | ** Return TRUE if a semicolon occurs anywhere in the first N characters |
| 7595 | ** of string z[]. |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 7596 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7597 | static int line_contains_semicolon(const char *z, int N){ |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 7598 | int i; |
| 7599 | for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } |
| 7600 | return 0; |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 7601 | } |
| 7602 | |
| 7603 | /* |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 7604 | ** Test to see if a line consists entirely of whitespace. |
| 7605 | */ |
| 7606 | static int _all_whitespace(const char *z){ |
| 7607 | for(; *z; z++){ |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 7608 | if( IsSpace(z[0]) ) continue; |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 7609 | if( *z=='/' && z[1]=='*' ){ |
| 7610 | z += 2; |
| 7611 | while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
| 7612 | if( *z==0 ) return 0; |
| 7613 | z++; |
| 7614 | continue; |
| 7615 | } |
| 7616 | if( *z=='-' && z[1]=='-' ){ |
| 7617 | z += 2; |
| 7618 | while( *z && *z!='\n' ){ z++; } |
| 7619 | if( *z==0 ) return 1; |
| 7620 | continue; |
| 7621 | } |
| 7622 | return 0; |
| 7623 | } |
| 7624 | return 1; |
| 7625 | } |
| 7626 | |
| 7627 | /* |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 7628 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 7629 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 7630 | ** as is the Oracle "/". |
| 7631 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7632 | static int line_is_command_terminator(const char *zLine){ |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 7633 | while( IsSpace(zLine[0]) ){ zLine++; }; |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 7634 | if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ |
| 7635 | return 1; /* Oracle */ |
| 7636 | } |
drh | f0693c8 | 2011-10-11 20:41:54 +0000 | [diff] [blame] | 7637 | if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 7638 | && _all_whitespace(&zLine[2]) ){ |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 7639 | return 1; /* SQL Server */ |
| 7640 | } |
| 7641 | return 0; |
| 7642 | } |
| 7643 | |
| 7644 | /* |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 7645 | ** Return true if zSql is a complete SQL statement. Return false if it |
| 7646 | ** ends in the middle of a string literal or C-style comment. |
| 7647 | */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7648 | static int line_is_complete(char *zSql, int nSql){ |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 7649 | int rc; |
| 7650 | if( zSql==0 ) return 1; |
| 7651 | zSql[nSql] = ';'; |
| 7652 | zSql[nSql+1] = 0; |
| 7653 | rc = sqlite3_complete(zSql); |
| 7654 | zSql[nSql] = 0; |
| 7655 | return rc; |
| 7656 | } |
| 7657 | |
| 7658 | /* |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 7659 | ** Run a single line of SQL |
| 7660 | */ |
| 7661 | static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ |
| 7662 | int rc; |
| 7663 | char *zErrMsg = 0; |
| 7664 | |
| 7665 | open_db(p, 0); |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7666 | if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 7667 | BEGIN_TIMER; |
| 7668 | rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); |
| 7669 | END_TIMER; |
| 7670 | if( rc || zErrMsg ){ |
| 7671 | char zPrefix[100]; |
| 7672 | if( in!=0 || !stdin_is_interactive ){ |
| 7673 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
| 7674 | "Error: near line %d:", startline); |
| 7675 | }else{ |
| 7676 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); |
| 7677 | } |
| 7678 | if( zErrMsg!=0 ){ |
| 7679 | utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); |
| 7680 | sqlite3_free(zErrMsg); |
| 7681 | zErrMsg = 0; |
| 7682 | }else{ |
| 7683 | utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
| 7684 | } |
| 7685 | return 1; |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7686 | }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 7687 | raw_printf(p->out, "changes: %3d total_changes: %d\n", |
| 7688 | sqlite3_changes(p->db), sqlite3_total_changes(p->db)); |
| 7689 | } |
| 7690 | return 0; |
| 7691 | } |
| 7692 | |
| 7693 | |
| 7694 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7695 | ** Read input from *in and process it. If *in==0 then input |
| 7696 | ** is interactive - the user is typing it it. Otherwise, input |
| 7697 | ** is coming from a file or device. A prompt is issued and history |
| 7698 | ** is saved only if input is interactive. An interrupt signal will |
| 7699 | ** cause this routine to exit immediately, unless input is interactive. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7700 | ** |
| 7701 | ** Return the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7702 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 7703 | static int process_input(ShellState *p, FILE *in){ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7704 | char *zLine = 0; /* A single input line */ |
| 7705 | char *zSql = 0; /* Accumulated SQL text */ |
| 7706 | int nLine; /* Length of current line */ |
| 7707 | int nSql = 0; /* Bytes of zSql[] used */ |
| 7708 | int nAlloc = 0; /* Allocated zSql[] space */ |
| 7709 | int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7710 | int rc; /* Error code */ |
| 7711 | int errCnt = 0; /* Number of errors seen */ |
| 7712 | int lineno = 0; /* Current line number */ |
| 7713 | int startline = 0; /* Line number for start of current input */ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7714 | |
| 7715 | while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ |
| 7716 | fflush(p->out); |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7717 | zLine = one_input_line(in, zLine, nSql>0); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7718 | if( zLine==0 ){ |
drh | 9b8d357 | 2012-04-21 11:33:39 +0000 | [diff] [blame] | 7719 | /* End of input */ |
drh | fc8b40f | 2016-09-07 10:10:18 +0000 | [diff] [blame] | 7720 | if( in==0 && stdin_is_interactive ) printf("\n"); |
drh | 9b8d357 | 2012-04-21 11:33:39 +0000 | [diff] [blame] | 7721 | break; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7722 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7723 | if( seenInterrupt ){ |
| 7724 | if( in!=0 ) break; |
| 7725 | seenInterrupt = 0; |
| 7726 | } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7727 | lineno++; |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 7728 | if( nSql==0 && _all_whitespace(zLine) ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7729 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 7730 | continue; |
| 7731 | } |
drh | 2af0b2d | 2002-02-21 02:25:02 +0000 | [diff] [blame] | 7732 | if( zLine && zLine[0]=='.' && nSql==0 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7733 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7734 | rc = do_meta_command(zLine, p); |
shane | 916f961 | 2009-10-23 00:37:15 +0000 | [diff] [blame] | 7735 | if( rc==2 ){ /* exit requested */ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 7736 | break; |
| 7737 | }else if( rc ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7738 | errCnt++; |
| 7739 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7740 | continue; |
| 7741 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7742 | if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7743 | memcpy(zLine,";",2); |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 7744 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7745 | nLine = strlen30(zLine); |
| 7746 | if( nSql+nLine+2>=nAlloc ){ |
| 7747 | nAlloc = nSql+nLine+100; |
| 7748 | zSql = realloc(zSql, nAlloc); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7749 | if( zSql==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7750 | raw_printf(stderr, "Error: out of memory\n"); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7751 | exit(1); |
| 7752 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7753 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7754 | nSqlPrior = nSql; |
| 7755 | if( nSql==0 ){ |
| 7756 | int i; |
| 7757 | for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 7758 | assert( nAlloc>0 && zSql!=0 ); |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7759 | memcpy(zSql, zLine+i, nLine+1-i); |
| 7760 | startline = lineno; |
| 7761 | nSql = nLine-i; |
| 7762 | }else{ |
| 7763 | zSql[nSql++] = '\n'; |
| 7764 | memcpy(zSql+nSql, zLine, nLine+1); |
| 7765 | nSql += nLine; |
| 7766 | } |
| 7767 | if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 7768 | && sqlite3_complete(zSql) ){ |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 7769 | errCnt += runOneSqlLine(p, zSql, in, startline); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7770 | nSql = 0; |
drh | c2ce0be | 2014-05-29 12:36:14 +0000 | [diff] [blame] | 7771 | if( p->outCount ){ |
| 7772 | output_reset(p); |
| 7773 | p->outCount = 0; |
| 7774 | } |
drh | 9f099fd | 2013-08-06 14:01:46 +0000 | [diff] [blame] | 7775 | }else if( nSql && _all_whitespace(zSql) ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 7776 | if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql); |
drh | 7a411f4 | 2013-04-17 17:33:17 +0000 | [diff] [blame] | 7777 | nSql = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7778 | } |
| 7779 | } |
drh | 4e8142c | 2016-11-11 14:54:22 +0000 | [diff] [blame] | 7780 | if( nSql && !_all_whitespace(zSql) ){ |
| 7781 | runOneSqlLine(p, zSql, in, startline); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7782 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 7783 | free(zSql); |
danielk1977 | 2ac2762 | 2007-07-03 05:31:16 +0000 | [diff] [blame] | 7784 | free(zLine); |
drh | 4d15a0d | 2012-12-01 20:21:22 +0000 | [diff] [blame] | 7785 | return errCnt>0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 7786 | } |
| 7787 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7788 | /* |
| 7789 | ** Return a pathname which is the user's home directory. A |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7790 | ** 0 return indicates an error of some kind. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7791 | */ |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7792 | static char *find_home_dir(int clearFlag){ |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7793 | static char *home_dir = NULL; |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7794 | if( clearFlag ){ |
| 7795 | free(home_dir); |
| 7796 | home_dir = 0; |
| 7797 | return 0; |
| 7798 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7799 | if( home_dir ) return home_dir; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7800 | |
drh | 4ace536 | 2014-11-10 14:42:28 +0000 | [diff] [blame] | 7801 | #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ |
| 7802 | && !defined(__RTP__) && !defined(_WRS_KERNEL) |
mistachkin | c8bde37 | 2012-06-18 08:00:56 +0000 | [diff] [blame] | 7803 | { |
| 7804 | struct passwd *pwent; |
| 7805 | uid_t uid = getuid(); |
| 7806 | if( (pwent=getpwuid(uid)) != NULL) { |
| 7807 | home_dir = pwent->pw_dir; |
| 7808 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7809 | } |
| 7810 | #endif |
| 7811 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 7812 | #if defined(_WIN32_WCE) |
| 7813 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
| 7814 | */ |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7815 | home_dir = "/"; |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 7816 | #else |
| 7817 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 7818 | #if defined(_WIN32) || defined(WIN32) |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 7819 | if (!home_dir) { |
| 7820 | home_dir = getenv("USERPROFILE"); |
| 7821 | } |
| 7822 | #endif |
| 7823 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7824 | if (!home_dir) { |
| 7825 | home_dir = getenv("HOME"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7826 | } |
| 7827 | |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 7828 | #if defined(_WIN32) || defined(WIN32) |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7829 | if (!home_dir) { |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 7830 | char *zDrive, *zPath; |
| 7831 | int n; |
| 7832 | zDrive = getenv("HOMEDRIVE"); |
| 7833 | zPath = getenv("HOMEPATH"); |
| 7834 | if( zDrive && zPath ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 7835 | n = strlen30(zDrive) + strlen30(zPath) + 1; |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 7836 | home_dir = malloc( n ); |
| 7837 | if( home_dir==0 ) return 0; |
| 7838 | sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
| 7839 | return home_dir; |
| 7840 | } |
| 7841 | home_dir = "c:\\"; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7842 | } |
| 7843 | #endif |
| 7844 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 7845 | #endif /* !_WIN32_WCE */ |
| 7846 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7847 | if( home_dir ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 7848 | int n = strlen30(home_dir) + 1; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7849 | char *z = malloc( n ); |
| 7850 | if( z ) memcpy(z, home_dir, n); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7851 | home_dir = z; |
| 7852 | } |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7853 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7854 | return home_dir; |
| 7855 | } |
| 7856 | |
| 7857 | /* |
| 7858 | ** Read input from the file given by sqliterc_override. Or if that |
| 7859 | ** parameter is NULL, take input from ~/.sqliterc |
shane | 9bd1b44 | 2009-10-23 01:27:39 +0000 | [diff] [blame] | 7860 | ** |
| 7861 | ** Returns the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7862 | */ |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7863 | static void process_sqliterc( |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 7864 | ShellState *p, /* Configuration data */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7865 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 7866 | ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7867 | char *home_dir = NULL; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7868 | const char *sqliterc = sqliterc_override; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 7869 | char *zBuf = 0; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7870 | FILE *in = NULL; |
| 7871 | |
| 7872 | if (sqliterc == NULL) { |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 7873 | home_dir = find_home_dir(0); |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7874 | if( home_dir==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7875 | raw_printf(stderr, "-- warning: cannot find home directory;" |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7876 | " cannot read ~/.sqliterc\n"); |
| 7877 | return; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 7878 | } |
drh | 2f3de32 | 2012-06-27 16:41:31 +0000 | [diff] [blame] | 7879 | sqlite3_initialize(); |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7880 | zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
| 7881 | sqliterc = zBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7882 | } |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 7883 | in = fopen(sqliterc,"rb"); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7884 | if( in ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 7885 | if( stdin_is_interactive ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7886 | utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 7887 | } |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 7888 | process_input(p,in); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 7889 | fclose(in); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7890 | } |
drh | 85e7243 | 2012-04-11 11:38:53 +0000 | [diff] [blame] | 7891 | sqlite3_free(zBuf); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7892 | } |
| 7893 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7894 | /* |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7895 | ** Show available command line options |
| 7896 | */ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7897 | static const char zOptions[] = |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 7898 | " -ascii set output mode to 'ascii'\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7899 | " -bail stop after hitting an error\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7900 | " -batch force batch I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7901 | " -column set output mode to 'column'\n" |
mistachkin | 6d81d75 | 2012-10-25 15:43:28 +0000 | [diff] [blame] | 7902 | " -cmd COMMAND run \"COMMAND\" before reading stdin\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 7903 | " -csv set output mode to 'csv'\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7904 | " -echo print commands before execution\n" |
mistachkin | 6d81d75 | 2012-10-25 15:43:28 +0000 | [diff] [blame] | 7905 | " -init FILENAME read/process named file\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7906 | " -[no]header turn headers on or off\n" |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7907 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 7908 | " -heap SIZE Size of heap for memsys3 or memsys5\n" |
| 7909 | #endif |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7910 | " -help show this message\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7911 | " -html set output mode to HTML\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7912 | " -interactive force interactive I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7913 | " -line set output mode to 'line'\n" |
| 7914 | " -list set output mode to 'list'\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7915 | " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 7916 | " -mmap N default mmap size set to N\n" |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 7917 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 7918 | " -multiplex enable the multiplexor VFS\n" |
| 7919 | #endif |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 7920 | " -newline SEP set output row separator. Default: '\\n'\n" |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7921 | " -nullvalue TEXT set text string for NULL values. Default ''\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7922 | " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
drh | a501f7d | 2017-06-29 21:33:25 +0000 | [diff] [blame] | 7923 | " -quote set output mode to 'quote'\n" |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7924 | " -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] | 7925 | " -separator SEP set output column separator. Default: '|'\n" |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 7926 | " -stats print memory stats before each finalize\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7927 | " -version show SQLite version\n" |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7928 | " -vfs NAME use NAME as the default VFS\n" |
drh | 2b625e2 | 2011-03-16 17:05:28 +0000 | [diff] [blame] | 7929 | #ifdef SQLITE_ENABLE_VFSTRACE |
| 7930 | " -vfstrace enable tracing of all VFS calls\n" |
| 7931 | #endif |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7932 | ; |
| 7933 | static void usage(int showDetail){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7934 | utf8_printf(stderr, |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7935 | "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
drh | 80e8be9 | 2006-08-29 12:04:19 +0000 | [diff] [blame] | 7936 | "FILENAME is the name of an SQLite database. A new database is created\n" |
| 7937 | "if the file does not previously exist.\n", Argv0); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7938 | if( showDetail ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7939 | utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7940 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7941 | raw_printf(stderr, "Use the -help option for additional information\n"); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 7942 | } |
| 7943 | exit(1); |
| 7944 | } |
| 7945 | |
| 7946 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 7947 | ** Initialize the state information in data |
| 7948 | */ |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 7949 | static void main_init(ShellState *data) { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7950 | memset(data, 0, sizeof(*data)); |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 7951 | data->normalMode = data->cMode = data->mode = MODE_List; |
| 7952 | data->autoExplain = 1; |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 7953 | memcpy(data->colSeparator,SEP_Column, 2); |
| 7954 | memcpy(data->rowSeparator,SEP_Row, 2); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7955 | data->showHeader = 0; |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7956 | data->shellFlgs = SHFLG_Lookaside; |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 7957 | sqlite3_config(SQLITE_CONFIG_URI, 1); |
drh | 127f9d7 | 2010-02-23 01:47:00 +0000 | [diff] [blame] | 7958 | sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 7959 | sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7960 | sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
| 7961 | sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 7962 | } |
| 7963 | |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7964 | /* |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 7965 | ** Output text to the console in a font that attracts extra attention. |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7966 | */ |
| 7967 | #ifdef _WIN32 |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 7968 | static void printBold(const char *zText){ |
| 7969 | HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
| 7970 | CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; |
| 7971 | GetConsoleScreenBufferInfo(out, &defaultScreenInfo); |
| 7972 | SetConsoleTextAttribute(out, |
| 7973 | FOREGROUND_RED|FOREGROUND_INTENSITY |
| 7974 | ); |
| 7975 | printf("%s", zText); |
| 7976 | SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7977 | } |
| 7978 | #else |
drh | 5c7976f | 2014-02-10 19:59:27 +0000 | [diff] [blame] | 7979 | static void printBold(const char *zText){ |
| 7980 | printf("\033[1m%s\033[0m", zText); |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 7981 | } |
| 7982 | #endif |
| 7983 | |
| 7984 | /* |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7985 | ** Get the argument to an --option. Throw an error and die if no argument |
| 7986 | ** is available. |
| 7987 | */ |
| 7988 | static char *cmdline_option_value(int argc, char **argv, int i){ |
| 7989 | if( i==argc ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 7990 | utf8_printf(stderr, "%s: Error: missing argument to %s\n", |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 7991 | argv[0], argv[argc-1]); |
| 7992 | exit(1); |
| 7993 | } |
| 7994 | return argv[i]; |
| 7995 | } |
| 7996 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 7997 | #ifndef SQLITE_SHELL_IS_UTF8 |
| 7998 | # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) |
| 7999 | # define SQLITE_SHELL_IS_UTF8 (0) |
| 8000 | # else |
| 8001 | # define SQLITE_SHELL_IS_UTF8 (1) |
| 8002 | # endif |
| 8003 | #endif |
| 8004 | |
| 8005 | #if SQLITE_SHELL_IS_UTF8 |
mistachkin | 44723ce | 2015-03-21 02:22:37 +0000 | [diff] [blame] | 8006 | int SQLITE_CDECL main(int argc, char **argv){ |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 8007 | #else |
| 8008 | int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 8009 | char **argv; |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 8010 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 8011 | char *zErrMsg = 0; |
drh | dcd87a9 | 2014-08-18 13:45:42 +0000 | [diff] [blame] | 8012 | ShellState data; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8013 | const char *zInitFile = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 8014 | int i; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 8015 | int rc = 0; |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 8016 | int warnInmemoryDb = 0; |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 8017 | int readStdin = 1; |
| 8018 | int nCmd = 0; |
| 8019 | char **azCmd = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 8020 | |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 8021 | setBinaryMode(stdin, 0); |
| 8022 | setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 8023 | stdin_is_interactive = isatty(0); |
| 8024 | stdout_is_console = isatty(1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 8025 | |
drh | 69b30ab | 2014-02-27 15:11:52 +0000 | [diff] [blame] | 8026 | #if USE_SYSTEM_SQLITE+0!=1 |
drh | 52784bd | 2011-05-18 17:15:06 +0000 | [diff] [blame] | 8027 | if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 8028 | 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] | 8029 | sqlite3_sourceid(), SQLITE_SOURCE_ID); |
| 8030 | exit(1); |
| 8031 | } |
drh | c718190 | 2014-02-27 15:04:13 +0000 | [diff] [blame] | 8032 | #endif |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 8033 | main_init(&data); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 8034 | #if !SQLITE_SHELL_IS_UTF8 |
| 8035 | sqlite3_initialize(); |
| 8036 | argv = sqlite3_malloc64(sizeof(argv[0])*argc); |
| 8037 | if( argv==0 ){ |
| 8038 | raw_printf(stderr, "out of memory\n"); |
| 8039 | exit(1); |
| 8040 | } |
| 8041 | for(i=0; i<argc; i++){ |
| 8042 | argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]); |
| 8043 | if( argv[i]==0 ){ |
| 8044 | raw_printf(stderr, "out of memory\n"); |
| 8045 | exit(1); |
| 8046 | } |
| 8047 | } |
| 8048 | #endif |
mistachkin | 1810f22 | 2016-04-04 02:33:34 +0000 | [diff] [blame] | 8049 | assert( argc>=1 && argv && argv[0] ); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 8050 | Argv0 = argv[0]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 8051 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 8052 | /* Make sure we have a valid signal handler early, before anything |
| 8053 | ** else is done. |
| 8054 | */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 8055 | #ifdef SIGINT |
| 8056 | signal(SIGINT, interrupt_handler); |
| 8057 | #endif |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 8058 | |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 8059 | #ifdef SQLITE_SHELL_DBNAME_PROC |
| 8060 | { |
| 8061 | /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name |
| 8062 | ** of a C-function that will provide the name of the database file. Use |
| 8063 | ** this compile-time option to embed this shell program in larger |
| 8064 | ** applications. */ |
| 8065 | extern void SQLITE_SHELL_DBNAME_PROC(const char**); |
| 8066 | SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); |
| 8067 | warnInmemoryDb = 0; |
| 8068 | } |
| 8069 | #endif |
| 8070 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8071 | /* Do an initial pass through the command-line argument to locate |
| 8072 | ** the name of the database file, the name of the initialization file, |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 8073 | ** the size of the alternative malloc heap, |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8074 | ** and the first command to execute. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 8075 | */ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8076 | for(i=1; i<argc; i++){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 8077 | char *z; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 8078 | z = argv[i]; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8079 | if( z[0]!='-' ){ |
| 8080 | if( data.zDbFilename==0 ){ |
| 8081 | data.zDbFilename = z; |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 8082 | }else{ |
| 8083 | /* Excesss arguments are interpreted as SQL (or dot-commands) and |
| 8084 | ** mean that nothing is read from stdin */ |
| 8085 | readStdin = 0; |
| 8086 | nCmd++; |
| 8087 | azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); |
| 8088 | if( azCmd==0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 8089 | raw_printf(stderr, "out of memory\n"); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 8090 | exit(1); |
| 8091 | } |
| 8092 | azCmd[nCmd-1] = z; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8093 | } |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8094 | } |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8095 | if( z[1]=='-' ) z++; |
| 8096 | if( strcmp(z,"-separator")==0 |
| 8097 | || strcmp(z,"-nullvalue")==0 |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 8098 | || strcmp(z,"-newline")==0 |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8099 | || strcmp(z,"-cmd")==0 |
| 8100 | ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8101 | (void)cmdline_option_value(argc, argv, ++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8102 | }else if( strcmp(z,"-init")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8103 | zInitFile = cmdline_option_value(argc, argv, ++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8104 | }else if( strcmp(z,"-batch")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8105 | /* Need to check for batch mode here to so we can avoid printing |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 8106 | ** informational messages (like from process_sqliterc) before |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8107 | ** we do the actual processing of arguments later in a second pass. |
| 8108 | */ |
shane | f69573d | 2009-10-24 02:06:14 +0000 | [diff] [blame] | 8109 | stdin_is_interactive = 0; |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8110 | }else if( strcmp(z,"-heap")==0 ){ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 8111 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 8112 | const char *zSize; |
| 8113 | sqlite3_int64 szHeap; |
| 8114 | |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8115 | zSize = cmdline_option_value(argc, argv, ++i); |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 8116 | szHeap = integerValue(zSize); |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 8117 | if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 8118 | sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
drh | c530b9c | 2016-07-25 11:27:22 +0000 | [diff] [blame] | 8119 | #else |
| 8120 | (void)cmdline_option_value(argc, argv, ++i); |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 8121 | #endif |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 8122 | }else if( strcmp(z,"-scratch")==0 ){ |
| 8123 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 8124 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 8125 | if( sz>400000 ) sz = 400000; |
| 8126 | if( sz<2500 ) sz = 2500; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 8127 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 8128 | if( n>10 ) n = 10; |
| 8129 | if( n<1 ) n = 1; |
| 8130 | sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n); |
| 8131 | data.shellFlgs |= SHFLG_Scratch; |
| 8132 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 8133 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 8134 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 8135 | if( sz>70000 ) sz = 70000; |
drh | 3d38cec | 2015-11-11 15:28:52 +0000 | [diff] [blame] | 8136 | if( sz<0 ) sz = 0; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 8137 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 3d38cec | 2015-11-11 15:28:52 +0000 | [diff] [blame] | 8138 | sqlite3_config(SQLITE_CONFIG_PAGECACHE, |
| 8139 | (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 8140 | data.shellFlgs |= SHFLG_Pagecache; |
| 8141 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 8142 | int n, sz; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 8143 | sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 8144 | if( sz<0 ) sz = 0; |
mistachkin | 31970cc | 2014-09-01 01:16:49 +0000 | [diff] [blame] | 8145 | n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 8146 | if( n<0 ) n = 0; |
| 8147 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); |
| 8148 | if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 8149 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8150 | }else if( strcmp(z,"-vfstrace")==0 ){ |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 8151 | extern int vfstrace_register( |
| 8152 | const char *zTraceName, |
| 8153 | const char *zOldVfsName, |
| 8154 | int (*xOut)(const char*,void*), |
| 8155 | void *pOutArg, |
| 8156 | int makeDefault |
| 8157 | ); |
drh | 2b625e2 | 2011-03-16 17:05:28 +0000 | [diff] [blame] | 8158 | vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 8159 | #endif |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 8160 | #ifdef SQLITE_ENABLE_MULTIPLEX |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8161 | }else if( strcmp(z,"-multiplex")==0 ){ |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 8162 | extern int sqlite3_multiple_initialize(const char*,int); |
| 8163 | sqlite3_multiplex_initialize(0, 1); |
| 8164 | #endif |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 8165 | }else if( strcmp(z,"-mmap")==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 8166 | sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
| 8167 | sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8168 | }else if( strcmp(z,"-vfs")==0 ){ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8169 | sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i)); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 8170 | if( pVfs ){ |
| 8171 | sqlite3_vfs_register(pVfs, 1); |
| 8172 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 8173 | utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 8174 | exit(1); |
| 8175 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 8176 | } |
| 8177 | } |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8178 | if( data.zDbFilename==0 ){ |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 8179 | #ifndef SQLITE_OMIT_MEMORYDB |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8180 | data.zDbFilename = ":memory:"; |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 8181 | warnInmemoryDb = argc==1; |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 8182 | #else |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 8183 | utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
shane | 86f5bdb | 2009-10-24 02:00:07 +0000 | [diff] [blame] | 8184 | return 1; |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 8185 | #endif |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8186 | } |
| 8187 | data.out = stdout; |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 8188 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 8189 | /* Go ahead and open the database file if it already exists. If the |
| 8190 | ** file does not exist, delay opening it. This prevents empty database |
| 8191 | ** files from being created if a user mistypes the database name argument |
| 8192 | ** to the sqlite command-line tool. |
| 8193 | */ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 8194 | if( access(data.zDbFilename, 0)==0 ){ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 8195 | open_db(&data, 0); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 8196 | } |
| 8197 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8198 | /* Process the initialization file if there is one. If no -init option |
| 8199 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 8200 | ** try to process it. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 8201 | */ |
drh | 534f4df | 2015-02-28 14:03:35 +0000 | [diff] [blame] | 8202 | process_sqliterc(&data,zInitFile); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 8203 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8204 | /* Make a second pass through the command-line argument and set |
| 8205 | ** options. This second pass is delayed until after the initialization |
| 8206 | ** file is processed so that the command-line arguments will override |
| 8207 | ** settings in the initialization file. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 8208 | */ |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8209 | for(i=1; i<argc; i++){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8210 | char *z = argv[i]; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8211 | if( z[0]!='-' ) continue; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 8212 | if( z[1]=='-' ){ z++; } |
drh | 2e584cd | 2006-09-25 13:09:22 +0000 | [diff] [blame] | 8213 | if( strcmp(z,"-init")==0 ){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8214 | i++; |
| 8215 | }else if( strcmp(z,"-html")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 8216 | data.mode = MODE_Html; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8217 | }else if( strcmp(z,"-list")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 8218 | data.mode = MODE_List; |
drh | a501f7d | 2017-06-29 21:33:25 +0000 | [diff] [blame] | 8219 | }else if( strcmp(z,"-quote")==0 ){ |
| 8220 | data.mode = MODE_Quote; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8221 | }else if( strcmp(z,"-line")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 8222 | data.mode = MODE_Line; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8223 | }else if( strcmp(z,"-column")==0 ){ |
drh | 8b32e17 | 2002-04-08 02:42:57 +0000 | [diff] [blame] | 8224 | data.mode = MODE_Column; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 8225 | }else if( strcmp(z,"-csv")==0 ){ |
| 8226 | data.mode = MODE_Csv; |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 8227 | memcpy(data.colSeparator,",",2); |
| 8228 | }else if( strcmp(z,"-ascii")==0 ){ |
| 8229 | data.mode = MODE_Ascii; |
| 8230 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 8231 | SEP_Unit); |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 8232 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
mistachkin | fad4208 | 2014-07-24 22:13:12 +0000 | [diff] [blame] | 8233 | SEP_Record); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8234 | }else if( strcmp(z,"-separator")==0 ){ |
mistachkin | 636bf9f | 2014-07-19 20:15:16 +0000 | [diff] [blame] | 8235 | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8236 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 8237 | }else if( strcmp(z,"-newline")==0 ){ |
mistachkin | e0d6885 | 2014-12-11 03:12:33 +0000 | [diff] [blame] | 8238 | sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
drh | 6976c21 | 2014-07-24 12:09:47 +0000 | [diff] [blame] | 8239 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8240 | }else if( strcmp(z,"-nullvalue")==0 ){ |
mistachkin | 44b99f7 | 2014-12-11 03:29:14 +0000 | [diff] [blame] | 8241 | sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8242 | "%s",cmdline_option_value(argc,argv,++i)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8243 | }else if( strcmp(z,"-header")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 8244 | data.showHeader = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8245 | }else if( strcmp(z,"-noheader")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 8246 | data.showHeader = 0; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8247 | }else if( strcmp(z,"-echo")==0 ){ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 8248 | ShellSetFlag(&data, SHFLG_Echo); |
drh | efbf3b1 | 2014-02-28 20:47:24 +0000 | [diff] [blame] | 8249 | }else if( strcmp(z,"-eqp")==0 ){ |
| 8250 | data.autoEQP = 1; |
drh | eacd29d | 2016-04-15 15:03:27 +0000 | [diff] [blame] | 8251 | }else if( strcmp(z,"-eqpfull")==0 ){ |
| 8252 | data.autoEQP = 2; |
shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame] | 8253 | }else if( strcmp(z,"-stats")==0 ){ |
| 8254 | data.statsOn = 1; |
dan | 8d1edb9 | 2014-11-05 09:07:28 +0000 | [diff] [blame] | 8255 | }else if( strcmp(z,"-scanstats")==0 ){ |
| 8256 | data.scanstatsOn = 1; |
drh | 9569f60 | 2015-04-16 15:05:04 +0000 | [diff] [blame] | 8257 | }else if( strcmp(z,"-backslash")==0 ){ |
| 8258 | /* Undocumented command-line option: -backslash |
| 8259 | ** Causes C-style backslash escapes to be evaluated in SQL statements |
| 8260 | ** prior to sending the SQL into SQLite. Useful for injecting |
| 8261 | ** crazy bytes in the middle of SQL statements for testing and debugging. |
| 8262 | */ |
drh | e6e1d12 | 2017-03-09 13:50:49 +0000 | [diff] [blame] | 8263 | ShellSetFlag(&data, SHFLG_Backslash); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 8264 | }else if( strcmp(z,"-bail")==0 ){ |
| 8265 | bail_on_error = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 8266 | }else if( strcmp(z,"-version")==0 ){ |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 8267 | printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
drh | 151e3e1 | 2006-06-06 12:32:21 +0000 | [diff] [blame] | 8268 | return 0; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 8269 | }else if( strcmp(z,"-interactive")==0 ){ |
| 8270 | stdin_is_interactive = 1; |
| 8271 | }else if( strcmp(z,"-batch")==0 ){ |
| 8272 | stdin_is_interactive = 0; |
drh | 9c88d68 | 2010-12-17 14:03:01 +0000 | [diff] [blame] | 8273 | }else if( strcmp(z,"-heap")==0 ){ |
| 8274 | i++; |
drh | 44dec87 | 2014-08-30 15:49:25 +0000 | [diff] [blame] | 8275 | }else if( strcmp(z,"-scratch")==0 ){ |
| 8276 | i+=2; |
| 8277 | }else if( strcmp(z,"-pagecache")==0 ){ |
| 8278 | i+=2; |
| 8279 | }else if( strcmp(z,"-lookaside")==0 ){ |
| 8280 | i+=2; |
drh | 7d9f394 | 2013-04-03 01:26:54 +0000 | [diff] [blame] | 8281 | }else if( strcmp(z,"-mmap")==0 ){ |
| 8282 | i++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 8283 | }else if( strcmp(z,"-vfs")==0 ){ |
| 8284 | i++; |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 8285 | #ifdef SQLITE_ENABLE_VFSTRACE |
drh | 97ae8ff | 2011-03-16 16:56:29 +0000 | [diff] [blame] | 8286 | }else if( strcmp(z,"-vfstrace")==0 ){ |
| 8287 | i++; |
drh | 6f25e89 | 2011-07-08 17:02:57 +0000 | [diff] [blame] | 8288 | #endif |
| 8289 | #ifdef SQLITE_ENABLE_MULTIPLEX |
| 8290 | }else if( strcmp(z,"-multiplex")==0 ){ |
| 8291 | i++; |
| 8292 | #endif |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8293 | }else if( strcmp(z,"-help")==0 ){ |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 8294 | usage(1); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8295 | }else if( strcmp(z,"-cmd")==0 ){ |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 8296 | /* Run commands that follow -cmd first and separately from commands |
| 8297 | ** that simply appear on the command-line. This seems goofy. It would |
| 8298 | ** be better if all commands ran in the order that they appear. But |
| 8299 | ** we retain the goofy behavior for historical compatibility. */ |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8300 | if( i==argc-1 ) break; |
drh | 98d312f | 2012-10-25 15:23:14 +0000 | [diff] [blame] | 8301 | z = cmdline_option_value(argc,argv,++i); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8302 | if( z[0]=='.' ){ |
| 8303 | rc = do_meta_command(z, &data); |
drh | 99b3908 | 2013-04-17 12:19:48 +0000 | [diff] [blame] | 8304 | if( rc && bail_on_error ) return rc==2 ? 0 : rc; |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8305 | }else{ |
drh | 0578248 | 2013-10-24 15:20:20 +0000 | [diff] [blame] | 8306 | open_db(&data, 0); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8307 | rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg); |
| 8308 | if( zErrMsg!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 8309 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8310 | if( bail_on_error ) return rc!=0 ? rc : 1; |
| 8311 | }else if( rc!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 8312 | utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); |
drh | cc3b4f8 | 2012-02-07 14:13:50 +0000 | [diff] [blame] | 8313 | if( bail_on_error ) return rc; |
| 8314 | } |
| 8315 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 8316 | }else{ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 8317 | utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); |
| 8318 | raw_printf(stderr,"Use -help for a list of options.\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 8319 | return 1; |
| 8320 | } |
drh | 700c252 | 2016-02-09 18:39:25 +0000 | [diff] [blame] | 8321 | data.cMode = data.mode; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 8322 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 8323 | |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 8324 | if( !readStdin ){ |
| 8325 | /* Run all arguments that do not begin with '-' as if they were separate |
| 8326 | ** command-line inputs, except for the argToSkip argument which contains |
| 8327 | ** the database filename. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 8328 | */ |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 8329 | for(i=0; i<nCmd; i++){ |
| 8330 | if( azCmd[i][0]=='.' ){ |
| 8331 | rc = do_meta_command(azCmd[i], &data); |
| 8332 | if( rc ) return rc==2 ? 0 : rc; |
| 8333 | }else{ |
| 8334 | open_db(&data, 0); |
| 8335 | rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg); |
| 8336 | if( zErrMsg!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 8337 | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 8338 | return rc!=0 ? rc : 1; |
| 8339 | }else if( rc!=0 ){ |
mistachkin | aae280e | 2015-12-31 19:06:24 +0000 | [diff] [blame] | 8340 | utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 8341 | return rc; |
| 8342 | } |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 8343 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 8344 | } |
drh | ac5649a | 2014-11-28 13:35:03 +0000 | [diff] [blame] | 8345 | free(azCmd); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 8346 | }else{ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 8347 | /* Run commands received from standard input |
| 8348 | */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 8349 | if( stdin_is_interactive ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 8350 | char *zHome; |
| 8351 | char *zHistory = 0; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 8352 | int nHistory; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 8353 | printf( |
drh | 743e003 | 2011-12-12 16:51:50 +0000 | [diff] [blame] | 8354 | "SQLite version %s %.19s\n" /*extra-version-info*/ |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 8355 | "Enter \".help\" for usage hints.\n", |
drh | 9fd301b | 2011-06-03 13:28:22 +0000 | [diff] [blame] | 8356 | sqlite3_libversion(), sqlite3_sourceid() |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 8357 | ); |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 8358 | if( warnInmemoryDb ){ |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 8359 | printf("Connected to a "); |
mistachkin | 378d01a | 2014-03-06 02:15:42 +0000 | [diff] [blame] | 8360 | printBold("transient in-memory database"); |
| 8361 | printf(".\nUse \".open FILENAME\" to reopen on a " |
drh | 1247aa4 | 2014-02-10 19:27:05 +0000 | [diff] [blame] | 8362 | "persistent database.\n"); |
drh | b373591 | 2014-02-10 16:13:42 +0000 | [diff] [blame] | 8363 | } |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 8364 | zHome = find_home_dir(0); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 8365 | if( zHome ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 8366 | nHistory = strlen30(zHome) + 20; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 8367 | if( (zHistory = malloc(nHistory))!=0 ){ |
| 8368 | sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 8369 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 8370 | } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 8371 | if( zHistory ){ shell_read_history(zHistory); } |
drh | 56eb09b | 2017-07-11 13:59:07 +0000 | [diff] [blame] | 8372 | #if HAVE_READLINE || HAVE_EDITLINE |
| 8373 | rl_attempted_completion_function = readline_completion; |
| 8374 | #elif HAVE_LINENOISE |
| 8375 | linenoiseSetCompletionCallback(linenoise_completion); |
| 8376 | #endif |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 8377 | rc = process_input(&data, 0); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 8378 | if( zHistory ){ |
dan | fd34d6d | 2015-02-25 10:54:53 +0000 | [diff] [blame] | 8379 | shell_stifle_history(100); |
| 8380 | shell_write_history(zHistory); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 8381 | free(zHistory); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 8382 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 8383 | }else{ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 8384 | rc = process_input(&data, stdin); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 8385 | } |
| 8386 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 8387 | set_table_name(&data, 0); |
drh | 72af077 | 2010-05-06 20:19:55 +0000 | [diff] [blame] | 8388 | if( data.db ){ |
drh | e622961 | 2014-08-18 15:08:26 +0000 | [diff] [blame] | 8389 | session_close_all(&data); |
drh | e14cd93 | 2010-12-08 03:28:17 +0000 | [diff] [blame] | 8390 | sqlite3_close(data.db); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 8391 | } |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 8392 | sqlite3_free(data.zFreeOnClose); |
drh | d145915 | 2016-09-16 19:11:03 +0000 | [diff] [blame] | 8393 | find_home_dir(1); |
mistachkin | 1fe36bb | 2016-04-04 02:16:44 +0000 | [diff] [blame] | 8394 | #if !SQLITE_SHELL_IS_UTF8 |
| 8395 | for(i=0; i<argc; i++) sqlite3_free(argv[i]); |
| 8396 | sqlite3_free(argv); |
| 8397 | #endif |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 8398 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 8399 | } |
drh | 2ce15c3 | 2017-07-11 13:34:40 +0000 | [diff] [blame] | 8400 | |