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 | ** |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame^] | 15 | ** $Id: shell.c,v 1.102 2004/06/05 08:04:44 danielk1977 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 | */ |
| 60 | static sqlite *db = 0; |
| 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 | */ |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 84 | extern int sqlite3IsNumber(const char*, int*, unsigned char); |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 85 | |
| 86 | /* |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 87 | ** This routine reads a line of text from standard input, stores |
| 88 | ** the text in memory obtained from malloc() and returns a pointer |
| 89 | ** to the text. NULL is returned at end of file, or if malloc() |
| 90 | ** fails. |
| 91 | ** |
| 92 | ** The interface is like "readline" but no command-line editing |
| 93 | ** is done. |
| 94 | */ |
drh | 9347b20 | 2003-07-18 01:30:59 +0000 | [diff] [blame] | 95 | static char *local_getline(char *zPrompt, FILE *in){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 96 | char *zLine; |
| 97 | int nLine; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 98 | int n; |
| 99 | int eol; |
| 100 | |
| 101 | if( zPrompt && *zPrompt ){ |
| 102 | printf("%s",zPrompt); |
| 103 | fflush(stdout); |
| 104 | } |
| 105 | nLine = 100; |
| 106 | zLine = malloc( nLine ); |
| 107 | if( zLine==0 ) return 0; |
| 108 | n = 0; |
| 109 | eol = 0; |
| 110 | while( !eol ){ |
| 111 | if( n+100>nLine ){ |
| 112 | nLine = nLine*2 + 100; |
| 113 | zLine = realloc(zLine, nLine); |
| 114 | if( zLine==0 ) return 0; |
| 115 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 116 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 117 | if( n==0 ){ |
| 118 | free(zLine); |
| 119 | return 0; |
| 120 | } |
| 121 | zLine[n] = 0; |
| 122 | eol = 1; |
| 123 | break; |
| 124 | } |
| 125 | while( zLine[n] ){ n++; } |
| 126 | if( n>0 && zLine[n-1]=='\n' ){ |
| 127 | n--; |
| 128 | zLine[n] = 0; |
| 129 | eol = 1; |
| 130 | } |
| 131 | } |
| 132 | zLine = realloc( zLine, n+1 ); |
| 133 | return zLine; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | ** Retrieve a single line of input text. "isatty" is true if text |
| 138 | ** is coming from a terminal. In that case, we issue a prompt and |
| 139 | ** attempt to use "readline" for command-line editing. If "isatty" |
drh | 9347b20 | 2003-07-18 01:30:59 +0000 | [diff] [blame] | 140 | ** is false, use "local_getline" instead of "readline" and issue no prompt. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 141 | ** |
| 142 | ** zPrior is a string of prior text retrieved. If not the empty |
| 143 | ** string, then issue a continuation prompt. |
| 144 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 145 | static char *one_input_line(const char *zPrior, FILE *in){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 146 | char *zPrompt; |
| 147 | char *zResult; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 148 | if( in!=0 ){ |
drh | 9347b20 | 2003-07-18 01:30:59 +0000 | [diff] [blame] | 149 | return local_getline(0, in); |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 150 | } |
| 151 | if( zPrior && zPrior[0] ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 152 | zPrompt = continuePrompt; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 153 | }else{ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 154 | zPrompt = mainPrompt; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 155 | } |
| 156 | zResult = readline(zPrompt); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 157 | if( zResult ) add_history(zResult); |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 158 | return zResult; |
| 159 | } |
| 160 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 161 | struct previous_mode_data { |
| 162 | int valid; /* Is there legit data in here? */ |
| 163 | int mode; |
| 164 | int showHeader; |
| 165 | int colWidth[100]; |
| 166 | }; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 167 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 168 | ** An pointer to an instance of this structure is passed from |
| 169 | ** the main program to the callback. This is used to communicate |
| 170 | ** state and mode information. |
| 171 | */ |
| 172 | struct callback_data { |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 173 | sqlite *db; /* The database */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 174 | int echoOn; /* True to echo input commands */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 175 | int cnt; /* Number of records displayed so far */ |
| 176 | FILE *out; /* Write results here */ |
| 177 | int mode; /* An output mode setting */ |
| 178 | int showHeader; /* True to show column names in List or Column mode */ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 179 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 180 | char separator[20]; /* Separator character for MODE_List */ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 181 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 182 | int actualWidth[100]; /* Actual width of each column */ |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 183 | char nullvalue[20]; /* The text to print when a NULL comes back from |
| 184 | ** the database */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 185 | struct previous_mode_data explainPrev; |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 186 | /* Holds the mode information just before |
| 187 | ** .explain ON */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 188 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
| 189 | const char *zDbFilename; /* name of the database file */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 190 | char *zKey; /* Encryption key */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | /* |
| 194 | ** These are the allowed modes. |
| 195 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 196 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 197 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 198 | #define MODE_List 2 /* One record per line with a separator */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 199 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 200 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 201 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 202 | #define MODE_NUM_OF 6 /* The number of modes (not a mode itself) */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 203 | |
| 204 | char *modeDescr[MODE_NUM_OF] = { |
| 205 | "line", |
| 206 | "column", |
| 207 | "list", |
| 208 | "semi", |
| 209 | "html", |
| 210 | "insert" |
| 211 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | ** Number of elements in an array |
| 215 | */ |
| 216 | #define ArraySize(X) (sizeof(X)/sizeof(X[0])) |
| 217 | |
| 218 | /* |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 219 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 220 | */ |
| 221 | static void output_quoted_string(FILE *out, const char *z){ |
| 222 | int i; |
| 223 | int nSingle = 0; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 224 | for(i=0; z[i]; i++){ |
| 225 | if( z[i]=='\'' ) nSingle++; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 226 | } |
| 227 | if( nSingle==0 ){ |
| 228 | fprintf(out,"'%s'",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 229 | }else{ |
| 230 | fprintf(out,"'"); |
| 231 | while( *z ){ |
| 232 | for(i=0; z[i] && z[i]!='\''; i++){} |
| 233 | if( i==0 ){ |
| 234 | fprintf(out,"''"); |
| 235 | z++; |
| 236 | }else if( z[i]=='\'' ){ |
| 237 | fprintf(out,"%.*s''",i,z); |
| 238 | z += i+1; |
| 239 | }else{ |
drh | cd7d273 | 2002-02-26 23:24:26 +0000 | [diff] [blame] | 240 | fprintf(out,"%s",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 241 | break; |
| 242 | } |
| 243 | } |
drh | cd7d273 | 2002-02-26 23:24:26 +0000 | [diff] [blame] | 244 | fprintf(out,"'"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | /* |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 249 | ** Output the given string with characters that are special to |
| 250 | ** HTML escaped. |
| 251 | */ |
| 252 | static void output_html_string(FILE *out, const char *z){ |
| 253 | int i; |
| 254 | while( *z ){ |
| 255 | for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){} |
| 256 | if( i>0 ){ |
| 257 | fprintf(out,"%.*s",i,z); |
| 258 | } |
| 259 | if( z[i]=='<' ){ |
| 260 | fprintf(out,"<"); |
| 261 | }else if( z[i]=='&' ){ |
| 262 | fprintf(out,"&"); |
| 263 | }else{ |
| 264 | break; |
| 265 | } |
| 266 | z += i + 1; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 271 | ** This routine runs when the user presses Ctrl-C |
| 272 | */ |
| 273 | static void interrupt_handler(int NotUsed){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 274 | seenInterrupt = 1; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 275 | if( db ) sqlite3_interrupt(db); |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 279 | ** This is the callback routine that the SQLite library |
| 280 | ** invokes for each row of a query result. |
| 281 | */ |
| 282 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 283 | int i; |
| 284 | struct callback_data *p = (struct callback_data*)pArg; |
| 285 | switch( p->mode ){ |
| 286 | case MODE_Line: { |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 287 | int w = 5; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 288 | if( azArg==0 ) break; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 289 | for(i=0; i<nArg; i++){ |
| 290 | int len = strlen(azCol[i]); |
| 291 | if( len>w ) w = len; |
| 292 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 293 | if( p->cnt++>0 ) fprintf(p->out,"\n"); |
| 294 | for(i=0; i<nArg; i++){ |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 295 | fprintf(p->out,"%*s = %s\n", w, azCol[i], |
| 296 | azArg[i] ? azArg[i] : p->nullvalue); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 297 | } |
| 298 | break; |
| 299 | } |
| 300 | case MODE_Column: { |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 301 | if( p->cnt++==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 302 | for(i=0; i<nArg; i++){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 303 | int w, n; |
| 304 | if( i<ArraySize(p->colWidth) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 305 | w = p->colWidth[i]; |
| 306 | }else{ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 307 | w = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 308 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 309 | if( w<=0 ){ |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 310 | w = strlen(azCol[i] ? azCol[i] : ""); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 311 | if( w<10 ) w = 10; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 312 | n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 313 | if( w<n ) w = n; |
| 314 | } |
| 315 | if( i<ArraySize(p->actualWidth) ){ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 316 | p->actualWidth[i] = w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 317 | } |
| 318 | if( p->showHeader ){ |
| 319 | fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " "); |
| 320 | } |
| 321 | } |
| 322 | if( p->showHeader ){ |
| 323 | for(i=0; i<nArg; i++){ |
| 324 | int w; |
| 325 | if( i<ArraySize(p->actualWidth) ){ |
| 326 | w = p->actualWidth[i]; |
| 327 | }else{ |
| 328 | w = 10; |
| 329 | } |
| 330 | fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------" |
| 331 | "----------------------------------------------------------", |
| 332 | i==nArg-1 ? "\n": " "); |
| 333 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 336 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 337 | for(i=0; i<nArg; i++){ |
| 338 | int w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 339 | if( i<ArraySize(p->actualWidth) ){ |
| 340 | w = p->actualWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 341 | }else{ |
| 342 | w = 10; |
| 343 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 344 | fprintf(p->out,"%-*.*s%s",w,w, |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 345 | azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " "); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 346 | } |
| 347 | break; |
| 348 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 349 | case MODE_Semi: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 350 | case MODE_List: { |
| 351 | if( p->cnt++==0 && p->showHeader ){ |
| 352 | for(i=0; i<nArg; i++){ |
| 353 | fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator); |
| 354 | } |
| 355 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 356 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 357 | for(i=0; i<nArg; i++){ |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 358 | char *z = azArg[i]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 359 | if( z==0 ) z = p->nullvalue; |
drh | 71172c5 | 2002-01-24 00:00:21 +0000 | [diff] [blame] | 360 | fprintf(p->out, "%s", z); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 361 | if( i<nArg-1 ){ |
| 362 | fprintf(p->out, "%s", p->separator); |
| 363 | }else if( p->mode==MODE_Semi ){ |
| 364 | fprintf(p->out, ";\n"); |
| 365 | }else{ |
| 366 | fprintf(p->out, "\n"); |
| 367 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 368 | } |
| 369 | break; |
| 370 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 371 | case MODE_Html: { |
| 372 | if( p->cnt++==0 && p->showHeader ){ |
| 373 | fprintf(p->out,"<TR>"); |
| 374 | for(i=0; i<nArg; i++){ |
| 375 | fprintf(p->out,"<TH>%s</TH>",azCol[i]); |
| 376 | } |
| 377 | fprintf(p->out,"</TR>\n"); |
| 378 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 379 | if( azArg==0 ) break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 380 | fprintf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 381 | for(i=0; i<nArg; i++){ |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 382 | fprintf(p->out,"<TD>"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 383 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue); |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 384 | fprintf(p->out,"</TD>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 385 | } |
drh | 7d686b2 | 2002-11-11 13:56:47 +0000 | [diff] [blame] | 386 | fprintf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 387 | break; |
| 388 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 389 | case MODE_Insert: { |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 390 | if( azArg==0 ) break; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 391 | fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 392 | for(i=0; i<nArg; i++){ |
| 393 | char *zSep = i>0 ? ",": ""; |
| 394 | if( azArg[i]==0 ){ |
| 395 | fprintf(p->out,"%sNULL",zSep); |
danielk1977 | 8a6b541 | 2004-05-24 07:04:25 +0000 | [diff] [blame] | 396 | }else if( sqlite3IsNumber(azArg[i], 0, 1) ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 397 | fprintf(p->out,"%s%s",zSep, azArg[i]); |
| 398 | }else{ |
| 399 | if( zSep[0] ) fprintf(p->out,"%s",zSep); |
| 400 | output_quoted_string(p->out, azArg[i]); |
| 401 | } |
| 402 | } |
| 403 | fprintf(p->out,");\n"); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 404 | break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 405 | } |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 406 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | /* |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 411 | ** Set the destination table field of the callback_data structure to |
| 412 | ** the name of the table given. Escape any quote characters in the |
| 413 | ** table name. |
| 414 | */ |
| 415 | static void set_table_name(struct callback_data *p, const char *zName){ |
| 416 | int i, n; |
| 417 | int needQuote; |
| 418 | char *z; |
| 419 | |
| 420 | if( p->zDestTable ){ |
| 421 | free(p->zDestTable); |
| 422 | p->zDestTable = 0; |
| 423 | } |
| 424 | if( zName==0 ) return; |
| 425 | needQuote = !isalpha(*zName) && *zName!='_'; |
| 426 | for(i=n=0; zName[i]; i++, n++){ |
| 427 | if( !isalnum(zName[i]) && zName[i]!='_' ){ |
| 428 | needQuote = 1; |
| 429 | if( zName[i]=='\'' ) n++; |
| 430 | } |
| 431 | } |
| 432 | if( needQuote ) n += 2; |
| 433 | z = p->zDestTable = malloc( n+1 ); |
| 434 | if( z==0 ){ |
| 435 | fprintf(stderr,"Out of memory!\n"); |
| 436 | exit(1); |
| 437 | } |
| 438 | n = 0; |
| 439 | if( needQuote ) z[n++] = '\''; |
| 440 | for(i=0; zName[i]; i++){ |
| 441 | z[n++] = zName[i]; |
| 442 | if( zName[i]=='\'' ) z[n++] = '\''; |
| 443 | } |
| 444 | if( needQuote ) z[n++] = '\''; |
| 445 | z[n] = 0; |
| 446 | } |
| 447 | |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame^] | 448 | /* zIn is either a pointer to a NULL-terminated string in memory obtained |
| 449 | ** from malloc(), or a NULL pointer. The string pointed to by zAppend is |
| 450 | ** added to zIn, and the result returned in memory obtained from malloc(). |
| 451 | ** zIn, if it was not NULL, is freed. |
| 452 | ** |
| 453 | ** If the third argument, quote, is not '\0', then it is used as a |
| 454 | ** quote character for zAppend. |
| 455 | */ |
| 456 | static char * appendText(char *zIn, char const *zAppend, char quote){ |
| 457 | int len; |
| 458 | int i; |
| 459 | int nAppend = strlen(zAppend); |
| 460 | int nIn = (zIn?strlen(zIn):0); |
| 461 | |
| 462 | len = nAppend+nIn+1; |
| 463 | if( quote ){ |
| 464 | len += 2; |
| 465 | for(i=0; i<nAppend; i++){ |
| 466 | if( zAppend[i]==quote ) len++; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | zIn = (char *)realloc(zIn, len); |
| 471 | if( !zIn ){ |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | if( quote ){ |
| 476 | char *zCsr = &zIn[nIn]; |
| 477 | *zCsr++ = quote; |
| 478 | for(i=0; i<nAppend; i++){ |
| 479 | *zCsr++ = zAppend[i]; |
| 480 | if( zAppend[i]==quote ) *zCsr++ = quote; |
| 481 | } |
| 482 | *zCsr++ = quote; |
| 483 | *zCsr++ = '\0'; |
| 484 | assert( (zCsr-zIn)==len ); |
| 485 | }else{ |
| 486 | memcpy(&zIn[nIn], zAppend, nAppend); |
| 487 | zIn[len-1] = '\0'; |
| 488 | } |
| 489 | |
| 490 | return zIn; |
| 491 | } |
| 492 | |
| 493 | /* This function implements the SQL scalar function dump_literal()used by |
| 494 | * the '.dump' built-in. It takes one argument and returns the fully quoted |
| 495 | ** literal version of the argument depending on the type. |
| 496 | ** |
| 497 | ** Type Example |
| 498 | ** ----------------------- |
| 499 | ** NULL "NULL" |
| 500 | ** INTEGER "0" |
| 501 | ** REAL "0.0" |
| 502 | ** TEXT "'abc'" |
| 503 | ** BLOB "X'89AB'" |
| 504 | ** |
| 505 | */ |
| 506 | static void dump_literalFunc( |
| 507 | sqlite3_context *context, |
| 508 | int argc, |
| 509 | sqlite3_value **argv |
| 510 | ){ |
| 511 | static const char hexdigits[] = { |
| 512 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 513 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 514 | }; |
| 515 | assert( argc==1 ); |
| 516 | |
| 517 | switch( sqlite3_value_type(argv[0]) ){ |
| 518 | case SQLITE_NULL: |
| 519 | sqlite3_result_text(context, "NULL", -1, 0); |
| 520 | break; |
| 521 | case SQLITE_INTEGER: |
| 522 | sqlite3_result_text(context, sqlite3_value_text(argv[0]), -1, 1); |
| 523 | break; |
| 524 | case SQLITE_FLOAT: { |
| 525 | char zBuf[40]; |
| 526 | sprintf(zBuf, "%.15g", sqlite3_value_double(argv[0])); |
| 527 | sqlite3_result_text(context, zBuf, -1, 1); |
| 528 | break; |
| 529 | } |
| 530 | case SQLITE_TEXT: { |
| 531 | char *zText; |
| 532 | zText = appendText(0, sqlite3_value_text(argv[0]), '\''); |
| 533 | if( !zText ){ |
| 534 | sqlite3_result_error(context, "out of memory", -1); |
| 535 | }else{ |
| 536 | sqlite3_result_text(context, zText, -1, 1); |
| 537 | free(zText); |
| 538 | } |
| 539 | break; |
| 540 | } |
| 541 | case SQLITE_BLOB: { |
| 542 | char *zText = 0; |
| 543 | int nBlob = sqlite3_value_bytes(argv[0]); |
| 544 | char const *zBlob = sqlite3_value_blob(argv[0]); |
| 545 | |
| 546 | zText = (char *)malloc((2*nBlob)+4); |
| 547 | if( !zText ){ |
| 548 | sqlite3_result_error(context, "out of memory", -1); |
| 549 | }else{ |
| 550 | int i; |
| 551 | for(i=0; i<nBlob; i++){ |
| 552 | zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; |
| 553 | zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; |
| 554 | } |
| 555 | zText[(nBlob*2)+2] = '\''; |
| 556 | zText[(nBlob*2)+3] = '\0'; |
| 557 | zText[0] = 'X'; |
| 558 | zText[1] = '\''; |
| 559 | sqlite3_result_text(context, zText, -1, 1); |
| 560 | free(zText); |
| 561 | } |
| 562 | break; |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 567 | /* |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 568 | ** This is a different callback routine used for dumping the database. |
| 569 | ** Each row received by this callback consists of a table name, |
| 570 | ** the table type ("index" or "table") and SQL to create the table. |
| 571 | ** This routine should print text sufficient to recreate the table. |
| 572 | */ |
| 573 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame^] | 574 | int rc; |
| 575 | const char *zTable; |
| 576 | const char *zType; |
| 577 | const char *zSql; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 578 | struct callback_data *p = (struct callback_data *)pArg; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame^] | 579 | |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 580 | if( nArg!=3 ) return 1; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame^] | 581 | zTable = azArg[0]; |
| 582 | zType = azArg[1]; |
| 583 | zSql = azArg[2]; |
| 584 | |
| 585 | fprintf(p->out, "%s;\n", zSql); |
| 586 | |
| 587 | if( strcmp(zType, "table")==0 ){ |
| 588 | sqlite3_stmt *pTableInfo = 0; |
| 589 | sqlite3_stmt *pSelect = 0; |
| 590 | char *zSelect = 0; |
| 591 | char *zTableInfo = 0; |
| 592 | char *zTmp = 0; |
| 593 | |
| 594 | zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0); |
| 595 | zTableInfo = appendText(zTableInfo, zTable, '"'); |
| 596 | zTableInfo = appendText(zTableInfo, ");", 0); |
| 597 | |
| 598 | rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0); |
| 599 | if( zTableInfo ) free(zTableInfo); |
| 600 | if( rc!=SQLITE_OK || !pTableInfo ){ |
| 601 | return 1; |
| 602 | } |
| 603 | |
| 604 | zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0); |
| 605 | zTmp = appendText(zTmp, zTable, '"'); |
| 606 | if( zTmp ){ |
| 607 | zSelect = appendText(zSelect, zTmp, '\''); |
| 608 | } |
| 609 | zSelect = appendText(zSelect, " || ' VALUES(' || ", 0); |
| 610 | rc = sqlite3_step(pTableInfo); |
| 611 | while( rc==SQLITE_ROW ){ |
| 612 | zSelect = appendText(zSelect, "dump_literal(", 0); |
| 613 | zSelect = appendText(zSelect, sqlite3_column_text(pTableInfo, 1), '"'); |
| 614 | rc = sqlite3_step(pTableInfo); |
| 615 | if( rc==SQLITE_ROW ){ |
| 616 | zSelect = appendText(zSelect, ") || ', ' || ", 0); |
| 617 | }else{ |
| 618 | zSelect = appendText(zSelect, ") ", 0); |
| 619 | } |
| 620 | } |
| 621 | rc = sqlite3_finalize(pTableInfo); |
| 622 | if( rc!=SQLITE_OK ){ |
| 623 | if( zSelect ) free(zSelect); |
| 624 | return 1; |
| 625 | } |
| 626 | zSelect = appendText(zSelect, "|| ')' FROM ", 0); |
| 627 | zSelect = appendText(zSelect, zTable, '"'); |
| 628 | |
| 629 | rc = sqlite3_prepare(p->db, zSelect, -1, &pSelect, 0); |
| 630 | if( zSelect ) free(zSelect); |
| 631 | if( rc!=SQLITE_OK || !pSelect ){ |
| 632 | return 1; |
| 633 | } |
| 634 | |
| 635 | rc = sqlite3_step(pSelect); |
| 636 | while( rc==SQLITE_ROW ){ |
| 637 | fprintf(p->out, "%s;\n", sqlite3_column_text(pSelect, 0)); |
| 638 | rc = sqlite3_step(pSelect); |
| 639 | } |
| 640 | rc = sqlite3_finalize(pSelect); |
| 641 | if( rc!=SQLITE_OK ){ |
| 642 | return 1; |
| 643 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 644 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame^] | 645 | |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 650 | ** Text of a help message |
| 651 | */ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 652 | static char zHelp[] = |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 653 | ".databases List names and files of attached databases\n" |
| 654 | ".dump ?TABLE? ... Dump the database in a text format\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 655 | ".echo ON|OFF Turn command echo on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 656 | ".exit Exit this program\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 657 | ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 658 | ".header(s) ON|OFF Turn display of headers on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 659 | ".help Show this message\n" |
| 660 | ".indices TABLE Show names of all indices on TABLE\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 661 | ".mode MODE Set mode to one of \"line(s)\", \"column(s)\", \n" |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 662 | " \"insert\", \"list\", or \"html\"\n" |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 663 | ".mode insert TABLE Generate SQL insert statements for TABLE\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 664 | ".nullvalue STRING Print STRING instead of nothing for NULL data\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 665 | ".output FILENAME Send output to FILENAME\n" |
| 666 | ".output stdout Send output to the screen\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 667 | ".prompt MAIN CONTINUE Replace the standard prompts\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 668 | ".quit Exit this program\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 669 | ".read FILENAME Execute SQL in FILENAME\n" |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 670 | #ifdef SQLITE_HAS_CODEC |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 671 | ".rekey OLD NEW NEW Change the encryption key\n" |
| 672 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 673 | ".schema ?TABLE? Show the CREATE statements\n" |
| 674 | ".separator STRING Change separator string for \"list\" mode\n" |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 675 | ".show Show the current values for various settings\n" |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 676 | ".tables ?PATTERN? List names of tables matching a pattern\n" |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 677 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 678 | ".width NUM NUM ... Set column widths for \"column\" mode\n" |
| 679 | ; |
| 680 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 681 | /* Forward reference */ |
| 682 | static void process_input(struct callback_data *p, FILE *in); |
| 683 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 684 | /* |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 685 | ** Make sure the database is open. If it is not, then open it. If |
| 686 | ** the database fails to open, print an error message and exit. |
| 687 | */ |
| 688 | static void open_db(struct callback_data *p){ |
| 689 | if( p->db==0 ){ |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 690 | #ifdef SQLITE_HAS_CODEC |
drh | eb8ed70 | 2004-02-11 10:37:23 +0000 | [diff] [blame] | 691 | int n = p->zKey ? strlen(p->zKey) : 0; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 692 | db = p->db = sqlite3_open_encrypted(p->zDbFilename, p->zKey, n, 0, &zErrMsg); |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 693 | assert(0); /* Encrypted databases are broken in SQLite 3 */ |
drh | eb8ed70 | 2004-02-11 10:37:23 +0000 | [diff] [blame] | 694 | #else |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 695 | sqlite3_open(p->zDbFilename, &p->db, 0); |
| 696 | db = p->db; |
drh | eb8ed70 | 2004-02-11 10:37:23 +0000 | [diff] [blame] | 697 | #endif |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 698 | if( SQLITE_OK!=sqlite3_errcode(db) ){ |
| 699 | fprintf(stderr,"Unable to open database \"%s\": %s\n", |
| 700 | p->zDbFilename, sqlite3_errmsg(db)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 701 | exit(1); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 702 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame^] | 703 | |
| 704 | /* Add the 'dump_literal' SQL function used by .dump */ |
| 705 | sqlite3_create_function(db,"dump_literal",1,0,0,0,dump_literalFunc,0,0); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
| 709 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 710 | ** If an input line begins with "." then invoke this routine to |
| 711 | ** process that line. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 712 | ** |
| 713 | ** Return 1 to exit and 0 to continue. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 714 | */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 715 | static int do_meta_command(char *zLine, struct callback_data *p){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 716 | int i = 1; |
| 717 | int nArg = 0; |
| 718 | int n, c; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 719 | int rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 720 | char *azArg[50]; |
| 721 | |
| 722 | /* Parse the input line into tokens. |
| 723 | */ |
| 724 | while( zLine[i] && nArg<ArraySize(azArg) ){ |
| 725 | while( isspace(zLine[i]) ){ i++; } |
drh | 0633368 | 2004-03-09 13:37:45 +0000 | [diff] [blame] | 726 | if( zLine[i]==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 727 | if( zLine[i]=='\'' || zLine[i]=='"' ){ |
| 728 | int delim = zLine[i++]; |
| 729 | azArg[nArg++] = &zLine[i]; |
| 730 | while( zLine[i] && zLine[i]!=delim ){ i++; } |
| 731 | if( zLine[i]==delim ){ |
| 732 | zLine[i++] = 0; |
| 733 | } |
| 734 | }else{ |
| 735 | azArg[nArg++] = &zLine[i]; |
| 736 | while( zLine[i] && !isspace(zLine[i]) ){ i++; } |
| 737 | if( zLine[i] ) zLine[i++] = 0; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | /* Process the input line. |
| 742 | */ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 743 | if( nArg==0 ) return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 744 | n = strlen(azArg[0]); |
| 745 | c = azArg[0][0]; |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 746 | if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 747 | struct callback_data data; |
| 748 | char *zErrMsg = 0; |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 749 | open_db(p); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 750 | memcpy(&data, p, sizeof(data)); |
drh | d888544 | 2004-03-17 23:42:12 +0000 | [diff] [blame] | 751 | data.showHeader = 1; |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 752 | data.mode = MODE_Column; |
drh | d888544 | 2004-03-17 23:42:12 +0000 | [diff] [blame] | 753 | data.colWidth[0] = 3; |
| 754 | data.colWidth[1] = 15; |
| 755 | data.colWidth[2] = 58; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 756 | sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 757 | if( zErrMsg ){ |
| 758 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 759 | sqlite3_free(zErrMsg); |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 760 | } |
| 761 | }else |
| 762 | |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 763 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
| 764 | char *zErrMsg = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 765 | open_db(p); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 766 | fprintf(p->out, "BEGIN TRANSACTION;\n"); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 767 | if( nArg==1 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 768 | sqlite3_exec(p->db, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 769 | "SELECT name, type, sql FROM sqlite_master " |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 770 | "WHERE type!='meta' AND sql NOT NULL " |
drh | fc6cdfe | 2002-04-13 23:42:24 +0000 | [diff] [blame] | 771 | "ORDER BY substr(type,2,1), name", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 772 | dump_callback, p, &zErrMsg |
| 773 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 774 | }else{ |
| 775 | int i; |
| 776 | for(i=1; i<nArg && zErrMsg==0; i++){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 777 | sqlite3_exec_printf(p->db, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 778 | "SELECT name, type, sql FROM sqlite_master " |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 779 | "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL " |
drh | fc6cdfe | 2002-04-13 23:42:24 +0000 | [diff] [blame] | 780 | "ORDER BY substr(type,2,1), name", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 781 | dump_callback, p, &zErrMsg, azArg[i] |
| 782 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | if( zErrMsg ){ |
| 786 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 787 | sqlite3_free(zErrMsg); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 788 | }else{ |
| 789 | fprintf(p->out, "COMMIT;\n"); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 790 | } |
| 791 | }else |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 792 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 793 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){ |
| 794 | int j; |
| 795 | char *z = azArg[1]; |
| 796 | int val = atoi(azArg[1]); |
| 797 | for(j=0; z[j]; j++){ |
| 798 | if( isupper(z[j]) ) z[j] = tolower(z[j]); |
| 799 | } |
| 800 | if( strcmp(z,"on")==0 ){ |
| 801 | val = 1; |
| 802 | }else if( strcmp(z,"yes")==0 ){ |
| 803 | val = 1; |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 804 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 805 | p->echoOn = val; |
| 806 | }else |
| 807 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 808 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 809 | rc = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 810 | }else |
| 811 | |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 812 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 813 | int j; |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 814 | char *z = nArg>=2 ? azArg[1] : "1"; |
| 815 | int val = atoi(z); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 816 | for(j=0; z[j]; j++){ |
| 817 | if( isupper(z[j]) ) z[j] = tolower(z[j]); |
| 818 | } |
| 819 | if( strcmp(z,"on")==0 ){ |
| 820 | val = 1; |
| 821 | }else if( strcmp(z,"yes")==0 ){ |
| 822 | val = 1; |
| 823 | } |
| 824 | if(val == 1) { |
| 825 | if(!p->explainPrev.valid) { |
| 826 | p->explainPrev.valid = 1; |
| 827 | p->explainPrev.mode = p->mode; |
| 828 | p->explainPrev.showHeader = p->showHeader; |
| 829 | memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth)); |
| 830 | } |
| 831 | /* We could put this code under the !p->explainValid |
| 832 | ** condition so that it does not execute if we are already in |
| 833 | ** explain mode. However, always executing it allows us an easy |
| 834 | ** was to reset to explain mode in case the user previously |
| 835 | ** did an .explain followed by a .width, .mode or .header |
| 836 | ** command. |
| 837 | */ |
| 838 | p->mode = MODE_Column; |
| 839 | p->showHeader = 1; |
| 840 | memset(p->colWidth,0,ArraySize(p->colWidth)); |
| 841 | p->colWidth[0] = 4; |
| 842 | p->colWidth[1] = 12; |
| 843 | p->colWidth[2] = 10; |
| 844 | p->colWidth[3] = 10; |
| 845 | p->colWidth[4] = 35; |
| 846 | }else if (p->explainPrev.valid) { |
| 847 | p->explainPrev.valid = 0; |
| 848 | p->mode = p->explainPrev.mode; |
| 849 | p->showHeader = p->explainPrev.showHeader; |
| 850 | memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth)); |
| 851 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 852 | }else |
| 853 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 854 | if( c=='h' && (strncmp(azArg[0], "header", n)==0 |
| 855 | || |
| 856 | strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 857 | int j; |
| 858 | char *z = azArg[1]; |
| 859 | int val = atoi(azArg[1]); |
| 860 | for(j=0; z[j]; j++){ |
| 861 | if( isupper(z[j]) ) z[j] = tolower(z[j]); |
| 862 | } |
| 863 | if( strcmp(z,"on")==0 ){ |
| 864 | val = 1; |
| 865 | }else if( strcmp(z,"yes")==0 ){ |
| 866 | val = 1; |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 867 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 868 | p->showHeader = val; |
| 869 | }else |
| 870 | |
| 871 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
| 872 | fprintf(stderr,zHelp); |
| 873 | }else |
| 874 | |
| 875 | if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){ |
| 876 | struct callback_data data; |
| 877 | char *zErrMsg = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 878 | open_db(p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 879 | memcpy(&data, p, sizeof(data)); |
| 880 | data.showHeader = 0; |
| 881 | data.mode = MODE_List; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 882 | sqlite3_exec_printf(p->db, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 883 | "SELECT name FROM sqlite_master " |
| 884 | "WHERE type='index' AND tbl_name LIKE '%q' " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 885 | "UNION ALL " |
| 886 | "SELECT name FROM sqlite_temp_master " |
| 887 | "WHERE type='index' AND tbl_name LIKE '%q' " |
| 888 | "ORDER BY 1", |
| 889 | callback, &data, &zErrMsg, azArg[1], azArg[1] |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 890 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 891 | if( zErrMsg ){ |
| 892 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 893 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 894 | } |
| 895 | }else |
| 896 | |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 897 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 898 | int n2 = strlen(azArg[1]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 899 | if( strncmp(azArg[1],"line",n2)==0 |
| 900 | || |
| 901 | strncmp(azArg[1],"lines",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 902 | p->mode = MODE_Line; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 903 | }else if( strncmp(azArg[1],"column",n2)==0 |
| 904 | || |
| 905 | strncmp(azArg[1],"columns",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 906 | p->mode = MODE_Column; |
| 907 | }else if( strncmp(azArg[1],"list",n2)==0 ){ |
| 908 | p->mode = MODE_List; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 909 | }else if( strncmp(azArg[1],"html",n2)==0 ){ |
| 910 | p->mode = MODE_Html; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 911 | }else if( strncmp(azArg[1],"insert",n2)==0 ){ |
| 912 | p->mode = MODE_Insert; |
| 913 | if( nArg>=3 ){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 914 | set_table_name(p, azArg[2]); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 915 | }else{ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 916 | set_table_name(p, "table"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 917 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 918 | }else { |
| 919 | fprintf(stderr,"mode should be on of: column html insert line list\n"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 920 | } |
| 921 | }else |
| 922 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 923 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) { |
| 924 | sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]); |
| 925 | }else |
| 926 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 927 | if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){ |
| 928 | if( p->out!=stdout ){ |
| 929 | fclose(p->out); |
| 930 | } |
| 931 | if( strcmp(azArg[1],"stdout")==0 ){ |
| 932 | p->out = stdout; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 933 | strcpy(p->outfile,"stdout"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 934 | }else{ |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 935 | p->out = fopen(azArg[1], "wb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 936 | if( p->out==0 ){ |
| 937 | fprintf(stderr,"can't write to \"%s\"\n", azArg[1]); |
| 938 | p->out = stdout; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 939 | } else { |
| 940 | strcpy(p->outfile,azArg[1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | }else |
| 944 | |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 945 | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 946 | if( nArg >= 2) { |
| 947 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 948 | } |
| 949 | if( nArg >= 3) { |
| 950 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 951 | } |
| 952 | }else |
| 953 | |
| 954 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
drh | f73287c | 2004-02-25 02:25:37 +0000 | [diff] [blame] | 955 | rc = 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 956 | }else |
| 957 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 958 | if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){ |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 959 | FILE *alt = fopen(azArg[1], "rb"); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 960 | if( alt==0 ){ |
| 961 | fprintf(stderr,"can't open \"%s\"\n", azArg[1]); |
| 962 | }else{ |
| 963 | process_input(p, alt); |
| 964 | fclose(alt); |
| 965 | } |
| 966 | }else |
| 967 | |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 968 | #ifdef SQLITE_HAS_CODEC |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 969 | if( c=='r' && strncmp(azArg[0],"rekey", n)==0 && nArg==4 ){ |
| 970 | char *zOld = p->zKey; |
| 971 | if( zOld==0 ) zOld = ""; |
| 972 | if( strcmp(azArg[1],zOld) ){ |
| 973 | fprintf(stderr,"old key is incorrect\n"); |
| 974 | }else if( strcmp(azArg[2], azArg[3]) ){ |
| 975 | fprintf(stderr,"2nd copy of new key does not match the 1st\n"); |
| 976 | }else{ |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 977 | sqlite3_free(p->zKey); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 978 | p->zKey = sqlite3_mprintf("%s", azArg[2]); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 979 | sqlite_rekey(p->db, p->zKey, strlen(p->zKey)); |
| 980 | } |
| 981 | }else |
| 982 | #endif |
| 983 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 984 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
| 985 | struct callback_data data; |
| 986 | char *zErrMsg = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 987 | open_db(p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 988 | memcpy(&data, p, sizeof(data)); |
| 989 | data.showHeader = 0; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 990 | data.mode = MODE_Semi; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 991 | if( nArg>1 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 992 | extern int sqlite3StrICmp(const char*,const char*); |
| 993 | if( sqlite3StrICmp(azArg[1],"sqlite_master")==0 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 994 | char *new_argv[2], *new_colv[2]; |
| 995 | new_argv[0] = "CREATE TABLE sqlite_master (\n" |
| 996 | " type text,\n" |
| 997 | " name text,\n" |
| 998 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 999 | " rootpage integer,\n" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1000 | " sql text\n" |
| 1001 | ")"; |
| 1002 | new_argv[1] = 0; |
| 1003 | new_colv[0] = "sql"; |
| 1004 | new_colv[1] = 0; |
| 1005 | callback(&data, 1, new_argv, new_colv); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1006 | }else if( sqlite3StrICmp(azArg[1],"sqlite_temp_master")==0 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1007 | char *new_argv[2], *new_colv[2]; |
| 1008 | new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" |
| 1009 | " type text,\n" |
| 1010 | " name text,\n" |
| 1011 | " tbl_name text,\n" |
| 1012 | " rootpage integer,\n" |
| 1013 | " sql text\n" |
| 1014 | ")"; |
| 1015 | new_argv[1] = 0; |
| 1016 | new_colv[0] = "sql"; |
| 1017 | new_colv[1] = 0; |
| 1018 | callback(&data, 1, new_argv, new_colv); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1019 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1020 | sqlite3_exec_printf(p->db, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1021 | "SELECT sql FROM " |
| 1022 | " (SELECT * FROM sqlite_master UNION ALL" |
| 1023 | " SELECT * FROM sqlite_temp_master) " |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1024 | "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1025 | "ORDER BY substr(type,2,1), name", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1026 | callback, &data, &zErrMsg, azArg[1]); |
| 1027 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1028 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1029 | sqlite3_exec(p->db, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1030 | "SELECT sql FROM " |
| 1031 | " (SELECT * FROM sqlite_master UNION ALL" |
| 1032 | " SELECT * FROM sqlite_temp_master) " |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1033 | "WHERE type!='meta' AND sql NOTNULL " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1034 | "ORDER BY substr(type,2,1), name", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1035 | callback, &data, &zErrMsg |
| 1036 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1037 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1038 | if( zErrMsg ){ |
| 1039 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1040 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1041 | } |
| 1042 | }else |
| 1043 | |
| 1044 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){ |
| 1045 | sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]); |
| 1046 | }else |
| 1047 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1048 | if( c=='s' && strncmp(azArg[0], "show", n)==0){ |
| 1049 | int i; |
| 1050 | fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1051 | fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off"); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 1052 | fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1053 | fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]); |
| 1054 | fprintf(p->out,"%9.9s: %s\n","nullvalue", p->nullvalue); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1055 | fprintf(p->out,"%9.9s: %s\n","output", |
| 1056 | strlen(p->outfile) ? p->outfile : "stdout"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1057 | fprintf(p->out,"%9.9s: %s\n","separator", p->separator); |
| 1058 | fprintf(p->out,"%9.9s: ","width"); |
| 1059 | for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
| 1060 | fprintf(p->out,"%d ",p->colWidth[i]); |
| 1061 | } |
| 1062 | fprintf(p->out,"\n\n"); |
| 1063 | }else |
| 1064 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1065 | if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1066 | char **azResult; |
| 1067 | int nRow, rc; |
| 1068 | char *zErrMsg; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1069 | open_db(p); |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1070 | if( nArg==1 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1071 | rc = sqlite3_get_table(p->db, |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1072 | "SELECT name FROM sqlite_master " |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1073 | "WHERE type IN ('table','view') " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1074 | "UNION ALL " |
| 1075 | "SELECT name FROM sqlite_temp_master " |
| 1076 | "WHERE type IN ('table','view') " |
| 1077 | "ORDER BY 1", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1078 | &azResult, &nRow, 0, &zErrMsg |
| 1079 | ); |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1080 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1081 | rc = sqlite3_get_table_printf(p->db, |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1082 | "SELECT name FROM sqlite_master " |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 1083 | "WHERE type IN ('table','view') AND name LIKE '%%%q%%' " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1084 | "UNION ALL " |
| 1085 | "SELECT name FROM sqlite_temp_master " |
| 1086 | "WHERE type IN ('table','view') AND name LIKE '%%%q%%' " |
| 1087 | "ORDER BY 1", |
| 1088 | &azResult, &nRow, 0, &zErrMsg, azArg[1], azArg[1] |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 1089 | ); |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 1090 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1091 | if( zErrMsg ){ |
| 1092 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1093 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1094 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1095 | if( rc==SQLITE_OK ){ |
| 1096 | int len, maxlen = 0; |
| 1097 | int i, j; |
| 1098 | int nPrintCol, nPrintRow; |
| 1099 | for(i=1; i<=nRow; i++){ |
| 1100 | if( azResult[i]==0 ) continue; |
| 1101 | len = strlen(azResult[i]); |
| 1102 | if( len>maxlen ) maxlen = len; |
| 1103 | } |
| 1104 | nPrintCol = 80/(maxlen+2); |
| 1105 | if( nPrintCol<1 ) nPrintCol = 1; |
| 1106 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 1107 | for(i=0; i<nPrintRow; i++){ |
| 1108 | for(j=i+1; j<=nRow; j+=nPrintRow){ |
| 1109 | char *zSp = j<=nPrintRow ? "" : " "; |
| 1110 | printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : ""); |
| 1111 | } |
| 1112 | printf("\n"); |
| 1113 | } |
| 1114 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1115 | sqlite3_free_table(azResult); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1116 | }else |
| 1117 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1118 | if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1119 | open_db(p); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1120 | sqlite3_busy_timeout(p->db, atoi(azArg[1])); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1121 | }else |
| 1122 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1123 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
| 1124 | int j; |
| 1125 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
| 1126 | p->colWidth[j-1] = atoi(azArg[j]); |
| 1127 | } |
| 1128 | }else |
| 1129 | |
| 1130 | { |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1131 | fprintf(stderr, "unknown command or invalid arguments: " |
| 1132 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1133 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1134 | |
| 1135 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1138 | /* |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 1139 | ** Return TRUE if the last non-whitespace character in z[] is a semicolon. |
| 1140 | ** z[] is N characters long. |
| 1141 | */ |
| 1142 | static int _ends_with_semicolon(const char *z, int N){ |
| 1143 | while( N>0 && isspace(z[N-1]) ){ N--; } |
| 1144 | return N>0 && z[N-1]==';'; |
| 1145 | } |
| 1146 | |
| 1147 | /* |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 1148 | ** Test to see if a line consists entirely of whitespace. |
| 1149 | */ |
| 1150 | static int _all_whitespace(const char *z){ |
| 1151 | for(; *z; z++){ |
| 1152 | if( isspace(*z) ) continue; |
| 1153 | if( *z=='/' && z[1]=='*' ){ |
| 1154 | z += 2; |
| 1155 | while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
| 1156 | if( *z==0 ) return 0; |
| 1157 | z++; |
| 1158 | continue; |
| 1159 | } |
| 1160 | if( *z=='-' && z[1]=='-' ){ |
| 1161 | z += 2; |
| 1162 | while( *z && *z!='\n' ){ z++; } |
| 1163 | if( *z==0 ) return 1; |
| 1164 | continue; |
| 1165 | } |
| 1166 | return 0; |
| 1167 | } |
| 1168 | return 1; |
| 1169 | } |
| 1170 | |
| 1171 | /* |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 1172 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 1173 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 1174 | ** as is the Oracle "/". |
| 1175 | */ |
| 1176 | static int _is_command_terminator(const char *zLine){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1177 | extern int sqlite3StrNICmp(const char*,const char*,int); |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 1178 | while( isspace(*zLine) ){ zLine++; }; |
| 1179 | if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1180 | if( sqlite3StrNICmp(zLine,"go",2)==0 && _all_whitespace(&zLine[2]) ){ |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 1181 | return 1; /* SQL Server */ |
| 1182 | } |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1187 | ** Read input from *in and process it. If *in==0 then input |
| 1188 | ** is interactive - the user is typing it it. Otherwise, input |
| 1189 | ** is coming from a file or device. A prompt is issued and history |
| 1190 | ** is saved only if input is interactive. An interrupt signal will |
| 1191 | ** cause this routine to exit immediately, unless input is interactive. |
| 1192 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1193 | static void process_input(struct callback_data *p, FILE *in){ |
| 1194 | char *zLine; |
| 1195 | char *zSql = 0; |
| 1196 | int nSql = 0; |
| 1197 | char *zErrMsg; |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 1198 | int rc; |
drh | 41e941d | 2002-04-04 15:10:12 +0000 | [diff] [blame] | 1199 | while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1200 | if( seenInterrupt ){ |
| 1201 | if( in!=0 ) break; |
| 1202 | seenInterrupt = 0; |
| 1203 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1204 | if( p->echoOn ) printf("%s\n", zLine); |
drh | f817b6b | 2003-06-16 00:16:41 +0000 | [diff] [blame] | 1205 | if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue; |
drh | 2af0b2d | 2002-02-21 02:25:02 +0000 | [diff] [blame] | 1206 | if( zLine && zLine[0]=='.' && nSql==0 ){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1207 | int rc = do_meta_command(zLine, p); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1208 | free(zLine); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1209 | if( rc ) break; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1210 | continue; |
| 1211 | } |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 1212 | if( _is_command_terminator(zLine) ){ |
| 1213 | strcpy(zLine,";"); |
| 1214 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1215 | if( zSql==0 ){ |
| 1216 | int i; |
| 1217 | for(i=0; zLine[i] && isspace(zLine[i]); i++){} |
| 1218 | if( zLine[i]!=0 ){ |
| 1219 | nSql = strlen(zLine); |
| 1220 | zSql = malloc( nSql+1 ); |
| 1221 | strcpy(zSql, zLine); |
| 1222 | } |
| 1223 | }else{ |
| 1224 | int len = strlen(zLine); |
| 1225 | zSql = realloc( zSql, nSql + len + 2 ); |
| 1226 | if( zSql==0 ){ |
| 1227 | fprintf(stderr,"%s: out of memory!\n", Argv0); |
| 1228 | exit(1); |
| 1229 | } |
| 1230 | strcpy(&zSql[nSql++], "\n"); |
| 1231 | strcpy(&zSql[nSql], zLine); |
| 1232 | nSql += len; |
| 1233 | } |
| 1234 | free(zLine); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1235 | if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite3_complete(zSql) ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1236 | p->cnt = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1237 | open_db(p); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1238 | rc = sqlite3_exec(p->db, zSql, callback, p, &zErrMsg); |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 1239 | if( rc || zErrMsg ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1240 | if( in!=0 && !p->echoOn ) printf("%s\n",zSql); |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 1241 | if( zErrMsg!=0 ){ |
| 1242 | printf("SQL error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 1243 | sqlite3_free(zErrMsg); |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 1244 | zErrMsg = 0; |
| 1245 | }else{ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame^] | 1246 | printf("SQL error: %s\n", sqlite3_errmsg(p->db)); |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 1247 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1248 | } |
| 1249 | free(zSql); |
| 1250 | zSql = 0; |
| 1251 | nSql = 0; |
| 1252 | } |
| 1253 | } |
| 1254 | if( zSql ){ |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 1255 | if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1256 | free(zSql); |
| 1257 | } |
| 1258 | } |
| 1259 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1260 | /* |
| 1261 | ** Return a pathname which is the user's home directory. A |
| 1262 | ** 0 return indicates an error of some kind. Space to hold the |
| 1263 | ** resulting string is obtained from malloc(). The calling |
| 1264 | ** function should free the result. |
| 1265 | */ |
| 1266 | static char *find_home_dir(void){ |
| 1267 | char *home_dir = NULL; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1268 | |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 1269 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__) |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1270 | struct passwd *pwent; |
| 1271 | uid_t uid = getuid(); |
drh | bd842ba | 2002-08-21 11:26:41 +0000 | [diff] [blame] | 1272 | if( (pwent=getpwuid(uid)) != NULL) { |
| 1273 | home_dir = pwent->pw_dir; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1274 | } |
| 1275 | #endif |
| 1276 | |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 1277 | #ifdef __MACOS__ |
| 1278 | char home_path[_MAX_PATH+1]; |
| 1279 | home_dir = getcwd(home_path, _MAX_PATH); |
| 1280 | #endif |
| 1281 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1282 | if (!home_dir) { |
| 1283 | home_dir = getenv("HOME"); |
| 1284 | if (!home_dir) { |
| 1285 | home_dir = getenv("HOMEPATH"); /* Windows? */ |
| 1286 | } |
| 1287 | } |
| 1288 | |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 1289 | #if defined(_WIN32) || defined(WIN32) |
| 1290 | if (!home_dir) { |
| 1291 | home_dir = "c:"; |
| 1292 | } |
| 1293 | #endif |
| 1294 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1295 | if( home_dir ){ |
| 1296 | char *z = malloc( strlen(home_dir)+1 ); |
| 1297 | if( z ) strcpy(z, home_dir); |
| 1298 | home_dir = z; |
| 1299 | } |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 1300 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1301 | return home_dir; |
| 1302 | } |
| 1303 | |
| 1304 | /* |
| 1305 | ** Read input from the file given by sqliterc_override. Or if that |
| 1306 | ** parameter is NULL, take input from ~/.sqliterc |
| 1307 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1308 | static void process_sqliterc( |
| 1309 | struct callback_data *p, /* Configuration data */ |
| 1310 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 1311 | ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1312 | char *home_dir = NULL; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1313 | const char *sqliterc = sqliterc_override; |
| 1314 | char *zBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1315 | FILE *in = NULL; |
| 1316 | |
| 1317 | if (sqliterc == NULL) { |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1318 | home_dir = find_home_dir(); |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 1319 | if( home_dir==0 ){ |
| 1320 | fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0); |
| 1321 | return; |
| 1322 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1323 | zBuf = malloc(strlen(home_dir) + 15); |
| 1324 | if( zBuf==0 ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1325 | fprintf(stderr,"%s: out of memory!\n", Argv0); |
| 1326 | exit(1); |
| 1327 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1328 | sprintf(zBuf,"%s/.sqliterc",home_dir); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1329 | free(home_dir); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1330 | sqliterc = (const char*)zBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1331 | } |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 1332 | in = fopen(sqliterc,"rb"); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1333 | if( in ){ |
| 1334 | if( isatty(fileno(stdout)) ){ |
| 1335 | printf("Loading resources from %s\n",sqliterc); |
| 1336 | } |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1337 | process_input(p,in); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 1338 | fclose(in); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1339 | } |
| 1340 | return; |
| 1341 | } |
| 1342 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1343 | /* |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1344 | ** Show available command line options |
| 1345 | */ |
| 1346 | static const char zOptions[] = |
| 1347 | " -init filename read/process named file\n" |
| 1348 | " -echo print commands before execution\n" |
| 1349 | " -[no]header turn headers on or off\n" |
| 1350 | " -column set output mode to 'column'\n" |
| 1351 | " -html set output mode to HTML\n" |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 1352 | #ifdef SQLITE_HAS_CODEC |
drh | 57ced91 | 2004-02-10 02:57:59 +0000 | [diff] [blame] | 1353 | " -key KEY encryption key\n" |
| 1354 | #endif |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1355 | " -line set output mode to 'line'\n" |
| 1356 | " -list set output mode to 'list'\n" |
| 1357 | " -separator 'x' set output field separator (|)\n" |
| 1358 | " -nullvalue 'text' set text string for NULL values\n" |
| 1359 | " -version show SQLite version\n" |
| 1360 | " -help show this text, also show dot-commands\n" |
| 1361 | ; |
| 1362 | static void usage(int showDetail){ |
| 1363 | fprintf(stderr, "Usage: %s [OPTIONS] FILENAME [SQL]\n", Argv0); |
| 1364 | if( showDetail ){ |
| 1365 | fprintf(stderr, "Options are:\n%s", zOptions); |
| 1366 | }else{ |
| 1367 | fprintf(stderr, "Use the -help option for additional information\n"); |
| 1368 | } |
| 1369 | exit(1); |
| 1370 | } |
| 1371 | |
| 1372 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1373 | ** Initialize the state information in data |
| 1374 | */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1375 | void main_init(struct callback_data *data) { |
| 1376 | memset(data, 0, sizeof(*data)); |
| 1377 | data->mode = MODE_List; |
| 1378 | strcpy(data->separator,"|"); |
| 1379 | data->showHeader = 0; |
| 1380 | strcpy(mainPrompt,"sqlite> "); |
| 1381 | strcpy(continuePrompt," ...> "); |
| 1382 | } |
| 1383 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1384 | int main(int argc, char **argv){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1385 | char *zErrMsg = 0; |
| 1386 | struct callback_data data; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1387 | const char *zInitFile = 0; |
| 1388 | char *zFirstCmd = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1389 | int i; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1390 | extern int sqlite3OsFileExists(const char*); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1391 | |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 1392 | #ifdef __MACOS__ |
| 1393 | argc = ccommand(&argv); |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 1394 | #endif |
| 1395 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1396 | Argv0 = argv[0]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1397 | main_init(&data); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1398 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1399 | /* Make sure we have a valid signal handler early, before anything |
| 1400 | ** else is done. |
| 1401 | */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1402 | #ifdef SIGINT |
| 1403 | signal(SIGINT, interrupt_handler); |
| 1404 | #endif |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1405 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1406 | /* Do an initial pass through the command-line argument to locate |
| 1407 | ** the name of the database file, the name of the initialization file, |
| 1408 | ** and the first command to execute. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1409 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1410 | for(i=1; i<argc-1; i++){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1411 | if( argv[i][0]!='-' ) break; |
| 1412 | if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){ |
| 1413 | i++; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1414 | }else if( strcmp(argv[i],"-init")==0 ){ |
| 1415 | i++; |
| 1416 | zInitFile = argv[i]; |
| 1417 | }else if( strcmp(argv[i],"-key")==0 ){ |
| 1418 | i++; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1419 | data.zKey = sqlite3_mprintf("%s",argv[i]); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1420 | } |
| 1421 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1422 | if( i<argc ){ |
| 1423 | data.zDbFilename = argv[i++]; |
| 1424 | }else{ |
| 1425 | data.zDbFilename = ":memory:"; |
| 1426 | } |
| 1427 | if( i<argc ){ |
| 1428 | zFirstCmd = argv[i++]; |
| 1429 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1430 | data.out = stdout; |
| 1431 | |
| 1432 | /* Go ahead and open the database file if it already exists. If the |
| 1433 | ** file does not exist, delay opening it. This prevents empty database |
| 1434 | ** files from being created if a user mistypes the database name argument |
| 1435 | ** to the sqlite command-line tool. |
| 1436 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1437 | if( sqlite3OsFileExists(data.zDbFilename) ){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1438 | open_db(&data); |
| 1439 | } |
| 1440 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1441 | /* Process the initialization file if there is one. If no -init option |
| 1442 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 1443 | ** try to process it. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1444 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1445 | process_sqliterc(&data,zInitFile); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1446 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1447 | /* Make a second pass through the command-line argument and set |
| 1448 | ** options. This second pass is delayed until after the initialization |
| 1449 | ** file is processed so that the command-line arguments will override |
| 1450 | ** settings in the initialization file. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1451 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1452 | for(i=1; i<argc && argv[i][0]=='-'; i++){ |
| 1453 | char *z = argv[i]; |
| 1454 | if( strcmp(z,"-init")==0 || strcmp(z,"-key")==0 ){ |
| 1455 | i++; |
| 1456 | }else if( strcmp(z,"-html")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1457 | data.mode = MODE_Html; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1458 | }else if( strcmp(z,"-list")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1459 | data.mode = MODE_List; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1460 | }else if( strcmp(z,"-line")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1461 | data.mode = MODE_Line; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1462 | }else if( strcmp(z,"-column")==0 ){ |
drh | 8b32e17 | 2002-04-08 02:42:57 +0000 | [diff] [blame] | 1463 | data.mode = MODE_Column; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1464 | }else if( strcmp(z,"-separator")==0 ){ |
| 1465 | i++; |
| 1466 | sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[i]); |
| 1467 | }else if( strcmp(z,"-nullvalue")==0 ){ |
| 1468 | i++; |
| 1469 | sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[i]); |
| 1470 | }else if( strcmp(z,"-header")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1471 | data.showHeader = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1472 | }else if( strcmp(z,"-noheader")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1473 | data.showHeader = 0; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1474 | }else if( strcmp(z,"-echo")==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1475 | data.echoOn = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1476 | }else if( strcmp(z,"-version")==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1477 | printf("%s\n", sqlite3_version); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1478 | return 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1479 | }else if( strcmp(z,"-help")==0 ){ |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1480 | usage(1); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1481 | }else{ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1482 | fprintf(stderr,"%s: unknown option: %s\n", Argv0, z); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 1483 | fprintf(stderr,"Use -help for a list of options.\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1484 | return 1; |
| 1485 | } |
| 1486 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1487 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1488 | if( zFirstCmd ){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1489 | /* Run just the command that follows the database name |
| 1490 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 1491 | if( zFirstCmd[0]=='.' ){ |
| 1492 | do_meta_command(zFirstCmd, &data); |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 1493 | exit(0); |
| 1494 | }else{ |
| 1495 | int rc; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1496 | open_db(&data); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1497 | rc = sqlite3_exec(data.db, zFirstCmd, callback, &data, &zErrMsg); |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 1498 | if( rc!=0 && zErrMsg!=0 ){ |
| 1499 | fprintf(stderr,"SQL error: %s\n", zErrMsg); |
| 1500 | exit(1); |
| 1501 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1502 | } |
| 1503 | }else{ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1504 | /* Run commands received from standard input |
| 1505 | */ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 1506 | if( isatty(fileno(stdout)) && isatty(fileno(stdin)) ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1507 | char *zHome; |
| 1508 | char *zHistory = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1509 | printf( |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 1510 | "SQLite version %s\n" |
| 1511 | "Enter \".help\" for instructions\n", |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1512 | sqlite3_version |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1513 | ); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1514 | zHome = find_home_dir(); |
| 1515 | if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){ |
| 1516 | sprintf(zHistory,"%s/.sqlite_history", zHome); |
| 1517 | } |
| 1518 | if( zHistory ) read_history(zHistory); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1519 | process_input(&data, 0); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1520 | if( zHistory ){ |
| 1521 | stifle_history(100); |
| 1522 | write_history(zHistory); |
| 1523 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1524 | }else{ |
| 1525 | process_input(&data, stdin); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1526 | } |
| 1527 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1528 | set_table_name(&data, 0); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1529 | if( db ) sqlite3_close(db); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1530 | return 0; |
| 1531 | } |