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