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