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 | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame^] | 15 | ** $Id: shell.c,v 1.47 2002/03/03 23:06:01 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 | a297b5c | 2002-01-15 18:39:43 +0000 | [diff] [blame] | 22 | #if !defined(_WIN32) && !defined(WIN32) |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 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" |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 95 | ** is false, use "getline" instead of "readline" and issue no prompt. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 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 */ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 128 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 129 | char separator[20]; /* Separator character for MODE_List */ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 130 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 131 | int actualWidth[100]; /* Actual width of each column */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | /* |
| 135 | ** These are the allowed modes. |
| 136 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 137 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 138 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 139 | #define MODE_List 2 /* One record per line with a separator */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 140 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 141 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 142 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | ** Number of elements in an array |
| 146 | */ |
| 147 | #define ArraySize(X) (sizeof(X)/sizeof(X[0])) |
| 148 | |
| 149 | /* |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 150 | ** Return TRUE if the string supplied is a number of some kinds. |
| 151 | */ |
| 152 | static int is_numeric(const char *z){ |
| 153 | int seen_digit = 0; |
| 154 | if( *z=='-' || *z=='+' ){ |
| 155 | z++; |
| 156 | } |
| 157 | while( isdigit(*z) ){ |
| 158 | seen_digit = 1; |
| 159 | z++; |
| 160 | } |
| 161 | if( seen_digit && *z=='.' ){ |
| 162 | z++; |
| 163 | while( isdigit(*z) ){ z++; } |
| 164 | } |
| 165 | if( seen_digit && (*z=='e' || *z=='E') |
| 166 | && (isdigit(z[1]) || ((z[1]=='-' || z[1]=='+') && isdigit(z[2]))) |
| 167 | ){ |
| 168 | z+=2; |
| 169 | while( isdigit(*z) ){ z++; } |
| 170 | } |
| 171 | return seen_digit && *z==0; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 176 | */ |
| 177 | static void output_quoted_string(FILE *out, const char *z){ |
| 178 | int i; |
| 179 | int nSingle = 0; |
| 180 | int nDouble = 0; |
| 181 | for(i=0; z[i]; i++){ |
| 182 | if( z[i]=='\'' ) nSingle++; |
| 183 | else if( z[i]=='"' ) nDouble++; |
| 184 | } |
| 185 | if( nSingle==0 ){ |
| 186 | fprintf(out,"'%s'",z); |
| 187 | }else if( nDouble==0 ){ |
| 188 | fprintf(out,"\"%s\"",z); |
| 189 | }else{ |
| 190 | fprintf(out,"'"); |
| 191 | while( *z ){ |
| 192 | for(i=0; z[i] && z[i]!='\''; i++){} |
| 193 | if( i==0 ){ |
| 194 | fprintf(out,"''"); |
| 195 | z++; |
| 196 | }else if( z[i]=='\'' ){ |
| 197 | fprintf(out,"%.*s''",i,z); |
| 198 | z += i+1; |
| 199 | }else{ |
drh | cd7d273 | 2002-02-26 23:24:26 +0000 | [diff] [blame] | 200 | fprintf(out,"%s",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 201 | break; |
| 202 | } |
| 203 | } |
drh | cd7d273 | 2002-02-26 23:24:26 +0000 | [diff] [blame] | 204 | fprintf(out,"'"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 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 = ""; |
drh | 71172c5 | 2002-01-24 00:00:21 +0000 | [diff] [blame] | 318 | fprintf(p->out, "%s", z); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 319 | if( i<nArg-1 ){ |
| 320 | fprintf(p->out, "%s", p->separator); |
| 321 | }else if( p->mode==MODE_Semi ){ |
| 322 | fprintf(p->out, ";\n"); |
| 323 | }else{ |
| 324 | fprintf(p->out, "\n"); |
| 325 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 326 | } |
| 327 | break; |
| 328 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 329 | case MODE_Html: { |
| 330 | if( p->cnt++==0 && p->showHeader ){ |
| 331 | fprintf(p->out,"<TR>"); |
| 332 | for(i=0; i<nArg; i++){ |
| 333 | fprintf(p->out,"<TH>%s</TH>",azCol[i]); |
| 334 | } |
| 335 | fprintf(p->out,"</TR>\n"); |
| 336 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 337 | if( azArg==0 ) break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 338 | fprintf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 339 | for(i=0; i<nArg; i++){ |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 340 | fprintf(p->out,"<TD>"); |
| 341 | output_html_string(p->out, azArg[i] ? azArg[i] : ""); |
| 342 | fprintf(p->out,"</TD>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 343 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 344 | fprintf(p->out,"</TD></TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 345 | break; |
| 346 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 347 | case MODE_Insert: { |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 348 | if( azArg==0 ) break; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 349 | fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 350 | for(i=0; i<nArg; i++){ |
| 351 | char *zSep = i>0 ? ",": ""; |
| 352 | if( azArg[i]==0 ){ |
| 353 | fprintf(p->out,"%sNULL",zSep); |
| 354 | }else if( is_numeric(azArg[i]) ){ |
| 355 | fprintf(p->out,"%s%s",zSep, azArg[i]); |
| 356 | }else{ |
| 357 | if( zSep[0] ) fprintf(p->out,"%s",zSep); |
| 358 | output_quoted_string(p->out, azArg[i]); |
| 359 | } |
| 360 | } |
| 361 | fprintf(p->out,");\n"); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 362 | break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 363 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 364 | } |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | /* |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 369 | ** Set the destination table field of the callback_data structure to |
| 370 | ** the name of the table given. Escape any quote characters in the |
| 371 | ** table name. |
| 372 | */ |
| 373 | static void set_table_name(struct callback_data *p, const char *zName){ |
| 374 | int i, n; |
| 375 | int needQuote; |
| 376 | char *z; |
| 377 | |
| 378 | if( p->zDestTable ){ |
| 379 | free(p->zDestTable); |
| 380 | p->zDestTable = 0; |
| 381 | } |
| 382 | if( zName==0 ) return; |
| 383 | needQuote = !isalpha(*zName) && *zName!='_'; |
| 384 | for(i=n=0; zName[i]; i++, n++){ |
| 385 | if( !isalnum(zName[i]) && zName[i]!='_' ){ |
| 386 | needQuote = 1; |
| 387 | if( zName[i]=='\'' ) n++; |
| 388 | } |
| 389 | } |
| 390 | if( needQuote ) n += 2; |
| 391 | z = p->zDestTable = malloc( n+1 ); |
| 392 | if( z==0 ){ |
| 393 | fprintf(stderr,"Out of memory!\n"); |
| 394 | exit(1); |
| 395 | } |
| 396 | n = 0; |
| 397 | if( needQuote ) z[n++] = '\''; |
| 398 | for(i=0; zName[i]; i++){ |
| 399 | z[n++] = zName[i]; |
| 400 | if( zName[i]=='\'' ) z[n++] = '\''; |
| 401 | } |
| 402 | if( needQuote ) z[n++] = '\''; |
| 403 | z[n] = 0; |
| 404 | } |
| 405 | |
| 406 | /* |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 407 | ** This is a different callback routine used for dumping the database. |
| 408 | ** Each row received by this callback consists of a table name, |
| 409 | ** the table type ("index" or "table") and SQL to create the table. |
| 410 | ** This routine should print text sufficient to recreate the table. |
| 411 | */ |
| 412 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 413 | struct callback_data *p = (struct callback_data *)pArg; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 414 | if( nArg!=3 ) return 1; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 415 | fprintf(p->out, "%s;\n", azArg[2]); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 416 | if( strcmp(azArg[1],"table")==0 ){ |
| 417 | struct callback_data d2; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 418 | d2 = *p; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 419 | d2.mode = MODE_Insert; |
| 420 | d2.zDestTable = 0; |
| 421 | set_table_name(&d2, azArg[0]); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 422 | sqlite_exec_printf(p->db, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 423 | "SELECT * FROM '%q'", |
| 424 | callback, &d2, 0, azArg[0] |
| 425 | ); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 426 | set_table_name(&d2, 0); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 427 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 432 | ** Text of a help message |
| 433 | */ |
| 434 | static char zHelp[] = |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 435 | ".dump ?TABLE? ... Dump the database in an text format\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 436 | ".echo ON|OFF Turn command echo on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 437 | ".exit Exit this program\n" |
| 438 | ".explain Set output mode suitable for EXPLAIN\n" |
| 439 | ".header ON|OFF Turn display of headers on or off\n" |
| 440 | ".help Show this message\n" |
| 441 | ".indices TABLE Show names of all indices on TABLE\n" |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 442 | ".mode MODE Set mode to one of \"line\", \"column\", \n" |
| 443 | " \"insert\", \"list\", or \"html\"\n" |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 444 | ".mode insert TABLE Generate SQL insert statements for TABLE\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 445 | ".output FILENAME Send output to FILENAME\n" |
| 446 | ".output stdout Send output to the screen\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 447 | ".read FILENAME Execute SQL in FILENAME\n" |
| 448 | ".reindex ?TABLE? Rebuild indices\n" |
| 449 | /* ".rename OLD NEW Change the name of a table or index\n" */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 450 | ".schema ?TABLE? Show the CREATE statements\n" |
| 451 | ".separator STRING Change separator string for \"list\" mode\n" |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 452 | ".tables ?PATTERN? List names of tables matching a pattern\n" |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 453 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 454 | ".width NUM NUM ... Set column widths for \"column\" mode\n" |
| 455 | ; |
| 456 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 457 | /* Forward reference */ |
| 458 | static void process_input(struct callback_data *p, FILE *in); |
| 459 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 460 | /* |
| 461 | ** If an input line begins with "." then invoke this routine to |
| 462 | ** process that line. |
| 463 | */ |
| 464 | static void do_meta_command(char *zLine, sqlite *db, struct callback_data *p){ |
| 465 | int i = 1; |
| 466 | int nArg = 0; |
| 467 | int n, c; |
| 468 | char *azArg[50]; |
| 469 | |
| 470 | /* Parse the input line into tokens. |
| 471 | */ |
| 472 | while( zLine[i] && nArg<ArraySize(azArg) ){ |
| 473 | while( isspace(zLine[i]) ){ i++; } |
| 474 | if( zLine[i]=='\'' || zLine[i]=='"' ){ |
| 475 | int delim = zLine[i++]; |
| 476 | azArg[nArg++] = &zLine[i]; |
| 477 | while( zLine[i] && zLine[i]!=delim ){ i++; } |
| 478 | if( zLine[i]==delim ){ |
| 479 | zLine[i++] = 0; |
| 480 | } |
| 481 | }else{ |
| 482 | azArg[nArg++] = &zLine[i]; |
| 483 | while( zLine[i] && !isspace(zLine[i]) ){ i++; } |
| 484 | if( zLine[i] ) zLine[i++] = 0; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /* Process the input line. |
| 489 | */ |
| 490 | if( nArg==0 ) return; |
| 491 | n = strlen(azArg[0]); |
| 492 | c = azArg[0][0]; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 493 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
| 494 | char *zErrMsg = 0; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 495 | fprintf(p->out, "BEGIN TRANSACTION;\n"); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 496 | if( nArg==1 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 497 | sqlite_exec(db, |
| 498 | "SELECT name, type, sql FROM sqlite_master " |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 499 | "WHERE type!='meta' AND sql NOT NULL " |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 500 | "ORDER BY tbl_name, type DESC, name", |
| 501 | dump_callback, p, &zErrMsg |
| 502 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 503 | }else{ |
| 504 | int i; |
| 505 | for(i=1; i<nArg && zErrMsg==0; i++){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 506 | sqlite_exec_printf(db, |
| 507 | "SELECT name, type, sql FROM sqlite_master " |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 508 | "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL " |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 509 | "ORDER BY type DESC, name", |
| 510 | dump_callback, p, &zErrMsg, azArg[i] |
| 511 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 512 | |
| 513 | } |
| 514 | } |
| 515 | if( zErrMsg ){ |
| 516 | fprintf(stderr,"Error: %s\n", zErrMsg); |
| 517 | free(zErrMsg); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 518 | }else{ |
| 519 | fprintf(p->out, "COMMIT;\n"); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 520 | } |
| 521 | }else |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 522 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 523 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){ |
| 524 | int j; |
| 525 | char *z = azArg[1]; |
| 526 | int val = atoi(azArg[1]); |
| 527 | for(j=0; z[j]; j++){ |
| 528 | if( isupper(z[j]) ) z[j] = tolower(z[j]); |
| 529 | } |
| 530 | if( strcmp(z,"on")==0 ){ |
| 531 | val = 1; |
| 532 | }else if( strcmp(z,"yes")==0 ){ |
| 533 | val = 1; |
| 534 | } |
| 535 | p->echoOn = val; |
| 536 | }else |
| 537 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 538 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 539 | sqlite_close(db); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 540 | exit(0); |
| 541 | }else |
| 542 | |
| 543 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
| 544 | p->mode = MODE_Column; |
| 545 | p->showHeader = 1; |
| 546 | p->colWidth[0] = 4; |
| 547 | p->colWidth[1] = 12; |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 548 | p->colWidth[2] = 10; |
| 549 | p->colWidth[3] = 10; |
| 550 | p->colWidth[4] = 35; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 551 | }else |
| 552 | |
| 553 | if( c=='h' && strncmp(azArg[0], "header", n)==0 && nArg>1 ){ |
| 554 | int j; |
| 555 | char *z = azArg[1]; |
| 556 | int val = atoi(azArg[1]); |
| 557 | for(j=0; z[j]; j++){ |
| 558 | if( isupper(z[j]) ) z[j] = tolower(z[j]); |
| 559 | } |
| 560 | if( strcmp(z,"on")==0 ){ |
| 561 | val = 1; |
| 562 | }else if( strcmp(z,"yes")==0 ){ |
| 563 | val = 1; |
| 564 | } |
| 565 | p->showHeader = val; |
| 566 | }else |
| 567 | |
| 568 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
| 569 | fprintf(stderr,zHelp); |
| 570 | }else |
| 571 | |
| 572 | if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){ |
| 573 | struct callback_data data; |
| 574 | char *zErrMsg = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 575 | memcpy(&data, p, sizeof(data)); |
| 576 | data.showHeader = 0; |
| 577 | data.mode = MODE_List; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 578 | sqlite_exec_printf(db, |
| 579 | "SELECT name FROM sqlite_master " |
| 580 | "WHERE type='index' AND tbl_name LIKE '%q' " |
| 581 | "ORDER BY name", |
| 582 | callback, &data, &zErrMsg, azArg[1] |
| 583 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 584 | if( zErrMsg ){ |
| 585 | fprintf(stderr,"Error: %s\n", zErrMsg); |
| 586 | free(zErrMsg); |
| 587 | } |
| 588 | }else |
| 589 | |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 590 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 591 | int n2 = strlen(azArg[1]); |
| 592 | if( strncmp(azArg[1],"line",n2)==0 ){ |
| 593 | p->mode = MODE_Line; |
| 594 | }else if( strncmp(azArg[1],"column",n2)==0 ){ |
| 595 | p->mode = MODE_Column; |
| 596 | }else if( strncmp(azArg[1],"list",n2)==0 ){ |
| 597 | p->mode = MODE_List; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 598 | }else if( strncmp(azArg[1],"html",n2)==0 ){ |
| 599 | p->mode = MODE_Html; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 600 | }else if( strncmp(azArg[1],"insert",n2)==0 ){ |
| 601 | p->mode = MODE_Insert; |
| 602 | if( nArg>=3 ){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 603 | set_table_name(p, azArg[2]); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 604 | }else{ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 605 | set_table_name(p, "table"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 606 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 607 | }else { |
| 608 | fprintf(stderr,"mode should be on of: column html insert line list\n"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 609 | } |
| 610 | }else |
| 611 | |
| 612 | if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){ |
| 613 | if( p->out!=stdout ){ |
| 614 | fclose(p->out); |
| 615 | } |
| 616 | if( strcmp(azArg[1],"stdout")==0 ){ |
| 617 | p->out = stdout; |
| 618 | }else{ |
| 619 | p->out = fopen(azArg[1], "w"); |
| 620 | if( p->out==0 ){ |
| 621 | fprintf(stderr,"can't write to \"%s\"\n", azArg[1]); |
| 622 | p->out = stdout; |
| 623 | } |
| 624 | } |
| 625 | }else |
| 626 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 627 | if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){ |
| 628 | FILE *alt = fopen(azArg[1], "r"); |
| 629 | if( alt==0 ){ |
| 630 | fprintf(stderr,"can't open \"%s\"\n", azArg[1]); |
| 631 | }else{ |
| 632 | process_input(p, alt); |
| 633 | fclose(alt); |
| 634 | } |
| 635 | }else |
| 636 | |
| 637 | if( c=='r' && strncmp(azArg[0], "reindex", n)==0 ){ |
| 638 | char **azResult; |
| 639 | int nRow, rc; |
| 640 | char *zErrMsg; |
| 641 | int i; |
| 642 | char *zSql; |
| 643 | if( nArg==1 ){ |
| 644 | rc = sqlite_get_table(db, |
| 645 | "SELECT name, sql FROM sqlite_master " |
| 646 | "WHERE type='index'", |
| 647 | &azResult, &nRow, 0, &zErrMsg |
| 648 | ); |
| 649 | }else{ |
| 650 | rc = sqlite_get_table_printf(db, |
| 651 | "SELECT name, sql FROM sqlite_master " |
| 652 | "WHERE type='index' AND tbl_name LIKE '%q'", |
| 653 | &azResult, &nRow, 0, &zErrMsg, azArg[1] |
| 654 | ); |
| 655 | } |
| 656 | for(i=1; rc==SQLITE_OK && i<=nRow; i++){ |
| 657 | extern char *sqlite_mprintf(const char *, ...); |
| 658 | zSql = sqlite_mprintf( |
| 659 | "DROP INDEX '%q';\n%s;\nVACUUM '%q';", |
| 660 | azResult[i*2], azResult[i*2+1], azResult[i*2]); |
| 661 | if( p->echoOn ) printf("%s\n", zSql); |
| 662 | rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg); |
| 663 | } |
| 664 | sqlite_free_table(azResult); |
| 665 | if( zErrMsg ){ |
| 666 | fprintf(stderr,"Error: %s\n", zErrMsg); |
| 667 | free(zErrMsg); |
| 668 | } |
| 669 | }else |
| 670 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 671 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
| 672 | struct callback_data data; |
| 673 | char *zErrMsg = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 674 | memcpy(&data, p, sizeof(data)); |
| 675 | data.showHeader = 0; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 676 | data.mode = MODE_Semi; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 677 | if( nArg>1 ){ |
drh | ff9821a | 2001-04-04 21:22:14 +0000 | [diff] [blame] | 678 | extern int sqliteStrICmp(const char*,const char*); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 679 | if( sqliteStrICmp(azArg[1],"sqlite_master")==0 ){ |
| 680 | char *new_argv[2], *new_colv[2]; |
| 681 | new_argv[0] = "CREATE TABLE sqlite_master (\n" |
| 682 | " type text,\n" |
| 683 | " name text,\n" |
| 684 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 685 | " rootpage integer,\n" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 686 | " sql text\n" |
| 687 | ")"; |
| 688 | new_argv[1] = 0; |
| 689 | new_colv[0] = "sql"; |
| 690 | new_colv[1] = 0; |
| 691 | callback(&data, 1, new_argv, new_colv); |
| 692 | }else{ |
| 693 | sqlite_exec_printf(db, |
| 694 | "SELECT sql FROM sqlite_master " |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 695 | "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL " |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 696 | "ORDER BY type DESC, name", |
| 697 | callback, &data, &zErrMsg, azArg[1]); |
| 698 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 699 | }else{ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 700 | sqlite_exec(db, |
| 701 | "SELECT sql FROM sqlite_master " |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 702 | "WHERE type!='meta' AND sql NOTNULL " |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 703 | "ORDER BY tbl_name, type DESC, name", |
| 704 | callback, &data, &zErrMsg |
| 705 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 706 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 707 | if( zErrMsg ){ |
| 708 | fprintf(stderr,"Error: %s\n", zErrMsg); |
| 709 | free(zErrMsg); |
| 710 | } |
| 711 | }else |
| 712 | |
| 713 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){ |
| 714 | sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]); |
| 715 | }else |
| 716 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 717 | if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 718 | char **azResult; |
| 719 | int nRow, rc; |
| 720 | char *zErrMsg; |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 721 | if( nArg==1 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 722 | rc = sqlite_get_table(db, |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 723 | "SELECT name FROM sqlite_master " |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame^] | 724 | "WHERE type IN ('table','view') " |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 725 | "ORDER BY name", |
| 726 | &azResult, &nRow, 0, &zErrMsg |
| 727 | ); |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 728 | }else{ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 729 | rc = sqlite_get_table_printf(db, |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 730 | "SELECT name FROM sqlite_master " |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame^] | 731 | "WHERE type IN ('table','view') AND name LIKE '%%%q%%' " |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 732 | "ORDER BY name", |
| 733 | &azResult, &nRow, 0, &zErrMsg, azArg[1] |
| 734 | ); |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 735 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 736 | if( zErrMsg ){ |
| 737 | fprintf(stderr,"Error: %s\n", zErrMsg); |
| 738 | free(zErrMsg); |
| 739 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 740 | if( rc==SQLITE_OK ){ |
| 741 | int len, maxlen = 0; |
| 742 | int i, j; |
| 743 | int nPrintCol, nPrintRow; |
| 744 | for(i=1; i<=nRow; i++){ |
| 745 | if( azResult[i]==0 ) continue; |
| 746 | len = strlen(azResult[i]); |
| 747 | if( len>maxlen ) maxlen = len; |
| 748 | } |
| 749 | nPrintCol = 80/(maxlen+2); |
| 750 | if( nPrintCol<1 ) nPrintCol = 1; |
| 751 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 752 | for(i=0; i<nPrintRow; i++){ |
| 753 | for(j=i+1; j<=nRow; j+=nPrintRow){ |
| 754 | char *zSp = j<=nPrintRow ? "" : " "; |
| 755 | printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : ""); |
| 756 | } |
| 757 | printf("\n"); |
| 758 | } |
| 759 | } |
| 760 | sqlite_free_table(azResult); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 761 | }else |
| 762 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 763 | if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){ |
| 764 | sqlite_busy_timeout(db, atoi(azArg[1])); |
| 765 | }else |
| 766 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 767 | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
| 768 | int j; |
| 769 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
| 770 | p->colWidth[j-1] = atoi(azArg[j]); |
| 771 | } |
| 772 | }else |
| 773 | |
| 774 | { |
| 775 | fprintf(stderr, "unknown command: \"%s\". Enter \".help\" for help\n", |
| 776 | azArg[0]); |
| 777 | } |
| 778 | } |
| 779 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 780 | static char *Argv0; |
| 781 | static void process_input(struct callback_data *p, FILE *in){ |
| 782 | char *zLine; |
| 783 | char *zSql = 0; |
| 784 | int nSql = 0; |
| 785 | char *zErrMsg; |
| 786 | while( (zLine = one_input_line(zSql, in))!=0 ){ |
| 787 | if( p->echoOn ) printf("%s\n", zLine); |
drh | 2af0b2d | 2002-02-21 02:25:02 +0000 | [diff] [blame] | 788 | if( zLine && zLine[0]=='.' && nSql==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 789 | do_meta_command(zLine, db, p); |
| 790 | free(zLine); |
| 791 | continue; |
| 792 | } |
| 793 | if( zSql==0 ){ |
| 794 | int i; |
| 795 | for(i=0; zLine[i] && isspace(zLine[i]); i++){} |
| 796 | if( zLine[i]!=0 ){ |
| 797 | nSql = strlen(zLine); |
| 798 | zSql = malloc( nSql+1 ); |
| 799 | strcpy(zSql, zLine); |
| 800 | } |
| 801 | }else{ |
| 802 | int len = strlen(zLine); |
| 803 | zSql = realloc( zSql, nSql + len + 2 ); |
| 804 | if( zSql==0 ){ |
| 805 | fprintf(stderr,"%s: out of memory!\n", Argv0); |
| 806 | exit(1); |
| 807 | } |
| 808 | strcpy(&zSql[nSql++], "\n"); |
| 809 | strcpy(&zSql[nSql], zLine); |
| 810 | nSql += len; |
| 811 | } |
| 812 | free(zLine); |
| 813 | if( zSql && sqlite_complete(zSql) ){ |
| 814 | p->cnt = 0; |
| 815 | if( sqlite_exec(db, zSql, callback, p, &zErrMsg)!=0 |
| 816 | && zErrMsg!=0 ){ |
| 817 | if( in!=0 && !p->echoOn ) printf("%s\n",zSql); |
| 818 | printf("SQL error: %s\n", zErrMsg); |
| 819 | free(zErrMsg); |
| 820 | zErrMsg = 0; |
| 821 | } |
| 822 | free(zSql); |
| 823 | zSql = 0; |
| 824 | nSql = 0; |
| 825 | } |
| 826 | } |
| 827 | if( zSql ){ |
| 828 | printf("Incomplete SQL: %s\n", zSql); |
| 829 | free(zSql); |
| 830 | } |
| 831 | } |
| 832 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 833 | int main(int argc, char **argv){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 834 | char *zErrMsg = 0; |
| 835 | struct callback_data data; |
| 836 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 837 | Argv0 = argv[0]; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 838 | memset(&data, 0, sizeof(data)); |
| 839 | data.mode = MODE_List; |
| 840 | strcpy(data.separator,"|"); |
| 841 | data.showHeader = 0; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 842 | #ifdef SIGINT |
| 843 | signal(SIGINT, interrupt_handler); |
| 844 | #endif |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 845 | while( argc>=2 && argv[1][0]=='-' ){ |
| 846 | if( strcmp(argv[1],"-html")==0 ){ |
| 847 | data.mode = MODE_Html; |
| 848 | argc--; |
| 849 | argv++; |
| 850 | }else if( strcmp(argv[1],"-list")==0 ){ |
| 851 | data.mode = MODE_List; |
| 852 | argc--; |
| 853 | argv++; |
| 854 | }else if( strcmp(argv[1],"-line")==0 ){ |
| 855 | data.mode = MODE_Line; |
| 856 | argc--; |
| 857 | argv++; |
drh | 7613bfa | 2002-01-22 12:39:24 +0000 | [diff] [blame] | 858 | }else if( argc>=3 && strcmp(argv[1],"-separator")==0 ){ |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 859 | sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[2]); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 860 | argc -= 2; |
| 861 | argv += 2; |
| 862 | }else if( strcmp(argv[1],"-header")==0 ){ |
| 863 | data.showHeader = 1; |
| 864 | argc--; |
| 865 | argv++; |
| 866 | }else if( strcmp(argv[1],"-noheader")==0 ){ |
| 867 | data.showHeader = 0; |
| 868 | argc--; |
| 869 | argv++; |
drh | 660f68d | 2001-01-04 14:27:07 +0000 | [diff] [blame] | 870 | }else if( strcmp(argv[1],"-echo")==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 871 | data.echoOn = 1; |
drh | 660f68d | 2001-01-04 14:27:07 +0000 | [diff] [blame] | 872 | argc--; |
| 873 | argv++; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 874 | }else{ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 875 | fprintf(stderr,"%s: unknown option: %s\n", Argv0, argv[1]); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 876 | return 1; |
| 877 | } |
| 878 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 879 | if( argc!=2 && argc!=3 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 880 | fprintf(stderr,"Usage: %s ?OPTIONS? FILENAME ?SQL?\n", Argv0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 881 | exit(1); |
| 882 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 883 | data.db = db = sqlite_open(argv[1], 0666, &zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 884 | if( db==0 ){ |
drh | 167a4b1 | 2000-08-17 09:49:59 +0000 | [diff] [blame] | 885 | data.db = db = sqlite_open(argv[1], 0444, &zErrMsg); |
| 886 | if( db==0 ){ |
| 887 | if( zErrMsg ){ |
| 888 | fprintf(stderr,"Unable to open database \"%s\": %s\n", argv[1],zErrMsg); |
| 889 | }else{ |
| 890 | fprintf(stderr,"Unable to open database %s\n", argv[1]); |
| 891 | } |
| 892 | exit(1); |
drh | d1dedb8 | 2000-06-05 02:07:04 +0000 | [diff] [blame] | 893 | }else{ |
drh | 80afdca | 2000-08-22 13:27:22 +0000 | [diff] [blame] | 894 | fprintf(stderr,"Database \"%s\" opened READ ONLY!\n", argv[1]); |
drh | d1dedb8 | 2000-06-05 02:07:04 +0000 | [diff] [blame] | 895 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 896 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 897 | data.out = stdout; |
| 898 | if( argc==3 ){ |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 899 | if( argv[2][0]=='.' ){ |
| 900 | do_meta_command(argv[2], db, &data); |
| 901 | exit(0); |
| 902 | }else{ |
| 903 | int rc; |
| 904 | rc = sqlite_exec(db, argv[2], callback, &data, &zErrMsg); |
| 905 | if( rc!=0 && zErrMsg!=0 ){ |
| 906 | fprintf(stderr,"SQL error: %s\n", zErrMsg); |
| 907 | exit(1); |
| 908 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 909 | } |
| 910 | }else{ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 911 | extern int isatty(); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 912 | if( isatty(0) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 913 | printf( |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 914 | "SQLite version %s\n" |
| 915 | "Enter \".help\" for instructions\n", |
| 916 | sqlite_version |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 917 | ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 918 | process_input(&data, 0); |
| 919 | }else{ |
| 920 | process_input(&data, stdin); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 921 | } |
| 922 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 923 | set_table_name(&data, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 924 | sqlite_close(db); |
| 925 | return 0; |
| 926 | } |