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