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