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