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 | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame^] | 15 | ** $Id: shell.c,v 1.38 2001/11/09 22:41:45 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> |
| 20 | #include "sqlite.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 21 | #include <ctype.h> |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 22 | #ifdef OS_UNIX |
| 23 | # include <signal.h> |
| 24 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 25 | |
drh | 16e5955 | 2000-07-31 11:57:37 +0000 | [diff] [blame] | 26 | #if defined(HAVE_READLINE) && HAVE_READLINE==1 |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 27 | # include <readline/readline.h> |
| 28 | # include <readline/history.h> |
| 29 | #else |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 30 | # define readline(p) getline(p,stdin) |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 31 | # define add_history(X) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
| 34 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 35 | ** The following is the open SQLite database. We make a pointer |
| 36 | ** to this database a static variable so that it can be accessed |
| 37 | ** by the SIGINT handler to interrupt database processing. |
| 38 | */ |
| 39 | static sqlite *db = 0; |
| 40 | |
| 41 | /* |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 42 | ** This routine reads a line of text from standard input, stores |
| 43 | ** the text in memory obtained from malloc() and returns a pointer |
| 44 | ** to the text. NULL is returned at end of file, or if malloc() |
| 45 | ** fails. |
| 46 | ** |
| 47 | ** The interface is like "readline" but no command-line editing |
| 48 | ** is done. |
| 49 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 50 | static char *getline(char *zPrompt, FILE *in){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 51 | char *zLine; |
| 52 | int nLine; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 53 | int n; |
| 54 | int eol; |
| 55 | |
| 56 | if( zPrompt && *zPrompt ){ |
| 57 | printf("%s",zPrompt); |
| 58 | fflush(stdout); |
| 59 | } |
| 60 | nLine = 100; |
| 61 | zLine = malloc( nLine ); |
| 62 | if( zLine==0 ) return 0; |
| 63 | n = 0; |
| 64 | eol = 0; |
| 65 | while( !eol ){ |
| 66 | if( n+100>nLine ){ |
| 67 | nLine = nLine*2 + 100; |
| 68 | zLine = realloc(zLine, nLine); |
| 69 | if( zLine==0 ) return 0; |
| 70 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 71 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 72 | if( n==0 ){ |
| 73 | free(zLine); |
| 74 | return 0; |
| 75 | } |
| 76 | zLine[n] = 0; |
| 77 | eol = 1; |
| 78 | break; |
| 79 | } |
| 80 | while( zLine[n] ){ n++; } |
| 81 | if( n>0 && zLine[n-1]=='\n' ){ |
| 82 | n--; |
| 83 | zLine[n] = 0; |
| 84 | eol = 1; |
| 85 | } |
| 86 | } |
| 87 | zLine = realloc( zLine, n+1 ); |
| 88 | return zLine; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | ** Retrieve a single line of input text. "isatty" is true if text |
| 93 | ** is coming from a terminal. In that case, we issue a prompt and |
| 94 | ** attempt to use "readline" for command-line editing. If "isatty" |
| 95 | ** is false, use "getline" instead of "readline" and issue to prompt. |
| 96 | ** |
| 97 | ** zPrior is a string of prior text retrieved. If not the empty |
| 98 | ** string, then issue a continuation prompt. |
| 99 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 100 | static char *one_input_line(const char *zPrior, FILE *in){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 101 | char *zPrompt; |
| 102 | char *zResult; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 103 | if( in!=0 ){ |
| 104 | return getline(0, in); |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 105 | } |
| 106 | if( zPrior && zPrior[0] ){ |
| 107 | zPrompt = " ...> "; |
| 108 | }else{ |
| 109 | zPrompt = "sqlite> "; |
| 110 | } |
| 111 | zResult = readline(zPrompt); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 112 | if( zResult ) add_history(zResult); |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 113 | return zResult; |
| 114 | } |
| 115 | |
| 116 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 117 | ** An pointer to an instance of this structure is passed from |
| 118 | ** the main program to the callback. This is used to communicate |
| 119 | ** state and mode information. |
| 120 | */ |
| 121 | struct callback_data { |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 122 | sqlite *db; /* The database */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 123 | int echoOn; /* True to echo input commands */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 124 | int cnt; /* Number of records displayed so far */ |
| 125 | FILE *out; /* Write results here */ |
| 126 | int mode; /* An output mode setting */ |
| 127 | int showHeader; /* True to show column names in List or Column mode */ |
| 128 | int escape; /* Escape this character when in MODE_List */ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 129 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 130 | char separator[20]; /* Separator character for MODE_List */ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 131 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 132 | int actualWidth[100]; /* Actual width of each column */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | /* |
| 136 | ** These are the allowed modes. |
| 137 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 138 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 139 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 140 | #define MODE_List 2 /* One record per line with a separator */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 141 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 142 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 143 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | ** Number of elements in an array |
| 147 | */ |
| 148 | #define ArraySize(X) (sizeof(X)/sizeof(X[0])) |
| 149 | |
| 150 | /* |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 151 | ** Return TRUE if the string supplied is a number of some kinds. |
| 152 | */ |
| 153 | static int is_numeric(const char *z){ |
| 154 | int seen_digit = 0; |
| 155 | if( *z=='-' || *z=='+' ){ |
| 156 | z++; |
| 157 | } |
| 158 | while( isdigit(*z) ){ |
| 159 | seen_digit = 1; |
| 160 | z++; |
| 161 | } |
| 162 | if( seen_digit && *z=='.' ){ |
| 163 | z++; |
| 164 | while( isdigit(*z) ){ z++; } |
| 165 | } |
| 166 | if( seen_digit && (*z=='e' || *z=='E') |
| 167 | && (isdigit(z[1]) || ((z[1]=='-' || z[1]=='+') && isdigit(z[2]))) |
| 168 | ){ |
| 169 | z+=2; |
| 170 | while( isdigit(*z) ){ z++; } |
| 171 | } |
| 172 | return seen_digit && *z==0; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 177 | */ |
| 178 | static void output_quoted_string(FILE *out, const char *z){ |
| 179 | int i; |
| 180 | int nSingle = 0; |
| 181 | int nDouble = 0; |
| 182 | for(i=0; z[i]; i++){ |
| 183 | if( z[i]=='\'' ) nSingle++; |
| 184 | else if( z[i]=='"' ) nDouble++; |
| 185 | } |
| 186 | if( nSingle==0 ){ |
| 187 | fprintf(out,"'%s'",z); |
| 188 | }else if( nDouble==0 ){ |
| 189 | fprintf(out,"\"%s\"",z); |
| 190 | }else{ |
| 191 | fprintf(out,"'"); |
| 192 | while( *z ){ |
| 193 | for(i=0; z[i] && z[i]!='\''; i++){} |
| 194 | if( i==0 ){ |
| 195 | fprintf(out,"''"); |
| 196 | z++; |
| 197 | }else if( z[i]=='\'' ){ |
| 198 | fprintf(out,"%.*s''",i,z); |
| 199 | z += i+1; |
| 200 | }else{ |
| 201 | fprintf(out,"%s'",z); |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /* |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 209 | ** Output the given string with characters that are special to |
| 210 | ** HTML escaped. |
| 211 | */ |
| 212 | static void output_html_string(FILE *out, const char *z){ |
| 213 | int i; |
| 214 | while( *z ){ |
| 215 | for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){} |
| 216 | if( i>0 ){ |
| 217 | fprintf(out,"%.*s",i,z); |
| 218 | } |
| 219 | if( z[i]=='<' ){ |
| 220 | fprintf(out,"<"); |
| 221 | }else if( z[i]=='&' ){ |
| 222 | fprintf(out,"&"); |
| 223 | }else{ |
| 224 | break; |
| 225 | } |
| 226 | z += i + 1; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 231 | ** This routine runs when the user presses Ctrl-C |
| 232 | */ |
| 233 | static void interrupt_handler(int NotUsed){ |
| 234 | if( db ) sqlite_interrupt(db); |
| 235 | } |
| 236 | |
| 237 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 238 | ** This is the callback routine that the SQLite library |
| 239 | ** invokes for each row of a query result. |
| 240 | */ |
| 241 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 242 | int i; |
| 243 | struct callback_data *p = (struct callback_data*)pArg; |
| 244 | switch( p->mode ){ |
| 245 | case MODE_Line: { |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 246 | int w = 5; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 247 | if( azArg==0 ) break; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 248 | for(i=0; i<nArg; i++){ |
| 249 | int len = strlen(azCol[i]); |
| 250 | if( len>w ) w = len; |
| 251 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 252 | if( p->cnt++>0 ) fprintf(p->out,"\n"); |
| 253 | for(i=0; i<nArg; i++){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 254 | fprintf(p->out,"%*s = %s\n", w, azCol[i], azArg[i] ? azArg[i] : 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 255 | } |
| 256 | break; |
| 257 | } |
| 258 | case MODE_Column: { |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 259 | if( p->cnt++==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 260 | for(i=0; i<nArg; i++){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 261 | int w, n; |
| 262 | if( i<ArraySize(p->colWidth) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 263 | w = p->colWidth[i]; |
| 264 | }else{ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 265 | w = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 266 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 267 | if( w<=0 ){ |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 268 | w = strlen(azCol[i] ? azCol[i] : ""); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 269 | if( w<10 ) w = 10; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 270 | n = strlen(azArg && azArg[i] ? azArg[i] : ""); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 271 | if( w<n ) w = n; |
| 272 | } |
| 273 | if( i<ArraySize(p->actualWidth) ){ |
| 274 | p->actualWidth[i] = w; |
| 275 | } |
| 276 | if( p->showHeader ){ |
| 277 | fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " "); |
| 278 | } |
| 279 | } |
| 280 | if( p->showHeader ){ |
| 281 | for(i=0; i<nArg; i++){ |
| 282 | int w; |
| 283 | if( i<ArraySize(p->actualWidth) ){ |
| 284 | w = p->actualWidth[i]; |
| 285 | }else{ |
| 286 | w = 10; |
| 287 | } |
| 288 | fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------" |
| 289 | "----------------------------------------------------------", |
| 290 | i==nArg-1 ? "\n": " "); |
| 291 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 294 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 295 | for(i=0; i<nArg; i++){ |
| 296 | int w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 297 | if( i<ArraySize(p->actualWidth) ){ |
| 298 | w = p->actualWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 299 | }else{ |
| 300 | w = 10; |
| 301 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 302 | fprintf(p->out,"%-*.*s%s",w,w, |
| 303 | azArg[i] ? azArg[i] : "", i==nArg-1 ? "\n": " "); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 304 | } |
| 305 | break; |
| 306 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 307 | case MODE_Semi: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 308 | case MODE_List: { |
| 309 | if( p->cnt++==0 && p->showHeader ){ |
| 310 | for(i=0; i<nArg; i++){ |
| 311 | fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator); |
| 312 | } |
| 313 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 314 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 315 | for(i=0; i<nArg; i++){ |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 316 | char *z = azArg[i]; |
| 317 | if( z==0 ) z = ""; |
| 318 | while( *z ){ |
| 319 | int j; |
| 320 | for(j=0; z[j] && z[j]!=p->escape && z[j]!='\\'; j++){} |
| 321 | if( j>0 ){ |
| 322 | fprintf(p->out, "%.*s", j, z); |
| 323 | } |
| 324 | if( z[j] ){ |
| 325 | fprintf(p->out, "\\%c", z[j]); |
drh | 670f74f | 2000-06-07 02:04:22 +0000 | [diff] [blame] | 326 | z++; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 327 | } |
drh | 7375592 | 2000-06-07 01:33:42 +0000 | [diff] [blame] | 328 | z += j; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 329 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 330 | if( i<nArg-1 ){ |
| 331 | fprintf(p->out, "%s", p->separator); |
| 332 | }else if( p->mode==MODE_Semi ){ |
| 333 | fprintf(p->out, ";\n"); |
| 334 | }else{ |
| 335 | fprintf(p->out, "\n"); |
| 336 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 337 | } |
| 338 | break; |
| 339 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 340 | case MODE_Html: { |
| 341 | if( p->cnt++==0 && p->showHeader ){ |
| 342 | fprintf(p->out,"<TR>"); |
| 343 | for(i=0; i<nArg; i++){ |
| 344 | fprintf(p->out,"<TH>%s</TH>",azCol[i]); |
| 345 | } |
| 346 | fprintf(p->out,"</TR>\n"); |
| 347 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 348 | if( azArg==0 ) break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 349 | fprintf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 350 | for(i=0; i<nArg; i++){ |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 351 | fprintf(p->out,"<TD>"); |
| 352 | output_html_string(p->out, azArg[i] ? azArg[i] : ""); |
| 353 | fprintf(p->out,"</TD>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 354 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 355 | fprintf(p->out,"</TD></TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 356 | break; |
| 357 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 358 | case MODE_Insert: { |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 359 | if( azArg==0 ) break; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 360 | fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 361 | for(i=0; i<nArg; i++){ |
| 362 | char *zSep = i>0 ? ",": ""; |
| 363 | if( azArg[i]==0 ){ |
| 364 | fprintf(p->out,"%sNULL",zSep); |
| 365 | }else if( is_numeric(azArg[i]) ){ |
| 366 | fprintf(p->out,"%s%s",zSep, azArg[i]); |
| 367 | }else{ |
| 368 | if( zSep[0] ) fprintf(p->out,"%s",zSep); |
| 369 | output_quoted_string(p->out, azArg[i]); |
| 370 | } |
| 371 | } |
| 372 | fprintf(p->out,");\n"); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 373 | break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 374 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 375 | } |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /* |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 380 | ** Set the destination table field of the callback_data structure to |
| 381 | ** the name of the table given. Escape any quote characters in the |
| 382 | ** table name. |
| 383 | */ |
| 384 | static void set_table_name(struct callback_data *p, const char *zName){ |
| 385 | int i, n; |
| 386 | int needQuote; |
| 387 | char *z; |
| 388 | |
| 389 | if( p->zDestTable ){ |
| 390 | free(p->zDestTable); |
| 391 | p->zDestTable = 0; |
| 392 | } |
| 393 | if( zName==0 ) return; |
| 394 | needQuote = !isalpha(*zName) && *zName!='_'; |
| 395 | for(i=n=0; zName[i]; i++, n++){ |
| 396 | if( !isalnum(zName[i]) && zName[i]!='_' ){ |
| 397 | needQuote = 1; |
| 398 | if( zName[i]=='\'' ) n++; |
| 399 | } |
| 400 | } |
| 401 | if( needQuote ) n += 2; |
| 402 | z = p->zDestTable = malloc( n+1 ); |
| 403 | if( z==0 ){ |
| 404 | fprintf(stderr,"Out of memory!\n"); |
| 405 | exit(1); |
| 406 | } |
| 407 | n = 0; |
| 408 | if( needQuote ) z[n++] = '\''; |
| 409 | for(i=0; zName[i]; i++){ |
| 410 | z[n++] = zName[i]; |
| 411 | if( zName[i]=='\'' ) z[n++] = '\''; |
| 412 | } |
| 413 | if( needQuote ) z[n++] = '\''; |
| 414 | z[n] = 0; |
| 415 | } |
| 416 | |
| 417 | /* |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 418 | ** This is a different callback routine used for dumping the database. |
| 419 | ** Each row received by this callback consists of a table name, |
| 420 | ** the table type ("index" or "table") and SQL to create the table. |
| 421 | ** This routine should print text sufficient to recreate the table. |
| 422 | */ |
| 423 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 424 | struct callback_data *p = (struct callback_data *)pArg; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 425 | if( nArg!=3 ) return 1; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 426 | fprintf(p->out, "%s;\n", azArg[2]); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 427 | if( strcmp(azArg[1],"table")==0 ){ |
| 428 | struct callback_data d2; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 429 | d2 = *p; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 430 | d2.mode = MODE_Insert; |
| 431 | d2.zDestTable = 0; |
| 432 | set_table_name(&d2, azArg[0]); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 433 | sqlite_exec_printf(p->db, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 434 | "SELECT * FROM '%q'", |
| 435 | callback, &d2, 0, azArg[0] |
| 436 | ); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 437 | set_table_name(&d2, 0); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 438 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 443 | ** Text of a help message |
| 444 | */ |
| 445 | static char zHelp[] = |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 446 | ".dump ?TABLE? ... Dump the database in an text format\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 447 | ".echo ON|OFF Turn command echo on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 448 | ".exit Exit this program\n" |
| 449 | ".explain Set output mode suitable for EXPLAIN\n" |
| 450 | ".header ON|OFF Turn display of headers on or off\n" |
| 451 | ".help Show this message\n" |
| 452 | ".indices TABLE Show names of all indices on TABLE\n" |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 453 | ".mode MODE Set mode to one of \"line\", \"column\", \n" |
| 454 | " \"insert\", \"list\", or \"html\"\n" |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 455 | ".mode insert TABLE Generate SQL insert statements for TABLE\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 456 | ".output FILENAME Send output to FILENAME\n" |
| 457 | ".output stdout Send output to the screen\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 458 | ".read FILENAME Execute SQL in FILENAME\n" |
| 459 | ".reindex ?TABLE? Rebuild indices\n" |
| 460 | /* ".rename OLD NEW Change the name of a table or index\n" */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 461 | ".schema ?TABLE? Show the CREATE statements\n" |
| 462 | ".separator STRING Change separator string for \"list\" mode\n" |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 463 | ".tables ?PATTERN? List names of tables matching a pattern\n" |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 464 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 465 | ".width NUM NUM ... Set column widths for \"column\" mode\n" |
| 466 | ; |
| 467 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 468 | /* Forward reference */ |
| 469 | static void process_input(struct callback_data *p, FILE *in); |
| 470 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 471 | /* |
| 472 | ** If an input line begins with "." then invoke this routine to |
| 473 | ** process that line. |
| 474 | */ |
| 475 | static void do_meta_command(char *zLine, sqlite *db, struct callback_data *p){ |
| 476 | int i = 1; |
| 477 | int nArg = 0; |
| 478 | int n, c; |
| 479 | char *azArg[50]; |
| 480 | |
| 481 | /* Parse the input line into tokens. |
| 482 | */ |
| 483 | while( zLine[i] && nArg<ArraySize(azArg) ){ |
| 484 | while( isspace(zLine[i]) ){ i++; } |
| 485 | if( zLine[i]=='\'' || zLine[i]=='"' ){ |
| 486 | int delim = zLine[i++]; |
| 487 | azArg[nArg++] = &zLine[i]; |
| 488 | while( zLine[i] && zLine[i]!=delim ){ i++; } |
| 489 | if( zLine[i]==delim ){ |
| 490 | zLine[i++] = 0; |
| 491 | } |
| 492 | }else{ |
| 493 | azArg[nArg++] = &zLine[i]; |
| 494 | while( zLine[i] && !isspace(zLine[i]) ){ i++; } |
| 495 | if( zLine[i] ) zLine[i++] = 0; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /* Process the input line. |
| 500 | */ |
| 501 | if( nArg==0 ) return; |
| 502 | n = strlen(azArg[0]); |
| 503 | c = azArg[0][0]; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 504 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
| 505 | char *zErrMsg = 0; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 506 | fprintf(p->out, "BEGIN TRANSACTION;\n"); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 507 | if( nArg==1 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 508 | sqlite_exec(db, |
| 509 | "SELECT name, type, sql FROM sqlite_master " |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 510 | "WHERE type!='meta' AND sql NOT NULL " |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 511 | "ORDER BY tbl_name, type DESC, name", |
| 512 | dump_callback, p, &zErrMsg |
| 513 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 514 | }else{ |
| 515 | int i; |
| 516 | for(i=1; i<nArg && zErrMsg==0; i++){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 517 | sqlite_exec_printf(db, |
| 518 | "SELECT name, type, sql FROM sqlite_master " |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 519 | "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL " |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 520 | "ORDER BY type DESC, name", |
| 521 | dump_callback, p, &zErrMsg, azArg[i] |
| 522 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 523 | |
| 524 | } |
| 525 | } |
| 526 | if( zErrMsg ){ |
| 527 | fprintf(stderr,"Error: %s\n", zErrMsg); |
| 528 | free(zErrMsg); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 529 | }else{ |
| 530 | fprintf(p->out, "COMMIT;\n"); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 531 | } |
| 532 | }else |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 533 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 534 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){ |
| 535 | int j; |
| 536 | char *z = azArg[1]; |
| 537 | int val = atoi(azArg[1]); |
| 538 | for(j=0; z[j]; j++){ |
| 539 | if( isupper(z[j]) ) z[j] = tolower(z[j]); |
| 540 | } |
| 541 | if( strcmp(z,"on")==0 ){ |
| 542 | val = 1; |
| 543 | }else if( strcmp(z,"yes")==0 ){ |
| 544 | val = 1; |
| 545 | } |
| 546 | p->echoOn = val; |
| 547 | }else |
| 548 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 549 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame^] | 550 | sqlite_close(db); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 551 | exit(0); |
| 552 | }else |
| 553 | |
| 554 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
| 555 | p->mode = MODE_Column; |
| 556 | p->showHeader = 1; |
| 557 | p->colWidth[0] = 4; |
| 558 | p->colWidth[1] = 12; |
| 559 | p->colWidth[2] = 5; |
| 560 | p->colWidth[3] = 5; |
| 561 | p->colWidth[4] = 40; |
| 562 | }else |
| 563 | |
| 564 | if( c=='h' && strncmp(azArg[0], "header", n)==0 && nArg>1 ){ |
| 565 | int j; |
| 566 | char *z = azArg[1]; |
| 567 | int val = atoi(azArg[1]); |
| 568 | for(j=0; z[j]; j++){ |
| 569 | if( isupper(z[j]) ) z[j] = tolower(z[j]); |
| 570 | } |
| 571 | if( strcmp(z,"on")==0 ){ |
| 572 | val = 1; |
| 573 | }else if( strcmp(z,"yes")==0 ){ |
| 574 | val = 1; |
| 575 | } |
| 576 | p->showHeader = val; |
| 577 | }else |
| 578 | |
| 579 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
| 580 | fprintf(stderr,zHelp); |
| 581 | }else |
| 582 | |
| 583 | if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){ |
| 584 | struct callback_data data; |
| 585 | char *zErrMsg = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 586 | memcpy(&data, p, sizeof(data)); |
| 587 | data.showHeader = 0; |
| 588 | data.mode = MODE_List; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 589 | sqlite_exec_printf(db, |
| 590 | "SELECT name FROM sqlite_master " |
| 591 | "WHERE type='index' AND tbl_name LIKE '%q' " |
| 592 | "ORDER BY name", |
| 593 | callback, &data, &zErrMsg, azArg[1] |
| 594 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 595 | if( zErrMsg ){ |
| 596 | fprintf(stderr,"Error: %s\n", zErrMsg); |
| 597 | free(zErrMsg); |
| 598 | } |
| 599 | }else |
| 600 | |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 601 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 602 | int n2 = strlen(azArg[1]); |
| 603 | if( strncmp(azArg[1],"line",n2)==0 ){ |
| 604 | p->mode = MODE_Line; |
| 605 | }else if( strncmp(azArg[1],"column",n2)==0 ){ |
| 606 | p->mode = MODE_Column; |
| 607 | }else if( strncmp(azArg[1],"list",n2)==0 ){ |
| 608 | p->mode = MODE_List; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 609 | }else if( strncmp(azArg[1],"html",n2)==0 ){ |
| 610 | p->mode = MODE_Html; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 611 | }else if( strncmp(azArg[1],"insert",n2)==0 ){ |
| 612 | p->mode = MODE_Insert; |
| 613 | if( nArg>=3 ){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 614 | set_table_name(p, azArg[2]); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 615 | }else{ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 616 | set_table_name(p, "table"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 617 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 618 | }else { |
| 619 | fprintf(stderr,"mode should be on of: column html insert line list\n"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 620 | } |
| 621 | }else |
| 622 | |
| 623 | if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){ |
| 624 | if( p->out!=stdout ){ |
| 625 | fclose(p->out); |
| 626 | } |
| 627 | if( strcmp(azArg[1],"stdout")==0 ){ |
| 628 | p->out = stdout; |
| 629 | }else{ |
| 630 | p->out = fopen(azArg[1], "w"); |
| 631 | if( p->out==0 ){ |
| 632 | fprintf(stderr,"can't write to \"%s\"\n", azArg[1]); |
| 633 | p->out = stdout; |
| 634 | } |
| 635 | } |
| 636 | }else |
| 637 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 638 | if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){ |
| 639 | FILE *alt = fopen(azArg[1], "r"); |
| 640 | if( alt==0 ){ |
| 641 | fprintf(stderr,"can't open \"%s\"\n", azArg[1]); |
| 642 | }else{ |
| 643 | process_input(p, alt); |
| 644 | fclose(alt); |
| 645 | } |
| 646 | }else |
| 647 | |
| 648 | if( c=='r' && strncmp(azArg[0], "reindex", n)==0 ){ |
| 649 | char **azResult; |
| 650 | int nRow, rc; |
| 651 | char *zErrMsg; |
| 652 | int i; |
| 653 | char *zSql; |
| 654 | if( nArg==1 ){ |
| 655 | rc = sqlite_get_table(db, |
| 656 | "SELECT name, sql FROM sqlite_master " |
| 657 | "WHERE type='index'", |
| 658 | &azResult, &nRow, 0, &zErrMsg |
| 659 | ); |
| 660 | }else{ |
| 661 | rc = sqlite_get_table_printf(db, |
| 662 | "SELECT name, sql FROM sqlite_master " |
| 663 | "WHERE type='index' AND tbl_name LIKE '%q'", |
| 664 | &azResult, &nRow, 0, &zErrMsg, azArg[1] |
| 665 | ); |
| 666 | } |
| 667 | for(i=1; rc==SQLITE_OK && i<=nRow; i++){ |
| 668 | extern char *sqlite_mprintf(const char *, ...); |
| 669 | zSql = sqlite_mprintf( |
| 670 | "DROP INDEX '%q';\n%s;\nVACUUM '%q';", |
| 671 | azResult[i*2], azResult[i*2+1], azResult[i*2]); |
| 672 | if( p->echoOn ) printf("%s\n", zSql); |
| 673 | rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg); |
| 674 | } |
| 675 | sqlite_free_table(azResult); |
| 676 | if( zErrMsg ){ |
| 677 | fprintf(stderr,"Error: %s\n", zErrMsg); |
| 678 | free(zErrMsg); |
| 679 | } |
| 680 | }else |
| 681 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 682 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
| 683 | struct callback_data data; |
| 684 | char *zErrMsg = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 685 | memcpy(&data, p, sizeof(data)); |
| 686 | data.showHeader = 0; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 687 | data.mode = MODE_Semi; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 688 | if( nArg>1 ){ |
drh | ff9821a | 2001-04-04 21:22:14 +0000 | [diff] [blame] | 689 | extern int sqliteStrICmp(const char*,const char*); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 690 | if( sqliteStrICmp(azArg[1],"sqlite_master")==0 ){ |
| 691 | char *new_argv[2], *new_colv[2]; |
| 692 | new_argv[0] = "CREATE TABLE sqlite_master (\n" |
| 693 | " type text,\n" |
| 694 | " name text,\n" |
| 695 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 696 | " rootpage integer,\n" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 697 | " sql text\n" |
| 698 | ")"; |
| 699 | new_argv[1] = 0; |
| 700 | new_colv[0] = "sql"; |
| 701 | new_colv[1] = 0; |
| 702 | callback(&data, 1, new_argv, new_colv); |
| 703 | }else{ |
| 704 | sqlite_exec_printf(db, |
| 705 | "SELECT sql FROM sqlite_master " |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 706 | "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL " |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 707 | "ORDER BY type DESC, name", |
| 708 | callback, &data, &zErrMsg, azArg[1]); |
| 709 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 710 | }else{ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 711 | sqlite_exec(db, |
| 712 | "SELECT sql FROM sqlite_master " |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 713 | "WHERE type!='meta' AND sql NOTNULL " |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 714 | "ORDER BY tbl_name, type DESC, name", |
| 715 | callback, &data, &zErrMsg |
| 716 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 717 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 718 | if( zErrMsg ){ |
| 719 | fprintf(stderr,"Error: %s\n", zErrMsg); |
| 720 | free(zErrMsg); |
| 721 | } |
| 722 | }else |
| 723 | |
| 724 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){ |
| 725 | sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]); |
| 726 | }else |
| 727 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 728 | if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 729 | char **azResult; |
| 730 | int nRow, rc; |
| 731 | char *zErrMsg; |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 732 | if( nArg==1 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 733 | rc = sqlite_get_table(db, |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 734 | "SELECT name FROM sqlite_master " |
| 735 | "WHERE type='table' " |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 736 | "ORDER BY name", |
| 737 | &azResult, &nRow, 0, &zErrMsg |
| 738 | ); |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 739 | }else{ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 740 | rc = sqlite_get_table_printf(db, |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 741 | "SELECT name FROM sqlite_master " |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 742 | "WHERE type='table' AND name LIKE '%%%q%%' " |
| 743 | "ORDER BY name", |
| 744 | &azResult, &nRow, 0, &zErrMsg, azArg[1] |
| 745 | ); |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 746 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 747 | if( zErrMsg ){ |
| 748 | fprintf(stderr,"Error: %s\n", zErrMsg); |
| 749 | free(zErrMsg); |
| 750 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 751 | if( rc==SQLITE_OK ){ |
| 752 | int len, maxlen = 0; |
| 753 | int i, j; |
| 754 | int nPrintCol, nPrintRow; |
| 755 | for(i=1; i<=nRow; i++){ |
| 756 | if( azResult[i]==0 ) continue; |
| 757 | len = strlen(azResult[i]); |
| 758 | if( len>maxlen ) maxlen = len; |
| 759 | } |
| 760 | nPrintCol = 80/(maxlen+2); |
| 761 | if( nPrintCol<1 ) nPrintCol = 1; |
| 762 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 763 | for(i=0; i<nPrintRow; i++){ |
| 764 | for(j=i+1; j<=nRow; j+=nPrintRow){ |
| 765 | char *zSp = j<=nPrintRow ? "" : " "; |
| 766 | printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : ""); |
| 767 | } |
| 768 | printf("\n"); |
| 769 | } |
| 770 | } |
| 771 | sqlite_free_table(azResult); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 772 | }else |
| 773 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 774 | if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){ |
| 775 | sqlite_busy_timeout(db, atoi(azArg[1])); |
| 776 | }else |
| 777 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 778 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
| 779 | int j; |
| 780 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
| 781 | p->colWidth[j-1] = atoi(azArg[j]); |
| 782 | } |
| 783 | }else |
| 784 | |
| 785 | { |
| 786 | fprintf(stderr, "unknown command: \"%s\". Enter \".help\" for help\n", |
| 787 | azArg[0]); |
| 788 | } |
| 789 | } |
| 790 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 791 | static char *Argv0; |
| 792 | static void process_input(struct callback_data *p, FILE *in){ |
| 793 | char *zLine; |
| 794 | char *zSql = 0; |
| 795 | int nSql = 0; |
| 796 | char *zErrMsg; |
| 797 | while( (zLine = one_input_line(zSql, in))!=0 ){ |
| 798 | if( p->echoOn ) printf("%s\n", zLine); |
| 799 | if( zLine && zLine[0]=='.' ){ |
| 800 | do_meta_command(zLine, db, p); |
| 801 | free(zLine); |
| 802 | continue; |
| 803 | } |
| 804 | if( zSql==0 ){ |
| 805 | int i; |
| 806 | for(i=0; zLine[i] && isspace(zLine[i]); i++){} |
| 807 | if( zLine[i]!=0 ){ |
| 808 | nSql = strlen(zLine); |
| 809 | zSql = malloc( nSql+1 ); |
| 810 | strcpy(zSql, zLine); |
| 811 | } |
| 812 | }else{ |
| 813 | int len = strlen(zLine); |
| 814 | zSql = realloc( zSql, nSql + len + 2 ); |
| 815 | if( zSql==0 ){ |
| 816 | fprintf(stderr,"%s: out of memory!\n", Argv0); |
| 817 | exit(1); |
| 818 | } |
| 819 | strcpy(&zSql[nSql++], "\n"); |
| 820 | strcpy(&zSql[nSql], zLine); |
| 821 | nSql += len; |
| 822 | } |
| 823 | free(zLine); |
| 824 | if( zSql && sqlite_complete(zSql) ){ |
| 825 | p->cnt = 0; |
| 826 | if( sqlite_exec(db, zSql, callback, p, &zErrMsg)!=0 |
| 827 | && zErrMsg!=0 ){ |
| 828 | if( in!=0 && !p->echoOn ) printf("%s\n",zSql); |
| 829 | printf("SQL error: %s\n", zErrMsg); |
| 830 | free(zErrMsg); |
| 831 | zErrMsg = 0; |
| 832 | } |
| 833 | free(zSql); |
| 834 | zSql = 0; |
| 835 | nSql = 0; |
| 836 | } |
| 837 | } |
| 838 | if( zSql ){ |
| 839 | printf("Incomplete SQL: %s\n", zSql); |
| 840 | free(zSql); |
| 841 | } |
| 842 | } |
| 843 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 844 | int main(int argc, char **argv){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 845 | char *zErrMsg = 0; |
| 846 | struct callback_data data; |
| 847 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 848 | Argv0 = argv[0]; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 849 | memset(&data, 0, sizeof(data)); |
| 850 | data.mode = MODE_List; |
| 851 | strcpy(data.separator,"|"); |
| 852 | data.showHeader = 0; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 853 | #ifdef SIGINT |
| 854 | signal(SIGINT, interrupt_handler); |
| 855 | #endif |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 856 | while( argc>=2 && argv[1][0]=='-' ){ |
| 857 | if( strcmp(argv[1],"-html")==0 ){ |
| 858 | data.mode = MODE_Html; |
| 859 | argc--; |
| 860 | argv++; |
| 861 | }else if( strcmp(argv[1],"-list")==0 ){ |
| 862 | data.mode = MODE_List; |
| 863 | argc--; |
| 864 | argv++; |
| 865 | }else if( strcmp(argv[1],"-line")==0 ){ |
| 866 | data.mode = MODE_Line; |
| 867 | argc--; |
| 868 | argv++; |
| 869 | }else if( argc>=3 && strcmp(argv[0],"-separator")==0 ){ |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 870 | sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[2]); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 871 | argc -= 2; |
| 872 | argv += 2; |
| 873 | }else if( strcmp(argv[1],"-header")==0 ){ |
| 874 | data.showHeader = 1; |
| 875 | argc--; |
| 876 | argv++; |
| 877 | }else if( strcmp(argv[1],"-noheader")==0 ){ |
| 878 | data.showHeader = 0; |
| 879 | argc--; |
| 880 | argv++; |
drh | 660f68d | 2001-01-04 14:27:07 +0000 | [diff] [blame] | 881 | }else if( strcmp(argv[1],"-echo")==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 882 | data.echoOn = 1; |
drh | 660f68d | 2001-01-04 14:27:07 +0000 | [diff] [blame] | 883 | argc--; |
| 884 | argv++; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 885 | }else{ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 886 | fprintf(stderr,"%s: unknown option: %s\n", Argv0, argv[1]); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 887 | return 1; |
| 888 | } |
| 889 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 890 | if( argc!=2 && argc!=3 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 891 | fprintf(stderr,"Usage: %s ?OPTIONS? FILENAME ?SQL?\n", Argv0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 892 | exit(1); |
| 893 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 894 | data.db = db = sqlite_open(argv[1], 0666, &zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 895 | if( db==0 ){ |
drh | 167a4b1 | 2000-08-17 09:49:59 +0000 | [diff] [blame] | 896 | data.db = db = sqlite_open(argv[1], 0444, &zErrMsg); |
| 897 | if( db==0 ){ |
| 898 | if( zErrMsg ){ |
| 899 | fprintf(stderr,"Unable to open database \"%s\": %s\n", argv[1],zErrMsg); |
| 900 | }else{ |
| 901 | fprintf(stderr,"Unable to open database %s\n", argv[1]); |
| 902 | } |
| 903 | exit(1); |
drh | d1dedb8 | 2000-06-05 02:07:04 +0000 | [diff] [blame] | 904 | }else{ |
drh | 80afdca | 2000-08-22 13:27:22 +0000 | [diff] [blame] | 905 | fprintf(stderr,"Database \"%s\" opened READ ONLY!\n", argv[1]); |
drh | d1dedb8 | 2000-06-05 02:07:04 +0000 | [diff] [blame] | 906 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 907 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 908 | data.out = stdout; |
| 909 | if( argc==3 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 910 | if( sqlite_exec(db, argv[2], callback, &data, &zErrMsg)!=0 && zErrMsg!=0 ){ |
| 911 | fprintf(stderr,"SQL error: %s\n", zErrMsg); |
| 912 | exit(1); |
| 913 | } |
| 914 | }else{ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 915 | extern int isatty(); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 916 | if( isatty(0) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 917 | printf( |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 918 | "SQLite version %s\n" |
| 919 | "Enter \".help\" for instructions\n", |
| 920 | sqlite_version |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 921 | ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 922 | process_input(&data, 0); |
| 923 | }else{ |
| 924 | process_input(&data, stdin); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 927 | set_table_name(&data, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 928 | sqlite_close(db); |
| 929 | return 0; |
| 930 | } |