blob: 592536034896b5594ea49c9c5618d9da2b5bc757 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** This file contains code to implement the "sqlite" command line
13** utility for accessing SQLite databases.
14**
drhc8d74412004-08-31 23:41:26 +000015** $Id: shell.c,v 1.113 2004/08/31 23:41:26 drh Exp $
drh75897232000-05-29 14:26:00 +000016*/
17#include <stdlib.h>
18#include <string.h>
19#include <stdio.h>
danielk19772a02e332004-06-05 08:04:36 +000020#include <assert.h>
drh1d482dd2004-05-31 18:23:07 +000021#include "sqlite3.h"
drh75897232000-05-29 14:26:00 +000022#include <ctype.h>
persicom7e2dfdd2002-04-18 02:46:52 +000023
drh820f3812003-01-08 13:02:52 +000024#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh4c504392000-10-16 22:06:40 +000025# include <signal.h>
drhdd45df82002-04-18 12:39:03 +000026# include <pwd.h>
27# include <unistd.h>
28# include <sys/types.h>
drh4c504392000-10-16 22:06:40 +000029#endif
drh75897232000-05-29 14:26:00 +000030
drh820f3812003-01-08 13:02:52 +000031#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
drh16e59552000-07-31 11:57:37 +000040#if defined(HAVE_READLINE) && HAVE_READLINE==1
drh8e7e7a22000-05-30 18:45:23 +000041# include <readline/readline.h>
42# include <readline/history.h>
43#else
drh9347b202003-07-18 01:30:59 +000044# define readline(p) local_getline(p,stdin)
persicom1d0b8722002-04-18 02:53:04 +000045# define add_history(X)
drh67505e72002-04-19 12:34:06 +000046# define read_history(X)
47# define write_history(X)
48# define stifle_history(X)
drh75897232000-05-29 14:26:00 +000049#endif
50
drh4328c8b2003-04-26 02:50:11 +000051/* Make sure isatty() has a prototype.
52*/
53extern int isatty();
54
drh75897232000-05-29 14:26:00 +000055/*
drh4c504392000-10-16 22:06:40 +000056** The following is the open SQLite database. We make a pointer
57** to this database a static variable so that it can be accessed
58** by the SIGINT handler to interrupt database processing.
59*/
danielk197792f9a1b2004-06-19 09:08:16 +000060static sqlite3 *db = 0;
drh4c504392000-10-16 22:06:40 +000061
62/*
drh67505e72002-04-19 12:34:06 +000063** True if an interrupt (Control-C) has been received.
64*/
65static int seenInterrupt = 0;
66
67/*
persicom7e2dfdd2002-04-18 02:46:52 +000068** This is the name of our program. It is set in main(), used
69** in a number of other places, mostly for error messages.
70*/
71static char *Argv0;
72
73/*
74** Prompt strings. Initialized in main. Settable with
75** .prompt main continue
76*/
77static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
78static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
79
drh44c2eb12003-04-30 11:38:26 +000080
persicom7e2dfdd2002-04-18 02:46:52 +000081/*
drh83965662003-04-17 02:54:13 +000082** Determines if a string is a number of not.
83*/
drhc8d74412004-08-31 23:41:26 +000084static int isNumber(const unsigned char *z, int *realnum){
85 if( *z=='-' || *z=='+' ) z++;
86 if( !isdigit(*z) ){
87 return 0;
88 }
89 z++;
90 if( realnum ) *realnum = 0;
91 while( isdigit(*z) ){ z++; }
92 if( *z=='.' ){
93 z++;
94 if( !isdigit(*z) ) return 0;
95 while( isdigit(*z) ){ z++; }
96 if( realnum ) *realnum = 1;
97 }
98 if( *z=='e' || *z=='E' ){
99 z++;
100 if( *z=='+' || *z=='-' ) z++;
101 if( !isdigit(*z) ) return 0;
102 while( isdigit(*z) ){ z++; }
103 if( realnum ) *realnum = 1;
104 }
105 return *z==0;
106}
drh83965662003-04-17 02:54:13 +0000107
108/*
danielk1977bc6ada42004-06-30 08:20:16 +0000109** A global char* and an SQL function to access its current value
110** from within an SQL statement. This program used to use the
111** sqlite_exec_printf() API to substitue a string into an SQL statement.
112** The correct way to do this with sqlite3 is to use the bind API, but
113** since the shell is built around the callback paradigm it would be a lot
114** of work. Instead just use this hack, which is quite harmless.
115*/
116static const char *zShellStatic = 0;
117static void shellstaticFunc(
118 sqlite3_context *context,
119 int argc,
120 sqlite3_value **argv
121){
122 assert( 0==argc );
123 assert( zShellStatic );
124 sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
125}
126
127
128/*
drhfeac5f82004-08-01 00:10:45 +0000129** This routine reads a line of text from FILE in, stores
drh8e7e7a22000-05-30 18:45:23 +0000130** the text in memory obtained from malloc() and returns a pointer
131** to the text. NULL is returned at end of file, or if malloc()
132** fails.
133**
134** The interface is like "readline" but no command-line editing
135** is done.
136*/
drh9347b202003-07-18 01:30:59 +0000137static char *local_getline(char *zPrompt, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000138 char *zLine;
139 int nLine;
drh8e7e7a22000-05-30 18:45:23 +0000140 int n;
141 int eol;
142
143 if( zPrompt && *zPrompt ){
144 printf("%s",zPrompt);
145 fflush(stdout);
146 }
147 nLine = 100;
148 zLine = malloc( nLine );
149 if( zLine==0 ) return 0;
150 n = 0;
151 eol = 0;
152 while( !eol ){
153 if( n+100>nLine ){
154 nLine = nLine*2 + 100;
155 zLine = realloc(zLine, nLine);
156 if( zLine==0 ) return 0;
157 }
drhdaffd0e2001-04-11 14:28:42 +0000158 if( fgets(&zLine[n], nLine - n, in)==0 ){
drh8e7e7a22000-05-30 18:45:23 +0000159 if( n==0 ){
160 free(zLine);
161 return 0;
162 }
163 zLine[n] = 0;
164 eol = 1;
165 break;
166 }
167 while( zLine[n] ){ n++; }
168 if( n>0 && zLine[n-1]=='\n' ){
169 n--;
170 zLine[n] = 0;
171 eol = 1;
172 }
173 }
174 zLine = realloc( zLine, n+1 );
175 return zLine;
176}
177
178/*
179** Retrieve a single line of input text. "isatty" is true if text
180** is coming from a terminal. In that case, we issue a prompt and
181** attempt to use "readline" for command-line editing. If "isatty"
drh9347b202003-07-18 01:30:59 +0000182** is false, use "local_getline" instead of "readline" and issue no prompt.
drh8e7e7a22000-05-30 18:45:23 +0000183**
184** zPrior is a string of prior text retrieved. If not the empty
185** string, then issue a continuation prompt.
186*/
drhdaffd0e2001-04-11 14:28:42 +0000187static char *one_input_line(const char *zPrior, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000188 char *zPrompt;
189 char *zResult;
drhdaffd0e2001-04-11 14:28:42 +0000190 if( in!=0 ){
drh9347b202003-07-18 01:30:59 +0000191 return local_getline(0, in);
drh8e7e7a22000-05-30 18:45:23 +0000192 }
193 if( zPrior && zPrior[0] ){
persicom7e2dfdd2002-04-18 02:46:52 +0000194 zPrompt = continuePrompt;
drh8e7e7a22000-05-30 18:45:23 +0000195 }else{
persicom7e2dfdd2002-04-18 02:46:52 +0000196 zPrompt = mainPrompt;
drh8e7e7a22000-05-30 18:45:23 +0000197 }
198 zResult = readline(zPrompt);
drh2dfbbca2000-07-28 14:32:48 +0000199 if( zResult ) add_history(zResult);
drh8e7e7a22000-05-30 18:45:23 +0000200 return zResult;
201}
202
persicom7e2dfdd2002-04-18 02:46:52 +0000203struct previous_mode_data {
204 int valid; /* Is there legit data in here? */
205 int mode;
206 int showHeader;
207 int colWidth[100];
208};
drh8e7e7a22000-05-30 18:45:23 +0000209/*
drh75897232000-05-29 14:26:00 +0000210** An pointer to an instance of this structure is passed from
211** the main program to the callback. This is used to communicate
212** state and mode information.
213*/
214struct callback_data {
danielk197792f9a1b2004-06-19 09:08:16 +0000215 sqlite3 *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +0000216 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +0000217 int cnt; /* Number of records displayed so far */
218 FILE *out; /* Write results here */
219 int mode; /* An output mode setting */
220 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +0000221 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000222 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000223 int colWidth[100]; /* Requested width of each column when in column mode*/
224 int actualWidth[100]; /* Actual width of each column */
drh83965662003-04-17 02:54:13 +0000225 char nullvalue[20]; /* The text to print when a NULL comes back from
226 ** the database */
persicom7e2dfdd2002-04-18 02:46:52 +0000227 struct previous_mode_data explainPrev;
drh83965662003-04-17 02:54:13 +0000228 /* Holds the mode information just before
229 ** .explain ON */
drh44c2eb12003-04-30 11:38:26 +0000230 char outfile[FILENAME_MAX]; /* Filename for *out */
231 const char *zDbFilename; /* name of the database file */
drh22fbcb82004-02-01 01:22:50 +0000232 char *zKey; /* Encryption key */
drh75897232000-05-29 14:26:00 +0000233};
234
235/*
236** These are the allowed modes.
237*/
drh967e8b72000-06-21 13:59:10 +0000238#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +0000239#define MODE_Column 1 /* One record per line in neat columns */
240#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +0000241#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
242#define MODE_Html 4 /* Generate an XHTML table */
243#define MODE_Insert 5 /* Generate SQL "insert" statements */
drhfeac5f82004-08-01 00:10:45 +0000244#define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */
245#define MODE_NUM_OF 7 /* The number of modes (not a mode itself) */
persicom7e2dfdd2002-04-18 02:46:52 +0000246
247char *modeDescr[MODE_NUM_OF] = {
248 "line",
249 "column",
250 "list",
251 "semi",
252 "html",
drhfeac5f82004-08-01 00:10:45 +0000253 "insert",
254 "tcl",
persicom7e2dfdd2002-04-18 02:46:52 +0000255};
drh75897232000-05-29 14:26:00 +0000256
257/*
258** Number of elements in an array
259*/
260#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
261
262/*
drh28bd4bc2000-06-15 15:57:22 +0000263** Output the given string as a quoted string using SQL quoting conventions.
264*/
265static void output_quoted_string(FILE *out, const char *z){
266 int i;
267 int nSingle = 0;
drh28bd4bc2000-06-15 15:57:22 +0000268 for(i=0; z[i]; i++){
269 if( z[i]=='\'' ) nSingle++;
drh28bd4bc2000-06-15 15:57:22 +0000270 }
271 if( nSingle==0 ){
272 fprintf(out,"'%s'",z);
drh28bd4bc2000-06-15 15:57:22 +0000273 }else{
274 fprintf(out,"'");
275 while( *z ){
276 for(i=0; z[i] && z[i]!='\''; i++){}
277 if( i==0 ){
278 fprintf(out,"''");
279 z++;
280 }else if( z[i]=='\'' ){
281 fprintf(out,"%.*s''",i,z);
282 z += i+1;
283 }else{
drhcd7d2732002-02-26 23:24:26 +0000284 fprintf(out,"%s",z);
drh28bd4bc2000-06-15 15:57:22 +0000285 break;
286 }
287 }
drhcd7d2732002-02-26 23:24:26 +0000288 fprintf(out,"'");
drh28bd4bc2000-06-15 15:57:22 +0000289 }
290}
291
292/*
drhfeac5f82004-08-01 00:10:45 +0000293** Output the given string as a quoted according to C or TCL quoting rules.
294*/
295static void output_c_string(FILE *out, const char *z){
296 unsigned int c;
297 fputc('"', out);
298 while( (c = *(z++))!=0 ){
299 if( c=='\\' ){
300 fputc(c, out);
301 fputc(c, out);
302 }else if( c=='\t' ){
303 fputc('\\', out);
304 fputc('t', out);
305 }else if( c=='\n' ){
306 fputc('\\', out);
307 fputc('n', out);
308 }else if( c=='\r' ){
309 fputc('\\', out);
310 fputc('r', out);
311 }else if( !isprint(c) ){
312 fprintf(out, "\\%03o", c);
313 }else{
314 fputc(c, out);
315 }
316 }
317 fputc('"', out);
318}
319
320/*
drhc08a4f12000-06-15 16:49:48 +0000321** Output the given string with characters that are special to
322** HTML escaped.
323*/
324static void output_html_string(FILE *out, const char *z){
325 int i;
326 while( *z ){
327 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
328 if( i>0 ){
329 fprintf(out,"%.*s",i,z);
330 }
331 if( z[i]=='<' ){
332 fprintf(out,"&lt;");
333 }else if( z[i]=='&' ){
334 fprintf(out,"&amp;");
335 }else{
336 break;
337 }
338 z += i + 1;
339 }
340}
341
342/*
drh4c504392000-10-16 22:06:40 +0000343** This routine runs when the user presses Ctrl-C
344*/
345static void interrupt_handler(int NotUsed){
drh67505e72002-04-19 12:34:06 +0000346 seenInterrupt = 1;
danielk19776f8a5032004-05-10 10:34:51 +0000347 if( db ) sqlite3_interrupt(db);
drh4c504392000-10-16 22:06:40 +0000348}
349
350/*
drh75897232000-05-29 14:26:00 +0000351** This is the callback routine that the SQLite library
352** invokes for each row of a query result.
353*/
354static int callback(void *pArg, int nArg, char **azArg, char **azCol){
355 int i;
356 struct callback_data *p = (struct callback_data*)pArg;
357 switch( p->mode ){
358 case MODE_Line: {
drhe3710332000-09-29 13:30:53 +0000359 int w = 5;
drh6a535342001-10-19 16:44:56 +0000360 if( azArg==0 ) break;
drhe3710332000-09-29 13:30:53 +0000361 for(i=0; i<nArg; i++){
362 int len = strlen(azCol[i]);
363 if( len>w ) w = len;
364 }
drh75897232000-05-29 14:26:00 +0000365 if( p->cnt++>0 ) fprintf(p->out,"\n");
366 for(i=0; i<nArg; i++){
drha69d9162003-04-17 22:57:53 +0000367 fprintf(p->out,"%*s = %s\n", w, azCol[i],
368 azArg[i] ? azArg[i] : p->nullvalue);
drh75897232000-05-29 14:26:00 +0000369 }
370 break;
371 }
372 case MODE_Column: {
drha0c66f52000-07-29 13:20:21 +0000373 if( p->cnt++==0 ){
drh75897232000-05-29 14:26:00 +0000374 for(i=0; i<nArg; i++){
drha0c66f52000-07-29 13:20:21 +0000375 int w, n;
376 if( i<ArraySize(p->colWidth) ){
drh75897232000-05-29 14:26:00 +0000377 w = p->colWidth[i];
378 }else{
drha0c66f52000-07-29 13:20:21 +0000379 w = 0;
drh75897232000-05-29 14:26:00 +0000380 }
drha0c66f52000-07-29 13:20:21 +0000381 if( w<=0 ){
drhff6e9112000-08-28 16:21:58 +0000382 w = strlen(azCol[i] ? azCol[i] : "");
drha0c66f52000-07-29 13:20:21 +0000383 if( w<10 ) w = 10;
persicom7e2dfdd2002-04-18 02:46:52 +0000384 n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
drha0c66f52000-07-29 13:20:21 +0000385 if( w<n ) w = n;
386 }
387 if( i<ArraySize(p->actualWidth) ){
persicom1d0b8722002-04-18 02:53:04 +0000388 p->actualWidth[i] = w;
drha0c66f52000-07-29 13:20:21 +0000389 }
390 if( p->showHeader ){
391 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
392 }
393 }
394 if( p->showHeader ){
395 for(i=0; i<nArg; i++){
396 int w;
397 if( i<ArraySize(p->actualWidth) ){
398 w = p->actualWidth[i];
399 }else{
400 w = 10;
401 }
402 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
403 "----------------------------------------------------------",
404 i==nArg-1 ? "\n": " ");
405 }
drh75897232000-05-29 14:26:00 +0000406 }
407 }
drh6a535342001-10-19 16:44:56 +0000408 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000409 for(i=0; i<nArg; i++){
410 int w;
drha0c66f52000-07-29 13:20:21 +0000411 if( i<ArraySize(p->actualWidth) ){
412 w = p->actualWidth[i];
drh75897232000-05-29 14:26:00 +0000413 }else{
414 w = 10;
415 }
drhc61053b2000-06-04 12:58:36 +0000416 fprintf(p->out,"%-*.*s%s",w,w,
persicom7e2dfdd2002-04-18 02:46:52 +0000417 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
drh75897232000-05-29 14:26:00 +0000418 }
419 break;
420 }
drhe3710332000-09-29 13:30:53 +0000421 case MODE_Semi:
drh75897232000-05-29 14:26:00 +0000422 case MODE_List: {
423 if( p->cnt++==0 && p->showHeader ){
424 for(i=0; i<nArg; i++){
425 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
426 }
427 }
drh6a535342001-10-19 16:44:56 +0000428 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000429 for(i=0; i<nArg; i++){
drh4c653a02000-06-07 01:27:47 +0000430 char *z = azArg[i];
persicom7e2dfdd2002-04-18 02:46:52 +0000431 if( z==0 ) z = p->nullvalue;
drh71172c52002-01-24 00:00:21 +0000432 fprintf(p->out, "%s", z);
drhe3710332000-09-29 13:30:53 +0000433 if( i<nArg-1 ){
434 fprintf(p->out, "%s", p->separator);
435 }else if( p->mode==MODE_Semi ){
436 fprintf(p->out, ";\n");
437 }else{
438 fprintf(p->out, "\n");
439 }
drh75897232000-05-29 14:26:00 +0000440 }
441 break;
442 }
drh1e5d0e92000-05-31 23:33:17 +0000443 case MODE_Html: {
444 if( p->cnt++==0 && p->showHeader ){
445 fprintf(p->out,"<TR>");
446 for(i=0; i<nArg; i++){
447 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
448 }
449 fprintf(p->out,"</TR>\n");
450 }
drh6a535342001-10-19 16:44:56 +0000451 if( azArg==0 ) break;
drh28bd4bc2000-06-15 15:57:22 +0000452 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +0000453 for(i=0; i<nArg; i++){
drhc08a4f12000-06-15 16:49:48 +0000454 fprintf(p->out,"<TD>");
persicom7e2dfdd2002-04-18 02:46:52 +0000455 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
drhc08a4f12000-06-15 16:49:48 +0000456 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +0000457 }
drh7d686b22002-11-11 13:56:47 +0000458 fprintf(p->out,"</TR>\n");
drh1e5d0e92000-05-31 23:33:17 +0000459 break;
460 }
drhfeac5f82004-08-01 00:10:45 +0000461 case MODE_Tcl: {
462 if( p->cnt++==0 && p->showHeader ){
463 for(i=0; i<nArg; i++){
464 output_c_string(p->out,azCol[i]);
465 fprintf(p->out, "%s", p->separator);
466 }
467 fprintf(p->out,"\n");
468 }
469 if( azArg==0 ) break;
470 for(i=0; i<nArg; i++){
471 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
472 fprintf(p->out, "%s", p->separator);
473 }
474 fprintf(p->out,"\n");
475 break;
476 }
drh28bd4bc2000-06-15 15:57:22 +0000477 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +0000478 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +0000479 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +0000480 for(i=0; i<nArg; i++){
481 char *zSep = i>0 ? ",": "";
482 if( azArg[i]==0 ){
483 fprintf(p->out,"%sNULL",zSep);
drhc8d74412004-08-31 23:41:26 +0000484 }else if( isNumber(azArg[i], 0) ){
drh28bd4bc2000-06-15 15:57:22 +0000485 fprintf(p->out,"%s%s",zSep, azArg[i]);
486 }else{
487 if( zSep[0] ) fprintf(p->out,"%s",zSep);
488 output_quoted_string(p->out, azArg[i]);
489 }
490 }
491 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +0000492 break;
drh28bd4bc2000-06-15 15:57:22 +0000493 }
persicom1d0b8722002-04-18 02:53:04 +0000494 }
drh75897232000-05-29 14:26:00 +0000495 return 0;
496}
497
498/*
drh33048c02001-10-01 14:29:22 +0000499** Set the destination table field of the callback_data structure to
500** the name of the table given. Escape any quote characters in the
501** table name.
502*/
503static void set_table_name(struct callback_data *p, const char *zName){
504 int i, n;
505 int needQuote;
506 char *z;
507
508 if( p->zDestTable ){
509 free(p->zDestTable);
510 p->zDestTable = 0;
511 }
512 if( zName==0 ) return;
drh4c755c02004-08-08 20:22:17 +0000513 needQuote = !isalpha((unsigned char)*zName) && *zName!='_';
drh33048c02001-10-01 14:29:22 +0000514 for(i=n=0; zName[i]; i++, n++){
drh4c755c02004-08-08 20:22:17 +0000515 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){
drh33048c02001-10-01 14:29:22 +0000516 needQuote = 1;
517 if( zName[i]=='\'' ) n++;
518 }
519 }
520 if( needQuote ) n += 2;
521 z = p->zDestTable = malloc( n+1 );
522 if( z==0 ){
523 fprintf(stderr,"Out of memory!\n");
524 exit(1);
525 }
526 n = 0;
527 if( needQuote ) z[n++] = '\'';
528 for(i=0; zName[i]; i++){
529 z[n++] = zName[i];
530 if( zName[i]=='\'' ) z[n++] = '\'';
531 }
532 if( needQuote ) z[n++] = '\'';
533 z[n] = 0;
534}
535
danielk19772a02e332004-06-05 08:04:36 +0000536/* zIn is either a pointer to a NULL-terminated string in memory obtained
537** from malloc(), or a NULL pointer. The string pointed to by zAppend is
538** added to zIn, and the result returned in memory obtained from malloc().
539** zIn, if it was not NULL, is freed.
540**
541** If the third argument, quote, is not '\0', then it is used as a
542** quote character for zAppend.
543*/
544static char * appendText(char *zIn, char const *zAppend, char quote){
545 int len;
546 int i;
547 int nAppend = strlen(zAppend);
548 int nIn = (zIn?strlen(zIn):0);
549
550 len = nAppend+nIn+1;
551 if( quote ){
552 len += 2;
553 for(i=0; i<nAppend; i++){
554 if( zAppend[i]==quote ) len++;
555 }
556 }
557
558 zIn = (char *)realloc(zIn, len);
559 if( !zIn ){
560 return 0;
561 }
562
563 if( quote ){
564 char *zCsr = &zIn[nIn];
565 *zCsr++ = quote;
566 for(i=0; i<nAppend; i++){
567 *zCsr++ = zAppend[i];
568 if( zAppend[i]==quote ) *zCsr++ = quote;
569 }
570 *zCsr++ = quote;
571 *zCsr++ = '\0';
572 assert( (zCsr-zIn)==len );
573 }else{
574 memcpy(&zIn[nIn], zAppend, nAppend);
575 zIn[len-1] = '\0';
576 }
577
578 return zIn;
579}
580
drhdd3d4592004-08-30 01:54:05 +0000581
582/*
583** Execute a query statement that has a single result column. Print
584** that result column on a line by itself with a semicolon terminator.
585*/
586static int run_table_dump_query(FILE *out, sqlite3 *db, const char *zSelect){
587 sqlite3_stmt *pSelect;
588 int rc;
589 rc = sqlite3_prepare(db, zSelect, -1, &pSelect, 0);
590 if( rc!=SQLITE_OK || !pSelect ){
591 return rc;
592 }
593 rc = sqlite3_step(pSelect);
594 while( rc==SQLITE_ROW ){
595 fprintf(out, "%s;\n", sqlite3_column_text(pSelect, 0));
596 rc = sqlite3_step(pSelect);
597 }
598 return sqlite3_finalize(pSelect);
599}
600
601
drh33048c02001-10-01 14:29:22 +0000602/*
drh4c653a02000-06-07 01:27:47 +0000603** This is a different callback routine used for dumping the database.
604** Each row received by this callback consists of a table name,
605** the table type ("index" or "table") and SQL to create the table.
606** This routine should print text sufficient to recreate the table.
607*/
608static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
danielk19772a02e332004-06-05 08:04:36 +0000609 int rc;
610 const char *zTable;
611 const char *zType;
612 const char *zSql;
drhdaffd0e2001-04-11 14:28:42 +0000613 struct callback_data *p = (struct callback_data *)pArg;
danielk19772a02e332004-06-05 08:04:36 +0000614
drh4c653a02000-06-07 01:27:47 +0000615 if( nArg!=3 ) return 1;
danielk19772a02e332004-06-05 08:04:36 +0000616 zTable = azArg[0];
617 zType = azArg[1];
618 zSql = azArg[2];
619
620 fprintf(p->out, "%s;\n", zSql);
621
622 if( strcmp(zType, "table")==0 ){
623 sqlite3_stmt *pTableInfo = 0;
danielk19772a02e332004-06-05 08:04:36 +0000624 char *zSelect = 0;
625 char *zTableInfo = 0;
626 char *zTmp = 0;
627
628 zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
629 zTableInfo = appendText(zTableInfo, zTable, '"');
630 zTableInfo = appendText(zTableInfo, ");", 0);
631
632 rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0);
633 if( zTableInfo ) free(zTableInfo);
634 if( rc!=SQLITE_OK || !pTableInfo ){
635 return 1;
636 }
637
638 zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
639 zTmp = appendText(zTmp, zTable, '"');
640 if( zTmp ){
641 zSelect = appendText(zSelect, zTmp, '\'');
642 }
643 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
644 rc = sqlite3_step(pTableInfo);
645 while( rc==SQLITE_ROW ){
danielk19773f41e972004-06-08 00:39:01 +0000646 zSelect = appendText(zSelect, "quote(", 0);
danielk19772a02e332004-06-05 08:04:36 +0000647 zSelect = appendText(zSelect, sqlite3_column_text(pTableInfo, 1), '"');
648 rc = sqlite3_step(pTableInfo);
649 if( rc==SQLITE_ROW ){
650 zSelect = appendText(zSelect, ") || ', ' || ", 0);
651 }else{
652 zSelect = appendText(zSelect, ") ", 0);
653 }
654 }
655 rc = sqlite3_finalize(pTableInfo);
656 if( rc!=SQLITE_OK ){
657 if( zSelect ) free(zSelect);
658 return 1;
659 }
660 zSelect = appendText(zSelect, "|| ')' FROM ", 0);
661 zSelect = appendText(zSelect, zTable, '"');
662
drhdd3d4592004-08-30 01:54:05 +0000663 rc = run_table_dump_query(p->out, p->db, zSelect);
664 if( rc==SQLITE_CORRUPT ){
665 zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
666 rc = run_table_dump_query(p->out, p->db, zSelect);
667 }
danielk19772a02e332004-06-05 08:04:36 +0000668 if( zSelect ) free(zSelect);
danielk19772a02e332004-06-05 08:04:36 +0000669 if( rc!=SQLITE_OK ){
670 return 1;
671 }
drh4c653a02000-06-07 01:27:47 +0000672 }
drh4c653a02000-06-07 01:27:47 +0000673 return 0;
674}
675
676/*
drhdd3d4592004-08-30 01:54:05 +0000677** Run zQuery. Update dump_callback() as the callback routine.
678** If we get a SQLITE_CORRUPT error, rerun the query after appending
679** "ORDER BY rowid DESC" to the end.
680*/
681static int run_schema_dump_query(
682 struct callback_data *p,
683 const char *zQuery,
684 char **pzErrMsg
685){
686 int rc;
687 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, pzErrMsg);
688 if( rc==SQLITE_CORRUPT ){
689 char *zQ2;
690 int len = strlen(zQuery);
691 if( pzErrMsg ) sqlite3_free(*pzErrMsg);
692 zQ2 = malloc( len+100 );
693 if( zQ2==0 ) return rc;
694 sprintf(zQ2, "%s ORDER BY rowid DESC", zQuery);
695 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, pzErrMsg);
696 free(zQ2);
697 }
698 return rc;
699}
700
701/*
drh75897232000-05-29 14:26:00 +0000702** Text of a help message
703*/
persicom1d0b8722002-04-18 02:53:04 +0000704static char zHelp[] =
jplyon6a65bb32003-05-04 07:25:57 +0000705 ".databases List names and files of attached databases\n"
drhb860bc92004-08-04 15:16:55 +0000706 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
drhdaffd0e2001-04-11 14:28:42 +0000707 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +0000708 ".exit Exit this program\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000709 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000710 ".header(s) ON|OFF Turn display of headers on or off\n"
drh75897232000-05-29 14:26:00 +0000711 ".help Show this message\n"
drhb860bc92004-08-04 15:16:55 +0000712 ".import FILE TABLE Import data from FILE into TABLE\n"
drh75897232000-05-29 14:26:00 +0000713 ".indices TABLE Show names of all indices on TABLE\n"
drhb860bc92004-08-04 15:16:55 +0000714 ".mode MODE ?TABLE? Set output mode where MODE is on of:\n"
715 " cvs Comma-separated values\n"
716 " column Left-aligned columns. (See .width)\n"
717 " html HTML <table> code\n"
718 " insert SQL insert statements for TABLE\n"
719 " line One value per line\n"
720 " list Values delimited by .separator string\n"
721 " tabs Tab-separated values\n"
722 " tcl TCL list elements\n"
723 ".nullvalue STRING Print STRING in place of NULL values\n"
drh75897232000-05-29 14:26:00 +0000724 ".output FILENAME Send output to FILENAME\n"
725 ".output stdout Send output to the screen\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000726 ".prompt MAIN CONTINUE Replace the standard prompts\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000727 ".quit Exit this program\n"
drhdaffd0e2001-04-11 14:28:42 +0000728 ".read FILENAME Execute SQL in FILENAME\n"
drh9eb9e262004-02-11 02:18:05 +0000729#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000730 ".rekey OLD NEW NEW Change the encryption key\n"
731#endif
drh75897232000-05-29 14:26:00 +0000732 ".schema ?TABLE? Show the CREATE statements\n"
drhb860bc92004-08-04 15:16:55 +0000733 ".separator STRING Change separator used by output mode and .import\n"
drhdd45df82002-04-18 12:39:03 +0000734 ".show Show the current values for various settings\n"
drhfeac5f82004-08-01 00:10:45 +0000735 ".tables ?PATTERN? List names of tables matching a LIKE pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000736 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000737 ".width NUM NUM ... Set column widths for \"column\" mode\n"
738;
739
drhdaffd0e2001-04-11 14:28:42 +0000740/* Forward reference */
741static void process_input(struct callback_data *p, FILE *in);
742
drh75897232000-05-29 14:26:00 +0000743/*
drh44c2eb12003-04-30 11:38:26 +0000744** Make sure the database is open. If it is not, then open it. If
745** the database fails to open, print an error message and exit.
746*/
747static void open_db(struct callback_data *p){
748 if( p->db==0 ){
danielk19774f057f92004-06-08 00:02:33 +0000749 sqlite3_open(p->zDbFilename, &p->db);
danielk197780290862004-05-22 09:21:21 +0000750 db = p->db;
drh2011d5f2004-07-22 02:40:37 +0000751#ifdef SQLITE_HAS_CODEC
752 sqlite3_key(p->db, p->zKey, p->zKey ? strlen(p->zKey) : 0);
drheb8ed702004-02-11 10:37:23 +0000753#endif
danielk1977bc6ada42004-06-30 08:20:16 +0000754 sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
755 shellstaticFunc, 0, 0);
danielk197780290862004-05-22 09:21:21 +0000756 if( SQLITE_OK!=sqlite3_errcode(db) ){
757 fprintf(stderr,"Unable to open database \"%s\": %s\n",
758 p->zDbFilename, sqlite3_errmsg(db));
drh22fbcb82004-02-01 01:22:50 +0000759 exit(1);
drh44c2eb12003-04-30 11:38:26 +0000760 }
761 }
762}
763
764/*
drhfeac5f82004-08-01 00:10:45 +0000765** Do C-language style dequoting.
766**
767** \t -> tab
768** \n -> newline
769** \r -> carriage return
770** \NNN -> ascii character NNN in octal
771** \\ -> backslash
772*/
773static void resolve_backslashes(char *z){
774 int i, j, c;
775 for(i=j=0; (c = z[i])!=0; i++, j++){
776 if( c=='\\' ){
777 c = z[++i];
778 if( c=='n' ){
779 c = '\n';
780 }else if( c=='t' ){
781 c = '\t';
782 }else if( c=='r' ){
783 c = '\r';
784 }else if( c>='0' && c<='7' ){
785 c =- '0';
786 if( z[i+1]>='0' && z[i+1]<='7' ){
787 i++;
788 c = (c<<3) + z[i] - '0';
789 if( z[i+1]>='0' && z[i+1]<='7' ){
790 i++;
791 c = (c<<3) + z[i] - '0';
792 }
793 }
794 }
795 }
796 z[j] = c;
797 }
798 z[j] = 0;
799}
800
801/*
drh75897232000-05-29 14:26:00 +0000802** If an input line begins with "." then invoke this routine to
803** process that line.
drh67505e72002-04-19 12:34:06 +0000804**
805** Return 1 to exit and 0 to continue.
drh75897232000-05-29 14:26:00 +0000806*/
drh44c2eb12003-04-30 11:38:26 +0000807static int do_meta_command(char *zLine, struct callback_data *p){
drh75897232000-05-29 14:26:00 +0000808 int i = 1;
809 int nArg = 0;
810 int n, c;
drh67505e72002-04-19 12:34:06 +0000811 int rc = 0;
drh75897232000-05-29 14:26:00 +0000812 char *azArg[50];
813
814 /* Parse the input line into tokens.
815 */
816 while( zLine[i] && nArg<ArraySize(azArg) ){
drh4c755c02004-08-08 20:22:17 +0000817 while( isspace((unsigned char)zLine[i]) ){ i++; }
drh06333682004-03-09 13:37:45 +0000818 if( zLine[i]==0 ) break;
drh75897232000-05-29 14:26:00 +0000819 if( zLine[i]=='\'' || zLine[i]=='"' ){
820 int delim = zLine[i++];
821 azArg[nArg++] = &zLine[i];
822 while( zLine[i] && zLine[i]!=delim ){ i++; }
823 if( zLine[i]==delim ){
824 zLine[i++] = 0;
825 }
drhfeac5f82004-08-01 00:10:45 +0000826 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
drh75897232000-05-29 14:26:00 +0000827 }else{
828 azArg[nArg++] = &zLine[i];
drh4c755c02004-08-08 20:22:17 +0000829 while( zLine[i] && !isspace((unsigned char)zLine[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +0000830 if( zLine[i] ) zLine[i++] = 0;
drhfeac5f82004-08-01 00:10:45 +0000831 resolve_backslashes(azArg[nArg-1]);
drh75897232000-05-29 14:26:00 +0000832 }
833 }
834
835 /* Process the input line.
836 */
drh67505e72002-04-19 12:34:06 +0000837 if( nArg==0 ) return rc;
drh75897232000-05-29 14:26:00 +0000838 n = strlen(azArg[0]);
839 c = azArg[0][0];
jplyon6a65bb32003-05-04 07:25:57 +0000840 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
jplyon672a1ed2003-05-11 20:07:05 +0000841 struct callback_data data;
842 char *zErrMsg = 0;
jplyon6a65bb32003-05-04 07:25:57 +0000843 open_db(p);
jplyon672a1ed2003-05-11 20:07:05 +0000844 memcpy(&data, p, sizeof(data));
drhd8885442004-03-17 23:42:12 +0000845 data.showHeader = 1;
jplyon672a1ed2003-05-11 20:07:05 +0000846 data.mode = MODE_Column;
drhd8885442004-03-17 23:42:12 +0000847 data.colWidth[0] = 3;
848 data.colWidth[1] = 15;
849 data.colWidth[2] = 58;
danielk19776f8a5032004-05-10 10:34:51 +0000850 sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
jplyon672a1ed2003-05-11 20:07:05 +0000851 if( zErrMsg ){
852 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000853 sqlite3_free(zErrMsg);
jplyon6a65bb32003-05-04 07:25:57 +0000854 }
855 }else
856
drh4c653a02000-06-07 01:27:47 +0000857 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
858 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000859 open_db(p);
drh33048c02001-10-01 14:29:22 +0000860 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000861 if( nArg==1 ){
drhdd3d4592004-08-30 01:54:05 +0000862 run_schema_dump_query(p,
drha18c5682000-10-08 22:20:57 +0000863 "SELECT name, type, sql FROM sqlite_master "
drhdd3d4592004-08-30 01:54:05 +0000864 "WHERE sql NOT NULL AND type=='table'", 0
865 );
866 run_schema_dump_query(p,
867 "SELECT name, type, sql FROM sqlite_master "
868 "WHERE sql NOT NULL AND type!='table' AND type!='meta'", 0
drha18c5682000-10-08 22:20:57 +0000869 );
drh4c653a02000-06-07 01:27:47 +0000870 }else{
871 int i;
drhdd3d4592004-08-30 01:54:05 +0000872 for(i=1; i<nArg; i++){
danielk1977bc6ada42004-06-30 08:20:16 +0000873 zShellStatic = azArg[i];
drhdd3d4592004-08-30 01:54:05 +0000874 run_schema_dump_query(p,
drha18c5682000-10-08 22:20:57 +0000875 "SELECT name, type, sql FROM sqlite_master "
drhdd3d4592004-08-30 01:54:05 +0000876 "WHERE tbl_name LIKE shellstatic() AND type=='table'"
877 " AND sql NOT NULL", 0);
878 run_schema_dump_query(p,
879 "SELECT name, type, sql FROM sqlite_master "
880 "WHERE tbl_name LIKE shellstatic() AND type!='table'"
881 " AND type!='meta' AND sql NOT NULL", 0);
danielk1977bc6ada42004-06-30 08:20:16 +0000882 zShellStatic = 0;
drh4c653a02000-06-07 01:27:47 +0000883 }
884 }
885 if( zErrMsg ){
886 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000887 sqlite3_free(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000888 }else{
889 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000890 }
891 }else
drh75897232000-05-29 14:26:00 +0000892
drhdaffd0e2001-04-11 14:28:42 +0000893 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
894 int j;
895 char *z = azArg[1];
896 int val = atoi(azArg[1]);
897 for(j=0; z[j]; j++){
drh4c755c02004-08-08 20:22:17 +0000898 z[j] = tolower((unsigned char)z[j]);
drhdaffd0e2001-04-11 14:28:42 +0000899 }
900 if( strcmp(z,"on")==0 ){
901 val = 1;
902 }else if( strcmp(z,"yes")==0 ){
903 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000904 }
drhdaffd0e2001-04-11 14:28:42 +0000905 p->echoOn = val;
906 }else
907
drh75897232000-05-29 14:26:00 +0000908 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh67505e72002-04-19 12:34:06 +0000909 rc = 1;
drh75897232000-05-29 14:26:00 +0000910 }else
911
drhdd45df82002-04-18 12:39:03 +0000912 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +0000913 int j;
drh472cbf62004-08-14 18:18:44 +0000914 static char zOne[] = "1";
915 char *z = nArg>=2 ? azArg[1] : zOne;
drhdd45df82002-04-18 12:39:03 +0000916 int val = atoi(z);
persicom7e2dfdd2002-04-18 02:46:52 +0000917 for(j=0; z[j]; j++){
drh4c755c02004-08-08 20:22:17 +0000918 z[j] = tolower((unsigned char)z[j]);
persicom7e2dfdd2002-04-18 02:46:52 +0000919 }
920 if( strcmp(z,"on")==0 ){
921 val = 1;
922 }else if( strcmp(z,"yes")==0 ){
923 val = 1;
924 }
925 if(val == 1) {
926 if(!p->explainPrev.valid) {
927 p->explainPrev.valid = 1;
928 p->explainPrev.mode = p->mode;
929 p->explainPrev.showHeader = p->showHeader;
930 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
931 }
932 /* We could put this code under the !p->explainValid
933 ** condition so that it does not execute if we are already in
934 ** explain mode. However, always executing it allows us an easy
935 ** was to reset to explain mode in case the user previously
936 ** did an .explain followed by a .width, .mode or .header
937 ** command.
938 */
939 p->mode = MODE_Column;
940 p->showHeader = 1;
941 memset(p->colWidth,0,ArraySize(p->colWidth));
942 p->colWidth[0] = 4;
943 p->colWidth[1] = 12;
944 p->colWidth[2] = 10;
945 p->colWidth[3] = 10;
946 p->colWidth[4] = 35;
947 }else if (p->explainPrev.valid) {
948 p->explainPrev.valid = 0;
949 p->mode = p->explainPrev.mode;
950 p->showHeader = p->explainPrev.showHeader;
951 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
952 }
drh75897232000-05-29 14:26:00 +0000953 }else
954
persicom7e2dfdd2002-04-18 02:46:52 +0000955 if( c=='h' && (strncmp(azArg[0], "header", n)==0
956 ||
957 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
drh75897232000-05-29 14:26:00 +0000958 int j;
959 char *z = azArg[1];
960 int val = atoi(azArg[1]);
961 for(j=0; z[j]; j++){
drh4c755c02004-08-08 20:22:17 +0000962 z[j] = tolower((unsigned char)z[j]);
drh75897232000-05-29 14:26:00 +0000963 }
964 if( strcmp(z,"on")==0 ){
965 val = 1;
966 }else if( strcmp(z,"yes")==0 ){
967 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000968 }
drh75897232000-05-29 14:26:00 +0000969 p->showHeader = val;
970 }else
971
972 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
973 fprintf(stderr,zHelp);
974 }else
975
drhfeac5f82004-08-01 00:10:45 +0000976 if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg>=3 ){
977 char *zTable = azArg[2]; /* Insert data into this table */
978 char *zFile = azArg[1]; /* The file from which to extract data */
979 sqlite3_stmt *pStmt; /* A statement */
980 int rc; /* Result code */
981 int nCol; /* Number of columns in the table */
982 int nByte; /* Number of bytes in an SQL string */
983 int i, j; /* Loop counters */
984 int nSep; /* Number of bytes in p->separator[] */
985 char *zSql; /* An SQL statement */
986 char *zLine; /* A single line of input from the file */
987 char **azCol; /* zLine[] broken up into columns */
988 char *zCommit; /* How to commit changes */
drhb860bc92004-08-04 15:16:55 +0000989 FILE *in; /* The input file */
990 int lineno = 0; /* Line number of input file */
drhfeac5f82004-08-01 00:10:45 +0000991
992 nSep = strlen(p->separator);
993 if( nSep==0 ){
994 fprintf(stderr, "non-null separator required for import\n");
995 return 0;
996 }
997 zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
998 if( zSql==0 ) return 0;
999 nByte = strlen(zSql);
1000 rc = sqlite3_prepare(p->db, zSql, 0, &pStmt, 0);
1001 sqlite3_free(zSql);
1002 if( rc ){
1003 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
1004 nCol = 0;
1005 }else{
1006 nCol = sqlite3_column_count(pStmt);
1007 }
1008 sqlite3_finalize(pStmt);
1009 if( nCol==0 ) return 0;
1010 zSql = malloc( nByte + 20 + nCol*2 );
1011 if( zSql==0 ) return 0;
1012 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable);
1013 j = strlen(zSql);
1014 for(i=1; i<nCol; i++){
1015 zSql[j++] = ',';
1016 zSql[j++] = '?';
1017 }
1018 zSql[j++] = ')';
1019 zSql[j] = 0;
1020 rc = sqlite3_prepare(p->db, zSql, 0, &pStmt, 0);
1021 free(zSql);
1022 if( rc ){
1023 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
1024 sqlite3_finalize(pStmt);
1025 return 0;
1026 }
1027 in = fopen(zFile, "rb");
1028 if( in==0 ){
1029 fprintf(stderr, "cannot open file: %s\n", zFile);
1030 sqlite3_finalize(pStmt);
1031 return 0;
1032 }
1033 azCol = malloc( sizeof(azCol[0])*(nCol+1) );
1034 if( azCol==0 ) return 0;
1035 sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
1036 zCommit = "COMMIT";
1037 while( (zLine = local_getline(0, in))!=0 ){
1038 char *z;
1039 i = 0;
drhb860bc92004-08-04 15:16:55 +00001040 lineno++;
drhfeac5f82004-08-01 00:10:45 +00001041 azCol[0] = zLine;
1042 for(i=0, z=zLine; *z; z++){
1043 if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){
1044 *z = 0;
1045 i++;
drhb860bc92004-08-04 15:16:55 +00001046 if( i<nCol ){
1047 azCol[i] = &z[nSep];
1048 z += nSep-1;
1049 }
drhfeac5f82004-08-01 00:10:45 +00001050 }
1051 }
drhb860bc92004-08-04 15:16:55 +00001052 if( i+1!=nCol ){
1053 fprintf(stderr,"%s line %d: expected %d columns of data but found %d\n",
1054 zFile, lineno, nCol, i+1);
1055 zCommit = "ROLLBACK";
1056 break;
1057 }
drhfeac5f82004-08-01 00:10:45 +00001058 for(i=0; i<nCol; i++){
1059 sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
1060 }
1061 sqlite3_step(pStmt);
1062 rc = sqlite3_reset(pStmt);
1063 free(zLine);
1064 if( rc!=SQLITE_OK ){
1065 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
1066 zCommit = "ROLLBACK";
1067 break;
1068 }
1069 }
1070 free(azCol);
1071 fclose(in);
1072 sqlite3_finalize(pStmt);
drhb860bc92004-08-04 15:16:55 +00001073 sqlite3_exec(p->db, zCommit, 0, 0, 0);
drhfeac5f82004-08-01 00:10:45 +00001074 }else
1075
drh75897232000-05-29 14:26:00 +00001076 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
1077 struct callback_data data;
1078 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +00001079 open_db(p);
drh75897232000-05-29 14:26:00 +00001080 memcpy(&data, p, sizeof(data));
1081 data.showHeader = 0;
1082 data.mode = MODE_List;
danielk1977bc6ada42004-06-30 08:20:16 +00001083 zShellStatic = azArg[1];
1084 sqlite3_exec(p->db,
drha18c5682000-10-08 22:20:57 +00001085 "SELECT name FROM sqlite_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001086 "WHERE type='index' AND tbl_name LIKE shellstatic() "
drhe0bc4042002-06-25 01:09:11 +00001087 "UNION ALL "
1088 "SELECT name FROM sqlite_temp_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001089 "WHERE type='index' AND tbl_name LIKE shellstatic() "
drhe0bc4042002-06-25 01:09:11 +00001090 "ORDER BY 1",
danielk1977bc6ada42004-06-30 08:20:16 +00001091 callback, &data, &zErrMsg
drha18c5682000-10-08 22:20:57 +00001092 );
danielk1977bc6ada42004-06-30 08:20:16 +00001093 zShellStatic = 0;
drh75897232000-05-29 14:26:00 +00001094 if( zErrMsg ){
1095 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001096 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001097 }
1098 }else
1099
drh28bd4bc2000-06-15 15:57:22 +00001100 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +00001101 int n2 = strlen(azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +00001102 if( strncmp(azArg[1],"line",n2)==0
1103 ||
1104 strncmp(azArg[1],"lines",n2)==0 ){
drh75897232000-05-29 14:26:00 +00001105 p->mode = MODE_Line;
persicom7e2dfdd2002-04-18 02:46:52 +00001106 }else if( strncmp(azArg[1],"column",n2)==0
1107 ||
1108 strncmp(azArg[1],"columns",n2)==0 ){
drh75897232000-05-29 14:26:00 +00001109 p->mode = MODE_Column;
1110 }else if( strncmp(azArg[1],"list",n2)==0 ){
1111 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +00001112 }else if( strncmp(azArg[1],"html",n2)==0 ){
1113 p->mode = MODE_Html;
drhfeac5f82004-08-01 00:10:45 +00001114 }else if( strncmp(azArg[1],"tcl",n2)==0 ){
1115 p->mode = MODE_Tcl;
1116 }else if( strncmp(azArg[1],"csv",n2)==0 ){
1117 p->mode = MODE_List;
1118 strcpy(p->separator, ",");
1119 }else if( strncmp(azArg[1],"tabs",n2)==0 ){
1120 p->mode = MODE_List;
1121 strcpy(p->separator, "\t");
drh28bd4bc2000-06-15 15:57:22 +00001122 }else if( strncmp(azArg[1],"insert",n2)==0 ){
1123 p->mode = MODE_Insert;
1124 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +00001125 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +00001126 }else{
drh33048c02001-10-01 14:29:22 +00001127 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +00001128 }
drhdaffd0e2001-04-11 14:28:42 +00001129 }else {
drhfeac5f82004-08-01 00:10:45 +00001130 fprintf(stderr,"mode should be on of: "
1131 "column csv html insert line list tabs tcl\n");
drh75897232000-05-29 14:26:00 +00001132 }
1133 }else
1134
persicom7e2dfdd2002-04-18 02:46:52 +00001135 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
1136 sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
1137 }else
1138
drh75897232000-05-29 14:26:00 +00001139 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
1140 if( p->out!=stdout ){
1141 fclose(p->out);
1142 }
1143 if( strcmp(azArg[1],"stdout")==0 ){
1144 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +00001145 strcpy(p->outfile,"stdout");
drh75897232000-05-29 14:26:00 +00001146 }else{
drha1f9b5e2004-02-14 16:31:02 +00001147 p->out = fopen(azArg[1], "wb");
drh75897232000-05-29 14:26:00 +00001148 if( p->out==0 ){
1149 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
1150 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +00001151 } else {
1152 strcpy(p->outfile,azArg[1]);
drh75897232000-05-29 14:26:00 +00001153 }
1154 }
1155 }else
1156
drhdd45df82002-04-18 12:39:03 +00001157 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
persicom7e2dfdd2002-04-18 02:46:52 +00001158 if( nArg >= 2) {
1159 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
1160 }
1161 if( nArg >= 3) {
1162 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
1163 }
1164 }else
1165
1166 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
drhf73287c2004-02-25 02:25:37 +00001167 rc = 1;
persicom7e2dfdd2002-04-18 02:46:52 +00001168 }else
1169
drhdaffd0e2001-04-11 14:28:42 +00001170 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
drha1f9b5e2004-02-14 16:31:02 +00001171 FILE *alt = fopen(azArg[1], "rb");
drhdaffd0e2001-04-11 14:28:42 +00001172 if( alt==0 ){
1173 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
1174 }else{
1175 process_input(p, alt);
1176 fclose(alt);
1177 }
1178 }else
1179
drh9eb9e262004-02-11 02:18:05 +00001180#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001181 if( c=='r' && strncmp(azArg[0],"rekey", n)==0 && nArg==4 ){
1182 char *zOld = p->zKey;
1183 if( zOld==0 ) zOld = "";
1184 if( strcmp(azArg[1],zOld) ){
1185 fprintf(stderr,"old key is incorrect\n");
1186 }else if( strcmp(azArg[2], azArg[3]) ){
1187 fprintf(stderr,"2nd copy of new key does not match the 1st\n");
1188 }else{
drh3f4fedb2004-05-31 19:34:33 +00001189 sqlite3_free(p->zKey);
danielk19776f8a5032004-05-10 10:34:51 +00001190 p->zKey = sqlite3_mprintf("%s", azArg[2]);
drh2011d5f2004-07-22 02:40:37 +00001191 sqlite3_rekey(p->db, p->zKey, strlen(p->zKey));
drh22fbcb82004-02-01 01:22:50 +00001192 }
1193 }else
1194#endif
1195
drh75897232000-05-29 14:26:00 +00001196 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
1197 struct callback_data data;
1198 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +00001199 open_db(p);
drh75897232000-05-29 14:26:00 +00001200 memcpy(&data, p, sizeof(data));
1201 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +00001202 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +00001203 if( nArg>1 ){
drhc8d74412004-08-31 23:41:26 +00001204 int i;
1205 for(i=0; azArg[1][i]; i++) azArg[1][i] = tolower(azArg[1][i]);
1206 if( strcmp(azArg[1],"sqlite_master")==0 ){
drha18c5682000-10-08 22:20:57 +00001207 char *new_argv[2], *new_colv[2];
1208 new_argv[0] = "CREATE TABLE sqlite_master (\n"
1209 " type text,\n"
1210 " name text,\n"
1211 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +00001212 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +00001213 " sql text\n"
1214 ")";
1215 new_argv[1] = 0;
1216 new_colv[0] = "sql";
1217 new_colv[1] = 0;
1218 callback(&data, 1, new_argv, new_colv);
drhc8d74412004-08-31 23:41:26 +00001219 }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){
drhe0bc4042002-06-25 01:09:11 +00001220 char *new_argv[2], *new_colv[2];
1221 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
1222 " type text,\n"
1223 " name text,\n"
1224 " tbl_name text,\n"
1225 " rootpage integer,\n"
1226 " sql text\n"
1227 ")";
1228 new_argv[1] = 0;
1229 new_colv[0] = "sql";
1230 new_colv[1] = 0;
1231 callback(&data, 1, new_argv, new_colv);
drha18c5682000-10-08 22:20:57 +00001232 }else{
danielk1977bc6ada42004-06-30 08:20:16 +00001233 zShellStatic = azArg[1];
1234 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +00001235 "SELECT sql FROM "
1236 " (SELECT * FROM sqlite_master UNION ALL"
1237 " SELECT * FROM sqlite_temp_master) "
danielk1977bc6ada42004-06-30 08:20:16 +00001238 "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +00001239 "ORDER BY substr(type,2,1), name",
danielk1977bc6ada42004-06-30 08:20:16 +00001240 callback, &data, &zErrMsg);
1241 zShellStatic = 0;
drha18c5682000-10-08 22:20:57 +00001242 }
drh75897232000-05-29 14:26:00 +00001243 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001244 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +00001245 "SELECT sql FROM "
1246 " (SELECT * FROM sqlite_master UNION ALL"
1247 " SELECT * FROM sqlite_temp_master) "
drh33048c02001-10-01 14:29:22 +00001248 "WHERE type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +00001249 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +00001250 callback, &data, &zErrMsg
1251 );
drh75897232000-05-29 14:26:00 +00001252 }
drh75897232000-05-29 14:26:00 +00001253 if( zErrMsg ){
1254 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001255 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001256 }
1257 }else
1258
1259 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
1260 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
1261 }else
1262
persicom7e2dfdd2002-04-18 02:46:52 +00001263 if( c=='s' && strncmp(azArg[0], "show", n)==0){
1264 int i;
1265 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
drh67505e72002-04-19 12:34:06 +00001266 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
drhdd45df82002-04-18 12:39:03 +00001267 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
persicom7e2dfdd2002-04-18 02:46:52 +00001268 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
drhfeac5f82004-08-01 00:10:45 +00001269 fprintf(p->out,"%9.9s: ", "nullvalue");
1270 output_c_string(p->out, p->nullvalue);
1271 fprintf(p->out, "\n");
drh67505e72002-04-19 12:34:06 +00001272 fprintf(p->out,"%9.9s: %s\n","output",
1273 strlen(p->outfile) ? p->outfile : "stdout");
drhfeac5f82004-08-01 00:10:45 +00001274 fprintf(p->out,"%9.9s: ", "separator");
1275 output_c_string(p->out, p->separator);
1276 fprintf(p->out, "\n");
persicom7e2dfdd2002-04-18 02:46:52 +00001277 fprintf(p->out,"%9.9s: ","width");
1278 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
drhfeac5f82004-08-01 00:10:45 +00001279 fprintf(p->out,"%d ",p->colWidth[i]);
persicom7e2dfdd2002-04-18 02:46:52 +00001280 }
drhfeac5f82004-08-01 00:10:45 +00001281 fprintf(p->out,"\n");
persicom7e2dfdd2002-04-18 02:46:52 +00001282 }else
1283
drh2dfbbca2000-07-28 14:32:48 +00001284 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +00001285 char **azResult;
1286 int nRow, rc;
1287 char *zErrMsg;
drh44c2eb12003-04-30 11:38:26 +00001288 open_db(p);
drha50da102000-08-08 20:19:09 +00001289 if( nArg==1 ){
danielk19776f8a5032004-05-10 10:34:51 +00001290 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +00001291 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +00001292 "WHERE type IN ('table','view') "
drhe0bc4042002-06-25 01:09:11 +00001293 "UNION ALL "
1294 "SELECT name FROM sqlite_temp_master "
1295 "WHERE type IN ('table','view') "
1296 "ORDER BY 1",
drha18c5682000-10-08 22:20:57 +00001297 &azResult, &nRow, 0, &zErrMsg
1298 );
drha50da102000-08-08 20:19:09 +00001299 }else{
danielk1977bc6ada42004-06-30 08:20:16 +00001300 zShellStatic = azArg[1];
1301 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +00001302 "SELECT name FROM sqlite_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001303 "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' "
drhe0bc4042002-06-25 01:09:11 +00001304 "UNION ALL "
1305 "SELECT name FROM sqlite_temp_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001306 "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' "
drhe0bc4042002-06-25 01:09:11 +00001307 "ORDER BY 1",
danielk1977bc6ada42004-06-30 08:20:16 +00001308 &azResult, &nRow, 0, &zErrMsg
drha18c5682000-10-08 22:20:57 +00001309 );
danielk1977bc6ada42004-06-30 08:20:16 +00001310 zShellStatic = 0;
drha50da102000-08-08 20:19:09 +00001311 }
drh75897232000-05-29 14:26:00 +00001312 if( zErrMsg ){
1313 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001314 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001315 }
drhe3710332000-09-29 13:30:53 +00001316 if( rc==SQLITE_OK ){
1317 int len, maxlen = 0;
1318 int i, j;
1319 int nPrintCol, nPrintRow;
1320 for(i=1; i<=nRow; i++){
1321 if( azResult[i]==0 ) continue;
1322 len = strlen(azResult[i]);
1323 if( len>maxlen ) maxlen = len;
1324 }
1325 nPrintCol = 80/(maxlen+2);
1326 if( nPrintCol<1 ) nPrintCol = 1;
1327 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
1328 for(i=0; i<nPrintRow; i++){
1329 for(j=i+1; j<=nRow; j+=nPrintRow){
1330 char *zSp = j<=nPrintRow ? "" : " ";
1331 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
1332 }
1333 printf("\n");
1334 }
1335 }
danielk19776f8a5032004-05-10 10:34:51 +00001336 sqlite3_free_table(azResult);
drh75897232000-05-29 14:26:00 +00001337 }else
1338
drh2dfbbca2000-07-28 14:32:48 +00001339 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
drh44c2eb12003-04-30 11:38:26 +00001340 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001341 sqlite3_busy_timeout(p->db, atoi(azArg[1]));
drh2dfbbca2000-07-28 14:32:48 +00001342 }else
1343
drh75897232000-05-29 14:26:00 +00001344 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
1345 int j;
1346 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
1347 p->colWidth[j-1] = atoi(azArg[j]);
1348 }
1349 }else
1350
1351 {
drh67505e72002-04-19 12:34:06 +00001352 fprintf(stderr, "unknown command or invalid arguments: "
1353 " \"%s\". Enter \".help\" for help\n", azArg[0]);
drh75897232000-05-29 14:26:00 +00001354 }
drh67505e72002-04-19 12:34:06 +00001355
1356 return rc;
drh75897232000-05-29 14:26:00 +00001357}
1358
drh67505e72002-04-19 12:34:06 +00001359/*
drh324ccef2003-02-05 14:06:20 +00001360** Return TRUE if the last non-whitespace character in z[] is a semicolon.
1361** z[] is N characters long.
1362*/
1363static int _ends_with_semicolon(const char *z, int N){
drh4c755c02004-08-08 20:22:17 +00001364 while( N>0 && isspace((unsigned char)z[N-1]) ){ N--; }
drh324ccef2003-02-05 14:06:20 +00001365 return N>0 && z[N-1]==';';
1366}
1367
1368/*
drh70c7a4b2003-04-26 03:03:06 +00001369** Test to see if a line consists entirely of whitespace.
1370*/
1371static int _all_whitespace(const char *z){
1372 for(; *z; z++){
drh4c755c02004-08-08 20:22:17 +00001373 if( isspace(*(unsigned char*)z) ) continue;
drh70c7a4b2003-04-26 03:03:06 +00001374 if( *z=='/' && z[1]=='*' ){
1375 z += 2;
1376 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
1377 if( *z==0 ) return 0;
1378 z++;
1379 continue;
1380 }
1381 if( *z=='-' && z[1]=='-' ){
1382 z += 2;
1383 while( *z && *z!='\n' ){ z++; }
1384 if( *z==0 ) return 1;
1385 continue;
1386 }
1387 return 0;
1388 }
1389 return 1;
1390}
1391
1392/*
drha9b17162003-04-29 18:01:28 +00001393** Return TRUE if the line typed in is an SQL command terminator other
1394** than a semi-colon. The SQL Server style "go" command is understood
1395** as is the Oracle "/".
1396*/
1397static int _is_command_terminator(const char *zLine){
drh4c755c02004-08-08 20:22:17 +00001398 while( isspace(*(unsigned char*)zLine) ){ zLine++; };
drha9b17162003-04-29 18:01:28 +00001399 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */
drhc8d74412004-08-31 23:41:26 +00001400 if( tolower(zLine[0])=='g' && tolower(zLine[1])=='o'
1401 && _all_whitespace(&zLine[2]) ){
drha9b17162003-04-29 18:01:28 +00001402 return 1; /* SQL Server */
1403 }
1404 return 0;
1405}
1406
1407/*
drh67505e72002-04-19 12:34:06 +00001408** Read input from *in and process it. If *in==0 then input
1409** is interactive - the user is typing it it. Otherwise, input
1410** is coming from a file or device. A prompt is issued and history
1411** is saved only if input is interactive. An interrupt signal will
1412** cause this routine to exit immediately, unless input is interactive.
1413*/
drhdaffd0e2001-04-11 14:28:42 +00001414static void process_input(struct callback_data *p, FILE *in){
1415 char *zLine;
1416 char *zSql = 0;
1417 int nSql = 0;
1418 char *zErrMsg;
drh7f953e22002-07-13 17:33:45 +00001419 int rc;
drh41e941d2002-04-04 15:10:12 +00001420 while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
drh67505e72002-04-19 12:34:06 +00001421 if( seenInterrupt ){
1422 if( in!=0 ) break;
1423 seenInterrupt = 0;
1424 }
drhdaffd0e2001-04-11 14:28:42 +00001425 if( p->echoOn ) printf("%s\n", zLine);
drhf817b6b2003-06-16 00:16:41 +00001426 if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
drh2af0b2d2002-02-21 02:25:02 +00001427 if( zLine && zLine[0]=='.' && nSql==0 ){
drh44c2eb12003-04-30 11:38:26 +00001428 int rc = do_meta_command(zLine, p);
drhdaffd0e2001-04-11 14:28:42 +00001429 free(zLine);
drh67505e72002-04-19 12:34:06 +00001430 if( rc ) break;
drhdaffd0e2001-04-11 14:28:42 +00001431 continue;
1432 }
drha9b17162003-04-29 18:01:28 +00001433 if( _is_command_terminator(zLine) ){
1434 strcpy(zLine,";");
1435 }
drhdaffd0e2001-04-11 14:28:42 +00001436 if( zSql==0 ){
1437 int i;
drh4c755c02004-08-08 20:22:17 +00001438 for(i=0; zLine[i] && isspace((unsigned char)zLine[i]); i++){}
drhdaffd0e2001-04-11 14:28:42 +00001439 if( zLine[i]!=0 ){
1440 nSql = strlen(zLine);
1441 zSql = malloc( nSql+1 );
1442 strcpy(zSql, zLine);
1443 }
1444 }else{
1445 int len = strlen(zLine);
1446 zSql = realloc( zSql, nSql + len + 2 );
1447 if( zSql==0 ){
1448 fprintf(stderr,"%s: out of memory!\n", Argv0);
1449 exit(1);
1450 }
1451 strcpy(&zSql[nSql++], "\n");
1452 strcpy(&zSql[nSql], zLine);
1453 nSql += len;
1454 }
1455 free(zLine);
danielk19776f8a5032004-05-10 10:34:51 +00001456 if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite3_complete(zSql) ){
drhdaffd0e2001-04-11 14:28:42 +00001457 p->cnt = 0;
drh44c2eb12003-04-30 11:38:26 +00001458 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001459 rc = sqlite3_exec(p->db, zSql, callback, p, &zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001460 if( rc || zErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +00001461 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
drh7f953e22002-07-13 17:33:45 +00001462 if( zErrMsg!=0 ){
1463 printf("SQL error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001464 sqlite3_free(zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001465 zErrMsg = 0;
1466 }else{
danielk19772a02e332004-06-05 08:04:36 +00001467 printf("SQL error: %s\n", sqlite3_errmsg(p->db));
drh7f953e22002-07-13 17:33:45 +00001468 }
drhdaffd0e2001-04-11 14:28:42 +00001469 }
1470 free(zSql);
1471 zSql = 0;
1472 nSql = 0;
1473 }
1474 }
1475 if( zSql ){
drh70c7a4b2003-04-26 03:03:06 +00001476 if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);
drhdaffd0e2001-04-11 14:28:42 +00001477 free(zSql);
1478 }
1479}
1480
drh67505e72002-04-19 12:34:06 +00001481/*
1482** Return a pathname which is the user's home directory. A
1483** 0 return indicates an error of some kind. Space to hold the
1484** resulting string is obtained from malloc(). The calling
1485** function should free the result.
1486*/
1487static char *find_home_dir(void){
1488 char *home_dir = NULL;
persicom7e2dfdd2002-04-18 02:46:52 +00001489
drh820f3812003-01-08 13:02:52 +00001490#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh67505e72002-04-19 12:34:06 +00001491 struct passwd *pwent;
1492 uid_t uid = getuid();
drhbd842ba2002-08-21 11:26:41 +00001493 if( (pwent=getpwuid(uid)) != NULL) {
1494 home_dir = pwent->pw_dir;
drh67505e72002-04-19 12:34:06 +00001495 }
1496#endif
1497
drh820f3812003-01-08 13:02:52 +00001498#ifdef __MACOS__
1499 char home_path[_MAX_PATH+1];
1500 home_dir = getcwd(home_path, _MAX_PATH);
1501#endif
1502
drh67505e72002-04-19 12:34:06 +00001503 if (!home_dir) {
1504 home_dir = getenv("HOME");
1505 if (!home_dir) {
1506 home_dir = getenv("HOMEPATH"); /* Windows? */
1507 }
1508 }
1509
drhe98d4fa2002-04-21 19:06:22 +00001510#if defined(_WIN32) || defined(WIN32)
1511 if (!home_dir) {
1512 home_dir = "c:";
1513 }
1514#endif
1515
drh67505e72002-04-19 12:34:06 +00001516 if( home_dir ){
1517 char *z = malloc( strlen(home_dir)+1 );
1518 if( z ) strcpy(z, home_dir);
1519 home_dir = z;
1520 }
drhe98d4fa2002-04-21 19:06:22 +00001521
drh67505e72002-04-19 12:34:06 +00001522 return home_dir;
1523}
1524
1525/*
1526** Read input from the file given by sqliterc_override. Or if that
1527** parameter is NULL, take input from ~/.sqliterc
1528*/
drh22fbcb82004-02-01 01:22:50 +00001529static void process_sqliterc(
1530 struct callback_data *p, /* Configuration data */
1531 const char *sqliterc_override /* Name of config file. NULL to use default */
1532){
persicom7e2dfdd2002-04-18 02:46:52 +00001533 char *home_dir = NULL;
drh22fbcb82004-02-01 01:22:50 +00001534 const char *sqliterc = sqliterc_override;
1535 char *zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001536 FILE *in = NULL;
1537
1538 if (sqliterc == NULL) {
drh67505e72002-04-19 12:34:06 +00001539 home_dir = find_home_dir();
drhe98d4fa2002-04-21 19:06:22 +00001540 if( home_dir==0 ){
1541 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
1542 return;
1543 }
drh22fbcb82004-02-01 01:22:50 +00001544 zBuf = malloc(strlen(home_dir) + 15);
1545 if( zBuf==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +00001546 fprintf(stderr,"%s: out of memory!\n", Argv0);
1547 exit(1);
1548 }
drh22fbcb82004-02-01 01:22:50 +00001549 sprintf(zBuf,"%s/.sqliterc",home_dir);
drh67505e72002-04-19 12:34:06 +00001550 free(home_dir);
drh22fbcb82004-02-01 01:22:50 +00001551 sqliterc = (const char*)zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001552 }
drha1f9b5e2004-02-14 16:31:02 +00001553 in = fopen(sqliterc,"rb");
drh22fbcb82004-02-01 01:22:50 +00001554 if( in ){
1555 if( isatty(fileno(stdout)) ){
1556 printf("Loading resources from %s\n",sqliterc);
1557 }
persicom7e2dfdd2002-04-18 02:46:52 +00001558 process_input(p,in);
drhdd45df82002-04-18 12:39:03 +00001559 fclose(in);
persicom7e2dfdd2002-04-18 02:46:52 +00001560 }
1561 return;
1562}
1563
drh67505e72002-04-19 12:34:06 +00001564/*
drhe1e38c42003-05-04 18:30:59 +00001565** Show available command line options
1566*/
1567static const char zOptions[] =
1568 " -init filename read/process named file\n"
1569 " -echo print commands before execution\n"
1570 " -[no]header turn headers on or off\n"
1571 " -column set output mode to 'column'\n"
1572 " -html set output mode to HTML\n"
drh9eb9e262004-02-11 02:18:05 +00001573#ifdef SQLITE_HAS_CODEC
drh57ced912004-02-10 02:57:59 +00001574 " -key KEY encryption key\n"
1575#endif
drhe1e38c42003-05-04 18:30:59 +00001576 " -line set output mode to 'line'\n"
1577 " -list set output mode to 'list'\n"
1578 " -separator 'x' set output field separator (|)\n"
1579 " -nullvalue 'text' set text string for NULL values\n"
1580 " -version show SQLite version\n"
1581 " -help show this text, also show dot-commands\n"
1582;
1583static void usage(int showDetail){
1584 fprintf(stderr, "Usage: %s [OPTIONS] FILENAME [SQL]\n", Argv0);
1585 if( showDetail ){
1586 fprintf(stderr, "Options are:\n%s", zOptions);
1587 }else{
1588 fprintf(stderr, "Use the -help option for additional information\n");
1589 }
1590 exit(1);
1591}
1592
1593/*
drh67505e72002-04-19 12:34:06 +00001594** Initialize the state information in data
1595*/
persicom7e2dfdd2002-04-18 02:46:52 +00001596void main_init(struct callback_data *data) {
1597 memset(data, 0, sizeof(*data));
1598 data->mode = MODE_List;
1599 strcpy(data->separator,"|");
1600 data->showHeader = 0;
1601 strcpy(mainPrompt,"sqlite> ");
1602 strcpy(continuePrompt," ...> ");
1603}
1604
drh75897232000-05-29 14:26:00 +00001605int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +00001606 char *zErrMsg = 0;
1607 struct callback_data data;
drh22fbcb82004-02-01 01:22:50 +00001608 const char *zInitFile = 0;
1609 char *zFirstCmd = 0;
drh44c2eb12003-04-30 11:38:26 +00001610 int i;
drh75897232000-05-29 14:26:00 +00001611
drh820f3812003-01-08 13:02:52 +00001612#ifdef __MACOS__
1613 argc = ccommand(&argv);
drh820f3812003-01-08 13:02:52 +00001614#endif
1615
drhdaffd0e2001-04-11 14:28:42 +00001616 Argv0 = argv[0];
persicom7e2dfdd2002-04-18 02:46:52 +00001617 main_init(&data);
persicom7e2dfdd2002-04-18 02:46:52 +00001618
drh44c2eb12003-04-30 11:38:26 +00001619 /* Make sure we have a valid signal handler early, before anything
1620 ** else is done.
1621 */
drh4c504392000-10-16 22:06:40 +00001622#ifdef SIGINT
1623 signal(SIGINT, interrupt_handler);
1624#endif
drh44c2eb12003-04-30 11:38:26 +00001625
drh22fbcb82004-02-01 01:22:50 +00001626 /* Do an initial pass through the command-line argument to locate
1627 ** the name of the database file, the name of the initialization file,
1628 ** and the first command to execute.
drh44c2eb12003-04-30 11:38:26 +00001629 */
drh22fbcb82004-02-01 01:22:50 +00001630 for(i=1; i<argc-1; i++){
drh44c2eb12003-04-30 11:38:26 +00001631 if( argv[i][0]!='-' ) break;
1632 if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
1633 i++;
drh22fbcb82004-02-01 01:22:50 +00001634 }else if( strcmp(argv[i],"-init")==0 ){
1635 i++;
1636 zInitFile = argv[i];
1637 }else if( strcmp(argv[i],"-key")==0 ){
1638 i++;
danielk19776f8a5032004-05-10 10:34:51 +00001639 data.zKey = sqlite3_mprintf("%s",argv[i]);
drh44c2eb12003-04-30 11:38:26 +00001640 }
1641 }
drh22fbcb82004-02-01 01:22:50 +00001642 if( i<argc ){
1643 data.zDbFilename = argv[i++];
1644 }else{
1645 data.zDbFilename = ":memory:";
1646 }
1647 if( i<argc ){
1648 zFirstCmd = argv[i++];
1649 }
drh44c2eb12003-04-30 11:38:26 +00001650 data.out = stdout;
1651
1652 /* Go ahead and open the database file if it already exists. If the
1653 ** file does not exist, delay opening it. This prevents empty database
1654 ** files from being created if a user mistypes the database name argument
1655 ** to the sqlite command-line tool.
1656 */
drhc8d74412004-08-31 23:41:26 +00001657 if( access(data.zDbFilename, 0)==0 ){
drh44c2eb12003-04-30 11:38:26 +00001658 open_db(&data);
1659 }
1660
drh22fbcb82004-02-01 01:22:50 +00001661 /* Process the initialization file if there is one. If no -init option
1662 ** is given on the command line, look for a file named ~/.sqliterc and
1663 ** try to process it.
drh44c2eb12003-04-30 11:38:26 +00001664 */
drh22fbcb82004-02-01 01:22:50 +00001665 process_sqliterc(&data,zInitFile);
drh44c2eb12003-04-30 11:38:26 +00001666
drh22fbcb82004-02-01 01:22:50 +00001667 /* Make a second pass through the command-line argument and set
1668 ** options. This second pass is delayed until after the initialization
1669 ** file is processed so that the command-line arguments will override
1670 ** settings in the initialization file.
drh44c2eb12003-04-30 11:38:26 +00001671 */
drh22fbcb82004-02-01 01:22:50 +00001672 for(i=1; i<argc && argv[i][0]=='-'; i++){
1673 char *z = argv[i];
1674 if( strcmp(z,"-init")==0 || strcmp(z,"-key")==0 ){
1675 i++;
1676 }else if( strcmp(z,"-html")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001677 data.mode = MODE_Html;
drh22fbcb82004-02-01 01:22:50 +00001678 }else if( strcmp(z,"-list")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001679 data.mode = MODE_List;
drh22fbcb82004-02-01 01:22:50 +00001680 }else if( strcmp(z,"-line")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001681 data.mode = MODE_Line;
drh22fbcb82004-02-01 01:22:50 +00001682 }else if( strcmp(z,"-column")==0 ){
drh8b32e172002-04-08 02:42:57 +00001683 data.mode = MODE_Column;
drh22fbcb82004-02-01 01:22:50 +00001684 }else if( strcmp(z,"-separator")==0 ){
1685 i++;
1686 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[i]);
1687 }else if( strcmp(z,"-nullvalue")==0 ){
1688 i++;
1689 sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
1690 }else if( strcmp(z,"-header")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001691 data.showHeader = 1;
drh22fbcb82004-02-01 01:22:50 +00001692 }else if( strcmp(z,"-noheader")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001693 data.showHeader = 0;
drh22fbcb82004-02-01 01:22:50 +00001694 }else if( strcmp(z,"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001695 data.echoOn = 1;
drh22fbcb82004-02-01 01:22:50 +00001696 }else if( strcmp(z,"-version")==0 ){
drhc8d74412004-08-31 23:41:26 +00001697 printf("%s\n", sqlite3_libversion());
drhe1e38c42003-05-04 18:30:59 +00001698 return 1;
drh22fbcb82004-02-01 01:22:50 +00001699 }else if( strcmp(z,"-help")==0 ){
drhe1e38c42003-05-04 18:30:59 +00001700 usage(1);
drh1e5d0e92000-05-31 23:33:17 +00001701 }else{
drh22fbcb82004-02-01 01:22:50 +00001702 fprintf(stderr,"%s: unknown option: %s\n", Argv0, z);
drhe1e38c42003-05-04 18:30:59 +00001703 fprintf(stderr,"Use -help for a list of options.\n");
drh1e5d0e92000-05-31 23:33:17 +00001704 return 1;
1705 }
1706 }
drh44c2eb12003-04-30 11:38:26 +00001707
drh22fbcb82004-02-01 01:22:50 +00001708 if( zFirstCmd ){
drh44c2eb12003-04-30 11:38:26 +00001709 /* Run just the command that follows the database name
1710 */
drh22fbcb82004-02-01 01:22:50 +00001711 if( zFirstCmd[0]=='.' ){
1712 do_meta_command(zFirstCmd, &data);
drh6ff13852001-11-25 13:18:23 +00001713 exit(0);
1714 }else{
1715 int rc;
drh44c2eb12003-04-30 11:38:26 +00001716 open_db(&data);
danielk19776f8a5032004-05-10 10:34:51 +00001717 rc = sqlite3_exec(data.db, zFirstCmd, callback, &data, &zErrMsg);
drh6ff13852001-11-25 13:18:23 +00001718 if( rc!=0 && zErrMsg!=0 ){
1719 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1720 exit(1);
1721 }
drh75897232000-05-29 14:26:00 +00001722 }
1723 }else{
drh44c2eb12003-04-30 11:38:26 +00001724 /* Run commands received from standard input
1725 */
drh5cf590c2003-04-24 01:45:04 +00001726 if( isatty(fileno(stdout)) && isatty(fileno(stdin)) ){
drh67505e72002-04-19 12:34:06 +00001727 char *zHome;
1728 char *zHistory = 0;
drh75897232000-05-29 14:26:00 +00001729 printf(
drhb217a572000-08-22 13:40:18 +00001730 "SQLite version %s\n"
1731 "Enter \".help\" for instructions\n",
drhc8d74412004-08-31 23:41:26 +00001732 sqlite3_libversion()
drh75897232000-05-29 14:26:00 +00001733 );
drh67505e72002-04-19 12:34:06 +00001734 zHome = find_home_dir();
1735 if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){
1736 sprintf(zHistory,"%s/.sqlite_history", zHome);
1737 }
1738 if( zHistory ) read_history(zHistory);
drhdaffd0e2001-04-11 14:28:42 +00001739 process_input(&data, 0);
drh67505e72002-04-19 12:34:06 +00001740 if( zHistory ){
1741 stifle_history(100);
1742 write_history(zHistory);
1743 }
drhdaffd0e2001-04-11 14:28:42 +00001744 }else{
1745 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +00001746 }
1747 }
drh33048c02001-10-01 14:29:22 +00001748 set_table_name(&data, 0);
danielk19776f8a5032004-05-10 10:34:51 +00001749 if( db ) sqlite3_close(db);
drh75897232000-05-29 14:26:00 +00001750 return 0;
1751}