annotate sqlite/shell.c @ 1571:0b69bc0eb2d2 trunk

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