00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
00016
00017 #define _CRT_SECURE_NO_WARNINGS
00018 #endif
00019
00020
00021
00022
00023 #ifndef SQLITE_DISABLE_LFS
00024 # define _LARGE_FILE 1
00025 # ifndef _FILE_OFFSET_BITS
00026 # define _FILE_OFFSET_BITS 64
00027 # endif
00028 # define _LARGEFILE_SOURCE 1
00029 #endif
00030
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <stdio.h>
00034 #include <assert.h>
00035 #include "sqlite3.h"
00036 #include <ctype.h>
00037 #include <stdarg.h>
00038
00039 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__)
00040 # include <signal.h>
00041 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
00042 # include <pwd.h>
00043 # endif
00044 # include <unistd.h>
00045 # include <sys/types.h>
00046 #endif
00047
00048 #ifdef __OS2__
00049 # include <unistd.h>
00050 #endif
00051
00052 #ifdef HAVE_EDITLINE
00053 # include <editline/editline.h>
00054 #endif
00055 #if defined(HAVE_READLINE) && HAVE_READLINE==1
00056 # include <readline/readline.h>
00057 # include <readline/history.h>
00058 #endif
00059 #if !defined(HAVE_EDITLINE) && (!defined(HAVE_READLINE) || HAVE_READLINE!=1)
00060 # define readline(p) local_getline(p,stdin)
00061 # define add_history(X)
00062 # define read_history(X)
00063 # define write_history(X)
00064 # define stifle_history(X)
00065 #endif
00066
00067 #if defined(_WIN32) || defined(WIN32)
00068 # include <io.h>
00069 #define isatty(h) _isatty(h)
00070 #define access(f,m) _access((f),(m))
00071 #else
00072
00073
00074 extern int isatty(int);
00075 #endif
00076
00077 #if defined(_WIN32_WCE)
00078
00079
00080
00081
00082 #define isatty(x) 1
00083 #endif
00084
00085
00086 static int enableTimer = 0;
00087
00088
00089 #define IsSpace(X) isspace((unsigned char)X)
00090 #define IsDigit(X) isdigit((unsigned char)X)
00091 #define ToLower(X) (char)tolower((unsigned char)X)
00092
00093 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(__RTP__) && !defined(_WRS_KERNEL)
00094 #include <sys/time.h>
00095 #include <sys/resource.h>
00096
00097
00098 static struct rusage sBegin;
00099
00100
00101
00102
00103 static void beginTimer(void){
00104 if( enableTimer ){
00105 getrusage(RUSAGE_SELF, &sBegin);
00106 }
00107 }
00108
00109
00110 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
00111 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
00112 (double)(pEnd->tv_sec - pStart->tv_sec);
00113 }
00114
00115
00116
00117
00118 static void endTimer(void){
00119 if( enableTimer ){
00120 struct rusage sEnd;
00121 getrusage(RUSAGE_SELF, &sEnd);
00122 printf("CPU Time: user %f sys %f\n",
00123 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
00124 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
00125 }
00126 }
00127
00128 #define BEGIN_TIMER beginTimer()
00129 #define END_TIMER endTimer()
00130 #define HAS_TIMER 1
00131
00132 #elif (defined(_WIN32) || defined(WIN32))
00133
00134 #include <windows.h>
00135
00136
00137 static HANDLE hProcess;
00138 static FILETIME ftKernelBegin;
00139 static FILETIME ftUserBegin;
00140 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME);
00141 static GETPROCTIMES getProcessTimesAddr = NULL;
00142
00143
00144
00145
00146
00147 static int hasTimer(void){
00148 if( getProcessTimesAddr ){
00149 return 1;
00150 } else {
00151
00152
00153
00154
00155 hProcess = GetCurrentProcess();
00156 if( hProcess ){
00157 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
00158 if( NULL != hinstLib ){
00159 getProcessTimesAddr = (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
00160 if( NULL != getProcessTimesAddr ){
00161 return 1;
00162 }
00163 FreeLibrary(hinstLib);
00164 }
00165 }
00166 }
00167 return 0;
00168 }
00169
00170
00171
00172
00173 static void beginTimer(void){
00174 if( enableTimer && getProcessTimesAddr ){
00175 FILETIME ftCreation, ftExit;
00176 getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelBegin, &ftUserBegin);
00177 }
00178 }
00179
00180
00181 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
00182 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
00183 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
00184 return (double) ((i64End - i64Start) / 10000000.0);
00185 }
00186
00187
00188
00189
00190 static void endTimer(void){
00191 if( enableTimer && getProcessTimesAddr){
00192 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
00193 getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelEnd, &ftUserEnd);
00194 printf("CPU Time: user %f sys %f\n",
00195 timeDiff(&ftUserBegin, &ftUserEnd),
00196 timeDiff(&ftKernelBegin, &ftKernelEnd));
00197 }
00198 }
00199
00200 #define BEGIN_TIMER beginTimer()
00201 #define END_TIMER endTimer()
00202 #define HAS_TIMER hasTimer()
00203
00204 #else
00205 #define BEGIN_TIMER
00206 #define END_TIMER
00207 #define HAS_TIMER 0
00208 #endif
00209
00210
00211
00212
00213 #define UNUSED_PARAMETER(x) (void)(x)
00214
00215
00216
00217
00218
00219 static int bail_on_error = 0;
00220
00221
00222
00223
00224
00225 static int stdin_is_interactive = 1;
00226
00227
00228
00229
00230
00231
00232 static sqlite3 *db = 0;
00233
00234
00235
00236
00237 static volatile int seenInterrupt = 0;
00238
00239
00240
00241
00242
00243 static char *Argv0;
00244
00245
00246
00247
00248
00249 static char mainPrompt[20];
00250 static char continuePrompt[20];
00251
00252
00253
00254
00255 #ifdef SQLITE_ENABLE_IOTRACE
00256 static FILE *iotrace = 0;
00257 #endif
00258
00259
00260
00261
00262
00263
00264
00265 #ifdef SQLITE_ENABLE_IOTRACE
00266 static void iotracePrintf(const char *zFormat, ...){
00267 va_list ap;
00268 char *z;
00269 if( iotrace==0 ) return;
00270 va_start(ap, zFormat);
00271 z = sqlite3_vmprintf(zFormat, ap);
00272 va_end(ap);
00273 fprintf(iotrace, "%s", z);
00274 sqlite3_free(z);
00275 }
00276 #endif
00277
00278
00279
00280
00281
00282 static int isNumber(const char *z, int *realnum){
00283 if( *z=='-' || *z=='+' ) z++;
00284 if( !IsDigit(*z) ){
00285 return 0;
00286 }
00287 z++;
00288 if( realnum ) *realnum = 0;
00289 while( IsDigit(*z) ){ z++; }
00290 if( *z=='.' ){
00291 z++;
00292 if( !IsDigit(*z) ) return 0;
00293 while( IsDigit(*z) ){ z++; }
00294 if( realnum ) *realnum = 1;
00295 }
00296 if( *z=='e' || *z=='E' ){
00297 z++;
00298 if( *z=='+' || *z=='-' ) z++;
00299 if( !IsDigit(*z) ) return 0;
00300 while( IsDigit(*z) ){ z++; }
00301 if( realnum ) *realnum = 1;
00302 }
00303 return *z==0;
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 static const char *zShellStatic = 0;
00315 static void shellstaticFunc(
00316 sqlite3_context *context,
00317 int argc,
00318 sqlite3_value **argv
00319 ){
00320 assert( 0==argc );
00321 assert( zShellStatic );
00322 UNUSED_PARAMETER(argc);
00323 UNUSED_PARAMETER(argv);
00324 sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337 static char *local_getline(char *zPrompt, FILE *in){
00338 char *zLine;
00339 int nLine;
00340 int n;
00341
00342 if( zPrompt && *zPrompt ){
00343 printf("%s",zPrompt);
00344 fflush(stdout);
00345 }
00346 nLine = 100;
00347 zLine = malloc( nLine );
00348 if( zLine==0 ) return 0;
00349 n = 0;
00350 while( 1 ){
00351 if( n+100>nLine ){
00352 nLine = nLine*2 + 100;
00353 zLine = realloc(zLine, nLine);
00354 if( zLine==0 ) return 0;
00355 }
00356 if( fgets(&zLine[n], nLine - n, in)==0 ){
00357 if( n==0 ){
00358 free(zLine);
00359 return 0;
00360 }
00361 zLine[n] = 0;
00362 break;
00363 }
00364 while( zLine[n] ){ n++; }
00365 if( n>0 && zLine[n-1]=='\n' ){
00366 n--;
00367 if( n>0 && zLine[n-1]=='\r' ) n--;
00368 zLine[n] = 0;
00369 break;
00370 }
00371 }
00372 zLine = realloc( zLine, n+1 );
00373 return zLine;
00374 }
00375
00376
00377
00378
00379
00380
00381
00382 static char *one_input_line(const char *zPrior, FILE *in){
00383 char *zPrompt;
00384 char *zResult;
00385 if( in!=0 ){
00386 return local_getline(0, in);
00387 }
00388 if( zPrior && zPrior[0] ){
00389 zPrompt = continuePrompt;
00390 }else{
00391 zPrompt = mainPrompt;
00392 }
00393 zResult = readline(zPrompt);
00394 #if defined(HAVE_READLINE) && HAVE_READLINE==1
00395 if( zResult && *zResult ) add_history(zResult);
00396 #endif
00397 return zResult;
00398 }
00399
00400 struct previous_mode_data {
00401 int valid;
00402 int mode;
00403 int showHeader;
00404 int colWidth[100];
00405 };
00406
00407
00408
00409
00410
00411
00412 struct callback_data {
00413 sqlite3 *db;
00414 int echoOn;
00415 int statsOn;
00416 int cnt;
00417 FILE *out;
00418 int nErr;
00419 int mode;
00420 int writableSchema;
00421 int showHeader;
00422 char *zDestTable;
00423 char separator[20];
00424 int colWidth[100];
00425 int actualWidth[100];
00426 char nullvalue[20];
00427
00428 struct previous_mode_data explainPrev;
00429
00430
00431 char outfile[FILENAME_MAX];
00432 const char *zDbFilename;
00433 const char *zVfs;
00434 sqlite3_stmt *pStmt;
00435 FILE *pLog;
00436 };
00437
00438
00439
00440
00441 #define MODE_Line 0
00442 #define MODE_Column 1
00443 #define MODE_List 2
00444 #define MODE_Semi 3
00445 #define MODE_Html 4
00446 #define MODE_Insert 5
00447 #define MODE_Tcl 6
00448 #define MODE_Csv 7
00449 #define MODE_Explain 8
00450
00451 static const char *modeDescr[] = {
00452 "line",
00453 "column",
00454 "list",
00455 "semi",
00456 "html",
00457 "insert",
00458 "tcl",
00459 "csv",
00460 "explain",
00461 };
00462
00463
00464
00465
00466 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
00467
00468
00469
00470
00471
00472 static int strlen30(const char *z){
00473 const char *z2 = z;
00474 while( *z2 ){ z2++; }
00475 return 0x3fffffff & (int)(z2 - z);
00476 }
00477
00478
00479
00480
00481 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
00482 struct callback_data *p = (struct callback_data*)pArg;
00483 if( p->pLog==0 ) return;
00484 fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
00485 fflush(p->pLog);
00486 }
00487
00488
00489
00490
00491 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
00492 int i;
00493 char *zBlob = (char *)pBlob;
00494 fprintf(out,"X'");
00495 for(i=0; i<nBlob; i++){ fprintf(out,"%02x",zBlob[i]); }
00496 fprintf(out,"'");
00497 }
00498
00499
00500
00501
00502 static void output_quoted_string(FILE *out, const char *z){
00503 int i;
00504 int nSingle = 0;
00505 for(i=0; z[i]; i++){
00506 if( z[i]=='\'' ) nSingle++;
00507 }
00508 if( nSingle==0 ){
00509 fprintf(out,"'%s'",z);
00510 }else{
00511 fprintf(out,"'");
00512 while( *z ){
00513 for(i=0; z[i] && z[i]!='\''; i++){}
00514 if( i==0 ){
00515 fprintf(out,"''");
00516 z++;
00517 }else if( z[i]=='\'' ){
00518 fprintf(out,"%.*s''",i,z);
00519 z += i+1;
00520 }else{
00521 fprintf(out,"%s",z);
00522 break;
00523 }
00524 }
00525 fprintf(out,"'");
00526 }
00527 }
00528
00529
00530
00531
00532 static void output_c_string(FILE *out, const char *z){
00533 unsigned int c;
00534 fputc('"', out);
00535 while( (c = *(z++))!=0 ){
00536 if( c=='\\' ){
00537 fputc(c, out);
00538 fputc(c, out);
00539 }else if( c=='\t' ){
00540 fputc('\\', out);
00541 fputc('t', out);
00542 }else if( c=='\n' ){
00543 fputc('\\', out);
00544 fputc('n', out);
00545 }else if( c=='\r' ){
00546 fputc('\\', out);
00547 fputc('r', out);
00548 }else if( !isprint(c) ){
00549 fprintf(out, "\\%03o", c&0xff);
00550 }else{
00551 fputc(c, out);
00552 }
00553 }
00554 fputc('"', out);
00555 }
00556
00557
00558
00559
00560
00561 static void output_html_string(FILE *out, const char *z){
00562 int i;
00563 while( *z ){
00564 for(i=0; z[i]
00565 && z[i]!='<'
00566 && z[i]!='&'
00567 && z[i]!='>'
00568 && z[i]!='\"'
00569 && z[i]!='\'';
00570 i++){}
00571 if( i>0 ){
00572 fprintf(out,"%.*s",i,z);
00573 }
00574 if( z[i]=='<' ){
00575 fprintf(out,"<");
00576 }else if( z[i]=='&' ){
00577 fprintf(out,"&");
00578 }else if( z[i]=='>' ){
00579 fprintf(out,">");
00580 }else if( z[i]=='\"' ){
00581 fprintf(out,""");
00582 }else if( z[i]=='\'' ){
00583 fprintf(out,"'");
00584 }else{
00585 break;
00586 }
00587 z += i + 1;
00588 }
00589 }
00590
00591
00592
00593
00594
00595 static const char needCsvQuote[] = {
00596 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00597 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00598 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
00599 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00600 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00601 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00602 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00603 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
00604 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00605 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00606 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00607 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00608 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00609 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00610 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00611 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00612 };
00613
00614
00615
00616
00617
00618
00619
00620 static void output_csv(struct callback_data *p, const char *z, int bSep){
00621 FILE *out = p->out;
00622 if( z==0 ){
00623 fprintf(out,"%s",p->nullvalue);
00624 }else{
00625 int i;
00626 int nSep = strlen30(p->separator);
00627 for(i=0; z[i]; i++){
00628 if( needCsvQuote[((unsigned char*)z)[i]]
00629 || (z[i]==p->separator[0] &&
00630 (nSep==1 || memcmp(z, p->separator, nSep)==0)) ){
00631 i = 0;
00632 break;
00633 }
00634 }
00635 if( i==0 ){
00636 putc('"', out);
00637 for(i=0; z[i]; i++){
00638 if( z[i]=='"' ) putc('"', out);
00639 putc(z[i], out);
00640 }
00641 putc('"', out);
00642 }else{
00643 fprintf(out, "%s", z);
00644 }
00645 }
00646 if( bSep ){
00647 fprintf(p->out, "%s", p->separator);
00648 }
00649 }
00650
00651 #ifdef SIGINT
00652
00653
00654
00655 static void interrupt_handler(int NotUsed){
00656 UNUSED_PARAMETER(NotUsed);
00657 seenInterrupt = 1;
00658 if( db ) sqlite3_interrupt(db);
00659 }
00660 #endif
00661
00662
00663
00664
00665
00666 static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
00667 int i;
00668 struct callback_data *p = (struct callback_data*)pArg;
00669
00670 switch( p->mode ){
00671 case MODE_Line: {
00672 int w = 5;
00673 if( azArg==0 ) break;
00674 for(i=0; i<nArg; i++){
00675 int len = strlen30(azCol[i] ? azCol[i] : "");
00676 if( len>w ) w = len;
00677 }
00678 if( p->cnt++>0 ) fprintf(p->out,"\n");
00679 for(i=0; i<nArg; i++){
00680 fprintf(p->out,"%*s = %s\n", w, azCol[i],
00681 azArg[i] ? azArg[i] : p->nullvalue);
00682 }
00683 break;
00684 }
00685 case MODE_Explain:
00686 case MODE_Column: {
00687 if( p->cnt++==0 ){
00688 for(i=0; i<nArg; i++){
00689 int w, n;
00690 if( i<ArraySize(p->colWidth) ){
00691 w = p->colWidth[i];
00692 }else{
00693 w = 0;
00694 }
00695 if( w<=0 ){
00696 w = strlen30(azCol[i] ? azCol[i] : "");
00697 if( w<10 ) w = 10;
00698 n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue);
00699 if( w<n ) w = n;
00700 }
00701 if( i<ArraySize(p->actualWidth) ){
00702 p->actualWidth[i] = w;
00703 }
00704 if( p->showHeader ){
00705 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
00706 }
00707 }
00708 if( p->showHeader ){
00709 for(i=0; i<nArg; i++){
00710 int w;
00711 if( i<ArraySize(p->actualWidth) ){
00712 w = p->actualWidth[i];
00713 }else{
00714 w = 10;
00715 }
00716 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
00717 "----------------------------------------------------------",
00718 i==nArg-1 ? "\n": " ");
00719 }
00720 }
00721 }
00722 if( azArg==0 ) break;
00723 for(i=0; i<nArg; i++){
00724 int w;
00725 if( i<ArraySize(p->actualWidth) ){
00726 w = p->actualWidth[i];
00727 }else{
00728 w = 10;
00729 }
00730 if( p->mode==MODE_Explain && azArg[i] &&
00731 strlen30(azArg[i])>w ){
00732 w = strlen30(azArg[i]);
00733 }
00734 fprintf(p->out,"%-*.*s%s",w,w,
00735 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
00736 }
00737 break;
00738 }
00739 case MODE_Semi:
00740 case MODE_List: {
00741 if( p->cnt++==0 && p->showHeader ){
00742 for(i=0; i<nArg; i++){
00743 fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
00744 }
00745 }
00746 if( azArg==0 ) break;
00747 for(i=0; i<nArg; i++){
00748 char *z = azArg[i];
00749 if( z==0 ) z = p->nullvalue;
00750 fprintf(p->out, "%s", z);
00751 if( i<nArg-1 ){
00752 fprintf(p->out, "%s", p->separator);
00753 }else if( p->mode==MODE_Semi ){
00754 fprintf(p->out, ";\n");
00755 }else{
00756 fprintf(p->out, "\n");
00757 }
00758 }
00759 break;
00760 }
00761 case MODE_Html: {
00762 if( p->cnt++==0 && p->showHeader ){
00763 fprintf(p->out,"<TR>");
00764 for(i=0; i<nArg; i++){
00765 fprintf(p->out,"<TH>");
00766 output_html_string(p->out, azCol[i]);
00767 fprintf(p->out,"</TH>\n");
00768 }
00769 fprintf(p->out,"</TR>\n");
00770 }
00771 if( azArg==0 ) break;
00772 fprintf(p->out,"<TR>");
00773 for(i=0; i<nArg; i++){
00774 fprintf(p->out,"<TD>");
00775 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
00776 fprintf(p->out,"</TD>\n");
00777 }
00778 fprintf(p->out,"</TR>\n");
00779 break;
00780 }
00781 case MODE_Tcl: {
00782 if( p->cnt++==0 && p->showHeader ){
00783 for(i=0; i<nArg; i++){
00784 output_c_string(p->out,azCol[i] ? azCol[i] : "");
00785 fprintf(p->out, "%s", p->separator);
00786 }
00787 fprintf(p->out,"\n");
00788 }
00789 if( azArg==0 ) break;
00790 for(i=0; i<nArg; i++){
00791 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
00792 fprintf(p->out, "%s", p->separator);
00793 }
00794 fprintf(p->out,"\n");
00795 break;
00796 }
00797 case MODE_Csv: {
00798 if( p->cnt++==0 && p->showHeader ){
00799 for(i=0; i<nArg; i++){
00800 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
00801 }
00802 fprintf(p->out,"\n");
00803 }
00804 if( azArg==0 ) break;
00805 for(i=0; i<nArg; i++){
00806 output_csv(p, azArg[i], i<nArg-1);
00807 }
00808 fprintf(p->out,"\n");
00809 break;
00810 }
00811 case MODE_Insert: {
00812 p->cnt++;
00813 if( azArg==0 ) break;
00814 fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
00815 for(i=0; i<nArg; i++){
00816 char *zSep = i>0 ? ",": "";
00817 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
00818 fprintf(p->out,"%sNULL",zSep);
00819 }else if( aiType && aiType[i]==SQLITE_TEXT ){
00820 if( zSep[0] ) fprintf(p->out,"%s",zSep);
00821 output_quoted_string(p->out, azArg[i]);
00822 }else if( aiType && (aiType[i]==SQLITE_INTEGER || aiType[i]==SQLITE_FLOAT) ){
00823 fprintf(p->out,"%s%s",zSep, azArg[i]);
00824 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
00825 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
00826 int nBlob = sqlite3_column_bytes(p->pStmt, i);
00827 if( zSep[0] ) fprintf(p->out,"%s",zSep);
00828 output_hex_blob(p->out, pBlob, nBlob);
00829 }else if( isNumber(azArg[i], 0) ){
00830 fprintf(p->out,"%s%s",zSep, azArg[i]);
00831 }else{
00832 if( zSep[0] ) fprintf(p->out,"%s",zSep);
00833 output_quoted_string(p->out, azArg[i]);
00834 }
00835 }
00836 fprintf(p->out,");\n");
00837 break;
00838 }
00839 }
00840 return 0;
00841 }
00842
00843
00844
00845
00846
00847 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
00848
00849 return shell_callback(pArg, nArg, azArg, azCol, NULL);
00850 }
00851
00852
00853
00854
00855
00856
00857 static void set_table_name(struct callback_data *p, const char *zName){
00858 int i, n;
00859 int needQuote;
00860 char *z;
00861
00862 if( p->zDestTable ){
00863 free(p->zDestTable);
00864 p->zDestTable = 0;
00865 }
00866 if( zName==0 ) return;
00867 needQuote = !isalpha((unsigned char)*zName) && *zName!='_';
00868 for(i=n=0; zName[i]; i++, n++){
00869 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){
00870 needQuote = 1;
00871 if( zName[i]=='\'' ) n++;
00872 }
00873 }
00874 if( needQuote ) n += 2;
00875 z = p->zDestTable = malloc( n+1 );
00876 if( z==0 ){
00877 fprintf(stderr,"Error: out of memory\n");
00878 exit(1);
00879 }
00880 n = 0;
00881 if( needQuote ) z[n++] = '\'';
00882 for(i=0; zName[i]; i++){
00883 z[n++] = zName[i];
00884 if( zName[i]=='\'' ) z[n++] = '\'';
00885 }
00886 if( needQuote ) z[n++] = '\'';
00887 z[n] = 0;
00888 }
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898 static char *appendText(char *zIn, char const *zAppend, char quote){
00899 int len;
00900 int i;
00901 int nAppend = strlen30(zAppend);
00902 int nIn = (zIn?strlen30(zIn):0);
00903
00904 len = nAppend+nIn+1;
00905 if( quote ){
00906 len += 2;
00907 for(i=0; i<nAppend; i++){
00908 if( zAppend[i]==quote ) len++;
00909 }
00910 }
00911
00912 zIn = (char *)realloc(zIn, len);
00913 if( !zIn ){
00914 return 0;
00915 }
00916
00917 if( quote ){
00918 char *zCsr = &zIn[nIn];
00919 *zCsr++ = quote;
00920 for(i=0; i<nAppend; i++){
00921 *zCsr++ = zAppend[i];
00922 if( zAppend[i]==quote ) *zCsr++ = quote;
00923 }
00924 *zCsr++ = quote;
00925 *zCsr++ = '\0';
00926 assert( (zCsr-zIn)==len );
00927 }else{
00928 memcpy(&zIn[nIn], zAppend, nAppend);
00929 zIn[len-1] = '\0';
00930 }
00931
00932 return zIn;
00933 }
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943 static int run_table_dump_query(
00944 struct callback_data *p,
00945 const char *zSelect,
00946 const char *zFirstRow
00947 ){
00948 sqlite3_stmt *pSelect;
00949 int rc;
00950 rc = sqlite3_prepare(p->db, zSelect, -1, &pSelect, 0);
00951 if( rc!=SQLITE_OK || !pSelect ){
00952 fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
00953 p->nErr++;
00954 return rc;
00955 }
00956 rc = sqlite3_step(pSelect);
00957 while( rc==SQLITE_ROW ){
00958 if( zFirstRow ){
00959 fprintf(p->out, "%s", zFirstRow);
00960 zFirstRow = 0;
00961 }
00962 fprintf(p->out, "%s;\n", sqlite3_column_text(pSelect, 0));
00963 rc = sqlite3_step(pSelect);
00964 }
00965 rc = sqlite3_finalize(pSelect);
00966 if( rc!=SQLITE_OK ){
00967 fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
00968 p->nErr++;
00969 }
00970 return rc;
00971 }
00972
00973
00974
00975
00976 static char *save_err_msg(
00977 sqlite3 *db
00978 ){
00979 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
00980 char *zErrMsg = sqlite3_malloc(nErrMsg);
00981 if( zErrMsg ){
00982 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
00983 }
00984 return zErrMsg;
00985 }
00986
00987
00988
00989
00990 static int display_stats(
00991 sqlite3 *db,
00992 struct callback_data *pArg,
00993 int bReset
00994 ){
00995 int iCur;
00996 int iHiwtr;
00997
00998 if( pArg && pArg->out ){
00999
01000 iHiwtr = iCur = -1;
01001 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
01002 fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr);
01003 iHiwtr = iCur = -1;
01004 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
01005 fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", iCur, iHiwtr);
01006
01007
01008
01009
01010
01011
01012 iHiwtr = iCur = -1;
01013 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
01014 fprintf(pArg->out, "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
01015
01016
01017
01018
01019
01020
01021 iHiwtr = iCur = -1;
01022 sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
01023 fprintf(pArg->out, "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
01024 iHiwtr = iCur = -1;
01025 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
01026 fprintf(pArg->out, "Largest Allocation: %d bytes\n", iHiwtr);
01027 iHiwtr = iCur = -1;
01028 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset);
01029 fprintf(pArg->out, "Largest Pcache Allocation: %d bytes\n", iHiwtr);
01030 iHiwtr = iCur = -1;
01031 sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset);
01032 fprintf(pArg->out, "Largest Scratch Allocation: %d bytes\n", iHiwtr);
01033 #ifdef YYTRACKMAXSTACKDEPTH
01034 iHiwtr = iCur = -1;
01035 sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset);
01036 fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", iCur, iHiwtr);
01037 #endif
01038 }
01039
01040 if( pArg && pArg->out && db ){
01041 iHiwtr = iCur = -1;
01042 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
01043 fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
01044 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
01045 fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
01046 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
01047 fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
01048 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
01049 fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
01050 iHiwtr = iCur = -1;
01051 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
01052 fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur); iHiwtr = iCur = -1;
01053 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
01054 fprintf(pArg->out, "Page cache hits: %d\n", iCur);
01055 iHiwtr = iCur = -1;
01056 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
01057 fprintf(pArg->out, "Page cache misses: %d\n", iCur);
01058 iHiwtr = iCur = -1;
01059 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
01060 fprintf(pArg->out, "Schema Heap Usage: %d bytes\n", iCur);
01061 iHiwtr = iCur = -1;
01062 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
01063 fprintf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", iCur);
01064 }
01065
01066 if( pArg && pArg->out && db && pArg->pStmt ){
01067 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, bReset);
01068 fprintf(pArg->out, "Fullscan Steps: %d\n", iCur);
01069 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
01070 fprintf(pArg->out, "Sort Operations: %d\n", iCur);
01071 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX, bReset);
01072 fprintf(pArg->out, "Autoindex Inserts: %d\n", iCur);
01073 }
01074
01075 return 0;
01076 }
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087 static int shell_exec(
01088 sqlite3 *db,
01089 const char *zSql,
01090 int (*xCallback)(void*,int,char**,char**,int*),
01091
01092 struct callback_data *pArg,
01093 char **pzErrMsg
01094 ){
01095 sqlite3_stmt *pStmt = NULL;
01096 int rc = SQLITE_OK;
01097 int rc2;
01098 const char *zLeftover;
01099
01100 if( pzErrMsg ){
01101 *pzErrMsg = NULL;
01102 }
01103
01104 while( zSql[0] && (SQLITE_OK == rc) ){
01105 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
01106 if( SQLITE_OK != rc ){
01107 if( pzErrMsg ){
01108 *pzErrMsg = save_err_msg(db);
01109 }
01110 }else{
01111 if( !pStmt ){
01112
01113 zSql = zLeftover;
01114 while( IsSpace(zSql[0]) ) zSql++;
01115 continue;
01116 }
01117
01118
01119 if( pArg ){
01120 pArg->pStmt = pStmt;
01121 pArg->cnt = 0;
01122 }
01123
01124
01125 if( pArg && pArg->echoOn ){
01126 const char *zStmtSql = sqlite3_sql(pStmt);
01127 fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
01128 }
01129
01130
01131
01132
01133 rc = sqlite3_step(pStmt);
01134
01135 if( SQLITE_ROW == rc ){
01136
01137 if( xCallback ){
01138
01139 int nCol = sqlite3_column_count(pStmt);
01140 void *pData = sqlite3_malloc(3*nCol*sizeof(const char*) + 1);
01141 if( !pData ){
01142 rc = SQLITE_NOMEM;
01143 }else{
01144 char **azCols = (char **)pData;
01145 char **azVals = &azCols[nCol];
01146 int *aiTypes = (int *)&azVals[nCol];
01147 int i;
01148 assert(sizeof(int) <= sizeof(char *));
01149
01150 for(i=0; i<nCol; i++){
01151 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
01152 }
01153 do{
01154
01155 for(i=0; i<nCol; i++){
01156 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
01157 aiTypes[i] = sqlite3_column_type(pStmt, i);
01158 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
01159 rc = SQLITE_NOMEM;
01160 break;
01161 }
01162 }
01163
01164
01165 if( SQLITE_ROW == rc ){
01166
01167 if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
01168 rc = SQLITE_ABORT;
01169 }else{
01170 rc = sqlite3_step(pStmt);
01171 }
01172 }
01173 } while( SQLITE_ROW == rc );
01174 sqlite3_free(pData);
01175 }
01176 }else{
01177 do{
01178 rc = sqlite3_step(pStmt);
01179 } while( rc == SQLITE_ROW );
01180 }
01181 }
01182
01183
01184 if( pArg && pArg->statsOn ){
01185 display_stats(db, pArg, 0);
01186 }
01187
01188
01189
01190
01191 rc2 = sqlite3_finalize(pStmt);
01192 if( rc!=SQLITE_NOMEM ) rc = rc2;
01193 if( rc==SQLITE_OK ){
01194 zSql = zLeftover;
01195 while( IsSpace(zSql[0]) ) zSql++;
01196 }else if( pzErrMsg ){
01197 *pzErrMsg = save_err_msg(db);
01198 }
01199
01200
01201 if( pArg ){
01202 pArg->pStmt = NULL;
01203 }
01204 }
01205 }
01206
01207 return rc;
01208 }
01209
01210
01211
01212
01213
01214
01215
01216
01217 static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
01218 int rc;
01219 const char *zTable;
01220 const char *zType;
01221 const char *zSql;
01222 const char *zPrepStmt = 0;
01223 struct callback_data *p = (struct callback_data *)pArg;
01224
01225 UNUSED_PARAMETER(azCol);
01226 if( nArg!=3 ) return 1;
01227 zTable = azArg[0];
01228 zType = azArg[1];
01229 zSql = azArg[2];
01230
01231 if( strcmp(zTable, "sqlite_sequence")==0 ){
01232 zPrepStmt = "DELETE FROM sqlite_sequence;\n";
01233 }else if( strcmp(zTable, "sqlite_stat1")==0 ){
01234 fprintf(p->out, "ANALYZE sqlite_master;\n");
01235 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
01236 return 0;
01237 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
01238 char *zIns;
01239 if( !p->writableSchema ){
01240 fprintf(p->out, "PRAGMA writable_schema=ON;\n");
01241 p->writableSchema = 1;
01242 }
01243 zIns = sqlite3_mprintf(
01244 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
01245 "VALUES('table','%q','%q',0,'%q');",
01246 zTable, zTable, zSql);
01247 fprintf(p->out, "%s\n", zIns);
01248 sqlite3_free(zIns);
01249 return 0;
01250 }else{
01251 fprintf(p->out, "%s;\n", zSql);
01252 }
01253
01254 if( strcmp(zType, "table")==0 ){
01255 sqlite3_stmt *pTableInfo = 0;
01256 char *zSelect = 0;
01257 char *zTableInfo = 0;
01258 char *zTmp = 0;
01259 int nRow = 0;
01260
01261 zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
01262 zTableInfo = appendText(zTableInfo, zTable, '"');
01263 zTableInfo = appendText(zTableInfo, ");", 0);
01264
01265 rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0);
01266 free(zTableInfo);
01267 if( rc!=SQLITE_OK || !pTableInfo ){
01268 return 1;
01269 }
01270
01271 zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
01272 zTmp = appendText(zTmp, zTable, '"');
01273 if( zTmp ){
01274 zSelect = appendText(zSelect, zTmp, '\'');
01275 }
01276 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
01277 rc = sqlite3_step(pTableInfo);
01278 while( rc==SQLITE_ROW ){
01279 const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
01280 zSelect = appendText(zSelect, "quote(", 0);
01281 zSelect = appendText(zSelect, zText, '"');
01282 rc = sqlite3_step(pTableInfo);
01283 if( rc==SQLITE_ROW ){
01284 zSelect = appendText(zSelect, ") || ',' || ", 0);
01285 }else{
01286 zSelect = appendText(zSelect, ") ", 0);
01287 }
01288 nRow++;
01289 }
01290 rc = sqlite3_finalize(pTableInfo);
01291 if( rc!=SQLITE_OK || nRow==0 ){
01292 free(zSelect);
01293 return 1;
01294 }
01295 zSelect = appendText(zSelect, "|| ')' FROM ", 0);
01296 zSelect = appendText(zSelect, zTable, '"');
01297
01298 rc = run_table_dump_query(p, zSelect, zPrepStmt);
01299 if( rc==SQLITE_CORRUPT ){
01300 zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
01301 run_table_dump_query(p, zSelect, 0);
01302 }
01303 if( zSelect ) free(zSelect);
01304 }
01305 return 0;
01306 }
01307
01308
01309
01310
01311
01312
01313
01314
01315 static int run_schema_dump_query(
01316 struct callback_data *p,
01317 const char *zQuery
01318 ){
01319 int rc;
01320 char *zErr = 0;
01321 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
01322 if( rc==SQLITE_CORRUPT ){
01323 char *zQ2;
01324 int len = strlen30(zQuery);
01325 fprintf(p->out, "/****** CORRUPTION ERROR *******/\n");
01326 if( zErr ){
01327 fprintf(p->out, "/****** %s ******/\n", zErr);
01328 sqlite3_free(zErr);
01329 zErr = 0;
01330 }
01331 zQ2 = malloc( len+100 );
01332 if( zQ2==0 ) return rc;
01333 sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery);
01334 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
01335 if( rc ){
01336 fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
01337 }else{
01338 rc = SQLITE_CORRUPT;
01339 }
01340 sqlite3_free(zErr);
01341 free(zQ2);
01342 }
01343 return rc;
01344 }
01345
01346
01347
01348
01349 static char zHelp[] =
01350 ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n"
01351 ".bail ON|OFF Stop after hitting an error. Default OFF\n"
01352 ".databases List names and files of attached databases\n"
01353 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
01354 " If TABLE specified, only dump tables matching\n"
01355 " LIKE pattern TABLE.\n"
01356 ".echo ON|OFF Turn command echo on or off\n"
01357 ".exit Exit this program\n"
01358 ".explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off.\n"
01359 " With no args, it turns EXPLAIN on.\n"
01360 ".header(s) ON|OFF Turn display of headers on or off\n"
01361 ".help Show this message\n"
01362 ".import FILE TABLE Import data from FILE into TABLE\n"
01363 ".indices ?TABLE? Show names of all indices\n"
01364 " If TABLE specified, only show indices for tables\n"
01365 " matching LIKE pattern TABLE.\n"
01366 #ifdef SQLITE_ENABLE_IOTRACE
01367 ".iotrace FILE Enable I/O diagnostic logging to FILE\n"
01368 #endif
01369 #ifndef SQLITE_OMIT_LOAD_EXTENSION
01370 ".load FILE ?ENTRY? Load an extension library\n"
01371 #endif
01372 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n"
01373 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n"
01374 " csv Comma-separated values\n"
01375 " column Left-aligned columns. (See .width)\n"
01376 " html HTML <table> code\n"
01377 " insert SQL insert statements for TABLE\n"
01378 " line One value per line\n"
01379 " list Values delimited by .separator string\n"
01380 " tabs Tab-separated values\n"
01381 " tcl TCL list elements\n"
01382 ".nullvalue STRING Print STRING in place of NULL values\n"
01383 ".output FILENAME Send output to FILENAME\n"
01384 ".output stdout Send output to the screen\n"
01385 ".prompt MAIN CONTINUE Replace the standard prompts\n"
01386 ".quit Exit this program\n"
01387 ".read FILENAME Execute SQL in FILENAME\n"
01388 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
01389 ".schema ?TABLE? Show the CREATE statements\n"
01390 " If TABLE specified, only show tables matching\n"
01391 " LIKE pattern TABLE.\n"
01392 ".separator STRING Change separator used by output mode and .import\n"
01393 ".show Show the current values for various settings\n"
01394 ".stats ON|OFF Turn stats on or off\n"
01395 ".tables ?TABLE? List names of tables\n"
01396 " If TABLE specified, only list tables matching\n"
01397 " LIKE pattern TABLE.\n"
01398 ".timeout MS Try opening locked tables for MS milliseconds\n"
01399 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
01400 ;
01401
01402 static char zTimerHelp[] =
01403 ".timer ON|OFF Turn the CPU timer measurement on or off\n"
01404 ;
01405
01406
01407 static int process_input(struct callback_data *p, FILE *in);
01408
01409
01410
01411
01412
01413 static void open_db(struct callback_data *p){
01414 if( p->db==0 ){
01415 sqlite3_open(p->zDbFilename, &p->db);
01416 db = p->db;
01417 if( db && sqlite3_errcode(db)==SQLITE_OK ){
01418 sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
01419 shellstaticFunc, 0, 0);
01420 }
01421 if( db==0 || SQLITE_OK!=sqlite3_errcode(db) ){
01422 fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
01423 p->zDbFilename, sqlite3_errmsg(db));
01424 exit(1);
01425 }
01426 #ifndef SQLITE_OMIT_LOAD_EXTENSION
01427 sqlite3_enable_load_extension(p->db, 1);
01428 #endif
01429 }
01430 }
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441 static void resolve_backslashes(char *z){
01442 int i, j;
01443 char c;
01444 for(i=j=0; (c = z[i])!=0; i++, j++){
01445 if( c=='\\' ){
01446 c = z[++i];
01447 if( c=='n' ){
01448 c = '\n';
01449 }else if( c=='t' ){
01450 c = '\t';
01451 }else if( c=='r' ){
01452 c = '\r';
01453 }else if( c>='0' && c<='7' ){
01454 c -= '0';
01455 if( z[i+1]>='0' && z[i+1]<='7' ){
01456 i++;
01457 c = (c<<3) + z[i] - '0';
01458 if( z[i+1]>='0' && z[i+1]<='7' ){
01459 i++;
01460 c = (c<<3) + z[i] - '0';
01461 }
01462 }
01463 }
01464 }
01465 z[j] = c;
01466 }
01467 z[j] = 0;
01468 }
01469
01470
01471
01472
01473 static int booleanValue(char *zArg){
01474 int val = atoi(zArg);
01475 int j;
01476 for(j=0; zArg[j]; j++){
01477 zArg[j] = ToLower(zArg[j]);
01478 }
01479 if( strcmp(zArg,"on")==0 ){
01480 val = 1;
01481 }else if( strcmp(zArg,"yes")==0 ){
01482 val = 1;
01483 }
01484 return val;
01485 }
01486
01487
01488
01489
01490
01491
01492
01493 static int do_meta_command(char *zLine, struct callback_data *p){
01494 int i = 1;
01495 int nArg = 0;
01496 int n, c;
01497 int rc = 0;
01498 char *azArg[50];
01499
01500
01501
01502 while( zLine[i] && nArg<ArraySize(azArg) ){
01503 while( IsSpace(zLine[i]) ){ i++; }
01504 if( zLine[i]==0 ) break;
01505 if( zLine[i]=='\'' || zLine[i]=='"' ){
01506 int delim = zLine[i++];
01507 azArg[nArg++] = &zLine[i];
01508 while( zLine[i] && zLine[i]!=delim ){ i++; }
01509 if( zLine[i]==delim ){
01510 zLine[i++] = 0;
01511 }
01512 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
01513 }else{
01514 azArg[nArg++] = &zLine[i];
01515 while( zLine[i] && !IsSpace(zLine[i]) ){ i++; }
01516 if( zLine[i] ) zLine[i++] = 0;
01517 resolve_backslashes(azArg[nArg-1]);
01518 }
01519 }
01520
01521
01522
01523 if( nArg==0 ) return 0;
01524 n = strlen30(azArg[0]);
01525 c = azArg[0][0];
01526 if( c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0 && nArg>1 && nArg<4){
01527 const char *zDestFile;
01528 const char *zDb;
01529 sqlite3 *pDest;
01530 sqlite3_backup *pBackup;
01531 if( nArg==2 ){
01532 zDestFile = azArg[1];
01533 zDb = "main";
01534 }else{
01535 zDestFile = azArg[2];
01536 zDb = azArg[1];
01537 }
01538 rc = sqlite3_open(zDestFile, &pDest);
01539 if( rc!=SQLITE_OK ){
01540 fprintf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
01541 sqlite3_close(pDest);
01542 return 1;
01543 }
01544 open_db(p);
01545 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
01546 if( pBackup==0 ){
01547 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
01548 sqlite3_close(pDest);
01549 return 1;
01550 }
01551 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
01552 sqlite3_backup_finish(pBackup);
01553 if( rc==SQLITE_DONE ){
01554 rc = 0;
01555 }else{
01556 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
01557 rc = 1;
01558 }
01559 sqlite3_close(pDest);
01560 }else
01561
01562 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 && nArg>1 && nArg<3 ){
01563 bail_on_error = booleanValue(azArg[1]);
01564 }else
01565
01566 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 && nArg==1 ){
01567 struct callback_data data;
01568 char *zErrMsg = 0;
01569 open_db(p);
01570 memcpy(&data, p, sizeof(data));
01571 data.showHeader = 1;
01572 data.mode = MODE_Column;
01573 data.colWidth[0] = 3;
01574 data.colWidth[1] = 15;
01575 data.colWidth[2] = 58;
01576 data.cnt = 0;
01577 sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
01578 if( zErrMsg ){
01579 fprintf(stderr,"Error: %s\n", zErrMsg);
01580 sqlite3_free(zErrMsg);
01581 rc = 1;
01582 }
01583 }else
01584
01585 if( c=='d' && strncmp(azArg[0], "dump", n)==0 && nArg<3 ){
01586 open_db(p);
01587
01588
01589
01590 fprintf(p->out, "PRAGMA foreign_keys=OFF;\n");
01591 fprintf(p->out, "BEGIN TRANSACTION;\n");
01592 p->writableSchema = 0;
01593 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
01594 p->nErr = 0;
01595 if( nArg==1 ){
01596 run_schema_dump_query(p,
01597 "SELECT name, type, sql FROM sqlite_master "
01598 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
01599 );
01600 run_schema_dump_query(p,
01601 "SELECT name, type, sql FROM sqlite_master "
01602 "WHERE name=='sqlite_sequence'"
01603 );
01604 run_table_dump_query(p,
01605 "SELECT sql FROM sqlite_master "
01606 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
01607 );
01608 }else{
01609 int i;
01610 for(i=1; i<nArg; i++){
01611 zShellStatic = azArg[i];
01612 run_schema_dump_query(p,
01613 "SELECT name, type, sql FROM sqlite_master "
01614 "WHERE tbl_name LIKE shellstatic() AND type=='table'"
01615 " AND sql NOT NULL");
01616 run_table_dump_query(p,
01617 "SELECT sql FROM sqlite_master "
01618 "WHERE sql NOT NULL"
01619 " AND type IN ('index','trigger','view')"
01620 " AND tbl_name LIKE shellstatic()", 0
01621 );
01622 zShellStatic = 0;
01623 }
01624 }
01625 if( p->writableSchema ){
01626 fprintf(p->out, "PRAGMA writable_schema=OFF;\n");
01627 p->writableSchema = 0;
01628 }
01629 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
01630 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
01631 fprintf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
01632 }else
01633
01634 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 && nArg<3 ){
01635 p->echoOn = booleanValue(azArg[1]);
01636 }else
01637
01638 if( c=='e' && strncmp(azArg[0], "exit", n)==0 && nArg==1 ){
01639 rc = 2;
01640 }else
01641
01642 if( c=='e' && strncmp(azArg[0], "explain", n)==0 && nArg<3 ){
01643 int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
01644 if(val == 1) {
01645 if(!p->explainPrev.valid) {
01646 p->explainPrev.valid = 1;
01647 p->explainPrev.mode = p->mode;
01648 p->explainPrev.showHeader = p->showHeader;
01649 memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth));
01650 }
01651
01652
01653
01654
01655
01656
01657
01658 p->mode = MODE_Explain;
01659 p->showHeader = 1;
01660 memset(p->colWidth,0,ArraySize(p->colWidth));
01661 p->colWidth[0] = 4;
01662 p->colWidth[1] = 13;
01663 p->colWidth[2] = 4;
01664 p->colWidth[3] = 4;
01665 p->colWidth[4] = 4;
01666 p->colWidth[5] = 13;
01667 p->colWidth[6] = 2;
01668 p->colWidth[7] = 13;
01669 }else if (p->explainPrev.valid) {
01670 p->explainPrev.valid = 0;
01671 p->mode = p->explainPrev.mode;
01672 p->showHeader = p->explainPrev.showHeader;
01673 memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth));
01674 }
01675 }else
01676
01677 if( c=='h' && (strncmp(azArg[0], "header", n)==0 ||
01678 strncmp(azArg[0], "headers", n)==0) && nArg>1 && nArg<3 ){
01679 p->showHeader = booleanValue(azArg[1]);
01680 }else
01681
01682 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
01683 fprintf(stderr,"%s",zHelp);
01684 if( HAS_TIMER ){
01685 fprintf(stderr,"%s",zTimerHelp);
01686 }
01687 }else
01688
01689 if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg==3 ){
01690 char *zTable = azArg[2];
01691 char *zFile = azArg[1];
01692 sqlite3_stmt *pStmt = NULL;
01693 int nCol;
01694 int nByte;
01695 int i, j;
01696 int nSep;
01697 char *zSql;
01698 char *zLine;
01699 char **azCol;
01700 char *zCommit;
01701 FILE *in;
01702 int lineno = 0;
01703
01704 open_db(p);
01705 nSep = strlen30(p->separator);
01706 if( nSep==0 ){
01707 fprintf(stderr, "Error: non-null separator required for import\n");
01708 return 1;
01709 }
01710 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
01711 if( zSql==0 ){
01712 fprintf(stderr, "Error: out of memory\n");
01713 return 1;
01714 }
01715 nByte = strlen30(zSql);
01716 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
01717 sqlite3_free(zSql);
01718 if( rc ){
01719 if (pStmt) sqlite3_finalize(pStmt);
01720 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
01721 return 1;
01722 }
01723 nCol = sqlite3_column_count(pStmt);
01724 sqlite3_finalize(pStmt);
01725 pStmt = 0;
01726 if( nCol==0 ) return 0;
01727 zSql = malloc( nByte + 20 + nCol*2 );
01728 if( zSql==0 ){
01729 fprintf(stderr, "Error: out of memory\n");
01730 return 1;
01731 }
01732 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zTable);
01733 j = strlen30(zSql);
01734 for(i=1; i<nCol; i++){
01735 zSql[j++] = ',';
01736 zSql[j++] = '?';
01737 }
01738 zSql[j++] = ')';
01739 zSql[j] = 0;
01740 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
01741 free(zSql);
01742 if( rc ){
01743 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
01744 if (pStmt) sqlite3_finalize(pStmt);
01745 return 1;
01746 }
01747 in = fopen(zFile, "rb");
01748 if( in==0 ){
01749 fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
01750 sqlite3_finalize(pStmt);
01751 return 1;
01752 }
01753 azCol = malloc( sizeof(azCol[0])*(nCol+1) );
01754 if( azCol==0 ){
01755 fprintf(stderr, "Error: out of memory\n");
01756 fclose(in);
01757 sqlite3_finalize(pStmt);
01758 return 1;
01759 }
01760 sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
01761 zCommit = "COMMIT";
01762 while( (zLine = local_getline(0, in))!=0 ){
01763 char *z;
01764 lineno++;
01765 azCol[0] = zLine;
01766 for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){
01767 if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){
01768 *z = 0;
01769 i++;
01770 if( i<nCol ){
01771 azCol[i] = &z[nSep];
01772 z += nSep-1;
01773 }
01774 }
01775 }
01776 *z = 0;
01777 if( i+1!=nCol ){
01778 fprintf(stderr,
01779 "Error: %s line %d: expected %d columns of data but found %d\n",
01780 zFile, lineno, nCol, i+1);
01781 zCommit = "ROLLBACK";
01782 free(zLine);
01783 rc = 1;
01784 break;
01785 }
01786 for(i=0; i<nCol; i++){
01787 sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
01788 }
01789 sqlite3_step(pStmt);
01790 rc = sqlite3_reset(pStmt);
01791 free(zLine);
01792 if( rc!=SQLITE_OK ){
01793 fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
01794 zCommit = "ROLLBACK";
01795 rc = 1;
01796 break;
01797 }
01798 }
01799 free(azCol);
01800 fclose(in);
01801 sqlite3_finalize(pStmt);
01802 sqlite3_exec(p->db, zCommit, 0, 0, 0);
01803 }else
01804
01805 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg<3 ){
01806 struct callback_data data;
01807 char *zErrMsg = 0;
01808 open_db(p);
01809 memcpy(&data, p, sizeof(data));
01810 data.showHeader = 0;
01811 data.mode = MODE_List;
01812 if( nArg==1 ){
01813 rc = sqlite3_exec(p->db,
01814 "SELECT name FROM sqlite_master "
01815 "WHERE type='index' AND name NOT LIKE 'sqlite_%' "
01816 "UNION ALL "
01817 "SELECT name FROM sqlite_temp_master "
01818 "WHERE type='index' "
01819 "ORDER BY 1",
01820 callback, &data, &zErrMsg
01821 );
01822 }else{
01823 zShellStatic = azArg[1];
01824 rc = sqlite3_exec(p->db,
01825 "SELECT name FROM sqlite_master "
01826 "WHERE type='index' AND tbl_name LIKE shellstatic() "
01827 "UNION ALL "
01828 "SELECT name FROM sqlite_temp_master "
01829 "WHERE type='index' AND tbl_name LIKE shellstatic() "
01830 "ORDER BY 1",
01831 callback, &data, &zErrMsg
01832 );
01833 zShellStatic = 0;
01834 }
01835 if( zErrMsg ){
01836 fprintf(stderr,"Error: %s\n", zErrMsg);
01837 sqlite3_free(zErrMsg);
01838 rc = 1;
01839 }else if( rc != SQLITE_OK ){
01840 fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n");
01841 rc = 1;
01842 }
01843 }else
01844
01845 #ifdef SQLITE_ENABLE_IOTRACE
01846 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
01847 extern void (*sqlite3IoTrace)(const char*, ...);
01848 if( iotrace && iotrace!=stdout ) fclose(iotrace);
01849 iotrace = 0;
01850 if( nArg<2 ){
01851 sqlite3IoTrace = 0;
01852 }else if( strcmp(azArg[1], "-")==0 ){
01853 sqlite3IoTrace = iotracePrintf;
01854 iotrace = stdout;
01855 }else{
01856 iotrace = fopen(azArg[1], "w");
01857 if( iotrace==0 ){
01858 fprintf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
01859 sqlite3IoTrace = 0;
01860 rc = 1;
01861 }else{
01862 sqlite3IoTrace = iotracePrintf;
01863 }
01864 }
01865 }else
01866 #endif
01867
01868 #ifndef SQLITE_OMIT_LOAD_EXTENSION
01869 if( c=='l' && strncmp(azArg[0], "load", n)==0 && nArg>=2 ){
01870 const char *zFile, *zProc;
01871 char *zErrMsg = 0;
01872 zFile = azArg[1];
01873 zProc = nArg>=3 ? azArg[2] : 0;
01874 open_db(p);
01875 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
01876 if( rc!=SQLITE_OK ){
01877 fprintf(stderr, "Error: %s\n", zErrMsg);
01878 sqlite3_free(zErrMsg);
01879 rc = 1;
01880 }
01881 }else
01882 #endif
01883
01884 if( c=='l' && strncmp(azArg[0], "log", n)==0 && nArg>=2 ){
01885 const char *zFile = azArg[1];
01886 if( p->pLog && p->pLog!=stdout && p->pLog!=stderr ){
01887 fclose(p->pLog);
01888 p->pLog = 0;
01889 }
01890 if( strcmp(zFile,"stdout")==0 ){
01891 p->pLog = stdout;
01892 }else if( strcmp(zFile, "stderr")==0 ){
01893 p->pLog = stderr;
01894 }else if( strcmp(zFile, "off")==0 ){
01895 p->pLog = 0;
01896 }else{
01897 p->pLog = fopen(zFile, "w");
01898 if( p->pLog==0 ){
01899 fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
01900 }
01901 }
01902 }else
01903
01904 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==2 ){
01905 int n2 = strlen30(azArg[1]);
01906 if( (n2==4 && strncmp(azArg[1],"line",n2)==0)
01907 ||
01908 (n2==5 && strncmp(azArg[1],"lines",n2)==0) ){
01909 p->mode = MODE_Line;
01910 }else if( (n2==6 && strncmp(azArg[1],"column",n2)==0)
01911 ||
01912 (n2==7 && strncmp(azArg[1],"columns",n2)==0) ){
01913 p->mode = MODE_Column;
01914 }else if( n2==4 && strncmp(azArg[1],"list",n2)==0 ){
01915 p->mode = MODE_List;
01916 }else if( n2==4 && strncmp(azArg[1],"html",n2)==0 ){
01917 p->mode = MODE_Html;
01918 }else if( n2==3 && strncmp(azArg[1],"tcl",n2)==0 ){
01919 p->mode = MODE_Tcl;
01920 }else if( n2==3 && strncmp(azArg[1],"csv",n2)==0 ){
01921 p->mode = MODE_Csv;
01922 sqlite3_snprintf(sizeof(p->separator), p->separator, ",");
01923 }else if( n2==4 && strncmp(azArg[1],"tabs",n2)==0 ){
01924 p->mode = MODE_List;
01925 sqlite3_snprintf(sizeof(p->separator), p->separator, "\t");
01926 }else if( n2==6 && strncmp(azArg[1],"insert",n2)==0 ){
01927 p->mode = MODE_Insert;
01928 set_table_name(p, "table");
01929 }else {
01930 fprintf(stderr,"Error: mode should be one of: "
01931 "column csv html insert line list tabs tcl\n");
01932 rc = 1;
01933 }
01934 }else
01935
01936 if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==3 ){
01937 int n2 = strlen30(azArg[1]);
01938 if( n2==6 && strncmp(azArg[1],"insert",n2)==0 ){
01939 p->mode = MODE_Insert;
01940 set_table_name(p, azArg[2]);
01941 }else {
01942 fprintf(stderr, "Error: invalid arguments: "
01943 " \"%s\". Enter \".help\" for help\n", azArg[2]);
01944 rc = 1;
01945 }
01946 }else
01947
01948 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
01949 sqlite3_snprintf(sizeof(p->nullvalue), p->nullvalue,
01950 "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
01951 }else
01952
01953 if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
01954 if( p->out!=stdout ){
01955 fclose(p->out);
01956 }
01957 if( strcmp(azArg[1],"stdout")==0 ){
01958 p->out = stdout;
01959 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "stdout");
01960 }else{
01961 p->out = fopen(azArg[1], "wb");
01962 if( p->out==0 ){
01963 fprintf(stderr,"Error: cannot write to \"%s\"\n", azArg[1]);
01964 p->out = stdout;
01965 rc = 1;
01966 } else {
01967 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
01968 }
01969 }
01970 }else
01971
01972 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
01973 if( nArg >= 2) {
01974 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
01975 }
01976 if( nArg >= 3) {
01977 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
01978 }
01979 }else
01980
01981 if( c=='q' && strncmp(azArg[0], "quit", n)==0 && nArg==1 ){
01982 rc = 2;
01983 }else
01984
01985 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
01986 FILE *alt = fopen(azArg[1], "rb");
01987 if( alt==0 ){
01988 fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
01989 rc = 1;
01990 }else{
01991 rc = process_input(p, alt);
01992 fclose(alt);
01993 }
01994 }else
01995
01996 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 && nArg>1 && nArg<4){
01997 const char *zSrcFile;
01998 const char *zDb;
01999 sqlite3 *pSrc;
02000 sqlite3_backup *pBackup;
02001 int nTimeout = 0;
02002
02003 if( nArg==2 ){
02004 zSrcFile = azArg[1];
02005 zDb = "main";
02006 }else{
02007 zSrcFile = azArg[2];
02008 zDb = azArg[1];
02009 }
02010 rc = sqlite3_open(zSrcFile, &pSrc);
02011 if( rc!=SQLITE_OK ){
02012 fprintf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
02013 sqlite3_close(pSrc);
02014 return 1;
02015 }
02016 open_db(p);
02017 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
02018 if( pBackup==0 ){
02019 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
02020 sqlite3_close(pSrc);
02021 return 1;
02022 }
02023 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
02024 || rc==SQLITE_BUSY ){
02025 if( rc==SQLITE_BUSY ){
02026 if( nTimeout++ >= 3 ) break;
02027 sqlite3_sleep(100);
02028 }
02029 }
02030 sqlite3_backup_finish(pBackup);
02031 if( rc==SQLITE_DONE ){
02032 rc = 0;
02033 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
02034 fprintf(stderr, "Error: source database is busy\n");
02035 rc = 1;
02036 }else{
02037 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
02038 rc = 1;
02039 }
02040 sqlite3_close(pSrc);
02041 }else
02042
02043 if( c=='s' && strncmp(azArg[0], "schema", n)==0 && nArg<3 ){
02044 struct callback_data data;
02045 char *zErrMsg = 0;
02046 open_db(p);
02047 memcpy(&data, p, sizeof(data));
02048 data.showHeader = 0;
02049 data.mode = MODE_Semi;
02050 if( nArg>1 ){
02051 int i;
02052 for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
02053 if( strcmp(azArg[1],"sqlite_master")==0 ){
02054 char *new_argv[2], *new_colv[2];
02055 new_argv[0] = "CREATE TABLE sqlite_master (\n"
02056 " type text,\n"
02057 " name text,\n"
02058 " tbl_name text,\n"
02059 " rootpage integer,\n"
02060 " sql text\n"
02061 ")";
02062 new_argv[1] = 0;
02063 new_colv[0] = "sql";
02064 new_colv[1] = 0;
02065 callback(&data, 1, new_argv, new_colv);
02066 rc = SQLITE_OK;
02067 }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){
02068 char *new_argv[2], *new_colv[2];
02069 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
02070 " type text,\n"
02071 " name text,\n"
02072 " tbl_name text,\n"
02073 " rootpage integer,\n"
02074 " sql text\n"
02075 ")";
02076 new_argv[1] = 0;
02077 new_colv[0] = "sql";
02078 new_colv[1] = 0;
02079 callback(&data, 1, new_argv, new_colv);
02080 rc = SQLITE_OK;
02081 }else{
02082 zShellStatic = azArg[1];
02083 rc = sqlite3_exec(p->db,
02084 "SELECT sql FROM "
02085 " (SELECT sql sql, type type, tbl_name tbl_name, name name"
02086 " FROM sqlite_master UNION ALL"
02087 " SELECT sql, type, tbl_name, name FROM sqlite_temp_master) "
02088 "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL "
02089 "ORDER BY substr(type,2,1), name",
02090 callback, &data, &zErrMsg);
02091 zShellStatic = 0;
02092 }
02093 }else{
02094 rc = sqlite3_exec(p->db,
02095 "SELECT sql FROM "
02096 " (SELECT sql sql, type type, tbl_name tbl_name, name name"
02097 " FROM sqlite_master UNION ALL"
02098 " SELECT sql, type, tbl_name, name FROM sqlite_temp_master) "
02099 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
02100 "ORDER BY substr(type,2,1), name",
02101 callback, &data, &zErrMsg
02102 );
02103 }
02104 if( zErrMsg ){
02105 fprintf(stderr,"Error: %s\n", zErrMsg);
02106 sqlite3_free(zErrMsg);
02107 rc = 1;
02108 }else if( rc != SQLITE_OK ){
02109 fprintf(stderr,"Error: querying schema information\n");
02110 rc = 1;
02111 }else{
02112 rc = 0;
02113 }
02114 }else
02115
02116 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
02117 sqlite3_snprintf(sizeof(p->separator), p->separator,
02118 "%.*s", (int)sizeof(p->separator)-1, azArg[1]);
02119 }else
02120
02121 if( c=='s' && strncmp(azArg[0], "show", n)==0 && nArg==1 ){
02122 int i;
02123 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
02124 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
02125 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
02126 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
02127 fprintf(p->out,"%9.9s: ", "nullvalue");
02128 output_c_string(p->out, p->nullvalue);
02129 fprintf(p->out, "\n");
02130 fprintf(p->out,"%9.9s: %s\n","output",
02131 strlen30(p->outfile) ? p->outfile : "stdout");
02132 fprintf(p->out,"%9.9s: ", "separator");
02133 output_c_string(p->out, p->separator);
02134 fprintf(p->out, "\n");
02135 fprintf(p->out,"%9.9s: %s\n","stats", p->statsOn ? "on" : "off");
02136 fprintf(p->out,"%9.9s: ","width");
02137 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
02138 fprintf(p->out,"%d ",p->colWidth[i]);
02139 }
02140 fprintf(p->out,"\n");
02141 }else
02142
02143 if( c=='s' && strncmp(azArg[0], "stats", n)==0 && nArg>1 && nArg<3 ){
02144 p->statsOn = booleanValue(azArg[1]);
02145 }else
02146
02147 if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 && nArg<3 ){
02148 char **azResult;
02149 int nRow;
02150 char *zErrMsg;
02151 open_db(p);
02152 if( nArg==1 ){
02153 rc = sqlite3_get_table(p->db,
02154 "SELECT name FROM sqlite_master "
02155 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' "
02156 "UNION ALL "
02157 "SELECT name FROM sqlite_temp_master "
02158 "WHERE type IN ('table','view') "
02159 "ORDER BY 1",
02160 &azResult, &nRow, 0, &zErrMsg
02161 );
02162 }else{
02163 zShellStatic = azArg[1];
02164 rc = sqlite3_get_table(p->db,
02165 "SELECT name FROM sqlite_master "
02166 "WHERE type IN ('table','view') AND name LIKE shellstatic() "
02167 "UNION ALL "
02168 "SELECT name FROM sqlite_temp_master "
02169 "WHERE type IN ('table','view') AND name LIKE shellstatic() "
02170 "ORDER BY 1",
02171 &azResult, &nRow, 0, &zErrMsg
02172 );
02173 zShellStatic = 0;
02174 }
02175 if( zErrMsg ){
02176 fprintf(stderr,"Error: %s\n", zErrMsg);
02177 sqlite3_free(zErrMsg);
02178 rc = 1;
02179 }else if( rc != SQLITE_OK ){
02180 fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n");
02181 rc = 1;
02182 }else{
02183 int len, maxlen = 0;
02184 int i, j;
02185 int nPrintCol, nPrintRow;
02186 for(i=1; i<=nRow; i++){
02187 if( azResult[i]==0 ) continue;
02188 len = strlen30(azResult[i]);
02189 if( len>maxlen ) maxlen = len;
02190 }
02191 nPrintCol = 80/(maxlen+2);
02192 if( nPrintCol<1 ) nPrintCol = 1;
02193 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
02194 for(i=0; i<nPrintRow; i++){
02195 for(j=i+1; j<=nRow; j+=nPrintRow){
02196 char *zSp = j<=nPrintRow ? "" : " ";
02197 printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
02198 }
02199 printf("\n");
02200 }
02201 }
02202 sqlite3_free_table(azResult);
02203 }else
02204
02205 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
02206 static const struct {
02207 const char *zCtrlName;
02208 int ctrlCode;
02209 } aCtrl[] = {
02210 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE },
02211 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE },
02212 { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET },
02213 { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST },
02214 { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL },
02215 { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS },
02216 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE },
02217 { "assert", SQLITE_TESTCTRL_ASSERT },
02218 { "always", SQLITE_TESTCTRL_ALWAYS },
02219 { "reserve", SQLITE_TESTCTRL_RESERVE },
02220 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS },
02221 { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD },
02222 { "pghdrsz", SQLITE_TESTCTRL_PGHDRSZ },
02223 { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC },
02224 };
02225 int testctrl = -1;
02226 int rc = 0;
02227 int i, n;
02228 open_db(p);
02229
02230
02231
02232 n = strlen30(azArg[1]);
02233 for(i=0; i<(int)(sizeof(aCtrl)/sizeof(aCtrl[0])); i++){
02234 if( strncmp(azArg[1], aCtrl[i].zCtrlName, n)==0 ){
02235 if( testctrl<0 ){
02236 testctrl = aCtrl[i].ctrlCode;
02237 }else{
02238 fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
02239 testctrl = -1;
02240 break;
02241 }
02242 }
02243 }
02244 if( testctrl<0 ) testctrl = atoi(azArg[1]);
02245 if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){
02246 fprintf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
02247 }else{
02248 switch(testctrl){
02249
02250
02251 case SQLITE_TESTCTRL_OPTIMIZATIONS:
02252 case SQLITE_TESTCTRL_RESERVE:
02253 if( nArg==3 ){
02254 int opt = (int)strtol(azArg[2], 0, 0);
02255 rc = sqlite3_test_control(testctrl, p->db, opt);
02256 printf("%d (0x%08x)\n", rc, rc);
02257 } else {
02258 fprintf(stderr,"Error: testctrl %s takes a single int option\n",
02259 azArg[1]);
02260 }
02261 break;
02262
02263
02264 case SQLITE_TESTCTRL_PRNG_SAVE:
02265 case SQLITE_TESTCTRL_PRNG_RESTORE:
02266 case SQLITE_TESTCTRL_PRNG_RESET:
02267 case SQLITE_TESTCTRL_PGHDRSZ:
02268 if( nArg==2 ){
02269 rc = sqlite3_test_control(testctrl);
02270 printf("%d (0x%08x)\n", rc, rc);
02271 } else {
02272 fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]);
02273 }
02274 break;
02275
02276
02277 case SQLITE_TESTCTRL_PENDING_BYTE:
02278 if( nArg==3 ){
02279 unsigned int opt = (unsigned int)atoi(azArg[2]);
02280 rc = sqlite3_test_control(testctrl, opt);
02281 printf("%d (0x%08x)\n", rc, rc);
02282 } else {
02283 fprintf(stderr,"Error: testctrl %s takes a single unsigned"
02284 " int option\n", azArg[1]);
02285 }
02286 break;
02287
02288
02289 case SQLITE_TESTCTRL_ASSERT:
02290 case SQLITE_TESTCTRL_ALWAYS:
02291 if( nArg==3 ){
02292 int opt = atoi(azArg[2]);
02293 rc = sqlite3_test_control(testctrl, opt);
02294 printf("%d (0x%08x)\n", rc, rc);
02295 } else {
02296 fprintf(stderr,"Error: testctrl %s takes a single int option\n",
02297 azArg[1]);
02298 }
02299 break;
02300
02301
02302 #ifdef SQLITE_N_KEYWORD
02303 case SQLITE_TESTCTRL_ISKEYWORD:
02304 if( nArg==3 ){
02305 const char *opt = azArg[2];
02306 rc = sqlite3_test_control(testctrl, opt);
02307 printf("%d (0x%08x)\n", rc, rc);
02308 } else {
02309 fprintf(stderr,"Error: testctrl %s takes a single char * option\n",
02310 azArg[1]);
02311 }
02312 break;
02313 #endif
02314
02315 case SQLITE_TESTCTRL_BITVEC_TEST:
02316 case SQLITE_TESTCTRL_FAULT_INSTALL:
02317 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
02318 case SQLITE_TESTCTRL_SCRATCHMALLOC:
02319 default:
02320 fprintf(stderr,"Error: CLI support for testctrl %s not implemented\n",
02321 azArg[1]);
02322 break;
02323 }
02324 }
02325 }else
02326
02327 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 && nArg==2 ){
02328 open_db(p);
02329 sqlite3_busy_timeout(p->db, atoi(azArg[1]));
02330 }else
02331
02332 if( HAS_TIMER && c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0
02333 && nArg==2
02334 ){
02335 enableTimer = booleanValue(azArg[1]);
02336 }else
02337
02338 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
02339 printf("SQLite %s %s\n",
02340 sqlite3_libversion(), sqlite3_sourceid());
02341 }else
02342
02343 if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
02344 int j;
02345 assert( nArg<=ArraySize(azArg) );
02346 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
02347 p->colWidth[j-1] = atoi(azArg[j]);
02348 }
02349 }else
02350
02351 {
02352 fprintf(stderr, "Error: unknown command or invalid arguments: "
02353 " \"%s\". Enter \".help\" for help\n", azArg[0]);
02354 rc = 1;
02355 }
02356
02357 return rc;
02358 }
02359
02360
02361
02362
02363
02364 static int _contains_semicolon(const char *z, int N){
02365 int i;
02366 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
02367 return 0;
02368 }
02369
02370
02371
02372
02373 static int _all_whitespace(const char *z){
02374 for(; *z; z++){
02375 if( IsSpace(z[0]) ) continue;
02376 if( *z=='/' && z[1]=='*' ){
02377 z += 2;
02378 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
02379 if( *z==0 ) return 0;
02380 z++;
02381 continue;
02382 }
02383 if( *z=='-' && z[1]=='-' ){
02384 z += 2;
02385 while( *z && *z!='\n' ){ z++; }
02386 if( *z==0 ) return 1;
02387 continue;
02388 }
02389 return 0;
02390 }
02391 return 1;
02392 }
02393
02394
02395
02396
02397
02398
02399 static int _is_command_terminator(const char *zLine){
02400 while( IsSpace(zLine[0]) ){ zLine++; };
02401 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
02402 return 1;
02403 }
02404 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
02405 && _all_whitespace(&zLine[2]) ){
02406 return 1;
02407 }
02408 return 0;
02409 }
02410
02411
02412
02413
02414
02415 static int _is_complete(char *zSql, int nSql){
02416 int rc;
02417 if( zSql==0 ) return 1;
02418 zSql[nSql] = ';';
02419 zSql[nSql+1] = 0;
02420 rc = sqlite3_complete(zSql);
02421 zSql[nSql] = 0;
02422 return rc;
02423 }
02424
02425
02426
02427
02428
02429
02430
02431
02432
02433
02434 static int process_input(struct callback_data *p, FILE *in){
02435 char *zLine = 0;
02436 char *zSql = 0;
02437 int nSql = 0;
02438 int nSqlPrior = 0;
02439 char *zErrMsg;
02440 int rc;
02441 int errCnt = 0;
02442 int lineno = 0;
02443 int startline = 0;
02444
02445 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
02446 fflush(p->out);
02447 free(zLine);
02448 zLine = one_input_line(zSql, in);
02449 if( zLine==0 ){
02450 break;
02451 }
02452 if( seenInterrupt ){
02453 if( in!=0 ) break;
02454 seenInterrupt = 0;
02455 }
02456 lineno++;
02457 if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
02458 if( zLine && zLine[0]=='.' && nSql==0 ){
02459 if( p->echoOn ) printf("%s\n", zLine);
02460 rc = do_meta_command(zLine, p);
02461 if( rc==2 ){
02462 break;
02463 }else if( rc ){
02464 errCnt++;
02465 }
02466 continue;
02467 }
02468 if( _is_command_terminator(zLine) && _is_complete(zSql, nSql) ){
02469 memcpy(zLine,";",2);
02470 }
02471 nSqlPrior = nSql;
02472 if( zSql==0 ){
02473 int i;
02474 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
02475 if( zLine[i]!=0 ){
02476 nSql = strlen30(zLine);
02477 zSql = malloc( nSql+3 );
02478 if( zSql==0 ){
02479 fprintf(stderr, "Error: out of memory\n");
02480 exit(1);
02481 }
02482 memcpy(zSql, zLine, nSql+1);
02483 startline = lineno;
02484 }
02485 }else{
02486 int len = strlen30(zLine);
02487 zSql = realloc( zSql, nSql + len + 4 );
02488 if( zSql==0 ){
02489 fprintf(stderr,"Error: out of memory\n");
02490 exit(1);
02491 }
02492 zSql[nSql++] = '\n';
02493 memcpy(&zSql[nSql], zLine, len+1);
02494 nSql += len;
02495 }
02496 if( zSql && _contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
02497 && sqlite3_complete(zSql) ){
02498 p->cnt = 0;
02499 open_db(p);
02500 BEGIN_TIMER;
02501 rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
02502 END_TIMER;
02503 if( rc || zErrMsg ){
02504 char zPrefix[100];
02505 if( in!=0 || !stdin_is_interactive ){
02506 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
02507 "Error: near line %d:", startline);
02508 }else{
02509 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
02510 }
02511 if( zErrMsg!=0 ){
02512 fprintf(stderr, "%s %s\n", zPrefix, zErrMsg);
02513 sqlite3_free(zErrMsg);
02514 zErrMsg = 0;
02515 }else{
02516 fprintf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
02517 }
02518 errCnt++;
02519 }
02520 free(zSql);
02521 zSql = 0;
02522 nSql = 0;
02523 }
02524 }
02525 if( zSql ){
02526 if( !_all_whitespace(zSql) ){
02527 fprintf(stderr, "Error: incomplete SQL: %s\n", zSql);
02528 }
02529 free(zSql);
02530 }
02531 free(zLine);
02532 return errCnt;
02533 }
02534
02535
02536
02537
02538
02539
02540
02541 static char *find_home_dir(void){
02542 char *home_dir = NULL;
02543
02544 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
02545 struct passwd *pwent;
02546 uid_t uid = getuid();
02547 if( (pwent=getpwuid(uid)) != NULL) {
02548 home_dir = pwent->pw_dir;
02549 }
02550 #endif
02551
02552 #if defined(_WIN32_WCE)
02553
02554
02555 home_dir = strdup("/");
02556 #else
02557
02558 #if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
02559 if (!home_dir) {
02560 home_dir = getenv("USERPROFILE");
02561 }
02562 #endif
02563
02564 if (!home_dir) {
02565 home_dir = getenv("HOME");
02566 }
02567
02568 #if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
02569 if (!home_dir) {
02570 char *zDrive, *zPath;
02571 int n;
02572 zDrive = getenv("HOMEDRIVE");
02573 zPath = getenv("HOMEPATH");
02574 if( zDrive && zPath ){
02575 n = strlen30(zDrive) + strlen30(zPath) + 1;
02576 home_dir = malloc( n );
02577 if( home_dir==0 ) return 0;
02578 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
02579 return home_dir;
02580 }
02581 home_dir = "c:\\";
02582 }
02583 #endif
02584
02585 #endif
02586
02587 if( home_dir ){
02588 int n = strlen30(home_dir) + 1;
02589 char *z = malloc( n );
02590 if( z ) memcpy(z, home_dir, n);
02591 home_dir = z;
02592 }
02593
02594 return home_dir;
02595 }
02596
02597
02598
02599
02600
02601
02602
02603 static int process_sqliterc(
02604 struct callback_data *p,
02605 const char *sqliterc_override
02606 ){
02607 char *home_dir = NULL;
02608 const char *sqliterc = sqliterc_override;
02609 char *zBuf = 0;
02610 FILE *in = NULL;
02611 int nBuf;
02612 int rc = 0;
02613
02614 if (sqliterc == NULL) {
02615 home_dir = find_home_dir();
02616 if( home_dir==0 ){
02617 #if !defined(__RTP__) && !defined(_WRS_KERNEL)
02618 fprintf(stderr,"%s: Error: cannot locate your home directory\n", Argv0);
02619 #endif
02620 return 1;
02621 }
02622 nBuf = strlen30(home_dir) + 16;
02623 zBuf = malloc( nBuf );
02624 if( zBuf==0 ){
02625 fprintf(stderr,"%s: Error: out of memory\n",Argv0);
02626 return 1;
02627 }
02628 sqlite3_snprintf(nBuf, zBuf,"%s/.sqliterc",home_dir);
02629 free(home_dir);
02630 sqliterc = (const char*)zBuf;
02631 }
02632 in = fopen(sqliterc,"rb");
02633 if( in ){
02634 if( stdin_is_interactive ){
02635 fprintf(stderr,"-- Loading resources from %s\n",sqliterc);
02636 }
02637 rc = process_input(p,in);
02638 fclose(in);
02639 }
02640 free(zBuf);
02641 return rc;
02642 }
02643
02644
02645
02646
02647 static const char zOptions[] =
02648 " -help show this message\n"
02649 " -init filename read/process named file\n"
02650 " -echo print commands before execution\n"
02651 " -[no]header turn headers on or off\n"
02652 " -bail stop after hitting an error\n"
02653 " -interactive force interactive I/O\n"
02654 " -batch force batch I/O\n"
02655 " -column set output mode to 'column'\n"
02656 " -csv set output mode to 'csv'\n"
02657 " -html set output mode to HTML\n"
02658 " -line set output mode to 'line'\n"
02659 " -list set output mode to 'list'\n"
02660 " -separator 'x' set output field separator (|)\n"
02661 " -stats print memory stats before each finalize\n"
02662 " -nullvalue 'text' set text string for NULL values\n"
02663 " -version show SQLite version\n"
02664 " -vfs NAME use NAME as the default VFS\n"
02665 #ifdef SQLITE_ENABLE_VFSTRACE
02666 " -vfstrace enable tracing of all VFS calls\n"
02667 #endif
02668 #ifdef SQLITE_ENABLE_MULTIPLEX
02669 " -multiplex enable the multiplexor VFS\n"
02670 #endif
02671 ;
02672 static void usage(int showDetail){
02673 fprintf(stderr,
02674 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
02675 "FILENAME is the name of an SQLite database. A new database is created\n"
02676 "if the file does not previously exist.\n", Argv0);
02677 if( showDetail ){
02678 fprintf(stderr, "OPTIONS include:\n%s", zOptions);
02679 }else{
02680 fprintf(stderr, "Use the -help option for additional information\n");
02681 }
02682 exit(1);
02683 }
02684
02685
02686
02687
02688 static void main_init(struct callback_data *data) {
02689 memset(data, 0, sizeof(*data));
02690 data->mode = MODE_List;
02691 memcpy(data->separator,"|", 2);
02692 data->showHeader = 0;
02693 sqlite3_config(SQLITE_CONFIG_URI, 1);
02694 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
02695 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
02696 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
02697 sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
02698 }
02699
02700 int main(int argc, char **argv){
02701 char *zErrMsg = 0;
02702 struct callback_data data;
02703 const char *zInitFile = 0;
02704 char *zFirstCmd = 0;
02705 int i;
02706 int rc = 0;
02707
02708 if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){
02709 fprintf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
02710 sqlite3_sourceid(), SQLITE_SOURCE_ID);
02711 exit(1);
02712 }
02713 Argv0 = argv[0];
02714 main_init(&data);
02715 stdin_is_interactive = isatty(0);
02716
02717
02718
02719
02720 #ifdef SIGINT
02721 signal(SIGINT, interrupt_handler);
02722 #endif
02723
02724
02725
02726
02727
02728
02729 for(i=1; i<argc-1; i++){
02730 char *z;
02731 if( argv[i][0]!='-' ) break;
02732 z = argv[i];
02733 if( z[0]=='-' && z[1]=='-' ) z++;
02734 if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){
02735 i++;
02736 }else if( strcmp(argv[i],"-init")==0 ){
02737 i++;
02738 zInitFile = argv[i];
02739
02740
02741
02742
02743 }else if( strcmp(argv[i],"-batch")==0 ){
02744 stdin_is_interactive = 0;
02745 }else if( strcmp(argv[i],"-heap")==0 ){
02746 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
02747 int j, c;
02748 const char *zSize;
02749 sqlite3_int64 szHeap;
02750
02751 zSize = argv[++i];
02752 szHeap = atoi(zSize);
02753 for(j=0; (c = zSize[j])!=0; j++){
02754 if( c=='M' ){ szHeap *= 1000000; break; }
02755 if( c=='K' ){ szHeap *= 1000; break; }
02756 if( c=='G' ){ szHeap *= 1000000000; break; }
02757 }
02758 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
02759 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
02760 #endif
02761 #ifdef SQLITE_ENABLE_VFSTRACE
02762 }else if( strcmp(argv[i],"-vfstrace")==0 ){
02763 extern int vfstrace_register(
02764 const char *zTraceName,
02765 const char *zOldVfsName,
02766 int (*xOut)(const char*,void*),
02767 void *pOutArg,
02768 int makeDefault
02769 );
02770 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
02771 #endif
02772 #ifdef SQLITE_ENABLE_MULTIPLEX
02773 }else if( strcmp(argv[i],"-multiplex")==0 ){
02774 extern int sqlite3_multiple_initialize(const char*,int);
02775 sqlite3_multiplex_initialize(0, 1);
02776 #endif
02777 }else if( strcmp(argv[i],"-vfs")==0 ){
02778 sqlite3_vfs *pVfs = sqlite3_vfs_find(argv[++i]);
02779 if( pVfs ){
02780 sqlite3_vfs_register(pVfs, 1);
02781 }else{
02782 fprintf(stderr, "no such VFS: \"%s\"\n", argv[i]);
02783 exit(1);
02784 }
02785 }
02786 }
02787 if( i<argc ){
02788 #if defined(SQLITE_OS_OS2) && SQLITE_OS_OS2
02789 data.zDbFilename = (const char *)convertCpPathToUtf8( argv[i++] );
02790 #else
02791 data.zDbFilename = argv[i++];
02792 #endif
02793 }else{
02794 #ifndef SQLITE_OMIT_MEMORYDB
02795 data.zDbFilename = ":memory:";
02796 #else
02797 data.zDbFilename = 0;
02798 #endif
02799 }
02800 if( i<argc ){
02801 zFirstCmd = argv[i++];
02802 }
02803 if( i<argc ){
02804 fprintf(stderr,"%s: Error: too many options: \"%s\"\n", Argv0, argv[i]);
02805 fprintf(stderr,"Use -help for a list of options.\n");
02806 return 1;
02807 }
02808 data.out = stdout;
02809
02810 #ifdef SQLITE_OMIT_MEMORYDB
02811 if( data.zDbFilename==0 ){
02812 fprintf(stderr,"%s: Error: no database filename specified\n", Argv0);
02813 return 1;
02814 }
02815 #endif
02816
02817
02818
02819
02820
02821
02822 if( access(data.zDbFilename, 0)==0 ){
02823 open_db(&data);
02824 }
02825
02826
02827
02828
02829
02830 rc = process_sqliterc(&data,zInitFile);
02831 if( rc>0 ){
02832 return rc;
02833 }
02834
02835
02836
02837
02838
02839
02840 for(i=1; i<argc && argv[i][0]=='-'; i++){
02841 char *z = argv[i];
02842 if( z[1]=='-' ){ z++; }
02843 if( strcmp(z,"-init")==0 ){
02844 i++;
02845 }else if( strcmp(z,"-html")==0 ){
02846 data.mode = MODE_Html;
02847 }else if( strcmp(z,"-list")==0 ){
02848 data.mode = MODE_List;
02849 }else if( strcmp(z,"-line")==0 ){
02850 data.mode = MODE_Line;
02851 }else if( strcmp(z,"-column")==0 ){
02852 data.mode = MODE_Column;
02853 }else if( strcmp(z,"-csv")==0 ){
02854 data.mode = MODE_Csv;
02855 memcpy(data.separator,",",2);
02856 }else if( strcmp(z,"-separator")==0 ){
02857 i++;
02858 if(i>=argc){
02859 fprintf(stderr,"%s: Error: missing argument for option: %s\n", Argv0, z);
02860 fprintf(stderr,"Use -help for a list of options.\n");
02861 return 1;
02862 }
02863 sqlite3_snprintf(sizeof(data.separator), data.separator,
02864 "%.*s",(int)sizeof(data.separator)-1,argv[i]);
02865 }else if( strcmp(z,"-nullvalue")==0 ){
02866 i++;
02867 if(i>=argc){
02868 fprintf(stderr,"%s: Error: missing argument for option: %s\n", Argv0, z);
02869 fprintf(stderr,"Use -help for a list of options.\n");
02870 return 1;
02871 }
02872 sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue,
02873 "%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
02874 }else if( strcmp(z,"-header")==0 ){
02875 data.showHeader = 1;
02876 }else if( strcmp(z,"-noheader")==0 ){
02877 data.showHeader = 0;
02878 }else if( strcmp(z,"-echo")==0 ){
02879 data.echoOn = 1;
02880 }else if( strcmp(z,"-stats")==0 ){
02881 data.statsOn = 1;
02882 }else if( strcmp(z,"-bail")==0 ){
02883 bail_on_error = 1;
02884 }else if( strcmp(z,"-version")==0 ){
02885 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
02886 return 0;
02887 }else if( strcmp(z,"-interactive")==0 ){
02888 stdin_is_interactive = 1;
02889 }else if( strcmp(z,"-batch")==0 ){
02890 stdin_is_interactive = 0;
02891 }else if( strcmp(z,"-heap")==0 ){
02892 i++;
02893 }else if( strcmp(z,"-vfs")==0 ){
02894 i++;
02895 #ifdef SQLITE_ENABLE_VFSTRACE
02896 }else if( strcmp(z,"-vfstrace")==0 ){
02897 i++;
02898 #endif
02899 #ifdef SQLITE_ENABLE_MULTIPLEX
02900 }else if( strcmp(z,"-multiplex")==0 ){
02901 i++;
02902 #endif
02903 }else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){
02904 usage(1);
02905 }else{
02906 fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
02907 fprintf(stderr,"Use -help for a list of options.\n");
02908 return 1;
02909 }
02910 }
02911
02912 if( zFirstCmd ){
02913
02914
02915 if( zFirstCmd[0]=='.' ){
02916 rc = do_meta_command(zFirstCmd, &data);
02917 }else{
02918 open_db(&data);
02919 rc = shell_exec(data.db, zFirstCmd, shell_callback, &data, &zErrMsg);
02920 if( zErrMsg!=0 ){
02921 fprintf(stderr,"Error: %s\n", zErrMsg);
02922 return rc!=0 ? rc : 1;
02923 }else if( rc!=0 ){
02924 fprintf(stderr,"Error: unable to process SQL \"%s\"\n", zFirstCmd);
02925 return rc;
02926 }
02927 }
02928 }else{
02929
02930
02931 if( stdin_is_interactive ){
02932 char *zHome;
02933 char *zHistory = 0;
02934 int nHistory;
02935 printf(
02936 "SQLite version %s %.19s\n"
02937 "Enter \".help\" for instructions\n"
02938 "Enter SQL statements terminated with a \";\"\n",
02939 sqlite3_libversion(), sqlite3_sourceid()
02940 );
02941 zHome = find_home_dir();
02942 if( zHome ){
02943 nHistory = strlen30(zHome) + 20;
02944 if( (zHistory = malloc(nHistory))!=0 ){
02945 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
02946 }
02947 }
02948 #if defined(HAVE_READLINE) && HAVE_READLINE==1
02949 if( zHistory ) read_history(zHistory);
02950 #endif
02951 rc = process_input(&data, 0);
02952 if( zHistory ){
02953 stifle_history(100);
02954 write_history(zHistory);
02955 free(zHistory);
02956 }
02957 free(zHome);
02958 }else{
02959 rc = process_input(&data, stdin);
02960 }
02961 }
02962 set_table_name(&data, 0);
02963 if( data.db ){
02964 sqlite3_close(data.db);
02965 }
02966 return rc;
02967 }