blob: b2368d009a1f96c74a0bf4aac729ec9ccf4d1239 [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**
drh4ff6dfa2002-03-03 23:06:00 +000015** $Id: shell.c,v 1.47 2002/03/03 23:06:01 drh Exp $
drh75897232000-05-29 14:26:00 +000016*/
17#include <stdlib.h>
18#include <string.h>
19#include <stdio.h>
20#include "sqlite.h"
drh75897232000-05-29 14:26:00 +000021#include <ctype.h>
drha297b5c2002-01-15 18:39:43 +000022#if !defined(_WIN32) && !defined(WIN32)
drh4c504392000-10-16 22:06:40 +000023# include <signal.h>
24#endif
drh75897232000-05-29 14:26:00 +000025
drh16e59552000-07-31 11:57:37 +000026#if defined(HAVE_READLINE) && HAVE_READLINE==1
drh8e7e7a22000-05-30 18:45:23 +000027# include <readline/readline.h>
28# include <readline/history.h>
29#else
drh5e00f6c2001-09-13 13:46:56 +000030# define readline(p) getline(p,stdin)
drh8e7e7a22000-05-30 18:45:23 +000031# define add_history(X)
drh75897232000-05-29 14:26:00 +000032#endif
33
34/*
drh4c504392000-10-16 22:06:40 +000035** The following is the open SQLite database. We make a pointer
36** to this database a static variable so that it can be accessed
37** by the SIGINT handler to interrupt database processing.
38*/
39static sqlite *db = 0;
40
41/*
drh8e7e7a22000-05-30 18:45:23 +000042** This routine reads a line of text from standard input, stores
43** the text in memory obtained from malloc() and returns a pointer
44** to the text. NULL is returned at end of file, or if malloc()
45** fails.
46**
47** The interface is like "readline" but no command-line editing
48** is done.
49*/
drhdaffd0e2001-04-11 14:28:42 +000050static char *getline(char *zPrompt, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +000051 char *zLine;
52 int nLine;
drh8e7e7a22000-05-30 18:45:23 +000053 int n;
54 int eol;
55
56 if( zPrompt && *zPrompt ){
57 printf("%s",zPrompt);
58 fflush(stdout);
59 }
60 nLine = 100;
61 zLine = malloc( nLine );
62 if( zLine==0 ) return 0;
63 n = 0;
64 eol = 0;
65 while( !eol ){
66 if( n+100>nLine ){
67 nLine = nLine*2 + 100;
68 zLine = realloc(zLine, nLine);
69 if( zLine==0 ) return 0;
70 }
drhdaffd0e2001-04-11 14:28:42 +000071 if( fgets(&zLine[n], nLine - n, in)==0 ){
drh8e7e7a22000-05-30 18:45:23 +000072 if( n==0 ){
73 free(zLine);
74 return 0;
75 }
76 zLine[n] = 0;
77 eol = 1;
78 break;
79 }
80 while( zLine[n] ){ n++; }
81 if( n>0 && zLine[n-1]=='\n' ){
82 n--;
83 zLine[n] = 0;
84 eol = 1;
85 }
86 }
87 zLine = realloc( zLine, n+1 );
88 return zLine;
89}
90
91/*
92** Retrieve a single line of input text. "isatty" is true if text
93** is coming from a terminal. In that case, we issue a prompt and
94** attempt to use "readline" for command-line editing. If "isatty"
drhaacc5432002-01-06 17:07:40 +000095** is false, use "getline" instead of "readline" and issue no prompt.
drh8e7e7a22000-05-30 18:45:23 +000096**
97** zPrior is a string of prior text retrieved. If not the empty
98** string, then issue a continuation prompt.
99*/
drhdaffd0e2001-04-11 14:28:42 +0000100static char *one_input_line(const char *zPrior, FILE *in){
drh8e7e7a22000-05-30 18:45:23 +0000101 char *zPrompt;
102 char *zResult;
drhdaffd0e2001-04-11 14:28:42 +0000103 if( in!=0 ){
104 return getline(0, in);
drh8e7e7a22000-05-30 18:45:23 +0000105 }
106 if( zPrior && zPrior[0] ){
107 zPrompt = " ...> ";
108 }else{
109 zPrompt = "sqlite> ";
110 }
111 zResult = readline(zPrompt);
drh2dfbbca2000-07-28 14:32:48 +0000112 if( zResult ) add_history(zResult);
drh8e7e7a22000-05-30 18:45:23 +0000113 return zResult;
114}
115
116/*
drh75897232000-05-29 14:26:00 +0000117** An pointer to an instance of this structure is passed from
118** the main program to the callback. This is used to communicate
119** state and mode information.
120*/
121struct callback_data {
drh28bd4bc2000-06-15 15:57:22 +0000122 sqlite *db; /* The database */
drhdaffd0e2001-04-11 14:28:42 +0000123 int echoOn; /* True to echo input commands */
drh28bd4bc2000-06-15 15:57:22 +0000124 int cnt; /* Number of records displayed so far */
125 FILE *out; /* Write results here */
126 int mode; /* An output mode setting */
127 int showHeader; /* True to show column names in List or Column mode */
drh33048c02001-10-01 14:29:22 +0000128 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000129 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000130 int colWidth[100]; /* Requested width of each column when in column mode*/
131 int actualWidth[100]; /* Actual width of each column */
drh75897232000-05-29 14:26:00 +0000132};
133
134/*
135** These are the allowed modes.
136*/
drh967e8b72000-06-21 13:59:10 +0000137#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +0000138#define MODE_Column 1 /* One record per line in neat columns */
139#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +0000140#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
141#define MODE_Html 4 /* Generate an XHTML table */
142#define MODE_Insert 5 /* Generate SQL "insert" statements */
drh75897232000-05-29 14:26:00 +0000143
144/*
145** Number of elements in an array
146*/
147#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
148
149/*
drh28bd4bc2000-06-15 15:57:22 +0000150** Return TRUE if the string supplied is a number of some kinds.
151*/
152static int is_numeric(const char *z){
153 int seen_digit = 0;
154 if( *z=='-' || *z=='+' ){
155 z++;
156 }
157 while( isdigit(*z) ){
158 seen_digit = 1;
159 z++;
160 }
161 if( seen_digit && *z=='.' ){
162 z++;
163 while( isdigit(*z) ){ z++; }
164 }
165 if( seen_digit && (*z=='e' || *z=='E')
166 && (isdigit(z[1]) || ((z[1]=='-' || z[1]=='+') && isdigit(z[2])))
167 ){
168 z+=2;
169 while( isdigit(*z) ){ z++; }
170 }
171 return seen_digit && *z==0;
172}
173
174/*
175** Output the given string as a quoted string using SQL quoting conventions.
176*/
177static void output_quoted_string(FILE *out, const char *z){
178 int i;
179 int nSingle = 0;
180 int nDouble = 0;
181 for(i=0; z[i]; i++){
182 if( z[i]=='\'' ) nSingle++;
183 else if( z[i]=='"' ) nDouble++;
184 }
185 if( nSingle==0 ){
186 fprintf(out,"'%s'",z);
187 }else if( nDouble==0 ){
188 fprintf(out,"\"%s\"",z);
189 }else{
190 fprintf(out,"'");
191 while( *z ){
192 for(i=0; z[i] && z[i]!='\''; i++){}
193 if( i==0 ){
194 fprintf(out,"''");
195 z++;
196 }else if( z[i]=='\'' ){
197 fprintf(out,"%.*s''",i,z);
198 z += i+1;
199 }else{
drhcd7d2732002-02-26 23:24:26 +0000200 fprintf(out,"%s",z);
drh28bd4bc2000-06-15 15:57:22 +0000201 break;
202 }
203 }
drhcd7d2732002-02-26 23:24:26 +0000204 fprintf(out,"'");
drh28bd4bc2000-06-15 15:57:22 +0000205 }
206}
207
208/*
drhc08a4f12000-06-15 16:49:48 +0000209** Output the given string with characters that are special to
210** HTML escaped.
211*/
212static void output_html_string(FILE *out, const char *z){
213 int i;
214 while( *z ){
215 for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){}
216 if( i>0 ){
217 fprintf(out,"%.*s",i,z);
218 }
219 if( z[i]=='<' ){
220 fprintf(out,"&lt;");
221 }else if( z[i]=='&' ){
222 fprintf(out,"&amp;");
223 }else{
224 break;
225 }
226 z += i + 1;
227 }
228}
229
230/*
drh4c504392000-10-16 22:06:40 +0000231** This routine runs when the user presses Ctrl-C
232*/
233static void interrupt_handler(int NotUsed){
234 if( db ) sqlite_interrupt(db);
235}
236
237/*
drh75897232000-05-29 14:26:00 +0000238** This is the callback routine that the SQLite library
239** invokes for each row of a query result.
240*/
241static int callback(void *pArg, int nArg, char **azArg, char **azCol){
242 int i;
243 struct callback_data *p = (struct callback_data*)pArg;
244 switch( p->mode ){
245 case MODE_Line: {
drhe3710332000-09-29 13:30:53 +0000246 int w = 5;
drh6a535342001-10-19 16:44:56 +0000247 if( azArg==0 ) break;
drhe3710332000-09-29 13:30:53 +0000248 for(i=0; i<nArg; i++){
249 int len = strlen(azCol[i]);
250 if( len>w ) w = len;
251 }
drh75897232000-05-29 14:26:00 +0000252 if( p->cnt++>0 ) fprintf(p->out,"\n");
253 for(i=0; i<nArg; i++){
drhe3710332000-09-29 13:30:53 +0000254 fprintf(p->out,"%*s = %s\n", w, azCol[i], azArg[i] ? azArg[i] : 0);
drh75897232000-05-29 14:26:00 +0000255 }
256 break;
257 }
258 case MODE_Column: {
drha0c66f52000-07-29 13:20:21 +0000259 if( p->cnt++==0 ){
drh75897232000-05-29 14:26:00 +0000260 for(i=0; i<nArg; i++){
drha0c66f52000-07-29 13:20:21 +0000261 int w, n;
262 if( i<ArraySize(p->colWidth) ){
drh75897232000-05-29 14:26:00 +0000263 w = p->colWidth[i];
264 }else{
drha0c66f52000-07-29 13:20:21 +0000265 w = 0;
drh75897232000-05-29 14:26:00 +0000266 }
drha0c66f52000-07-29 13:20:21 +0000267 if( w<=0 ){
drhff6e9112000-08-28 16:21:58 +0000268 w = strlen(azCol[i] ? azCol[i] : "");
drha0c66f52000-07-29 13:20:21 +0000269 if( w<10 ) w = 10;
drh6a535342001-10-19 16:44:56 +0000270 n = strlen(azArg && azArg[i] ? azArg[i] : "");
drha0c66f52000-07-29 13:20:21 +0000271 if( w<n ) w = n;
272 }
273 if( i<ArraySize(p->actualWidth) ){
274 p->actualWidth[i] = w;
275 }
276 if( p->showHeader ){
277 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
278 }
279 }
280 if( p->showHeader ){
281 for(i=0; i<nArg; i++){
282 int w;
283 if( i<ArraySize(p->actualWidth) ){
284 w = p->actualWidth[i];
285 }else{
286 w = 10;
287 }
288 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
289 "----------------------------------------------------------",
290 i==nArg-1 ? "\n": " ");
291 }
drh75897232000-05-29 14:26:00 +0000292 }
293 }
drh6a535342001-10-19 16:44:56 +0000294 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000295 for(i=0; i<nArg; i++){
296 int w;
drha0c66f52000-07-29 13:20:21 +0000297 if( i<ArraySize(p->actualWidth) ){
298 w = p->actualWidth[i];
drh75897232000-05-29 14:26:00 +0000299 }else{
300 w = 10;
301 }
drhc61053b2000-06-04 12:58:36 +0000302 fprintf(p->out,"%-*.*s%s",w,w,
303 azArg[i] ? azArg[i] : "", i==nArg-1 ? "\n": " ");
drh75897232000-05-29 14:26:00 +0000304 }
305 break;
306 }
drhe3710332000-09-29 13:30:53 +0000307 case MODE_Semi:
drh75897232000-05-29 14:26:00 +0000308 case MODE_List: {
309 if( p->cnt++==0 && p->showHeader ){
310 for(i=0; i<nArg; i++){
311 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
312 }
313 }
drh6a535342001-10-19 16:44:56 +0000314 if( azArg==0 ) break;
drh75897232000-05-29 14:26:00 +0000315 for(i=0; i<nArg; i++){
drh4c653a02000-06-07 01:27:47 +0000316 char *z = azArg[i];
317 if( z==0 ) z = "";
drh71172c52002-01-24 00:00:21 +0000318 fprintf(p->out, "%s", z);
drhe3710332000-09-29 13:30:53 +0000319 if( i<nArg-1 ){
320 fprintf(p->out, "%s", p->separator);
321 }else if( p->mode==MODE_Semi ){
322 fprintf(p->out, ";\n");
323 }else{
324 fprintf(p->out, "\n");
325 }
drh75897232000-05-29 14:26:00 +0000326 }
327 break;
328 }
drh1e5d0e92000-05-31 23:33:17 +0000329 case MODE_Html: {
330 if( p->cnt++==0 && p->showHeader ){
331 fprintf(p->out,"<TR>");
332 for(i=0; i<nArg; i++){
333 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
334 }
335 fprintf(p->out,"</TR>\n");
336 }
drh6a535342001-10-19 16:44:56 +0000337 if( azArg==0 ) break;
drh28bd4bc2000-06-15 15:57:22 +0000338 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +0000339 for(i=0; i<nArg; i++){
drhc08a4f12000-06-15 16:49:48 +0000340 fprintf(p->out,"<TD>");
341 output_html_string(p->out, azArg[i] ? azArg[i] : "");
342 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +0000343 }
drh28bd4bc2000-06-15 15:57:22 +0000344 fprintf(p->out,"</TD></TR>\n");
drh1e5d0e92000-05-31 23:33:17 +0000345 break;
346 }
drh28bd4bc2000-06-15 15:57:22 +0000347 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +0000348 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +0000349 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +0000350 for(i=0; i<nArg; i++){
351 char *zSep = i>0 ? ",": "";
352 if( azArg[i]==0 ){
353 fprintf(p->out,"%sNULL",zSep);
354 }else if( is_numeric(azArg[i]) ){
355 fprintf(p->out,"%s%s",zSep, azArg[i]);
356 }else{
357 if( zSep[0] ) fprintf(p->out,"%s",zSep);
358 output_quoted_string(p->out, azArg[i]);
359 }
360 }
361 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +0000362 break;
drh28bd4bc2000-06-15 15:57:22 +0000363 }
drh75897232000-05-29 14:26:00 +0000364 }
365 return 0;
366}
367
368/*
drh33048c02001-10-01 14:29:22 +0000369** Set the destination table field of the callback_data structure to
370** the name of the table given. Escape any quote characters in the
371** table name.
372*/
373static void set_table_name(struct callback_data *p, const char *zName){
374 int i, n;
375 int needQuote;
376 char *z;
377
378 if( p->zDestTable ){
379 free(p->zDestTable);
380 p->zDestTable = 0;
381 }
382 if( zName==0 ) return;
383 needQuote = !isalpha(*zName) && *zName!='_';
384 for(i=n=0; zName[i]; i++, n++){
385 if( !isalnum(zName[i]) && zName[i]!='_' ){
386 needQuote = 1;
387 if( zName[i]=='\'' ) n++;
388 }
389 }
390 if( needQuote ) n += 2;
391 z = p->zDestTable = malloc( n+1 );
392 if( z==0 ){
393 fprintf(stderr,"Out of memory!\n");
394 exit(1);
395 }
396 n = 0;
397 if( needQuote ) z[n++] = '\'';
398 for(i=0; zName[i]; i++){
399 z[n++] = zName[i];
400 if( zName[i]=='\'' ) z[n++] = '\'';
401 }
402 if( needQuote ) z[n++] = '\'';
403 z[n] = 0;
404}
405
406/*
drh4c653a02000-06-07 01:27:47 +0000407** This is a different callback routine used for dumping the database.
408** Each row received by this callback consists of a table name,
409** the table type ("index" or "table") and SQL to create the table.
410** This routine should print text sufficient to recreate the table.
411*/
412static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
drhdaffd0e2001-04-11 14:28:42 +0000413 struct callback_data *p = (struct callback_data *)pArg;
drh4c653a02000-06-07 01:27:47 +0000414 if( nArg!=3 ) return 1;
drhdaffd0e2001-04-11 14:28:42 +0000415 fprintf(p->out, "%s;\n", azArg[2]);
drh4c653a02000-06-07 01:27:47 +0000416 if( strcmp(azArg[1],"table")==0 ){
417 struct callback_data d2;
drhdaffd0e2001-04-11 14:28:42 +0000418 d2 = *p;
drh33048c02001-10-01 14:29:22 +0000419 d2.mode = MODE_Insert;
420 d2.zDestTable = 0;
421 set_table_name(&d2, azArg[0]);
drhdaffd0e2001-04-11 14:28:42 +0000422 sqlite_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000423 "SELECT * FROM '%q'",
424 callback, &d2, 0, azArg[0]
425 );
drh33048c02001-10-01 14:29:22 +0000426 set_table_name(&d2, 0);
drh4c653a02000-06-07 01:27:47 +0000427 }
drh4c653a02000-06-07 01:27:47 +0000428 return 0;
429}
430
431/*
drh75897232000-05-29 14:26:00 +0000432** Text of a help message
433*/
434static char zHelp[] =
drh4c653a02000-06-07 01:27:47 +0000435 ".dump ?TABLE? ... Dump the database in an text format\n"
drhdaffd0e2001-04-11 14:28:42 +0000436 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +0000437 ".exit Exit this program\n"
438 ".explain Set output mode suitable for EXPLAIN\n"
439 ".header ON|OFF Turn display of headers on or off\n"
440 ".help Show this message\n"
441 ".indices TABLE Show names of all indices on TABLE\n"
drhe3710332000-09-29 13:30:53 +0000442 ".mode MODE Set mode to one of \"line\", \"column\", \n"
443 " \"insert\", \"list\", or \"html\"\n"
drhc08a4f12000-06-15 16:49:48 +0000444 ".mode insert TABLE Generate SQL insert statements for TABLE\n"
drh75897232000-05-29 14:26:00 +0000445 ".output FILENAME Send output to FILENAME\n"
446 ".output stdout Send output to the screen\n"
drhdaffd0e2001-04-11 14:28:42 +0000447 ".read FILENAME Execute SQL in FILENAME\n"
448 ".reindex ?TABLE? Rebuild indices\n"
449/* ".rename OLD NEW Change the name of a table or index\n" */
drh75897232000-05-29 14:26:00 +0000450 ".schema ?TABLE? Show the CREATE statements\n"
451 ".separator STRING Change separator string for \"list\" mode\n"
drha50da102000-08-08 20:19:09 +0000452 ".tables ?PATTERN? List names of tables matching a pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000453 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000454 ".width NUM NUM ... Set column widths for \"column\" mode\n"
455;
456
drhdaffd0e2001-04-11 14:28:42 +0000457/* Forward reference */
458static void process_input(struct callback_data *p, FILE *in);
459
drh75897232000-05-29 14:26:00 +0000460/*
461** If an input line begins with "." then invoke this routine to
462** process that line.
463*/
464static void do_meta_command(char *zLine, sqlite *db, struct callback_data *p){
465 int i = 1;
466 int nArg = 0;
467 int n, c;
468 char *azArg[50];
469
470 /* Parse the input line into tokens.
471 */
472 while( zLine[i] && nArg<ArraySize(azArg) ){
473 while( isspace(zLine[i]) ){ i++; }
474 if( zLine[i]=='\'' || zLine[i]=='"' ){
475 int delim = zLine[i++];
476 azArg[nArg++] = &zLine[i];
477 while( zLine[i] && zLine[i]!=delim ){ i++; }
478 if( zLine[i]==delim ){
479 zLine[i++] = 0;
480 }
481 }else{
482 azArg[nArg++] = &zLine[i];
483 while( zLine[i] && !isspace(zLine[i]) ){ i++; }
484 if( zLine[i] ) zLine[i++] = 0;
485 }
486 }
487
488 /* Process the input line.
489 */
490 if( nArg==0 ) return;
491 n = strlen(azArg[0]);
492 c = azArg[0][0];
drh4c653a02000-06-07 01:27:47 +0000493 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
494 char *zErrMsg = 0;
drh33048c02001-10-01 14:29:22 +0000495 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000496 if( nArg==1 ){
drha18c5682000-10-08 22:20:57 +0000497 sqlite_exec(db,
498 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000499 "WHERE type!='meta' AND sql NOT NULL "
drha18c5682000-10-08 22:20:57 +0000500 "ORDER BY tbl_name, type DESC, name",
501 dump_callback, p, &zErrMsg
502 );
drh4c653a02000-06-07 01:27:47 +0000503 }else{
504 int i;
505 for(i=1; i<nArg && zErrMsg==0; i++){
drha18c5682000-10-08 22:20:57 +0000506 sqlite_exec_printf(db,
507 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000508 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL "
drha18c5682000-10-08 22:20:57 +0000509 "ORDER BY type DESC, name",
510 dump_callback, p, &zErrMsg, azArg[i]
511 );
drh4c653a02000-06-07 01:27:47 +0000512
513 }
514 }
515 if( zErrMsg ){
516 fprintf(stderr,"Error: %s\n", zErrMsg);
517 free(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000518 }else{
519 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000520 }
521 }else
drh75897232000-05-29 14:26:00 +0000522
drhdaffd0e2001-04-11 14:28:42 +0000523 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
524 int j;
525 char *z = azArg[1];
526 int val = atoi(azArg[1]);
527 for(j=0; z[j]; j++){
528 if( isupper(z[j]) ) z[j] = tolower(z[j]);
529 }
530 if( strcmp(z,"on")==0 ){
531 val = 1;
532 }else if( strcmp(z,"yes")==0 ){
533 val = 1;
534 }
535 p->echoOn = val;
536 }else
537
drh75897232000-05-29 14:26:00 +0000538 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh04096482001-11-09 22:41:44 +0000539 sqlite_close(db);
drh75897232000-05-29 14:26:00 +0000540 exit(0);
541 }else
542
543 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
544 p->mode = MODE_Column;
545 p->showHeader = 1;
546 p->colWidth[0] = 4;
547 p->colWidth[1] = 12;
drh7a7c7392001-11-24 00:31:46 +0000548 p->colWidth[2] = 10;
549 p->colWidth[3] = 10;
550 p->colWidth[4] = 35;
drh75897232000-05-29 14:26:00 +0000551 }else
552
553 if( c=='h' && strncmp(azArg[0], "header", n)==0 && nArg>1 ){
554 int j;
555 char *z = azArg[1];
556 int val = atoi(azArg[1]);
557 for(j=0; z[j]; j++){
558 if( isupper(z[j]) ) z[j] = tolower(z[j]);
559 }
560 if( strcmp(z,"on")==0 ){
561 val = 1;
562 }else if( strcmp(z,"yes")==0 ){
563 val = 1;
564 }
565 p->showHeader = val;
566 }else
567
568 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
569 fprintf(stderr,zHelp);
570 }else
571
572 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
573 struct callback_data data;
574 char *zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000575 memcpy(&data, p, sizeof(data));
576 data.showHeader = 0;
577 data.mode = MODE_List;
drha18c5682000-10-08 22:20:57 +0000578 sqlite_exec_printf(db,
579 "SELECT name FROM sqlite_master "
580 "WHERE type='index' AND tbl_name LIKE '%q' "
581 "ORDER BY name",
582 callback, &data, &zErrMsg, azArg[1]
583 );
drh75897232000-05-29 14:26:00 +0000584 if( zErrMsg ){
585 fprintf(stderr,"Error: %s\n", zErrMsg);
586 free(zErrMsg);
587 }
588 }else
589
drh28bd4bc2000-06-15 15:57:22 +0000590 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +0000591 int n2 = strlen(azArg[1]);
592 if( strncmp(azArg[1],"line",n2)==0 ){
593 p->mode = MODE_Line;
594 }else if( strncmp(azArg[1],"column",n2)==0 ){
595 p->mode = MODE_Column;
596 }else if( strncmp(azArg[1],"list",n2)==0 ){
597 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +0000598 }else if( strncmp(azArg[1],"html",n2)==0 ){
599 p->mode = MODE_Html;
drh28bd4bc2000-06-15 15:57:22 +0000600 }else if( strncmp(azArg[1],"insert",n2)==0 ){
601 p->mode = MODE_Insert;
602 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +0000603 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +0000604 }else{
drh33048c02001-10-01 14:29:22 +0000605 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +0000606 }
drhdaffd0e2001-04-11 14:28:42 +0000607 }else {
608 fprintf(stderr,"mode should be on of: column html insert line list\n");
drh75897232000-05-29 14:26:00 +0000609 }
610 }else
611
612 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
613 if( p->out!=stdout ){
614 fclose(p->out);
615 }
616 if( strcmp(azArg[1],"stdout")==0 ){
617 p->out = stdout;
618 }else{
619 p->out = fopen(azArg[1], "w");
620 if( p->out==0 ){
621 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
622 p->out = stdout;
623 }
624 }
625 }else
626
drhdaffd0e2001-04-11 14:28:42 +0000627 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
628 FILE *alt = fopen(azArg[1], "r");
629 if( alt==0 ){
630 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
631 }else{
632 process_input(p, alt);
633 fclose(alt);
634 }
635 }else
636
637 if( c=='r' && strncmp(azArg[0], "reindex", n)==0 ){
638 char **azResult;
639 int nRow, rc;
640 char *zErrMsg;
641 int i;
642 char *zSql;
643 if( nArg==1 ){
644 rc = sqlite_get_table(db,
645 "SELECT name, sql FROM sqlite_master "
646 "WHERE type='index'",
647 &azResult, &nRow, 0, &zErrMsg
648 );
649 }else{
650 rc = sqlite_get_table_printf(db,
651 "SELECT name, sql FROM sqlite_master "
652 "WHERE type='index' AND tbl_name LIKE '%q'",
653 &azResult, &nRow, 0, &zErrMsg, azArg[1]
654 );
655 }
656 for(i=1; rc==SQLITE_OK && i<=nRow; i++){
657 extern char *sqlite_mprintf(const char *, ...);
658 zSql = sqlite_mprintf(
659 "DROP INDEX '%q';\n%s;\nVACUUM '%q';",
660 azResult[i*2], azResult[i*2+1], azResult[i*2]);
661 if( p->echoOn ) printf("%s\n", zSql);
662 rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
663 }
664 sqlite_free_table(azResult);
665 if( zErrMsg ){
666 fprintf(stderr,"Error: %s\n", zErrMsg);
667 free(zErrMsg);
668 }
669 }else
670
drh75897232000-05-29 14:26:00 +0000671 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
672 struct callback_data data;
673 char *zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000674 memcpy(&data, p, sizeof(data));
675 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +0000676 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +0000677 if( nArg>1 ){
drhff9821a2001-04-04 21:22:14 +0000678 extern int sqliteStrICmp(const char*,const char*);
drha18c5682000-10-08 22:20:57 +0000679 if( sqliteStrICmp(azArg[1],"sqlite_master")==0 ){
680 char *new_argv[2], *new_colv[2];
681 new_argv[0] = "CREATE TABLE sqlite_master (\n"
682 " type text,\n"
683 " name text,\n"
684 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000685 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +0000686 " sql text\n"
687 ")";
688 new_argv[1] = 0;
689 new_colv[0] = "sql";
690 new_colv[1] = 0;
691 callback(&data, 1, new_argv, new_colv);
692 }else{
693 sqlite_exec_printf(db,
694 "SELECT sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000695 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL "
drha18c5682000-10-08 22:20:57 +0000696 "ORDER BY type DESC, name",
697 callback, &data, &zErrMsg, azArg[1]);
698 }
drh75897232000-05-29 14:26:00 +0000699 }else{
drha18c5682000-10-08 22:20:57 +0000700 sqlite_exec(db,
701 "SELECT sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000702 "WHERE type!='meta' AND sql NOTNULL "
drha18c5682000-10-08 22:20:57 +0000703 "ORDER BY tbl_name, type DESC, name",
704 callback, &data, &zErrMsg
705 );
drh75897232000-05-29 14:26:00 +0000706 }
drh75897232000-05-29 14:26:00 +0000707 if( zErrMsg ){
708 fprintf(stderr,"Error: %s\n", zErrMsg);
709 free(zErrMsg);
710 }
711 }else
712
713 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
714 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
715 }else
716
drh2dfbbca2000-07-28 14:32:48 +0000717 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +0000718 char **azResult;
719 int nRow, rc;
720 char *zErrMsg;
drha50da102000-08-08 20:19:09 +0000721 if( nArg==1 ){
drha18c5682000-10-08 22:20:57 +0000722 rc = sqlite_get_table(db,
drha50da102000-08-08 20:19:09 +0000723 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +0000724 "WHERE type IN ('table','view') "
drha18c5682000-10-08 22:20:57 +0000725 "ORDER BY name",
726 &azResult, &nRow, 0, &zErrMsg
727 );
drha50da102000-08-08 20:19:09 +0000728 }else{
drha18c5682000-10-08 22:20:57 +0000729 rc = sqlite_get_table_printf(db,
drha50da102000-08-08 20:19:09 +0000730 "SELECT name FROM sqlite_master "
drh4ff6dfa2002-03-03 23:06:00 +0000731 "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
drha18c5682000-10-08 22:20:57 +0000732 "ORDER BY name",
733 &azResult, &nRow, 0, &zErrMsg, azArg[1]
734 );
drha50da102000-08-08 20:19:09 +0000735 }
drh75897232000-05-29 14:26:00 +0000736 if( zErrMsg ){
737 fprintf(stderr,"Error: %s\n", zErrMsg);
738 free(zErrMsg);
739 }
drhe3710332000-09-29 13:30:53 +0000740 if( rc==SQLITE_OK ){
741 int len, maxlen = 0;
742 int i, j;
743 int nPrintCol, nPrintRow;
744 for(i=1; i<=nRow; i++){
745 if( azResult[i]==0 ) continue;
746 len = strlen(azResult[i]);
747 if( len>maxlen ) maxlen = len;
748 }
749 nPrintCol = 80/(maxlen+2);
750 if( nPrintCol<1 ) nPrintCol = 1;
751 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
752 for(i=0; i<nPrintRow; i++){
753 for(j=i+1; j<=nRow; j+=nPrintRow){
754 char *zSp = j<=nPrintRow ? "" : " ";
755 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
756 }
757 printf("\n");
758 }
759 }
760 sqlite_free_table(azResult);
drh75897232000-05-29 14:26:00 +0000761 }else
762
drh2dfbbca2000-07-28 14:32:48 +0000763 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
764 sqlite_busy_timeout(db, atoi(azArg[1]));
765 }else
766
drh75897232000-05-29 14:26:00 +0000767 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
768 int j;
769 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
770 p->colWidth[j-1] = atoi(azArg[j]);
771 }
772 }else
773
774 {
775 fprintf(stderr, "unknown command: \"%s\". Enter \".help\" for help\n",
776 azArg[0]);
777 }
778}
779
drhdaffd0e2001-04-11 14:28:42 +0000780static char *Argv0;
781static void process_input(struct callback_data *p, FILE *in){
782 char *zLine;
783 char *zSql = 0;
784 int nSql = 0;
785 char *zErrMsg;
786 while( (zLine = one_input_line(zSql, in))!=0 ){
787 if( p->echoOn ) printf("%s\n", zLine);
drh2af0b2d2002-02-21 02:25:02 +0000788 if( zLine && zLine[0]=='.' && nSql==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000789 do_meta_command(zLine, db, p);
790 free(zLine);
791 continue;
792 }
793 if( zSql==0 ){
794 int i;
795 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
796 if( zLine[i]!=0 ){
797 nSql = strlen(zLine);
798 zSql = malloc( nSql+1 );
799 strcpy(zSql, zLine);
800 }
801 }else{
802 int len = strlen(zLine);
803 zSql = realloc( zSql, nSql + len + 2 );
804 if( zSql==0 ){
805 fprintf(stderr,"%s: out of memory!\n", Argv0);
806 exit(1);
807 }
808 strcpy(&zSql[nSql++], "\n");
809 strcpy(&zSql[nSql], zLine);
810 nSql += len;
811 }
812 free(zLine);
813 if( zSql && sqlite_complete(zSql) ){
814 p->cnt = 0;
815 if( sqlite_exec(db, zSql, callback, p, &zErrMsg)!=0
816 && zErrMsg!=0 ){
817 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
818 printf("SQL error: %s\n", zErrMsg);
819 free(zErrMsg);
820 zErrMsg = 0;
821 }
822 free(zSql);
823 zSql = 0;
824 nSql = 0;
825 }
826 }
827 if( zSql ){
828 printf("Incomplete SQL: %s\n", zSql);
829 free(zSql);
830 }
831}
832
drh75897232000-05-29 14:26:00 +0000833int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +0000834 char *zErrMsg = 0;
835 struct callback_data data;
836
drhdaffd0e2001-04-11 14:28:42 +0000837 Argv0 = argv[0];
drh1e5d0e92000-05-31 23:33:17 +0000838 memset(&data, 0, sizeof(data));
839 data.mode = MODE_List;
840 strcpy(data.separator,"|");
841 data.showHeader = 0;
drh4c504392000-10-16 22:06:40 +0000842#ifdef SIGINT
843 signal(SIGINT, interrupt_handler);
844#endif
drh1e5d0e92000-05-31 23:33:17 +0000845 while( argc>=2 && argv[1][0]=='-' ){
846 if( strcmp(argv[1],"-html")==0 ){
847 data.mode = MODE_Html;
848 argc--;
849 argv++;
850 }else if( strcmp(argv[1],"-list")==0 ){
851 data.mode = MODE_List;
852 argc--;
853 argv++;
854 }else if( strcmp(argv[1],"-line")==0 ){
855 data.mode = MODE_Line;
856 argc--;
857 argv++;
drh7613bfa2002-01-22 12:39:24 +0000858 }else if( argc>=3 && strcmp(argv[1],"-separator")==0 ){
drhbed86902000-06-02 13:27:59 +0000859 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[2]);
drh1e5d0e92000-05-31 23:33:17 +0000860 argc -= 2;
861 argv += 2;
862 }else if( strcmp(argv[1],"-header")==0 ){
863 data.showHeader = 1;
864 argc--;
865 argv++;
866 }else if( strcmp(argv[1],"-noheader")==0 ){
867 data.showHeader = 0;
868 argc--;
869 argv++;
drh660f68d2001-01-04 14:27:07 +0000870 }else if( strcmp(argv[1],"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000871 data.echoOn = 1;
drh660f68d2001-01-04 14:27:07 +0000872 argc--;
873 argv++;
drh1e5d0e92000-05-31 23:33:17 +0000874 }else{
drhdaffd0e2001-04-11 14:28:42 +0000875 fprintf(stderr,"%s: unknown option: %s\n", Argv0, argv[1]);
drh1e5d0e92000-05-31 23:33:17 +0000876 return 1;
877 }
878 }
drh75897232000-05-29 14:26:00 +0000879 if( argc!=2 && argc!=3 ){
drhdaffd0e2001-04-11 14:28:42 +0000880 fprintf(stderr,"Usage: %s ?OPTIONS? FILENAME ?SQL?\n", Argv0);
drh75897232000-05-29 14:26:00 +0000881 exit(1);
882 }
drh4c653a02000-06-07 01:27:47 +0000883 data.db = db = sqlite_open(argv[1], 0666, &zErrMsg);
drh75897232000-05-29 14:26:00 +0000884 if( db==0 ){
drh167a4b12000-08-17 09:49:59 +0000885 data.db = db = sqlite_open(argv[1], 0444, &zErrMsg);
886 if( db==0 ){
887 if( zErrMsg ){
888 fprintf(stderr,"Unable to open database \"%s\": %s\n", argv[1],zErrMsg);
889 }else{
890 fprintf(stderr,"Unable to open database %s\n", argv[1]);
891 }
892 exit(1);
drhd1dedb82000-06-05 02:07:04 +0000893 }else{
drh80afdca2000-08-22 13:27:22 +0000894 fprintf(stderr,"Database \"%s\" opened READ ONLY!\n", argv[1]);
drhd1dedb82000-06-05 02:07:04 +0000895 }
drh75897232000-05-29 14:26:00 +0000896 }
drh75897232000-05-29 14:26:00 +0000897 data.out = stdout;
898 if( argc==3 ){
drh6ff13852001-11-25 13:18:23 +0000899 if( argv[2][0]=='.' ){
900 do_meta_command(argv[2], db, &data);
901 exit(0);
902 }else{
903 int rc;
904 rc = sqlite_exec(db, argv[2], callback, &data, &zErrMsg);
905 if( rc!=0 && zErrMsg!=0 ){
906 fprintf(stderr,"SQL error: %s\n", zErrMsg);
907 exit(1);
908 }
drh75897232000-05-29 14:26:00 +0000909 }
910 }else{
drh382c0242001-10-06 16:33:02 +0000911 extern int isatty();
drhdaffd0e2001-04-11 14:28:42 +0000912 if( isatty(0) ){
drh75897232000-05-29 14:26:00 +0000913 printf(
drhb217a572000-08-22 13:40:18 +0000914 "SQLite version %s\n"
915 "Enter \".help\" for instructions\n",
916 sqlite_version
drh75897232000-05-29 14:26:00 +0000917 );
drhdaffd0e2001-04-11 14:28:42 +0000918 process_input(&data, 0);
919 }else{
920 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +0000921 }
922 }
drh33048c02001-10-01 14:29:22 +0000923 set_table_name(&data, 0);
drh75897232000-05-29 14:26:00 +0000924 sqlite_close(db);
925 return 0;
926}