blob: e9fc69845572f24dcecf9ffab8766c0db240a691 [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**
drh04096482001-11-09 22:41:44 +000015** $Id: shell.c,v 1.38 2001/11/09 22:41:45 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>
drh4c504392000-10-16 22:06:40 +000022#ifdef OS_UNIX
23# 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"
95** is false, use "getline" instead of "readline" and issue to prompt.
96**
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 */
128 int escape; /* Escape this character when in MODE_List */
drh33048c02001-10-01 14:29:22 +0000129 char *zDestTable; /* Name of destination table when MODE_Insert */
drh28bd4bc2000-06-15 15:57:22 +0000130 char separator[20]; /* Separator character for MODE_List */
drha0c66f52000-07-29 13:20:21 +0000131 int colWidth[100]; /* Requested width of each column when in column mode*/
132 int actualWidth[100]; /* Actual width of each column */
drh75897232000-05-29 14:26:00 +0000133};
134
135/*
136** These are the allowed modes.
137*/
drh967e8b72000-06-21 13:59:10 +0000138#define MODE_Line 0 /* One column per line. Blank line between records */
drh75897232000-05-29 14:26:00 +0000139#define MODE_Column 1 /* One record per line in neat columns */
140#define MODE_List 2 /* One record per line with a separator */
drhe3710332000-09-29 13:30:53 +0000141#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
142#define MODE_Html 4 /* Generate an XHTML table */
143#define MODE_Insert 5 /* Generate SQL "insert" statements */
drh75897232000-05-29 14:26:00 +0000144
145/*
146** Number of elements in an array
147*/
148#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
149
150/*
drh28bd4bc2000-06-15 15:57:22 +0000151** Return TRUE if the string supplied is a number of some kinds.
152*/
153static int is_numeric(const char *z){
154 int seen_digit = 0;
155 if( *z=='-' || *z=='+' ){
156 z++;
157 }
158 while( isdigit(*z) ){
159 seen_digit = 1;
160 z++;
161 }
162 if( seen_digit && *z=='.' ){
163 z++;
164 while( isdigit(*z) ){ z++; }
165 }
166 if( seen_digit && (*z=='e' || *z=='E')
167 && (isdigit(z[1]) || ((z[1]=='-' || z[1]=='+') && isdigit(z[2])))
168 ){
169 z+=2;
170 while( isdigit(*z) ){ z++; }
171 }
172 return seen_digit && *z==0;
173}
174
175/*
176** Output the given string as a quoted string using SQL quoting conventions.
177*/
178static void output_quoted_string(FILE *out, const char *z){
179 int i;
180 int nSingle = 0;
181 int nDouble = 0;
182 for(i=0; z[i]; i++){
183 if( z[i]=='\'' ) nSingle++;
184 else if( z[i]=='"' ) nDouble++;
185 }
186 if( nSingle==0 ){
187 fprintf(out,"'%s'",z);
188 }else if( nDouble==0 ){
189 fprintf(out,"\"%s\"",z);
190 }else{
191 fprintf(out,"'");
192 while( *z ){
193 for(i=0; z[i] && z[i]!='\''; i++){}
194 if( i==0 ){
195 fprintf(out,"''");
196 z++;
197 }else if( z[i]=='\'' ){
198 fprintf(out,"%.*s''",i,z);
199 z += i+1;
200 }else{
201 fprintf(out,"%s'",z);
202 break;
203 }
204 }
205 }
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 = "";
318 while( *z ){
319 int j;
320 for(j=0; z[j] && z[j]!=p->escape && z[j]!='\\'; j++){}
321 if( j>0 ){
322 fprintf(p->out, "%.*s", j, z);
323 }
324 if( z[j] ){
325 fprintf(p->out, "\\%c", z[j]);
drh670f74f2000-06-07 02:04:22 +0000326 z++;
drh4c653a02000-06-07 01:27:47 +0000327 }
drh73755922000-06-07 01:33:42 +0000328 z += j;
drh4c653a02000-06-07 01:27:47 +0000329 }
drhe3710332000-09-29 13:30:53 +0000330 if( i<nArg-1 ){
331 fprintf(p->out, "%s", p->separator);
332 }else if( p->mode==MODE_Semi ){
333 fprintf(p->out, ";\n");
334 }else{
335 fprintf(p->out, "\n");
336 }
drh75897232000-05-29 14:26:00 +0000337 }
338 break;
339 }
drh1e5d0e92000-05-31 23:33:17 +0000340 case MODE_Html: {
341 if( p->cnt++==0 && p->showHeader ){
342 fprintf(p->out,"<TR>");
343 for(i=0; i<nArg; i++){
344 fprintf(p->out,"<TH>%s</TH>",azCol[i]);
345 }
346 fprintf(p->out,"</TR>\n");
347 }
drh6a535342001-10-19 16:44:56 +0000348 if( azArg==0 ) break;
drh28bd4bc2000-06-15 15:57:22 +0000349 fprintf(p->out,"<TR>");
drh1e5d0e92000-05-31 23:33:17 +0000350 for(i=0; i<nArg; i++){
drhc08a4f12000-06-15 16:49:48 +0000351 fprintf(p->out,"<TD>");
352 output_html_string(p->out, azArg[i] ? azArg[i] : "");
353 fprintf(p->out,"</TD>\n");
drh1e5d0e92000-05-31 23:33:17 +0000354 }
drh28bd4bc2000-06-15 15:57:22 +0000355 fprintf(p->out,"</TD></TR>\n");
drh1e5d0e92000-05-31 23:33:17 +0000356 break;
357 }
drh28bd4bc2000-06-15 15:57:22 +0000358 case MODE_Insert: {
drh6a535342001-10-19 16:44:56 +0000359 if( azArg==0 ) break;
drh33048c02001-10-01 14:29:22 +0000360 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
drh28bd4bc2000-06-15 15:57:22 +0000361 for(i=0; i<nArg; i++){
362 char *zSep = i>0 ? ",": "";
363 if( azArg[i]==0 ){
364 fprintf(p->out,"%sNULL",zSep);
365 }else if( is_numeric(azArg[i]) ){
366 fprintf(p->out,"%s%s",zSep, azArg[i]);
367 }else{
368 if( zSep[0] ) fprintf(p->out,"%s",zSep);
369 output_quoted_string(p->out, azArg[i]);
370 }
371 }
372 fprintf(p->out,");\n");
drh6a535342001-10-19 16:44:56 +0000373 break;
drh28bd4bc2000-06-15 15:57:22 +0000374 }
drh75897232000-05-29 14:26:00 +0000375 }
376 return 0;
377}
378
379/*
drh33048c02001-10-01 14:29:22 +0000380** Set the destination table field of the callback_data structure to
381** the name of the table given. Escape any quote characters in the
382** table name.
383*/
384static void set_table_name(struct callback_data *p, const char *zName){
385 int i, n;
386 int needQuote;
387 char *z;
388
389 if( p->zDestTable ){
390 free(p->zDestTable);
391 p->zDestTable = 0;
392 }
393 if( zName==0 ) return;
394 needQuote = !isalpha(*zName) && *zName!='_';
395 for(i=n=0; zName[i]; i++, n++){
396 if( !isalnum(zName[i]) && zName[i]!='_' ){
397 needQuote = 1;
398 if( zName[i]=='\'' ) n++;
399 }
400 }
401 if( needQuote ) n += 2;
402 z = p->zDestTable = malloc( n+1 );
403 if( z==0 ){
404 fprintf(stderr,"Out of memory!\n");
405 exit(1);
406 }
407 n = 0;
408 if( needQuote ) z[n++] = '\'';
409 for(i=0; zName[i]; i++){
410 z[n++] = zName[i];
411 if( zName[i]=='\'' ) z[n++] = '\'';
412 }
413 if( needQuote ) z[n++] = '\'';
414 z[n] = 0;
415}
416
417/*
drh4c653a02000-06-07 01:27:47 +0000418** This is a different callback routine used for dumping the database.
419** Each row received by this callback consists of a table name,
420** the table type ("index" or "table") and SQL to create the table.
421** This routine should print text sufficient to recreate the table.
422*/
423static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
drhdaffd0e2001-04-11 14:28:42 +0000424 struct callback_data *p = (struct callback_data *)pArg;
drh4c653a02000-06-07 01:27:47 +0000425 if( nArg!=3 ) return 1;
drhdaffd0e2001-04-11 14:28:42 +0000426 fprintf(p->out, "%s;\n", azArg[2]);
drh4c653a02000-06-07 01:27:47 +0000427 if( strcmp(azArg[1],"table")==0 ){
428 struct callback_data d2;
drhdaffd0e2001-04-11 14:28:42 +0000429 d2 = *p;
drh33048c02001-10-01 14:29:22 +0000430 d2.mode = MODE_Insert;
431 d2.zDestTable = 0;
432 set_table_name(&d2, azArg[0]);
drhdaffd0e2001-04-11 14:28:42 +0000433 sqlite_exec_printf(p->db,
drha18c5682000-10-08 22:20:57 +0000434 "SELECT * FROM '%q'",
435 callback, &d2, 0, azArg[0]
436 );
drh33048c02001-10-01 14:29:22 +0000437 set_table_name(&d2, 0);
drh4c653a02000-06-07 01:27:47 +0000438 }
drh4c653a02000-06-07 01:27:47 +0000439 return 0;
440}
441
442/*
drh75897232000-05-29 14:26:00 +0000443** Text of a help message
444*/
445static char zHelp[] =
drh4c653a02000-06-07 01:27:47 +0000446 ".dump ?TABLE? ... Dump the database in an text format\n"
drhdaffd0e2001-04-11 14:28:42 +0000447 ".echo ON|OFF Turn command echo on or off\n"
drh75897232000-05-29 14:26:00 +0000448 ".exit Exit this program\n"
449 ".explain Set output mode suitable for EXPLAIN\n"
450 ".header ON|OFF Turn display of headers on or off\n"
451 ".help Show this message\n"
452 ".indices TABLE Show names of all indices on TABLE\n"
drhe3710332000-09-29 13:30:53 +0000453 ".mode MODE Set mode to one of \"line\", \"column\", \n"
454 " \"insert\", \"list\", or \"html\"\n"
drhc08a4f12000-06-15 16:49:48 +0000455 ".mode insert TABLE Generate SQL insert statements for TABLE\n"
drh75897232000-05-29 14:26:00 +0000456 ".output FILENAME Send output to FILENAME\n"
457 ".output stdout Send output to the screen\n"
drhdaffd0e2001-04-11 14:28:42 +0000458 ".read FILENAME Execute SQL in FILENAME\n"
459 ".reindex ?TABLE? Rebuild indices\n"
460/* ".rename OLD NEW Change the name of a table or index\n" */
drh75897232000-05-29 14:26:00 +0000461 ".schema ?TABLE? Show the CREATE statements\n"
462 ".separator STRING Change separator string for \"list\" mode\n"
drha50da102000-08-08 20:19:09 +0000463 ".tables ?PATTERN? List names of tables matching a pattern\n"
drh2dfbbca2000-07-28 14:32:48 +0000464 ".timeout MS Try opening locked tables for MS milliseconds\n"
drh75897232000-05-29 14:26:00 +0000465 ".width NUM NUM ... Set column widths for \"column\" mode\n"
466;
467
drhdaffd0e2001-04-11 14:28:42 +0000468/* Forward reference */
469static void process_input(struct callback_data *p, FILE *in);
470
drh75897232000-05-29 14:26:00 +0000471/*
472** If an input line begins with "." then invoke this routine to
473** process that line.
474*/
475static void do_meta_command(char *zLine, sqlite *db, struct callback_data *p){
476 int i = 1;
477 int nArg = 0;
478 int n, c;
479 char *azArg[50];
480
481 /* Parse the input line into tokens.
482 */
483 while( zLine[i] && nArg<ArraySize(azArg) ){
484 while( isspace(zLine[i]) ){ i++; }
485 if( zLine[i]=='\'' || zLine[i]=='"' ){
486 int delim = zLine[i++];
487 azArg[nArg++] = &zLine[i];
488 while( zLine[i] && zLine[i]!=delim ){ i++; }
489 if( zLine[i]==delim ){
490 zLine[i++] = 0;
491 }
492 }else{
493 azArg[nArg++] = &zLine[i];
494 while( zLine[i] && !isspace(zLine[i]) ){ i++; }
495 if( zLine[i] ) zLine[i++] = 0;
496 }
497 }
498
499 /* Process the input line.
500 */
501 if( nArg==0 ) return;
502 n = strlen(azArg[0]);
503 c = azArg[0][0];
drh4c653a02000-06-07 01:27:47 +0000504 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
505 char *zErrMsg = 0;
drh33048c02001-10-01 14:29:22 +0000506 fprintf(p->out, "BEGIN TRANSACTION;\n");
drh4c653a02000-06-07 01:27:47 +0000507 if( nArg==1 ){
drha18c5682000-10-08 22:20:57 +0000508 sqlite_exec(db,
509 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000510 "WHERE type!='meta' AND sql NOT NULL "
drha18c5682000-10-08 22:20:57 +0000511 "ORDER BY tbl_name, type DESC, name",
512 dump_callback, p, &zErrMsg
513 );
drh4c653a02000-06-07 01:27:47 +0000514 }else{
515 int i;
516 for(i=1; i<nArg && zErrMsg==0; i++){
drha18c5682000-10-08 22:20:57 +0000517 sqlite_exec_printf(db,
518 "SELECT name, type, sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000519 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOT NULL "
drha18c5682000-10-08 22:20:57 +0000520 "ORDER BY type DESC, name",
521 dump_callback, p, &zErrMsg, azArg[i]
522 );
drh4c653a02000-06-07 01:27:47 +0000523
524 }
525 }
526 if( zErrMsg ){
527 fprintf(stderr,"Error: %s\n", zErrMsg);
528 free(zErrMsg);
drh33048c02001-10-01 14:29:22 +0000529 }else{
530 fprintf(p->out, "COMMIT;\n");
drh4c653a02000-06-07 01:27:47 +0000531 }
532 }else
drh75897232000-05-29 14:26:00 +0000533
drhdaffd0e2001-04-11 14:28:42 +0000534 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){
535 int j;
536 char *z = azArg[1];
537 int val = atoi(azArg[1]);
538 for(j=0; z[j]; j++){
539 if( isupper(z[j]) ) z[j] = tolower(z[j]);
540 }
541 if( strcmp(z,"on")==0 ){
542 val = 1;
543 }else if( strcmp(z,"yes")==0 ){
544 val = 1;
545 }
546 p->echoOn = val;
547 }else
548
drh75897232000-05-29 14:26:00 +0000549 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
drh04096482001-11-09 22:41:44 +0000550 sqlite_close(db);
drh75897232000-05-29 14:26:00 +0000551 exit(0);
552 }else
553
554 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
555 p->mode = MODE_Column;
556 p->showHeader = 1;
557 p->colWidth[0] = 4;
558 p->colWidth[1] = 12;
559 p->colWidth[2] = 5;
560 p->colWidth[3] = 5;
561 p->colWidth[4] = 40;
562 }else
563
564 if( c=='h' && strncmp(azArg[0], "header", n)==0 && nArg>1 ){
565 int j;
566 char *z = azArg[1];
567 int val = atoi(azArg[1]);
568 for(j=0; z[j]; j++){
569 if( isupper(z[j]) ) z[j] = tolower(z[j]);
570 }
571 if( strcmp(z,"on")==0 ){
572 val = 1;
573 }else if( strcmp(z,"yes")==0 ){
574 val = 1;
575 }
576 p->showHeader = val;
577 }else
578
579 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
580 fprintf(stderr,zHelp);
581 }else
582
583 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
584 struct callback_data data;
585 char *zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000586 memcpy(&data, p, sizeof(data));
587 data.showHeader = 0;
588 data.mode = MODE_List;
drha18c5682000-10-08 22:20:57 +0000589 sqlite_exec_printf(db,
590 "SELECT name FROM sqlite_master "
591 "WHERE type='index' AND tbl_name LIKE '%q' "
592 "ORDER BY name",
593 callback, &data, &zErrMsg, azArg[1]
594 );
drh75897232000-05-29 14:26:00 +0000595 if( zErrMsg ){
596 fprintf(stderr,"Error: %s\n", zErrMsg);
597 free(zErrMsg);
598 }
599 }else
600
drh28bd4bc2000-06-15 15:57:22 +0000601 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
drh75897232000-05-29 14:26:00 +0000602 int n2 = strlen(azArg[1]);
603 if( strncmp(azArg[1],"line",n2)==0 ){
604 p->mode = MODE_Line;
605 }else if( strncmp(azArg[1],"column",n2)==0 ){
606 p->mode = MODE_Column;
607 }else if( strncmp(azArg[1],"list",n2)==0 ){
608 p->mode = MODE_List;
drh1e5d0e92000-05-31 23:33:17 +0000609 }else if( strncmp(azArg[1],"html",n2)==0 ){
610 p->mode = MODE_Html;
drh28bd4bc2000-06-15 15:57:22 +0000611 }else if( strncmp(azArg[1],"insert",n2)==0 ){
612 p->mode = MODE_Insert;
613 if( nArg>=3 ){
drh33048c02001-10-01 14:29:22 +0000614 set_table_name(p, azArg[2]);
drh28bd4bc2000-06-15 15:57:22 +0000615 }else{
drh33048c02001-10-01 14:29:22 +0000616 set_table_name(p, "table");
drh28bd4bc2000-06-15 15:57:22 +0000617 }
drhdaffd0e2001-04-11 14:28:42 +0000618 }else {
619 fprintf(stderr,"mode should be on of: column html insert line list\n");
drh75897232000-05-29 14:26:00 +0000620 }
621 }else
622
623 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
624 if( p->out!=stdout ){
625 fclose(p->out);
626 }
627 if( strcmp(azArg[1],"stdout")==0 ){
628 p->out = stdout;
629 }else{
630 p->out = fopen(azArg[1], "w");
631 if( p->out==0 ){
632 fprintf(stderr,"can't write to \"%s\"\n", azArg[1]);
633 p->out = stdout;
634 }
635 }
636 }else
637
drhdaffd0e2001-04-11 14:28:42 +0000638 if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
639 FILE *alt = fopen(azArg[1], "r");
640 if( alt==0 ){
641 fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
642 }else{
643 process_input(p, alt);
644 fclose(alt);
645 }
646 }else
647
648 if( c=='r' && strncmp(azArg[0], "reindex", n)==0 ){
649 char **azResult;
650 int nRow, rc;
651 char *zErrMsg;
652 int i;
653 char *zSql;
654 if( nArg==1 ){
655 rc = sqlite_get_table(db,
656 "SELECT name, sql FROM sqlite_master "
657 "WHERE type='index'",
658 &azResult, &nRow, 0, &zErrMsg
659 );
660 }else{
661 rc = sqlite_get_table_printf(db,
662 "SELECT name, sql FROM sqlite_master "
663 "WHERE type='index' AND tbl_name LIKE '%q'",
664 &azResult, &nRow, 0, &zErrMsg, azArg[1]
665 );
666 }
667 for(i=1; rc==SQLITE_OK && i<=nRow; i++){
668 extern char *sqlite_mprintf(const char *, ...);
669 zSql = sqlite_mprintf(
670 "DROP INDEX '%q';\n%s;\nVACUUM '%q';",
671 azResult[i*2], azResult[i*2+1], azResult[i*2]);
672 if( p->echoOn ) printf("%s\n", zSql);
673 rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
674 }
675 sqlite_free_table(azResult);
676 if( zErrMsg ){
677 fprintf(stderr,"Error: %s\n", zErrMsg);
678 free(zErrMsg);
679 }
680 }else
681
drh75897232000-05-29 14:26:00 +0000682 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
683 struct callback_data data;
684 char *zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000685 memcpy(&data, p, sizeof(data));
686 data.showHeader = 0;
drhe3710332000-09-29 13:30:53 +0000687 data.mode = MODE_Semi;
drh75897232000-05-29 14:26:00 +0000688 if( nArg>1 ){
drhff9821a2001-04-04 21:22:14 +0000689 extern int sqliteStrICmp(const char*,const char*);
drha18c5682000-10-08 22:20:57 +0000690 if( sqliteStrICmp(azArg[1],"sqlite_master")==0 ){
691 char *new_argv[2], *new_colv[2];
692 new_argv[0] = "CREATE TABLE sqlite_master (\n"
693 " type text,\n"
694 " name text,\n"
695 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000696 " rootpage integer,\n"
drha18c5682000-10-08 22:20:57 +0000697 " sql text\n"
698 ")";
699 new_argv[1] = 0;
700 new_colv[0] = "sql";
701 new_colv[1] = 0;
702 callback(&data, 1, new_argv, new_colv);
703 }else{
704 sqlite_exec_printf(db,
705 "SELECT sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000706 "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL "
drha18c5682000-10-08 22:20:57 +0000707 "ORDER BY type DESC, name",
708 callback, &data, &zErrMsg, azArg[1]);
709 }
drh75897232000-05-29 14:26:00 +0000710 }else{
drha18c5682000-10-08 22:20:57 +0000711 sqlite_exec(db,
712 "SELECT sql FROM sqlite_master "
drh33048c02001-10-01 14:29:22 +0000713 "WHERE type!='meta' AND sql NOTNULL "
drha18c5682000-10-08 22:20:57 +0000714 "ORDER BY tbl_name, type DESC, name",
715 callback, &data, &zErrMsg
716 );
drh75897232000-05-29 14:26:00 +0000717 }
drh75897232000-05-29 14:26:00 +0000718 if( zErrMsg ){
719 fprintf(stderr,"Error: %s\n", zErrMsg);
720 free(zErrMsg);
721 }
722 }else
723
724 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
725 sprintf(p->separator, "%.*s", (int)ArraySize(p->separator)-1, azArg[1]);
726 }else
727
drh2dfbbca2000-07-28 14:32:48 +0000728 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
drhe3710332000-09-29 13:30:53 +0000729 char **azResult;
730 int nRow, rc;
731 char *zErrMsg;
drha50da102000-08-08 20:19:09 +0000732 if( nArg==1 ){
drha18c5682000-10-08 22:20:57 +0000733 rc = sqlite_get_table(db,
drha50da102000-08-08 20:19:09 +0000734 "SELECT name FROM sqlite_master "
735 "WHERE type='table' "
drha18c5682000-10-08 22:20:57 +0000736 "ORDER BY name",
737 &azResult, &nRow, 0, &zErrMsg
738 );
drha50da102000-08-08 20:19:09 +0000739 }else{
drha18c5682000-10-08 22:20:57 +0000740 rc = sqlite_get_table_printf(db,
drha50da102000-08-08 20:19:09 +0000741 "SELECT name FROM sqlite_master "
drha18c5682000-10-08 22:20:57 +0000742 "WHERE type='table' AND name LIKE '%%%q%%' "
743 "ORDER BY name",
744 &azResult, &nRow, 0, &zErrMsg, azArg[1]
745 );
drha50da102000-08-08 20:19:09 +0000746 }
drh75897232000-05-29 14:26:00 +0000747 if( zErrMsg ){
748 fprintf(stderr,"Error: %s\n", zErrMsg);
749 free(zErrMsg);
750 }
drhe3710332000-09-29 13:30:53 +0000751 if( rc==SQLITE_OK ){
752 int len, maxlen = 0;
753 int i, j;
754 int nPrintCol, nPrintRow;
755 for(i=1; i<=nRow; i++){
756 if( azResult[i]==0 ) continue;
757 len = strlen(azResult[i]);
758 if( len>maxlen ) maxlen = len;
759 }
760 nPrintCol = 80/(maxlen+2);
761 if( nPrintCol<1 ) nPrintCol = 1;
762 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
763 for(i=0; i<nPrintRow; i++){
764 for(j=i+1; j<=nRow; j+=nPrintRow){
765 char *zSp = j<=nPrintRow ? "" : " ";
766 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
767 }
768 printf("\n");
769 }
770 }
771 sqlite_free_table(azResult);
drh75897232000-05-29 14:26:00 +0000772 }else
773
drh2dfbbca2000-07-28 14:32:48 +0000774 if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
775 sqlite_busy_timeout(db, atoi(azArg[1]));
776 }else
777
drh75897232000-05-29 14:26:00 +0000778 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
779 int j;
780 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
781 p->colWidth[j-1] = atoi(azArg[j]);
782 }
783 }else
784
785 {
786 fprintf(stderr, "unknown command: \"%s\". Enter \".help\" for help\n",
787 azArg[0]);
788 }
789}
790
drhdaffd0e2001-04-11 14:28:42 +0000791static char *Argv0;
792static void process_input(struct callback_data *p, FILE *in){
793 char *zLine;
794 char *zSql = 0;
795 int nSql = 0;
796 char *zErrMsg;
797 while( (zLine = one_input_line(zSql, in))!=0 ){
798 if( p->echoOn ) printf("%s\n", zLine);
799 if( zLine && zLine[0]=='.' ){
800 do_meta_command(zLine, db, p);
801 free(zLine);
802 continue;
803 }
804 if( zSql==0 ){
805 int i;
806 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
807 if( zLine[i]!=0 ){
808 nSql = strlen(zLine);
809 zSql = malloc( nSql+1 );
810 strcpy(zSql, zLine);
811 }
812 }else{
813 int len = strlen(zLine);
814 zSql = realloc( zSql, nSql + len + 2 );
815 if( zSql==0 ){
816 fprintf(stderr,"%s: out of memory!\n", Argv0);
817 exit(1);
818 }
819 strcpy(&zSql[nSql++], "\n");
820 strcpy(&zSql[nSql], zLine);
821 nSql += len;
822 }
823 free(zLine);
824 if( zSql && sqlite_complete(zSql) ){
825 p->cnt = 0;
826 if( sqlite_exec(db, zSql, callback, p, &zErrMsg)!=0
827 && zErrMsg!=0 ){
828 if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
829 printf("SQL error: %s\n", zErrMsg);
830 free(zErrMsg);
831 zErrMsg = 0;
832 }
833 free(zSql);
834 zSql = 0;
835 nSql = 0;
836 }
837 }
838 if( zSql ){
839 printf("Incomplete SQL: %s\n", zSql);
840 free(zSql);
841 }
842}
843
drh75897232000-05-29 14:26:00 +0000844int main(int argc, char **argv){
drh75897232000-05-29 14:26:00 +0000845 char *zErrMsg = 0;
846 struct callback_data data;
847
drhdaffd0e2001-04-11 14:28:42 +0000848 Argv0 = argv[0];
drh1e5d0e92000-05-31 23:33:17 +0000849 memset(&data, 0, sizeof(data));
850 data.mode = MODE_List;
851 strcpy(data.separator,"|");
852 data.showHeader = 0;
drh4c504392000-10-16 22:06:40 +0000853#ifdef SIGINT
854 signal(SIGINT, interrupt_handler);
855#endif
drh1e5d0e92000-05-31 23:33:17 +0000856 while( argc>=2 && argv[1][0]=='-' ){
857 if( strcmp(argv[1],"-html")==0 ){
858 data.mode = MODE_Html;
859 argc--;
860 argv++;
861 }else if( strcmp(argv[1],"-list")==0 ){
862 data.mode = MODE_List;
863 argc--;
864 argv++;
865 }else if( strcmp(argv[1],"-line")==0 ){
866 data.mode = MODE_Line;
867 argc--;
868 argv++;
869 }else if( argc>=3 && strcmp(argv[0],"-separator")==0 ){
drhbed86902000-06-02 13:27:59 +0000870 sprintf(data.separator,"%.*s",(int)sizeof(data.separator)-1,argv[2]);
drh1e5d0e92000-05-31 23:33:17 +0000871 argc -= 2;
872 argv += 2;
873 }else if( strcmp(argv[1],"-header")==0 ){
874 data.showHeader = 1;
875 argc--;
876 argv++;
877 }else if( strcmp(argv[1],"-noheader")==0 ){
878 data.showHeader = 0;
879 argc--;
880 argv++;
drh660f68d2001-01-04 14:27:07 +0000881 }else if( strcmp(argv[1],"-echo")==0 ){
drhdaffd0e2001-04-11 14:28:42 +0000882 data.echoOn = 1;
drh660f68d2001-01-04 14:27:07 +0000883 argc--;
884 argv++;
drh1e5d0e92000-05-31 23:33:17 +0000885 }else{
drhdaffd0e2001-04-11 14:28:42 +0000886 fprintf(stderr,"%s: unknown option: %s\n", Argv0, argv[1]);
drh1e5d0e92000-05-31 23:33:17 +0000887 return 1;
888 }
889 }
drh75897232000-05-29 14:26:00 +0000890 if( argc!=2 && argc!=3 ){
drhdaffd0e2001-04-11 14:28:42 +0000891 fprintf(stderr,"Usage: %s ?OPTIONS? FILENAME ?SQL?\n", Argv0);
drh75897232000-05-29 14:26:00 +0000892 exit(1);
893 }
drh4c653a02000-06-07 01:27:47 +0000894 data.db = db = sqlite_open(argv[1], 0666, &zErrMsg);
drh75897232000-05-29 14:26:00 +0000895 if( db==0 ){
drh167a4b12000-08-17 09:49:59 +0000896 data.db = db = sqlite_open(argv[1], 0444, &zErrMsg);
897 if( db==0 ){
898 if( zErrMsg ){
899 fprintf(stderr,"Unable to open database \"%s\": %s\n", argv[1],zErrMsg);
900 }else{
901 fprintf(stderr,"Unable to open database %s\n", argv[1]);
902 }
903 exit(1);
drhd1dedb82000-06-05 02:07:04 +0000904 }else{
drh80afdca2000-08-22 13:27:22 +0000905 fprintf(stderr,"Database \"%s\" opened READ ONLY!\n", argv[1]);
drhd1dedb82000-06-05 02:07:04 +0000906 }
drh75897232000-05-29 14:26:00 +0000907 }
drh75897232000-05-29 14:26:00 +0000908 data.out = stdout;
909 if( argc==3 ){
drh75897232000-05-29 14:26:00 +0000910 if( sqlite_exec(db, argv[2], callback, &data, &zErrMsg)!=0 && zErrMsg!=0 ){
911 fprintf(stderr,"SQL error: %s\n", zErrMsg);
912 exit(1);
913 }
914 }else{
drh382c0242001-10-06 16:33:02 +0000915 extern int isatty();
drhdaffd0e2001-04-11 14:28:42 +0000916 if( isatty(0) ){
drh75897232000-05-29 14:26:00 +0000917 printf(
drhb217a572000-08-22 13:40:18 +0000918 "SQLite version %s\n"
919 "Enter \".help\" for instructions\n",
920 sqlite_version
drh75897232000-05-29 14:26:00 +0000921 );
drhdaffd0e2001-04-11 14:28:42 +0000922 process_input(&data, 0);
923 }else{
924 process_input(&data, stdin);
drh75897232000-05-29 14:26:00 +0000925 }
926 }
drh33048c02001-10-01 14:29:22 +0000927 set_table_name(&data, 0);
drh75897232000-05-29 14:26:00 +0000928 sqlite_close(db);
929 return 0;
930}