drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains code to implement the "sqlite" command line |
| 13 | ** utility for accessing SQLite databases. |
| 14 | ** |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame^] | 15 | ** $Id: shell.c,v 1.195 2008/12/11 02:58:27 shane Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 16 | */ |
shane | 18e526c | 2008-12-10 22:30:24 +0000 | [diff] [blame] | 17 | #if defined(_WIN32) || defined(WIN32) |
| 18 | /* This needs to come before any includes for MSVC compiler */ |
| 19 | #define _CRT_SECURE_NO_WARNINGS |
| 20 | #endif |
| 21 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 25 | #include <assert.h> |
drh | 1d482dd | 2004-05-31 18:23:07 +0000 | [diff] [blame] | 26 | #include "sqlite3.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 27 | #include <ctype.h> |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 28 | #include <stdarg.h> |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 29 | |
drh | 454ad58 | 2007-11-26 22:54:27 +0000 | [diff] [blame] | 30 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 31 | # include <signal.h> |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 32 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 33 | # include <pwd.h> |
| 34 | # endif |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 35 | # include <unistd.h> |
| 36 | # include <sys/types.h> |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 37 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 38 | |
drh | cdb36b7 | 2006-06-12 12:57:45 +0000 | [diff] [blame] | 39 | #ifdef __OS2__ |
| 40 | # include <unistd.h> |
| 41 | #endif |
| 42 | |
drh | 16e5955 | 2000-07-31 11:57:37 +0000 | [diff] [blame] | 43 | #if defined(HAVE_READLINE) && HAVE_READLINE==1 |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 44 | # include <readline/readline.h> |
| 45 | # include <readline/history.h> |
| 46 | #else |
drh | 9347b20 | 2003-07-18 01:30:59 +0000 | [diff] [blame] | 47 | # define readline(p) local_getline(p,stdin) |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 48 | # define add_history(X) |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 49 | # define read_history(X) |
| 50 | # define write_history(X) |
| 51 | # define stifle_history(X) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
adamd | 2e8464a | 2006-09-06 21:39:40 +0000 | [diff] [blame] | 54 | #if defined(_WIN32) || defined(WIN32) |
| 55 | # include <io.h> |
shane | 18e526c | 2008-12-10 22:30:24 +0000 | [diff] [blame] | 56 | #define isatty(h) _isatty(h) |
| 57 | #define access(f,m) _access((f),(m)) |
adamd | 2e8464a | 2006-09-06 21:39:40 +0000 | [diff] [blame] | 58 | #else |
drh | 4328c8b | 2003-04-26 02:50:11 +0000 | [diff] [blame] | 59 | /* Make sure isatty() has a prototype. |
| 60 | */ |
| 61 | extern int isatty(); |
adamd | 2e8464a | 2006-09-06 21:39:40 +0000 | [diff] [blame] | 62 | #endif |
drh | 4328c8b | 2003-04-26 02:50:11 +0000 | [diff] [blame] | 63 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 64 | #if defined(_WIN32_WCE) |
| 65 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() |
| 66 | * thus we always assume that we have a console. That can be |
| 67 | * overridden with the -batch command line option. |
| 68 | */ |
| 69 | #define isatty(x) 1 |
| 70 | #endif |
| 71 | |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 72 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(__RTP__) && !defined(_WRS_KERNEL) |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 73 | #include <sys/time.h> |
| 74 | #include <sys/resource.h> |
| 75 | |
| 76 | /* Saved resource information for the beginning of an operation */ |
| 77 | static struct rusage sBegin; |
| 78 | |
| 79 | /* True if the timer is enabled */ |
| 80 | static int enableTimer = 0; |
| 81 | |
| 82 | /* |
| 83 | ** Begin timing an operation |
| 84 | */ |
| 85 | static void beginTimer(void){ |
| 86 | if( enableTimer ){ |
| 87 | getrusage(RUSAGE_SELF, &sBegin); |
| 88 | } |
| 89 | } |
| 90 | |
drh | f460809 | 2008-07-11 17:23:24 +0000 | [diff] [blame] | 91 | /* Return the difference of two time_structs in seconds */ |
| 92 | static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ |
| 93 | return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + |
| 94 | (double)(pEnd->tv_sec - pStart->tv_sec); |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
| 98 | ** Print the timing results. |
| 99 | */ |
| 100 | static void endTimer(void){ |
| 101 | if( enableTimer ){ |
| 102 | struct rusage sEnd; |
| 103 | getrusage(RUSAGE_SELF, &sEnd); |
| 104 | printf("CPU Time: user %f sys %f\n", |
drh | f460809 | 2008-07-11 17:23:24 +0000 | [diff] [blame] | 105 | timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), |
| 106 | timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | #define BEGIN_TIMER beginTimer() |
| 110 | #define END_TIMER endTimer() |
| 111 | #define HAS_TIMER 1 |
| 112 | #else |
| 113 | #define BEGIN_TIMER |
| 114 | #define END_TIMER |
| 115 | #define HAS_TIMER 0 |
| 116 | #endif |
| 117 | |
drh | e91d16b | 2008-12-08 18:27:31 +0000 | [diff] [blame] | 118 | /* |
| 119 | ** Used to prevent warnings about unused parameters |
| 120 | */ |
| 121 | #define UNUSED_PARAMETER(x) (void)(x) |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 122 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 123 | /* |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 124 | ** If the following flag is set, then command execution stops |
| 125 | ** at an error if we are not interactive. |
| 126 | */ |
| 127 | static int bail_on_error = 0; |
| 128 | |
| 129 | /* |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 130 | ** Threat stdin as an interactive input if the following variable |
| 131 | ** is true. Otherwise, assume stdin is connected to a file or pipe. |
| 132 | */ |
| 133 | static int stdin_is_interactive = 1; |
| 134 | |
| 135 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 136 | ** The following is the open SQLite database. We make a pointer |
| 137 | ** to this database a static variable so that it can be accessed |
| 138 | ** by the SIGINT handler to interrupt database processing. |
| 139 | */ |
danielk1977 | 92f9a1b | 2004-06-19 09:08:16 +0000 | [diff] [blame] | 140 | static sqlite3 *db = 0; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 141 | |
| 142 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 143 | ** True if an interrupt (Control-C) has been received. |
| 144 | */ |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 145 | static volatile int seenInterrupt = 0; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 146 | |
| 147 | /* |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 148 | ** This is the name of our program. It is set in main(), used |
| 149 | ** in a number of other places, mostly for error messages. |
| 150 | */ |
| 151 | static char *Argv0; |
| 152 | |
| 153 | /* |
| 154 | ** Prompt strings. Initialized in main. Settable with |
| 155 | ** .prompt main continue |
| 156 | */ |
| 157 | static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ |
| 158 | static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ |
| 159 | |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 160 | /* |
| 161 | ** Write I/O traces to the following stream. |
| 162 | */ |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 163 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 164 | static FILE *iotrace = 0; |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 165 | #endif |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | ** This routine works like printf in that its first argument is a |
| 169 | ** format string and subsequent arguments are values to be substituted |
| 170 | ** in place of % fields. The result of formatting this string |
| 171 | ** is written to iotrace. |
| 172 | */ |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 173 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 174 | static void iotracePrintf(const char *zFormat, ...){ |
| 175 | va_list ap; |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 176 | char *z; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 177 | if( iotrace==0 ) return; |
| 178 | va_start(ap, zFormat); |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 179 | z = sqlite3_vmprintf(zFormat, ap); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 180 | va_end(ap); |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 181 | fprintf(iotrace, "%s", z); |
| 182 | sqlite3_free(z); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 183 | } |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 184 | #endif |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 185 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 186 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 187 | /* |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 188 | ** Determines if a string is a number of not. |
| 189 | */ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 190 | static int isNumber(const char *z, int *realnum){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 191 | if( *z=='-' || *z=='+' ) z++; |
| 192 | if( !isdigit(*z) ){ |
| 193 | return 0; |
| 194 | } |
| 195 | z++; |
| 196 | if( realnum ) *realnum = 0; |
| 197 | while( isdigit(*z) ){ z++; } |
| 198 | if( *z=='.' ){ |
| 199 | z++; |
| 200 | if( !isdigit(*z) ) return 0; |
| 201 | while( isdigit(*z) ){ z++; } |
| 202 | if( realnum ) *realnum = 1; |
| 203 | } |
| 204 | if( *z=='e' || *z=='E' ){ |
| 205 | z++; |
| 206 | if( *z=='+' || *z=='-' ) z++; |
| 207 | if( !isdigit(*z) ) return 0; |
| 208 | while( isdigit(*z) ){ z++; } |
| 209 | if( realnum ) *realnum = 1; |
| 210 | } |
| 211 | return *z==0; |
| 212 | } |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 213 | |
| 214 | /* |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 215 | ** A global char* and an SQL function to access its current value |
| 216 | ** from within an SQL statement. This program used to use the |
| 217 | ** sqlite_exec_printf() API to substitue a string into an SQL statement. |
| 218 | ** The correct way to do this with sqlite3 is to use the bind API, but |
| 219 | ** since the shell is built around the callback paradigm it would be a lot |
| 220 | ** of work. Instead just use this hack, which is quite harmless. |
| 221 | */ |
| 222 | static const char *zShellStatic = 0; |
| 223 | static void shellstaticFunc( |
| 224 | sqlite3_context *context, |
| 225 | int argc, |
| 226 | sqlite3_value **argv |
| 227 | ){ |
| 228 | assert( 0==argc ); |
| 229 | assert( zShellStatic ); |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 230 | UNUSED_PARAMETER(argv); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 231 | sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 236 | ** This routine reads a line of text from FILE in, stores |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 237 | ** the text in memory obtained from malloc() and returns a pointer |
| 238 | ** to the text. NULL is returned at end of file, or if malloc() |
| 239 | ** fails. |
| 240 | ** |
| 241 | ** The interface is like "readline" but no command-line editing |
| 242 | ** is done. |
| 243 | */ |
drh | 9347b20 | 2003-07-18 01:30:59 +0000 | [diff] [blame] | 244 | static char *local_getline(char *zPrompt, FILE *in){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 245 | char *zLine; |
| 246 | int nLine; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 247 | int n; |
| 248 | int eol; |
| 249 | |
| 250 | if( zPrompt && *zPrompt ){ |
| 251 | printf("%s",zPrompt); |
| 252 | fflush(stdout); |
| 253 | } |
| 254 | nLine = 100; |
| 255 | zLine = malloc( nLine ); |
| 256 | if( zLine==0 ) return 0; |
| 257 | n = 0; |
| 258 | eol = 0; |
| 259 | while( !eol ){ |
| 260 | if( n+100>nLine ){ |
| 261 | nLine = nLine*2 + 100; |
| 262 | zLine = realloc(zLine, nLine); |
| 263 | if( zLine==0 ) return 0; |
| 264 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 265 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 266 | if( n==0 ){ |
| 267 | free(zLine); |
| 268 | return 0; |
| 269 | } |
| 270 | zLine[n] = 0; |
| 271 | eol = 1; |
| 272 | break; |
| 273 | } |
| 274 | while( zLine[n] ){ n++; } |
| 275 | if( n>0 && zLine[n-1]=='\n' ){ |
| 276 | n--; |
| 277 | zLine[n] = 0; |
| 278 | eol = 1; |
| 279 | } |
| 280 | } |
| 281 | zLine = realloc( zLine, n+1 ); |
| 282 | return zLine; |
| 283 | } |
| 284 | |
| 285 | /* |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 286 | ** Retrieve a single line of input text. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 287 | ** |
| 288 | ** zPrior is a string of prior text retrieved. If not the empty |
| 289 | ** string, then issue a continuation prompt. |
| 290 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 291 | static char *one_input_line(const char *zPrior, FILE *in){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 292 | char *zPrompt; |
| 293 | char *zResult; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 294 | if( in!=0 ){ |
drh | 9347b20 | 2003-07-18 01:30:59 +0000 | [diff] [blame] | 295 | return local_getline(0, in); |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 296 | } |
| 297 | if( zPrior && zPrior[0] ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 298 | zPrompt = continuePrompt; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 299 | }else{ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 300 | zPrompt = mainPrompt; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 301 | } |
| 302 | zResult = readline(zPrompt); |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 303 | #if defined(HAVE_READLINE) && HAVE_READLINE==1 |
drh | eb741d5 | 2006-06-03 17:37:25 +0000 | [diff] [blame] | 304 | if( zResult && *zResult ) add_history(zResult); |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 305 | #endif |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 306 | return zResult; |
| 307 | } |
| 308 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 309 | struct previous_mode_data { |
| 310 | int valid; /* Is there legit data in here? */ |
| 311 | int mode; |
| 312 | int showHeader; |
| 313 | int colWidth[100]; |
| 314 | }; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 315 | |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 316 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 317 | ** An pointer to an instance of this structure is passed from |
| 318 | ** the main program to the callback. This is used to communicate |
| 319 | ** state and mode information. |
| 320 | */ |
| 321 | struct callback_data { |
danielk1977 | 92f9a1b | 2004-06-19 09:08:16 +0000 | [diff] [blame] | 322 | sqlite3 *db; /* The database */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 323 | int echoOn; /* True to echo input commands */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 324 | int cnt; /* Number of records displayed so far */ |
| 325 | FILE *out; /* Write results here */ |
| 326 | int mode; /* An output mode setting */ |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 327 | int writableSchema; /* True if PRAGMA writable_schema=ON */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 328 | int showHeader; /* True to show column names in List or Column mode */ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 329 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 330 | char separator[20]; /* Separator character for MODE_List */ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 331 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 332 | int actualWidth[100]; /* Actual width of each column */ |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 333 | char nullvalue[20]; /* The text to print when a NULL comes back from |
| 334 | ** the database */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 335 | struct previous_mode_data explainPrev; |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 336 | /* Holds the mode information just before |
| 337 | ** .explain ON */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 338 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
| 339 | const char *zDbFilename; /* name of the database file */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 340 | }; |
| 341 | |
| 342 | /* |
| 343 | ** These are the allowed modes. |
| 344 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 345 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 346 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 347 | #define MODE_List 2 /* One record per line with a separator */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 348 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 349 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 350 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 351 | #define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */ |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 352 | #define MODE_Csv 7 /* Quote strings, numbers are plain */ |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 353 | #define MODE_Explain 8 /* Like MODE_Column, but do not truncate data */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 354 | |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 355 | static const char *modeDescr[] = { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 356 | "line", |
| 357 | "column", |
| 358 | "list", |
| 359 | "semi", |
| 360 | "html", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 361 | "insert", |
| 362 | "tcl", |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 363 | "csv", |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 364 | "explain", |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 365 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 366 | |
| 367 | /* |
| 368 | ** Number of elements in an array |
| 369 | */ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 370 | #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 371 | |
| 372 | /* |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 373 | ** Compute a string length that is limited to what can be stored in |
| 374 | ** lower 30 bits of a 32-bit signed integer. |
| 375 | */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 376 | static int strlen30(const char *z){ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 377 | const char *z2 = z; |
| 378 | while( *z2 ){ z2++; } |
| 379 | return 0x3fffffff & (int)(z2 - z); |
| 380 | } |
| 381 | |
| 382 | /* |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 383 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 384 | */ |
| 385 | static void output_quoted_string(FILE *out, const char *z){ |
| 386 | int i; |
| 387 | int nSingle = 0; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 388 | for(i=0; z[i]; i++){ |
| 389 | if( z[i]=='\'' ) nSingle++; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 390 | } |
| 391 | if( nSingle==0 ){ |
| 392 | fprintf(out,"'%s'",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 393 | }else{ |
| 394 | fprintf(out,"'"); |
| 395 | while( *z ){ |
| 396 | for(i=0; z[i] && z[i]!='\''; i++){} |
| 397 | if( i==0 ){ |
| 398 | fprintf(out,"''"); |
| 399 | z++; |
| 400 | }else if( z[i]=='\'' ){ |
| 401 | fprintf(out,"%.*s''",i,z); |
| 402 | z += i+1; |
| 403 | }else{ |
drh | cd7d273 | 2002-02-26 23:24:26 +0000 | [diff] [blame] | 404 | fprintf(out,"%s",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 405 | break; |
| 406 | } |
| 407 | } |
drh | cd7d273 | 2002-02-26 23:24:26 +0000 | [diff] [blame] | 408 | fprintf(out,"'"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 413 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 414 | */ |
| 415 | static void output_c_string(FILE *out, const char *z){ |
| 416 | unsigned int c; |
| 417 | fputc('"', out); |
| 418 | while( (c = *(z++))!=0 ){ |
| 419 | if( c=='\\' ){ |
| 420 | fputc(c, out); |
| 421 | fputc(c, out); |
| 422 | }else if( c=='\t' ){ |
| 423 | fputc('\\', out); |
| 424 | fputc('t', out); |
| 425 | }else if( c=='\n' ){ |
| 426 | fputc('\\', out); |
| 427 | fputc('n', out); |
| 428 | }else if( c=='\r' ){ |
| 429 | fputc('\\', out); |
| 430 | fputc('r', out); |
| 431 | }else if( !isprint(c) ){ |
drh | 0a8640d | 2005-08-30 20:12:02 +0000 | [diff] [blame] | 432 | fprintf(out, "\\%03o", c&0xff); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 433 | }else{ |
| 434 | fputc(c, out); |
| 435 | } |
| 436 | } |
| 437 | fputc('"', out); |
| 438 | } |
| 439 | |
| 440 | /* |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 441 | ** Output the given string with characters that are special to |
| 442 | ** HTML escaped. |
| 443 | */ |
| 444 | static void output_html_string(FILE *out, const char *z){ |
| 445 | int i; |
| 446 | while( *z ){ |
| 447 | for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){} |
| 448 | if( i>0 ){ |
| 449 | fprintf(out,"%.*s",i,z); |
| 450 | } |
| 451 | if( z[i]=='<' ){ |
| 452 | fprintf(out,"<"); |
| 453 | }else if( z[i]=='&' ){ |
| 454 | fprintf(out,"&"); |
| 455 | }else{ |
| 456 | break; |
| 457 | } |
| 458 | z += i + 1; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /* |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 463 | ** If a field contains any character identified by a 1 in the following |
| 464 | ** array, then the string must be quoted for CSV. |
| 465 | */ |
| 466 | static const char needCsvQuote[] = { |
| 467 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 468 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 469 | 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 470 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 471 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 472 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 473 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 474 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 475 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 476 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 477 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 478 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 479 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 480 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 481 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 482 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 483 | }; |
| 484 | |
| 485 | /* |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 486 | ** Output a single term of CSV. Actually, p->separator is used for |
| 487 | ** the separator, which may or may not be a comma. p->nullvalue is |
| 488 | ** the null value. Strings are quoted using ANSI-C rules. Numbers |
| 489 | ** appear outside of quotes. |
| 490 | */ |
| 491 | static void output_csv(struct callback_data *p, const char *z, int bSep){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 492 | FILE *out = p->out; |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 493 | if( z==0 ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 494 | fprintf(out,"%s",p->nullvalue); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 495 | }else{ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 496 | int i; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 497 | int nSep = strlen30(p->separator); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 498 | for(i=0; z[i]; i++){ |
drh | c85375d | 2007-12-18 15:41:44 +0000 | [diff] [blame] | 499 | if( needCsvQuote[((unsigned char*)z)[i]] |
| 500 | || (z[i]==p->separator[0] && |
| 501 | (nSep==1 || memcmp(z, p->separator, nSep)==0)) ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 502 | i = 0; |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | if( i==0 ){ |
| 507 | putc('"', out); |
| 508 | for(i=0; z[i]; i++){ |
| 509 | if( z[i]=='"' ) putc('"', out); |
| 510 | putc(z[i], out); |
| 511 | } |
| 512 | putc('"', out); |
| 513 | }else{ |
| 514 | fprintf(out, "%s", z); |
| 515 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 516 | } |
| 517 | if( bSep ){ |
drh | d0e7788 | 2008-01-14 15:20:08 +0000 | [diff] [blame] | 518 | fprintf(p->out, "%s", p->separator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 522 | #ifdef SIGINT |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 523 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 524 | ** This routine runs when the user presses Ctrl-C |
| 525 | */ |
| 526 | static void interrupt_handler(int NotUsed){ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 527 | UNUSED_PARAMETER(NotUsed); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 528 | seenInterrupt = 1; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 529 | if( db ) sqlite3_interrupt(db); |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 530 | } |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 531 | #endif |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 532 | |
| 533 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 534 | ** This is the callback routine that the SQLite library |
| 535 | ** invokes for each row of a query result. |
| 536 | */ |
| 537 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 538 | int i; |
| 539 | struct callback_data *p = (struct callback_data*)pArg; |
| 540 | switch( p->mode ){ |
| 541 | case MODE_Line: { |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 542 | int w = 5; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 543 | if( azArg==0 ) break; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 544 | for(i=0; i<nArg; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 545 | int len = strlen30(azCol[i] ? azCol[i] : ""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 546 | if( len>w ) w = len; |
| 547 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 548 | if( p->cnt++>0 ) fprintf(p->out,"\n"); |
| 549 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 550 | fprintf(p->out,"%*s = %s\n", w, azCol[i], |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 551 | azArg[i] ? azArg[i] : p->nullvalue); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 552 | } |
| 553 | break; |
| 554 | } |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 555 | case MODE_Explain: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 556 | case MODE_Column: { |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 557 | if( p->cnt++==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 558 | for(i=0; i<nArg; i++){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 559 | int w, n; |
| 560 | if( i<ArraySize(p->colWidth) ){ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 561 | w = p->colWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 562 | }else{ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 563 | w = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 564 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 565 | if( w<=0 ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 566 | w = strlen30(azCol[i] ? azCol[i] : ""); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 567 | if( w<10 ) w = 10; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 568 | n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 569 | if( w<n ) w = n; |
| 570 | } |
| 571 | if( i<ArraySize(p->actualWidth) ){ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 572 | p->actualWidth[i] = w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 573 | } |
| 574 | if( p->showHeader ){ |
| 575 | fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " "); |
| 576 | } |
| 577 | } |
| 578 | if( p->showHeader ){ |
| 579 | for(i=0; i<nArg; i++){ |
| 580 | int w; |
| 581 | if( i<ArraySize(p->actualWidth) ){ |
| 582 | w = p->actualWidth[i]; |
| 583 | }else{ |
| 584 | w = 10; |
| 585 | } |
| 586 | fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------" |
| 587 | "----------------------------------------------------------", |
| 588 | i==nArg-1 ? "\n": " "); |
| 589 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 592 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 593 | for(i=0; i<nArg; i++){ |
| 594 | int w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 595 | if( i<ArraySize(p->actualWidth) ){ |
| 596 | w = p->actualWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 597 | }else{ |
| 598 | w = 10; |
| 599 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 600 | if( p->mode==MODE_Explain && azArg[i] && |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 601 | strlen30(azArg[i])>w ){ |
| 602 | w = strlen30(azArg[i]); |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 603 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 604 | fprintf(p->out,"%-*.*s%s",w,w, |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 605 | azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " "); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 606 | } |
| 607 | break; |
| 608 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 609 | case MODE_Semi: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 610 | case MODE_List: { |
| 611 | if( p->cnt++==0 && p->showHeader ){ |
| 612 | for(i=0; i<nArg; i++){ |
| 613 | fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator); |
| 614 | } |
| 615 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 616 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 617 | for(i=0; i<nArg; i++){ |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 618 | char *z = azArg[i]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 619 | if( z==0 ) z = p->nullvalue; |
drh | 71172c5 | 2002-01-24 00:00:21 +0000 | [diff] [blame] | 620 | fprintf(p->out, "%s", z); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 621 | if( i<nArg-1 ){ |
| 622 | fprintf(p->out, "%s", p->separator); |
| 623 | }else if( p->mode==MODE_Semi ){ |
| 624 | fprintf(p->out, ";\n"); |
| 625 | }else{ |
| 626 | fprintf(p->out, "\n"); |
| 627 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 628 | } |
| 629 | break; |
| 630 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 631 | case MODE_Html: { |
| 632 | if( p->cnt++==0 && p->showHeader ){ |
mihailim | 57c591a | 2008-06-23 21:26:05 +0000 | [diff] [blame] | 633 | fprintf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 634 | for(i=0; i<nArg; i++){ |
mihailim | 57c591a | 2008-06-23 21:26:05 +0000 | [diff] [blame] | 635 | fprintf(p->out,"<TH>%s</TH>",azCol[i]); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 636 | } |
mihailim | 57c591a | 2008-06-23 21:26:05 +0000 | [diff] [blame] | 637 | fprintf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 638 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 639 | if( azArg==0 ) break; |
mihailim | 57c591a | 2008-06-23 21:26:05 +0000 | [diff] [blame] | 640 | fprintf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 641 | for(i=0; i<nArg; i++){ |
mihailim | 57c591a | 2008-06-23 21:26:05 +0000 | [diff] [blame] | 642 | fprintf(p->out,"<TD>"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 643 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue); |
mihailim | 57c591a | 2008-06-23 21:26:05 +0000 | [diff] [blame] | 644 | fprintf(p->out,"</TD>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 645 | } |
mihailim | 57c591a | 2008-06-23 21:26:05 +0000 | [diff] [blame] | 646 | fprintf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 647 | break; |
| 648 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 649 | case MODE_Tcl: { |
| 650 | if( p->cnt++==0 && p->showHeader ){ |
| 651 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 652 | output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 653 | fprintf(p->out, "%s", p->separator); |
| 654 | } |
| 655 | fprintf(p->out,"\n"); |
| 656 | } |
| 657 | if( azArg==0 ) break; |
| 658 | for(i=0; i<nArg; i++){ |
| 659 | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue); |
| 660 | fprintf(p->out, "%s", p->separator); |
| 661 | } |
| 662 | fprintf(p->out,"\n"); |
| 663 | break; |
| 664 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 665 | case MODE_Csv: { |
| 666 | if( p->cnt++==0 && p->showHeader ){ |
| 667 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 668 | output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 669 | } |
| 670 | fprintf(p->out,"\n"); |
| 671 | } |
| 672 | if( azArg==0 ) break; |
| 673 | for(i=0; i<nArg; i++){ |
| 674 | output_csv(p, azArg[i], i<nArg-1); |
| 675 | } |
| 676 | fprintf(p->out,"\n"); |
| 677 | break; |
| 678 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 679 | case MODE_Insert: { |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 680 | if( azArg==0 ) break; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 681 | fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 682 | for(i=0; i<nArg; i++){ |
| 683 | char *zSep = i>0 ? ",": ""; |
| 684 | if( azArg[i]==0 ){ |
| 685 | fprintf(p->out,"%sNULL",zSep); |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 686 | }else if( isNumber(azArg[i], 0) ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 687 | fprintf(p->out,"%s%s",zSep, azArg[i]); |
| 688 | }else{ |
| 689 | if( zSep[0] ) fprintf(p->out,"%s",zSep); |
| 690 | output_quoted_string(p->out, azArg[i]); |
| 691 | } |
| 692 | } |
| 693 | fprintf(p->out,");\n"); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 694 | break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 695 | } |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 696 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | /* |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 701 | ** Set the destination table field of the callback_data structure to |
| 702 | ** the name of the table given. Escape any quote characters in the |
| 703 | ** table name. |
| 704 | */ |
| 705 | static void set_table_name(struct callback_data *p, const char *zName){ |
| 706 | int i, n; |
| 707 | int needQuote; |
| 708 | char *z; |
| 709 | |
| 710 | if( p->zDestTable ){ |
| 711 | free(p->zDestTable); |
| 712 | p->zDestTable = 0; |
| 713 | } |
| 714 | if( zName==0 ) return; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 715 | needQuote = !isalpha((unsigned char)*zName) && *zName!='_'; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 716 | for(i=n=0; zName[i]; i++, n++){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 717 | if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 718 | needQuote = 1; |
| 719 | if( zName[i]=='\'' ) n++; |
| 720 | } |
| 721 | } |
| 722 | if( needQuote ) n += 2; |
| 723 | z = p->zDestTable = malloc( n+1 ); |
| 724 | if( z==0 ){ |
| 725 | fprintf(stderr,"Out of memory!\n"); |
| 726 | exit(1); |
| 727 | } |
| 728 | n = 0; |
| 729 | if( needQuote ) z[n++] = '\''; |
| 730 | for(i=0; zName[i]; i++){ |
| 731 | z[n++] = zName[i]; |
| 732 | if( zName[i]=='\'' ) z[n++] = '\''; |
| 733 | } |
| 734 | if( needQuote ) z[n++] = '\''; |
| 735 | z[n] = 0; |
| 736 | } |
| 737 | |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 738 | /* zIn is either a pointer to a NULL-terminated string in memory obtained |
| 739 | ** from malloc(), or a NULL pointer. The string pointed to by zAppend is |
| 740 | ** added to zIn, and the result returned in memory obtained from malloc(). |
| 741 | ** zIn, if it was not NULL, is freed. |
| 742 | ** |
| 743 | ** If the third argument, quote, is not '\0', then it is used as a |
| 744 | ** quote character for zAppend. |
| 745 | */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 746 | static char *appendText(char *zIn, char const *zAppend, char quote){ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 747 | int len; |
| 748 | int i; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 749 | int nAppend = strlen30(zAppend); |
| 750 | int nIn = (zIn?strlen30(zIn):0); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 751 | |
| 752 | len = nAppend+nIn+1; |
| 753 | if( quote ){ |
| 754 | len += 2; |
| 755 | for(i=0; i<nAppend; i++){ |
| 756 | if( zAppend[i]==quote ) len++; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | zIn = (char *)realloc(zIn, len); |
| 761 | if( !zIn ){ |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | if( quote ){ |
| 766 | char *zCsr = &zIn[nIn]; |
| 767 | *zCsr++ = quote; |
| 768 | for(i=0; i<nAppend; i++){ |
| 769 | *zCsr++ = zAppend[i]; |
| 770 | if( zAppend[i]==quote ) *zCsr++ = quote; |
| 771 | } |
| 772 | *zCsr++ = quote; |
| 773 | *zCsr++ = '\0'; |
| 774 | assert( (zCsr-zIn)==len ); |
| 775 | }else{ |
| 776 | memcpy(&zIn[nIn], zAppend, nAppend); |
| 777 | zIn[len-1] = '\0'; |
| 778 | } |
| 779 | |
| 780 | return zIn; |
| 781 | } |
| 782 | |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 783 | |
| 784 | /* |
| 785 | ** Execute a query statement that has a single result column. Print |
| 786 | ** that result column on a line by itself with a semicolon terminator. |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 787 | ** |
| 788 | ** This is used, for example, to show the schema of the database by |
| 789 | ** querying the SQLITE_MASTER table. |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 790 | */ |
| 791 | static int run_table_dump_query(FILE *out, sqlite3 *db, const char *zSelect){ |
| 792 | sqlite3_stmt *pSelect; |
| 793 | int rc; |
| 794 | rc = sqlite3_prepare(db, zSelect, -1, &pSelect, 0); |
| 795 | if( rc!=SQLITE_OK || !pSelect ){ |
| 796 | return rc; |
| 797 | } |
| 798 | rc = sqlite3_step(pSelect); |
| 799 | while( rc==SQLITE_ROW ){ |
| 800 | fprintf(out, "%s;\n", sqlite3_column_text(pSelect, 0)); |
| 801 | rc = sqlite3_step(pSelect); |
| 802 | } |
| 803 | return sqlite3_finalize(pSelect); |
| 804 | } |
| 805 | |
| 806 | |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 807 | /* |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 808 | ** This is a different callback routine used for dumping the database. |
| 809 | ** Each row received by this callback consists of a table name, |
| 810 | ** the table type ("index" or "table") and SQL to create the table. |
| 811 | ** This routine should print text sufficient to recreate the table. |
| 812 | */ |
| 813 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 814 | int rc; |
| 815 | const char *zTable; |
| 816 | const char *zType; |
| 817 | const char *zSql; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 818 | struct callback_data *p = (struct callback_data *)pArg; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 819 | |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 820 | UNUSED_PARAMETER(azCol); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 821 | if( nArg!=3 ) return 1; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 822 | zTable = azArg[0]; |
| 823 | zType = azArg[1]; |
| 824 | zSql = azArg[2]; |
| 825 | |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 826 | if( strcmp(zTable, "sqlite_sequence")==0 ){ |
drh | f8eb96a | 2005-02-03 00:42:34 +0000 | [diff] [blame] | 827 | fprintf(p->out, "DELETE FROM sqlite_sequence;\n"); |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 828 | }else if( strcmp(zTable, "sqlite_stat1")==0 ){ |
| 829 | fprintf(p->out, "ANALYZE sqlite_master;\n"); |
| 830 | }else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
| 831 | return 0; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 832 | }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
| 833 | char *zIns; |
| 834 | if( !p->writableSchema ){ |
| 835 | fprintf(p->out, "PRAGMA writable_schema=ON;\n"); |
| 836 | p->writableSchema = 1; |
| 837 | } |
| 838 | zIns = sqlite3_mprintf( |
| 839 | "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" |
| 840 | "VALUES('table','%q','%q',0,'%q');", |
| 841 | zTable, zTable, zSql); |
| 842 | fprintf(p->out, "%s\n", zIns); |
| 843 | sqlite3_free(zIns); |
| 844 | return 0; |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 845 | }else{ |
| 846 | fprintf(p->out, "%s;\n", zSql); |
drh | f8eb96a | 2005-02-03 00:42:34 +0000 | [diff] [blame] | 847 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 848 | |
| 849 | if( strcmp(zType, "table")==0 ){ |
| 850 | sqlite3_stmt *pTableInfo = 0; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 851 | char *zSelect = 0; |
| 852 | char *zTableInfo = 0; |
| 853 | char *zTmp = 0; |
| 854 | |
| 855 | zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0); |
| 856 | zTableInfo = appendText(zTableInfo, zTable, '"'); |
| 857 | zTableInfo = appendText(zTableInfo, ");", 0); |
| 858 | |
| 859 | rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0); |
| 860 | if( zTableInfo ) free(zTableInfo); |
| 861 | if( rc!=SQLITE_OK || !pTableInfo ){ |
| 862 | return 1; |
| 863 | } |
| 864 | |
| 865 | zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0); |
| 866 | zTmp = appendText(zTmp, zTable, '"'); |
| 867 | if( zTmp ){ |
| 868 | zSelect = appendText(zSelect, zTmp, '\''); |
| 869 | } |
| 870 | zSelect = appendText(zSelect, " || ' VALUES(' || ", 0); |
| 871 | rc = sqlite3_step(pTableInfo); |
| 872 | while( rc==SQLITE_ROW ){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 873 | const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1); |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 874 | zSelect = appendText(zSelect, "quote(", 0); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 875 | zSelect = appendText(zSelect, zText, '"'); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 876 | rc = sqlite3_step(pTableInfo); |
| 877 | if( rc==SQLITE_ROW ){ |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 878 | zSelect = appendText(zSelect, ") || ',' || ", 0); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 879 | }else{ |
| 880 | zSelect = appendText(zSelect, ") ", 0); |
| 881 | } |
| 882 | } |
| 883 | rc = sqlite3_finalize(pTableInfo); |
| 884 | if( rc!=SQLITE_OK ){ |
| 885 | if( zSelect ) free(zSelect); |
| 886 | return 1; |
| 887 | } |
| 888 | zSelect = appendText(zSelect, "|| ')' FROM ", 0); |
| 889 | zSelect = appendText(zSelect, zTable, '"'); |
| 890 | |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 891 | rc = run_table_dump_query(p->out, p->db, zSelect); |
| 892 | if( rc==SQLITE_CORRUPT ){ |
| 893 | zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0); |
| 894 | rc = run_table_dump_query(p->out, p->db, zSelect); |
| 895 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 896 | if( zSelect ) free(zSelect); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 897 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | /* |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 902 | ** Run zQuery. Use dump_callback() as the callback routine so that |
| 903 | ** the contents of the query are output as SQL statements. |
| 904 | ** |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 905 | ** If we get a SQLITE_CORRUPT error, rerun the query after appending |
| 906 | ** "ORDER BY rowid DESC" to the end. |
| 907 | */ |
| 908 | static int run_schema_dump_query( |
| 909 | struct callback_data *p, |
| 910 | const char *zQuery, |
| 911 | char **pzErrMsg |
| 912 | ){ |
| 913 | int rc; |
| 914 | rc = sqlite3_exec(p->db, zQuery, dump_callback, p, pzErrMsg); |
| 915 | if( rc==SQLITE_CORRUPT ){ |
| 916 | char *zQ2; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 917 | int len = strlen30(zQuery); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 918 | if( pzErrMsg ) sqlite3_free(*pzErrMsg); |
| 919 | zQ2 = malloc( len+100 ); |
| 920 | if( zQ2==0 ) return rc; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 921 | sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 922 | rc = sqlite3_exec(p->db, zQ2, dump_callback, p, pzErrMsg); |
| 923 | free(zQ2); |
| 924 | } |
| 925 | return rc; |
| 926 | } |
| 927 | |
| 928 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 929 | ** Text of a help message |
| 930 | */ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 931 | static char zHelp[] = |
drh | 20f99c4 | 2007-01-08 14:31:35 +0000 | [diff] [blame] | 932 | ".bail ON|OFF Stop after hitting an error. Default OFF\n" |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 933 | ".databases List names and files of attached databases\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 934 | ".dump ?TABLE? ... Dump the database in an SQL text format\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 935 | ".echo ON|OFF Turn command echo on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 936 | ".exit Exit this program\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 937 | ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 938 | ".header(s) ON|OFF Turn display of headers on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 939 | ".help Show this message\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 940 | ".import FILE TABLE Import data from FILE into TABLE\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 941 | ".indices TABLE Show names of all indices on TABLE\n" |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 942 | #ifdef SQLITE_ENABLE_IOTRACE |
| 943 | ".iotrace FILE Enable I/O diagnostic logging to FILE\n" |
| 944 | #endif |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 945 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 946 | ".load FILE ?ENTRY? Load an extension library\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 947 | #endif |
danielk1977 | 6b77a36 | 2005-01-13 11:10:25 +0000 | [diff] [blame] | 948 | ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" |
drh | 3b584fa | 2004-09-24 12:50:03 +0000 | [diff] [blame] | 949 | " csv Comma-separated values\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 950 | " column Left-aligned columns. (See .width)\n" |
| 951 | " html HTML <table> code\n" |
| 952 | " insert SQL insert statements for TABLE\n" |
| 953 | " line One value per line\n" |
| 954 | " list Values delimited by .separator string\n" |
| 955 | " tabs Tab-separated values\n" |
| 956 | " tcl TCL list elements\n" |
| 957 | ".nullvalue STRING Print STRING in place of NULL values\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 958 | ".output FILENAME Send output to FILENAME\n" |
| 959 | ".output stdout Send output to the screen\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 960 | ".prompt MAIN CONTINUE Replace the standard prompts\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 961 | ".quit Exit this program\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 962 | ".read FILENAME Execute SQL in FILENAME\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 963 | ".schema ?TABLE? Show the CREATE statements\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 964 | ".separator STRING Change separator used by output mode and .import\n" |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 965 | ".show Show the current values for various settings\n" |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 966 | ".tables ?PATTERN? List names of tables matching a LIKE pattern\n" |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 967 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 968 | #if HAS_TIMER |
| 969 | ".timer ON|OFF Turn the CPU timer measurement on or off\n" |
| 970 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 971 | ".width NUM NUM ... Set column widths for \"column\" mode\n" |
| 972 | ; |
| 973 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 974 | /* Forward reference */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 975 | static int process_input(struct callback_data *p, FILE *in); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 976 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 977 | /* |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 978 | ** Make sure the database is open. If it is not, then open it. If |
| 979 | ** the database fails to open, print an error message and exit. |
| 980 | */ |
| 981 | static void open_db(struct callback_data *p){ |
| 982 | if( p->db==0 ){ |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 983 | sqlite3_open(p->zDbFilename, &p->db); |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 984 | db = p->db; |
drh | 4cea5ba | 2008-05-05 16:27:24 +0000 | [diff] [blame] | 985 | if( db && sqlite3_errcode(db)==SQLITE_OK ){ |
| 986 | sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0, |
| 987 | shellstaticFunc, 0, 0); |
| 988 | } |
| 989 | if( db==0 || SQLITE_OK!=sqlite3_errcode(db) ){ |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 990 | fprintf(stderr,"Unable to open database \"%s\": %s\n", |
| 991 | p->zDbFilename, sqlite3_errmsg(db)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 992 | exit(1); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 993 | } |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 994 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 995 | sqlite3_enable_load_extension(p->db, 1); |
| 996 | #endif |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | |
| 1000 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1001 | ** Do C-language style dequoting. |
| 1002 | ** |
| 1003 | ** \t -> tab |
| 1004 | ** \n -> newline |
| 1005 | ** \r -> carriage return |
| 1006 | ** \NNN -> ascii character NNN in octal |
| 1007 | ** \\ -> backslash |
| 1008 | */ |
| 1009 | static void resolve_backslashes(char *z){ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame^] | 1010 | int i, j; |
| 1011 | char c; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1012 | for(i=j=0; (c = z[i])!=0; i++, j++){ |
| 1013 | if( c=='\\' ){ |
| 1014 | c = z[++i]; |
| 1015 | if( c=='n' ){ |
| 1016 | c = '\n'; |
| 1017 | }else if( c=='t' ){ |
| 1018 | c = '\t'; |
| 1019 | }else if( c=='r' ){ |
| 1020 | c = '\r'; |
| 1021 | }else if( c>='0' && c<='7' ){ |
drh | aa81608 | 2005-12-29 12:53:09 +0000 | [diff] [blame] | 1022 | c -= '0'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1023 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 1024 | i++; |
| 1025 | c = (c<<3) + z[i] - '0'; |
| 1026 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 1027 | i++; |
| 1028 | c = (c<<3) + z[i] - '0'; |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | z[j] = c; |
| 1034 | } |
| 1035 | z[j] = 0; |
| 1036 | } |
| 1037 | |
| 1038 | /* |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1039 | ** Interpret zArg as a boolean value. Return either 0 or 1. |
| 1040 | */ |
| 1041 | static int booleanValue(char *zArg){ |
| 1042 | int val = atoi(zArg); |
| 1043 | int j; |
| 1044 | for(j=0; zArg[j]; j++){ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame^] | 1045 | zArg[j] = (char)tolower(zArg[j]); |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1046 | } |
| 1047 | if( strcmp(zArg,"on")==0 ){ |
| 1048 | val = 1; |
| 1049 | }else if( strcmp(zArg,"yes")==0 ){ |
| 1050 | val = 1; |
| 1051 | } |
| 1052 | return val; |
| 1053 | } |
| 1054 | |
| 1055 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1056 | ** If an input line begins with "." then invoke this routine to |
| 1057 | ** process that line. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1058 | ** |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 1059 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1060 | */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1061 | static int do_meta_command(char *zLine, struct callback_data *p){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1062 | int i = 1; |
| 1063 | int nArg = 0; |
| 1064 | int n, c; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1065 | int rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1066 | char *azArg[50]; |
| 1067 | |
| 1068 | /* Parse the input line into tokens. |
| 1069 | */ |
| 1070 | while( zLine[i] && nArg<ArraySize(azArg) ){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 1071 | while( isspace((unsigned char)zLine[i]) ){ i++; } |
drh | 0633368 | 2004-03-09 13:37:45 +0000 | [diff] [blame] | 1072 | if( zLine[i]==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1073 | if( zLine[i]=='\'' || zLine[i]=='"' ){ |
| 1074 | int delim = zLine[i++]; |
| 1075 | azArg[nArg++] = &zLine[i]; |
| 1076 | while( zLine[i] && zLine[i]!=delim ){ i++; } |
| 1077 | if( zLine[i]==delim ){ |
| 1078 | zLine[i++] = 0; |
| 1079 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1080 | if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1081 | }else{ |
| 1082 | azArg[nArg++] = &zLine[i]; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 1083 | while( zLine[i] && !isspace((unsigned char)zLine[i]) ){ i++; } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1084 | if( zLine[i] ) zLine[i++] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1085 | resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | /* Process the input line. |
| 1090 | */ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1091 | if( nArg==0 ) return rc; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1092 | n = strlen30(azArg[0]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1093 | c = azArg[0][0]; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1094 | if( c=='b' && n>1 && strncmp(azArg[0], "bail", n)==0 && nArg>1 ){ |
| 1095 | bail_on_error = booleanValue(azArg[1]); |
| 1096 | }else |
| 1097 | |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 1098 | if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 1099 | struct callback_data data; |
| 1100 | char *zErrMsg = 0; |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 1101 | open_db(p); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 1102 | memcpy(&data, p, sizeof(data)); |
drh | d888544 | 2004-03-17 23:42:12 +0000 | [diff] [blame] | 1103 | data.showHeader = 1; |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 1104 | data.mode = MODE_Column; |
drh | d888544 | 2004-03-17 23:42:12 +0000 | [diff] [blame] | 1105 | data.colWidth[0] = 3; |
| 1106 | data.colWidth[1] = 15; |
| 1107 | data.colWidth[2] = 58; |
drh | 0b2110c | 2004-10-26 00:08:10 +0000 | [diff] [blame] | 1108 | data.cnt = 0; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1109 | sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 1110 | if( zErrMsg ){ |
| 1111 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1112 | sqlite3_free(zErrMsg); |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 1113 | } |
| 1114 | }else |
| 1115 | |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 1116 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
| 1117 | char *zErrMsg = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1118 | open_db(p); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1119 | fprintf(p->out, "BEGIN TRANSACTION;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1120 | p->writableSchema = 0; |
drh | 93f41e5 | 2008-08-11 19:12:34 +0000 | [diff] [blame] | 1121 | sqlite3_exec(p->db, "PRAGMA writable_schema=ON", 0, 0, 0); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 1122 | if( nArg==1 ){ |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1123 | run_schema_dump_query(p, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1124 | "SELECT name, type, sql FROM sqlite_master " |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1125 | "WHERE sql NOT NULL AND type=='table'", 0 |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 1126 | ); |
| 1127 | run_table_dump_query(p->out, p->db, |
| 1128 | "SELECT sql FROM sqlite_master " |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1129 | "WHERE sql NOT NULL AND type IN ('index','trigger','view')" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1130 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 1131 | }else{ |
| 1132 | int i; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1133 | for(i=1; i<nArg; i++){ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1134 | zShellStatic = azArg[i]; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1135 | run_schema_dump_query(p, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1136 | "SELECT name, type, sql FROM sqlite_master " |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1137 | "WHERE tbl_name LIKE shellstatic() AND type=='table'" |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1138 | " AND sql NOT NULL", 0); |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 1139 | run_table_dump_query(p->out, p->db, |
| 1140 | "SELECT sql FROM sqlite_master " |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1141 | "WHERE sql NOT NULL" |
| 1142 | " AND type IN ('index','trigger','view')" |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 1143 | " AND tbl_name LIKE shellstatic()" |
| 1144 | ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1145 | zShellStatic = 0; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 1146 | } |
| 1147 | } |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1148 | if( p->writableSchema ){ |
| 1149 | fprintf(p->out, "PRAGMA writable_schema=OFF;\n"); |
| 1150 | p->writableSchema = 0; |
| 1151 | } |
drh | 93f41e5 | 2008-08-11 19:12:34 +0000 | [diff] [blame] | 1152 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF", 0, 0, 0); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 1153 | if( zErrMsg ){ |
| 1154 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1155 | sqlite3_free(zErrMsg); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1156 | }else{ |
| 1157 | fprintf(p->out, "COMMIT;\n"); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 1158 | } |
| 1159 | }else |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1160 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1161 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1162 | p->echoOn = booleanValue(azArg[1]); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1163 | }else |
| 1164 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1165 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 1166 | rc = 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1167 | }else |
| 1168 | |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 1169 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1170 | int val = nArg>=2 ? booleanValue(azArg[1]) : 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1171 | if(val == 1) { |
| 1172 | if(!p->explainPrev.valid) { |
| 1173 | p->explainPrev.valid = 1; |
| 1174 | p->explainPrev.mode = p->mode; |
| 1175 | p->explainPrev.showHeader = p->showHeader; |
| 1176 | memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth)); |
| 1177 | } |
| 1178 | /* We could put this code under the !p->explainValid |
| 1179 | ** condition so that it does not execute if we are already in |
| 1180 | ** explain mode. However, always executing it allows us an easy |
| 1181 | ** was to reset to explain mode in case the user previously |
| 1182 | ** did an .explain followed by a .width, .mode or .header |
| 1183 | ** command. |
| 1184 | */ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1185 | p->mode = MODE_Explain; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1186 | p->showHeader = 1; |
| 1187 | memset(p->colWidth,0,ArraySize(p->colWidth)); |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1188 | p->colWidth[0] = 4; /* addr */ |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1189 | p->colWidth[1] = 13; /* opcode */ |
| 1190 | p->colWidth[2] = 4; /* P1 */ |
| 1191 | p->colWidth[3] = 4; /* P2 */ |
| 1192 | p->colWidth[4] = 4; /* P3 */ |
| 1193 | p->colWidth[5] = 13; /* P4 */ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1194 | p->colWidth[6] = 2; /* P5 */ |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 1195 | p->colWidth[7] = 13; /* Comment */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1196 | }else if (p->explainPrev.valid) { |
| 1197 | p->explainPrev.valid = 0; |
| 1198 | p->mode = p->explainPrev.mode; |
| 1199 | p->showHeader = p->explainPrev.showHeader; |
| 1200 | memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth)); |
| 1201 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1202 | }else |
| 1203 | |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1204 | if( c=='h' && (strncmp(azArg[0], "header", n)==0 || |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1205 | strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1206 | p->showHeader = booleanValue(azArg[1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1207 | }else |
| 1208 | |
| 1209 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
| 1210 | fprintf(stderr,zHelp); |
| 1211 | }else |
| 1212 | |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1213 | if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg>=3 ){ |
| 1214 | char *zTable = azArg[2]; /* Insert data into this table */ |
| 1215 | char *zFile = azArg[1]; /* The file from which to extract data */ |
| 1216 | sqlite3_stmt *pStmt; /* A statement */ |
| 1217 | int rc; /* Result code */ |
| 1218 | int nCol; /* Number of columns in the table */ |
| 1219 | int nByte; /* Number of bytes in an SQL string */ |
| 1220 | int i, j; /* Loop counters */ |
| 1221 | int nSep; /* Number of bytes in p->separator[] */ |
| 1222 | char *zSql; /* An SQL statement */ |
| 1223 | char *zLine; /* A single line of input from the file */ |
| 1224 | char **azCol; /* zLine[] broken up into columns */ |
| 1225 | char *zCommit; /* How to commit changes */ |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 1226 | FILE *in; /* The input file */ |
| 1227 | int lineno = 0; /* Line number of input file */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1228 | |
drh | a543c82 | 2006-06-08 16:10:14 +0000 | [diff] [blame] | 1229 | open_db(p); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1230 | nSep = strlen30(p->separator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1231 | if( nSep==0 ){ |
| 1232 | fprintf(stderr, "non-null separator required for import\n"); |
| 1233 | return 0; |
| 1234 | } |
| 1235 | zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); |
| 1236 | if( zSql==0 ) return 0; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1237 | nByte = strlen30(zSql); |
drh | 5e6078b | 2006-01-31 19:07:22 +0000 | [diff] [blame] | 1238 | rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1239 | sqlite3_free(zSql); |
| 1240 | if( rc ){ |
| 1241 | fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db)); |
| 1242 | nCol = 0; |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 1243 | rc = 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1244 | }else{ |
| 1245 | nCol = sqlite3_column_count(pStmt); |
| 1246 | } |
| 1247 | sqlite3_finalize(pStmt); |
| 1248 | if( nCol==0 ) return 0; |
| 1249 | zSql = malloc( nByte + 20 + nCol*2 ); |
| 1250 | if( zSql==0 ) return 0; |
| 1251 | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1252 | j = strlen30(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1253 | for(i=1; i<nCol; i++){ |
| 1254 | zSql[j++] = ','; |
| 1255 | zSql[j++] = '?'; |
| 1256 | } |
| 1257 | zSql[j++] = ')'; |
| 1258 | zSql[j] = 0; |
drh | 5e6078b | 2006-01-31 19:07:22 +0000 | [diff] [blame] | 1259 | rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1260 | free(zSql); |
| 1261 | if( rc ){ |
| 1262 | fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db)); |
| 1263 | sqlite3_finalize(pStmt); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 1264 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1265 | } |
| 1266 | in = fopen(zFile, "rb"); |
| 1267 | if( in==0 ){ |
| 1268 | fprintf(stderr, "cannot open file: %s\n", zFile); |
| 1269 | sqlite3_finalize(pStmt); |
| 1270 | return 0; |
| 1271 | } |
| 1272 | azCol = malloc( sizeof(azCol[0])*(nCol+1) ); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 1273 | if( azCol==0 ){ |
| 1274 | fclose(in); |
| 1275 | return 0; |
| 1276 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1277 | sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
| 1278 | zCommit = "COMMIT"; |
| 1279 | while( (zLine = local_getline(0, in))!=0 ){ |
| 1280 | char *z; |
| 1281 | i = 0; |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 1282 | lineno++; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1283 | azCol[0] = zLine; |
drh | 36d4e97 | 2004-10-06 14:39:06 +0000 | [diff] [blame] | 1284 | for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1285 | if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){ |
| 1286 | *z = 0; |
| 1287 | i++; |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 1288 | if( i<nCol ){ |
| 1289 | azCol[i] = &z[nSep]; |
| 1290 | z += nSep-1; |
| 1291 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1292 | } |
| 1293 | } |
drh | 1cd7f83 | 2005-08-05 18:50:51 +0000 | [diff] [blame] | 1294 | *z = 0; |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 1295 | if( i+1!=nCol ){ |
| 1296 | fprintf(stderr,"%s line %d: expected %d columns of data but found %d\n", |
| 1297 | zFile, lineno, nCol, i+1); |
| 1298 | zCommit = "ROLLBACK"; |
drh | 1822eee | 2008-12-04 12:26:00 +0000 | [diff] [blame] | 1299 | free(zLine); |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 1300 | break; |
| 1301 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1302 | for(i=0; i<nCol; i++){ |
| 1303 | sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); |
| 1304 | } |
| 1305 | sqlite3_step(pStmt); |
| 1306 | rc = sqlite3_reset(pStmt); |
| 1307 | free(zLine); |
| 1308 | if( rc!=SQLITE_OK ){ |
| 1309 | fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db)); |
| 1310 | zCommit = "ROLLBACK"; |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 1311 | rc = 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1312 | break; |
| 1313 | } |
| 1314 | } |
| 1315 | free(azCol); |
| 1316 | fclose(in); |
| 1317 | sqlite3_finalize(pStmt); |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 1318 | sqlite3_exec(p->db, zCommit, 0, 0, 0); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1319 | }else |
| 1320 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1321 | if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){ |
| 1322 | struct callback_data data; |
| 1323 | char *zErrMsg = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1324 | open_db(p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1325 | memcpy(&data, p, sizeof(data)); |
| 1326 | data.showHeader = 0; |
| 1327 | data.mode = MODE_List; |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1328 | zShellStatic = azArg[1]; |
| 1329 | sqlite3_exec(p->db, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1330 | "SELECT name FROM sqlite_master " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1331 | "WHERE type='index' AND tbl_name LIKE shellstatic() " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1332 | "UNION ALL " |
| 1333 | "SELECT name FROM sqlite_temp_master " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1334 | "WHERE type='index' AND tbl_name LIKE shellstatic() " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1335 | "ORDER BY 1", |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1336 | callback, &data, &zErrMsg |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1337 | ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1338 | zShellStatic = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1339 | if( zErrMsg ){ |
| 1340 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1341 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1342 | } |
| 1343 | }else |
| 1344 | |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 1345 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1346 | if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1347 | extern void (*sqlite3IoTrace)(const char*, ...); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1348 | if( iotrace && iotrace!=stdout ) fclose(iotrace); |
| 1349 | iotrace = 0; |
| 1350 | if( nArg<2 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1351 | sqlite3IoTrace = 0; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1352 | }else if( strcmp(azArg[1], "-")==0 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1353 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1354 | iotrace = stdout; |
| 1355 | }else{ |
| 1356 | iotrace = fopen(azArg[1], "w"); |
| 1357 | if( iotrace==0 ){ |
| 1358 | fprintf(stderr, "cannot open \"%s\"\n", azArg[1]); |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1359 | sqlite3IoTrace = 0; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1360 | }else{ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1361 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1362 | } |
| 1363 | } |
| 1364 | }else |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 1365 | #endif |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1366 | |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 1367 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 1368 | if( c=='l' && strncmp(azArg[0], "load", n)==0 && nArg>=2 ){ |
| 1369 | const char *zFile, *zProc; |
| 1370 | char *zErrMsg = 0; |
| 1371 | int rc; |
| 1372 | zFile = azArg[1]; |
| 1373 | zProc = nArg>=3 ? azArg[2] : 0; |
| 1374 | open_db(p); |
| 1375 | rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
| 1376 | if( rc!=SQLITE_OK ){ |
| 1377 | fprintf(stderr, "%s\n", zErrMsg); |
| 1378 | sqlite3_free(zErrMsg); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 1379 | rc = 1; |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 1380 | } |
| 1381 | }else |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 1382 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 1383 | |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1384 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1385 | int n2 = strlen30(azArg[1]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1386 | if( strncmp(azArg[1],"line",n2)==0 |
| 1387 | || |
| 1388 | strncmp(azArg[1],"lines",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1389 | p->mode = MODE_Line; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1390 | }else if( strncmp(azArg[1],"column",n2)==0 |
| 1391 | || |
| 1392 | strncmp(azArg[1],"columns",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1393 | p->mode = MODE_Column; |
| 1394 | }else if( strncmp(azArg[1],"list",n2)==0 ){ |
| 1395 | p->mode = MODE_List; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1396 | }else if( strncmp(azArg[1],"html",n2)==0 ){ |
| 1397 | p->mode = MODE_Html; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1398 | }else if( strncmp(azArg[1],"tcl",n2)==0 ){ |
| 1399 | p->mode = MODE_Tcl; |
| 1400 | }else if( strncmp(azArg[1],"csv",n2)==0 ){ |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1401 | p->mode = MODE_Csv; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1402 | sqlite3_snprintf(sizeof(p->separator), p->separator, ","); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1403 | }else if( strncmp(azArg[1],"tabs",n2)==0 ){ |
| 1404 | p->mode = MODE_List; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1405 | sqlite3_snprintf(sizeof(p->separator), p->separator, "\t"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1406 | }else if( strncmp(azArg[1],"insert",n2)==0 ){ |
| 1407 | p->mode = MODE_Insert; |
| 1408 | if( nArg>=3 ){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1409 | set_table_name(p, azArg[2]); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1410 | }else{ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1411 | set_table_name(p, "table"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1412 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1413 | }else { |
drh | cf68ae9 | 2006-12-19 18:47:41 +0000 | [diff] [blame] | 1414 | fprintf(stderr,"mode should be one of: " |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1415 | "column csv html insert line list tabs tcl\n"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1416 | } |
| 1417 | }else |
| 1418 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1419 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) { |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1420 | sqlite3_snprintf(sizeof(p->nullvalue), p->nullvalue, |
| 1421 | "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1422 | }else |
| 1423 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1424 | if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){ |
| 1425 | if( p->out!=stdout ){ |
| 1426 | fclose(p->out); |
| 1427 | } |
| 1428 | if( strcmp(azArg[1],"stdout")==0 ){ |
| 1429 | p->out = stdout; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1430 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "stdout"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1431 | }else{ |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 1432 | p->out = fopen(azArg[1], "wb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1433 | if( p->out==0 ){ |
| 1434 | fprintf(stderr,"can't write to \"%s\"\n", azArg[1]); |
| 1435 | p->out = stdout; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1436 | } else { |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1437 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | }else |
| 1441 | |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 1442 | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1443 | if( nArg >= 2) { |
| 1444 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 1445 | } |
| 1446 | if( nArg >= 3) { |
| 1447 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 1448 | } |
| 1449 | }else |
| 1450 | |
| 1451 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 1452 | rc = 2; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1453 | }else |
| 1454 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1455 | if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){ |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 1456 | FILE *alt = fopen(azArg[1], "rb"); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1457 | if( alt==0 ){ |
| 1458 | fprintf(stderr,"can't open \"%s\"\n", azArg[1]); |
| 1459 | }else{ |
| 1460 | process_input(p, alt); |
| 1461 | fclose(alt); |
| 1462 | } |
| 1463 | }else |
| 1464 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1465 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
| 1466 | struct callback_data data; |
| 1467 | char *zErrMsg = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1468 | open_db(p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1469 | memcpy(&data, p, sizeof(data)); |
| 1470 | data.showHeader = 0; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1471 | data.mode = MODE_Semi; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1472 | if( nArg>1 ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 1473 | int i; |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame^] | 1474 | for(i=0; azArg[1][i]; i++) azArg[1][i] = (char)tolower(azArg[1][i]); |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 1475 | if( strcmp(azArg[1],"sqlite_master")==0 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1476 | char *new_argv[2], *new_colv[2]; |
| 1477 | new_argv[0] = "CREATE TABLE sqlite_master (\n" |
| 1478 | " type text,\n" |
| 1479 | " name text,\n" |
| 1480 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1481 | " rootpage integer,\n" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1482 | " sql text\n" |
| 1483 | ")"; |
| 1484 | new_argv[1] = 0; |
| 1485 | new_colv[0] = "sql"; |
| 1486 | new_colv[1] = 0; |
| 1487 | callback(&data, 1, new_argv, new_colv); |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 1488 | }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1489 | char *new_argv[2], *new_colv[2]; |
| 1490 | new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" |
| 1491 | " type text,\n" |
| 1492 | " name text,\n" |
| 1493 | " tbl_name text,\n" |
| 1494 | " rootpage integer,\n" |
| 1495 | " sql text\n" |
| 1496 | ")"; |
| 1497 | new_argv[1] = 0; |
| 1498 | new_colv[0] = "sql"; |
| 1499 | new_colv[1] = 0; |
| 1500 | callback(&data, 1, new_argv, new_colv); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1501 | }else{ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1502 | zShellStatic = azArg[1]; |
| 1503 | sqlite3_exec(p->db, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1504 | "SELECT sql FROM " |
| 1505 | " (SELECT * FROM sqlite_master UNION ALL" |
| 1506 | " SELECT * FROM sqlite_temp_master) " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1507 | "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1508 | "ORDER BY substr(type,2,1), name", |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1509 | callback, &data, &zErrMsg); |
| 1510 | zShellStatic = 0; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1511 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1512 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1513 | sqlite3_exec(p->db, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1514 | "SELECT sql FROM " |
| 1515 | " (SELECT * FROM sqlite_master UNION ALL" |
| 1516 | " SELECT * FROM sqlite_temp_master) " |
drh | 0c35667 | 2005-09-10 22:40:53 +0000 | [diff] [blame] | 1517 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'" |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1518 | "ORDER BY substr(type,2,1), name", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1519 | callback, &data, &zErrMsg |
| 1520 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1521 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1522 | if( zErrMsg ){ |
| 1523 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1524 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1525 | } |
| 1526 | }else |
| 1527 | |
| 1528 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1529 | sqlite3_snprintf(sizeof(p->separator), p->separator, |
| 1530 | "%.*s", (int)sizeof(p->separator)-1, azArg[1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1531 | }else |
| 1532 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1533 | if( c=='s' && strncmp(azArg[0], "show", n)==0){ |
| 1534 | int i; |
| 1535 | fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1536 | fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off"); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 1537 | fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1538 | fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1539 | fprintf(p->out,"%9.9s: ", "nullvalue"); |
| 1540 | output_c_string(p->out, p->nullvalue); |
| 1541 | fprintf(p->out, "\n"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1542 | fprintf(p->out,"%9.9s: %s\n","output", |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1543 | strlen30(p->outfile) ? p->outfile : "stdout"); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1544 | fprintf(p->out,"%9.9s: ", "separator"); |
| 1545 | output_c_string(p->out, p->separator); |
| 1546 | fprintf(p->out, "\n"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1547 | fprintf(p->out,"%9.9s: ","width"); |
| 1548 | for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1549 | fprintf(p->out,"%d ",p->colWidth[i]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1550 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1551 | fprintf(p->out,"\n"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1552 | }else |
| 1553 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1554 | if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1555 | char **azResult; |
| 1556 | int nRow, rc; |
| 1557 | char *zErrMsg; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1558 | open_db(p); |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1559 | if( nArg==1 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1560 | rc = sqlite3_get_table(p->db, |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1561 | "SELECT name FROM sqlite_master " |
drh | 0c35667 | 2005-09-10 22:40:53 +0000 | [diff] [blame] | 1562 | "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'" |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1563 | "UNION ALL " |
| 1564 | "SELECT name FROM sqlite_temp_master " |
| 1565 | "WHERE type IN ('table','view') " |
| 1566 | "ORDER BY 1", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1567 | &azResult, &nRow, 0, &zErrMsg |
| 1568 | ); |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1569 | }else{ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1570 | zShellStatic = azArg[1]; |
| 1571 | rc = sqlite3_get_table(p->db, |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1572 | "SELECT name FROM sqlite_master " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1573 | "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1574 | "UNION ALL " |
| 1575 | "SELECT name FROM sqlite_temp_master " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1576 | "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1577 | "ORDER BY 1", |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1578 | &azResult, &nRow, 0, &zErrMsg |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1579 | ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1580 | zShellStatic = 0; |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1581 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1582 | if( zErrMsg ){ |
| 1583 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1584 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1585 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1586 | if( rc==SQLITE_OK ){ |
| 1587 | int len, maxlen = 0; |
| 1588 | int i, j; |
| 1589 | int nPrintCol, nPrintRow; |
| 1590 | for(i=1; i<=nRow; i++){ |
| 1591 | if( azResult[i]==0 ) continue; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1592 | len = strlen30(azResult[i]); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1593 | if( len>maxlen ) maxlen = len; |
| 1594 | } |
| 1595 | nPrintCol = 80/(maxlen+2); |
| 1596 | if( nPrintCol<1 ) nPrintCol = 1; |
| 1597 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 1598 | for(i=0; i<nPrintRow; i++){ |
| 1599 | for(j=i+1; j<=nRow; j+=nPrintRow){ |
| 1600 | char *zSp = j<=nPrintRow ? "" : " "; |
| 1601 | printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : ""); |
| 1602 | } |
| 1603 | printf("\n"); |
| 1604 | } |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 1605 | }else{ |
| 1606 | rc = 1; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1607 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1608 | sqlite3_free_table(azResult); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1609 | }else |
| 1610 | |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 1611 | if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1612 | open_db(p); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1613 | sqlite3_busy_timeout(p->db, atoi(azArg[1])); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1614 | }else |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 1615 | |
| 1616 | #if HAS_TIMER |
| 1617 | if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 && nArg>1 ){ |
| 1618 | enableTimer = booleanValue(azArg[1]); |
| 1619 | }else |
| 1620 | #endif |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1621 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1622 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
| 1623 | int j; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 1624 | assert( nArg<=ArraySize(azArg) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1625 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
| 1626 | p->colWidth[j-1] = atoi(azArg[j]); |
| 1627 | } |
| 1628 | }else |
| 1629 | |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 1630 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1631 | { |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1632 | fprintf(stderr, "unknown command or invalid arguments: " |
| 1633 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1634 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1635 | |
| 1636 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1639 | /* |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 1640 | ** Return TRUE if a semicolon occurs anywhere in the first N characters |
| 1641 | ** of string z[]. |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 1642 | */ |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 1643 | static int _contains_semicolon(const char *z, int N){ |
| 1644 | int i; |
| 1645 | for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } |
| 1646 | return 0; |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | /* |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 1650 | ** Test to see if a line consists entirely of whitespace. |
| 1651 | */ |
| 1652 | static int _all_whitespace(const char *z){ |
| 1653 | for(; *z; z++){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 1654 | if( isspace(*(unsigned char*)z) ) continue; |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 1655 | if( *z=='/' && z[1]=='*' ){ |
| 1656 | z += 2; |
| 1657 | while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
| 1658 | if( *z==0 ) return 0; |
| 1659 | z++; |
| 1660 | continue; |
| 1661 | } |
| 1662 | if( *z=='-' && z[1]=='-' ){ |
| 1663 | z += 2; |
| 1664 | while( *z && *z!='\n' ){ z++; } |
| 1665 | if( *z==0 ) return 1; |
| 1666 | continue; |
| 1667 | } |
| 1668 | return 0; |
| 1669 | } |
| 1670 | return 1; |
| 1671 | } |
| 1672 | |
| 1673 | /* |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 1674 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 1675 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 1676 | ** as is the Oracle "/". |
| 1677 | */ |
| 1678 | static int _is_command_terminator(const char *zLine){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 1679 | while( isspace(*(unsigned char*)zLine) ){ zLine++; }; |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 1680 | if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 1681 | if( tolower(zLine[0])=='g' && tolower(zLine[1])=='o' |
| 1682 | && _all_whitespace(&zLine[2]) ){ |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 1683 | return 1; /* SQL Server */ |
| 1684 | } |
| 1685 | return 0; |
| 1686 | } |
| 1687 | |
| 1688 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1689 | ** Read input from *in and process it. If *in==0 then input |
| 1690 | ** is interactive - the user is typing it it. Otherwise, input |
| 1691 | ** is coming from a file or device. A prompt is issued and history |
| 1692 | ** is saved only if input is interactive. An interrupt signal will |
| 1693 | ** cause this routine to exit immediately, unless input is interactive. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1694 | ** |
| 1695 | ** Return the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1696 | */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1697 | static int process_input(struct callback_data *p, FILE *in){ |
danielk1977 | 2ac2762 | 2007-07-03 05:31:16 +0000 | [diff] [blame] | 1698 | char *zLine = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1699 | char *zSql = 0; |
| 1700 | int nSql = 0; |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 1701 | int nSqlPrior = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1702 | char *zErrMsg; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1703 | int rc; |
| 1704 | int errCnt = 0; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1705 | int lineno = 0; |
| 1706 | int startline = 0; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1707 | |
| 1708 | while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ |
| 1709 | fflush(p->out); |
danielk1977 | 2ac2762 | 2007-07-03 05:31:16 +0000 | [diff] [blame] | 1710 | free(zLine); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1711 | zLine = one_input_line(zSql, in); |
| 1712 | if( zLine==0 ){ |
| 1713 | break; /* We have reached EOF */ |
| 1714 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1715 | if( seenInterrupt ){ |
| 1716 | if( in!=0 ) break; |
| 1717 | seenInterrupt = 0; |
| 1718 | } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1719 | lineno++; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1720 | if( p->echoOn ) printf("%s\n", zLine); |
drh | f817b6b | 2003-06-16 00:16:41 +0000 | [diff] [blame] | 1721 | if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue; |
drh | 2af0b2d | 2002-02-21 02:25:02 +0000 | [diff] [blame] | 1722 | if( zLine && zLine[0]=='.' && nSql==0 ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1723 | rc = do_meta_command(zLine, p); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 1724 | if( rc==2 ){ |
| 1725 | break; |
| 1726 | }else if( rc ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1727 | errCnt++; |
| 1728 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1729 | continue; |
| 1730 | } |
drh | c717b38 | 2008-11-11 00:30:11 +0000 | [diff] [blame] | 1731 | if( _is_command_terminator(zLine) && sqlite3_complete(zSql) ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1732 | memcpy(zLine,";",2); |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 1733 | } |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 1734 | nSqlPrior = nSql; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1735 | if( zSql==0 ){ |
| 1736 | int i; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 1737 | for(i=0; zLine[i] && isspace((unsigned char)zLine[i]); i++){} |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1738 | if( zLine[i]!=0 ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1739 | nSql = strlen30(zLine); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1740 | zSql = malloc( nSql+1 ); |
drh | c1f4494 | 2006-05-10 14:39:13 +0000 | [diff] [blame] | 1741 | if( zSql==0 ){ |
| 1742 | fprintf(stderr, "out of memory\n"); |
| 1743 | exit(1); |
| 1744 | } |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1745 | memcpy(zSql, zLine, nSql+1); |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1746 | startline = lineno; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1747 | } |
| 1748 | }else{ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1749 | int len = strlen30(zLine); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1750 | zSql = realloc( zSql, nSql + len + 2 ); |
| 1751 | if( zSql==0 ){ |
| 1752 | fprintf(stderr,"%s: out of memory!\n", Argv0); |
| 1753 | exit(1); |
| 1754 | } |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1755 | zSql[nSql++] = '\n'; |
| 1756 | memcpy(&zSql[nSql], zLine, len+1); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1757 | nSql += len; |
| 1758 | } |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 1759 | if( zSql && _contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) |
| 1760 | && sqlite3_complete(zSql) ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1761 | p->cnt = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1762 | open_db(p); |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 1763 | BEGIN_TIMER; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1764 | rc = sqlite3_exec(p->db, zSql, callback, p, &zErrMsg); |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 1765 | END_TIMER; |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 1766 | if( rc || zErrMsg ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1767 | char zPrefix[100]; |
| 1768 | if( in!=0 || !stdin_is_interactive ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1769 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
| 1770 | "SQL error near line %d:", startline); |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1771 | }else{ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1772 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "SQL error:"); |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1773 | } |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 1774 | if( zErrMsg!=0 ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1775 | printf("%s %s\n", zPrefix, zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1776 | sqlite3_free(zErrMsg); |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 1777 | zErrMsg = 0; |
| 1778 | }else{ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1779 | printf("%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 1780 | } |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1781 | errCnt++; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1782 | } |
| 1783 | free(zSql); |
| 1784 | zSql = 0; |
| 1785 | nSql = 0; |
| 1786 | } |
| 1787 | } |
| 1788 | if( zSql ){ |
drh | dfef499 | 2008-11-11 18:55:03 +0000 | [diff] [blame] | 1789 | if( !_all_whitespace(zSql) ) fprintf(stderr, "Incomplete SQL: %s\n", zSql); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1790 | free(zSql); |
| 1791 | } |
danielk1977 | 2ac2762 | 2007-07-03 05:31:16 +0000 | [diff] [blame] | 1792 | free(zLine); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1793 | return errCnt; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1796 | /* |
| 1797 | ** Return a pathname which is the user's home directory. A |
| 1798 | ** 0 return indicates an error of some kind. Space to hold the |
| 1799 | ** resulting string is obtained from malloc(). The calling |
| 1800 | ** function should free the result. |
| 1801 | */ |
| 1802 | static char *find_home_dir(void){ |
| 1803 | char *home_dir = NULL; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1804 | |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1805 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL) |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1806 | struct passwd *pwent; |
| 1807 | uid_t uid = getuid(); |
drh | bd842ba | 2002-08-21 11:26:41 +0000 | [diff] [blame] | 1808 | if( (pwent=getpwuid(uid)) != NULL) { |
| 1809 | home_dir = pwent->pw_dir; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1810 | } |
| 1811 | #endif |
| 1812 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 1813 | #if defined(_WIN32_WCE) |
| 1814 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
| 1815 | */ |
| 1816 | home_dir = strdup("/"); |
| 1817 | #else |
| 1818 | |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 1819 | #if defined(_WIN32) || defined(WIN32) || defined(__OS2__) |
| 1820 | if (!home_dir) { |
| 1821 | home_dir = getenv("USERPROFILE"); |
| 1822 | } |
| 1823 | #endif |
| 1824 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1825 | if (!home_dir) { |
| 1826 | home_dir = getenv("HOME"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
drh | cdb36b7 | 2006-06-12 12:57:45 +0000 | [diff] [blame] | 1829 | #if defined(_WIN32) || defined(WIN32) || defined(__OS2__) |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 1830 | if (!home_dir) { |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 1831 | char *zDrive, *zPath; |
| 1832 | int n; |
| 1833 | zDrive = getenv("HOMEDRIVE"); |
| 1834 | zPath = getenv("HOMEPATH"); |
| 1835 | if( zDrive && zPath ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1836 | n = strlen30(zDrive) + strlen30(zPath) + 1; |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 1837 | home_dir = malloc( n ); |
| 1838 | if( home_dir==0 ) return 0; |
| 1839 | sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
| 1840 | return home_dir; |
| 1841 | } |
| 1842 | home_dir = "c:\\"; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 1843 | } |
| 1844 | #endif |
| 1845 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 1846 | #endif /* !_WIN32_WCE */ |
| 1847 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1848 | if( home_dir ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1849 | int n = strlen30(home_dir) + 1; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1850 | char *z = malloc( n ); |
| 1851 | if( z ) memcpy(z, home_dir, n); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1852 | home_dir = z; |
| 1853 | } |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 1854 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1855 | return home_dir; |
| 1856 | } |
| 1857 | |
| 1858 | /* |
| 1859 | ** Read input from the file given by sqliterc_override. Or if that |
| 1860 | ** parameter is NULL, take input from ~/.sqliterc |
| 1861 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1862 | static void process_sqliterc( |
| 1863 | struct callback_data *p, /* Configuration data */ |
| 1864 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 1865 | ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1866 | char *home_dir = NULL; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1867 | const char *sqliterc = sqliterc_override; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 1868 | char *zBuf = 0; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1869 | FILE *in = NULL; |
drh | a959ac4 | 2007-06-20 13:10:00 +0000 | [diff] [blame] | 1870 | int nBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1871 | |
| 1872 | if (sqliterc == NULL) { |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1873 | home_dir = find_home_dir(); |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 1874 | if( home_dir==0 ){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1875 | #if !defined(__RTP__) && !defined(_WRS_KERNEL) |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 1876 | fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1877 | #endif |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 1878 | return; |
| 1879 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1880 | nBuf = strlen30(home_dir) + 16; |
drh | a959ac4 | 2007-06-20 13:10:00 +0000 | [diff] [blame] | 1881 | zBuf = malloc( nBuf ); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1882 | if( zBuf==0 ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1883 | fprintf(stderr,"%s: out of memory!\n", Argv0); |
| 1884 | exit(1); |
| 1885 | } |
drh | a959ac4 | 2007-06-20 13:10:00 +0000 | [diff] [blame] | 1886 | sqlite3_snprintf(nBuf, zBuf,"%s/.sqliterc",home_dir); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1887 | free(home_dir); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1888 | sqliterc = (const char*)zBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1889 | } |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 1890 | in = fopen(sqliterc,"rb"); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1891 | if( in ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1892 | if( stdin_is_interactive ){ |
drh | b695aca | 2007-07-30 20:41:52 +0000 | [diff] [blame] | 1893 | printf("-- Loading resources from %s\n",sqliterc); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1894 | } |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1895 | process_input(p,in); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 1896 | fclose(in); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1897 | } |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 1898 | free(zBuf); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1899 | return; |
| 1900 | } |
| 1901 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1902 | /* |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1903 | ** Show available command line options |
| 1904 | */ |
| 1905 | static const char zOptions[] = |
| 1906 | " -init filename read/process named file\n" |
| 1907 | " -echo print commands before execution\n" |
| 1908 | " -[no]header turn headers on or off\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1909 | " -bail stop after hitting an error\n" |
| 1910 | " -interactive force interactive I/O\n" |
| 1911 | " -batch force batch I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1912 | " -column set output mode to 'column'\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1913 | " -csv set output mode to 'csv'\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1914 | " -html set output mode to HTML\n" |
| 1915 | " -line set output mode to 'line'\n" |
| 1916 | " -list set output mode to 'list'\n" |
| 1917 | " -separator 'x' set output field separator (|)\n" |
| 1918 | " -nullvalue 'text' set text string for NULL values\n" |
| 1919 | " -version show SQLite version\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1920 | ; |
| 1921 | static void usage(int showDetail){ |
drh | 80e8be9 | 2006-08-29 12:04:19 +0000 | [diff] [blame] | 1922 | fprintf(stderr, |
| 1923 | "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
| 1924 | "FILENAME is the name of an SQLite database. A new database is created\n" |
| 1925 | "if the file does not previously exist.\n", Argv0); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1926 | if( showDetail ){ |
drh | 80e8be9 | 2006-08-29 12:04:19 +0000 | [diff] [blame] | 1927 | fprintf(stderr, "OPTIONS include:\n%s", zOptions); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1928 | }else{ |
| 1929 | fprintf(stderr, "Use the -help option for additional information\n"); |
| 1930 | } |
| 1931 | exit(1); |
| 1932 | } |
| 1933 | |
| 1934 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1935 | ** Initialize the state information in data |
| 1936 | */ |
drh | 0850b53 | 2006-01-31 19:31:43 +0000 | [diff] [blame] | 1937 | static void main_init(struct callback_data *data) { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1938 | memset(data, 0, sizeof(*data)); |
| 1939 | data->mode = MODE_List; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1940 | memcpy(data->separator,"|", 2); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1941 | data->showHeader = 0; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1942 | sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
| 1943 | sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1946 | int main(int argc, char **argv){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1947 | char *zErrMsg = 0; |
| 1948 | struct callback_data data; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1949 | const char *zInitFile = 0; |
| 1950 | char *zFirstCmd = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1951 | int i; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1952 | int rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1953 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1954 | Argv0 = argv[0]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1955 | main_init(&data); |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1956 | stdin_is_interactive = isatty(0); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1957 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1958 | /* Make sure we have a valid signal handler early, before anything |
| 1959 | ** else is done. |
| 1960 | */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1961 | #ifdef SIGINT |
| 1962 | signal(SIGINT, interrupt_handler); |
| 1963 | #endif |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1964 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1965 | /* Do an initial pass through the command-line argument to locate |
| 1966 | ** the name of the database file, the name of the initialization file, |
| 1967 | ** and the first command to execute. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1968 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1969 | for(i=1; i<argc-1; i++){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1970 | char *z; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1971 | if( argv[i][0]!='-' ) break; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1972 | z = argv[i]; |
| 1973 | if( z[0]=='-' && z[1]=='-' ) z++; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1974 | if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){ |
| 1975 | i++; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1976 | }else if( strcmp(argv[i],"-init")==0 ){ |
| 1977 | i++; |
| 1978 | zInitFile = argv[i]; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1979 | } |
| 1980 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1981 | if( i<argc ){ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 1982 | #if defined(SQLITE_OS_OS2) && SQLITE_OS_OS2 |
pweilbacher | d190be8 | 2008-04-15 18:50:02 +0000 | [diff] [blame] | 1983 | data.zDbFilename = (const char *)convertCpPathToUtf8( argv[i++] ); |
| 1984 | #else |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1985 | data.zDbFilename = argv[i++]; |
pweilbacher | d190be8 | 2008-04-15 18:50:02 +0000 | [diff] [blame] | 1986 | #endif |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1987 | }else{ |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1988 | #ifndef SQLITE_OMIT_MEMORYDB |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1989 | data.zDbFilename = ":memory:"; |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1990 | #else |
| 1991 | data.zDbFilename = 0; |
| 1992 | #endif |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1993 | } |
| 1994 | if( i<argc ){ |
| 1995 | zFirstCmd = argv[i++]; |
| 1996 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1997 | data.out = stdout; |
| 1998 | |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 1999 | #ifdef SQLITE_OMIT_MEMORYDB |
| 2000 | if( data.zDbFilename==0 ){ |
| 2001 | fprintf(stderr,"%s: no database filename specified\n", argv[0]); |
| 2002 | exit(1); |
| 2003 | } |
| 2004 | #endif |
| 2005 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2006 | /* Go ahead and open the database file if it already exists. If the |
| 2007 | ** file does not exist, delay opening it. This prevents empty database |
| 2008 | ** files from being created if a user mistypes the database name argument |
| 2009 | ** to the sqlite command-line tool. |
| 2010 | */ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 2011 | if( access(data.zDbFilename, 0)==0 ){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2012 | open_db(&data); |
| 2013 | } |
| 2014 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2015 | /* Process the initialization file if there is one. If no -init option |
| 2016 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 2017 | ** try to process it. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2018 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2019 | process_sqliterc(&data,zInitFile); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2020 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2021 | /* Make a second pass through the command-line argument and set |
| 2022 | ** options. This second pass is delayed until after the initialization |
| 2023 | ** file is processed so that the command-line arguments will override |
| 2024 | ** settings in the initialization file. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2025 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2026 | for(i=1; i<argc && argv[i][0]=='-'; i++){ |
| 2027 | char *z = argv[i]; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2028 | if( z[1]=='-' ){ z++; } |
drh | 2e584cd | 2006-09-25 13:09:22 +0000 | [diff] [blame] | 2029 | if( strcmp(z,"-init")==0 ){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2030 | i++; |
| 2031 | }else if( strcmp(z,"-html")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2032 | data.mode = MODE_Html; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2033 | }else if( strcmp(z,"-list")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2034 | data.mode = MODE_List; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2035 | }else if( strcmp(z,"-line")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2036 | data.mode = MODE_Line; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2037 | }else if( strcmp(z,"-column")==0 ){ |
drh | 8b32e17 | 2002-04-08 02:42:57 +0000 | [diff] [blame] | 2038 | data.mode = MODE_Column; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2039 | }else if( strcmp(z,"-csv")==0 ){ |
| 2040 | data.mode = MODE_Csv; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2041 | memcpy(data.separator,",",2); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2042 | }else if( strcmp(z,"-separator")==0 ){ |
| 2043 | i++; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2044 | sqlite3_snprintf(sizeof(data.separator), data.separator, |
| 2045 | "%.*s",(int)sizeof(data.separator)-1,argv[i]); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2046 | }else if( strcmp(z,"-nullvalue")==0 ){ |
| 2047 | i++; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2048 | sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue, |
| 2049 | "%.*s",(int)sizeof(data.nullvalue)-1,argv[i]); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2050 | }else if( strcmp(z,"-header")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2051 | data.showHeader = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2052 | }else if( strcmp(z,"-noheader")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2053 | data.showHeader = 0; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2054 | }else if( strcmp(z,"-echo")==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2055 | data.echoOn = 1; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2056 | }else if( strcmp(z,"-bail")==0 ){ |
| 2057 | bail_on_error = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2058 | }else if( strcmp(z,"-version")==0 ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 2059 | printf("%s\n", sqlite3_libversion()); |
drh | 151e3e1 | 2006-06-06 12:32:21 +0000 | [diff] [blame] | 2060 | return 0; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2061 | }else if( strcmp(z,"-interactive")==0 ){ |
| 2062 | stdin_is_interactive = 1; |
| 2063 | }else if( strcmp(z,"-batch")==0 ){ |
| 2064 | stdin_is_interactive = 0; |
drh | 80e8be9 | 2006-08-29 12:04:19 +0000 | [diff] [blame] | 2065 | }else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){ |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 2066 | usage(1); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2067 | }else{ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2068 | fprintf(stderr,"%s: unknown option: %s\n", Argv0, z); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 2069 | fprintf(stderr,"Use -help for a list of options.\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2070 | return 1; |
| 2071 | } |
| 2072 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2073 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2074 | if( zFirstCmd ){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2075 | /* Run just the command that follows the database name |
| 2076 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2077 | if( zFirstCmd[0]=='.' ){ |
| 2078 | do_meta_command(zFirstCmd, &data); |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 2079 | exit(0); |
| 2080 | }else{ |
| 2081 | int rc; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2082 | open_db(&data); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2083 | rc = sqlite3_exec(data.db, zFirstCmd, callback, &data, &zErrMsg); |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 2084 | if( rc!=0 && zErrMsg!=0 ){ |
| 2085 | fprintf(stderr,"SQL error: %s\n", zErrMsg); |
| 2086 | exit(1); |
| 2087 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2088 | } |
| 2089 | }else{ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2090 | /* Run commands received from standard input |
| 2091 | */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2092 | if( stdin_is_interactive ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2093 | char *zHome; |
| 2094 | char *zHistory = 0; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2095 | int nHistory; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2096 | printf( |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 2097 | "SQLite version %s\n" |
mihailim | 65df9db | 2008-06-28 11:29:22 +0000 | [diff] [blame] | 2098 | "Enter \".help\" for instructions\n" |
| 2099 | "Enter SQL statements terminated with a \";\"\n", |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 2100 | sqlite3_libversion() |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2101 | ); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2102 | zHome = find_home_dir(); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 2103 | if( zHome ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2104 | nHistory = strlen30(zHome) + 20; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 2105 | if( (zHistory = malloc(nHistory))!=0 ){ |
| 2106 | sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 2107 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2108 | } |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 2109 | #if defined(HAVE_READLINE) && HAVE_READLINE==1 |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2110 | if( zHistory ) read_history(zHistory); |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 2111 | #endif |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2112 | rc = process_input(&data, 0); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2113 | if( zHistory ){ |
| 2114 | stifle_history(100); |
| 2115 | write_history(zHistory); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 2116 | free(zHistory); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2117 | } |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 2118 | free(zHome); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2119 | }else{ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2120 | rc = process_input(&data, stdin); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2121 | } |
| 2122 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2123 | set_table_name(&data, 0); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 2124 | if( db ){ |
| 2125 | if( sqlite3_close(db)!=SQLITE_OK ){ |
| 2126 | fprintf(stderr,"error closing database: %s\n", sqlite3_errmsg(db)); |
| 2127 | } |
| 2128 | } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2129 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2130 | } |