blob: b06f913091abe5b6c3da9ac718ff8a42acc857a8 [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**
danielk19772e588c72005-12-09 14:25:08 +000015** $Id: shell.c,v 1.129 2005/12/09 14:25:08 danielk1977 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*/
danielk19772e588c72005-12-09 14:25:08 +000084static int isNumber(const char *z, int *realnum){
drhc8d74412004-08-31 23:41:26 +000085 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);
danielk19774af00c62005-01-23 23:43:21 +0000199#if defined(HAVE_READLINE) && HAVE_READLINE==1
drh2dfbbca2000-07-28 14:32:48 +0000200 if( zResult ) add_history(zResult);
danielk19774af00c62005-01-23 23:43:21 +0000201#endif
drh8e7e7a22000-05-30 18:45:23 +0000202 return zResult;
203}
204
persicom7e2dfdd2002-04-18 02:46:52 +0000205struct previous_mode_data {
206 int valid; /* Is there legit data in here? */
207 int mode;
208 int showHeader;
209 int colWidth[100];
210};
drh8e7e7a22000-05-30 18:45:23 +0000211/*
drh75897232000-05-29 14:26:00 +0000212** An pointer to an instance of this structure is passed from
213** the main program to the callback. This is used to communicate
214** state and mode information.
215*/
216struct callback_data {
danielk197792f9a1b2004-06-19 09:08:16 +0000217 sqlite3 *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +0000218 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +0000219 int cnt; /* Number of records displayed so far */
220 FILE *out; /* Write results here */
221 int mode; /* An output mode setting */
222 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +0000223 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000224 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000225 int colWidth[100]; /* Requested width of each column when in column mode*/
226 int actualWidth[100]; /* Actual width of each column */
drh83965662003-04-17 02:54:13 +0000227 char nullvalue[20]; /* The text to print when a NULL comes back from
228 ** the database */
persicom7e2dfdd2002-04-18 02:46:52 +0000229 struct previous_mode_data explainPrev;
drh83965662003-04-17 02:54:13 +0000230 /* Holds the mode information just before
231 ** .explain ON */
drh44c2eb12003-04-30 11:38:26 +0000232 char outfile[FILENAME_MAX]; /* Filename for *out */
233 const char *zDbFilename; /* name of the database file */
drh22fbcb82004-02-01 01:22:50 +0000234 char *zKey; /* Encryption key */
drh75897232000-05-29 14:26:00 +0000235};
236
237/*
238** These are the allowed modes.
239*/
drh967e8b72000-06-21 13:59:10 +0000240#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +0000241#define MODE_Column 1 /* One record per line in neat columns */
242#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +0000243#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
244#define MODE_Html 4 /* Generate an XHTML table */
245#define MODE_Insert 5 /* Generate SQL "insert" statements */
drhfeac5f82004-08-01 00:10:45 +0000246#define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */
drh8e64d1c2004-10-07 00:32:39 +0000247#define MODE_Csv 7 /* Quote strings, numbers are plain */
248#define MODE_NUM_OF 8 /* The number of modes (not a mode itself) */
persicom7e2dfdd2002-04-18 02:46:52 +0000249
250char *modeDescr[MODE_NUM_OF] = {
251 "line",
252 "column",
253 "list",
254 "semi",
255 "html",
drhfeac5f82004-08-01 00:10:45 +0000256 "insert",
257 "tcl",
drh8e64d1c2004-10-07 00:32:39 +0000258 "csv",
persicom7e2dfdd2002-04-18 02:46:52 +0000259};
drh75897232000-05-29 14:26:00 +0000260
261/*
262** Number of elements in an array
263*/
264#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
265
266/*
drh28bd4bc2000-06-15 15:57:22 +0000267** Output the given string as a quoted string using SQL quoting conventions.
268*/
269static void output_quoted_string(FILE *out, const char *z){
270 int i;
271 int nSingle = 0;
drh28bd4bc2000-06-15 15:57:22 +0000272 for(i=0; z[i]; i++){
273 if( z[i]=='\'' ) nSingle++;
drh28bd4bc2000-06-15 15:57:22 +0000274 }
275 if( nSingle==0 ){
276 fprintf(out,"'%s'",z);
drh28bd4bc2000-06-15 15:57:22 +0000277 }else{
278 fprintf(out,"'");
279 while( *z ){
280 for(i=0; z[i] && z[i]!='\''; i++){}
281 if( i==0 ){
282 fprintf(out,"''");
283 z++;
284 }else if( z[i]=='\'' ){
285 fprintf(out,"%.*s''",i,z);
286 z += i+1;
287 }else{
drhcd7d2732002-02-26 23:24:26 +0000288 fprintf(out,"%s",z);
drh28bd4bc2000-06-15 15:57:22 +0000289 break;
290 }
291 }
drhcd7d2732002-02-26 23:24:26 +0000292 fprintf(out,"'");
drh28bd4bc2000-06-15 15:57:22 +0000293 }
294}
295
296/*
drhfeac5f82004-08-01 00:10:45 +0000297** Output the given string as a quoted according to C or TCL quoting rules.
298*/
299static void output_c_string(FILE *out, const char *z){
300 unsigned int c;
301 fputc('"', out);
302 while( (c = *(z++))!=0 ){
303 if( c=='\\' ){
304 fputc(c, out);
305 fputc(c, out);
306 }else if( c=='\t' ){
307 fputc('\\', out);
308 fputc('t', out);
309 }else if( c=='\n' ){
310 fputc('\\', out);
311 fputc('n', out);
312 }else if( c=='\r' ){
313 fputc('\\', out);
314 fputc('r', out);
315 }else if( !isprint(c) ){
drh0a8640d2005-08-30 20:12:02 +0000316 fprintf(out, "\\%03o", c&0xff);
drhfeac5f82004-08-01 00:10:45 +0000317 }else{
318 fputc(c, out);
319 }
320 }
321 fputc('"', out);
322}
323
324/*
drhc08a4f12000-06-15 16:49:48 +0000325** Output the given string with characters that are special to
326** HTML escaped.
327*/
328static void output_html_string(FILE *out, const char *z){
329 int i;
330 while( *z ){
331 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
332 if( i>0 ){
333 fprintf(out,"%.*s",i,z);
334 }
335 if( z[i]=='<' ){
336 fprintf(out,"&lt;");
337 }else if( z[i]=='&' ){
338 fprintf(out,"&amp;");
339 }else{
340 break;
341 }
342 z += i + 1;
343 }
344}
345
346/*
drh8e64d1c2004-10-07 00:32:39 +0000347** Output a single term of CSV. Actually, p->separator is used for
348** the separator, which may or may not be a comma. p->nullvalue is
349** the null value. Strings are quoted using ANSI-C rules. Numbers
350** appear outside of quotes.
351*/
352static void output_csv(struct callback_data *p, const char *z, int bSep){
353 if( z==0 ){
354 fprintf(p->out,"%s",p->nullvalue);
355 }else if( isNumber(z, 0) ){
356 fprintf(p->out,"%s",z);
357 }else{
358 output_c_string(p->out, z);
359 }
360 if( bSep ){
361 fprintf(p->out, p->separator);
362 }
363}
364
danielk19774af00c62005-01-23 23:43:21 +0000365#ifdef SIGINT
drh8e64d1c2004-10-07 00:32:39 +0000366/*
drh4c504392000-10-16 22:06:40 +0000367** This routine runs when the user presses Ctrl-C
368*/
369static void interrupt_handler(int NotUsed){
drh67505e72002-04-19 12:34:06 +0000370 seenInterrupt = 1;
danielk19776f8a5032004-05-10 10:34:51 +0000371 if( db ) sqlite3_interrupt(db);
drh4c504392000-10-16 22:06:40 +0000372}
danielk19774af00c62005-01-23 23:43:21 +0000373#endif
drh4c504392000-10-16 22:06:40 +0000374
375/*
drh75897232000-05-29 14:26:00 +0000376** This is the callback routine that the SQLite library
377** invokes for each row of a query result.
378*/
379static int callback(void *pArg, int nArg, char **azArg, char **azCol){
380 int i;
381 struct callback_data *p = (struct callback_data*)pArg;
382 switch( p->mode ){
383 case MODE_Line: {
drhe3710332000-09-29 13:30:53 +0000384 int w = 5;
drh6a535342001-10-19 16:44:56 +0000385 if( azArg==0 ) break;
drhe3710332000-09-29 13:30:53 +0000386 for(i=0; i<nArg; i++){
387 int len = strlen(azCol[i]);
388 if( len>w ) w = len;
389 }
drh75897232000-05-29 14:26:00 +0000390 if( p->cnt++>0 ) fprintf(p->out,"\n");
391 for(i=0; i<nArg; i++){
drha69d9162003-04-17 22:57:53 +0000392 fprintf(p->out,"%*s = %s\n", w, azCol[i],
393 azArg[i] ? azArg[i] : p->nullvalue);
drh75897232000-05-29 14:26:00 +0000394 }
395 break;
396 }
397 case MODE_Column: {
drha0c66f52000-07-29 13:20:21 +0000398 if( p->cnt++==0 ){
drh75897232000-05-29 14:26:00 +0000399 for(i=0; i<nArg; i++){
drha0c66f52000-07-29 13:20:21 +0000400 int w, n;
401 if( i<ArraySize(p->colWidth) ){
drh75897232000-05-29 14:26:00 +0000402 w = p->colWidth[i];
403 }else{
drha0c66f52000-07-29 13:20:21 +0000404 w = 0;
drh75897232000-05-29 14:26:00 +0000405 }
drha0c66f52000-07-29 13:20:21 +0000406 if( w<=0 ){
drhff6e9112000-08-28 16:21:58 +0000407 w = strlen(azCol[i] ? azCol[i] : "");
drha0c66f52000-07-29 13:20:21 +0000408 if( w<10 ) w = 10;
persicom7e2dfdd2002-04-18 02:46:52 +0000409 n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
drha0c66f52000-07-29 13:20:21 +0000410 if( w<n ) w = n;
411 }
412 if( i<ArraySize(p->actualWidth) ){
persicom1d0b8722002-04-18 02:53:04 +0000413 p->actualWidth[i] = w;
drha0c66f52000-07-29 13:20:21 +0000414 }
415 if( p->showHeader ){
416 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
417 }
418 }
419 if( p->showHeader ){
420 for(i=0; i<nArg; i++){
421 int w;
422 if( i<ArraySize(p->actualWidth) ){
423 w = p->actualWidth[i];
424 }else{
425 w = 10;
426 }
427 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
428 "----------------------------------------------------------",
429 i==nArg-1 ? "\n": " ");
430 }
drh75897232000-05-29 14:26:00 +0000431 }
432 }
drh6a535342001-10-19 16:44:56 +0000433 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000434 for(i=0; i<nArg; i++){
435 int w;
drha0c66f52000-07-29 13:20:21 +0000436 if( i<ArraySize(p->actualWidth) ){
437 w = p->actualWidth[i];
drh75897232000-05-29 14:26:00 +0000438 }else{
439 w = 10;
440 }
drhc61053b2000-06-04 12:58:36 +0000441 fprintf(p->out,"%-*.*s%s",w,w,
persicom7e2dfdd2002-04-18 02:46:52 +0000442 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
drh75897232000-05-29 14:26:00 +0000443 }
444 break;
445 }
drhe3710332000-09-29 13:30:53 +0000446 case MODE_Semi:
drh75897232000-05-29 14:26:00 +0000447 case MODE_List: {
448 if( p->cnt++==0 && p->showHeader ){
449 for(i=0; i<nArg; i++){
450 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
451 }
452 }
drh6a535342001-10-19 16:44:56 +0000453 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000454 for(i=0; i<nArg; i++){
drh4c653a02000-06-07 01:27:47 +0000455 char *z = azArg[i];
persicom7e2dfdd2002-04-18 02:46:52 +0000456 if( z==0 ) z = p->nullvalue;
drh71172c52002-01-24 00:00:21 +0000457 fprintf(p->out, "%s", z);
drhe3710332000-09-29 13:30:53 +0000458 if( i<nArg-1 ){
459 fprintf(p->out, "%s", p->separator);
460 }else if( p->mode==MODE_Semi ){
461 fprintf(p->out, ";\n");
462 }else{
463 fprintf(p->out, "\n");
464 }
drh75897232000-05-29 14:26:00 +0000465 }
466 break;
467 }
drh1e5d0e92000-05-31 23:33:17 +0000468 case MODE_Html: {
469 if( p->cnt++==0 && p->showHeader ){
470 fprintf(p->out,"<TR>");
471 for(i=0; i<nArg; i++){
472 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
473 }
474 fprintf(p->out,"</TR>\n");
475 }
drh6a535342001-10-19 16:44:56 +0000476 if( azArg==0 ) break;
drh28bd4bc2000-06-15 15:57:22 +0000477 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +0000478 for(i=0; i<nArg; i++){
drhc08a4f12000-06-15 16:49:48 +0000479 fprintf(p->out,"<TD>");
persicom7e2dfdd2002-04-18 02:46:52 +0000480 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
drhc08a4f12000-06-15 16:49:48 +0000481 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +0000482 }
drh7d686b22002-11-11 13:56:47 +0000483 fprintf(p->out,"</TR>\n");
drh1e5d0e92000-05-31 23:33:17 +0000484 break;
485 }
drhfeac5f82004-08-01 00:10:45 +0000486 case MODE_Tcl: {
487 if( p->cnt++==0 && p->showHeader ){
488 for(i=0; i<nArg; i++){
489 output_c_string(p->out,azCol[i]);
490 fprintf(p->out, "%s", p->separator);
491 }
492 fprintf(p->out,"\n");
493 }
494 if( azArg==0 ) break;
495 for(i=0; i<nArg; i++){
496 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
497 fprintf(p->out, "%s", p->separator);
498 }
499 fprintf(p->out,"\n");
500 break;
501 }
drh8e64d1c2004-10-07 00:32:39 +0000502 case MODE_Csv: {
503 if( p->cnt++==0 && p->showHeader ){
504 for(i=0; i<nArg; i++){
505 output_csv(p, azCol[i], i<nArg-1);
506 }
507 fprintf(p->out,"\n");
508 }
509 if( azArg==0 ) break;
510 for(i=0; i<nArg; i++){
511 output_csv(p, azArg[i], i<nArg-1);
512 }
513 fprintf(p->out,"\n");
514 break;
515 }
drh28bd4bc2000-06-15 15:57:22 +0000516 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +0000517 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +0000518 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +0000519 for(i=0; i<nArg; i++){
520 char *zSep = i>0 ? ",": "";
521 if( azArg[i]==0 ){
522 fprintf(p->out,"%sNULL",zSep);
drhc8d74412004-08-31 23:41:26 +0000523 }else if( isNumber(azArg[i], 0) ){
drh28bd4bc2000-06-15 15:57:22 +0000524 fprintf(p->out,"%s%s",zSep, azArg[i]);
525 }else{
526 if( zSep[0] ) fprintf(p->out,"%s",zSep);
527 output_quoted_string(p->out, azArg[i]);
528 }
529 }
530 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +0000531 break;
drh28bd4bc2000-06-15 15:57:22 +0000532 }
persicom1d0b8722002-04-18 02:53:04 +0000533 }
drh75897232000-05-29 14:26:00 +0000534 return 0;
535}
536
537/*
drh33048c02001-10-01 14:29:22 +0000538** Set the destination table field of the callback_data structure to
539** the name of the table given. Escape any quote characters in the
540** table name.
541*/
542static void set_table_name(struct callback_data *p, const char *zName){
543 int i, n;
544 int needQuote;
545 char *z;
546
547 if( p->zDestTable ){
548 free(p->zDestTable);
549 p->zDestTable = 0;
550 }
551 if( zName==0 ) return;
drh4c755c02004-08-08 20:22:17 +0000552 needQuote = !isalpha((unsigned char)*zName) && *zName!='_';
drh33048c02001-10-01 14:29:22 +0000553 for(i=n=0; zName[i]; i++, n++){
drh4c755c02004-08-08 20:22:17 +0000554 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){
drh33048c02001-10-01 14:29:22 +0000555 needQuote = 1;
556 if( zName[i]=='\'' ) n++;
557 }
558 }
559 if( needQuote ) n += 2;
560 z = p->zDestTable = malloc( n+1 );
561 if( z==0 ){
562 fprintf(stderr,"Out of memory!\n");
563 exit(1);
564 }
565 n = 0;
566 if( needQuote ) z[n++] = '\'';
567 for(i=0; zName[i]; i++){
568 z[n++] = zName[i];
569 if( zName[i]=='\'' ) z[n++] = '\'';
570 }
571 if( needQuote ) z[n++] = '\'';
572 z[n] = 0;
573}
574
danielk19772a02e332004-06-05 08:04:36 +0000575/* zIn is either a pointer to a NULL-terminated string in memory obtained
576** from malloc(), or a NULL pointer. The string pointed to by zAppend is
577** added to zIn, and the result returned in memory obtained from malloc().
578** zIn, if it was not NULL, is freed.
579**
580** If the third argument, quote, is not '\0', then it is used as a
581** quote character for zAppend.
582*/
583static char * appendText(char *zIn, char const *zAppend, char quote){
584 int len;
585 int i;
586 int nAppend = strlen(zAppend);
587 int nIn = (zIn?strlen(zIn):0);
588
589 len = nAppend+nIn+1;
590 if( quote ){
591 len += 2;
592 for(i=0; i<nAppend; i++){
593 if( zAppend[i]==quote ) len++;
594 }
595 }
596
597 zIn = (char *)realloc(zIn, len);
598 if( !zIn ){
599 return 0;
600 }
601
602 if( quote ){
603 char *zCsr = &zIn[nIn];
604 *zCsr++ = quote;
605 for(i=0; i<nAppend; i++){
606 *zCsr++ = zAppend[i];
607 if( zAppend[i]==quote ) *zCsr++ = quote;
608 }
609 *zCsr++ = quote;
610 *zCsr++ = '\0';
611 assert( (zCsr-zIn)==len );
612 }else{
613 memcpy(&zIn[nIn], zAppend, nAppend);
614 zIn[len-1] = '\0';
615 }
616
617 return zIn;
618}
619
drhdd3d4592004-08-30 01:54:05 +0000620
621/*
622** Execute a query statement that has a single result column. Print
623** that result column on a line by itself with a semicolon terminator.
624*/
625static int run_table_dump_query(FILE *out, sqlite3 *db, const char *zSelect){
626 sqlite3_stmt *pSelect;
627 int rc;
628 rc = sqlite3_prepare(db, zSelect, -1, &pSelect, 0);
629 if( rc!=SQLITE_OK || !pSelect ){
630 return rc;
631 }
632 rc = sqlite3_step(pSelect);
633 while( rc==SQLITE_ROW ){
634 fprintf(out, "%s;\n", sqlite3_column_text(pSelect, 0));
635 rc = sqlite3_step(pSelect);
636 }
637 return sqlite3_finalize(pSelect);
638}
639
640
drh33048c02001-10-01 14:29:22 +0000641/*
drh4c653a02000-06-07 01:27:47 +0000642** This is a different callback routine used for dumping the database.
643** Each row received by this callback consists of a table name,
644** the table type ("index" or "table") and SQL to create the table.
645** This routine should print text sufficient to recreate the table.
646*/
647static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
danielk19772a02e332004-06-05 08:04:36 +0000648 int rc;
649 const char *zTable;
650 const char *zType;
651 const char *zSql;
drhdaffd0e2001-04-11 14:28:42 +0000652 struct callback_data *p = (struct callback_data *)pArg;
danielk19772a02e332004-06-05 08:04:36 +0000653
drh4c653a02000-06-07 01:27:47 +0000654 if( nArg!=3 ) return 1;
danielk19772a02e332004-06-05 08:04:36 +0000655 zTable = azArg[0];
656 zType = azArg[1];
657 zSql = azArg[2];
658
drh00b950d2005-09-11 02:03:03 +0000659 if( strcmp(zTable, "sqlite_sequence")==0 ){
drhf8eb96a2005-02-03 00:42:34 +0000660 fprintf(p->out, "DELETE FROM sqlite_sequence;\n");
drh00b950d2005-09-11 02:03:03 +0000661 }else if( strcmp(zTable, "sqlite_stat1")==0 ){
662 fprintf(p->out, "ANALYZE sqlite_master;\n");
663 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
664 return 0;
665 }else{
666 fprintf(p->out, "%s;\n", zSql);
drhf8eb96a2005-02-03 00:42:34 +0000667 }
danielk19772a02e332004-06-05 08:04:36 +0000668
669 if( strcmp(zType, "table")==0 ){
670 sqlite3_stmt *pTableInfo = 0;
danielk19772a02e332004-06-05 08:04:36 +0000671 char *zSelect = 0;
672 char *zTableInfo = 0;
673 char *zTmp = 0;
674
675 zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
676 zTableInfo = appendText(zTableInfo, zTable, '"');
677 zTableInfo = appendText(zTableInfo, ");", 0);
678
679 rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0);
680 if( zTableInfo ) free(zTableInfo);
681 if( rc!=SQLITE_OK || !pTableInfo ){
682 return 1;
683 }
684
685 zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
686 zTmp = appendText(zTmp, zTable, '"');
687 if( zTmp ){
688 zSelect = appendText(zSelect, zTmp, '\'');
689 }
690 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
691 rc = sqlite3_step(pTableInfo);
692 while( rc==SQLITE_ROW ){
danielk19772e588c72005-12-09 14:25:08 +0000693 const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
danielk19773f41e972004-06-08 00:39:01 +0000694 zSelect = appendText(zSelect, "quote(", 0);
danielk19772e588c72005-12-09 14:25:08 +0000695 zSelect = appendText(zSelect, zText, '"');
danielk19772a02e332004-06-05 08:04:36 +0000696 rc = sqlite3_step(pTableInfo);
697 if( rc==SQLITE_ROW ){
698 zSelect = appendText(zSelect, ") || ', ' || ", 0);
699 }else{
700 zSelect = appendText(zSelect, ") ", 0);
701 }
702 }
703 rc = sqlite3_finalize(pTableInfo);
704 if( rc!=SQLITE_OK ){
705 if( zSelect ) free(zSelect);
706 return 1;
707 }
708 zSelect = appendText(zSelect, "|| ')' FROM ", 0);
709 zSelect = appendText(zSelect, zTable, '"');
710
drhdd3d4592004-08-30 01:54:05 +0000711 rc = run_table_dump_query(p->out, p->db, zSelect);
712 if( rc==SQLITE_CORRUPT ){
713 zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
714 rc = run_table_dump_query(p->out, p->db, zSelect);
715 }
danielk19772a02e332004-06-05 08:04:36 +0000716 if( zSelect ) free(zSelect);
danielk19772a02e332004-06-05 08:04:36 +0000717 if( rc!=SQLITE_OK ){
718 return 1;
719 }
drh4c653a02000-06-07 01:27:47 +0000720 }
drh4c653a02000-06-07 01:27:47 +0000721 return 0;
722}
723
724/*
drhdd3d4592004-08-30 01:54:05 +0000725** Run zQuery. Update dump_callback() as the callback routine.
726** If we get a SQLITE_CORRUPT error, rerun the query after appending
727** "ORDER BY rowid DESC" to the end.
728*/
729static int run_schema_dump_query(
730 struct callback_data *p,
731 const char *zQuery,
732 char **pzErrMsg
733){
734 int rc;
735 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, pzErrMsg);
736 if( rc==SQLITE_CORRUPT ){
737 char *zQ2;
738 int len = strlen(zQuery);
739 if( pzErrMsg ) sqlite3_free(*pzErrMsg);
740 zQ2 = malloc( len+100 );
741 if( zQ2==0 ) return rc;
742 sprintf(zQ2, "%s ORDER BY rowid DESC", zQuery);
743 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, pzErrMsg);
744 free(zQ2);
745 }
746 return rc;
747}
748
749/*
drh75897232000-05-29 14:26:00 +0000750** Text of a help message
751*/
persicom1d0b8722002-04-18 02:53:04 +0000752static char zHelp[] =
jplyon6a65bb32003-05-04 07:25:57 +0000753 ".databases List names and files of attached databases\n"
drhb860bc92004-08-04 15:16:55 +0000754 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
drhdaffd0e2001-04-11 14:28:42 +0000755 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +0000756 ".exit Exit this program\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000757 ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000758 ".header(s) ON|OFF Turn display of headers on or off\n"
drh75897232000-05-29 14:26:00 +0000759 ".help Show this message\n"
drhb860bc92004-08-04 15:16:55 +0000760 ".import FILE TABLE Import data from FILE into TABLE\n"
drh75897232000-05-29 14:26:00 +0000761 ".indices TABLE Show names of all indices on TABLE\n"
danielk19776b77a362005-01-13 11:10:25 +0000762 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n"
drh3b584fa2004-09-24 12:50:03 +0000763 " csv Comma-separated values\n"
drhb860bc92004-08-04 15:16:55 +0000764 " column Left-aligned columns. (See .width)\n"
765 " html HTML <table> code\n"
766 " insert SQL insert statements for TABLE\n"
767 " line One value per line\n"
768 " list Values delimited by .separator string\n"
769 " tabs Tab-separated values\n"
770 " tcl TCL list elements\n"
771 ".nullvalue STRING Print STRING in place of NULL values\n"
drh75897232000-05-29 14:26:00 +0000772 ".output FILENAME Send output to FILENAME\n"
773 ".output stdout Send output to the screen\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000774 ".prompt MAIN CONTINUE Replace the standard prompts\n"
persicom7e2dfdd2002-04-18 02:46:52 +0000775 ".quit Exit this program\n"
drhdaffd0e2001-04-11 14:28:42 +0000776 ".read FILENAME Execute SQL in FILENAME\n"
drh9eb9e262004-02-11 02:18:05 +0000777#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +0000778 ".rekey OLD NEW NEW Change the encryption key\n"
779#endif
drh75897232000-05-29 14:26:00 +0000780 ".schema ?TABLE? Show the CREATE statements\n"
drhb860bc92004-08-04 15:16:55 +0000781 ".separator STRING Change separator used by output mode and .import\n"
drhdd45df82002-04-18 12:39:03 +0000782 ".show Show the current values for various settings\n"
drhfeac5f82004-08-01 00:10:45 +0000783 ".tables ?PATTERN? List names of tables matching a LIKE pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000784 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000785 ".width NUM NUM ... Set column widths for \"column\" mode\n"
786;
787
drhdaffd0e2001-04-11 14:28:42 +0000788/* Forward reference */
789static void process_input(struct callback_data *p, FILE *in);
790
drh75897232000-05-29 14:26:00 +0000791/*
drh44c2eb12003-04-30 11:38:26 +0000792** Make sure the database is open. If it is not, then open it. If
793** the database fails to open, print an error message and exit.
794*/
795static void open_db(struct callback_data *p){
796 if( p->db==0 ){
danielk19774f057f92004-06-08 00:02:33 +0000797 sqlite3_open(p->zDbFilename, &p->db);
danielk197780290862004-05-22 09:21:21 +0000798 db = p->db;
drh2011d5f2004-07-22 02:40:37 +0000799#ifdef SQLITE_HAS_CODEC
800 sqlite3_key(p->db, p->zKey, p->zKey ? strlen(p->zKey) : 0);
drheb8ed702004-02-11 10:37:23 +0000801#endif
danielk1977bc6ada42004-06-30 08:20:16 +0000802 sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
803 shellstaticFunc, 0, 0);
danielk197780290862004-05-22 09:21:21 +0000804 if( SQLITE_OK!=sqlite3_errcode(db) ){
805 fprintf(stderr,"Unable to open database \"%s\": %s\n",
806 p->zDbFilename, sqlite3_errmsg(db));
drh22fbcb82004-02-01 01:22:50 +0000807 exit(1);
drh44c2eb12003-04-30 11:38:26 +0000808 }
809 }
810}
811
812/*
drhfeac5f82004-08-01 00:10:45 +0000813** Do C-language style dequoting.
814**
815** \t -> tab
816** \n -> newline
817** \r -> carriage return
818** \NNN -> ascii character NNN in octal
819** \\ -> backslash
820*/
821static void resolve_backslashes(char *z){
822 int i, j, c;
823 for(i=j=0; (c = z[i])!=0; i++, j++){
824 if( c=='\\' ){
825 c = z[++i];
826 if( c=='n' ){
827 c = '\n';
828 }else if( c=='t' ){
829 c = '\t';
830 }else if( c=='r' ){
831 c = '\r';
832 }else if( c>='0' && c<='7' ){
833 c =- '0';
834 if( z[i+1]>='0' && z[i+1]<='7' ){
835 i++;
836 c = (c<<3) + z[i] - '0';
837 if( z[i+1]>='0' && z[i+1]<='7' ){
838 i++;
839 c = (c<<3) + z[i] - '0';
840 }
841 }
842 }
843 }
844 z[j] = c;
845 }
846 z[j] = 0;
847}
848
849/*
drh75897232000-05-29 14:26:00 +0000850** If an input line begins with "." then invoke this routine to
851** process that line.
drh67505e72002-04-19 12:34:06 +0000852**
853** Return 1 to exit and 0 to continue.
drh75897232000-05-29 14:26:00 +0000854*/
drh44c2eb12003-04-30 11:38:26 +0000855static int do_meta_command(char *zLine, struct callback_data *p){
drh75897232000-05-29 14:26:00 +0000856 int i = 1;
857 int nArg = 0;
858 int n, c;
drh67505e72002-04-19 12:34:06 +0000859 int rc = 0;
drh75897232000-05-29 14:26:00 +0000860 char *azArg[50];
861
862 /* Parse the input line into tokens.
863 */
864 while( zLine[i] && nArg<ArraySize(azArg) ){
drh4c755c02004-08-08 20:22:17 +0000865 while( isspace((unsigned char)zLine[i]) ){ i++; }
drh06333682004-03-09 13:37:45 +0000866 if( zLine[i]==0 ) break;
drh75897232000-05-29 14:26:00 +0000867 if( zLine[i]=='\'' || zLine[i]=='"' ){
868 int delim = zLine[i++];
869 azArg[nArg++] = &zLine[i];
870 while( zLine[i] && zLine[i]!=delim ){ i++; }
871 if( zLine[i]==delim ){
872 zLine[i++] = 0;
873 }
drhfeac5f82004-08-01 00:10:45 +0000874 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
drh75897232000-05-29 14:26:00 +0000875 }else{
876 azArg[nArg++] = &zLine[i];
drh4c755c02004-08-08 20:22:17 +0000877 while( zLine[i] && !isspace((unsigned char)zLine[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +0000878 if( zLine[i] ) zLine[i++] = 0;
drhfeac5f82004-08-01 00:10:45 +0000879 resolve_backslashes(azArg[nArg-1]);
drh75897232000-05-29 14:26:00 +0000880 }
881 }
882
883 /* Process the input line.
884 */
drh67505e72002-04-19 12:34:06 +0000885 if( nArg==0 ) return rc;
drh75897232000-05-29 14:26:00 +0000886 n = strlen(azArg[0]);
887 c = azArg[0][0];
jplyon6a65bb32003-05-04 07:25:57 +0000888 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
jplyon672a1ed2003-05-11 20:07:05 +0000889 struct callback_data data;
890 char *zErrMsg = 0;
jplyon6a65bb32003-05-04 07:25:57 +0000891 open_db(p);
jplyon672a1ed2003-05-11 20:07:05 +0000892 memcpy(&data, p, sizeof(data));
drhd8885442004-03-17 23:42:12 +0000893 data.showHeader = 1;
jplyon672a1ed2003-05-11 20:07:05 +0000894 data.mode = MODE_Column;
drhd8885442004-03-17 23:42:12 +0000895 data.colWidth[0] = 3;
896 data.colWidth[1] = 15;
897 data.colWidth[2] = 58;
drh0b2110c2004-10-26 00:08:10 +0000898 data.cnt = 0;
danielk19776f8a5032004-05-10 10:34:51 +0000899 sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
jplyon672a1ed2003-05-11 20:07:05 +0000900 if( zErrMsg ){
901 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000902 sqlite3_free(zErrMsg);
jplyon6a65bb32003-05-04 07:25:57 +0000903 }
904 }else
905
drh4c653a02000-06-07 01:27:47 +0000906 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
907 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +0000908 open_db(p);
drh33048c02001-10-01 14:29:22 +0000909 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000910 if( nArg==1 ){
drhdd3d4592004-08-30 01:54:05 +0000911 run_schema_dump_query(p,
drha18c5682000-10-08 22:20:57 +0000912 "SELECT name, type, sql FROM sqlite_master "
drh00b950d2005-09-11 02:03:03 +0000913 "WHERE sql NOT NULL AND type=='table'", 0
drhdd3d4592004-08-30 01:54:05 +0000914 );
915 run_schema_dump_query(p,
916 "SELECT name, type, sql FROM sqlite_master "
drh00b950d2005-09-11 02:03:03 +0000917 "WHERE sql NOT NULL AND type!='table' AND type!='meta'", 0
drha18c5682000-10-08 22:20:57 +0000918 );
drh4c653a02000-06-07 01:27:47 +0000919 }else{
920 int i;
drhdd3d4592004-08-30 01:54:05 +0000921 for(i=1; i<nArg; i++){
danielk1977bc6ada42004-06-30 08:20:16 +0000922 zShellStatic = azArg[i];
drhdd3d4592004-08-30 01:54:05 +0000923 run_schema_dump_query(p,
drha18c5682000-10-08 22:20:57 +0000924 "SELECT name, type, sql FROM sqlite_master "
drhdd3d4592004-08-30 01:54:05 +0000925 "WHERE tbl_name LIKE shellstatic() AND type=='table'"
926 " AND sql NOT NULL", 0);
927 run_schema_dump_query(p,
928 "SELECT name, type, sql FROM sqlite_master "
929 "WHERE tbl_name LIKE shellstatic() AND type!='table'"
930 " AND type!='meta' AND sql NOT NULL", 0);
danielk1977bc6ada42004-06-30 08:20:16 +0000931 zShellStatic = 0;
drh4c653a02000-06-07 01:27:47 +0000932 }
933 }
934 if( zErrMsg ){
935 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +0000936 sqlite3_free(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000937 }else{
938 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000939 }
940 }else
drh75897232000-05-29 14:26:00 +0000941
drhdaffd0e2001-04-11 14:28:42 +0000942 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
943 int j;
944 char *z = azArg[1];
945 int val = atoi(azArg[1]);
946 for(j=0; z[j]; j++){
drh4c755c02004-08-08 20:22:17 +0000947 z[j] = tolower((unsigned char)z[j]);
drhdaffd0e2001-04-11 14:28:42 +0000948 }
949 if( strcmp(z,"on")==0 ){
950 val = 1;
951 }else if( strcmp(z,"yes")==0 ){
952 val = 1;
persicom1d0b8722002-04-18 02:53:04 +0000953 }
drhdaffd0e2001-04-11 14:28:42 +0000954 p->echoOn = val;
955 }else
956
drh75897232000-05-29 14:26:00 +0000957 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh67505e72002-04-19 12:34:06 +0000958 rc = 1;
drh75897232000-05-29 14:26:00 +0000959 }else
960
drhdd45df82002-04-18 12:39:03 +0000961 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +0000962 int j;
drh472cbf62004-08-14 18:18:44 +0000963 static char zOne[] = "1";
964 char *z = nArg>=2 ? azArg[1] : zOne;
drhdd45df82002-04-18 12:39:03 +0000965 int val = atoi(z);
persicom7e2dfdd2002-04-18 02:46:52 +0000966 for(j=0; z[j]; j++){
drh4c755c02004-08-08 20:22:17 +0000967 z[j] = tolower((unsigned char)z[j]);
persicom7e2dfdd2002-04-18 02:46:52 +0000968 }
969 if( strcmp(z,"on")==0 ){
970 val = 1;
971 }else if( strcmp(z,"yes")==0 ){
972 val = 1;
973 }
974 if(val == 1) {
975 if(!p->explainPrev.valid) {
976 p->explainPrev.valid = 1;
977 p->explainPrev.mode = p->mode;
978 p->explainPrev.showHeader = p->showHeader;
979 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
980 }
981 /* We could put this code under the !p->explainValid
982 ** condition so that it does not execute if we are already in
983 ** explain mode. However, always executing it allows us an easy
984 ** was to reset to explain mode in case the user previously
985 ** did an .explain followed by a .width, .mode or .header
986 ** command.
987 */
988 p->mode = MODE_Column;
989 p->showHeader = 1;
990 memset(p->colWidth,0,ArraySize(p->colWidth));
991 p->colWidth[0] = 4;
drhe69cc5b2005-08-27 01:50:53 +0000992 p->colWidth[1] = 14;
persicom7e2dfdd2002-04-18 02:46:52 +0000993 p->colWidth[2] = 10;
994 p->colWidth[3] = 10;
drhe69cc5b2005-08-27 01:50:53 +0000995 p->colWidth[4] = 33;
persicom7e2dfdd2002-04-18 02:46:52 +0000996 }else if (p->explainPrev.valid) {
997 p->explainPrev.valid = 0;
998 p->mode = p->explainPrev.mode;
999 p->showHeader = p->explainPrev.showHeader;
1000 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
1001 }
drh75897232000-05-29 14:26:00 +00001002 }else
1003
persicom7e2dfdd2002-04-18 02:46:52 +00001004 if( c=='h' && (strncmp(azArg[0], "header", n)==0
1005 ||
1006 strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){
drh75897232000-05-29 14:26:00 +00001007 int j;
1008 char *z = azArg[1];
1009 int val = atoi(azArg[1]);
1010 for(j=0; z[j]; j++){
drh4c755c02004-08-08 20:22:17 +00001011 z[j] = tolower((unsigned char)z[j]);
drh75897232000-05-29 14:26:00 +00001012 }
1013 if( strcmp(z,"on")==0 ){
1014 val = 1;
1015 }else if( strcmp(z,"yes")==0 ){
1016 val = 1;
persicom1d0b8722002-04-18 02:53:04 +00001017 }
drh75897232000-05-29 14:26:00 +00001018 p->showHeader = val;
1019 }else
1020
1021 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
1022 fprintf(stderr,zHelp);
1023 }else
1024
drhfeac5f82004-08-01 00:10:45 +00001025 if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg>=3 ){
1026 char *zTable = azArg[2]; /* Insert data into this table */
1027 char *zFile = azArg[1]; /* The file from which to extract data */
1028 sqlite3_stmt *pStmt; /* A statement */
1029 int rc; /* Result code */
1030 int nCol; /* Number of columns in the table */
1031 int nByte; /* Number of bytes in an SQL string */
1032 int i, j; /* Loop counters */
1033 int nSep; /* Number of bytes in p->separator[] */
1034 char *zSql; /* An SQL statement */
1035 char *zLine; /* A single line of input from the file */
1036 char **azCol; /* zLine[] broken up into columns */
1037 char *zCommit; /* How to commit changes */
drhb860bc92004-08-04 15:16:55 +00001038 FILE *in; /* The input file */
1039 int lineno = 0; /* Line number of input file */
drhfeac5f82004-08-01 00:10:45 +00001040
1041 nSep = strlen(p->separator);
1042 if( nSep==0 ){
1043 fprintf(stderr, "non-null separator required for import\n");
1044 return 0;
1045 }
1046 zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
1047 if( zSql==0 ) return 0;
1048 nByte = strlen(zSql);
1049 rc = sqlite3_prepare(p->db, zSql, 0, &pStmt, 0);
1050 sqlite3_free(zSql);
1051 if( rc ){
1052 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
1053 nCol = 0;
1054 }else{
1055 nCol = sqlite3_column_count(pStmt);
1056 }
1057 sqlite3_finalize(pStmt);
1058 if( nCol==0 ) return 0;
1059 zSql = malloc( nByte + 20 + nCol*2 );
1060 if( zSql==0 ) return 0;
1061 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable);
1062 j = strlen(zSql);
1063 for(i=1; i<nCol; i++){
1064 zSql[j++] = ',';
1065 zSql[j++] = '?';
1066 }
1067 zSql[j++] = ')';
1068 zSql[j] = 0;
1069 rc = sqlite3_prepare(p->db, zSql, 0, &pStmt, 0);
1070 free(zSql);
1071 if( rc ){
1072 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
1073 sqlite3_finalize(pStmt);
1074 return 0;
1075 }
1076 in = fopen(zFile, "rb");
1077 if( in==0 ){
1078 fprintf(stderr, "cannot open file: %s\n", zFile);
1079 sqlite3_finalize(pStmt);
1080 return 0;
1081 }
1082 azCol = malloc( sizeof(azCol[0])*(nCol+1) );
1083 if( azCol==0 ) return 0;
1084 sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
1085 zCommit = "COMMIT";
1086 while( (zLine = local_getline(0, in))!=0 ){
1087 char *z;
1088 i = 0;
drhb860bc92004-08-04 15:16:55 +00001089 lineno++;
drhfeac5f82004-08-01 00:10:45 +00001090 azCol[0] = zLine;
drh36d4e972004-10-06 14:39:06 +00001091 for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){
drhfeac5f82004-08-01 00:10:45 +00001092 if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){
1093 *z = 0;
1094 i++;
drhb860bc92004-08-04 15:16:55 +00001095 if( i<nCol ){
1096 azCol[i] = &z[nSep];
1097 z += nSep-1;
1098 }
drhfeac5f82004-08-01 00:10:45 +00001099 }
1100 }
drh1cd7f832005-08-05 18:50:51 +00001101 *z = 0;
drhb860bc92004-08-04 15:16:55 +00001102 if( i+1!=nCol ){
1103 fprintf(stderr,"%s line %d: expected %d columns of data but found %d\n",
1104 zFile, lineno, nCol, i+1);
1105 zCommit = "ROLLBACK";
1106 break;
1107 }
drhfeac5f82004-08-01 00:10:45 +00001108 for(i=0; i<nCol; i++){
1109 sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
1110 }
1111 sqlite3_step(pStmt);
1112 rc = sqlite3_reset(pStmt);
1113 free(zLine);
1114 if( rc!=SQLITE_OK ){
1115 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
1116 zCommit = "ROLLBACK";
1117 break;
1118 }
1119 }
1120 free(azCol);
1121 fclose(in);
1122 sqlite3_finalize(pStmt);
drhb860bc92004-08-04 15:16:55 +00001123 sqlite3_exec(p->db, zCommit, 0, 0, 0);
drhfeac5f82004-08-01 00:10:45 +00001124 }else
1125
drh75897232000-05-29 14:26:00 +00001126 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
1127 struct callback_data data;
1128 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +00001129 open_db(p);
drh75897232000-05-29 14:26:00 +00001130 memcpy(&data, p, sizeof(data));
1131 data.showHeader = 0;
1132 data.mode = MODE_List;
danielk1977bc6ada42004-06-30 08:20:16 +00001133 zShellStatic = azArg[1];
1134 sqlite3_exec(p->db,
drha18c5682000-10-08 22:20:57 +00001135 "SELECT name FROM sqlite_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001136 "WHERE type='index' AND tbl_name LIKE shellstatic() "
drhe0bc4042002-06-25 01:09:11 +00001137 "UNION ALL "
1138 "SELECT name FROM sqlite_temp_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001139 "WHERE type='index' AND tbl_name LIKE shellstatic() "
drhe0bc4042002-06-25 01:09:11 +00001140 "ORDER BY 1",
danielk1977bc6ada42004-06-30 08:20:16 +00001141 callback, &data, &zErrMsg
drha18c5682000-10-08 22:20:57 +00001142 );
danielk1977bc6ada42004-06-30 08:20:16 +00001143 zShellStatic = 0;
drh75897232000-05-29 14:26:00 +00001144 if( zErrMsg ){
1145 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001146 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001147 }
1148 }else
1149
drh28bd4bc2000-06-15 15:57:22 +00001150 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +00001151 int n2 = strlen(azArg[1]);
persicom7e2dfdd2002-04-18 02:46:52 +00001152 if( strncmp(azArg[1],"line",n2)==0
1153 ||
1154 strncmp(azArg[1],"lines",n2)==0 ){
drh75897232000-05-29 14:26:00 +00001155 p->mode = MODE_Line;
persicom7e2dfdd2002-04-18 02:46:52 +00001156 }else if( strncmp(azArg[1],"column",n2)==0
1157 ||
1158 strncmp(azArg[1],"columns",n2)==0 ){
drh75897232000-05-29 14:26:00 +00001159 p->mode = MODE_Column;
1160 }else if( strncmp(azArg[1],"list",n2)==0 ){
1161 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +00001162 }else if( strncmp(azArg[1],"html",n2)==0 ){
1163 p->mode = MODE_Html;
drhfeac5f82004-08-01 00:10:45 +00001164 }else if( strncmp(azArg[1],"tcl",n2)==0 ){
1165 p->mode = MODE_Tcl;
1166 }else if( strncmp(azArg[1],"csv",n2)==0 ){
drh8e64d1c2004-10-07 00:32:39 +00001167 p->mode = MODE_Csv;
drhfeac5f82004-08-01 00:10:45 +00001168 strcpy(p->separator, ",");
1169 }else if( strncmp(azArg[1],"tabs",n2)==0 ){
1170 p->mode = MODE_List;
1171 strcpy(p->separator, "\t");
drh28bd4bc2000-06-15 15:57:22 +00001172 }else if( strncmp(azArg[1],"insert",n2)==0 ){
1173 p->mode = MODE_Insert;
1174 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +00001175 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +00001176 }else{
drh33048c02001-10-01 14:29:22 +00001177 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +00001178 }
drhdaffd0e2001-04-11 14:28:42 +00001179 }else {
drhfeac5f82004-08-01 00:10:45 +00001180 fprintf(stderr,"mode should be on of: "
1181 "column csv html insert line list tabs tcl\n");
drh75897232000-05-29 14:26:00 +00001182 }
1183 }else
1184
persicom7e2dfdd2002-04-18 02:46:52 +00001185 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
1186 sprintf(p->nullvalue, "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
1187 }else
1188
drh75897232000-05-29 14:26:00 +00001189 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
1190 if( p->out!=stdout ){
1191 fclose(p->out);
1192 }
1193 if( strcmp(azArg[1],"stdout")==0 ){
1194 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +00001195 strcpy(p->outfile,"stdout");
drh75897232000-05-29 14:26:00 +00001196 }else{
drha1f9b5e2004-02-14 16:31:02 +00001197 p->out = fopen(azArg[1], "wb");
drh75897232000-05-29 14:26:00 +00001198 if( p->out==0 ){
1199 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
1200 p->out = stdout;
persicom7e2dfdd2002-04-18 02:46:52 +00001201 } else {
1202 strcpy(p->outfile,azArg[1]);
drh75897232000-05-29 14:26:00 +00001203 }
1204 }
1205 }else
1206
drhdd45df82002-04-18 12:39:03 +00001207 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
persicom7e2dfdd2002-04-18 02:46:52 +00001208 if( nArg >= 2) {
1209 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
1210 }
1211 if( nArg >= 3) {
1212 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
1213 }
1214 }else
1215
1216 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
drhf73287c2004-02-25 02:25:37 +00001217 rc = 1;
persicom7e2dfdd2002-04-18 02:46:52 +00001218 }else
1219
drhdaffd0e2001-04-11 14:28:42 +00001220 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
drha1f9b5e2004-02-14 16:31:02 +00001221 FILE *alt = fopen(azArg[1], "rb");
drhdaffd0e2001-04-11 14:28:42 +00001222 if( alt==0 ){
1223 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
1224 }else{
1225 process_input(p, alt);
1226 fclose(alt);
1227 }
1228 }else
1229
drh9eb9e262004-02-11 02:18:05 +00001230#ifdef SQLITE_HAS_CODEC
drh22fbcb82004-02-01 01:22:50 +00001231 if( c=='r' && strncmp(azArg[0],"rekey", n)==0 && nArg==4 ){
1232 char *zOld = p->zKey;
1233 if( zOld==0 ) zOld = "";
1234 if( strcmp(azArg[1],zOld) ){
1235 fprintf(stderr,"old key is incorrect\n");
1236 }else if( strcmp(azArg[2], azArg[3]) ){
1237 fprintf(stderr,"2nd copy of new key does not match the 1st\n");
1238 }else{
drh3f4fedb2004-05-31 19:34:33 +00001239 sqlite3_free(p->zKey);
danielk19776f8a5032004-05-10 10:34:51 +00001240 p->zKey = sqlite3_mprintf("%s", azArg[2]);
drh2011d5f2004-07-22 02:40:37 +00001241 sqlite3_rekey(p->db, p->zKey, strlen(p->zKey));
drh22fbcb82004-02-01 01:22:50 +00001242 }
1243 }else
1244#endif
1245
drh75897232000-05-29 14:26:00 +00001246 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
1247 struct callback_data data;
1248 char *zErrMsg = 0;
drh44c2eb12003-04-30 11:38:26 +00001249 open_db(p);
drh75897232000-05-29 14:26:00 +00001250 memcpy(&data, p, sizeof(data));
1251 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +00001252 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +00001253 if( nArg>1 ){
drhc8d74412004-08-31 23:41:26 +00001254 int i;
1255 for(i=0; azArg[1][i]; i++) azArg[1][i] = tolower(azArg[1][i]);
1256 if( strcmp(azArg[1],"sqlite_master")==0 ){
drha18c5682000-10-08 22:20:57 +00001257 char *new_argv[2], *new_colv[2];
1258 new_argv[0] = "CREATE TABLE sqlite_master (\n"
1259 " type text,\n"
1260 " name text,\n"
1261 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +00001262 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +00001263 " sql text\n"
1264 ")";
1265 new_argv[1] = 0;
1266 new_colv[0] = "sql";
1267 new_colv[1] = 0;
1268 callback(&data, 1, new_argv, new_colv);
drhc8d74412004-08-31 23:41:26 +00001269 }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){
drhe0bc4042002-06-25 01:09:11 +00001270 char *new_argv[2], *new_colv[2];
1271 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
1272 " type text,\n"
1273 " name text,\n"
1274 " tbl_name text,\n"
1275 " rootpage integer,\n"
1276 " sql text\n"
1277 ")";
1278 new_argv[1] = 0;
1279 new_colv[0] = "sql";
1280 new_colv[1] = 0;
1281 callback(&data, 1, new_argv, new_colv);
drha18c5682000-10-08 22:20:57 +00001282 }else{
danielk1977bc6ada42004-06-30 08:20:16 +00001283 zShellStatic = azArg[1];
1284 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +00001285 "SELECT sql FROM "
1286 " (SELECT * FROM sqlite_master UNION ALL"
1287 " SELECT * FROM sqlite_temp_master) "
danielk1977bc6ada42004-06-30 08:20:16 +00001288 "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL "
drhe0bc4042002-06-25 01:09:11 +00001289 "ORDER BY substr(type,2,1), name",
danielk1977bc6ada42004-06-30 08:20:16 +00001290 callback, &data, &zErrMsg);
1291 zShellStatic = 0;
drha18c5682000-10-08 22:20:57 +00001292 }
drh75897232000-05-29 14:26:00 +00001293 }else{
danielk19776f8a5032004-05-10 10:34:51 +00001294 sqlite3_exec(p->db,
drhe0bc4042002-06-25 01:09:11 +00001295 "SELECT sql FROM "
1296 " (SELECT * FROM sqlite_master UNION ALL"
1297 " SELECT * FROM sqlite_temp_master) "
drh0c356672005-09-10 22:40:53 +00001298 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
drhe0bc4042002-06-25 01:09:11 +00001299 "ORDER BY substr(type,2,1), name",
drha18c5682000-10-08 22:20:57 +00001300 callback, &data, &zErrMsg
1301 );
drh75897232000-05-29 14:26:00 +00001302 }
drh75897232000-05-29 14:26:00 +00001303 if( zErrMsg ){
1304 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001305 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001306 }
1307 }else
1308
1309 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
1310 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
1311 }else
1312
persicom7e2dfdd2002-04-18 02:46:52 +00001313 if( c=='s' && strncmp(azArg[0], "show", n)==0){
1314 int i;
1315 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
drh67505e72002-04-19 12:34:06 +00001316 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
drhdd45df82002-04-18 12:39:03 +00001317 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
persicom7e2dfdd2002-04-18 02:46:52 +00001318 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
drhfeac5f82004-08-01 00:10:45 +00001319 fprintf(p->out,"%9.9s: ", "nullvalue");
1320 output_c_string(p->out, p->nullvalue);
1321 fprintf(p->out, "\n");
drh67505e72002-04-19 12:34:06 +00001322 fprintf(p->out,"%9.9s: %s\n","output",
1323 strlen(p->outfile) ? p->outfile : "stdout");
drhfeac5f82004-08-01 00:10:45 +00001324 fprintf(p->out,"%9.9s: ", "separator");
1325 output_c_string(p->out, p->separator);
1326 fprintf(p->out, "\n");
persicom7e2dfdd2002-04-18 02:46:52 +00001327 fprintf(p->out,"%9.9s: ","width");
1328 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
drhfeac5f82004-08-01 00:10:45 +00001329 fprintf(p->out,"%d ",p->colWidth[i]);
persicom7e2dfdd2002-04-18 02:46:52 +00001330 }
drhfeac5f82004-08-01 00:10:45 +00001331 fprintf(p->out,"\n");
persicom7e2dfdd2002-04-18 02:46:52 +00001332 }else
1333
drh2dfbbca2000-07-28 14:32:48 +00001334 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +00001335 char **azResult;
1336 int nRow, rc;
1337 char *zErrMsg;
drh44c2eb12003-04-30 11:38:26 +00001338 open_db(p);
drha50da102000-08-08 20:19:09 +00001339 if( nArg==1 ){
danielk19776f8a5032004-05-10 10:34:51 +00001340 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +00001341 "SELECT name FROM sqlite_master "
drh0c356672005-09-10 22:40:53 +00001342 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'"
drhe0bc4042002-06-25 01:09:11 +00001343 "UNION ALL "
1344 "SELECT name FROM sqlite_temp_master "
1345 "WHERE type IN ('table','view') "
1346 "ORDER BY 1",
drha18c5682000-10-08 22:20:57 +00001347 &azResult, &nRow, 0, &zErrMsg
1348 );
drha50da102000-08-08 20:19:09 +00001349 }else{
danielk1977bc6ada42004-06-30 08:20:16 +00001350 zShellStatic = azArg[1];
1351 rc = sqlite3_get_table(p->db,
drha50da102000-08-08 20:19:09 +00001352 "SELECT name FROM sqlite_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001353 "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' "
drhe0bc4042002-06-25 01:09:11 +00001354 "UNION ALL "
1355 "SELECT name FROM sqlite_temp_master "
danielk1977bc6ada42004-06-30 08:20:16 +00001356 "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' "
drhe0bc4042002-06-25 01:09:11 +00001357 "ORDER BY 1",
danielk1977bc6ada42004-06-30 08:20:16 +00001358 &azResult, &nRow, 0, &zErrMsg
drha18c5682000-10-08 22:20:57 +00001359 );
danielk1977bc6ada42004-06-30 08:20:16 +00001360 zShellStatic = 0;
drha50da102000-08-08 20:19:09 +00001361 }
drh75897232000-05-29 14:26:00 +00001362 if( zErrMsg ){
1363 fprintf(stderr,"Error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001364 sqlite3_free(zErrMsg);
drh75897232000-05-29 14:26:00 +00001365 }
drhe3710332000-09-29 13:30:53 +00001366 if( rc==SQLITE_OK ){
1367 int len, maxlen = 0;
1368 int i, j;
1369 int nPrintCol, nPrintRow;
1370 for(i=1; i<=nRow; i++){
1371 if( azResult[i]==0 ) continue;
1372 len = strlen(azResult[i]);
1373 if( len>maxlen ) maxlen = len;
1374 }
1375 nPrintCol = 80/(maxlen+2);
1376 if( nPrintCol<1 ) nPrintCol = 1;
1377 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
1378 for(i=0; i<nPrintRow; i++){
1379 for(j=i+1; j<=nRow; j+=nPrintRow){
1380 char *zSp = j<=nPrintRow ? "" : " ";
1381 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
1382 }
1383 printf("\n");
1384 }
1385 }
danielk19776f8a5032004-05-10 10:34:51 +00001386 sqlite3_free_table(azResult);
drh75897232000-05-29 14:26:00 +00001387 }else
1388
drh2dfbbca2000-07-28 14:32:48 +00001389 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
drh44c2eb12003-04-30 11:38:26 +00001390 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001391 sqlite3_busy_timeout(p->db, atoi(azArg[1]));
drh2dfbbca2000-07-28 14:32:48 +00001392 }else
1393
drh75897232000-05-29 14:26:00 +00001394 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
1395 int j;
1396 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
1397 p->colWidth[j-1] = atoi(azArg[j]);
1398 }
1399 }else
1400
1401 {
drh67505e72002-04-19 12:34:06 +00001402 fprintf(stderr, "unknown command or invalid arguments: "
1403 " \"%s\". Enter \".help\" for help\n", azArg[0]);
drh75897232000-05-29 14:26:00 +00001404 }
drh67505e72002-04-19 12:34:06 +00001405
1406 return rc;
drh75897232000-05-29 14:26:00 +00001407}
1408
drh67505e72002-04-19 12:34:06 +00001409/*
drh324ccef2003-02-05 14:06:20 +00001410** Return TRUE if the last non-whitespace character in z[] is a semicolon.
1411** z[] is N characters long.
1412*/
1413static int _ends_with_semicolon(const char *z, int N){
drh4c755c02004-08-08 20:22:17 +00001414 while( N>0 && isspace((unsigned char)z[N-1]) ){ N--; }
drh324ccef2003-02-05 14:06:20 +00001415 return N>0 && z[N-1]==';';
1416}
1417
1418/*
drh70c7a4b2003-04-26 03:03:06 +00001419** Test to see if a line consists entirely of whitespace.
1420*/
1421static int _all_whitespace(const char *z){
1422 for(; *z; z++){
drh4c755c02004-08-08 20:22:17 +00001423 if( isspace(*(unsigned char*)z) ) continue;
drh70c7a4b2003-04-26 03:03:06 +00001424 if( *z=='/' && z[1]=='*' ){
1425 z += 2;
1426 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
1427 if( *z==0 ) return 0;
1428 z++;
1429 continue;
1430 }
1431 if( *z=='-' && z[1]=='-' ){
1432 z += 2;
1433 while( *z && *z!='\n' ){ z++; }
1434 if( *z==0 ) return 1;
1435 continue;
1436 }
1437 return 0;
1438 }
1439 return 1;
1440}
1441
1442/*
drha9b17162003-04-29 18:01:28 +00001443** Return TRUE if the line typed in is an SQL command terminator other
1444** than a semi-colon. The SQL Server style "go" command is understood
1445** as is the Oracle "/".
1446*/
1447static int _is_command_terminator(const char *zLine){
drh4c755c02004-08-08 20:22:17 +00001448 while( isspace(*(unsigned char*)zLine) ){ zLine++; };
drha9b17162003-04-29 18:01:28 +00001449 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1; /* Oracle */
drhc8d74412004-08-31 23:41:26 +00001450 if( tolower(zLine[0])=='g' && tolower(zLine[1])=='o'
1451 && _all_whitespace(&zLine[2]) ){
drha9b17162003-04-29 18:01:28 +00001452 return 1; /* SQL Server */
1453 }
1454 return 0;
1455}
1456
1457/*
drh67505e72002-04-19 12:34:06 +00001458** Read input from *in and process it. If *in==0 then input
1459** is interactive - the user is typing it it. Otherwise, input
1460** is coming from a file or device. A prompt is issued and history
1461** is saved only if input is interactive. An interrupt signal will
1462** cause this routine to exit immediately, unless input is interactive.
1463*/
drhdaffd0e2001-04-11 14:28:42 +00001464static void process_input(struct callback_data *p, FILE *in){
1465 char *zLine;
1466 char *zSql = 0;
1467 int nSql = 0;
1468 char *zErrMsg;
drh7f953e22002-07-13 17:33:45 +00001469 int rc;
drh41e941d2002-04-04 15:10:12 +00001470 while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
drh67505e72002-04-19 12:34:06 +00001471 if( seenInterrupt ){
1472 if( in!=0 ) break;
1473 seenInterrupt = 0;
1474 }
drhdaffd0e2001-04-11 14:28:42 +00001475 if( p->echoOn ) printf("%s\n", zLine);
drhf817b6b2003-06-16 00:16:41 +00001476 if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
drh2af0b2d2002-02-21 02:25:02 +00001477 if( zLine && zLine[0]=='.' && nSql==0 ){
drh44c2eb12003-04-30 11:38:26 +00001478 int rc = do_meta_command(zLine, p);
drhdaffd0e2001-04-11 14:28:42 +00001479 free(zLine);
drh67505e72002-04-19 12:34:06 +00001480 if( rc ) break;
drhdaffd0e2001-04-11 14:28:42 +00001481 continue;
1482 }
drha9b17162003-04-29 18:01:28 +00001483 if( _is_command_terminator(zLine) ){
1484 strcpy(zLine,";");
1485 }
drhdaffd0e2001-04-11 14:28:42 +00001486 if( zSql==0 ){
1487 int i;
drh4c755c02004-08-08 20:22:17 +00001488 for(i=0; zLine[i] && isspace((unsigned char)zLine[i]); i++){}
drhdaffd0e2001-04-11 14:28:42 +00001489 if( zLine[i]!=0 ){
1490 nSql = strlen(zLine);
1491 zSql = malloc( nSql+1 );
1492 strcpy(zSql, zLine);
1493 }
1494 }else{
1495 int len = strlen(zLine);
1496 zSql = realloc( zSql, nSql + len + 2 );
1497 if( zSql==0 ){
1498 fprintf(stderr,"%s: out of memory!\n", Argv0);
1499 exit(1);
1500 }
1501 strcpy(&zSql[nSql++], "\n");
1502 strcpy(&zSql[nSql], zLine);
1503 nSql += len;
1504 }
1505 free(zLine);
danielk19776f8a5032004-05-10 10:34:51 +00001506 if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite3_complete(zSql) ){
drhdaffd0e2001-04-11 14:28:42 +00001507 p->cnt = 0;
drh44c2eb12003-04-30 11:38:26 +00001508 open_db(p);
danielk19776f8a5032004-05-10 10:34:51 +00001509 rc = sqlite3_exec(p->db, zSql, callback, p, &zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001510 if( rc || zErrMsg ){
drhdaffd0e2001-04-11 14:28:42 +00001511 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
drh7f953e22002-07-13 17:33:45 +00001512 if( zErrMsg!=0 ){
1513 printf("SQL error: %s\n", zErrMsg);
drh3f4fedb2004-05-31 19:34:33 +00001514 sqlite3_free(zErrMsg);
drh7f953e22002-07-13 17:33:45 +00001515 zErrMsg = 0;
1516 }else{
danielk19772a02e332004-06-05 08:04:36 +00001517 printf("SQL error: %s\n", sqlite3_errmsg(p->db));
drh7f953e22002-07-13 17:33:45 +00001518 }
drhdaffd0e2001-04-11 14:28:42 +00001519 }
1520 free(zSql);
1521 zSql = 0;
1522 nSql = 0;
1523 }
1524 }
1525 if( zSql ){
drh70c7a4b2003-04-26 03:03:06 +00001526 if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);
drhdaffd0e2001-04-11 14:28:42 +00001527 free(zSql);
1528 }
1529}
1530
drh67505e72002-04-19 12:34:06 +00001531/*
1532** Return a pathname which is the user's home directory. A
1533** 0 return indicates an error of some kind. Space to hold the
1534** resulting string is obtained from malloc(). The calling
1535** function should free the result.
1536*/
1537static char *find_home_dir(void){
1538 char *home_dir = NULL;
persicom7e2dfdd2002-04-18 02:46:52 +00001539
drh820f3812003-01-08 13:02:52 +00001540#if !defined(_WIN32) && !defined(WIN32) && !defined(__MACOS__)
drh67505e72002-04-19 12:34:06 +00001541 struct passwd *pwent;
1542 uid_t uid = getuid();
drhbd842ba2002-08-21 11:26:41 +00001543 if( (pwent=getpwuid(uid)) != NULL) {
1544 home_dir = pwent->pw_dir;
drh67505e72002-04-19 12:34:06 +00001545 }
1546#endif
1547
drh820f3812003-01-08 13:02:52 +00001548#ifdef __MACOS__
1549 char home_path[_MAX_PATH+1];
1550 home_dir = getcwd(home_path, _MAX_PATH);
1551#endif
1552
drh67505e72002-04-19 12:34:06 +00001553 if (!home_dir) {
1554 home_dir = getenv("HOME");
1555 if (!home_dir) {
1556 home_dir = getenv("HOMEPATH"); /* Windows? */
1557 }
1558 }
1559
drhe98d4fa2002-04-21 19:06:22 +00001560#if defined(_WIN32) || defined(WIN32)
1561 if (!home_dir) {
1562 home_dir = "c:";
1563 }
1564#endif
1565
drh67505e72002-04-19 12:34:06 +00001566 if( home_dir ){
1567 char *z = malloc( strlen(home_dir)+1 );
1568 if( z ) strcpy(z, home_dir);
1569 home_dir = z;
1570 }
drhe98d4fa2002-04-21 19:06:22 +00001571
drh67505e72002-04-19 12:34:06 +00001572 return home_dir;
1573}
1574
1575/*
1576** Read input from the file given by sqliterc_override. Or if that
1577** parameter is NULL, take input from ~/.sqliterc
1578*/
drh22fbcb82004-02-01 01:22:50 +00001579static void process_sqliterc(
1580 struct callback_data *p, /* Configuration data */
1581 const char *sqliterc_override /* Name of config file. NULL to use default */
1582){
persicom7e2dfdd2002-04-18 02:46:52 +00001583 char *home_dir = NULL;
drh22fbcb82004-02-01 01:22:50 +00001584 const char *sqliterc = sqliterc_override;
1585 char *zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001586 FILE *in = NULL;
1587
1588 if (sqliterc == NULL) {
drh67505e72002-04-19 12:34:06 +00001589 home_dir = find_home_dir();
drhe98d4fa2002-04-21 19:06:22 +00001590 if( home_dir==0 ){
1591 fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0);
1592 return;
1593 }
drh22fbcb82004-02-01 01:22:50 +00001594 zBuf = malloc(strlen(home_dir) + 15);
1595 if( zBuf==0 ){
persicom7e2dfdd2002-04-18 02:46:52 +00001596 fprintf(stderr,"%s: out of memory!\n", Argv0);
1597 exit(1);
1598 }
drh22fbcb82004-02-01 01:22:50 +00001599 sprintf(zBuf,"%s/.sqliterc",home_dir);
drh67505e72002-04-19 12:34:06 +00001600 free(home_dir);
drh22fbcb82004-02-01 01:22:50 +00001601 sqliterc = (const char*)zBuf;
persicom7e2dfdd2002-04-18 02:46:52 +00001602 }
drha1f9b5e2004-02-14 16:31:02 +00001603 in = fopen(sqliterc,"rb");
drh22fbcb82004-02-01 01:22:50 +00001604 if( in ){
1605 if( isatty(fileno(stdout)) ){
1606 printf("Loading resources from %s\n",sqliterc);
1607 }
persicom7e2dfdd2002-04-18 02:46:52 +00001608 process_input(p,in);
drhdd45df82002-04-18 12:39:03 +00001609 fclose(in);
persicom7e2dfdd2002-04-18 02:46:52 +00001610 }
1611 return;
1612}
1613
drh67505e72002-04-19 12:34:06 +00001614/*
drhe1e38c42003-05-04 18:30:59 +00001615** Show available command line options
1616*/
1617static const char zOptions[] =
1618 " -init filename read/process named file\n"
1619 " -echo print commands before execution\n"
1620 " -[no]header turn headers on or off\n"
1621 " -column set output mode to 'column'\n"
1622 " -html set output mode to HTML\n"
drh9eb9e262004-02-11 02:18:05 +00001623#ifdef SQLITE_HAS_CODEC
drh57ced912004-02-10 02:57:59 +00001624 " -key KEY encryption key\n"
1625#endif
drhe1e38c42003-05-04 18:30:59 +00001626 " -line set output mode to 'line'\n"
1627 " -list set output mode to 'list'\n"
1628 " -separator 'x' set output field separator (|)\n"
1629 " -nullvalue 'text' set text string for NULL values\n"
1630 " -version show SQLite version\n"
1631 " -help show this text, also show dot-commands\n"
1632;
1633static void usage(int showDetail){
1634 fprintf(stderr, "Usage: %s [OPTIONS] FILENAME [SQL]\n", Argv0);
1635 if( showDetail ){
1636 fprintf(stderr, "Options are:\n%s", zOptions);
1637 }else{
1638 fprintf(stderr, "Use the -help option for additional information\n");
1639 }
1640 exit(1);
1641}
1642
1643/*
drh67505e72002-04-19 12:34:06 +00001644** Initialize the state information in data
1645*/
persicom7e2dfdd2002-04-18 02:46:52 +00001646void main_init(struct callback_data *data) {
1647 memset(data, 0, sizeof(*data));
1648 data->mode = MODE_List;
1649 strcpy(data->separator,"|");
1650 data->showHeader = 0;
1651 strcpy(mainPrompt,"sqlite> ");
1652 strcpy(continuePrompt," ...> ");
1653}
1654
drh75897232000-05-29 14:26:00 +00001655int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +00001656 char *zErrMsg = 0;
1657 struct callback_data data;
drh22fbcb82004-02-01 01:22:50 +00001658 const char *zInitFile = 0;
1659 char *zFirstCmd = 0;
drh44c2eb12003-04-30 11:38:26 +00001660 int i;
drh75897232000-05-29 14:26:00 +00001661
drh820f3812003-01-08 13:02:52 +00001662#ifdef __MACOS__
1663 argc = ccommand(&argv);
drh820f3812003-01-08 13:02:52 +00001664#endif
1665
drhdaffd0e2001-04-11 14:28:42 +00001666 Argv0 = argv[0];
persicom7e2dfdd2002-04-18 02:46:52 +00001667 main_init(&data);
persicom7e2dfdd2002-04-18 02:46:52 +00001668
drh44c2eb12003-04-30 11:38:26 +00001669 /* Make sure we have a valid signal handler early, before anything
1670 ** else is done.
1671 */
drh4c504392000-10-16 22:06:40 +00001672#ifdef SIGINT
1673 signal(SIGINT, interrupt_handler);
1674#endif
drh44c2eb12003-04-30 11:38:26 +00001675
drh22fbcb82004-02-01 01:22:50 +00001676 /* Do an initial pass through the command-line argument to locate
1677 ** the name of the database file, the name of the initialization file,
1678 ** and the first command to execute.
drh44c2eb12003-04-30 11:38:26 +00001679 */
drh22fbcb82004-02-01 01:22:50 +00001680 for(i=1; i<argc-1; i++){
drh44c2eb12003-04-30 11:38:26 +00001681 if( argv[i][0]!='-' ) break;
1682 if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
1683 i++;
drh22fbcb82004-02-01 01:22:50 +00001684 }else if( strcmp(argv[i],"-init")==0 ){
1685 i++;
1686 zInitFile = argv[i];
1687 }else if( strcmp(argv[i],"-key")==0 ){
1688 i++;
danielk19776f8a5032004-05-10 10:34:51 +00001689 data.zKey = sqlite3_mprintf("%s",argv[i]);
drh44c2eb12003-04-30 11:38:26 +00001690 }
1691 }
drh22fbcb82004-02-01 01:22:50 +00001692 if( i<argc ){
1693 data.zDbFilename = argv[i++];
1694 }else{
danielk197703aded42004-11-22 05:26:27 +00001695#ifndef SQLITE_OMIT_MEMORYDB
drh22fbcb82004-02-01 01:22:50 +00001696 data.zDbFilename = ":memory:";
danielk197703aded42004-11-22 05:26:27 +00001697#else
1698 data.zDbFilename = 0;
1699#endif
drh22fbcb82004-02-01 01:22:50 +00001700 }
1701 if( i<argc ){
1702 zFirstCmd = argv[i++];
1703 }
drh44c2eb12003-04-30 11:38:26 +00001704 data.out = stdout;
1705
drh01b41712005-08-29 23:06:23 +00001706#ifdef SQLITE_OMIT_MEMORYDB
1707 if( data.zDbFilename==0 ){
1708 fprintf(stderr,"%s: no database filename specified\n", argv[0]);
1709 exit(1);
1710 }
1711#endif
1712
drh44c2eb12003-04-30 11:38:26 +00001713 /* Go ahead and open the database file if it already exists. If the
1714 ** file does not exist, delay opening it. This prevents empty database
1715 ** files from being created if a user mistypes the database name argument
1716 ** to the sqlite command-line tool.
1717 */
drhc8d74412004-08-31 23:41:26 +00001718 if( access(data.zDbFilename, 0)==0 ){
drh44c2eb12003-04-30 11:38:26 +00001719 open_db(&data);
1720 }
1721
drh22fbcb82004-02-01 01:22:50 +00001722 /* Process the initialization file if there is one. If no -init option
1723 ** is given on the command line, look for a file named ~/.sqliterc and
1724 ** try to process it.
drh44c2eb12003-04-30 11:38:26 +00001725 */
drh22fbcb82004-02-01 01:22:50 +00001726 process_sqliterc(&data,zInitFile);
drh44c2eb12003-04-30 11:38:26 +00001727
drh22fbcb82004-02-01 01:22:50 +00001728 /* Make a second pass through the command-line argument and set
1729 ** options. This second pass is delayed until after the initialization
1730 ** file is processed so that the command-line arguments will override
1731 ** settings in the initialization file.
drh44c2eb12003-04-30 11:38:26 +00001732 */
drh22fbcb82004-02-01 01:22:50 +00001733 for(i=1; i<argc && argv[i][0]=='-'; i++){
1734 char *z = argv[i];
1735 if( strcmp(z,"-init")==0 || strcmp(z,"-key")==0 ){
1736 i++;
1737 }else if( strcmp(z,"-html")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001738 data.mode = MODE_Html;
drh22fbcb82004-02-01 01:22:50 +00001739 }else if( strcmp(z,"-list")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001740 data.mode = MODE_List;
drh22fbcb82004-02-01 01:22:50 +00001741 }else if( strcmp(z,"-line")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001742 data.mode = MODE_Line;
drh22fbcb82004-02-01 01:22:50 +00001743 }else if( strcmp(z,"-column")==0 ){
drh8b32e172002-04-08 02:42:57 +00001744 data.mode = MODE_Column;
drh22fbcb82004-02-01 01:22:50 +00001745 }else if( strcmp(z,"-separator")==0 ){
1746 i++;
1747 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[i]);
1748 }else if( strcmp(z,"-nullvalue")==0 ){
1749 i++;
1750 sprintf(data.nullvalue,"%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
1751 }else if( strcmp(z,"-header")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001752 data.showHeader = 1;
drh22fbcb82004-02-01 01:22:50 +00001753 }else if( strcmp(z,"-noheader")==0 ){
drh1e5d0e92000-05-31 23:33:17 +00001754 data.showHeader = 0;
drh22fbcb82004-02-01 01:22:50 +00001755 }else if( strcmp(z,"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001756 data.echoOn = 1;
drh22fbcb82004-02-01 01:22:50 +00001757 }else if( strcmp(z,"-version")==0 ){
drhc8d74412004-08-31 23:41:26 +00001758 printf("%s\n", sqlite3_libversion());
drhe1e38c42003-05-04 18:30:59 +00001759 return 1;
drh22fbcb82004-02-01 01:22:50 +00001760 }else if( strcmp(z,"-help")==0 ){
drhe1e38c42003-05-04 18:30:59 +00001761 usage(1);
drh1e5d0e92000-05-31 23:33:17 +00001762 }else{
drh22fbcb82004-02-01 01:22:50 +00001763 fprintf(stderr,"%s: unknown option: %s\n", Argv0, z);
drhe1e38c42003-05-04 18:30:59 +00001764 fprintf(stderr,"Use -help for a list of options.\n");
drh1e5d0e92000-05-31 23:33:17 +00001765 return 1;
1766 }
1767 }
drh44c2eb12003-04-30 11:38:26 +00001768
drh22fbcb82004-02-01 01:22:50 +00001769 if( zFirstCmd ){
drh44c2eb12003-04-30 11:38:26 +00001770 /* Run just the command that follows the database name
1771 */
drh22fbcb82004-02-01 01:22:50 +00001772 if( zFirstCmd[0]=='.' ){
1773 do_meta_command(zFirstCmd, &data);
drh6ff13852001-11-25 13:18:23 +00001774 exit(0);
1775 }else{
1776 int rc;
drh44c2eb12003-04-30 11:38:26 +00001777 open_db(&data);
danielk19776f8a5032004-05-10 10:34:51 +00001778 rc = sqlite3_exec(data.db, zFirstCmd, callback, &data, &zErrMsg);
drh6ff13852001-11-25 13:18:23 +00001779 if( rc!=0 && zErrMsg!=0 ){
1780 fprintf(stderr,"SQL error: %s\n", zErrMsg);
1781 exit(1);
1782 }
drh75897232000-05-29 14:26:00 +00001783 }
1784 }else{
drh44c2eb12003-04-30 11:38:26 +00001785 /* Run commands received from standard input
1786 */
drh5cf590c2003-04-24 01:45:04 +00001787 if( isatty(fileno(stdout)) && isatty(fileno(stdin)) ){
drh67505e72002-04-19 12:34:06 +00001788 char *zHome;
1789 char *zHistory = 0;
drh75897232000-05-29 14:26:00 +00001790 printf(
drhb217a572000-08-22 13:40:18 +00001791 "SQLite version %s\n"
1792 "Enter \".help\" for instructions\n",
drhc8d74412004-08-31 23:41:26 +00001793 sqlite3_libversion()
drh75897232000-05-29 14:26:00 +00001794 );
drh67505e72002-04-19 12:34:06 +00001795 zHome = find_home_dir();
1796 if( zHome && (zHistory = malloc(strlen(zHome)+20))!=0 ){
1797 sprintf(zHistory,"%s/.sqlite_history", zHome);
1798 }
danielk19774af00c62005-01-23 23:43:21 +00001799#if defined(HAVE_READLINE) && HAVE_READLINE==1
drh67505e72002-04-19 12:34:06 +00001800 if( zHistory ) read_history(zHistory);
danielk19774af00c62005-01-23 23:43:21 +00001801#endif
drhdaffd0e2001-04-11 14:28:42 +00001802 process_input(&data, 0);
drh67505e72002-04-19 12:34:06 +00001803 if( zHistory ){
1804 stifle_history(100);
1805 write_history(zHistory);
1806 }
drhdaffd0e2001-04-11 14:28:42 +00001807 }else{
1808 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +00001809 }
1810 }
drh33048c02001-10-01 14:29:22 +00001811 set_table_name(&data, 0);
danielk19776f8a5032004-05-10 10:34:51 +00001812 if( db ) sqlite3_close(db);
drh75897232000-05-29 14:26:00 +00001813 return 0;
1814}