1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file contains code to implement the "sqlite" command line
13 ** utility for accessing SQLite databases.
14 */
15 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
16 /* This needs to come before any includes for MSVC compiler */
17 #define _CRT_SECURE_NO_WARNINGS
18 #endif
19
20 /*
21 ** Enable large-file support for fopen() and friends on unix.
22 */
23 #ifndef SQLITE_DISABLE_LFS
24 # define _LARGE_FILE 1
25 # ifndef _FILE_OFFSET_BITS
26 # define _FILE_OFFSET_BITS 64
27 # endif
28 # define _LARGEFILE_SOURCE 1
29 #endif
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <assert.h>
35 #include "sqlite3.h"
36
37 #ifndef SQLITE_CORE
38 #define SQLITE_CORE_DEFINED
39 #define SQLITE_CORE
40 #endif
41
42 #include "gpkg.h"
43
44 #ifdef SQLITE_CORE_DEFINED
45 #undef SQLITE_CORE_DEFINED
46 #undef SQLITE_CORE
47 #endif
48
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52 #include <ctype.h>
53 #include <stdarg.h>
54
55 #if !defined(_WIN32) && !defined(WIN32)
56 # include <signal.h>
57 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
58 # include <pwd.h>
59 # endif
60 # include <unistd.h>
61 # include <sys/types.h>
62 #endif
63
64 #if defined(HAVE_READLINE) && HAVE_READLINE!=0
65 # include <readline/readline.h>
66 # include <readline/history.h>
67 #else
68 # undef HAVE_READLINE
69 #endif
70 #if defined(HAVE_EDITLINE) && !defined(HAVE_READLINE)
71 # define HAVE_READLINE 1
72 # include <editline/readline.h>
73 #endif
74 #if !defined(HAVE_READLINE)
75 # define add_history(X)
76 # define read_history(X)
77 # define write_history(X)
78 # define stifle_history(X)
79 #endif
80
81 #if defined(_WIN32) || defined(WIN32)
82 # include <io.h>
83 #define isatty(h) _isatty(h)
84 #ifndef access
85 # define access(f,m) _access((f),(m))
86 #endif
87 #undef popen
88 #define popen _popen
89 #undef pclose
90 #define pclose _pclose
91 #else
92 /* Make sure isatty() has a prototype.
93 */
94 extern int isatty(int);
95
96 /* popen and pclose are not C89 functions and so are sometimes omitted from
97 ** the <stdio.h> header */
98 extern FILE *popen(const char*,const char*);
99 extern int pclose(FILE*);
100 #endif
101
102 #if defined(_WIN32_WCE)
103 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
104 * thus we always assume that we have a console. That can be
105 * overridden with the -batch command line option.
106 */
107 #define isatty(x) 1
108 #endif
109
110 /* ctype macros that work with signed characters */
111 #define IsSpace(X) isspace((unsigned char)X)
112 #define IsDigit(X) isdigit((unsigned char)X)
113 #define ToLower(X) (char)tolower((unsigned char)X)
114
115
116 /* True if the timer is enabled */
117 static int enableTimer = 0;
118
119 /* Return the current wall-clock time */
timeOfDay(void)120 static sqlite3_int64 timeOfDay(void){
121 static sqlite3_vfs *clockVfs = 0;
122 sqlite3_int64 t;
123 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
124 if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){
125 clockVfs->xCurrentTimeInt64(clockVfs, &t);
126 }else{
127 double r;
128 clockVfs->xCurrentTime(clockVfs, &r);
129 t = (sqlite3_int64)(r*86400000.0);
130 }
131 return t;
132 }
133
134 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WRS_KERNEL) \
135 && !defined(__minux)
136 #include <sys/time.h>
137 #include <sys/resource.h>
138
139 /* Saved resource information for the beginning of an operation */
140 static struct rusage sBegin; /* CPU time at start */
141 static sqlite3_int64 iBegin; /* Wall-clock time at start */
142
143 /*
144 ** Begin timing an operation
145 */
beginTimer(void)146 static void beginTimer(void){
147 if( enableTimer ){
148 getrusage(RUSAGE_SELF, &sBegin);
149 iBegin = timeOfDay();
150 }
151 }
152
153 /* Return the difference of two time_structs in seconds */
timeDiff(struct timeval * pStart,struct timeval * pEnd)154 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
155 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
156 (double)(pEnd->tv_sec - pStart->tv_sec);
157 }
158
159 /*
160 ** Print the timing results.
161 */
endTimer(void)162 static void endTimer(void){
163 if( enableTimer ){
164 struct rusage sEnd;
165 sqlite3_int64 iEnd = timeOfDay();
166 getrusage(RUSAGE_SELF, &sEnd);
167 printf("Run Time: real %.3f user %f sys %f\n",
168 (iEnd - iBegin)*0.001,
169 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
170 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
171 }
172 }
173
174 #define BEGIN_TIMER beginTimer()
175 #define END_TIMER endTimer()
176 #define HAS_TIMER 1
177
178 #elif (defined(_WIN32) || defined(WIN32))
179
180 #include <windows.h>
181
182 /* Saved resource information for the beginning of an operation */
183 static HANDLE hProcess;
184 static FILETIME ftKernelBegin;
185 static FILETIME ftUserBegin;
186 static sqlite3_int64 ftWallBegin;
187 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME);
188 static GETPROCTIMES getProcessTimesAddr = NULL;
189
190 /*
191 ** Check to see if we have timer support. Return 1 if necessary
192 ** support found (or found previously).
193 */
hasTimer(void)194 static int hasTimer(void){
195 if( getProcessTimesAddr ){
196 return 1;
197 } else {
198 /* GetProcessTimes() isn't supported in WIN95 and some other Windows versions.
199 ** See if the version we are running on has it, and if it does, save off
200 ** a pointer to it and the current process handle.
201 */
202 hProcess = GetCurrentProcess();
203 if( hProcess ){
204 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
205 if( NULL != hinstLib ){
206 getProcessTimesAddr = (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
207 if( NULL != getProcessTimesAddr ){
208 return 1;
209 }
210 FreeLibrary(hinstLib);
211 }
212 }
213 }
214 return 0;
215 }
216
217 /*
218 ** Begin timing an operation
219 */
beginTimer(void)220 static void beginTimer(void){
221 if( enableTimer && getProcessTimesAddr ){
222 FILETIME ftCreation, ftExit;
223 getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelBegin, &ftUserBegin);
224 ftWallBegin = timeOfDay();
225 }
226 }
227
228 /* Return the difference of two FILETIME structs in seconds */
timeDiff(FILETIME * pStart,FILETIME * pEnd)229 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
230 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
231 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
232 return (double) ((i64End - i64Start) / 10000000.0);
233 }
234
235 /*
236 ** Print the timing results.
237 */
endTimer(void)238 static void endTimer(void){
239 if( enableTimer && getProcessTimesAddr){
240 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
241 sqlite3_int64 ftWallEnd = timeOfDay();
242 getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelEnd, &ftUserEnd);
243 printf("Run Time: real %.3f user %f sys %f\n",
244 (ftWallEnd - ftWallBegin)*0.001,
245 timeDiff(&ftUserBegin, &ftUserEnd),
246 timeDiff(&ftKernelBegin, &ftKernelEnd));
247 }
248 }
249
250 #define BEGIN_TIMER beginTimer()
251 #define END_TIMER endTimer()
252 #define HAS_TIMER hasTimer()
253
254 #else
255 #define BEGIN_TIMER
256 #define END_TIMER
257 #define HAS_TIMER 0
258 #endif
259
260 /*
261 ** Used to prevent warnings about unused parameters
262 */
263 #define UNUSED_PARAMETER(x) (void)(x)
264
265 /*
266 ** If the following flag is set, then command execution stops
267 ** at an error if we are not interactive.
268 */
269 static int bail_on_error = 0;
270
271 /*
272 ** Threat stdin as an interactive input if the following variable
273 ** is true. Otherwise, assume stdin is connected to a file or pipe.
274 */
275 static int stdin_is_interactive = 1;
276
277 /*
278 ** The following is the open SQLite database. We make a pointer
279 ** to this database a static variable so that it can be accessed
280 ** by the SIGINT handler to interrupt database processing.
281 */
282 static sqlite3 *db = 0;
283
284 /*
285 ** True if an interrupt (Control-C) has been received.
286 */
287 static volatile int seenInterrupt = 0;
288
289 /*
290 ** This is the name of our program. It is set in main(), used
291 ** in a number of other places, mostly for error messages.
292 */
293 static char *Argv0;
294
295 /*
296 ** Prompt strings. Initialized in main. Settable with
297 ** .prompt main continue
298 */
299 static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
300 static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
301
302 /*
303 ** Write I/O traces to the following stream.
304 */
305 #ifdef SQLITE_ENABLE_IOTRACE
306 static FILE *iotrace = 0;
307 #endif
308
309 /*
310 ** This routine works like printf in that its first argument is a
311 ** format string and subsequent arguments are values to be substituted
312 ** in place of % fields. The result of formatting this string
313 ** is written to iotrace.
314 */
315 #ifdef SQLITE_ENABLE_IOTRACE
iotracePrintf(const char * zFormat,...)316 static void iotracePrintf(const char *zFormat, ...){
317 va_list ap;
318 char *z;
319 if( iotrace==0 ) return;
320 va_start(ap, zFormat);
321 z = sqlite3_vmprintf(zFormat, ap);
322 va_end(ap);
323 fprintf(iotrace, "%s", z);
324 sqlite3_free(z);
325 }
326 #endif
327
328
329 /*
330 ** Determines if a string is a number of not.
331 */
isNumber(const char * z,int * realnum)332 static int isNumber(const char *z, int *realnum){
333 if( *z=='-' || *z=='+' ) z++;
334 if( !IsDigit(*z) ){
335 return 0;
336 }
337 z++;
338 if( realnum ) *realnum = 0;
339 while( IsDigit(*z) ){ z++; }
340 if( *z=='.' ){
341 z++;
342 if( !IsDigit(*z) ) return 0;
343 while( IsDigit(*z) ){ z++; }
344 if( realnum ) *realnum = 1;
345 }
346 if( *z=='e' || *z=='E' ){
347 z++;
348 if( *z=='+' || *z=='-' ) z++;
349 if( !IsDigit(*z) ) return 0;
350 while( IsDigit(*z) ){ z++; }
351 if( realnum ) *realnum = 1;
352 }
353 return *z==0;
354 }
355
356 /*
357 ** A global char* and an SQL function to access its current value
358 ** from within an SQL statement. This program used to use the
359 ** sqlite_exec_printf() API to substitue a string into an SQL statement.
360 ** The correct way to do this with sqlite3 is to use the bind API, but
361 ** since the shell is built around the callback paradigm it would be a lot
362 ** of work. Instead just use this hack, which is quite harmless.
363 */
364 static const char *zShellStatic = 0;
shellstaticFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)365 static void shellstaticFunc(
366 sqlite3_context *context,
367 int argc,
368 sqlite3_value **argv
369 ){
370 assert( 0==argc );
371 assert( zShellStatic );
372 UNUSED_PARAMETER(argc);
373 UNUSED_PARAMETER(argv);
374 sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
375 }
376
377
378 /*
379 ** This routine reads a line of text from FILE in, stores
380 ** the text in memory obtained from malloc() and returns a pointer
381 ** to the text. NULL is returned at end of file, or if malloc()
382 ** fails.
383 **
384 ** If zLine is not NULL then it is a malloced buffer returned from
385 ** a previous call to this routine that may be reused.
386 */
local_getline(char * zLine,FILE * in)387 static char *local_getline(char *zLine, FILE *in){
388 int nLine = zLine==0 ? 0 : 100;
389 int n = 0;
390
391 while( 1 ){
392 if( n+100>nLine ){
393 nLine = nLine*2 + 100;
394 zLine = realloc(zLine, nLine);
395 if( zLine==0 ) return 0;
396 }
397 if( fgets(&zLine[n], nLine - n, in)==0 ){
398 if( n==0 ){
399 free(zLine);
400 return 0;
401 }
402 zLine[n] = 0;
403 break;
404 }
405 while( zLine[n] ) n++;
406 if( n>0 && zLine[n-1]=='\n' ){
407 n--;
408 if( n>0 && zLine[n-1]=='\r' ) n--;
409 zLine[n] = 0;
410 break;
411 }
412 }
413 return zLine;
414 }
415
416 /*
417 ** Retrieve a single line of input text.
418 **
419 ** If in==0 then read from standard input and prompt before each line.
420 ** If isContinuation is true, then a continuation prompt is appropriate.
421 ** If isContinuation is zero, then the main prompt should be used.
422 **
423 ** If zPrior is not NULL then it is a buffer from a prior call to this
424 ** routine that can be reused.
425 **
426 ** The result is stored in space obtained from malloc() and must either
427 ** be freed by the caller or else passed back into this routine via the
428 ** zPrior argument for reuse.
429 */
one_input_line(FILE * in,char * zPrior,int isContinuation)430 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
431 char *zPrompt;
432 char *zResult;
433 if( in!=0 ){
434 zResult = local_getline(zPrior, in);
435 }else{
436 zPrompt = isContinuation ? continuePrompt : mainPrompt;
437 #if defined(HAVE_READLINE)
438 free(zPrior);
439 zResult = readline(zPrompt);
440 if( zResult && *zResult ) add_history(zResult);
441 #else
442 printf("%s", zPrompt);
443 fflush(stdout);
444 zResult = local_getline(zPrior, stdin);
445 #endif
446 }
447 return zResult;
448 }
449
450 struct previous_mode_data {
451 int valid; /* Is there legit data in here? */
452 int mode;
453 int showHeader;
454 int colWidth[100];
455 };
456
457 /*
458 ** An pointer to an instance of this structure is passed from
459 ** the main program to the callback. This is used to communicate
460 ** state and mode information.
461 */
462 struct callback_data {
463 sqlite3 *db; /* The database */
464 int echoOn; /* True to echo input commands */
465 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
466 int statsOn; /* True to display memory stats before each finalize */
467 int outCount; /* Revert to stdout when reaching zero */
468 int cnt; /* Number of records displayed so far */
469 FILE *out; /* Write results here */
470 FILE *traceOut; /* Output for sqlite3_trace() */
471 int nErr; /* Number of errors seen */
472 int mode; /* An output mode setting */
473 int writableSchema; /* True if PRAGMA writable_schema=ON */
474 int showHeader; /* True to show column names in List or Column mode */
475 char *zDestTable; /* Name of destination table when MODE_Insert */
476 char separator[20]; /* Separator character for MODE_List */
477 int colWidth[100]; /* Requested width of each column when in column mode*/
478 int actualWidth[100]; /* Actual width of each column */
479 char nullvalue[20]; /* The text to print when a NULL comes back from
480 ** the database */
481 struct previous_mode_data explainPrev;
482 /* Holds the mode information just before
483 ** .explain ON */
484 char outfile[FILENAME_MAX]; /* Filename for *out */
485 const char *zDbFilename; /* name of the database file */
486 char *zFreeOnClose; /* Filename to free when closing */
487 const char *zVfs; /* Name of VFS to use */
488 sqlite3_stmt *pStmt; /* Current statement if any. */
489 FILE *pLog; /* Write log output here */
490 int (*gpkgEntryPoint)(sqlite3 *db, const char **pzErrMsg, const sqlite3_api_routines *pThunk);
491 int *aiIndent; /* Array of indents used in MODE_Explain */
492 int nIndent; /* Size of array aiIndent[] */
493 int iIndent; /* Index of current op in aiIndent[] */
494 };
495
496 /*
497 ** These are the allowed modes.
498 */
499 #define MODE_Line 0 /* One column per line. Blank line between records */
500 #define MODE_Column 1 /* One record per line in neat columns */
501 #define MODE_List 2 /* One record per line with a separator */
502 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
503 #define MODE_Html 4 /* Generate an XHTML table */
504 #define MODE_Insert 5 /* Generate SQL "insert" statements */
505 #define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */
506 #define MODE_Csv 7 /* Quote strings, numbers are plain */
507 #define MODE_Explain 8 /* Like MODE_Column, but do not truncate data */
508
509 static const char *modeDescr[] = {
510 "line",
511 "column",
512 "list",
513 "semi",
514 "html",
515 "insert",
516 "tcl",
517 "csv",
518 "explain",
519 };
520
521 /*
522 ** Number of elements in an array
523 */
524 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
525
526 /*
527 ** Compute a string length that is limited to what can be stored in
528 ** lower 30 bits of a 32-bit signed integer.
529 */
strlen30(const char * z)530 static int strlen30(const char *z){
531 const char *z2 = z;
532 while( *z2 ){ z2++; }
533 return 0x3fffffff & (int)(z2 - z);
534 }
535
536 /*
537 ** A callback for the sqlite3_log() interface.
538 */
shellLog(void * pArg,int iErrCode,const char * zMsg)539 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
540 struct callback_data *p = (struct callback_data*)pArg;
541 if( p->pLog==0 ) return;
542 fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
543 fflush(p->pLog);
544 }
545
546 /*
547 ** Output the given string as a hex-encoded blob (eg. X'1234' )
548 */
output_hex_blob(FILE * out,const void * pBlob,int nBlob)549 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
550 int i;
551 char *zBlob = (char *)pBlob;
552 fprintf(out,"X'");
553 for(i=0; i<nBlob; i++){ fprintf(out,"%02x",zBlob[i]&0xff); }
554 fprintf(out,"'");
555 }
556
557 /*
558 ** Output the given string as a quoted string using SQL quoting conventions.
559 */
output_quoted_string(FILE * out,const char * z)560 static void output_quoted_string(FILE *out, const char *z){
561 int i;
562 int nSingle = 0;
563 for(i=0; z[i]; i++){
564 if( z[i]=='\'' ) nSingle++;
565 }
566 if( nSingle==0 ){
567 fprintf(out,"'%s'",z);
568 }else{
569 fprintf(out,"'");
570 while( *z ){
571 for(i=0; z[i] && z[i]!='\''; i++){}
572 if( i==0 ){
573 fprintf(out,"''");
574 z++;
575 }else if( z[i]=='\'' ){
576 fprintf(out,"%.*s''",i,z);
577 z += i+1;
578 }else{
579 fprintf(out,"%s",z);
580 break;
581 }
582 }
583 fprintf(out,"'");
584 }
585 }
586
587 /*
588 ** Output the given string as a quoted according to C or TCL quoting rules.
589 */
output_c_string(FILE * out,const char * z)590 static void output_c_string(FILE *out, const char *z){
591 unsigned int c;
592 fputc('"', out);
593 while( (c = *(z++))!=0 ){
594 if( c=='\\' ){
595 fputc(c, out);
596 fputc(c, out);
597 }else if( c=='"' ){
598 fputc('\\', out);
599 fputc('"', out);
600 }else if( c=='\t' ){
601 fputc('\\', out);
602 fputc('t', out);
603 }else if( c=='\n' ){
604 fputc('\\', out);
605 fputc('n', out);
606 }else if( c=='\r' ){
607 fputc('\\', out);
608 fputc('r', out);
609 }else if( !isprint(c&0xff) ){
610 fprintf(out, "\\%03o", c&0xff);
611 }else{
612 fputc(c, out);
613 }
614 }
615 fputc('"', out);
616 }
617
618 /*
619 ** Output the given string with characters that are special to
620 ** HTML escaped.
621 */
output_html_string(FILE * out,const char * z)622 static void output_html_string(FILE *out, const char *z){
623 int i;
624 if( z==0 ) z = "";
625 while( *z ){
626 for(i=0; z[i]
627 && z[i]!='<'
628 && z[i]!='&'
629 && z[i]!='>'
630 && z[i]!='\"'
631 && z[i]!='\'';
632 i++){}
633 if( i>0 ){
634 fprintf(out,"%.*s",i,z);
635 }
636 if( z[i]=='<' ){
637 fprintf(out,"<");
638 }else if( z[i]=='&' ){
639 fprintf(out,"&");
640 }else if( z[i]=='>' ){
641 fprintf(out,">");
642 }else if( z[i]=='\"' ){
643 fprintf(out,""");
644 }else if( z[i]=='\'' ){
645 fprintf(out,"'");
646 }else{
647 break;
648 }
649 z += i + 1;
650 }
651 }
652
653 /*
654 ** If a field contains any character identified by a 1 in the following
655 ** array, then the string must be quoted for CSV.
656 */
657 static const char needCsvQuote[] = {
658 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
659 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
660 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
661 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
662 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
663 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
664 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
665 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
666 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
667 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
668 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
669 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
670 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
671 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
672 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
673 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
674 };
675
676 /*
677 ** Output a single term of CSV. Actually, p->separator is used for
678 ** the separator, which may or may not be a comma. p->nullvalue is
679 ** the null value. Strings are quoted if necessary.
680 */
output_csv(struct callback_data * p,const char * z,int bSep)681 static void output_csv(struct callback_data *p, const char *z, int bSep){
682 FILE *out = p->out;
683 if( z==0 ){
684 fprintf(out,"%s",p->nullvalue);
685 }else{
686 int i;
687 int nSep = strlen30(p->separator);
688 for(i=0; z[i]; i++){
689 if( needCsvQuote[((unsigned char*)z)[i]]
690 || (z[i]==p->separator[0] &&
691 (nSep==1 || memcmp(z, p->separator, nSep)==0)) ){
692 i = 0;
693 break;
694 }
695 }
696 if( i==0 ){
697 putc('"', out);
698 for(i=0; z[i]; i++){
699 if( z[i]=='"' ) putc('"', out);
700 putc(z[i], out);
701 }
702 putc('"', out);
703 }else{
704 fprintf(out, "%s", z);
705 }
706 }
707 if( bSep ){
708 fprintf(p->out, "%s", p->separator);
709 }
710 }
711
712 #ifdef SIGINT
713 /*
714 ** This routine runs when the user presses Ctrl-C
715 */
interrupt_handler(int NotUsed)716 static void interrupt_handler(int NotUsed){
717 UNUSED_PARAMETER(NotUsed);
718 seenInterrupt++;
719 if( seenInterrupt>2 ) exit(1);
720 if( db ) sqlite3_interrupt(db);
721 }
722 #endif
723
724 /*
725 ** This is the callback routine that the shell
726 ** invokes for each row of a query result.
727 */
shell_callback(void * pArg,int nArg,char ** azArg,char ** azCol,int * aiType)728 static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
729 int i;
730 struct callback_data *p = (struct callback_data*)pArg;
731
732 switch( p->mode ){
733 case MODE_Line: {
734 int w = 5;
735 if( azArg==0 ) break;
736 for(i=0; i<nArg; i++){
737 int len = strlen30(azCol[i] ? azCol[i] : "");
738 if( len>w ) w = len;
739 }
740 if( p->cnt++>0 ) fprintf(p->out,"\n");
741 for(i=0; i<nArg; i++){
742 fprintf(p->out,"%*s = %s\n", w, azCol[i],
743 azArg[i] ? azArg[i] : p->nullvalue);
744 }
745 break;
746 }
747 case MODE_Explain:
748 case MODE_Column: {
749 if( p->cnt++==0 ){
750 for(i=0; i<nArg; i++){
751 int w, n;
752 if( i<ArraySize(p->colWidth) ){
753 w = p->colWidth[i];
754 }else{
755 w = 0;
756 }
757 if( w==0 ){
758 w = strlen30(azCol[i] ? azCol[i] : "");
759 if( w<10 ) w = 10;
760 n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue);
761 if( w<n ) w = n;
762 }
763 if( i<ArraySize(p->actualWidth) ){
764 p->actualWidth[i] = w;
765 }
766 if( p->showHeader ){
767 if( w<0 ){
768 fprintf(p->out,"%*.*s%s",-w,-w,azCol[i], i==nArg-1 ? "\n": " ");
769 }else{
770 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
771 }
772 }
773 }
774 if( p->showHeader ){
775 for(i=0; i<nArg; i++){
776 int w;
777 if( i<ArraySize(p->actualWidth) ){
778 w = p->actualWidth[i];
779 if( w<0 ) w = -w;
780 }else{
781 w = 10;
782 }
783 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
784 "----------------------------------------------------------",
785 i==nArg-1 ? "\n": " ");
786 }
787 }
788 }
789 if( azArg==0 ) break;
790 for(i=0; i<nArg; i++){
791 int w;
792 if( i<ArraySize(p->actualWidth) ){
793 w = p->actualWidth[i];
794 }else{
795 w = 10;
796 }
797 if( p->mode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){
798 w = strlen30(azArg[i]);
799 }
800 if( i==1 && p->aiIndent && p->pStmt ){
801 if( p->iIndent<p->nIndent ){
802 fprintf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
803 }
804 p->iIndent++;
805 }
806 if( w<0 ){
807 fprintf(p->out,"%*.*s%s",-w,-w,
808 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
809 }else{
810 fprintf(p->out,"%-*.*s%s",w,w,
811 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
812 }
813 }
814 break;
815 }
816 case MODE_Semi:
817 case MODE_List: {
818 if( p->cnt++==0 && p->showHeader ){
819 for(i=0; i<nArg; i++){
820 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
821 }
822 }
823 if( azArg==0 ) break;
824 for(i=0; i<nArg; i++){
825 char *z = azArg[i];
826 if( z==0 ) z = p->nullvalue;
827 fprintf(p->out, "%s", z);
828 if( i<nArg-1 ){
829 fprintf(p->out, "%s", p->separator);
830 }else if( p->mode==MODE_Semi ){
831 fprintf(p->out, ";\n");
832 }else{
833 fprintf(p->out, "\n");
834 }
835 }
836 break;
837 }
838 case MODE_Html: {
839 if( p->cnt++==0 && p->showHeader ){
840 fprintf(p->out,"<TR>");
841 for(i=0; i<nArg; i++){
842 fprintf(p->out,"<TH>");
843 output_html_string(p->out, azCol[i]);
844 fprintf(p->out,"</TH>\n");
845 }
846 fprintf(p->out,"</TR>\n");
847 }
848 if( azArg==0 ) break;
849 fprintf(p->out,"<TR>");
850 for(i=0; i<nArg; i++){
851 fprintf(p->out,"<TD>");
852 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
853 fprintf(p->out,"</TD>\n");
854 }
855 fprintf(p->out,"</TR>\n");
856 break;
857 }
858 case MODE_Tcl: {
859 if( p->cnt++==0 && p->showHeader ){
860 for(i=0; i<nArg; i++){
861 output_c_string(p->out,azCol[i] ? azCol[i] : "");
862 if(i<nArg-1) fprintf(p->out, "%s", p->separator);
863 }
864 fprintf(p->out,"\n");
865 }
866 if( azArg==0 ) break;
867 for(i=0; i<nArg; i++){
868 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
869 if(i<nArg-1) fprintf(p->out, "%s", p->separator);
870 }
871 fprintf(p->out,"\n");
872 break;
873 }
874 case MODE_Csv: {
875 if( p->cnt++==0 && p->showHeader ){
876 for(i=0; i<nArg; i++){
877 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
878 }
879 fprintf(p->out,"\n");
880 }
881 if( azArg==0 ) break;
882 for(i=0; i<nArg; i++){
883 output_csv(p, azArg[i], i<nArg-1);
884 }
885 fprintf(p->out,"\n");
886 break;
887 }
888 case MODE_Insert: {
889 p->cnt++;
890 if( azArg==0 ) break;
891 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
892 for(i=0; i<nArg; i++){
893 char *zSep = i>0 ? ",": "";
894 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
895 fprintf(p->out,"%sNULL",zSep);
896 }else if( aiType && aiType[i]==SQLITE_TEXT ){
897 if( zSep[0] ) fprintf(p->out,"%s",zSep);
898 output_quoted_string(p->out, azArg[i]);
899 }else if( aiType && (aiType[i]==SQLITE_INTEGER
900 || aiType[i]==SQLITE_FLOAT) ){
901 fprintf(p->out,"%s%s",zSep, azArg[i]);
902 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
903 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
904 int nBlob = sqlite3_column_bytes(p->pStmt, i);
905 if( zSep[0] ) fprintf(p->out,"%s",zSep);
906 output_hex_blob(p->out, pBlob, nBlob);
907 }else if( isNumber(azArg[i], 0) ){
908 fprintf(p->out,"%s%s",zSep, azArg[i]);
909 }else{
910 if( zSep[0] ) fprintf(p->out,"%s",zSep);
911 output_quoted_string(p->out, azArg[i]);
912 }
913 }
914 fprintf(p->out,");\n");
915 break;
916 }
917 }
918 return 0;
919 }
920
921 /*
922 ** This is the callback routine that the SQLite library
923 ** invokes for each row of a query result.
924 */
callback(void * pArg,int nArg,char ** azArg,char ** azCol)925 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
926 /* since we don't have type info, call the shell_callback with a NULL value */
927 return shell_callback(pArg, nArg, azArg, azCol, NULL);
928 }
929
930 /*
931 ** Set the destination table field of the callback_data structure to
932 ** the name of the table given. Escape any quote characters in the
933 ** table name.
934 */
set_table_name(struct callback_data * p,const char * zName)935 static void set_table_name(struct callback_data *p, const char *zName){
936 int i, n;
937 int needQuote;
938 char *z;
939
940 if( p->zDestTable ){
941 free(p->zDestTable);
942 p->zDestTable = 0;
943 }
944 if( zName==0 ) return;
945 needQuote = !isalpha((unsigned char)*zName) && *zName!='_';
946 for(i=n=0; zName[i]; i++, n++){
947 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){
948 needQuote = 1;
949 if( zName[i]=='\'' ) n++;
950 }
951 }
952 if( needQuote ) n += 2;
953 z = p->zDestTable = malloc( n+1 );
954 if( z==0 ){
955 fprintf(stderr,"Error: out of memory\n");
956 exit(1);
957 }
958 n = 0;
959 if( needQuote ) z[n++] = '\'';
960 for(i=0; zName[i]; i++){
961 z[n++] = zName[i];
962 if( zName[i]=='\'' ) z[n++] = '\'';
963 }
964 if( needQuote ) z[n++] = '\'';
965 z[n] = 0;
966 }
967
968 /* zIn is either a pointer to a NULL-terminated string in memory obtained
969 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
970 ** added to zIn, and the result returned in memory obtained from malloc().
971 ** zIn, if it was not NULL, is freed.
972 **
973 ** If the third argument, quote, is not '\0', then it is used as a
974 ** quote character for zAppend.
975 */
appendText(char * zIn,char const * zAppend,char quote)976 static char *appendText(char *zIn, char const *zAppend, char quote){
977 int len;
978 int i;
979 int nAppend = strlen30(zAppend);
980 int nIn = (zIn?strlen30(zIn):0);
981
982 len = nAppend+nIn+1;
983 if( quote ){
984 len += 2;
985 for(i=0; i<nAppend; i++){
986 if( zAppend[i]==quote ) len++;
987 }
988 }
989
990 zIn = (char *)realloc(zIn, len);
991 if( !zIn ){
992 return 0;
993 }
994
995 if( quote ){
996 char *zCsr = &zIn[nIn];
997 *zCsr++ = quote;
998 for(i=0; i<nAppend; i++){
999 *zCsr++ = zAppend[i];
1000 if( zAppend[i]==quote ) *zCsr++ = quote;
1001 }
1002 *zCsr++ = quote;
1003 *zCsr++ = '\0';
1004 assert( (zCsr-zIn)==len );
1005 }else{
1006 memcpy(&zIn[nIn], zAppend, nAppend);
1007 zIn[len-1] = '\0';
1008 }
1009
1010 return zIn;
1011 }
1012
1013
1014 /*
1015 ** Execute a query statement that will generate SQL output. Print
1016 ** the result columns, comma-separated, on a line and then add a
1017 ** semicolon terminator to the end of that line.
1018 **
1019 ** If the number of columns is 1 and that column contains text "--"
1020 ** then write the semicolon on a separate line. That way, if a
1021 ** "--" comment occurs at the end of the statement, the comment
1022 ** won't consume the semicolon terminator.
1023 */
run_table_dump_query(struct callback_data * p,const char * zSelect,const char * zFirstRow)1024 static int run_table_dump_query(
1025 struct callback_data *p, /* Query context */
1026 const char *zSelect, /* SELECT statement to extract content */
1027 const char *zFirstRow /* Print before first row, if not NULL */
1028 ){
1029 sqlite3_stmt *pSelect;
1030 int rc;
1031 int nResult;
1032 int i;
1033 const char *z;
1034 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
1035 if( rc!=SQLITE_OK || !pSelect ){
1036 fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
1037 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
1038 return rc;
1039 }
1040 rc = sqlite3_step(pSelect);
1041 nResult = sqlite3_column_count(pSelect);
1042 while( rc==SQLITE_ROW ){
1043 if( zFirstRow ){
1044 fprintf(p->out, "%s", zFirstRow);
1045 zFirstRow = 0;
1046 }
1047 z = (const char*)sqlite3_column_text(pSelect, 0);
1048 fprintf(p->out, "%s", z);
1049 for(i=1; i<nResult; i++){
1050 fprintf(p->out, ",%s", sqlite3_column_text(pSelect, i));
1051 }
1052 if( z==0 ) z = "";
1053 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
1054 if( z[0] ){
1055 fprintf(p->out, "\n;\n");
1056 }else{
1057 fprintf(p->out, ";\n");
1058 }
1059 rc = sqlite3_step(pSelect);
1060 }
1061 rc = sqlite3_finalize(pSelect);
1062 if( rc!=SQLITE_OK ){
1063 fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
1064 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
1065 }
1066 return rc;
1067 }
1068
1069 /*
1070 ** Allocate space and save off current error string.
1071 */
save_err_msg(sqlite3 * db)1072 static char *save_err_msg(
1073 sqlite3 *db /* Database to query */
1074 ){
1075 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
1076 char *zErrMsg = sqlite3_malloc(nErrMsg);
1077 if( zErrMsg ){
1078 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
1079 }
1080 return zErrMsg;
1081 }
1082
1083 /*
1084 ** Display memory stats.
1085 */
display_stats(sqlite3 * db,struct callback_data * pArg,int bReset)1086 static int display_stats(
1087 sqlite3 *db, /* Database to query */
1088 struct callback_data *pArg, /* Pointer to struct callback_data */
1089 int bReset /* True to reset the stats */
1090 ){
1091 int iCur;
1092 int iHiwtr;
1093
1094 if( pArg && pArg->out ){
1095
1096 iHiwtr = iCur = -1;
1097 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
1098 fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr);
1099 iHiwtr = iCur = -1;
1100 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
1101 fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", iCur, iHiwtr);
1102 /*
1103 ** Not currently used by the CLI.
1104 ** iHiwtr = iCur = -1;
1105 ** sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1106 ** fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
1107 */
1108 iHiwtr = iCur = -1;
1109 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
1110 fprintf(pArg->out, "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
1111 /*
1112 ** Not currently used by the CLI.
1113 ** iHiwtr = iCur = -1;
1114 ** sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1115 ** fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n", iCur, iHiwtr);
1116 */
1117 iHiwtr = iCur = -1;
1118 sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
1119 fprintf(pArg->out, "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
1120 iHiwtr = iCur = -1;
1121 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
1122 fprintf(pArg->out, "Largest Allocation: %d bytes\n", iHiwtr);
1123 iHiwtr = iCur = -1;
1124 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset);
1125 fprintf(pArg->out, "Largest Pcache Allocation: %d bytes\n", iHiwtr);
1126 iHiwtr = iCur = -1;
1127 sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset);
1128 fprintf(pArg->out, "Largest Scratch Allocation: %d bytes\n", iHiwtr);
1129 #ifdef YYTRACKMAXSTACKDEPTH
1130 iHiwtr = iCur = -1;
1131 sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset);
1132 fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", iCur, iHiwtr);
1133 #endif
1134 }
1135
1136 if( pArg && pArg->out && db ){
1137 iHiwtr = iCur = -1;
1138 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
1139 fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
1140 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
1141 fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
1142 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
1143 fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1144 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1145 fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
1146 iHiwtr = iCur = -1;
1147 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1148 fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur); iHiwtr = iCur = -1;
1149 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
1150 fprintf(pArg->out, "Page cache hits: %d\n", iCur);
1151 iHiwtr = iCur = -1;
1152 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
1153 fprintf(pArg->out, "Page cache misses: %d\n", iCur);
1154 iHiwtr = iCur = -1;
1155 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
1156 fprintf(pArg->out, "Page cache writes: %d\n", iCur);
1157 iHiwtr = iCur = -1;
1158 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
1159 fprintf(pArg->out, "Schema Heap Usage: %d bytes\n", iCur);
1160 iHiwtr = iCur = -1;
1161 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
1162 fprintf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", iCur);
1163 }
1164
1165 if( pArg && pArg->out && db && pArg->pStmt ){
1166 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, bReset);
1167 fprintf(pArg->out, "Fullscan Steps: %d\n", iCur);
1168 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
1169 fprintf(pArg->out, "Sort Operations: %d\n", iCur);
1170 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX, bReset);
1171 fprintf(pArg->out, "Autoindex Inserts: %d\n", iCur);
1172 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
1173 fprintf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
1174 }
1175
1176 return 0;
1177 }
1178
1179 /*
1180 ** Parameter azArray points to a zero-terminated array of strings. zStr
1181 ** points to a single nul-terminated string. Return non-zero if zStr
1182 ** is equal, according to strcmp(), to any of the strings in the array.
1183 ** Otherwise, return zero.
1184 */
str_in_array(const char * zStr,const char ** azArray)1185 static int str_in_array(const char *zStr, const char **azArray){
1186 int i;
1187 for(i=0; azArray[i]; i++){
1188 if( 0==strcmp(zStr, azArray[i]) ) return 1;
1189 }
1190 return 0;
1191 }
1192
1193 /*
1194 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
1195 ** and populate the callback_data.aiIndent[] array with the number of
1196 ** spaces each opcode should be indented before it is output.
1197 **
1198 ** The indenting rules are:
1199 **
1200 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
1201 ** all opcodes that occur between the p2 jump destination and the opcode
1202 ** itself by 2 spaces.
1203 **
1204 ** * For each "Goto", if the jump destination is earlier in the program
1205 ** and ends on one of:
1206 ** Yield SeekGt SeekLt RowSetRead Rewind
1207 ** or if the P1 parameter is one instead of zero,
1208 ** then indent all opcodes between the earlier instruction
1209 ** and "Goto" by 2 spaces.
1210 */
explain_data_prepare(struct callback_data * p,sqlite3_stmt * pSql)1211 static void explain_data_prepare(struct callback_data *p, sqlite3_stmt *pSql){
1212 const char *zSql; /* The text of the SQL statement */
1213 const char *z; /* Used to check if this is an EXPLAIN */
1214 int *abYield = 0; /* True if op is an OP_Yield */
1215 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
1216 int iOp; /* Index of operation in p->aiIndent[] */
1217
1218 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
1219 "NextIfOpen", "PrevIfOpen", 0 };
1220 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", "Rewind", 0 };
1221 const char *azGoto[] = { "Goto", 0 };
1222
1223 /* Try to figure out if this is really an EXPLAIN statement. If this
1224 ** cannot be verified, return early. */
1225 zSql = sqlite3_sql(pSql);
1226 if( zSql==0 ) return;
1227 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
1228 if( sqlite3_strnicmp(z, "explain", 7) ) return;
1229
1230 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
1231 int i;
1232 int iAddr = sqlite3_column_int(pSql, 0);
1233 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
1234
1235 /* Set p2 to the P2 field of the current opcode. Then, assuming that
1236 ** p2 is an instruction address, set variable p2op to the index of that
1237 ** instruction in the aiIndent[] array. p2 and p2op may be different if
1238 ** the current instruction is part of a sub-program generated by an
1239 ** SQL trigger or foreign key. */
1240 int p2 = sqlite3_column_int(pSql, 3);
1241 int p2op = (p2 + (iOp-iAddr));
1242
1243 /* Grow the p->aiIndent array as required */
1244 if( iOp>=nAlloc ){
1245 nAlloc += 100;
1246 p->aiIndent = (int*)sqlite3_realloc(p->aiIndent, nAlloc*sizeof(int));
1247 abYield = (int*)sqlite3_realloc(abYield, nAlloc*sizeof(int));
1248 }
1249 abYield[iOp] = str_in_array(zOp, azYield);
1250 p->aiIndent[iOp] = 0;
1251 p->nIndent = iOp+1;
1252
1253 if( str_in_array(zOp, azNext) ){
1254 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
1255 }
1256 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
1257 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
1258 ){
1259 for(i=p2op+1; i<iOp; i++) p->aiIndent[i] += 2;
1260 }
1261 }
1262
1263 p->iIndent = 0;
1264 sqlite3_free(abYield);
1265 sqlite3_reset(pSql);
1266 }
1267
1268 /*
1269 ** Free the array allocated by explain_data_prepare().
1270 */
explain_data_delete(struct callback_data * p)1271 static void explain_data_delete(struct callback_data *p){
1272 sqlite3_free(p->aiIndent);
1273 p->aiIndent = 0;
1274 p->nIndent = 0;
1275 p->iIndent = 0;
1276 }
1277
1278 /*
1279 ** Execute a statement or set of statements. Print
1280 ** any result rows/columns depending on the current mode
1281 ** set via the supplied callback.
1282 **
1283 ** This is very similar to SQLite's built-in sqlite3_exec()
1284 ** function except it takes a slightly different callback
1285 ** and callback data argument.
1286 */
shell_exec(sqlite3 * db,const char * zSql,int (* xCallback)(void *,int,char **,char **,int *),struct callback_data * pArg,char ** pzErrMsg)1287 static int shell_exec(
1288 sqlite3 *db, /* An open database */
1289 const char *zSql, /* SQL to be evaluated */
1290 int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
1291 /* (not the same as sqlite3_exec) */
1292 struct callback_data *pArg, /* Pointer to struct callback_data */
1293 char **pzErrMsg /* Error msg written here */
1294 ){
1295 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
1296 int rc = SQLITE_OK; /* Return Code */
1297 int rc2;
1298 const char *zLeftover; /* Tail of unprocessed SQL */
1299
1300 if( pzErrMsg ){
1301 *pzErrMsg = NULL;
1302 }
1303
1304 while( zSql[0] && (SQLITE_OK == rc) ){
1305 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
1306 if( SQLITE_OK != rc ){
1307 if( pzErrMsg ){
1308 *pzErrMsg = save_err_msg(db);
1309 }
1310 }else{
1311 if( !pStmt ){
1312 /* this happens for a comment or white-space */
1313 zSql = zLeftover;
1314 while( IsSpace(zSql[0]) ) zSql++;
1315 continue;
1316 }
1317
1318 /* save off the prepared statment handle and reset row count */
1319 if( pArg ){
1320 pArg->pStmt = pStmt;
1321 pArg->cnt = 0;
1322 }
1323
1324 /* echo the sql statement if echo on */
1325 if( pArg && pArg->echoOn ){
1326 const char *zStmtSql = sqlite3_sql(pStmt);
1327 fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
1328 }
1329
1330 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
1331 if( pArg && pArg->autoEQP ){
1332 sqlite3_stmt *pExplain;
1333 char *zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", sqlite3_sql(pStmt));
1334 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
1335 if( rc==SQLITE_OK ){
1336 while( sqlite3_step(pExplain)==SQLITE_ROW ){
1337 fprintf(pArg->out,"--EQP-- %d,", sqlite3_column_int(pExplain, 0));
1338 fprintf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
1339 fprintf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2));
1340 fprintf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
1341 }
1342 }
1343 sqlite3_finalize(pExplain);
1344 sqlite3_free(zEQP);
1345 }
1346
1347 /* Output TESTCTRL_EXPLAIN text of requested */
1348 if( pArg && pArg->mode==MODE_Explain ){
1349 const char *zExplain = 0;
1350 sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, pStmt, &zExplain);
1351 if( zExplain && zExplain[0] ){
1352 fprintf(pArg->out, "%s", zExplain);
1353 }
1354 }
1355
1356 /* If the shell is currently in ".explain" mode, gather the extra
1357 ** data required to add indents to the output.*/
1358 if( pArg && pArg->mode==MODE_Explain ){
1359 explain_data_prepare(pArg, pStmt);
1360 }
1361
1362 /* perform the first step. this will tell us if we
1363 ** have a result set or not and how wide it is.
1364 */
1365 rc = sqlite3_step(pStmt);
1366 /* if we have a result set... */
1367 if( SQLITE_ROW == rc ){
1368 /* if we have a callback... */
1369 if( xCallback ){
1370 /* allocate space for col name ptr, value ptr, and type */
1371 int nCol = sqlite3_column_count(pStmt);
1372 void *pData = sqlite3_malloc(3*nCol*sizeof(const char*) + 1);
1373 if( !pData ){
1374 rc = SQLITE_NOMEM;
1375 }else{
1376 char **azCols = (char **)pData; /* Names of result columns */
1377 char **azVals = &azCols[nCol]; /* Results */
1378 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
1379 int i, x;
1380 assert(sizeof(int) <= sizeof(char *));
1381 /* save off ptrs to column names */
1382 for(i=0; i<nCol; i++){
1383 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
1384 }
1385 do{
1386 /* extract the data and data types */
1387 for(i=0; i<nCol; i++){
1388 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
1389 if( x==SQLITE_BLOB && pArg && pArg->mode==MODE_Insert ){
1390 azVals[i] = "";
1391 }else{
1392 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
1393 }
1394 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
1395 rc = SQLITE_NOMEM;
1396 break; /* from for */
1397 }
1398 } /* end for */
1399
1400 /* if data and types extracted successfully... */
1401 if( SQLITE_ROW == rc ){
1402 /* call the supplied callback with the result row data */
1403 if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
1404 rc = SQLITE_ABORT;
1405 }else{
1406 rc = sqlite3_step(pStmt);
1407 }
1408 }
1409 } while( SQLITE_ROW == rc );
1410 sqlite3_free(pData);
1411 }
1412 }else{
1413 do{
1414 rc = sqlite3_step(pStmt);
1415 } while( rc == SQLITE_ROW );
1416 }
1417 }
1418
1419 explain_data_delete(pArg);
1420
1421 /* print usage stats if stats on */
1422 if( pArg && pArg->statsOn ){
1423 display_stats(db, pArg, 0);
1424 }
1425
1426 /* Finalize the statement just executed. If this fails, save a
1427 ** copy of the error message. Otherwise, set zSql to point to the
1428 ** next statement to execute. */
1429 rc2 = sqlite3_finalize(pStmt);
1430 if( rc!=SQLITE_NOMEM ) rc = rc2;
1431 if( rc==SQLITE_OK ){
1432 zSql = zLeftover;
1433 while( IsSpace(zSql[0]) ) zSql++;
1434 }else if( pzErrMsg ){
1435 *pzErrMsg = save_err_msg(db);
1436 }
1437
1438 /* clear saved stmt handle */
1439 if( pArg ){
1440 pArg->pStmt = NULL;
1441 }
1442 }
1443 } /* end while */
1444
1445 return rc;
1446 }
1447
1448
1449 /*
1450 ** This is a different callback routine used for dumping the database.
1451 ** Each row received by this callback consists of a table name,
1452 ** the table type ("index" or "table") and SQL to create the table.
1453 ** This routine should print text sufficient to recreate the table.
1454 */
dump_callback(void * pArg,int nArg,char ** azArg,char ** azCol)1455 static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
1456 int rc;
1457 const char *zTable;
1458 const char *zType;
1459 const char *zSql;
1460 const char *zPrepStmt = 0;
1461 struct callback_data *p = (struct callback_data *)pArg;
1462
1463 UNUSED_PARAMETER(azCol);
1464 if( nArg!=3 ) return 1;
1465 zTable = azArg[0];
1466 zType = azArg[1];
1467 zSql = azArg[2];
1468
1469 if( strcmp(zTable, "sqlite_sequence")==0 ){
1470 zPrepStmt = "DELETE FROM sqlite_sequence;\n";
1471 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
1472 fprintf(p->out, "ANALYZE sqlite_master;\n");
1473 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
1474 return 0;
1475 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
1476 char *zIns;
1477 if( !p->writableSchema ){
1478 fprintf(p->out, "PRAGMA writable_schema=ON;\n");
1479 p->writableSchema = 1;
1480 }
1481 zIns = sqlite3_mprintf(
1482 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
1483 "VALUES('table','%q','%q',0,'%q');",
1484 zTable, zTable, zSql);
1485 fprintf(p->out, "%s\n", zIns);
1486 sqlite3_free(zIns);
1487 return 0;
1488 }else{
1489 fprintf(p->out, "%s;\n", zSql);
1490 }
1491
1492 if( strcmp(zType, "table")==0 ){
1493 sqlite3_stmt *pTableInfo = 0;
1494 char *zSelect = 0;
1495 char *zTableInfo = 0;
1496 char *zTmp = 0;
1497 int nRow = 0;
1498
1499 zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
1500 zTableInfo = appendText(zTableInfo, zTable, '"');
1501 zTableInfo = appendText(zTableInfo, ");", 0);
1502
1503 rc = sqlite3_prepare_v2(p->db, zTableInfo, -1, &pTableInfo, 0);
1504 free(zTableInfo);
1505 if( rc!=SQLITE_OK || !pTableInfo ){
1506 return 1;
1507 }
1508
1509 zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
1510 /* Always quote the table name, even if it appears to be pure ascii,
1511 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
1512 zTmp = appendText(zTmp, zTable, '"');
1513 if( zTmp ){
1514 zSelect = appendText(zSelect, zTmp, '\'');
1515 free(zTmp);
1516 }
1517 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
1518 rc = sqlite3_step(pTableInfo);
1519 while( rc==SQLITE_ROW ){
1520 const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
1521 zSelect = appendText(zSelect, "quote(", 0);
1522 zSelect = appendText(zSelect, zText, '"');
1523 rc = sqlite3_step(pTableInfo);
1524 if( rc==SQLITE_ROW ){
1525 zSelect = appendText(zSelect, "), ", 0);
1526 }else{
1527 zSelect = appendText(zSelect, ") ", 0);
1528 }
1529 nRow++;
1530 }
1531 rc = sqlite3_finalize(pTableInfo);
1532 if( rc!=SQLITE_OK || nRow==0 ){
1533 free(zSelect);
1534 return 1;
1535 }
1536 zSelect = appendText(zSelect, "|| ')' FROM ", 0);
1537 zSelect = appendText(zSelect, zTable, '"');
1538
1539 rc = run_table_dump_query(p, zSelect, zPrepStmt);
1540 if( rc==SQLITE_CORRUPT ){
1541 zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
1542 run_table_dump_query(p, zSelect, 0);
1543 }
1544 free(zSelect);
1545 }
1546 return 0;
1547 }
1548
1549 /*
1550 ** Run zQuery. Use dump_callback() as the callback routine so that
1551 ** the contents of the query are output as SQL statements.
1552 **
1553 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
1554 ** "ORDER BY rowid DESC" to the end.
1555 */
run_schema_dump_query(struct callback_data * p,const char * zQuery)1556 static int run_schema_dump_query(
1557 struct callback_data *p,
1558 const char *zQuery
1559 ){
1560 int rc;
1561 char *zErr = 0;
1562 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
1563 if( rc==SQLITE_CORRUPT ){
1564 char *zQ2;
1565 int len = strlen30(zQuery);
1566 fprintf(p->out, "/****** CORRUPTION ERROR *******/\n");
1567 if( zErr ){
1568 fprintf(p->out, "/****** %s ******/\n", zErr);
1569 sqlite3_free(zErr);
1570 zErr = 0;
1571 }
1572 zQ2 = malloc( len+100 );
1573 if( zQ2==0 ) return rc;
1574 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
1575 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
1576 if( rc ){
1577 fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
1578 }else{
1579 rc = SQLITE_CORRUPT;
1580 }
1581 sqlite3_free(zErr);
1582 free(zQ2);
1583 }
1584 return rc;
1585 }
1586
1587 /*
1588 ** Text of a help message
1589 */
1590 static char zHelp[] =
1591 ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n"
1592 ".bail on|off Stop after hitting an error. Default OFF\n"
1593 ".clone NEWDB Clone data into NEWDB from the existing database\n"
1594 ".databases List names and files of attached databases\n"
1595 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
1596 " If TABLE specified, only dump tables matching\n"
1597 " LIKE pattern TABLE.\n"
1598 ".echo on|off Turn command echo on or off\n"
1599 ".exit Exit this program\n"
1600 ".explain ?on|off? Turn output mode suitable for EXPLAIN on or off.\n"
1601 " With no args, it turns EXPLAIN on.\n"
1602 ".headers on|off Turn display of headers on or off\n"
1603 ".help Show this message\n"
1604 ".import FILE TABLE Import data from FILE into TABLE\n"
1605 ".indices ?TABLE? Show names of all indices\n"
1606 " If TABLE specified, only show indices for tables\n"
1607 " matching LIKE pattern TABLE.\n"
1608 #ifdef SQLITE_ENABLE_IOTRACE
1609 ".iotrace FILE Enable I/O diagnostic logging to FILE\n"
1610 #endif
1611 #ifndef SQLITE_OMIT_LOAD_EXTENSION
1612 ".load FILE ?ENTRY? Load an extension library\n"
1613 #endif
1614 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n"
1615 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n"
1616 " csv Comma-separated values\n"
1617 " column Left-aligned columns. (See .width)\n"
1618 " html HTML <table> code\n"
1619 " insert SQL insert statements for TABLE\n"
1620 " line One value per line\n"
1621 " list Values delimited by .separator string\n"
1622 " tabs Tab-separated values\n"
1623 " tcl TCL list elements\n"
1624 ".nullvalue STRING Use STRING in place of NULL values\n"
1625 ".once FILENAME Output for the next SQL command only to FILENAME\n"
1626 ".open ?FILENAME? Close existing database and reopen FILENAME\n"
1627 ".output ?FILENAME? Send output to FILENAME or stdout\n"
1628 ".print STRING... Print literal STRING\n"
1629 ".prompt MAIN CONTINUE Replace the standard prompts\n"
1630 ".quit Exit this program\n"
1631 ".read FILENAME Execute SQL in FILENAME\n"
1632 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
1633 ".save FILE Write in-memory database into FILE\n"
1634 ".schema ?TABLE? Show the CREATE statements\n"
1635 " If TABLE specified, only show tables matching\n"
1636 " LIKE pattern TABLE.\n"
1637 ".separator STRING Change separator used by output mode and .import\n"
1638 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
1639 ".show Show the current values for various settings\n"
1640 ".stats on|off Turn stats on or off\n"
1641 ".system CMD ARGS... Run CMD ARGS... in a system shell\n"
1642 ".tables ?TABLE? List names of tables\n"
1643 " If TABLE specified, only list tables matching\n"
1644 " LIKE pattern TABLE.\n"
1645 ".timeout MS Try opening locked tables for MS milliseconds\n"
1646 ".timer on|off Turn SQL timer on or off\n"
1647 ".trace FILE|off Output each SQL statement as it is run\n"
1648 ".vfsname ?AUX? Print the name of the VFS stack\n"
1649 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
1650 " Negative values right-justify\n"
1651 ;
1652
1653 /* Forward reference */
1654 static int process_input(struct callback_data *p, FILE *in);
1655
update_prompt_callback(void * data,int nbCols,char ** colValues,char ** colNames)1656 static int update_prompt_callback(void *data, int nbCols, char** colValues, char** colNames) {
1657 char *spatialdbtype = colValues[0];
1658 size_t len = strlen(spatialdbtype);
1659
1660 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt, "%s> ", spatialdbtype);
1661 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"%*s> ", len, "...");
1662 return SQLITE_ABORT;
1663 }
1664
update_prompt(sqlite3 * db)1665 static void update_prompt(sqlite3 *db) {
1666 sqlite3_exec(db, "SELECT GPKG_SpatialDBType()", update_prompt_callback, NULL, NULL);
1667 }
1668
1669 /*
1670 ** Make sure the database is open. If it is not, then open it. If
1671 ** the database fails to open, print an error message and exit.
1672 */
open_db(struct callback_data * p,int keepAlive)1673 static void open_db(struct callback_data *p, int keepAlive){
1674 if( p->db==0 ){
1675 sqlite3_initialize();
1676 sqlite3_open(p->zDbFilename, &p->db);
1677 db = p->db;
1678 if( db && sqlite3_errcode(db)==SQLITE_OK ){
1679 sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
1680 shellstaticFunc, 0, 0);
1681 }
1682 if( db==0 || SQLITE_OK!=sqlite3_errcode(db) ){
1683 fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
1684 p->zDbFilename, sqlite3_errmsg(db));
1685 if( keepAlive ) return;
1686 exit(1);
1687 }
1688
1689 if (p->gpkgEntryPoint) {
1690 p->gpkgEntryPoint(db, NULL, NULL);
1691 }
1692
1693 #ifndef SQLITE_OMIT_LOAD_EXTENSION
1694 sqlite3_enable_load_extension(p->db, 1);
1695 #endif
1696 update_prompt(db);
1697 }
1698 }
1699
1700 /*
1701 ** Do C-language style dequoting.
1702 **
1703 ** \t -> tab
1704 ** \n -> newline
1705 ** \r -> carriage return
1706 ** \" -> "
1707 ** \NNN -> ascii character NNN in octal
1708 ** \\ -> backslash
1709 */
resolve_backslashes(char * z)1710 static void resolve_backslashes(char *z){
1711 int i, j;
1712 char c;
1713 while( *z && *z!='\\' ) z++;
1714 for(i=j=0; (c = z[i])!=0; i++, j++){
1715 if( c=='\\' ){
1716 c = z[++i];
1717 if( c=='n' ){
1718 c = '\n';
1719 }else if( c=='t' ){
1720 c = '\t';
1721 }else if( c=='r' ){
1722 c = '\r';
1723 }else if( c=='\\' ){
1724 c = '\\';
1725 }else if( c>='0' && c<='7' ){
1726 c -= '0';
1727 if( z[i+1]>='0' && z[i+1]<='7' ){
1728 i++;
1729 c = (c<<3) + z[i] - '0';
1730 if( z[i+1]>='0' && z[i+1]<='7' ){
1731 i++;
1732 c = (c<<3) + z[i] - '0';
1733 }
1734 }
1735 }
1736 }
1737 z[j] = c;
1738 }
1739 if( j<i ) z[j] = 0;
1740 }
1741
1742 /*
1743 ** Return the value of a hexadecimal digit. Return -1 if the input
1744 ** is not a hex digit.
1745 */
hexDigitValue(char c)1746 static int hexDigitValue(char c){
1747 if( c>='0' && c<='9' ) return c - '0';
1748 if( c>='a' && c<='f' ) return c - 'a' + 10;
1749 if( c>='A' && c<='F' ) return c - 'A' + 10;
1750 return -1;
1751 }
1752
1753 /*
1754 ** Interpret zArg as an integer value, possibly with suffixes.
1755 */
integerValue(const char * zArg)1756 static sqlite3_int64 integerValue(const char *zArg){
1757 sqlite3_int64 v = 0;
1758 static const struct { char *zSuffix; int iMult; } aMult[] = {
1759 { "KiB", 1024 },
1760 { "MiB", 1024*1024 },
1761 { "GiB", 1024*1024*1024 },
1762 { "KB", 1000 },
1763 { "MB", 1000000 },
1764 { "GB", 1000000000 },
1765 { "K", 1000 },
1766 { "M", 1000000 },
1767 { "G", 1000000000 },
1768 };
1769 int i;
1770 int isNeg = 0;
1771 if( zArg[0]=='-' ){
1772 isNeg = 1;
1773 zArg++;
1774 }else if( zArg[0]=='+' ){
1775 zArg++;
1776 }
1777 if( zArg[0]=='0' && zArg[1]=='x' ){
1778 int x;
1779 zArg += 2;
1780 while( (x = hexDigitValue(zArg[0]))>=0 ){
1781 v = (v<<4) + x;
1782 zArg++;
1783 }
1784 }else{
1785 while( IsDigit(zArg[0]) ){
1786 v = v*10 + zArg[0] - '0';
1787 zArg++;
1788 }
1789 }
1790 for(i=0; i<ArraySize(aMult); i++){
1791 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
1792 v *= aMult[i].iMult;
1793 break;
1794 }
1795 }
1796 return isNeg? -v : v;
1797 }
1798
1799 /*
1800 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
1801 ** for TRUE and FALSE. Return the integer value if appropriate.
1802 */
booleanValue(char * zArg)1803 static int booleanValue(char *zArg){
1804 int i;
1805 if( zArg[0]=='0' && zArg[1]=='x' ){
1806 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
1807 }else{
1808 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
1809 }
1810 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
1811 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
1812 return 1;
1813 }
1814 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
1815 return 0;
1816 }
1817 fprintf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
1818 zArg);
1819 return 0;
1820 }
1821
1822 /*
1823 ** Close an output file, assuming it is not stderr or stdout
1824 */
output_file_close(FILE * f)1825 static void output_file_close(FILE *f){
1826 if( f && f!=stdout && f!=stderr ) fclose(f);
1827 }
1828
1829 /*
1830 ** Try to open an output file. The names "stdout" and "stderr" are
1831 ** recognized and do the right thing. NULL is returned if the output
1832 ** filename is "off".
1833 */
output_file_open(const char * zFile)1834 static FILE *output_file_open(const char *zFile){
1835 FILE *f;
1836 if( strcmp(zFile,"stdout")==0 ){
1837 f = stdout;
1838 }else if( strcmp(zFile, "stderr")==0 ){
1839 f = stderr;
1840 }else if( strcmp(zFile, "off")==0 ){
1841 f = 0;
1842 }else{
1843 f = fopen(zFile, "wb");
1844 if( f==0 ){
1845 fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
1846 }
1847 }
1848 return f;
1849 }
1850
1851 /*
1852 ** A routine for handling output from sqlite3_trace().
1853 */
sql_trace_callback(void * pArg,const char * z)1854 static void sql_trace_callback(void *pArg, const char *z){
1855 FILE *f = (FILE*)pArg;
1856 if( f ) fprintf(f, "%s\n", z);
1857 }
1858
1859 /*
1860 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
1861 ** a useful spot to set a debugger breakpoint.
1862 */
test_breakpoint(void)1863 static void test_breakpoint(void){
1864 static int nCall = 0;
1865 nCall++;
1866 }
1867
1868 /*
1869 ** An object used to read a CSV file
1870 */
1871 typedef struct CSVReader CSVReader;
1872 struct CSVReader {
1873 const char *zFile; /* Name of the input file */
1874 FILE *in; /* Read the CSV text from this input stream */
1875 char *z; /* Accumulated text for a field */
1876 int n; /* Number of bytes in z */
1877 int nAlloc; /* Space allocated for z[] */
1878 int nLine; /* Current line number */
1879 int cTerm; /* Character that terminated the most recent field */
1880 int cSeparator; /* The separator character. (Usually ",") */
1881 };
1882
1883 /* Append a single byte to z[] */
csv_append_char(CSVReader * p,int c)1884 static void csv_append_char(CSVReader *p, int c){
1885 if( p->n+1>=p->nAlloc ){
1886 p->nAlloc += p->nAlloc + 100;
1887 p->z = sqlite3_realloc(p->z, p->nAlloc);
1888 if( p->z==0 ){
1889 fprintf(stderr, "out of memory\n");
1890 exit(1);
1891 }
1892 }
1893 p->z[p->n++] = (char)c;
1894 }
1895
1896 /* Read a single field of CSV text. Compatible with rfc4180 and extended
1897 ** with the option of having a separator other than ",".
1898 **
1899 ** + Input comes from p->in.
1900 ** + Store results in p->z of length p->n. Space to hold p->z comes
1901 ** from sqlite3_malloc().
1902 ** + Use p->cSep as the separator. The default is ",".
1903 ** + Keep track of the line number in p->nLine.
1904 ** + Store the character that terminates the field in p->cTerm. Store
1905 ** EOF on end-of-file.
1906 ** + Report syntax errors on stderr
1907 */
csv_read_one_field(CSVReader * p)1908 static char *csv_read_one_field(CSVReader *p){
1909 int c, pc, ppc;
1910 int cSep = p->cSeparator;
1911 p->n = 0;
1912 c = fgetc(p->in);
1913 if( c==EOF || seenInterrupt ){
1914 p->cTerm = EOF;
1915 return 0;
1916 }
1917 if( c=='"' ){
1918 int startLine = p->nLine;
1919 int cQuote = c;
1920 pc = ppc = 0;
1921 while( 1 ){
1922 c = fgetc(p->in);
1923 if( c=='\n' ) p->nLine++;
1924 if( c==cQuote ){
1925 if( pc==cQuote ){
1926 pc = 0;
1927 continue;
1928 }
1929 }
1930 if( (c==cSep && pc==cQuote)
1931 || (c=='\n' && pc==cQuote)
1932 || (c=='\n' && pc=='\r' && ppc==cQuote)
1933 || (c==EOF && pc==cQuote)
1934 ){
1935 do{ p->n--; }while( p->z[p->n]!=cQuote );
1936 p->cTerm = c;
1937 break;
1938 }
1939 if( pc==cQuote && c!='\r' ){
1940 fprintf(stderr, "%s:%d: unescaped %c character\n",
1941 p->zFile, p->nLine, cQuote);
1942 }
1943 if( c==EOF ){
1944 fprintf(stderr, "%s:%d: unterminated %c-quoted field\n",
1945 p->zFile, startLine, cQuote);
1946 p->cTerm = EOF;
1947 break;
1948 }
1949 csv_append_char(p, c);
1950 ppc = pc;
1951 pc = c;
1952 }
1953 }else{
1954 while( c!=EOF && c!=cSep && c!='\n' ){
1955 csv_append_char(p, c);
1956 c = fgetc(p->in);
1957 }
1958 if( c=='\n' ){
1959 p->nLine++;
1960 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
1961 }
1962 p->cTerm = c;
1963 }
1964 if( p->z ) p->z[p->n] = 0;
1965 return p->z;
1966 }
1967
1968 /*
1969 ** Try to transfer data for table zTable. If an error is seen while
1970 ** moving forward, try to go backwards. The backwards movement won't
1971 ** work for WITHOUT ROWID tables.
1972 */
tryToCloneData(struct callback_data * p,sqlite3 * newDb,const char * zTable)1973 static void tryToCloneData(
1974 struct callback_data *p,
1975 sqlite3 *newDb,
1976 const char *zTable
1977 ){
1978 sqlite3_stmt *pQuery = 0;
1979 sqlite3_stmt *pInsert = 0;
1980 char *zQuery = 0;
1981 char *zInsert = 0;
1982 int rc;
1983 int i, j, n;
1984 int nTable = (int)strlen(zTable);
1985 int k = 0;
1986 int cnt = 0;
1987 const int spinRate = 10000;
1988
1989 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
1990 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
1991 if( rc ){
1992 fprintf(stderr, "Error %d: %s on [%s]\n",
1993 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
1994 zQuery);
1995 goto end_data_xfer;
1996 }
1997 n = sqlite3_column_count(pQuery);
1998 zInsert = sqlite3_malloc(200 + nTable + n*3);
1999 if( zInsert==0 ){
2000 fprintf(stderr, "out of memory\n");
2001 goto end_data_xfer;
2002 }
2003 sqlite3_snprintf(200+nTable,zInsert,
2004 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
2005 i = (int)strlen(zInsert);
2006 for(j=1; j<n; j++){
2007 memcpy(zInsert+i, ",?", 2);
2008 i += 2;
2009 }
2010 memcpy(zInsert+i, ");", 3);
2011 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
2012 if( rc ){
2013 fprintf(stderr, "Error %d: %s on [%s]\n",
2014 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
2015 zQuery);
2016 goto end_data_xfer;
2017 }
2018 for(k=0; k<2; k++){
2019 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
2020 for(i=0; i<n; i++){
2021 switch( sqlite3_column_type(pQuery, i) ){
2022 case SQLITE_NULL: {
2023 sqlite3_bind_null(pInsert, i+1);
2024 break;
2025 }
2026 case SQLITE_INTEGER: {
2027 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
2028 break;
2029 }
2030 case SQLITE_FLOAT: {
2031 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
2032 break;
2033 }
2034 case SQLITE_TEXT: {
2035 sqlite3_bind_text(pInsert, i+1,
2036 (const char*)sqlite3_column_text(pQuery,i),
2037 -1, SQLITE_STATIC);
2038 break;
2039 }
2040 case SQLITE_BLOB: {
2041 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
2042 sqlite3_column_bytes(pQuery,i),
2043 SQLITE_STATIC);
2044 break;
2045 }
2046 }
2047 } /* End for */
2048 rc = sqlite3_step(pInsert);
2049 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
2050 fprintf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
2051 sqlite3_errmsg(newDb));
2052 }
2053 sqlite3_reset(pInsert);
2054 cnt++;
2055 if( (cnt%spinRate)==0 ){
2056 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
2057 fflush(stdout);
2058 }
2059 } /* End while */
2060 if( rc==SQLITE_DONE ) break;
2061 sqlite3_finalize(pQuery);
2062 sqlite3_free(zQuery);
2063 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
2064 zTable);
2065 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
2066 if( rc ){
2067 fprintf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
2068 break;
2069 }
2070 } /* End for(k=0...) */
2071
2072 end_data_xfer:
2073 sqlite3_finalize(pQuery);
2074 sqlite3_finalize(pInsert);
2075 sqlite3_free(zQuery);
2076 sqlite3_free(zInsert);
2077 }
2078
2079
2080 /*
2081 ** Try to transfer all rows of the schema that match zWhere. For
2082 ** each row, invoke xForEach() on the object defined by that row.
2083 ** If an error is encountered while moving forward through the
2084 ** sqlite_master table, try again moving backwards.
2085 */
tryToCloneSchema(struct callback_data * p,sqlite3 * newDb,const char * zWhere,void (* xForEach)(struct callback_data *,sqlite3 *,const char *))2086 static void tryToCloneSchema(
2087 struct callback_data *p,
2088 sqlite3 *newDb,
2089 const char *zWhere,
2090 void (*xForEach)(struct callback_data*,sqlite3*,const char*)
2091 ){
2092 sqlite3_stmt *pQuery = 0;
2093 char *zQuery = 0;
2094 int rc;
2095 const unsigned char *zName;
2096 const unsigned char *zSql;
2097 char *zErrMsg = 0;
2098
2099 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
2100 " WHERE %s", zWhere);
2101 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
2102 if( rc ){
2103 fprintf(stderr, "Error: (%d) %s on [%s]\n",
2104 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
2105 zQuery);
2106 goto end_schema_xfer;
2107 }
2108 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
2109 zName = sqlite3_column_text(pQuery, 0);
2110 zSql = sqlite3_column_text(pQuery, 1);
2111 printf("%s... ", zName); fflush(stdout);
2112 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
2113 if( zErrMsg ){
2114 fprintf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
2115 sqlite3_free(zErrMsg);
2116 zErrMsg = 0;
2117 }
2118 if( xForEach ){
2119 xForEach(p, newDb, (const char*)zName);
2120 }
2121 printf("done\n");
2122 }
2123 if( rc!=SQLITE_DONE ){
2124 sqlite3_finalize(pQuery);
2125 sqlite3_free(zQuery);
2126 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
2127 " WHERE %s ORDER BY rowid DESC", zWhere);
2128 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
2129 if( rc ){
2130 fprintf(stderr, "Error: (%d) %s on [%s]\n",
2131 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
2132 zQuery);
2133 goto end_schema_xfer;
2134 }
2135 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
2136 zName = sqlite3_column_text(pQuery, 0);
2137 zSql = sqlite3_column_text(pQuery, 1);
2138 printf("%s... ", zName); fflush(stdout);
2139 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
2140 if( zErrMsg ){
2141 fprintf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
2142 sqlite3_free(zErrMsg);
2143 zErrMsg = 0;
2144 }
2145 if( xForEach ){
2146 xForEach(p, newDb, (const char*)zName);
2147 }
2148 printf("done\n");
2149 }
2150 }
2151 end_schema_xfer:
2152 sqlite3_finalize(pQuery);
2153 sqlite3_free(zQuery);
2154 }
2155
2156 /*
2157 ** Open a new database file named "zNewDb". Try to recover as much information
2158 ** as possible out of the main database (which might be corrupt) and write it
2159 ** into zNewDb.
2160 */
tryToClone(struct callback_data * p,const char * zNewDb)2161 static void tryToClone(struct callback_data *p, const char *zNewDb){
2162 int rc;
2163 sqlite3 *newDb = 0;
2164 if( access(zNewDb,0)==0 ){
2165 fprintf(stderr, "File \"%s\" already exists.\n", zNewDb);
2166 return;
2167 }
2168 rc = sqlite3_open(zNewDb, &newDb);
2169 if( rc ){
2170 fprintf(stderr, "Cannot create output database: %s\n",
2171 sqlite3_errmsg(newDb));
2172 }else{
2173 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
2174 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
2175 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
2176 tryToCloneSchema(p, newDb, "type!='table'", 0);
2177 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
2178 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
2179 }
2180 sqlite3_close(newDb);
2181 }
2182
2183 /*
2184 ** Change the output file back to stdout
2185 */
output_reset(struct callback_data * p)2186 static void output_reset(struct callback_data *p){
2187 if( p->outfile[0]=='|' ){
2188 pclose(p->out);
2189 }else{
2190 output_file_close(p->out);
2191 }
2192 p->outfile[0] = 0;
2193 p->out = stdout;
2194 }
2195
2196 /*
2197 ** If an input line begins with "." then invoke this routine to
2198 ** process that line.
2199 **
2200 ** Return 1 on error, 2 to exit, and 0 otherwise.
2201 */
do_meta_command(char * zLine,struct callback_data * p)2202 static int do_meta_command(char *zLine, struct callback_data *p){
2203 int i = 1;
2204 int nArg = 0;
2205 int n, c;
2206 int rc = 0;
2207 char *azArg[50];
2208
2209 /* Parse the input line into tokens.
2210 */
2211 while( zLine[i] && nArg<ArraySize(azArg) ){
2212 while( IsSpace(zLine[i]) ){ i++; }
2213 if( zLine[i]==0 ) break;
2214 if( zLine[i]=='\'' || zLine[i]=='"' ){
2215 int delim = zLine[i++];
2216 azArg[nArg++] = &zLine[i];
2217 while( zLine[i] && zLine[i]!=delim ){
2218 if( zLine[i]=='\\' && delim=='"' && zLine[i+1]!=0 ) i++;
2219 i++;
2220 }
2221 if( zLine[i]==delim ){
2222 zLine[i++] = 0;
2223 }
2224 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
2225 }else{
2226 azArg[nArg++] = &zLine[i];
2227 while( zLine[i] && !IsSpace(zLine[i]) ){ i++; }
2228 if( zLine[i] ) zLine[i++] = 0;
2229 resolve_backslashes(azArg[nArg-1]);
2230 }
2231 }
2232
2233 /* Process the input line.
2234 */
2235 if( nArg==0 ) return 0; /* no tokens, no error */
2236 n = strlen30(azArg[0]);
2237 c = azArg[0][0];
2238 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
2239 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
2240 ){
2241 const char *zDestFile = 0;
2242 const char *zDb = 0;
2243 sqlite3 *pDest;
2244 sqlite3_backup *pBackup;
2245 int j;
2246 for(j=1; j<nArg; j++){
2247 const char *z = azArg[j];
2248 if( z[0]=='-' ){
2249 while( z[0]=='-' ) z++;
2250 /* No options to process at this time */
2251 {
2252 fprintf(stderr, "unknown option: %s\n", azArg[j]);
2253 return 1;
2254 }
2255 }else if( zDestFile==0 ){
2256 zDestFile = azArg[j];
2257 }else if( zDb==0 ){
2258 zDb = zDestFile;
2259 zDestFile = azArg[j];
2260 }else{
2261 fprintf(stderr, "too many arguments to .backup\n");
2262 return 1;
2263 }
2264 }
2265 if( zDestFile==0 ){
2266 fprintf(stderr, "missing FILENAME argument on .backup\n");
2267 return 1;
2268 }
2269 if( zDb==0 ) zDb = "main";
2270 rc = sqlite3_open(zDestFile, &pDest);
2271 if( rc!=SQLITE_OK ){
2272 fprintf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
2273 sqlite3_close(pDest);
2274 return 1;
2275 }
2276 open_db(p, 0);
2277 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
2278 if( pBackup==0 ){
2279 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
2280 sqlite3_close(pDest);
2281 return 1;
2282 }
2283 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
2284 sqlite3_backup_finish(pBackup);
2285 if( rc==SQLITE_DONE ){
2286 rc = 0;
2287 }else{
2288 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
2289 rc = 1;
2290 }
2291 sqlite3_close(pDest);
2292 }else
2293
2294 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
2295 if( nArg==2 ){
2296 bail_on_error = booleanValue(azArg[1]);
2297 }else{
2298 fprintf(stderr, "Usage: .bail on|off\n");
2299 rc = 1;
2300 }
2301 }else
2302
2303 /* The undocumented ".breakpoint" command causes a call to the no-op
2304 ** routine named test_breakpoint().
2305 */
2306 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
2307 test_breakpoint();
2308 }else
2309
2310 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
2311 if( nArg==2 ){
2312 tryToClone(p, azArg[1]);
2313 }else{
2314 fprintf(stderr, "Usage: .clone FILENAME\n");
2315 rc = 1;
2316 }
2317 }else
2318
2319 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
2320 struct callback_data data;
2321 char *zErrMsg = 0;
2322 open_db(p, 0);
2323 memcpy(&data, p, sizeof(data));
2324 data.showHeader = 1;
2325 data.mode = MODE_Column;
2326 data.colWidth[0] = 3;
2327 data.colWidth[1] = 15;
2328 data.colWidth[2] = 58;
2329 data.cnt = 0;
2330 sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
2331 if( zErrMsg ){
2332 fprintf(stderr,"Error: %s\n", zErrMsg);
2333 sqlite3_free(zErrMsg);
2334 rc = 1;
2335 }
2336 }else
2337
2338 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
2339 open_db(p, 0);
2340 /* When playing back a "dump", the content might appear in an order
2341 ** which causes immediate foreign key constraints to be violated.
2342 ** So disable foreign-key constraint enforcement to prevent problems. */
2343 if( nArg!=1 && nArg!=2 ){
2344 fprintf(stderr, "Usage: .dump ?LIKE-PATTERN?\n");
2345 rc = 1;
2346 goto meta_command_exit;
2347 }
2348 fprintf(p->out, "PRAGMA foreign_keys=OFF;\n");
2349 fprintf(p->out, "BEGIN TRANSACTION;\n");
2350 p->writableSchema = 0;
2351 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
2352 p->nErr = 0;
2353 if( nArg==1 ){
2354 run_schema_dump_query(p,
2355 "SELECT name, type, sql FROM sqlite_master "
2356 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
2357 );
2358 run_schema_dump_query(p,
2359 "SELECT name, type, sql FROM sqlite_master "
2360 "WHERE name=='sqlite_sequence'"
2361 );
2362 run_table_dump_query(p,
2363 "SELECT sql FROM sqlite_master "
2364 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
2365 );
2366 }else{
2367 int i;
2368 for(i=1; i<nArg; i++){
2369 zShellStatic = azArg[i];
2370 run_schema_dump_query(p,
2371 "SELECT name, type, sql FROM sqlite_master "
2372 "WHERE tbl_name LIKE shellstatic() AND type=='table'"
2373 " AND sql NOT NULL");
2374 run_table_dump_query(p,
2375 "SELECT sql FROM sqlite_master "
2376 "WHERE sql NOT NULL"
2377 " AND type IN ('index','trigger','view')"
2378 " AND tbl_name LIKE shellstatic()", 0
2379 );
2380 zShellStatic = 0;
2381 }
2382 }
2383 if( p->writableSchema ){
2384 fprintf(p->out, "PRAGMA writable_schema=OFF;\n");
2385 p->writableSchema = 0;
2386 }
2387 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
2388 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
2389 fprintf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
2390 }else
2391
2392 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
2393 if( nArg==2 ){
2394 p->echoOn = booleanValue(azArg[1]);
2395 }else{
2396 fprintf(stderr, "Usage: .echo on|off\n");
2397 rc = 1;
2398 }
2399 }else
2400
2401 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
2402 if( nArg==2 ){
2403 p->autoEQP = booleanValue(azArg[1]);
2404 }else{
2405 fprintf(stderr, "Usage: .eqp on|off\n");
2406 rc = 1;
2407 }
2408 }else
2409
2410 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
2411 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
2412 rc = 2;
2413 }else
2414
2415 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
2416 int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
2417 if(val == 1) {
2418 if(!p->explainPrev.valid) {
2419 p->explainPrev.valid = 1;
2420 p->explainPrev.mode = p->mode;
2421 p->explainPrev.showHeader = p->showHeader;
2422 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
2423 }
2424 /* We could put this code under the !p->explainValid
2425 ** condition so that it does not execute if we are already in
2426 ** explain mode. However, always executing it allows us an easy
2427 ** was to reset to explain mode in case the user previously
2428 ** did an .explain followed by a .width, .mode or .header
2429 ** command.
2430 */
2431 p->mode = MODE_Explain;
2432 p->showHeader = 1;
2433 memset(p->colWidth,0,sizeof(p->colWidth));
2434 p->colWidth[0] = 4; /* addr */
2435 p->colWidth[1] = 13; /* opcode */
2436 p->colWidth[2] = 4; /* P1 */
2437 p->colWidth[3] = 4; /* P2 */
2438 p->colWidth[4] = 4; /* P3 */
2439 p->colWidth[5] = 13; /* P4 */
2440 p->colWidth[6] = 2; /* P5 */
2441 p->colWidth[7] = 13; /* Comment */
2442 }else if (p->explainPrev.valid) {
2443 p->explainPrev.valid = 0;
2444 p->mode = p->explainPrev.mode;
2445 p->showHeader = p->explainPrev.showHeader;
2446 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
2447 }
2448 }else
2449
2450 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
2451 if( nArg==2 ){
2452 p->showHeader = booleanValue(azArg[1]);
2453 }else{
2454 fprintf(stderr, "Usage: .headers on|off\n");
2455 rc = 1;
2456 }
2457 }else
2458
2459 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
2460 fprintf(p->out, "%s", zHelp);
2461 }else
2462
2463 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
2464 char *zTable; /* Insert data into this table */
2465 char *zFile; /* Name of file to extra content from */
2466 sqlite3_stmt *pStmt = NULL; /* A statement */
2467 int nCol; /* Number of columns in the table */
2468 int nByte; /* Number of bytes in an SQL string */
2469 int i, j; /* Loop counters */
2470 int needCommit; /* True to COMMIT or ROLLBACK at end */
2471 int nSep; /* Number of bytes in p->separator[] */
2472 char *zSql; /* An SQL statement */
2473 CSVReader sCsv; /* Reader context */
2474 int (*xCloser)(FILE*); /* Procedure to close th3 connection */
2475
2476 if( nArg!=3 ){
2477 fprintf(stderr, "Usage: .import FILE TABLE\n");
2478 goto meta_command_exit;
2479 }
2480 zFile = azArg[1];
2481 zTable = azArg[2];
2482 seenInterrupt = 0;
2483 memset(&sCsv, 0, sizeof(sCsv));
2484 open_db(p, 0);
2485 nSep = strlen30(p->separator);
2486 if( nSep==0 ){
2487 fprintf(stderr, "Error: non-null separator required for import\n");
2488 return 1;
2489 }
2490 if( nSep>1 ){
2491 fprintf(stderr, "Error: multi-character separators not allowed"
2492 " for import\n");
2493 return 1;
2494 }
2495 sCsv.zFile = zFile;
2496 sCsv.nLine = 1;
2497 if( sCsv.zFile[0]=='|' ){
2498 sCsv.in = popen(sCsv.zFile+1, "r");
2499 sCsv.zFile = "<pipe>";
2500 xCloser = pclose;
2501 }else{
2502 sCsv.in = fopen(sCsv.zFile, "rb");
2503 xCloser = fclose;
2504 }
2505 if( sCsv.in==0 ){
2506 fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
2507 return 1;
2508 }
2509 sCsv.cSeparator = p->separator[0];
2510 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
2511 if( zSql==0 ){
2512 fprintf(stderr, "Error: out of memory\n");
2513 xCloser(sCsv.in);
2514 return 1;
2515 }
2516 nByte = strlen30(zSql);
2517 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2518 csv_append_char(&sCsv, 0); /* To ensure sCsv.z is allocated */
2519 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(db))==0 ){
2520 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
2521 char cSep = '(';
2522 while( csv_read_one_field(&sCsv) ){
2523 zCreate = sqlite3_mprintf("%z%c\n \"%s\" TEXT", zCreate, cSep, sCsv.z);
2524 cSep = ',';
2525 if( sCsv.cTerm!=sCsv.cSeparator ) break;
2526 }
2527 if( cSep=='(' ){
2528 sqlite3_free(zCreate);
2529 sqlite3_free(sCsv.z);
2530 xCloser(sCsv.in);
2531 fprintf(stderr,"%s: empty file\n", sCsv.zFile);
2532 return 1;
2533 }
2534 zCreate = sqlite3_mprintf("%z\n)", zCreate);
2535 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
2536 sqlite3_free(zCreate);
2537 if( rc ){
2538 fprintf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
2539 sqlite3_errmsg(db));
2540 sqlite3_free(sCsv.z);
2541 xCloser(sCsv.in);
2542 return 1;
2543 }
2544 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2545 }
2546 sqlite3_free(zSql);
2547 if( rc ){
2548 if (pStmt) sqlite3_finalize(pStmt);
2549 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
2550 xCloser(sCsv.in);
2551 return 1;
2552 }
2553 nCol = sqlite3_column_count(pStmt);
2554 sqlite3_finalize(pStmt);
2555 pStmt = 0;
2556 if( nCol==0 ) return 0; /* no columns, no error */
2557 zSql = sqlite3_malloc( nByte*2 + 20 + nCol*2 );
2558 if( zSql==0 ){
2559 fprintf(stderr, "Error: out of memory\n");
2560 xCloser(sCsv.in);
2561 return 1;
2562 }
2563 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
2564 j = strlen30(zSql);
2565 for(i=1; i<nCol; i++){
2566 zSql[j++] = ',';
2567 zSql[j++] = '?';
2568 }
2569 zSql[j++] = ')';
2570 zSql[j] = 0;
2571 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2572 sqlite3_free(zSql);
2573 if( rc ){
2574 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
2575 if (pStmt) sqlite3_finalize(pStmt);
2576 xCloser(sCsv.in);
2577 return 1;
2578 }
2579 needCommit = sqlite3_get_autocommit(db);
2580 if( needCommit ) sqlite3_exec(db, "BEGIN", 0, 0, 0);
2581 do{
2582 int startLine = sCsv.nLine;
2583 for(i=0; i<nCol; i++){
2584 char *z = csv_read_one_field(&sCsv);
2585 if( z==0 && i==0 ) break;
2586 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
2587 if( i<nCol-1 && sCsv.cTerm!=sCsv.cSeparator ){
2588 fprintf(stderr, "%s:%d: expected %d columns but found %d - "
2589 "filling the rest with NULL\n",
2590 sCsv.zFile, startLine, nCol, i+1);
2591 i++;
2592 while( i<nCol ){ sqlite3_bind_null(pStmt, i); i++; }
2593 }
2594 }
2595 if( sCsv.cTerm==sCsv.cSeparator ){
2596 do{
2597 csv_read_one_field(&sCsv);
2598 i++;
2599 }while( sCsv.cTerm==sCsv.cSeparator );
2600 fprintf(stderr, "%s:%d: expected %d columns but found %d - "
2601 "extras ignored\n",
2602 sCsv.zFile, startLine, nCol, i);
2603 }
2604 if( i>=nCol ){
2605 sqlite3_step(pStmt);
2606 rc = sqlite3_reset(pStmt);
2607 if( rc!=SQLITE_OK ){
2608 fprintf(stderr, "%s:%d: INSERT failed: %s\n", sCsv.zFile, startLine,
2609 sqlite3_errmsg(db));
2610 }
2611 }
2612 }while( sCsv.cTerm!=EOF );
2613
2614 xCloser(sCsv.in);
2615 sqlite3_free(sCsv.z);
2616 sqlite3_finalize(pStmt);
2617 if( needCommit ) sqlite3_exec(db, "COMMIT", 0, 0, 0);
2618 }else
2619
2620 if( c=='i' && strncmp(azArg[0], "indices", n)==0 ){
2621 struct callback_data data;
2622 char *zErrMsg = 0;
2623 open_db(p, 0);
2624 memcpy(&data, p, sizeof(data));
2625 data.showHeader = 0;
2626 data.mode = MODE_List;
2627 if( nArg==1 ){
2628 rc = sqlite3_exec(p->db,
2629 "SELECT name FROM sqlite_master "
2630 "WHERE type='index' AND name NOT LIKE 'sqlite_%' "
2631 "UNION ALL "
2632 "SELECT name FROM sqlite_temp_master "
2633 "WHERE type='index' "
2634 "ORDER BY 1",
2635 callback, &data, &zErrMsg
2636 );
2637 }else if( nArg==2 ){
2638 zShellStatic = azArg[1];
2639 rc = sqlite3_exec(p->db,
2640 "SELECT name FROM sqlite_master "
2641 "WHERE type='index' AND tbl_name LIKE shellstatic() "
2642 "UNION ALL "
2643 "SELECT name FROM sqlite_temp_master "
2644 "WHERE type='index' AND tbl_name LIKE shellstatic() "
2645 "ORDER BY 1",
2646 callback, &data, &zErrMsg
2647 );
2648 zShellStatic = 0;
2649 }else{
2650 fprintf(stderr, "Usage: .indices ?LIKE-PATTERN?\n");
2651 rc = 1;
2652 goto meta_command_exit;
2653 }
2654 if( zErrMsg ){
2655 fprintf(stderr,"Error: %s\n", zErrMsg);
2656 sqlite3_free(zErrMsg);
2657 rc = 1;
2658 }else if( rc != SQLITE_OK ){
2659 fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n");
2660 rc = 1;
2661 }
2662 }else
2663
2664 #ifdef SQLITE_ENABLE_IOTRACE
2665 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
2666 extern void (*sqlite3IoTrace)(const char*, ...);
2667 if( iotrace && iotrace!=stdout ) fclose(iotrace);
2668 iotrace = 0;
2669 if( nArg<2 ){
2670 sqlite3IoTrace = 0;
2671 }else if( strcmp(azArg[1], "-")==0 ){
2672 sqlite3IoTrace = iotracePrintf;
2673 iotrace = stdout;
2674 }else{
2675 iotrace = fopen(azArg[1], "w");
2676 if( iotrace==0 ){
2677 fprintf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
2678 sqlite3IoTrace = 0;
2679 rc = 1;
2680 }else{
2681 sqlite3IoTrace = iotracePrintf;
2682 }
2683 }
2684 }else
2685 #endif
2686
2687 #ifndef SQLITE_OMIT_LOAD_EXTENSION
2688 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
2689 const char *zFile, *zProc;
2690 char *zErrMsg = 0;
2691 if( nArg<2 ){
2692 fprintf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
2693 rc = 1;
2694 goto meta_command_exit;
2695 }
2696 zFile = azArg[1];
2697 zProc = nArg>=3 ? azArg[2] : 0;
2698 open_db(p, 0);
2699 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
2700 if( rc!=SQLITE_OK ){
2701 fprintf(stderr, "Error: %s\n", zErrMsg);
2702 sqlite3_free(zErrMsg);
2703 rc = 1;
2704 }
2705 }else
2706 #endif
2707
2708 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
2709 if( nArg!=2 ){
2710 fprintf(stderr, "Usage: .log FILENAME\n");
2711 rc = 1;
2712 }else{
2713 const char *zFile = azArg[1];
2714 output_file_close(p->pLog);
2715 p->pLog = output_file_open(zFile);
2716 }
2717 }else
2718
2719 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
2720 const char *zMode = nArg>=2 ? azArg[1] : "";
2721 int n2 = (int)strlen(zMode);
2722 int c2 = zMode[0];
2723 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
2724 p->mode = MODE_Line;
2725 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
2726 p->mode = MODE_Column;
2727 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
2728 p->mode = MODE_List;
2729 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
2730 p->mode = MODE_Html;
2731 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
2732 p->mode = MODE_Tcl;
2733 sqlite3_snprintf(sizeof(p->separator), p->separator, " ");
2734 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
2735 p->mode = MODE_Csv;
2736 sqlite3_snprintf(sizeof(p->separator), p->separator, ",");
2737 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
2738 p->mode = MODE_List;
2739 sqlite3_snprintf(sizeof(p->separator), p->separator, "\t");
2740 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
2741 p->mode = MODE_Insert;
2742 set_table_name(p, nArg>=3 ? azArg[2] : "table");
2743 }else {
2744 fprintf(stderr,"Error: mode should be one of: "
2745 "column csv html insert line list tabs tcl\n");
2746 rc = 1;
2747 }
2748 }else
2749
2750 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
2751 if( nArg==2 ){
2752 sqlite3_snprintf(sizeof(p->nullvalue), p->nullvalue,
2753 "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
2754 }else{
2755 fprintf(stderr, "Usage: .nullvalue STRING\n");
2756 rc = 1;
2757 }
2758 }else
2759
2760 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
2761 sqlite3 *savedDb = p->db;
2762 const char *zSavedFilename = p->zDbFilename;
2763 char *zNewFilename = 0;
2764 p->db = 0;
2765 if( nArg>=2 ){
2766 p->zDbFilename = zNewFilename = sqlite3_mprintf("%s", azArg[1]);
2767 }
2768 open_db(p, 1);
2769 if( p->db!=0 ){
2770 sqlite3_close(savedDb);
2771 sqlite3_free(p->zFreeOnClose);
2772 p->zFreeOnClose = zNewFilename;
2773 }else{
2774 sqlite3_free(zNewFilename);
2775 p->db = savedDb;
2776 p->zDbFilename = zSavedFilename;
2777 }
2778 }else
2779
2780 if( c=='o'
2781 && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0)
2782 ){
2783 const char *zFile = nArg>=2 ? azArg[1] : "stdout";
2784 if( nArg>2 ){
2785 fprintf(stderr, "Usage: .%s FILE\n", azArg[0]);
2786 rc = 1;
2787 goto meta_command_exit;
2788 }
2789 if( n>1 && strncmp(azArg[0], "once", n)==0 ){
2790 if( nArg<2 ){
2791 fprintf(stderr, "Usage: .once FILE\n");
2792 rc = 1;
2793 goto meta_command_exit;
2794 }
2795 p->outCount = 2;
2796 }else{
2797 p->outCount = 0;
2798 }
2799 output_reset(p);
2800 if( zFile[0]=='|' ){
2801 p->out = popen(zFile + 1, "w");
2802 if( p->out==0 ){
2803 fprintf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
2804 p->out = stdout;
2805 rc = 1;
2806 }else{
2807 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
2808 }
2809 }else{
2810 p->out = output_file_open(zFile);
2811 if( p->out==0 ){
2812 if( strcmp(zFile,"off")!=0 ){
2813 fprintf(stderr,"Error: cannot write to \"%s\"\n", zFile);
2814 }
2815 p->out = stdout;
2816 rc = 1;
2817 } else {
2818 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
2819 }
2820 }
2821 }else
2822
2823 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
2824 int i;
2825 for(i=1; i<nArg; i++){
2826 if( i>1 ) fprintf(p->out, " ");
2827 fprintf(p->out, "%s", azArg[i]);
2828 }
2829 fprintf(p->out, "\n");
2830 }else
2831
2832 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
2833 if( nArg >= 2) {
2834 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
2835 }
2836 if( nArg >= 3) {
2837 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
2838 }
2839 }else
2840
2841 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
2842 rc = 2;
2843 }else
2844
2845 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
2846 FILE *alt;
2847 if( nArg!=2 ){
2848 fprintf(stderr, "Usage: .read FILE\n");
2849 rc = 1;
2850 goto meta_command_exit;
2851 }
2852 alt = fopen(azArg[1], "rb");
2853 if( alt==0 ){
2854 fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
2855 rc = 1;
2856 }else{
2857 rc = process_input(p, alt);
2858 fclose(alt);
2859 }
2860 }else
2861
2862 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
2863 const char *zSrcFile;
2864 const char *zDb;
2865 sqlite3 *pSrc;
2866 sqlite3_backup *pBackup;
2867 int nTimeout = 0;
2868
2869 if( nArg==2 ){
2870 zSrcFile = azArg[1];
2871 zDb = "main";
2872 }else if( nArg==3 ){
2873 zSrcFile = azArg[2];
2874 zDb = azArg[1];
2875 }else{
2876 fprintf(stderr, "Usage: .restore ?DB? FILE\n");
2877 rc = 1;
2878 goto meta_command_exit;
2879 }
2880 rc = sqlite3_open(zSrcFile, &pSrc);
2881 if( rc!=SQLITE_OK ){
2882 fprintf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
2883 sqlite3_close(pSrc);
2884 return 1;
2885 }
2886 open_db(p, 0);
2887 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
2888 if( pBackup==0 ){
2889 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
2890 sqlite3_close(pSrc);
2891 return 1;
2892 }
2893 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
2894 || rc==SQLITE_BUSY ){
2895 if( rc==SQLITE_BUSY ){
2896 if( nTimeout++ >= 3 ) break;
2897 sqlite3_sleep(100);
2898 }
2899 }
2900 sqlite3_backup_finish(pBackup);
2901 if( rc==SQLITE_DONE ){
2902 rc = 0;
2903 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
2904 fprintf(stderr, "Error: source database is busy\n");
2905 rc = 1;
2906 }else{
2907 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
2908 rc = 1;
2909 }
2910 sqlite3_close(pSrc);
2911 }else
2912
2913 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
2914 struct callback_data data;
2915 char *zErrMsg = 0;
2916 open_db(p, 0);
2917 memcpy(&data, p, sizeof(data));
2918 data.showHeader = 0;
2919 data.mode = MODE_Semi;
2920 if( nArg==2 ){
2921 int i;
2922 for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
2923 if( strcmp(azArg[1],"sqlite_master")==0 ){
2924 char *new_argv[2], *new_colv[2];
2925 new_argv[0] = "CREATE TABLE sqlite_master (\n"
2926 " type text,\n"
2927 " name text,\n"
2928 " tbl_name text,\n"
2929 " rootpage integer,\n"
2930 " sql text\n"
2931 ")";
2932 new_argv[1] = 0;
2933 new_colv[0] = "sql";
2934 new_colv[1] = 0;
2935 callback(&data, 1, new_argv, new_colv);
2936 rc = SQLITE_OK;
2937 }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){
2938 char *new_argv[2], *new_colv[2];
2939 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
2940 " type text,\n"
2941 " name text,\n"
2942 " tbl_name text,\n"
2943 " rootpage integer,\n"
2944 " sql text\n"
2945 ")";
2946 new_argv[1] = 0;
2947 new_colv[0] = "sql";
2948 new_colv[1] = 0;
2949 callback(&data, 1, new_argv, new_colv);
2950 rc = SQLITE_OK;
2951 }else{
2952 zShellStatic = azArg[1];
2953 rc = sqlite3_exec(p->db,
2954 "SELECT sql FROM "
2955 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
2956 " FROM sqlite_master UNION ALL"
2957 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
2958 "WHERE lower(tbl_name) LIKE shellstatic()"
2959 " AND type!='meta' AND sql NOTNULL "
2960 "ORDER BY rowid",
2961 callback, &data, &zErrMsg);
2962 zShellStatic = 0;
2963 }
2964 }else if( nArg==1 ){
2965 rc = sqlite3_exec(p->db,
2966 "SELECT sql FROM "
2967 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
2968 " FROM sqlite_master UNION ALL"
2969 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
2970 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
2971 "ORDER BY rowid",
2972 callback, &data, &zErrMsg
2973 );
2974 }else{
2975 fprintf(stderr, "Usage: .schema ?LIKE-PATTERN?\n");
2976 rc = 1;
2977 goto meta_command_exit;
2978 }
2979 if( zErrMsg ){
2980 fprintf(stderr,"Error: %s\n", zErrMsg);
2981 sqlite3_free(zErrMsg);
2982 rc = 1;
2983 }else if( rc != SQLITE_OK ){
2984 fprintf(stderr,"Error: querying schema information\n");
2985 rc = 1;
2986 }else{
2987 rc = 0;
2988 }
2989 }else
2990
2991 #ifdef SQLITE_DEBUG
2992 /* Undocumented commands for internal testing. Subject to change
2993 ** without notice. */
2994 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
2995 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
2996 int i, v;
2997 for(i=1; i<nArg; i++){
2998 v = booleanValue(azArg[i]);
2999 fprintf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
3000 }
3001 }
3002 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
3003 int i; sqlite3_int64 v;
3004 for(i=1; i<nArg; i++){
3005 char zBuf[200];
3006 v = integerValue(azArg[i]);
3007 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
3008 fprintf(p->out, "%s", zBuf);
3009 }
3010 }
3011 }else
3012 #endif
3013
3014 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
3015 if( nArg==2 ){
3016 sqlite3_snprintf(sizeof(p->separator), p->separator,
3017 "%.*s", (int)sizeof(p->separator)-1, azArg[1]);
3018 }else{
3019 fprintf(stderr, "Usage: .separator STRING\n");
3020 rc = 1;
3021 }
3022 }else
3023
3024 if( c=='s'
3025 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
3026 ){
3027 char *zCmd;
3028 int i;
3029 if( nArg<2 ){
3030 fprintf(stderr, "Usage: .system COMMAND\n");
3031 rc = 1;
3032 goto meta_command_exit;
3033 }
3034 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
3035 for(i=2; i<nArg; i++){
3036 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
3037 zCmd, azArg[i]);
3038 }
3039 (void)system(zCmd);
3040 sqlite3_free(zCmd);
3041 }else
3042
3043 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
3044 int i;
3045 if( nArg!=1 ){
3046 fprintf(stderr, "Usage: .show\n");
3047 rc = 1;
3048 goto meta_command_exit;
3049 }
3050 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
3051 fprintf(p->out,"%9.9s: %s\n","eqp", p->autoEQP ? "on" : "off");
3052 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
3053 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
3054 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
3055 fprintf(p->out,"%9.9s: ", "nullvalue");
3056 output_c_string(p->out, p->nullvalue);
3057 fprintf(p->out, "\n");
3058 fprintf(p->out,"%9.9s: %s\n","output",
3059 strlen30(p->outfile) ? p->outfile : "stdout");
3060 fprintf(p->out,"%9.9s: ", "separator");
3061 output_c_string(p->out, p->separator);
3062 fprintf(p->out, "\n");
3063 fprintf(p->out,"%9.9s: %s\n","stats", p->statsOn ? "on" : "off");
3064 fprintf(p->out,"%9.9s: ","width");
3065 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
3066 fprintf(p->out,"%d ",p->colWidth[i]);
3067 }
3068 fprintf(p->out,"\n");
3069 }else
3070
3071 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
3072 if( nArg==2 ){
3073 p->statsOn = booleanValue(azArg[1]);
3074 }else{
3075 fprintf(stderr, "Usage: .stats on|off\n");
3076 rc = 1;
3077 }
3078 }else
3079
3080 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
3081 sqlite3_stmt *pStmt;
3082 char **azResult;
3083 int nRow, nAlloc;
3084 char *zSql = 0;
3085 int ii;
3086 open_db(p, 0);
3087 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
3088 if( rc ) return rc;
3089 zSql = sqlite3_mprintf(
3090 "SELECT name FROM sqlite_master"
3091 " WHERE type IN ('table','view')"
3092 " AND name NOT LIKE 'sqlite_%%'"
3093 " AND name LIKE ?1");
3094 while( sqlite3_step(pStmt)==SQLITE_ROW ){
3095 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
3096 if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue;
3097 if( strcmp(zDbName,"temp")==0 ){
3098 zSql = sqlite3_mprintf(
3099 "%z UNION ALL "
3100 "SELECT 'temp.' || name FROM sqlite_temp_master"
3101 " WHERE type IN ('table','view')"
3102 " AND name NOT LIKE 'sqlite_%%'"
3103 " AND name LIKE ?1", zSql);
3104 }else{
3105 zSql = sqlite3_mprintf(
3106 "%z UNION ALL "
3107 "SELECT '%q.' || name FROM \"%w\".sqlite_master"
3108 " WHERE type IN ('table','view')"
3109 " AND name NOT LIKE 'sqlite_%%'"
3110 " AND name LIKE ?1", zSql, zDbName, zDbName);
3111 }
3112 }
3113 sqlite3_finalize(pStmt);
3114 zSql = sqlite3_mprintf("%z ORDER BY 1", zSql);
3115 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3116 sqlite3_free(zSql);
3117 if( rc ) return rc;
3118 nRow = nAlloc = 0;
3119 azResult = 0;
3120 if( nArg>1 ){
3121 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
3122 }else{
3123 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
3124 }
3125 while( sqlite3_step(pStmt)==SQLITE_ROW ){
3126 if( nRow>=nAlloc ){
3127 char **azNew;
3128 int n = nAlloc*2 + 10;
3129 azNew = sqlite3_realloc(azResult, sizeof(azResult[0])*n);
3130 if( azNew==0 ){
3131 fprintf(stderr, "Error: out of memory\n");
3132 break;
3133 }
3134 nAlloc = n;
3135 azResult = azNew;
3136 }
3137 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
3138 if( azResult[nRow] ) nRow++;
3139 }
3140 sqlite3_finalize(pStmt);
3141 if( nRow>0 ){
3142 int len, maxlen = 0;
3143 int i, j;
3144 int nPrintCol, nPrintRow;
3145 for(i=0; i<nRow; i++){
3146 len = strlen30(azResult[i]);
3147 if( len>maxlen ) maxlen = len;
3148 }
3149 nPrintCol = 80/(maxlen+2);
3150 if( nPrintCol<1 ) nPrintCol = 1;
3151 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
3152 for(i=0; i<nPrintRow; i++){
3153 for(j=i; j<nRow; j+=nPrintRow){
3154 char *zSp = j<nPrintRow ? "" : " ";
3155 fprintf(p->out, "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
3156 }
3157 fprintf(p->out, "\n");
3158 }
3159 }
3160 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
3161 sqlite3_free(azResult);
3162 }else
3163
3164 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
3165 static const struct {
3166 const char *zCtrlName; /* Name of a test-control option */
3167 int ctrlCode; /* Integer code for that option */
3168 } aCtrl[] = {
3169 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE },
3170 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE },
3171 { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET },
3172 { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST },
3173 { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL },
3174 { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS },
3175 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE },
3176 { "assert", SQLITE_TESTCTRL_ASSERT },
3177 { "always", SQLITE_TESTCTRL_ALWAYS },
3178 { "reserve", SQLITE_TESTCTRL_RESERVE },
3179 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS },
3180 { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD },
3181 { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC },
3182 { "byteorder", SQLITE_TESTCTRL_BYTEORDER },
3183 };
3184 int testctrl = -1;
3185 int rc = 0;
3186 int i, n;
3187 open_db(p, 0);
3188
3189 /* convert testctrl text option to value. allow any unique prefix
3190 ** of the option name, or a numerical value. */
3191 n = strlen30(azArg[1]);
3192 for(i=0; i<(int)(sizeof(aCtrl)/sizeof(aCtrl[0])); i++){
3193 if( strncmp(azArg[1], aCtrl[i].zCtrlName, n)==0 ){
3194 if( testctrl<0 ){
3195 testctrl = aCtrl[i].ctrlCode;
3196 }else{
3197 fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
3198 testctrl = -1;
3199 break;
3200 }
3201 }
3202 }
3203 if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]);
3204 if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){
3205 fprintf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
3206 }else{
3207 switch(testctrl){
3208
3209 /* sqlite3_test_control(int, db, int) */
3210 case SQLITE_TESTCTRL_OPTIMIZATIONS:
3211 case SQLITE_TESTCTRL_RESERVE:
3212 if( nArg==3 ){
3213 int opt = (int)strtol(azArg[2], 0, 0);
3214 rc = sqlite3_test_control(testctrl, p->db, opt);
3215 fprintf(p->out, "%d (0x%08x)\n", rc, rc);
3216 } else {
3217 fprintf(stderr,"Error: testctrl %s takes a single int option\n",
3218 azArg[1]);
3219 }
3220 break;
3221
3222 /* sqlite3_test_control(int) */
3223 case SQLITE_TESTCTRL_PRNG_SAVE:
3224 case SQLITE_TESTCTRL_PRNG_RESTORE:
3225 case SQLITE_TESTCTRL_PRNG_RESET:
3226 case SQLITE_TESTCTRL_BYTEORDER:
3227 if( nArg==2 ){
3228 rc = sqlite3_test_control(testctrl);
3229 fprintf(p->out, "%d (0x%08x)\n", rc, rc);
3230 } else {
3231 fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]);
3232 }
3233 break;
3234
3235 /* sqlite3_test_control(int, uint) */
3236 case SQLITE_TESTCTRL_PENDING_BYTE:
3237 if( nArg==3 ){
3238 unsigned int opt = (unsigned int)integerValue(azArg[2]);
3239 rc = sqlite3_test_control(testctrl, opt);
3240 fprintf(p->out, "%d (0x%08x)\n", rc, rc);
3241 } else {
3242 fprintf(stderr,"Error: testctrl %s takes a single unsigned"
3243 " int option\n", azArg[1]);
3244 }
3245 break;
3246
3247 /* sqlite3_test_control(int, int) */
3248 case SQLITE_TESTCTRL_ASSERT:
3249 case SQLITE_TESTCTRL_ALWAYS:
3250 if( nArg==3 ){
3251 int opt = booleanValue(azArg[2]);
3252 rc = sqlite3_test_control(testctrl, opt);
3253 fprintf(p->out, "%d (0x%08x)\n", rc, rc);
3254 } else {
3255 fprintf(stderr,"Error: testctrl %s takes a single int option\n",
3256 azArg[1]);
3257 }
3258 break;
3259
3260 /* sqlite3_test_control(int, char *) */
3261 #ifdef SQLITE_N_KEYWORD
3262 case SQLITE_TESTCTRL_ISKEYWORD:
3263 if( nArg==3 ){
3264 const char *opt = azArg[2];
3265 rc = sqlite3_test_control(testctrl, opt);
3266 fprintf(p->out, "%d (0x%08x)\n", rc, rc);
3267 } else {
3268 fprintf(stderr,"Error: testctrl %s takes a single char * option\n",
3269 azArg[1]);
3270 }
3271 break;
3272 #endif
3273
3274 case SQLITE_TESTCTRL_BITVEC_TEST:
3275 case SQLITE_TESTCTRL_FAULT_INSTALL:
3276 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
3277 case SQLITE_TESTCTRL_SCRATCHMALLOC:
3278 default:
3279 fprintf(stderr,"Error: CLI support for testctrl %s not implemented\n",
3280 azArg[1]);
3281 break;
3282 }
3283 }
3284 }else
3285
3286 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
3287 open_db(p, 0);
3288 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
3289 }else
3290
3291 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
3292 if( nArg==2 ){
3293 enableTimer = booleanValue(azArg[1]);
3294 if( enableTimer && !HAS_TIMER ){
3295 fprintf(stderr, "Error: timer not available on this system.\n");
3296 enableTimer = 0;
3297 }
3298 }else{
3299 fprintf(stderr, "Usage: .timer on|off\n");
3300 rc = 1;
3301 }
3302 }else
3303
3304 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
3305 open_db(p, 0);
3306 output_file_close(p->traceOut);
3307 if( nArg!=2 ){
3308 fprintf(stderr, "Usage: .trace FILE|off\n");
3309 rc = 1;
3310 goto meta_command_exit;
3311 }
3312 p->traceOut = output_file_open(azArg[1]);
3313 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
3314 if( p->traceOut==0 ){
3315 sqlite3_trace(p->db, 0, 0);
3316 }else{
3317 sqlite3_trace(p->db, sql_trace_callback, p->traceOut);
3318 }
3319 #endif
3320 }else
3321
3322 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
3323 fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
3324 sqlite3_libversion(), sqlite3_sourceid());
3325 }else
3326
3327 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
3328 const char *zDbName = nArg==2 ? azArg[1] : "main";
3329 char *zVfsName = 0;
3330 if( p->db ){
3331 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
3332 if( zVfsName ){
3333 fprintf(p->out, "%s\n", zVfsName);
3334 sqlite3_free(zVfsName);
3335 }
3336 }
3337 }else
3338
3339 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
3340 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
3341 extern int sqlite3WhereTrace;
3342 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
3343 }else
3344 #endif
3345
3346 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
3347 int j;
3348 assert( nArg<=ArraySize(azArg) );
3349 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
3350 p->colWidth[j-1] = (int)integerValue(azArg[j]);
3351 }
3352 }else
3353
3354 {
3355 fprintf(stderr, "Error: unknown command or invalid arguments: "
3356 " \"%s\". Enter \".help\" for help\n", azArg[0]);
3357 rc = 1;
3358 }
3359
3360 meta_command_exit:
3361 if( p->outCount ){
3362 p->outCount--;
3363 if( p->outCount==0 ) output_reset(p);
3364 }
3365 return rc;
3366 }
3367
3368 /*
3369 ** Return TRUE if a semicolon occurs anywhere in the first N characters
3370 ** of string z[].
3371 */
line_contains_semicolon(const char * z,int N)3372 static int line_contains_semicolon(const char *z, int N){
3373 int i;
3374 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
3375 return 0;
3376 }
3377
3378 /*
3379 ** Test to see if a line consists entirely of whitespace.
3380 */
_all_whitespace(const char * z)3381 static int _all_whitespace(const char *z){
3382 for(; *z; z++){
3383 if( IsSpace(z[0]) ) continue;
3384 if( *z=='/' && z[1]=='*' ){
3385 z += 2;
3386 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
3387 if( *z==0 ) return 0;
3388 z++;
3389 continue;
3390 }
3391 if( *z=='-' && z[1]=='-' ){
3392 z += 2;
3393 while( *z && *z!='\n' ){ z++; }
3394 if( *z==0 ) return 1;
3395 continue;
3396 }
3397 return 0;
3398 }
3399 return 1;
3400 }
3401
3402 /*
3403 ** Return TRUE if the line typed in is an SQL command terminator other
3404 ** than a semi-colon. The SQL Server style "go" command is understood
3405 ** as is the Oracle "/".
3406 */
line_is_command_terminator(const char * zLine)3407 static int line_is_command_terminator(const char *zLine){
3408 while( IsSpace(zLine[0]) ){ zLine++; };
3409 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
3410 return 1; /* Oracle */
3411 }
3412 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
3413 && _all_whitespace(&zLine[2]) ){
3414 return 1; /* SQL Server */
3415 }
3416 return 0;
3417 }
3418
3419 /*
3420 ** Return true if zSql is a complete SQL statement. Return false if it
3421 ** ends in the middle of a string literal or C-style comment.
3422 */
line_is_complete(char * zSql,int nSql)3423 static int line_is_complete(char *zSql, int nSql){
3424 int rc;
3425 if( zSql==0 ) return 1;
3426 zSql[nSql] = ';';
3427 zSql[nSql+1] = 0;
3428 rc = sqlite3_complete(zSql);
3429 zSql[nSql] = 0;
3430 return rc;
3431 }
3432
3433 /*
3434 ** Read input from *in and process it. If *in==0 then input
3435 ** is interactive - the user is typing it it. Otherwise, input
3436 ** is coming from a file or device. A prompt is issued and history
3437 ** is saved only if input is interactive. An interrupt signal will
3438 ** cause this routine to exit immediately, unless input is interactive.
3439 **
3440 ** Return the number of errors.
3441 */
process_input(struct callback_data * p,FILE * in)3442 static int process_input(struct callback_data *p, FILE *in){
3443 char *zLine = 0; /* A single input line */
3444 char *zSql = 0; /* Accumulated SQL text */
3445 int nLine; /* Length of current line */
3446 int nSql = 0; /* Bytes of zSql[] used */
3447 int nAlloc = 0; /* Allocated zSql[] space */
3448 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
3449 char *zErrMsg; /* Error message returned */
3450 int rc; /* Error code */
3451 int errCnt = 0; /* Number of errors seen */
3452 int lineno = 0; /* Current line number */
3453 int startline = 0; /* Line number for start of current input */
3454
3455 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
3456 fflush(p->out);
3457 zLine = one_input_line(in, zLine, nSql>0);
3458 if( zLine==0 ){
3459 /* End of input */
3460 if( stdin_is_interactive ) printf("\n");
3461 break;
3462 }
3463 if( seenInterrupt ){
3464 if( in!=0 ) break;
3465 seenInterrupt = 0;
3466 }
3467 lineno++;
3468 if( nSql==0 && _all_whitespace(zLine) ){
3469 if( p->echoOn ) printf("%s\n", zLine);
3470 continue;
3471 }
3472 if( zLine && zLine[0]=='.' && nSql==0 ){
3473 if( p->echoOn ) printf("%s\n", zLine);
3474 rc = do_meta_command(zLine, p);
3475 if( rc==2 ){ /* exit requested */
3476 break;
3477 }else if( rc ){
3478 errCnt++;
3479 }
3480 continue;
3481 }
3482 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
3483 memcpy(zLine,";",2);
3484 }
3485 nLine = strlen30(zLine);
3486 if( nSql+nLine+2>=nAlloc ){
3487 nAlloc = nSql+nLine+100;
3488 zSql = realloc(zSql, nAlloc);
3489 if( zSql==0 ){
3490 fprintf(stderr, "Error: out of memory\n");
3491 exit(1);
3492 }
3493 }
3494 nSqlPrior = nSql;
3495 if( nSql==0 ){
3496 int i;
3497 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
3498 assert( nAlloc>0 && zSql!=0 );
3499 memcpy(zSql, zLine+i, nLine+1-i);
3500 startline = lineno;
3501 nSql = nLine-i;
3502 }else{
3503 zSql[nSql++] = '\n';
3504 memcpy(zSql+nSql, zLine, nLine+1);
3505 nSql += nLine;
3506 }
3507 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
3508 && sqlite3_complete(zSql) ){
3509 p->cnt = 0;
3510 open_db(p, 0);
3511 BEGIN_TIMER;
3512 rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
3513 END_TIMER;
3514 if( rc || zErrMsg ){
3515 char zPrefix[100];
3516 if( in!=0 || !stdin_is_interactive ){
3517 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
3518 "Error: near line %d:", startline);
3519 }else{
3520 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
3521 }
3522 if( zErrMsg!=0 ){
3523 fprintf(stderr, "%s %s\n", zPrefix, zErrMsg);
3524 sqlite3_free(zErrMsg);
3525 zErrMsg = 0;
3526 }else{
3527 fprintf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
3528 }
3529 errCnt++;
3530 }
3531 nSql = 0;
3532 if( p->outCount ){
3533 output_reset(p);
3534 p->outCount = 0;
3535 }
3536 }else if( nSql && _all_whitespace(zSql) ){
3537 if( p->echoOn ) printf("%s\n", zSql);
3538 nSql = 0;
3539 }
3540 }
3541 if( nSql ){
3542 if( !_all_whitespace(zSql) ){
3543 fprintf(stderr, "Error: incomplete SQL: %s\n", zSql);
3544 }
3545 free(zSql);
3546 }
3547 free(zLine);
3548 return errCnt>0;
3549 }
3550
3551 /*
3552 ** Return a pathname which is the user's home directory. A
3553 ** 0 return indicates an error of some kind.
3554 */
find_home_dir(void)3555 static char *find_home_dir(void){
3556 static char *home_dir = NULL;
3557 if( home_dir ) return home_dir;
3558
3559 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
3560 {
3561 struct passwd *pwent;
3562 uid_t uid = getuid();
3563 if( (pwent=getpwuid(uid)) != NULL) {
3564 home_dir = pwent->pw_dir;
3565 }
3566 }
3567 #endif
3568
3569 #if defined(_WIN32_WCE)
3570 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
3571 */
3572 home_dir = "/";
3573 #else
3574
3575 #if defined(_WIN32) || defined(WIN32)
3576 if (!home_dir) {
3577 home_dir = getenv("USERPROFILE");
3578 }
3579 #endif
3580
3581 if (!home_dir) {
3582 home_dir = getenv("HOME");
3583 }
3584
3585 #if defined(_WIN32) || defined(WIN32)
3586 if (!home_dir) {
3587 char *zDrive, *zPath;
3588 int n;
3589 zDrive = getenv("HOMEDRIVE");
3590 zPath = getenv("HOMEPATH");
3591 if( zDrive && zPath ){
3592 n = strlen30(zDrive) + strlen30(zPath) + 1;
3593 home_dir = malloc( n );
3594 if( home_dir==0 ) return 0;
3595 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
3596 return home_dir;
3597 }
3598 home_dir = "c:\\";
3599 }
3600 #endif
3601
3602 #endif /* !_WIN32_WCE */
3603
3604 if( home_dir ){
3605 int n = strlen30(home_dir) + 1;
3606 char *z = malloc( n );
3607 if( z ) memcpy(z, home_dir, n);
3608 home_dir = z;
3609 }
3610
3611 return home_dir;
3612 }
3613
3614 /*
3615 ** Read input from the file given by sqliterc_override. Or if that
3616 ** parameter is NULL, take input from ~/.sqliterc
3617 **
3618 ** Returns the number of errors.
3619 */
process_sqliterc(struct callback_data * p,const char * sqliterc_override)3620 static int process_sqliterc(
3621 struct callback_data *p, /* Configuration data */
3622 const char *sqliterc_override /* Name of config file. NULL to use default */
3623 ){
3624 char *home_dir = NULL;
3625 const char *sqliterc = sqliterc_override;
3626 char *zBuf = 0;
3627 FILE *in = NULL;
3628 int rc = 0;
3629
3630 if (sqliterc == NULL) {
3631 home_dir = find_home_dir();
3632 if( home_dir==0 ){
3633 #if !defined(__RTP__) && !defined(_WRS_KERNEL)
3634 fprintf(stderr,"%s: Error: cannot locate your home directory\n", Argv0);
3635 #endif
3636 return 1;
3637 }
3638 sqlite3_initialize();
3639 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
3640 sqliterc = zBuf;
3641 }
3642 in = fopen(sqliterc,"rb");
3643 if( in ){
3644 if( stdin_is_interactive ){
3645 fprintf(stderr,"-- Loading resources from %s\n",sqliterc);
3646 }
3647 rc = process_input(p,in);
3648 fclose(in);
3649 }
3650 sqlite3_free(zBuf);
3651 return rc;
3652 }
3653
3654 /*
3655 ** Show available command line options
3656 */
3657 static const char zOptions[] =
3658 " -bail stop after hitting an error\n"
3659 " -batch force batch I/O\n"
3660 " -column set output mode to 'column'\n"
3661 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
3662 " -csv set output mode to 'csv'\n"
3663 " -echo print commands before execution\n"
3664 " -init FILENAME read/process named file\n"
3665 " -gpkg use the GeoPackage database schema\n"
3666 " -[no]header turn headers on or off\n"
3667 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
3668 " -heap SIZE Size of heap for memsys3 or memsys5\n"
3669 #endif
3670 " -help show this message\n"
3671 " -html set output mode to HTML\n"
3672 " -interactive force interactive I/O\n"
3673 " -line set output mode to 'line'\n"
3674 " -list set output mode to 'list'\n"
3675 " -mmap N default mmap size set to N\n"
3676 #ifdef SQLITE_ENABLE_MULTIPLEX
3677 " -multiplex enable the multiplexor VFS\n"
3678 #endif
3679 " -nullvalue TEXT set text string for NULL values. Default ''\n"
3680 " -separator SEP set output field separator. Default: '|'\n"
3681 " -spl3 use the Spatialite 3.x database schema\n"
3682 " -spl4 use the Spatialite 4.x database schema\n"
3683 " -stats print memory stats before each finalize\n"
3684 " -version show SQLite version\n"
3685 " -vfs NAME use NAME as the default VFS\n"
3686 #ifdef SQLITE_ENABLE_VFSTRACE
3687 " -vfstrace enable tracing of all VFS calls\n"
3688 #endif
3689 ;
usage(int showDetail)3690 static void usage(int showDetail){
3691 fprintf(stderr,
3692 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
3693 "FILENAME is the name of an SQLite database. A new database is created\n"
3694 "if the file does not previously exist.\n", Argv0);
3695 if( showDetail ){
3696 fprintf(stderr, "OPTIONS include:\n%s", zOptions);
3697 }else{
3698 fprintf(stderr, "Use the -help option for additional information\n");
3699 }
3700 exit(1);
3701 }
3702
3703 /*
3704 ** Initialize the state information in data
3705 */
main_init(struct callback_data * data)3706 static void main_init(struct callback_data *data) {
3707 memset(data, 0, sizeof(*data));
3708 data->mode = MODE_List;
3709 memcpy(data->separator,"|", 2);
3710 data->showHeader = 0;
3711 sqlite3_config(SQLITE_CONFIG_URI, 1);
3712 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
3713 data->gpkgEntryPoint = sqlite3_gpkg_auto_init;
3714 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
3715 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
3716 sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
3717 }
3718
3719 /*
3720 ** Output text to the console in a font that attracts extra attention.
3721 */
3722 #ifdef _WIN32
printBold(const char * zText)3723 static void printBold(const char *zText){
3724 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
3725 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
3726 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
3727 SetConsoleTextAttribute(out,
3728 FOREGROUND_RED|FOREGROUND_INTENSITY
3729 );
3730 printf("%s", zText);
3731 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
3732 }
3733 #else
printBold(const char * zText)3734 static void printBold(const char *zText){
3735 printf("\033[1m%s\033[0m", zText);
3736 }
3737 #endif
3738
3739 /*
3740 ** Get the argument to an --option. Throw an error and die if no argument
3741 ** is available.
3742 */
cmdline_option_value(int argc,char ** argv,int i)3743 static char *cmdline_option_value(int argc, char **argv, int i){
3744 if( i==argc ){
3745 fprintf(stderr, "%s: Error: missing argument to %s\n",
3746 argv[0], argv[argc-1]);
3747 exit(1);
3748 }
3749 return argv[i];
3750 }
3751
main(int argc,char ** argv)3752 int main(int argc, char **argv){
3753 char *zErrMsg = 0;
3754 struct callback_data data;
3755 const char *zInitFile = 0;
3756 char *zFirstCmd = 0;
3757 int i;
3758 int rc = 0;
3759 int warnInmemoryDb = 0;
3760
3761 #if USE_SYSTEM_SQLITE+0!=1
3762 if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){
3763 fprintf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
3764 sqlite3_sourceid(), SQLITE_SOURCE_ID);
3765 exit(1);
3766 }
3767 #endif
3768 Argv0 = argv[0];
3769 main_init(&data);
3770 stdin_is_interactive = isatty(0);
3771
3772 /* Make sure we have a valid signal handler early, before anything
3773 ** else is done.
3774 */
3775 #ifdef SIGINT
3776 signal(SIGINT, interrupt_handler);
3777 #endif
3778
3779 /* Do an initial pass through the command-line argument to locate
3780 ** the name of the database file, the name of the initialization file,
3781 ** the size of the alternative malloc heap,
3782 ** and the first command to execute.
3783 */
3784 for(i=1; i<argc; i++){
3785 char *z;
3786 z = argv[i];
3787 if( z[0]!='-' ){
3788 if( data.zDbFilename==0 ){
3789 data.zDbFilename = z;
3790 continue;
3791 }
3792 if( zFirstCmd==0 ){
3793 zFirstCmd = z;
3794 continue;
3795 }
3796 fprintf(stderr,"%s: Error: too many options: \"%s\"\n", Argv0, argv[i]);
3797 fprintf(stderr,"Use -help for a list of options.\n");
3798 return 1;
3799 }
3800 if( z[1]=='-' ) z++;
3801 if( strcmp(z,"-separator")==0
3802 || strcmp(z,"-nullvalue")==0
3803 || strcmp(z,"-cmd")==0
3804 ){
3805 (void)cmdline_option_value(argc, argv, ++i);
3806 }else if( strcmp(z,"-init")==0 ){
3807 zInitFile = cmdline_option_value(argc, argv, ++i);
3808 }else if( strcmp(z,"-batch")==0 ){
3809 /* Need to check for batch mode here to so we can avoid printing
3810 ** informational messages (like from process_sqliterc) before
3811 ** we do the actual processing of arguments later in a second pass.
3812 */
3813 stdin_is_interactive = 0;
3814 }else if( strcmp(z,"-heap")==0 ){
3815 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
3816 const char *zSize;
3817 sqlite3_int64 szHeap;
3818
3819 zSize = cmdline_option_value(argc, argv, ++i);
3820 szHeap = integerValue(zSize);
3821 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
3822 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
3823 #endif
3824 #ifdef SQLITE_ENABLE_VFSTRACE
3825 }else if( strcmp(z,"-vfstrace")==0 ){
3826 extern int vfstrace_register(
3827 const char *zTraceName,
3828 const char *zOldVfsName,
3829 int (*xOut)(const char*,void*),
3830 void *pOutArg,
3831 int makeDefault
3832 );
3833 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
3834 #endif
3835 #ifdef SQLITE_ENABLE_MULTIPLEX
3836 }else if( strcmp(z,"-multiplex")==0 ){
3837 extern int sqlite3_multiple_initialize(const char*,int);
3838 sqlite3_multiplex_initialize(0, 1);
3839 #endif
3840 }else if( strcmp(z,"-mmap")==0 ){
3841 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
3842 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
3843 }else if( strcmp(z,"-vfs")==0 ){
3844 sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
3845 if( pVfs ){
3846 sqlite3_vfs_register(pVfs, 1);
3847 }else{
3848 fprintf(stderr, "no such VFS: \"%s\"\n", argv[i]);
3849 exit(1);
3850 }
3851 }else if( strcmp(z,"-gpkg")==0 ){
3852 data.gpkgEntryPoint = data.gpkgEntryPoint = sqlite3_gpkg_init;
3853 }else if( strcmp(z,"-spl3")==0 ){
3854 data.gpkgEntryPoint = data.gpkgEntryPoint = sqlite3_gpkg_spl3_init;
3855 }else if( strcmp(z,"-spl4")==0 ){
3856 data.gpkgEntryPoint = data.gpkgEntryPoint = sqlite3_gpkg_spl4_init;
3857 }
3858 }
3859 if( data.zDbFilename==0 ){
3860 #ifndef SQLITE_OMIT_MEMORYDB
3861 data.zDbFilename = ":memory:";
3862 warnInmemoryDb = argc==1;
3863 #else
3864 fprintf(stderr,"%s: Error: no database filename specified\n", Argv0);
3865 return 1;
3866 #endif
3867 #ifdef SQLITE_SHELL_DBNAME_PROC
3868 { extern void SQLITE_SHELL_DBNAME_PROC(const char**);
3869 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
3870 warnInmemoryDb = 0; }
3871 #endif
3872 }
3873 data.out = stdout;
3874
3875 /* Go ahead and open the database file if it already exists. If the
3876 ** file does not exist, delay opening it. This prevents empty database
3877 ** files from being created if a user mistypes the database name argument
3878 ** to the sqlite command-line tool.
3879 */
3880 if( strcmp(":memory:", data.zDbFilename) == 0 || access(data.zDbFilename, 0)==0 ){
3881 open_db(&data, 0);
3882 }
3883
3884 /* Process the initialization file if there is one. If no -init option
3885 ** is given on the command line, look for a file named ~/.sqliterc and
3886 ** try to process it.
3887 */
3888 rc = process_sqliterc(&data,zInitFile);
3889 if( rc>0 ){
3890 return rc;
3891 }
3892
3893 /* Make a second pass through the command-line argument and set
3894 ** options. This second pass is delayed until after the initialization
3895 ** file is processed so that the command-line arguments will override
3896 ** settings in the initialization file.
3897 */
3898 for(i=1; i<argc; i++){
3899 char *z = argv[i];
3900 if( z[0]!='-' ) continue;
3901 if( z[1]=='-' ){ z++; }
3902 if( strcmp(z,"-init")==0 ){
3903 i++;
3904 }else if( strcmp(z,"-html")==0 ){
3905 data.mode = MODE_Html;
3906 }else if( strcmp(z,"-list")==0 ){
3907 data.mode = MODE_List;
3908 }else if( strcmp(z,"-line")==0 ){
3909 data.mode = MODE_Line;
3910 }else if( strcmp(z,"-column")==0 ){
3911 data.mode = MODE_Column;
3912 }else if( strcmp(z,"-csv")==0 ){
3913 data.mode = MODE_Csv;
3914 memcpy(data.separator,",",2);
3915 }else if( strcmp(z,"-separator")==0 ){
3916 sqlite3_snprintf(sizeof(data.separator), data.separator,
3917 "%s",cmdline_option_value(argc,argv,++i));
3918 }else if( strcmp(z,"-nullvalue")==0 ){
3919 sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue,
3920 "%s",cmdline_option_value(argc,argv,++i));
3921 }else if( strcmp(z,"-header")==0 ){
3922 data.showHeader = 1;
3923 }else if( strcmp(z,"-noheader")==0 ){
3924 data.showHeader = 0;
3925 }else if( strcmp(z,"-echo")==0 ){
3926 data.echoOn = 1;
3927 }else if( strcmp(z,"-eqp")==0 ){
3928 data.autoEQP = 1;
3929 }else if( strcmp(z,"-stats")==0 ){
3930 data.statsOn = 1;
3931 }else if( strcmp(z,"-bail")==0 ){
3932 bail_on_error = 1;
3933 }else if( strcmp(z,"-version")==0 ){
3934 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
3935 return 0;
3936 }else if( strcmp(z,"-interactive")==0 ){
3937 stdin_is_interactive = 1;
3938 }else if( strcmp(z,"-batch")==0 ){
3939 stdin_is_interactive = 0;
3940 }else if( strcmp(z,"-heap")==0 ){
3941 i++;
3942 }else if( strcmp(z,"-mmap")==0 ){
3943 i++;
3944 }else if( strcmp(z,"-vfs")==0 ){
3945 i++;
3946 #ifdef SQLITE_ENABLE_VFSTRACE
3947 }else if( strcmp(z,"-vfstrace")==0 ){
3948 i++;
3949 #endif
3950 #ifdef SQLITE_ENABLE_MULTIPLEX
3951 }else if( strcmp(z,"-multiplex")==0 ){
3952 i++;
3953 #endif
3954 }else if( strcmp(z,"-help")==0 ){
3955 usage(1);
3956 }else if( strcmp(z,"-cmd")==0 ){
3957 if( i==argc-1 ) break;
3958 z = cmdline_option_value(argc,argv,++i);
3959 if( z[0]=='.' ){
3960 rc = do_meta_command(z, &data);
3961 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
3962 }else{
3963 open_db(&data, 0);
3964 rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
3965 if( zErrMsg!=0 ){
3966 fprintf(stderr,"Error: %s\n", zErrMsg);
3967 if( bail_on_error ) return rc!=0 ? rc : 1;
3968 }else if( rc!=0 ){
3969 fprintf(stderr,"Error: unable to process SQL \"%s\"\n", z);
3970 if( bail_on_error ) return rc;
3971 }
3972 }
3973 }else if( strcmp(z,"-gpkg")==0 || strcmp(z,"-spl3")==0 || strcmp(z,"-spl4")==0 ){
3974 i++;
3975 }else{
3976 fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
3977 fprintf(stderr,"Use -help for a list of options.\n");
3978 return 1;
3979 }
3980 }
3981
3982 if( zFirstCmd ){
3983 /* Run just the command that follows the database name
3984 */
3985 if( zFirstCmd[0]=='.' ){
3986 rc = do_meta_command(zFirstCmd, &data);
3987 if( rc==2 ) rc = 0;
3988 }else{
3989 open_db(&data, 0);
3990 rc = shell_exec(data.db, zFirstCmd, shell_callback, &data, &zErrMsg);
3991 if( zErrMsg!=0 ){
3992 fprintf(stderr,"Error: %s\n", zErrMsg);
3993 return rc!=0 ? rc : 1;
3994 }else if( rc!=0 ){
3995 fprintf(stderr,"Error: unable to process SQL \"%s\"\n", zFirstCmd);
3996 return rc;
3997 }
3998 }
3999 }else{
4000 /* Run commands received from standard input
4001 */
4002 if( stdin_is_interactive ){
4003 char *zHome;
4004 char *zHistory = 0;
4005 int nHistory;
4006 printf(
4007 "SQLite version %s %.19s\n" /*extra-version-info*/
4008 "libgpkg version %s\n"
4009 "Enter \".help\" for usage hints.\n",
4010 sqlite3_libversion(), sqlite3_sourceid(),
4011 gpkg_libversion()
4012 );
4013 if( warnInmemoryDb ){
4014 printf("Connected to a ");
4015 printBold("transient in-memory database");
4016 printf(".\nUse \".open FILENAME\" to reopen on a "
4017 "persistent database.\n");
4018 }
4019 zHome = find_home_dir();
4020 if( zHome ){
4021 nHistory = strlen30(zHome) + 20;
4022 if( (zHistory = malloc(nHistory))!=0 ){
4023 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
4024 }
4025 }
4026 #if defined(HAVE_READLINE)
4027 if( zHistory ) read_history(zHistory);
4028 #endif
4029 rc = process_input(&data, 0);
4030 if( zHistory ){
4031 stifle_history(100);
4032 write_history(zHistory);
4033 free(zHistory);
4034 }
4035 }else{
4036 rc = process_input(&data, stdin);
4037 }
4038 }
4039 set_table_name(&data, 0);
4040 if( data.db ){
4041 sqlite3_close(data.db);
4042 }
4043 sqlite3_free(data.zFreeOnClose);
4044 return rc;
4045 }
4046