Fossil SCM

Update the built-in SQLite to the latest 3.22.0 beta, for testing.

drh 2018-01-11 00:48 trunk
Commit c409f828e3da5ba2f734631b1d53e8d167556fcc7b737d34d4853697402d5cb3
3 files changed +691 -371 +29 -21 +1 -1
+691 -371
--- src/shell.c
+++ src/shell.c
@@ -368,10 +368,15 @@
368368
/*
369369
** Used to prevent warnings about unused parameters
370370
*/
371371
#define UNUSED_PARAMETER(x) (void)(x)
372372
373
+/*
374
+** Number of elements in an array
375
+*/
376
+#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
377
+
373378
/*
374379
** If the following flag is set, then command execution stops
375380
** at an error if we are not interactive.
376381
*/
377382
static int bail_on_error = 0;
@@ -640,10 +645,69 @@
640645
if( zResult && *zResult ) shell_add_history(zResult);
641646
#endif
642647
}
643648
return zResult;
644649
}
650
+
651
+
652
+/*
653
+** Return the value of a hexadecimal digit. Return -1 if the input
654
+** is not a hex digit.
655
+*/
656
+static int hexDigitValue(char c){
657
+ if( c>='0' && c<='9' ) return c - '0';
658
+ if( c>='a' && c<='f' ) return c - 'a' + 10;
659
+ if( c>='A' && c<='F' ) return c - 'A' + 10;
660
+ return -1;
661
+}
662
+
663
+/*
664
+** Interpret zArg as an integer value, possibly with suffixes.
665
+*/
666
+static sqlite3_int64 integerValue(const char *zArg){
667
+ sqlite3_int64 v = 0;
668
+ static const struct { char *zSuffix; int iMult; } aMult[] = {
669
+ { "KiB", 1024 },
670
+ { "MiB", 1024*1024 },
671
+ { "GiB", 1024*1024*1024 },
672
+ { "KB", 1000 },
673
+ { "MB", 1000000 },
674
+ { "GB", 1000000000 },
675
+ { "K", 1000 },
676
+ { "M", 1000000 },
677
+ { "G", 1000000000 },
678
+ };
679
+ int i;
680
+ int isNeg = 0;
681
+ if( zArg[0]=='-' ){
682
+ isNeg = 1;
683
+ zArg++;
684
+ }else if( zArg[0]=='+' ){
685
+ zArg++;
686
+ }
687
+ if( zArg[0]=='0' && zArg[1]=='x' ){
688
+ int x;
689
+ zArg += 2;
690
+ while( (x = hexDigitValue(zArg[0]))>=0 ){
691
+ v = (v<<4) + x;
692
+ zArg++;
693
+ }
694
+ }else{
695
+ while( IsDigit(zArg[0]) ){
696
+ v = v*10 + zArg[0] - '0';
697
+ zArg++;
698
+ }
699
+ }
700
+ for(i=0; i<ArraySize(aMult); i++){
701
+ if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
702
+ v *= aMult[i].iMult;
703
+ break;
704
+ }
705
+ }
706
+ return isNeg? -v : v;
707
+}
708
+
645709
/*
646710
** A variable length string to which one can append text.
647711
*/
648712
typedef struct ShellText ShellText;
649713
struct ShellText {
@@ -2334,10 +2398,44 @@
23342398
}else{
23352399
ctxErrorMsg(context, "failed to write file: %s", zFile);
23362400
}
23372401
}
23382402
}
2403
+
2404
+/*
2405
+** SQL function: lsmode(MODE)
2406
+**
2407
+** Given a numberic st_mode from stat(), convert it into a human-readable
2408
+** text string in the style of "ls -l".
2409
+*/
2410
+static void lsModeFunc(
2411
+ sqlite3_context *context,
2412
+ int argc,
2413
+ sqlite3_value **argv
2414
+){
2415
+ int i;
2416
+ int iMode = sqlite3_value_int(argv[0]);
2417
+ char z[16];
2418
+ if( S_ISLNK(iMode) ){
2419
+ z[0] = 'l';
2420
+ }else if( S_ISREG(iMode) ){
2421
+ z[0] = '-';
2422
+ }else if( S_ISDIR(iMode) ){
2423
+ z[0] = 'd';
2424
+ }else{
2425
+ z[0] = '?';
2426
+ }
2427
+ for(i=0; i<3; i++){
2428
+ int m = (iMode >> ((2-i)*3));
2429
+ char *a = &z[1 + i*3];
2430
+ a[0] = (m & 0x4) ? 'r' : '-';
2431
+ a[1] = (m & 0x2) ? 'w' : '-';
2432
+ a[2] = (m & 0x1) ? 'x' : '-';
2433
+ }
2434
+ z[10] = '\0';
2435
+ sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2436
+}
23392437
23402438
#ifndef SQLITE_OMIT_VIRTUALTABLE
23412439
23422440
/*
23432441
** Cursor type for recursively iterating through a directory structure.
@@ -2746,10 +2844,14 @@
27462844
readfileFunc, 0, 0);
27472845
if( rc==SQLITE_OK ){
27482846
rc = sqlite3_create_function(db, "writefile", -1, SQLITE_UTF8, 0,
27492847
writefileFunc, 0, 0);
27502848
}
2849
+ if( rc==SQLITE_OK ){
2850
+ rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
2851
+ lsModeFunc, 0, 0);
2852
+ }
27512853
if( rc==SQLITE_OK ){
27522854
rc = fsdirRegister(db);
27532855
}
27542856
return rc;
27552857
}
@@ -4879,11 +4981,11 @@
48794981
if( pNew==0 ){
48804982
rc = SQLITE_NOMEM;
48814983
}else{
48824984
memset(pNew, 0, sizeof(ZipfileEntry));
48834985
pNew->zPath = (char*)&pNew[1];
4884
- memcpy(pNew->zPath, &aBuf[ZIPFILE_CDS_FIXED_SZ], nFile);
4986
+ memcpy(pNew->zPath, &aRec[ZIPFILE_CDS_FIXED_SZ], nFile);
48854987
pNew->zPath[nFile] = '\0';
48864988
pNew->aCdsEntry = (u8*)&pNew->zPath[nFile+1];
48874989
pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
48884990
memcpy(pNew->aCdsEntry, aRec, pNew->nCdsEntry);
48894991
zipfileAddEntry(pTab, pNew);
@@ -5034,10 +5136,22 @@
50345136
50355137
parse_error:
50365138
pTab->base.zErrMsg = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
50375139
return SQLITE_ERROR;
50385140
}
5141
+
5142
+/*
5143
+** Both (const char*) arguments point to nul-terminated strings. Argument
5144
+** nB is the value of strlen(zB). This function returns 0 if the strings are
5145
+** identical, ignoring any trailing '/' character in either path. */
5146
+static int zipfileComparePath(const char *zA, const char *zB, int nB){
5147
+ int nA = (int)strlen(zA);
5148
+ if( zA[nA-1]=='/' ) nA--;
5149
+ if( zB[nB-1]=='/' ) nB--;
5150
+ if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5151
+ return 1;
5152
+}
50395153
50405154
/*
50415155
** xUpdate method.
50425156
*/
50435157
static int zipfileUpdate(
@@ -5048,19 +5162,20 @@
50485162
){
50495163
ZipfileTab *pTab = (ZipfileTab*)pVtab;
50505164
int rc = SQLITE_OK; /* Return Code */
50515165
ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
50525166
5053
- u32 mode; /* Mode for new entry */
5054
- i64 mTime; /* Modification time for new entry */
5167
+ u32 mode = 0; /* Mode for new entry */
5168
+ i64 mTime = 0; /* Modification time for new entry */
50555169
i64 sz = 0; /* Uncompressed size */
5056
- const char *zPath; /* Path for new entry */
5057
- int nPath; /* strlen(zPath) */
5170
+ const char *zPath = 0; /* Path for new entry */
5171
+ int nPath = 0; /* strlen(zPath) */
50585172
const u8 *pData = 0; /* Pointer to buffer containing content */
50595173
int nData = 0; /* Size of pData buffer in bytes */
50605174
int iMethod = 0; /* Compression method for new entry */
50615175
u8 *pFree = 0; /* Free this */
5176
+ char *zFree = 0; /* Also free this */
50625177
ZipfileCDS cds; /* New Central Directory Structure entry */
50635178
50645179
int bIsDir = 0;
50655180
50665181
int mNull;
@@ -5067,19 +5182,23 @@
50675182
50685183
assert( pTab->zFile );
50695184
assert( pTab->pWriteFd );
50705185
50715186
if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5072
- i64 iDelete = sqlite3_value_int64(apVal[0]);
5073
- ZipfileEntry *p;
5074
- for(p=pTab->pFirstEntry; p; p=p->pNext){
5075
- if( p->iRowid==iDelete ){
5076
- p->bDeleted = 1;
5077
- break;
5078
- }
5079
- }
5080
- if( nVal==1 ) return SQLITE_OK;
5187
+ if( nVal>1 ){
5188
+ return SQLITE_CONSTRAINT;
5189
+ }else{
5190
+ i64 iDelete = sqlite3_value_int64(apVal[0]);
5191
+ ZipfileEntry *p;
5192
+ for(p=pTab->pFirstEntry; p; p=p->pNext){
5193
+ if( p->iRowid==iDelete ){
5194
+ p->bDeleted = 1;
5195
+ break;
5196
+ }
5197
+ }
5198
+ return SQLITE_OK;
5199
+ }
50815200
}
50825201
50835202
mNull = (sqlite3_value_type(apVal[5])==SQLITE_NULL ? 0x0 : 0x8) /* sz */
50845203
+ (sqlite3_value_type(apVal[6])==SQLITE_NULL ? 0x0 : 0x4) /* rawdata */
50855204
+ (sqlite3_value_type(apVal[7])==SQLITE_NULL ? 0x0 : 0x2) /* data */
@@ -5148,10 +5267,34 @@
51485267
mTime = (sqlite3_int64)time(0);
51495268
}else{
51505269
mTime = sqlite3_value_int64(apVal[4]);
51515270
}
51525271
}
5272
+
5273
+ if( rc==SQLITE_OK && bIsDir ){
5274
+ /* For a directory, check that the last character in the path is a
5275
+ ** '/'. This appears to be required for compatibility with info-zip
5276
+ ** (the unzip command on unix). It does not create directories
5277
+ ** otherwise. */
5278
+ if( zPath[nPath-1]!='/' ){
5279
+ zFree = sqlite3_mprintf("%s/", zPath);
5280
+ if( zFree==0 ){ rc = SQLITE_NOMEM; }
5281
+ zPath = (const char*)zFree;
5282
+ nPath++;
5283
+ }
5284
+ }
5285
+
5286
+ /* Check that we're not inserting a duplicate entry */
5287
+ if( rc==SQLITE_OK ){
5288
+ ZipfileEntry *p;
5289
+ for(p=pTab->pFirstEntry; p; p=p->pNext){
5290
+ if( zipfileComparePath(p->zPath, zPath, nPath)==0 ){
5291
+ rc = SQLITE_CONSTRAINT;
5292
+ break;
5293
+ }
5294
+ }
5295
+ }
51535296
51545297
if( rc==SQLITE_OK ){
51555298
/* Create the new CDS record. */
51565299
memset(&cds, 0, sizeof(cds));
51575300
cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
@@ -5176,10 +5319,11 @@
51765319
if( rc==SQLITE_OK ){
51775320
rc = zipfileAppendEntry(pTab, &cds, zPath, nPath, pData, nData, (u32)mTime);
51785321
}
51795322
51805323
sqlite3_free(pFree);
5324
+ sqlite3_free(zFree);
51815325
return rc;
51825326
}
51835327
51845328
static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
51855329
u8 *aBuf = pTab->aBuffer;
@@ -5329,11 +5473,10 @@
53295473
){
53305474
SQLITE_EXTENSION_INIT2(pApi);
53315475
(void)pzErrMsg; /* Unused parameter */
53325476
return zipfileRegister(db);
53335477
}
5334
-
53355478
53365479
/************************* End ../ext/misc/zipfile.c ********************/
53375480
/************************* Begin ../ext/misc/sqlar.c ******************/
53385481
/*
53395482
** 2017-12-17
@@ -5642,10 +5785,12 @@
56425785
*************************************************************************
56435786
*/
56445787
#include <assert.h>
56455788
#include <string.h>
56465789
#include <stdio.h>
5790
+
5791
+#ifndef SQLITE_OMIT_VIRTUALTABLE
56475792
56485793
/* typedef sqlite3_int64 i64; */
56495794
/* typedef sqlite3_uint64 u64; */
56505795
56515796
typedef struct IdxColumn IdxColumn;
@@ -7151,10 +7296,11 @@
71517296
zCols = idxAppendText(&rc, zCols,
71527297
"%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
71537298
);
71547299
zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
71557300
}
7301
+ sqlite3_reset(pIndexXInfo);
71567302
if( rc==SQLITE_OK ){
71577303
if( p->iSample==100 ){
71587304
zQuery = sqlite3_mprintf(
71597305
"SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
71607306
);
@@ -7560,10 +7706,12 @@
75607706
idxHashClear(&p->hIdx);
75617707
sqlite3_free(p->zCandidates);
75627708
sqlite3_free(p);
75637709
}
75647710
}
7711
+
7712
+#endif /* ifndef SQLITE_OMIT_VIRTUAL_TABLE */
75657713
75667714
/************************* End ../ext/expert/sqlite3expert.c ********************/
75677715
75687716
#if defined(SQLITE_ENABLE_SESSION)
75697717
/*
@@ -7606,26 +7754,31 @@
76067754
u8 autoExplain; /* Automatically turn on .explain mode */
76077755
u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
76087756
u8 statsOn; /* True to display memory stats before each finalize */
76097757
u8 scanstatsOn; /* True to display scan stats before each finalize */
76107758
u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
7759
+ u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
76117760
int outCount; /* Revert to stdout when reaching zero */
76127761
int cnt; /* Number of records displayed so far */
76137762
FILE *out; /* Write results here */
76147763
FILE *traceOut; /* Output for sqlite3_trace() */
76157764
int nErr; /* Number of errors seen */
76167765
int mode; /* An output mode setting */
7766
+ int modePrior; /* Saved mode */
76177767
int cMode; /* temporary output mode for the current query */
76187768
int normalMode; /* Output mode before ".explain on" */
76197769
int writableSchema; /* True if PRAGMA writable_schema=ON */
76207770
int showHeader; /* True to show column names in List or Column mode */
76217771
int nCheck; /* Number of ".check" commands run */
76227772
unsigned shellFlgs; /* Various flags */
76237773
char *zDestTable; /* Name of destination table when MODE_Insert */
7774
+ char *zTempFile; /* Temporary file that might need deleting */
76247775
char zTestcase[30]; /* Name of current test case */
76257776
char colSeparator[20]; /* Column separator character for several modes */
76267777
char rowSeparator[20]; /* Row separator character for MODE_Ascii */
7778
+ char colSepPrior[20]; /* Saved column separator */
7779
+ char rowSepPrior[20]; /* Saved row separator */
76277780
int colWidth[100]; /* Requested width of each column when in column mode*/
76287781
int actualWidth[100]; /* Actual width of each column */
76297782
char nullValue[20]; /* The text to print when a NULL comes back from
76307783
** the database */
76317784
char outfile[FILENAME_MAX]; /* Filename for *out */
@@ -7719,24 +7872,175 @@
77197872
#define SEP_Comma ","
77207873
#define SEP_CrLf "\r\n"
77217874
#define SEP_Unit "\x1F"
77227875
#define SEP_Record "\x1E"
77237876
7724
-/*
7725
-** Number of elements in an array
7726
-*/
7727
-#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
7728
-
77297877
/*
77307878
** A callback for the sqlite3_log() interface.
77317879
*/
77327880
static void shellLog(void *pArg, int iErrCode, const char *zMsg){
77337881
ShellState *p = (ShellState*)pArg;
77347882
if( p->pLog==0 ) return;
77357883
utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
77367884
fflush(p->pLog);
77377885
}
7886
+
7887
+/*
7888
+** SQL function: shell_putsnl(X)
7889
+**
7890
+** Write the text X to the screen (or whatever output is being directed)
7891
+** adding a newline at the end, and then return X.
7892
+*/
7893
+static void shellPutsFunc(
7894
+ sqlite3_context *pCtx,
7895
+ int nVal,
7896
+ sqlite3_value **apVal
7897
+){
7898
+ ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
7899
+ utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
7900
+ sqlite3_result_value(pCtx, apVal[0]);
7901
+}
7902
+
7903
+/*
7904
+** SQL function: edit(VALUE)
7905
+** edit(VALUE,EDITOR)
7906
+**
7907
+** These steps:
7908
+**
7909
+** (1) Write VALUE into a temporary file.
7910
+** (2) Run program EDITOR on that temporary file.
7911
+** (3) Read the temporary file back and return its content as the result.
7912
+** (4) Delete the temporary file
7913
+**
7914
+** If the EDITOR argument is omitted, use the value in the VISUAL
7915
+** environment variable. If still there is no EDITOR, through an error.
7916
+**
7917
+** Also throw an error if the EDITOR program returns a non-zero exit code.
7918
+*/
7919
+static void editFunc(
7920
+ sqlite3_context *context,
7921
+ int argc,
7922
+ sqlite3_value **argv
7923
+){
7924
+ const char *zEditor;
7925
+ char *zTempFile = 0;
7926
+ sqlite3 *db;
7927
+ char *zCmd = 0;
7928
+ int bBin;
7929
+ int rc;
7930
+ FILE *f = 0;
7931
+ sqlite3_int64 sz;
7932
+ sqlite3_int64 x;
7933
+ unsigned char *p = 0;
7934
+
7935
+ if( argc==2 ){
7936
+ zEditor = (const char*)sqlite3_value_text(argv[1]);
7937
+ }else{
7938
+ zEditor = getenv("VISUAL");
7939
+ }
7940
+ if( zEditor==0 ){
7941
+ sqlite3_result_error(context, "no editor for edit()", -1);
7942
+ return;
7943
+ }
7944
+ if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
7945
+ sqlite3_result_error(context, "NULL input to edit()", -1);
7946
+ return;
7947
+ }
7948
+ db = sqlite3_context_db_handle(context);
7949
+ zTempFile = 0;
7950
+ sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
7951
+ if( zTempFile==0 ){
7952
+ sqlite3_uint64 r = 0;
7953
+ sqlite3_randomness(sizeof(r), &r);
7954
+ zTempFile = sqlite3_mprintf("temp%llx", r);
7955
+ if( zTempFile==0 ){
7956
+ sqlite3_result_error_nomem(context);
7957
+ return;
7958
+ }
7959
+ }
7960
+ bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
7961
+ f = fopen(zTempFile, bBin ? "wb" : "w");
7962
+ if( f==0 ){
7963
+ sqlite3_result_error(context, "edit() cannot open temp file", -1);
7964
+ goto edit_func_end;
7965
+ }
7966
+ sz = sqlite3_value_bytes(argv[0]);
7967
+ if( bBin ){
7968
+ x = fwrite(sqlite3_value_blob(argv[0]), 1, sz, f);
7969
+ }else{
7970
+ x = fwrite(sqlite3_value_text(argv[0]), 1, sz, f);
7971
+ }
7972
+ fclose(f);
7973
+ f = 0;
7974
+ if( x!=sz ){
7975
+ sqlite3_result_error(context, "edit() could not write the whole file", -1);
7976
+ goto edit_func_end;
7977
+ }
7978
+ zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
7979
+ if( zCmd==0 ){
7980
+ sqlite3_result_error_nomem(context);
7981
+ goto edit_func_end;
7982
+ }
7983
+ rc = system(zCmd);
7984
+ sqlite3_free(zCmd);
7985
+ if( rc ){
7986
+ sqlite3_result_error(context, "EDITOR returned non-zero", -1);
7987
+ goto edit_func_end;
7988
+ }
7989
+ f = fopen(zTempFile, bBin ? "rb" : "r");
7990
+ if( f==0 ){
7991
+ sqlite3_result_error(context,
7992
+ "edit() cannot reopen temp file after edit", -1);
7993
+ goto edit_func_end;
7994
+ }
7995
+ fseek(f, 0, SEEK_END);
7996
+ sz = ftell(f);
7997
+ rewind(f);
7998
+ p = sqlite3_malloc64( sz+(bBin==0) );
7999
+ if( p==0 ){
8000
+ sqlite3_result_error_nomem(context);
8001
+ goto edit_func_end;
8002
+ }
8003
+ if( bBin ){
8004
+ x = fread(p, 1, sz, f);
8005
+ }else{
8006
+ x = fread(p, 1, sz, f);
8007
+ p[sz] = 0;
8008
+ }
8009
+ fclose(f);
8010
+ f = 0;
8011
+ if( x!=sz ){
8012
+ sqlite3_result_error(context, "could not read back the whole file", -1);
8013
+ goto edit_func_end;
8014
+ }
8015
+ if( bBin ){
8016
+ sqlite3_result_blob(context, p, sz, sqlite3_free);
8017
+ }else{
8018
+ sqlite3_result_text(context, (const char*)p, sz, sqlite3_free);
8019
+ }
8020
+ p = 0;
8021
+
8022
+edit_func_end:
8023
+ if( f ) fclose(f);
8024
+ unlink(zTempFile);
8025
+ sqlite3_free(zTempFile);
8026
+ sqlite3_free(p);
8027
+}
8028
+
8029
+/*
8030
+** Save or restore the current output mode
8031
+*/
8032
+static void outputModePush(ShellState *p){
8033
+ p->modePrior = p->mode;
8034
+ memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
8035
+ memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
8036
+}
8037
+static void outputModePop(ShellState *p){
8038
+ p->mode = p->modePrior;
8039
+ memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
8040
+ memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
8041
+}
77388042
77398043
/*
77408044
** Output the given string as a hex-encoded blob (eg. X'1234' )
77418045
*/
77428046
static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
@@ -9058,10 +9362,11 @@
90589362
} while( rc == SQLITE_ROW );
90599363
}
90609364
}
90619365
}
90629366
9367
+#ifndef SQLITE_OMIT_VIRTUALTABLE
90639368
/*
90649369
** This function is called to process SQL if the previous shell command
90659370
** was ".expert". It passes the SQL in the second argument directly to
90669371
** the sqlite3expert object.
90679372
**
@@ -9130,10 +9435,67 @@
91309435
sqlite3_expert_destroy(p);
91319436
pState->expert.pExpert = 0;
91329437
return rc;
91339438
}
91349439
9440
+/*
9441
+** Implementation of ".expert" dot command.
9442
+*/
9443
+static int expertDotCommand(
9444
+ ShellState *pState, /* Current shell tool state */
9445
+ char **azArg, /* Array of arguments passed to dot command */
9446
+ int nArg /* Number of entries in azArg[] */
9447
+){
9448
+ int rc = SQLITE_OK;
9449
+ char *zErr = 0;
9450
+ int i;
9451
+ int iSample = 0;
9452
+
9453
+ assert( pState->expert.pExpert==0 );
9454
+ memset(&pState->expert, 0, sizeof(ExpertInfo));
9455
+
9456
+ for(i=1; rc==SQLITE_OK && i<nArg; i++){
9457
+ char *z = azArg[i];
9458
+ int n;
9459
+ if( z[0]=='-' && z[1]=='-' ) z++;
9460
+ n = strlen30(z);
9461
+ if( n>=2 && 0==strncmp(z, "-verbose", n) ){
9462
+ pState->expert.bVerbose = 1;
9463
+ }
9464
+ else if( n>=2 && 0==strncmp(z, "-sample", n) ){
9465
+ if( i==(nArg-1) ){
9466
+ raw_printf(stderr, "option requires an argument: %s\n", z);
9467
+ rc = SQLITE_ERROR;
9468
+ }else{
9469
+ iSample = (int)integerValue(azArg[++i]);
9470
+ if( iSample<0 || iSample>100 ){
9471
+ raw_printf(stderr, "value out of range: %s\n", azArg[i]);
9472
+ rc = SQLITE_ERROR;
9473
+ }
9474
+ }
9475
+ }
9476
+ else{
9477
+ raw_printf(stderr, "unknown option: %s\n", z);
9478
+ rc = SQLITE_ERROR;
9479
+ }
9480
+ }
9481
+
9482
+ if( rc==SQLITE_OK ){
9483
+ pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
9484
+ if( pState->expert.pExpert==0 ){
9485
+ raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
9486
+ rc = SQLITE_ERROR;
9487
+ }else{
9488
+ sqlite3_expert_config(
9489
+ pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
9490
+ );
9491
+ }
9492
+ }
9493
+
9494
+ return rc;
9495
+}
9496
+#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
91359497
91369498
/*
91379499
** Execute a statement or set of statements. Print
91389500
** any result rows/columns depending on the current mode
91399501
** set via the supplied callback.
@@ -9157,14 +9519,16 @@
91579519
91589520
if( pzErrMsg ){
91599521
*pzErrMsg = NULL;
91609522
}
91619523
9524
+#ifndef SQLITE_OMIT_VIRTUALTABLE
91629525
if( pArg->expert.pExpert ){
91639526
rc = expertHandleSQL(pArg, zSql, pzErrMsg);
91649527
return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
91659528
}
9529
+#endif
91669530
91679531
while( zSql[0] && (SQLITE_OK == rc) ){
91689532
static const char *zStmtSql;
91699533
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
91709534
if( SQLITE_OK != rc ){
@@ -9587,10 +9951,11 @@
95879951
".dump ?TABLE? ... Dump the database in an SQL text format\n"
95889952
" If TABLE specified, only dump tables matching\n"
95899953
" LIKE pattern TABLE.\n"
95909954
".echo on|off Turn command echo on or off\n"
95919955
".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
9956
+ ".excel Display the output of next command in a spreadsheet\n"
95929957
".exit Exit this program\n"
95939958
".expert EXPERIMENTAL. Suggest indexes for specified queries\n"
95949959
/* Because explain mode comes on automatically now, the ".explain" mode
95959960
** is removed from the help screen. It is still supported for legacy, however */
95969961
/*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
@@ -9624,14 +9989,16 @@
96249989
" list Values delimited by \"|\"\n"
96259990
" quote Escape answers as for SQL\n"
96269991
" tabs Tab-separated values\n"
96279992
" tcl TCL list elements\n"
96289993
".nullvalue STRING Use STRING in place of NULL values\n"
9629
- ".once FILENAME Output for the next SQL command only to FILENAME\n"
9994
+ ".once (-e|-x|FILE) Output for the next SQL command only to FILE\n"
9995
+ " or invoke system text editor (-e) or spreadsheet (-x)\n"
9996
+ " on the output.\n"
96309997
".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
96319998
" The --new option starts with an empty file\n"
9632
- ".output ?FILENAME? Send output to FILENAME or stdout\n"
9999
+ ".output ?FILE? Send output to FILE or stdout\n"
963310000
".print STRING... Print literal STRING\n"
963410001
".prompt MAIN CONTINUE Replace the standard prompts\n"
963510002
".quit Exit this program\n"
963610003
".read FILENAME Execute SQL in FILENAME\n"
963710004
".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
@@ -9846,10 +10213,16 @@
984610213
#endif
984710214
sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
984810215
shellAddSchemaName, 0, 0);
984910216
sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
985010217
shellModuleSchema, 0, 0);
10218
+ sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
10219
+ shellPutsFunc, 0, 0);
10220
+ sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
10221
+ editFunc, 0, 0);
10222
+ sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
10223
+ editFunc, 0, 0);
985110224
if( p->openMode==SHELL_OPEN_ZIPFILE ){
985210225
char *zSql = sqlite3_mprintf(
985310226
"CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
985410227
sqlite3_exec(p->db, zSql, 0, 0, 0);
985510228
sqlite3_free(zSql);
@@ -9979,67 +10352,10 @@
997910352
z[j] = c;
998010353
}
998110354
if( j<i ) z[j] = 0;
998210355
}
998310356
9984
-/*
9985
-** Return the value of a hexadecimal digit. Return -1 if the input
9986
-** is not a hex digit.
9987
-*/
9988
-static int hexDigitValue(char c){
9989
- if( c>='0' && c<='9' ) return c - '0';
9990
- if( c>='a' && c<='f' ) return c - 'a' + 10;
9991
- if( c>='A' && c<='F' ) return c - 'A' + 10;
9992
- return -1;
9993
-}
9994
-
9995
-/*
9996
-** Interpret zArg as an integer value, possibly with suffixes.
9997
-*/
9998
-static sqlite3_int64 integerValue(const char *zArg){
9999
- sqlite3_int64 v = 0;
10000
- static const struct { char *zSuffix; int iMult; } aMult[] = {
10001
- { "KiB", 1024 },
10002
- { "MiB", 1024*1024 },
10003
- { "GiB", 1024*1024*1024 },
10004
- { "KB", 1000 },
10005
- { "MB", 1000000 },
10006
- { "GB", 1000000000 },
10007
- { "K", 1000 },
10008
- { "M", 1000000 },
10009
- { "G", 1000000000 },
10010
- };
10011
- int i;
10012
- int isNeg = 0;
10013
- if( zArg[0]=='-' ){
10014
- isNeg = 1;
10015
- zArg++;
10016
- }else if( zArg[0]=='+' ){
10017
- zArg++;
10018
- }
10019
- if( zArg[0]=='0' && zArg[1]=='x' ){
10020
- int x;
10021
- zArg += 2;
10022
- while( (x = hexDigitValue(zArg[0]))>=0 ){
10023
- v = (v<<4) + x;
10024
- zArg++;
10025
- }
10026
- }else{
10027
- while( IsDigit(zArg[0]) ){
10028
- v = v*10 + zArg[0] - '0';
10029
- zArg++;
10030
- }
10031
- }
10032
- for(i=0; i<ArraySize(aMult); i++){
10033
- if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
10034
- v *= aMult[i].iMult;
10035
- break;
10036
- }
10037
- }
10038
- return isNeg? -v : v;
10039
-}
10040
-
1004110357
/*
1004210358
** Interpret zArg as either an integer or a boolean value. Return 1 or 0
1004310359
** for TRUE and FALSE. Return the integer value if appropriate.
1004410360
*/
1004510361
static int booleanValue(const char *zArg){
@@ -10082,20 +10398,20 @@
1008210398
/*
1008310399
** Try to open an output file. The names "stdout" and "stderr" are
1008410400
** recognized and do the right thing. NULL is returned if the output
1008510401
** filename is "off".
1008610402
*/
10087
-static FILE *output_file_open(const char *zFile){
10403
+static FILE *output_file_open(const char *zFile, int bTextMode){
1008810404
FILE *f;
1008910405
if( strcmp(zFile,"stdout")==0 ){
1009010406
f = stdout;
1009110407
}else if( strcmp(zFile, "stderr")==0 ){
1009210408
f = stderr;
1009310409
}else if( strcmp(zFile, "off")==0 ){
1009410410
f = 0;
1009510411
}else{
10096
- f = fopen(zFile, "wb");
10412
+ f = fopen(zFile, bTextMode ? "w" : "wb");
1009710413
if( f==0 ){
1009810414
utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
1009910415
}
1010010416
}
1010110417
return f;
@@ -10504,19 +10820,41 @@
1050410820
}
1050510821
sqlite3_close(newDb);
1050610822
}
1050710823
1050810824
/*
10509
-** Change the output file back to stdout
10825
+** Change the output file back to stdout.
10826
+**
10827
+** If the p->doXdgOpen flag is set, that means the output was being
10828
+** redirected to a temporary file named by p->zTempFile. In that case,
10829
+** launch start/open/xdg-open on that temporary file.
1051010830
*/
1051110831
static void output_reset(ShellState *p){
1051210832
if( p->outfile[0]=='|' ){
1051310833
#ifndef SQLITE_OMIT_POPEN
1051410834
pclose(p->out);
1051510835
#endif
1051610836
}else{
1051710837
output_file_close(p->out);
10838
+ if( p->doXdgOpen ){
10839
+ const char *zXdgOpenCmd =
10840
+#if defined(_WIN32)
10841
+ "start";
10842
+#elif defined(__APPLE__)
10843
+ "open";
10844
+#else
10845
+ "xdg-open";
10846
+#endif
10847
+ char *zCmd;
10848
+ zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
10849
+ if( system(zCmd) ){
10850
+ utf8_printf(stderr, "Failed: [%s]\n", zCmd);
10851
+ }
10852
+ sqlite3_free(zCmd);
10853
+ outputModePop(p);
10854
+ p->doXdgOpen = 0;
10855
+ }
1051810856
}
1051910857
p->outfile[0] = 0;
1052010858
p->out = stdout;
1052110859
}
1052210860
@@ -10770,10 +11108,45 @@
1077011108
#else
1077111109
rc = unlink(zFilename);
1077211110
#endif
1077311111
return rc;
1077411112
}
11113
+
11114
+/*
11115
+** Try to delete the temporary file (if there is one) and free the
11116
+** memory used to hold the name of the temp file.
11117
+*/
11118
+static void clearTempFile(ShellState *p){
11119
+ if( p->zTempFile==0 ) return;
11120
+ if( p->doXdgOpen ) return;
11121
+ if( shellDeleteFile(p->zTempFile) ) return;
11122
+ sqlite3_free(p->zTempFile);
11123
+ p->zTempFile = 0;
11124
+}
11125
+
11126
+/*
11127
+** Create a new temp file name with the given suffix.
11128
+*/
11129
+static void newTempFile(ShellState *p, const char *zSuffix){
11130
+ clearTempFile(p);
11131
+ sqlite3_free(p->zTempFile);
11132
+ p->zTempFile = 0;
11133
+ if( p->db ){
11134
+ sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
11135
+ }
11136
+ if( p->zTempFile==0 ){
11137
+ sqlite3_uint64 r;
11138
+ sqlite3_randomness(sizeof(r), &r);
11139
+ p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
11140
+ }else{
11141
+ p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
11142
+ }
11143
+ if( p->zTempFile==0 ){
11144
+ raw_printf(stderr, "out of memory\n");
11145
+ exit(1);
11146
+ }
11147
+}
1077511148
1077611149
1077711150
/*
1077811151
** The implementation of SQL scalar function fkey_collate_clause(), used
1077911152
** by the ".lint fkey-indexes" command. This scalar function is always
@@ -11099,17 +11472,22 @@
1109911472
/*
1110011473
** Structure representing a single ".ar" command.
1110111474
*/
1110211475
typedef struct ArCommand ArCommand;
1110311476
struct ArCommand {
11104
- int eCmd; /* An AR_CMD_* value */
11477
+ u8 eCmd; /* An AR_CMD_* value */
11478
+ u8 bVerbose; /* True if --verbose */
11479
+ u8 bZip; /* True if the archive is a ZIP */
11480
+ u8 bDryRun; /* True if --dry-run */
11481
+ u8 bAppend; /* True if --append */
11482
+ int nArg; /* Number of command arguments */
11483
+ char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
1110511484
const char *zFile; /* --file argument, or NULL */
1110611485
const char *zDir; /* --directory argument, or NULL */
11107
- int bVerbose; /* True if --verbose */
11108
- int bZip; /* True if --zip */
11109
- int nArg; /* Number of command arguments */
1111011486
char **azArg; /* Array of command arguments */
11487
+ ShellState *p; /* Shell state */
11488
+ sqlite3 *db; /* Database containing the archive */
1111111489
};
1111211490
1111311491
/*
1111411492
** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
1111511493
*/
@@ -11131,11 +11509,13 @@
1113111509
" -x, --extract Extract files from archive\n"
1113211510
"\n"
1113311511
"And zero or more optional options:\n"
1113411512
" -v, --verbose Print each filename as it is processed\n"
1113511513
" -f FILE, --file FILE Operate on archive FILE (default is current db)\n"
11514
+" -a FILE, --append FILE Operate on FILE opened using the apndvfs VFS\n"
1113611515
" -C DIR, --directory DIR Change to directory DIR to read/extract files\n"
11516
+" -n, --dryrun Show the SQL that would have occurred\n"
1113711517
"\n"
1113811518
"See also: http://sqlite.org/cli.html#sqlar_archive_support\n"
1113911519
"\n"
1114011520
);
1114111521
return SQLITE_ERROR;
@@ -11166,14 +11546,15 @@
1116611546
#define AR_CMD_HELP 5
1116711547
1116811548
/*
1116911549
** Other (non-command) switches.
1117011550
*/
11171
-#define AR_SWITCH_VERBOSE 6
11172
-#define AR_SWITCH_FILE 7
11173
-#define AR_SWITCH_DIRECTORY 8
11174
-#define AR_SWITCH_ZIP 9
11551
+#define AR_SWITCH_VERBOSE 6
11552
+#define AR_SWITCH_FILE 7
11553
+#define AR_SWITCH_DIRECTORY 8
11554
+#define AR_SWITCH_APPEND 9
11555
+#define AR_SWITCH_DRYRUN 10
1117511556
1117611557
static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
1117711558
switch( eSwitch ){
1117811559
case AR_CMD_CREATE:
1117911560
case AR_CMD_EXTRACT:
@@ -11184,17 +11565,19 @@
1118411565
return arErrorMsg("multiple command options");
1118511566
}
1118611567
pAr->eCmd = eSwitch;
1118711568
break;
1118811569
11570
+ case AR_SWITCH_DRYRUN:
11571
+ pAr->bDryRun = 1;
11572
+ break;
1118911573
case AR_SWITCH_VERBOSE:
1119011574
pAr->bVerbose = 1;
1119111575
break;
11192
- case AR_SWITCH_ZIP:
11193
- pAr->bZip = 1;
11194
- break;
11195
-
11576
+ case AR_SWITCH_APPEND:
11577
+ pAr->bAppend = 1;
11578
+ /* Fall thru into --file */
1119611579
case AR_SWITCH_FILE:
1119711580
pAr->zFile = zArg;
1119811581
break;
1119911582
case AR_SWITCH_DIRECTORY:
1120011583
pAr->zDir = zArg;
@@ -11214,24 +11597,25 @@
1121411597
char **azArg, /* Array of arguments passed to dot command */
1121511598
int nArg, /* Number of entries in azArg[] */
1121611599
ArCommand *pAr /* Populate this object */
1121711600
){
1121811601
struct ArSwitch {
11219
- char cShort;
1122011602
const char *zLong;
11221
- int eSwitch;
11222
- int bArg;
11603
+ char cShort;
11604
+ u8 eSwitch;
11605
+ u8 bArg;
1122311606
} aSwitch[] = {
11224
- { 'c', "create", AR_CMD_CREATE, 0 },
11225
- { 'x', "extract", AR_CMD_EXTRACT, 0 },
11226
- { 't', "list", AR_CMD_LIST, 0 },
11227
- { 'u', "update", AR_CMD_UPDATE, 0 },
11228
- { 'h', "help", AR_CMD_HELP, 0 },
11229
- { 'v', "verbose", AR_SWITCH_VERBOSE, 0 },
11230
- { 'f', "file", AR_SWITCH_FILE, 1 },
11231
- { 'C', "directory", AR_SWITCH_DIRECTORY, 1 },
11232
- { 'z', "zip", AR_SWITCH_ZIP, 0 }
11607
+ { "create", 'c', AR_CMD_CREATE, 0 },
11608
+ { "extract", 'x', AR_CMD_EXTRACT, 0 },
11609
+ { "list", 't', AR_CMD_LIST, 0 },
11610
+ { "update", 'u', AR_CMD_UPDATE, 0 },
11611
+ { "help", 'h', AR_CMD_HELP, 0 },
11612
+ { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
11613
+ { "file", 'f', AR_SWITCH_FILE, 1 },
11614
+ { "append", 'a', AR_SWITCH_APPEND, 1 },
11615
+ { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
11616
+ { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
1123311617
};
1123411618
int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
1123511619
struct ArSwitch *pEnd = &aSwitch[nSwitch];
1123611620
1123711621
if( nArg<=1 ){
@@ -11354,41 +11738,39 @@
1135411738
**
1135511739
** This function strips any trailing '/' characters from each argument.
1135611740
** This is consistent with the way the [tar] command seems to work on
1135711741
** Linux.
1135811742
*/
11359
-static int arCheckEntries(sqlite3 *db, ArCommand *pAr){
11743
+static int arCheckEntries(ArCommand *pAr){
1136011744
int rc = SQLITE_OK;
1136111745
if( pAr->nArg ){
11362
- int i;
11746
+ int i, j;
1136311747
sqlite3_stmt *pTest = 0;
1136411748
11365
- shellPreparePrintf(db, &rc, &pTest, "SELECT name FROM %s WHERE name=?1",
11366
- pAr->bZip ? "zipfile(?2)" : "sqlar"
11749
+ shellPreparePrintf(pAr->db, &rc, &pTest,
11750
+ "SELECT name FROM %s WHERE name=$name",
11751
+ pAr->zSrcTable
1136711752
);
11368
- if( rc==SQLITE_OK && pAr->bZip ){
11369
- sqlite3_bind_text(pTest, 2, pAr->zFile, -1, SQLITE_TRANSIENT);
11370
- }
11753
+ j = sqlite3_bind_parameter_index(pTest, "$name");
1137111754
for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
1137211755
char *z = pAr->azArg[i];
1137311756
int n = strlen30(z);
1137411757
int bOk = 0;
1137511758
while( n>0 && z[n-1]=='/' ) n--;
1137611759
z[n] = '\0';
11377
- sqlite3_bind_text(pTest, 1, z, -1, SQLITE_STATIC);
11760
+ sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
1137811761
if( SQLITE_ROW==sqlite3_step(pTest) ){
1137911762
bOk = 1;
1138011763
}
1138111764
shellReset(&rc, pTest);
1138211765
if( rc==SQLITE_OK && bOk==0 ){
11383
- raw_printf(stderr, "not found in archive: %s\n", z);
11766
+ utf8_printf(stderr, "not found in archive: %s\n", z);
1138411767
rc = SQLITE_ERROR;
1138511768
}
1138611769
}
1138711770
shellFinalize(&rc, pTest);
1138811771
}
11389
-
1139011772
return rc;
1139111773
}
1139211774
1139311775
/*
1139411776
** Format a WHERE clause that can be used against the "sqlar" table to
@@ -11410,13 +11792,13 @@
1141011792
int i;
1141111793
const char *zSep = "";
1141211794
for(i=0; i<pAr->nArg; i++){
1141311795
const char *z = pAr->azArg[i];
1141411796
zWhere = sqlite3_mprintf(
11415
- "%z%s name = '%q' OR name BETWEEN '%q/' AND '%q0'",
11416
- zWhere, zSep, z, z, z
11417
- );
11797
+ "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
11798
+ zWhere, zSep, z, strlen30(z)+1, z
11799
+ );
1141811800
if( zWhere==0 ){
1141911801
*pRc = SQLITE_NOMEM;
1142011802
break;
1142111803
}
1142211804
zSep = " OR ";
@@ -11424,107 +11806,75 @@
1142411806
}
1142511807
}
1142611808
*pzWhere = zWhere;
1142711809
}
1142811810
11429
-/*
11430
-** Argument zMode must point to a buffer at least 11 bytes in size. This
11431
-** function populates this buffer with the string interpretation of
11432
-** the unix file mode passed as the second argument (e.g. "drwxr-xr-x").
11433
-*/
11434
-static void shellModeToString(char *zMode, int mode){
11435
- int i;
11436
-
11437
- /* Magic numbers copied from [man 2 stat] */
11438
- if( mode & 0040000 ){
11439
- zMode[0] = 'd';
11440
- }else if( (mode & 0120000)==0120000 ){
11441
- zMode[0] = 'l';
11442
- }else{
11443
- zMode[0] = '-';
11444
- }
11445
-
11446
- for(i=0; i<3; i++){
11447
- int m = (mode >> ((2-i)*3));
11448
- char *a = &zMode[1 + i*3];
11449
- a[0] = (m & 0x4) ? 'r' : '-';
11450
- a[1] = (m & 0x2) ? 'w' : '-';
11451
- a[2] = (m & 0x1) ? 'x' : '-';
11452
- }
11453
- zMode[10] = '\0';
11454
-}
11455
-
1145611811
/*
1145711812
** Implementation of .ar "lisT" command.
1145811813
*/
11459
-static int arListCommand(ShellState *p, sqlite3 *db, ArCommand *pAr){
11814
+static int arListCommand(ArCommand *pAr){
1146011815
const char *zSql = "SELECT %s FROM %s WHERE %s";
11461
- const char *zTbl = (pAr->bZip ? "zipfile(?)" : "sqlar");
1146211816
const char *azCols[] = {
1146311817
"name",
11464
- "mode, sz, datetime(mtime, 'unixepoch'), name"
11818
+ "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
1146511819
};
1146611820
1146711821
char *zWhere = 0;
1146811822
sqlite3_stmt *pSql = 0;
1146911823
int rc;
1147011824
11471
- rc = arCheckEntries(db, pAr);
11825
+ rc = arCheckEntries(pAr);
1147211826
arWhereClause(&rc, pAr, &zWhere);
1147311827
11474
- shellPreparePrintf(db, &rc, &pSql, zSql, azCols[pAr->bVerbose], zTbl, zWhere);
11475
- if( rc==SQLITE_OK && pAr->bZip ){
11476
- sqlite3_bind_text(pSql, 1, pAr->zFile, -1, SQLITE_TRANSIENT);
11477
- }
11478
- while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
11479
- if( pAr->bVerbose ){
11480
- char zMode[11];
11481
- shellModeToString(zMode, sqlite3_column_int(pSql, 0));
11482
-
11483
- raw_printf(p->out, "%s % 10d %s %s\n", zMode,
11484
- sqlite3_column_int(pSql, 1),
11485
- sqlite3_column_text(pSql, 2),
11486
- sqlite3_column_text(pSql, 3)
11487
- );
11488
- }else{
11489
- raw_printf(p->out, "%s\n", sqlite3_column_text(pSql, 0));
11490
- }
11491
- }
11492
-
11828
+ shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
11829
+ pAr->zSrcTable, zWhere);
11830
+ if( pAr->bDryRun ){
11831
+ utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
11832
+ }else{
11833
+ while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
11834
+ if( pAr->bVerbose ){
11835
+ utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
11836
+ sqlite3_column_text(pSql, 0),
11837
+ sqlite3_column_int(pSql, 1),
11838
+ sqlite3_column_text(pSql, 2),
11839
+ sqlite3_column_text(pSql, 3)
11840
+ );
11841
+ }else{
11842
+ utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
11843
+ }
11844
+ }
11845
+ }
1149311846
shellFinalize(&rc, pSql);
1149411847
return rc;
1149511848
}
1149611849
1149711850
1149811851
/*
1149911852
** Implementation of .ar "eXtract" command.
1150011853
*/
11501
-static int arExtractCommand(ShellState *p, sqlite3 *db, ArCommand *pAr){
11854
+static int arExtractCommand(ArCommand *pAr){
1150211855
const char *zSql1 =
1150311856
"SELECT "
11504
- " :1 || name, "
11505
- " writefile(?1 || name, %s, mode, mtime) "
11506
- "FROM %s WHERE (%s) AND (data IS NULL OR ?2 = 0)";
11857
+ " ($dir || name),"
11858
+ " writefile(($dir || name), %s, mode, mtime) "
11859
+ "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)";
1150711860
1150811861
const char *azExtraArg[] = {
1150911862
"sqlar_uncompress(data, sz)",
1151011863
"data"
1151111864
};
11512
- const char *azSource[] = {
11513
- "sqlar", "zipfile(?3)"
11514
- };
1151511865
1151611866
sqlite3_stmt *pSql = 0;
1151711867
int rc = SQLITE_OK;
1151811868
char *zDir = 0;
1151911869
char *zWhere = 0;
11520
- int i;
11870
+ int i, j;
1152111871
1152211872
/* If arguments are specified, check that they actually exist within
1152311873
** the archive before proceeding. And formulate a WHERE clause to
1152411874
** match them. */
11525
- rc = arCheckEntries(db, pAr);
11875
+ rc = arCheckEntries(pAr);
1152611876
arWhereClause(&rc, pAr, &zWhere);
1152711877
1152811878
if( rc==SQLITE_OK ){
1152911879
if( pAr->zDir ){
1153011880
zDir = sqlite3_mprintf("%s/", pAr->zDir);
@@ -11532,30 +11882,33 @@
1153211882
zDir = sqlite3_mprintf("");
1153311883
}
1153411884
if( zDir==0 ) rc = SQLITE_NOMEM;
1153511885
}
1153611886
11537
- shellPreparePrintf(db, &rc, &pSql, zSql1,
11538
- azExtraArg[pAr->bZip], azSource[pAr->bZip], zWhere
11887
+ shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
11888
+ azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
1153911889
);
1154011890
1154111891
if( rc==SQLITE_OK ){
11542
- sqlite3_bind_text(pSql, 1, zDir, -1, SQLITE_STATIC);
11543
- if( pAr->bZip ){
11544
- sqlite3_bind_text(pSql, 3, pAr->zFile, -1, SQLITE_STATIC);
11545
- }
11892
+ j = sqlite3_bind_parameter_index(pSql, "$dir");
11893
+ sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
1154611894
1154711895
/* Run the SELECT statement twice. The first time, writefile() is called
1154811896
** for all archive members that should be extracted. The second time,
1154911897
** only for the directories. This is because the timestamps for
1155011898
** extracted directories must be reset after they are populated (as
1155111899
** populating them changes the timestamp). */
1155211900
for(i=0; i<2; i++){
11553
- sqlite3_bind_int(pSql, 2, i);
11554
- while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
11555
- if( i==0 && pAr->bVerbose ){
11556
- raw_printf(p->out, "%s\n", sqlite3_column_text(pSql, 0));
11901
+ j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
11902
+ sqlite3_bind_int(pSql, j, i);
11903
+ if( pAr->bDryRun ){
11904
+ utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
11905
+ }else{
11906
+ while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
11907
+ if( i==0 && pAr->bVerbose ){
11908
+ utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
11909
+ }
1155711910
}
1155811911
}
1155911912
shellReset(&rc, pSql);
1156011913
}
1156111914
shellFinalize(&rc, pSql);
@@ -11563,10 +11916,29 @@
1156311916
1156411917
sqlite3_free(zDir);
1156511918
sqlite3_free(zWhere);
1156611919
return rc;
1156711920
}
11921
+
11922
+/*
11923
+** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
11924
+*/
11925
+static int arExecSql(ArCommand *pAr, const char *zSql){
11926
+ int rc;
11927
+ if( pAr->bDryRun ){
11928
+ utf8_printf(pAr->p->out, "%s\n", zSql);
11929
+ rc = SQLITE_OK;
11930
+ }else{
11931
+ char *zErr = 0;
11932
+ rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
11933
+ if( zErr ){
11934
+ utf8_printf(stdout, "ERROR: %s\n", zErr);
11935
+ sqlite3_free(zErr);
11936
+ }
11937
+ }
11938
+ return rc;
11939
+}
1156811940
1156911941
1157011942
/*
1157111943
** Implementation of .ar "create" and "update" commands.
1157211944
**
@@ -11576,116 +11948,60 @@
1157611948
** printed on stdout for each file archived.
1157711949
**
1157811950
** The create command is the same as update, except that it drops
1157911951
** any existing "sqlar" table before beginning.
1158011952
*/
11581
-static int arCreateUpdate(
11582
- ShellState *p, /* Shell state pointer */
11583
- sqlite3 *db,
11953
+static int arCreateOrUpdateCommand(
1158411954
ArCommand *pAr, /* Command arguments and options */
11585
- int bUpdate
11955
+ int bUpdate /* true for a --create. false for --update */
1158611956
){
11587
- const char *zSql = "SELECT name, mode, mtime, data FROM fsdir(?, ?)";
1158811957
const char *zCreate =
1158911958
"CREATE TABLE IF NOT EXISTS sqlar(\n"
1159011959
" name TEXT PRIMARY KEY, -- name of the file\n"
1159111960
" mode INT, -- access permissions\n"
1159211961
" mtime INT, -- last modification time\n"
1159311962
" sz INT, -- original file size\n"
1159411963
" data BLOB -- compressed content\n"
1159511964
")";
1159611965
const char *zDrop = "DROP TABLE IF EXISTS sqlar";
11597
- const char *zInsert = "REPLACE INTO sqlar VALUES(?,?,?,?,sqlar_compress(?))";
11598
-
11599
- sqlite3_stmt *pStmt = 0; /* Directory traverser */
11600
- sqlite3_stmt *pInsert = 0; /* Compilation of zInsert */
11966
+ const char *zInsertFmt =
11967
+ "REPLACE INTO sqlar(name,mode,mtime,sz,data)\n"
11968
+ " SELECT\n"
11969
+ " %s,\n"
11970
+ " mode,\n"
11971
+ " mtime,\n"
11972
+ " CASE substr(lsmode(mode),1,1)\n"
11973
+ " WHEN '-' THEN length(data)\n"
11974
+ " WHEN 'd' THEN 0\n"
11975
+ " ELSE -1 END,\n"
11976
+ " CASE WHEN lsmode(mode) LIKE 'd%%' THEN NULL else data END\n"
11977
+ " FROM fsdir(%Q,%Q)\n"
11978
+ " WHERE lsmode(mode) NOT LIKE '?%%';";
1160111979
int i; /* For iterating through azFile[] */
1160211980
int rc; /* Return code */
1160311981
11604
- assert( pAr->bZip==0 );
11605
-
11606
- rc = sqlite3_exec(db, "SAVEPOINT ar;", 0, 0, 0);
11607
- if( rc!=SQLITE_OK ) return rc;
11608
-
11609
- if( bUpdate==0 ){
11610
- rc = sqlite3_exec(db, zDrop, 0, 0, 0);
11611
- if( rc!=SQLITE_OK ) return rc;
11612
- }
11613
-
11614
- rc = sqlite3_exec(db, zCreate, 0, 0, 0);
11615
- shellPrepare(db, &rc, zInsert, &pInsert);
11616
- shellPrepare(db, &rc, zSql, &pStmt);
11617
- sqlite3_bind_text(pStmt, 2, pAr->zDir, -1, SQLITE_STATIC);
11618
-
11619
- for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
11620
- sqlite3_bind_text(pStmt, 1, pAr->azArg[i], -1, SQLITE_STATIC);
11621
- while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
11622
- int sz;
11623
- const char *zName = (const char*)sqlite3_column_text(pStmt, 0);
11624
- int mode = sqlite3_column_int(pStmt, 1);
11625
- unsigned int mtime = sqlite3_column_int(pStmt, 2);
11626
-
11627
- if( pAr->bVerbose ){
11628
- raw_printf(p->out, "%s\n", zName);
11629
- }
11630
-
11631
- sqlite3_bind_text(pInsert, 1, zName, -1, SQLITE_STATIC);
11632
- sqlite3_bind_int(pInsert, 2, mode);
11633
- sqlite3_bind_int64(pInsert, 3, (sqlite3_int64)mtime);
11634
-
11635
- if( S_ISDIR(mode) ){
11636
- sz = 0;
11637
- sqlite3_bind_null(pInsert, 5);
11638
- }else{
11639
- sqlite3_bind_value(pInsert, 5, sqlite3_column_value(pStmt, 3));
11640
- if( S_ISLNK(mode) ){
11641
- sz = -1;
11642
- }else{
11643
- sz = sqlite3_column_bytes(pStmt, 3);
11644
- }
11645
- }
11646
-
11647
- sqlite3_bind_int(pInsert, 4, sz);
11648
- sqlite3_step(pInsert);
11649
- rc = sqlite3_reset(pInsert);
11650
- }
11651
- shellReset(&rc, pStmt);
11652
- }
11653
-
11654
- if( rc!=SQLITE_OK ){
11655
- sqlite3_exec(db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
11656
- }else{
11657
- rc = sqlite3_exec(db, "RELEASE ar;", 0, 0, 0);
11658
- }
11659
- shellFinalize(&rc, pStmt);
11660
- shellFinalize(&rc, pInsert);
11661
- return rc;
11662
-}
11663
-
11664
-/*
11665
-** Implementation of .ar "Create" command.
11666
-**
11667
-** Create the "sqlar" table in the database if it does not already exist.
11668
-** Then add each file in the azFile[] array to the archive. Directories
11669
-** are added recursively. If argument bVerbose is non-zero, a message is
11670
-** printed on stdout for each file archived.
11671
-*/
11672
-static int arCreateCommand(
11673
- ShellState *p, /* Shell state pointer */
11674
- sqlite3 *db,
11675
- ArCommand *pAr /* Command arguments and options */
11676
-){
11677
- return arCreateUpdate(p, db, pAr, 0);
11678
-}
11679
-
11680
-/*
11681
-** Implementation of .ar "Update" command.
11682
-*/
11683
-static int arUpdateCmd(ShellState *p, sqlite3 *db, ArCommand *pAr){
11684
- return arCreateUpdate(p, db, pAr, 1);
11685
-}
11686
-
11982
+ rc = arExecSql(pAr, "SAVEPOINT ar;");
11983
+ if( rc!=SQLITE_OK ) return rc;
11984
+ if( bUpdate==0 ){
11985
+ rc = arExecSql(pAr, zDrop);
11986
+ if( rc!=SQLITE_OK ) return rc;
11987
+ }
11988
+ rc = arExecSql(pAr, zCreate);
11989
+ for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
11990
+ char *zSql = sqlite3_mprintf(zInsertFmt,
11991
+ pAr->bVerbose ? "shell_putsnl(name)" : "name",
11992
+ pAr->azArg[i], pAr->zDir);
11993
+ rc = arExecSql(pAr, zSql);
11994
+ sqlite3_free(zSql);
11995
+ }
11996
+ if( rc!=SQLITE_OK ){
11997
+ arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
11998
+ }else{
11999
+ rc = arExecSql(pAr, "RELEASE ar;");
12000
+ }
12001
+ return rc;
12002
+}
1168712003
1168812004
/*
1168912005
** Implementation of ".ar" dot command.
1169012006
*/
1169112007
static int arDotCommand(
@@ -11693,138 +12009,108 @@
1169312009
char **azArg, /* Array of arguments passed to dot command */
1169412010
int nArg /* Number of entries in azArg[] */
1169512011
){
1169612012
ArCommand cmd;
1169712013
int rc;
12014
+ memset(&cmd, 0, sizeof(cmd));
1169812015
rc = arParseCommand(azArg, nArg, &cmd);
1169912016
if( rc==SQLITE_OK ){
11700
- sqlite3 *db = 0; /* Database handle to use as archive */
11701
-
11702
- if( cmd.bZip ){
12017
+ int eDbType = SHELL_OPEN_UNSPEC;
12018
+ cmd.p = pState;
12019
+ cmd.db = pState->db;
12020
+ if( cmd.zFile ){
12021
+ eDbType = deduceDatabaseType(cmd.zFile);
12022
+ }else{
12023
+ eDbType = pState->openMode;
12024
+ }
12025
+ if( eDbType==SHELL_OPEN_ZIPFILE ){
1170312026
if( cmd.zFile==0 ){
11704
- raw_printf(stderr, "zip format requires a --file switch\n");
11705
- return SQLITE_ERROR;
11706
- }else
12027
+ cmd.zSrcTable = sqlite3_mprintf("zip");
12028
+ }else{
12029
+ cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
12030
+ }
1170712031
if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
11708
- raw_printf(stderr, "zip archives are read-only\n");
11709
- return SQLITE_ERROR;
12032
+ utf8_printf(stderr, "zip archives are read-only\n");
12033
+ rc = SQLITE_ERROR;
12034
+ goto end_ar_command;
1171012035
}
11711
- db = pState->db;
12036
+ cmd.bZip = 1;
1171212037
}else if( cmd.zFile ){
1171312038
int flags;
12039
+ if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
1171412040
if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
1171512041
flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
1171612042
}else{
1171712043
flags = SQLITE_OPEN_READONLY;
1171812044
}
11719
- rc = sqlite3_open_v2(cmd.zFile, &db, flags, 0);
12045
+ cmd.db = 0;
12046
+ if( cmd.bDryRun ){
12047
+ utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
12048
+ eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
12049
+ }
12050
+ rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
12051
+ eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
1172012052
if( rc!=SQLITE_OK ){
11721
- raw_printf(stderr, "cannot open file: %s (%s)\n",
11722
- cmd.zFile, sqlite3_errmsg(db)
12053
+ utf8_printf(stderr, "cannot open file: %s (%s)\n",
12054
+ cmd.zFile, sqlite3_errmsg(cmd.db)
1172312055
);
11724
- sqlite3_close(db);
11725
- return rc;
12056
+ goto end_ar_command;
1172612057
}
11727
- sqlite3_fileio_init(db, 0, 0);
12058
+ sqlite3_fileio_init(cmd.db, 0, 0);
1172812059
#ifdef SQLITE_HAVE_ZLIB
11729
- sqlite3_sqlar_init(db, 0, 0);
12060
+ sqlite3_sqlar_init(cmd.db, 0, 0);
1173012061
#endif
11731
- }else{
11732
- db = pState->db;
12062
+ sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
12063
+ shellPutsFunc, 0, 0);
12064
+
12065
+ }
12066
+ if( cmd.zSrcTable==0 ){
12067
+ if( cmd.eCmd!=AR_CMD_CREATE
12068
+ && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
12069
+ ){
12070
+ utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
12071
+ rc = SQLITE_ERROR;
12072
+ goto end_ar_command;
12073
+ }
12074
+ cmd.zSrcTable = sqlite3_mprintf("sqlar");
1173312075
}
1173412076
1173512077
switch( cmd.eCmd ){
1173612078
case AR_CMD_CREATE:
11737
- rc = arCreateCommand(pState, db, &cmd);
12079
+ rc = arCreateOrUpdateCommand(&cmd, 0);
1173812080
break;
1173912081
1174012082
case AR_CMD_EXTRACT:
11741
- rc = arExtractCommand(pState, db, &cmd);
12083
+ rc = arExtractCommand(&cmd);
1174212084
break;
1174312085
1174412086
case AR_CMD_LIST:
11745
- rc = arListCommand(pState, db, &cmd);
12087
+ rc = arListCommand(&cmd);
1174612088
break;
1174712089
1174812090
case AR_CMD_HELP:
1174912091
arUsage(pState->out);
1175012092
break;
1175112093
1175212094
default:
1175312095
assert( cmd.eCmd==AR_CMD_UPDATE );
11754
- rc = arUpdateCmd(pState, db, &cmd);
12096
+ rc = arCreateOrUpdateCommand(&cmd, 1);
1175512097
break;
1175612098
}
11757
-
11758
- if( cmd.zFile ){
11759
- sqlite3_close(db);
11760
- }
12099
+ }
12100
+end_ar_command:
12101
+ if( cmd.db!=pState->db ){
12102
+ sqlite3_close(cmd.db);
1176112103
}
12104
+ sqlite3_free(cmd.zSrcTable);
1176212105
1176312106
return rc;
1176412107
}
1176512108
/* End of the ".archive" or ".ar" command logic
1176612109
**********************************************************************************/
1176712110
#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
1176812111
11769
-/*
11770
-** Implementation of ".expert" dot command.
11771
-*/
11772
-static int expertDotCommand(
11773
- ShellState *pState, /* Current shell tool state */
11774
- char **azArg, /* Array of arguments passed to dot command */
11775
- int nArg /* Number of entries in azArg[] */
11776
-){
11777
- int rc = SQLITE_OK;
11778
- char *zErr = 0;
11779
- int i;
11780
- int iSample = 0;
11781
-
11782
- assert( pState->expert.pExpert==0 );
11783
- memset(&pState->expert, 0, sizeof(ExpertInfo));
11784
-
11785
- for(i=1; rc==SQLITE_OK && i<nArg; i++){
11786
- char *z = azArg[i];
11787
- int n;
11788
- if( z[0]=='-' && z[1]=='-' ) z++;
11789
- n = strlen30(z);
11790
- if( n>=2 && 0==strncmp(z, "-verbose", n) ){
11791
- pState->expert.bVerbose = 1;
11792
- }
11793
- else if( n>=2 && 0==strncmp(z, "-sample", n) ){
11794
- if( i==(nArg-1) ){
11795
- raw_printf(stderr, "option requires an argument: %s\n", z);
11796
- rc = SQLITE_ERROR;
11797
- }else{
11798
- iSample = (int)integerValue(azArg[++i]);
11799
- if( iSample<0 || iSample>100 ){
11800
- raw_printf(stderr, "value out of range: %s\n", azArg[i]);
11801
- rc = SQLITE_ERROR;
11802
- }
11803
- }
11804
- }
11805
- else{
11806
- raw_printf(stderr, "unknown option: %s\n", z);
11807
- rc = SQLITE_ERROR;
11808
- }
11809
- }
11810
-
11811
- if( rc==SQLITE_OK ){
11812
- pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
11813
- if( pState->expert.pExpert==0 ){
11814
- raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
11815
- rc = SQLITE_ERROR;
11816
- }else{
11817
- sqlite3_expert_config(
11818
- pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
11819
- );
11820
- }
11821
- }
11822
-
11823
- return rc;
11824
-}
11825
-
1182612112
1182712113
/*
1182812114
** If an input line begins with "." then invoke this routine to
1182912115
** process that line.
1183012116
**
@@ -11835,13 +12121,15 @@
1183512121
int nArg = 0;
1183612122
int n, c;
1183712123
int rc = 0;
1183812124
char *azArg[50];
1183912125
12126
+#ifndef SQLITE_OMIT_VIRTUALTABLE
1184012127
if( p->expert.pExpert ){
1184112128
expertFinish(p, 1, 0);
1184212129
}
12130
+#endif
1184312131
1184412132
/* Parse the input line into tokens.
1184512133
*/
1184612134
while( zLine[h] && nArg<ArraySize(azArg) ){
1184712135
while( IsSpace(zLine[h]) ){ h++; }
@@ -11868,10 +12156,11 @@
1186812156
/* Process the input line.
1186912157
*/
1187012158
if( nArg==0 ) return 0; /* no tokens, no error */
1187112159
n = strlen30(azArg[0]);
1187212160
c = azArg[0][0];
12161
+ clearTempFile(p);
1187312162
1187412163
#ifndef SQLITE_OMIT_AUTHORIZATION
1187512164
if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
1187612165
if( nArg!=2 ){
1187712166
raw_printf(stderr, "Usage: .auth ON|OFF\n");
@@ -12202,14 +12491,16 @@
1220212491
if( p->mode==MODE_Explain ) p->mode = p->normalMode;
1220312492
p->autoExplain = 1;
1220412493
}
1220512494
}else
1220612495
12496
+#ifndef SQLITE_OMIT_VIRTUALTABLE
1220712497
if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
1220812498
open_db(p, 0);
1220912499
expertDotCommand(p, azArg, nArg);
1221012500
}else
12501
+#endif
1221112502
1221212503
if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
1221312504
ShellState data;
1221412505
char *zErrMsg = 0;
1221512506
int doStats = 0;
@@ -12661,11 +12952,11 @@
1266112952
raw_printf(stderr, "Usage: .log FILENAME\n");
1266212953
rc = 1;
1266312954
}else{
1266412955
const char *zFile = azArg[1];
1266512956
output_file_close(p->pLog);
12666
- p->pLog = output_file_open(zFile);
12957
+ p->pLog = output_file_open(zFile, 0);
1266712958
}
1266812959
}else
1266912960
1267012961
if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
1267112962
const char *zMode = nArg>=2 ? azArg[1] : "";
@@ -12770,30 +13061,54 @@
1277013061
p->zDbFilename = 0;
1277113062
open_db(p, 0);
1277213063
}
1277313064
}else
1277413065
12775
- if( c=='o'
12776
- && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0)
13066
+ if( (c=='o'
13067
+ && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
13068
+ || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
1277713069
){
1277813070
const char *zFile = nArg>=2 ? azArg[1] : "stdout";
13071
+ int bTxtMode = 0;
13072
+ if( azArg[0][0]=='e' ){
13073
+ /* Transform the ".excel" command into ".once -x" */
13074
+ nArg = 2;
13075
+ azArg[0] = "once";
13076
+ zFile = azArg[1] = "-x";
13077
+ n = 4;
13078
+ }
1277913079
if( nArg>2 ){
12780
- utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]);
13080
+ utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
1278113081
rc = 1;
1278213082
goto meta_command_exit;
1278313083
}
1278413084
if( n>1 && strncmp(azArg[0], "once", n)==0 ){
1278513085
if( nArg<2 ){
12786
- raw_printf(stderr, "Usage: .once FILE\n");
13086
+ raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
1278713087
rc = 1;
1278813088
goto meta_command_exit;
1278913089
}
1279013090
p->outCount = 2;
1279113091
}else{
1279213092
p->outCount = 0;
1279313093
}
1279413094
output_reset(p);
13095
+ if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
13096
+ if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
13097
+ p->doXdgOpen = 1;
13098
+ outputModePush(p);
13099
+ if( zFile[1]=='x' ){
13100
+ newTempFile(p, "csv");
13101
+ p->mode = MODE_Csv;
13102
+ sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
13103
+ sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
13104
+ }else{
13105
+ newTempFile(p, "txt");
13106
+ bTxtMode = 1;
13107
+ }
13108
+ zFile = p->zTempFile;
13109
+ }
1279513110
if( zFile[0]=='|' ){
1279613111
#ifdef SQLITE_OMIT_POPEN
1279713112
raw_printf(stderr, "Error: pipes are not supported in this OS\n");
1279813113
rc = 1;
1279913114
p->out = stdout;
@@ -12806,11 +13121,11 @@
1280613121
}else{
1280713122
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
1280813123
}
1280913124
#endif
1281013125
}else{
12811
- p->out = output_file_open(zFile);
13126
+ p->out = output_file_open(zFile, bTxtMode);
1281213127
if( p->out==0 ){
1281313128
if( strcmp(zFile,"off")!=0 ){
1281413129
utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
1281513130
}
1281613131
p->out = stdout;
@@ -13683,11 +13998,11 @@
1368313998
}else
1368413999
1368514000
/* Begin redirecting output to the file "testcase-out.txt" */
1368614001
if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
1368714002
output_reset(p);
13688
- p->out = output_file_open("testcase-out.txt");
14003
+ p->out = output_file_open("testcase-out.txt", 0);
1368914004
if( p->out==0 ){
1369014005
raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
1369114006
}
1369214007
if( nArg>=2 ){
1369314008
sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
@@ -13889,11 +14204,11 @@
1388914204
raw_printf(stderr, "Usage: .trace FILE|off\n");
1389014205
rc = 1;
1389114206
goto meta_command_exit;
1389214207
}
1389314208
output_file_close(p->traceOut);
13894
- p->traceOut = output_file_open(azArg[1]);
14209
+ p->traceOut = output_file_open(azArg[1], 0);
1389514210
#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
1389614211
if( p->traceOut==0 ){
1389714212
sqlite3_trace_v2(p->db, 0, 0, 0);
1389814213
}else{
1389914214
sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
@@ -14219,10 +14534,12 @@
1421914534
errCnt += runOneSqlLine(p, zSql, in, startline);
1422014535
nSql = 0;
1422114536
if( p->outCount ){
1422214537
output_reset(p);
1422314538
p->outCount = 0;
14539
+ }else{
14540
+ clearTempFile(p);
1422414541
}
1422514542
}else if( nSql && _all_whitespace(zSql) ){
1422614543
if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
1422714544
nSql = 0;
1422814545
}
@@ -14841,11 +15158,14 @@
1484115158
session_close_all(&data);
1484215159
sqlite3_close(data.db);
1484315160
}
1484415161
sqlite3_free(data.zFreeOnClose);
1484515162
find_home_dir(1);
15163
+ output_reset(&data);
15164
+ data.doXdgOpen = 0;
15165
+ clearTempFile(&data);
1484615166
#if !SQLITE_SHELL_IS_UTF8
1484715167
for(i=0; i<argc; i++) sqlite3_free(argv[i]);
1484815168
sqlite3_free(argv);
1484915169
#endif
1485015170
return rc;
1485115171
}
1485215172
--- src/shell.c
+++ src/shell.c
@@ -368,10 +368,15 @@
368 /*
369 ** Used to prevent warnings about unused parameters
370 */
371 #define UNUSED_PARAMETER(x) (void)(x)
372
 
 
 
 
 
373 /*
374 ** If the following flag is set, then command execution stops
375 ** at an error if we are not interactive.
376 */
377 static int bail_on_error = 0;
@@ -640,10 +645,69 @@
640 if( zResult && *zResult ) shell_add_history(zResult);
641 #endif
642 }
643 return zResult;
644 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
645 /*
646 ** A variable length string to which one can append text.
647 */
648 typedef struct ShellText ShellText;
649 struct ShellText {
@@ -2334,10 +2398,44 @@
2334 }else{
2335 ctxErrorMsg(context, "failed to write file: %s", zFile);
2336 }
2337 }
2338 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2339
2340 #ifndef SQLITE_OMIT_VIRTUALTABLE
2341
2342 /*
2343 ** Cursor type for recursively iterating through a directory structure.
@@ -2746,10 +2844,14 @@
2746 readfileFunc, 0, 0);
2747 if( rc==SQLITE_OK ){
2748 rc = sqlite3_create_function(db, "writefile", -1, SQLITE_UTF8, 0,
2749 writefileFunc, 0, 0);
2750 }
 
 
 
 
2751 if( rc==SQLITE_OK ){
2752 rc = fsdirRegister(db);
2753 }
2754 return rc;
2755 }
@@ -4879,11 +4981,11 @@
4879 if( pNew==0 ){
4880 rc = SQLITE_NOMEM;
4881 }else{
4882 memset(pNew, 0, sizeof(ZipfileEntry));
4883 pNew->zPath = (char*)&pNew[1];
4884 memcpy(pNew->zPath, &aBuf[ZIPFILE_CDS_FIXED_SZ], nFile);
4885 pNew->zPath[nFile] = '\0';
4886 pNew->aCdsEntry = (u8*)&pNew->zPath[nFile+1];
4887 pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
4888 memcpy(pNew->aCdsEntry, aRec, pNew->nCdsEntry);
4889 zipfileAddEntry(pTab, pNew);
@@ -5034,10 +5136,22 @@
5034
5035 parse_error:
5036 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5037 return SQLITE_ERROR;
5038 }
 
 
 
 
 
 
 
 
 
 
 
 
5039
5040 /*
5041 ** xUpdate method.
5042 */
5043 static int zipfileUpdate(
@@ -5048,19 +5162,20 @@
5048 ){
5049 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5050 int rc = SQLITE_OK; /* Return Code */
5051 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
5052
5053 u32 mode; /* Mode for new entry */
5054 i64 mTime; /* Modification time for new entry */
5055 i64 sz = 0; /* Uncompressed size */
5056 const char *zPath; /* Path for new entry */
5057 int nPath; /* strlen(zPath) */
5058 const u8 *pData = 0; /* Pointer to buffer containing content */
5059 int nData = 0; /* Size of pData buffer in bytes */
5060 int iMethod = 0; /* Compression method for new entry */
5061 u8 *pFree = 0; /* Free this */
 
5062 ZipfileCDS cds; /* New Central Directory Structure entry */
5063
5064 int bIsDir = 0;
5065
5066 int mNull;
@@ -5067,19 +5182,23 @@
5067
5068 assert( pTab->zFile );
5069 assert( pTab->pWriteFd );
5070
5071 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5072 i64 iDelete = sqlite3_value_int64(apVal[0]);
5073 ZipfileEntry *p;
5074 for(p=pTab->pFirstEntry; p; p=p->pNext){
5075 if( p->iRowid==iDelete ){
5076 p->bDeleted = 1;
5077 break;
5078 }
5079 }
5080 if( nVal==1 ) return SQLITE_OK;
 
 
 
 
5081 }
5082
5083 mNull = (sqlite3_value_type(apVal[5])==SQLITE_NULL ? 0x0 : 0x8) /* sz */
5084 + (sqlite3_value_type(apVal[6])==SQLITE_NULL ? 0x0 : 0x4) /* rawdata */
5085 + (sqlite3_value_type(apVal[7])==SQLITE_NULL ? 0x0 : 0x2) /* data */
@@ -5148,10 +5267,34 @@
5148 mTime = (sqlite3_int64)time(0);
5149 }else{
5150 mTime = sqlite3_value_int64(apVal[4]);
5151 }
5152 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5153
5154 if( rc==SQLITE_OK ){
5155 /* Create the new CDS record. */
5156 memset(&cds, 0, sizeof(cds));
5157 cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
@@ -5176,10 +5319,11 @@
5176 if( rc==SQLITE_OK ){
5177 rc = zipfileAppendEntry(pTab, &cds, zPath, nPath, pData, nData, (u32)mTime);
5178 }
5179
5180 sqlite3_free(pFree);
 
5181 return rc;
5182 }
5183
5184 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
5185 u8 *aBuf = pTab->aBuffer;
@@ -5329,11 +5473,10 @@
5329 ){
5330 SQLITE_EXTENSION_INIT2(pApi);
5331 (void)pzErrMsg; /* Unused parameter */
5332 return zipfileRegister(db);
5333 }
5334
5335
5336 /************************* End ../ext/misc/zipfile.c ********************/
5337 /************************* Begin ../ext/misc/sqlar.c ******************/
5338 /*
5339 ** 2017-12-17
@@ -5642,10 +5785,12 @@
5642 *************************************************************************
5643 */
5644 #include <assert.h>
5645 #include <string.h>
5646 #include <stdio.h>
 
 
5647
5648 /* typedef sqlite3_int64 i64; */
5649 /* typedef sqlite3_uint64 u64; */
5650
5651 typedef struct IdxColumn IdxColumn;
@@ -7151,10 +7296,11 @@
7151 zCols = idxAppendText(&rc, zCols,
7152 "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
7153 );
7154 zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
7155 }
 
7156 if( rc==SQLITE_OK ){
7157 if( p->iSample==100 ){
7158 zQuery = sqlite3_mprintf(
7159 "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
7160 );
@@ -7560,10 +7706,12 @@
7560 idxHashClear(&p->hIdx);
7561 sqlite3_free(p->zCandidates);
7562 sqlite3_free(p);
7563 }
7564 }
 
 
7565
7566 /************************* End ../ext/expert/sqlite3expert.c ********************/
7567
7568 #if defined(SQLITE_ENABLE_SESSION)
7569 /*
@@ -7606,26 +7754,31 @@
7606 u8 autoExplain; /* Automatically turn on .explain mode */
7607 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
7608 u8 statsOn; /* True to display memory stats before each finalize */
7609 u8 scanstatsOn; /* True to display scan stats before each finalize */
7610 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
 
7611 int outCount; /* Revert to stdout when reaching zero */
7612 int cnt; /* Number of records displayed so far */
7613 FILE *out; /* Write results here */
7614 FILE *traceOut; /* Output for sqlite3_trace() */
7615 int nErr; /* Number of errors seen */
7616 int mode; /* An output mode setting */
 
7617 int cMode; /* temporary output mode for the current query */
7618 int normalMode; /* Output mode before ".explain on" */
7619 int writableSchema; /* True if PRAGMA writable_schema=ON */
7620 int showHeader; /* True to show column names in List or Column mode */
7621 int nCheck; /* Number of ".check" commands run */
7622 unsigned shellFlgs; /* Various flags */
7623 char *zDestTable; /* Name of destination table when MODE_Insert */
 
7624 char zTestcase[30]; /* Name of current test case */
7625 char colSeparator[20]; /* Column separator character for several modes */
7626 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
 
 
7627 int colWidth[100]; /* Requested width of each column when in column mode*/
7628 int actualWidth[100]; /* Actual width of each column */
7629 char nullValue[20]; /* The text to print when a NULL comes back from
7630 ** the database */
7631 char outfile[FILENAME_MAX]; /* Filename for *out */
@@ -7719,24 +7872,175 @@
7719 #define SEP_Comma ","
7720 #define SEP_CrLf "\r\n"
7721 #define SEP_Unit "\x1F"
7722 #define SEP_Record "\x1E"
7723
7724 /*
7725 ** Number of elements in an array
7726 */
7727 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
7728
7729 /*
7730 ** A callback for the sqlite3_log() interface.
7731 */
7732 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
7733 ShellState *p = (ShellState*)pArg;
7734 if( p->pLog==0 ) return;
7735 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
7736 fflush(p->pLog);
7737 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7738
7739 /*
7740 ** Output the given string as a hex-encoded blob (eg. X'1234' )
7741 */
7742 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
@@ -9058,10 +9362,11 @@
9058 } while( rc == SQLITE_ROW );
9059 }
9060 }
9061 }
9062
 
9063 /*
9064 ** This function is called to process SQL if the previous shell command
9065 ** was ".expert". It passes the SQL in the second argument directly to
9066 ** the sqlite3expert object.
9067 **
@@ -9130,10 +9435,67 @@
9130 sqlite3_expert_destroy(p);
9131 pState->expert.pExpert = 0;
9132 return rc;
9133 }
9134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9135
9136 /*
9137 ** Execute a statement or set of statements. Print
9138 ** any result rows/columns depending on the current mode
9139 ** set via the supplied callback.
@@ -9157,14 +9519,16 @@
9157
9158 if( pzErrMsg ){
9159 *pzErrMsg = NULL;
9160 }
9161
 
9162 if( pArg->expert.pExpert ){
9163 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
9164 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
9165 }
 
9166
9167 while( zSql[0] && (SQLITE_OK == rc) ){
9168 static const char *zStmtSql;
9169 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
9170 if( SQLITE_OK != rc ){
@@ -9587,10 +9951,11 @@
9587 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
9588 " If TABLE specified, only dump tables matching\n"
9589 " LIKE pattern TABLE.\n"
9590 ".echo on|off Turn command echo on or off\n"
9591 ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
 
9592 ".exit Exit this program\n"
9593 ".expert EXPERIMENTAL. Suggest indexes for specified queries\n"
9594 /* Because explain mode comes on automatically now, the ".explain" mode
9595 ** is removed from the help screen. It is still supported for legacy, however */
9596 /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
@@ -9624,14 +9989,16 @@
9624 " list Values delimited by \"|\"\n"
9625 " quote Escape answers as for SQL\n"
9626 " tabs Tab-separated values\n"
9627 " tcl TCL list elements\n"
9628 ".nullvalue STRING Use STRING in place of NULL values\n"
9629 ".once FILENAME Output for the next SQL command only to FILENAME\n"
 
 
9630 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
9631 " The --new option starts with an empty file\n"
9632 ".output ?FILENAME? Send output to FILENAME or stdout\n"
9633 ".print STRING... Print literal STRING\n"
9634 ".prompt MAIN CONTINUE Replace the standard prompts\n"
9635 ".quit Exit this program\n"
9636 ".read FILENAME Execute SQL in FILENAME\n"
9637 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
@@ -9846,10 +10213,16 @@
9846 #endif
9847 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
9848 shellAddSchemaName, 0, 0);
9849 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
9850 shellModuleSchema, 0, 0);
 
 
 
 
 
 
9851 if( p->openMode==SHELL_OPEN_ZIPFILE ){
9852 char *zSql = sqlite3_mprintf(
9853 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
9854 sqlite3_exec(p->db, zSql, 0, 0, 0);
9855 sqlite3_free(zSql);
@@ -9979,67 +10352,10 @@
9979 z[j] = c;
9980 }
9981 if( j<i ) z[j] = 0;
9982 }
9983
9984 /*
9985 ** Return the value of a hexadecimal digit. Return -1 if the input
9986 ** is not a hex digit.
9987 */
9988 static int hexDigitValue(char c){
9989 if( c>='0' && c<='9' ) return c - '0';
9990 if( c>='a' && c<='f' ) return c - 'a' + 10;
9991 if( c>='A' && c<='F' ) return c - 'A' + 10;
9992 return -1;
9993 }
9994
9995 /*
9996 ** Interpret zArg as an integer value, possibly with suffixes.
9997 */
9998 static sqlite3_int64 integerValue(const char *zArg){
9999 sqlite3_int64 v = 0;
10000 static const struct { char *zSuffix; int iMult; } aMult[] = {
10001 { "KiB", 1024 },
10002 { "MiB", 1024*1024 },
10003 { "GiB", 1024*1024*1024 },
10004 { "KB", 1000 },
10005 { "MB", 1000000 },
10006 { "GB", 1000000000 },
10007 { "K", 1000 },
10008 { "M", 1000000 },
10009 { "G", 1000000000 },
10010 };
10011 int i;
10012 int isNeg = 0;
10013 if( zArg[0]=='-' ){
10014 isNeg = 1;
10015 zArg++;
10016 }else if( zArg[0]=='+' ){
10017 zArg++;
10018 }
10019 if( zArg[0]=='0' && zArg[1]=='x' ){
10020 int x;
10021 zArg += 2;
10022 while( (x = hexDigitValue(zArg[0]))>=0 ){
10023 v = (v<<4) + x;
10024 zArg++;
10025 }
10026 }else{
10027 while( IsDigit(zArg[0]) ){
10028 v = v*10 + zArg[0] - '0';
10029 zArg++;
10030 }
10031 }
10032 for(i=0; i<ArraySize(aMult); i++){
10033 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
10034 v *= aMult[i].iMult;
10035 break;
10036 }
10037 }
10038 return isNeg? -v : v;
10039 }
10040
10041 /*
10042 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
10043 ** for TRUE and FALSE. Return the integer value if appropriate.
10044 */
10045 static int booleanValue(const char *zArg){
@@ -10082,20 +10398,20 @@
10082 /*
10083 ** Try to open an output file. The names "stdout" and "stderr" are
10084 ** recognized and do the right thing. NULL is returned if the output
10085 ** filename is "off".
10086 */
10087 static FILE *output_file_open(const char *zFile){
10088 FILE *f;
10089 if( strcmp(zFile,"stdout")==0 ){
10090 f = stdout;
10091 }else if( strcmp(zFile, "stderr")==0 ){
10092 f = stderr;
10093 }else if( strcmp(zFile, "off")==0 ){
10094 f = 0;
10095 }else{
10096 f = fopen(zFile, "wb");
10097 if( f==0 ){
10098 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
10099 }
10100 }
10101 return f;
@@ -10504,19 +10820,41 @@
10504 }
10505 sqlite3_close(newDb);
10506 }
10507
10508 /*
10509 ** Change the output file back to stdout
 
 
 
 
10510 */
10511 static void output_reset(ShellState *p){
10512 if( p->outfile[0]=='|' ){
10513 #ifndef SQLITE_OMIT_POPEN
10514 pclose(p->out);
10515 #endif
10516 }else{
10517 output_file_close(p->out);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10518 }
10519 p->outfile[0] = 0;
10520 p->out = stdout;
10521 }
10522
@@ -10770,10 +11108,45 @@
10770 #else
10771 rc = unlink(zFilename);
10772 #endif
10773 return rc;
10774 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10775
10776
10777 /*
10778 ** The implementation of SQL scalar function fkey_collate_clause(), used
10779 ** by the ".lint fkey-indexes" command. This scalar function is always
@@ -11099,17 +11472,22 @@
11099 /*
11100 ** Structure representing a single ".ar" command.
11101 */
11102 typedef struct ArCommand ArCommand;
11103 struct ArCommand {
11104 int eCmd; /* An AR_CMD_* value */
 
 
 
 
 
 
11105 const char *zFile; /* --file argument, or NULL */
11106 const char *zDir; /* --directory argument, or NULL */
11107 int bVerbose; /* True if --verbose */
11108 int bZip; /* True if --zip */
11109 int nArg; /* Number of command arguments */
11110 char **azArg; /* Array of command arguments */
 
 
11111 };
11112
11113 /*
11114 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
11115 */
@@ -11131,11 +11509,13 @@
11131 " -x, --extract Extract files from archive\n"
11132 "\n"
11133 "And zero or more optional options:\n"
11134 " -v, --verbose Print each filename as it is processed\n"
11135 " -f FILE, --file FILE Operate on archive FILE (default is current db)\n"
 
11136 " -C DIR, --directory DIR Change to directory DIR to read/extract files\n"
 
11137 "\n"
11138 "See also: http://sqlite.org/cli.html#sqlar_archive_support\n"
11139 "\n"
11140 );
11141 return SQLITE_ERROR;
@@ -11166,14 +11546,15 @@
11166 #define AR_CMD_HELP 5
11167
11168 /*
11169 ** Other (non-command) switches.
11170 */
11171 #define AR_SWITCH_VERBOSE 6
11172 #define AR_SWITCH_FILE 7
11173 #define AR_SWITCH_DIRECTORY 8
11174 #define AR_SWITCH_ZIP 9
 
11175
11176 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
11177 switch( eSwitch ){
11178 case AR_CMD_CREATE:
11179 case AR_CMD_EXTRACT:
@@ -11184,17 +11565,19 @@
11184 return arErrorMsg("multiple command options");
11185 }
11186 pAr->eCmd = eSwitch;
11187 break;
11188
 
 
 
11189 case AR_SWITCH_VERBOSE:
11190 pAr->bVerbose = 1;
11191 break;
11192 case AR_SWITCH_ZIP:
11193 pAr->bZip = 1;
11194 break;
11195
11196 case AR_SWITCH_FILE:
11197 pAr->zFile = zArg;
11198 break;
11199 case AR_SWITCH_DIRECTORY:
11200 pAr->zDir = zArg;
@@ -11214,24 +11597,25 @@
11214 char **azArg, /* Array of arguments passed to dot command */
11215 int nArg, /* Number of entries in azArg[] */
11216 ArCommand *pAr /* Populate this object */
11217 ){
11218 struct ArSwitch {
11219 char cShort;
11220 const char *zLong;
11221 int eSwitch;
11222 int bArg;
 
11223 } aSwitch[] = {
11224 { 'c', "create", AR_CMD_CREATE, 0 },
11225 { 'x', "extract", AR_CMD_EXTRACT, 0 },
11226 { 't', "list", AR_CMD_LIST, 0 },
11227 { 'u', "update", AR_CMD_UPDATE, 0 },
11228 { 'h', "help", AR_CMD_HELP, 0 },
11229 { 'v', "verbose", AR_SWITCH_VERBOSE, 0 },
11230 { 'f', "file", AR_SWITCH_FILE, 1 },
11231 { 'C', "directory", AR_SWITCH_DIRECTORY, 1 },
11232 { 'z', "zip", AR_SWITCH_ZIP, 0 }
 
11233 };
11234 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
11235 struct ArSwitch *pEnd = &aSwitch[nSwitch];
11236
11237 if( nArg<=1 ){
@@ -11354,41 +11738,39 @@
11354 **
11355 ** This function strips any trailing '/' characters from each argument.
11356 ** This is consistent with the way the [tar] command seems to work on
11357 ** Linux.
11358 */
11359 static int arCheckEntries(sqlite3 *db, ArCommand *pAr){
11360 int rc = SQLITE_OK;
11361 if( pAr->nArg ){
11362 int i;
11363 sqlite3_stmt *pTest = 0;
11364
11365 shellPreparePrintf(db, &rc, &pTest, "SELECT name FROM %s WHERE name=?1",
11366 pAr->bZip ? "zipfile(?2)" : "sqlar"
 
11367 );
11368 if( rc==SQLITE_OK && pAr->bZip ){
11369 sqlite3_bind_text(pTest, 2, pAr->zFile, -1, SQLITE_TRANSIENT);
11370 }
11371 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
11372 char *z = pAr->azArg[i];
11373 int n = strlen30(z);
11374 int bOk = 0;
11375 while( n>0 && z[n-1]=='/' ) n--;
11376 z[n] = '\0';
11377 sqlite3_bind_text(pTest, 1, z, -1, SQLITE_STATIC);
11378 if( SQLITE_ROW==sqlite3_step(pTest) ){
11379 bOk = 1;
11380 }
11381 shellReset(&rc, pTest);
11382 if( rc==SQLITE_OK && bOk==0 ){
11383 raw_printf(stderr, "not found in archive: %s\n", z);
11384 rc = SQLITE_ERROR;
11385 }
11386 }
11387 shellFinalize(&rc, pTest);
11388 }
11389
11390 return rc;
11391 }
11392
11393 /*
11394 ** Format a WHERE clause that can be used against the "sqlar" table to
@@ -11410,13 +11792,13 @@
11410 int i;
11411 const char *zSep = "";
11412 for(i=0; i<pAr->nArg; i++){
11413 const char *z = pAr->azArg[i];
11414 zWhere = sqlite3_mprintf(
11415 "%z%s name = '%q' OR name BETWEEN '%q/' AND '%q0'",
11416 zWhere, zSep, z, z, z
11417 );
11418 if( zWhere==0 ){
11419 *pRc = SQLITE_NOMEM;
11420 break;
11421 }
11422 zSep = " OR ";
@@ -11424,107 +11806,75 @@
11424 }
11425 }
11426 *pzWhere = zWhere;
11427 }
11428
11429 /*
11430 ** Argument zMode must point to a buffer at least 11 bytes in size. This
11431 ** function populates this buffer with the string interpretation of
11432 ** the unix file mode passed as the second argument (e.g. "drwxr-xr-x").
11433 */
11434 static void shellModeToString(char *zMode, int mode){
11435 int i;
11436
11437 /* Magic numbers copied from [man 2 stat] */
11438 if( mode & 0040000 ){
11439 zMode[0] = 'd';
11440 }else if( (mode & 0120000)==0120000 ){
11441 zMode[0] = 'l';
11442 }else{
11443 zMode[0] = '-';
11444 }
11445
11446 for(i=0; i<3; i++){
11447 int m = (mode >> ((2-i)*3));
11448 char *a = &zMode[1 + i*3];
11449 a[0] = (m & 0x4) ? 'r' : '-';
11450 a[1] = (m & 0x2) ? 'w' : '-';
11451 a[2] = (m & 0x1) ? 'x' : '-';
11452 }
11453 zMode[10] = '\0';
11454 }
11455
11456 /*
11457 ** Implementation of .ar "lisT" command.
11458 */
11459 static int arListCommand(ShellState *p, sqlite3 *db, ArCommand *pAr){
11460 const char *zSql = "SELECT %s FROM %s WHERE %s";
11461 const char *zTbl = (pAr->bZip ? "zipfile(?)" : "sqlar");
11462 const char *azCols[] = {
11463 "name",
11464 "mode, sz, datetime(mtime, 'unixepoch'), name"
11465 };
11466
11467 char *zWhere = 0;
11468 sqlite3_stmt *pSql = 0;
11469 int rc;
11470
11471 rc = arCheckEntries(db, pAr);
11472 arWhereClause(&rc, pAr, &zWhere);
11473
11474 shellPreparePrintf(db, &rc, &pSql, zSql, azCols[pAr->bVerbose], zTbl, zWhere);
11475 if( rc==SQLITE_OK && pAr->bZip ){
11476 sqlite3_bind_text(pSql, 1, pAr->zFile, -1, SQLITE_TRANSIENT);
11477 }
11478 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
11479 if( pAr->bVerbose ){
11480 char zMode[11];
11481 shellModeToString(zMode, sqlite3_column_int(pSql, 0));
11482
11483 raw_printf(p->out, "%s % 10d %s %s\n", zMode,
11484 sqlite3_column_int(pSql, 1),
11485 sqlite3_column_text(pSql, 2),
11486 sqlite3_column_text(pSql, 3)
11487 );
11488 }else{
11489 raw_printf(p->out, "%s\n", sqlite3_column_text(pSql, 0));
11490 }
11491 }
11492
11493 shellFinalize(&rc, pSql);
11494 return rc;
11495 }
11496
11497
11498 /*
11499 ** Implementation of .ar "eXtract" command.
11500 */
11501 static int arExtractCommand(ShellState *p, sqlite3 *db, ArCommand *pAr){
11502 const char *zSql1 =
11503 "SELECT "
11504 " :1 || name, "
11505 " writefile(?1 || name, %s, mode, mtime) "
11506 "FROM %s WHERE (%s) AND (data IS NULL OR ?2 = 0)";
11507
11508 const char *azExtraArg[] = {
11509 "sqlar_uncompress(data, sz)",
11510 "data"
11511 };
11512 const char *azSource[] = {
11513 "sqlar", "zipfile(?3)"
11514 };
11515
11516 sqlite3_stmt *pSql = 0;
11517 int rc = SQLITE_OK;
11518 char *zDir = 0;
11519 char *zWhere = 0;
11520 int i;
11521
11522 /* If arguments are specified, check that they actually exist within
11523 ** the archive before proceeding. And formulate a WHERE clause to
11524 ** match them. */
11525 rc = arCheckEntries(db, pAr);
11526 arWhereClause(&rc, pAr, &zWhere);
11527
11528 if( rc==SQLITE_OK ){
11529 if( pAr->zDir ){
11530 zDir = sqlite3_mprintf("%s/", pAr->zDir);
@@ -11532,30 +11882,33 @@
11532 zDir = sqlite3_mprintf("");
11533 }
11534 if( zDir==0 ) rc = SQLITE_NOMEM;
11535 }
11536
11537 shellPreparePrintf(db, &rc, &pSql, zSql1,
11538 azExtraArg[pAr->bZip], azSource[pAr->bZip], zWhere
11539 );
11540
11541 if( rc==SQLITE_OK ){
11542 sqlite3_bind_text(pSql, 1, zDir, -1, SQLITE_STATIC);
11543 if( pAr->bZip ){
11544 sqlite3_bind_text(pSql, 3, pAr->zFile, -1, SQLITE_STATIC);
11545 }
11546
11547 /* Run the SELECT statement twice. The first time, writefile() is called
11548 ** for all archive members that should be extracted. The second time,
11549 ** only for the directories. This is because the timestamps for
11550 ** extracted directories must be reset after they are populated (as
11551 ** populating them changes the timestamp). */
11552 for(i=0; i<2; i++){
11553 sqlite3_bind_int(pSql, 2, i);
11554 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
11555 if( i==0 && pAr->bVerbose ){
11556 raw_printf(p->out, "%s\n", sqlite3_column_text(pSql, 0));
 
 
 
 
 
11557 }
11558 }
11559 shellReset(&rc, pSql);
11560 }
11561 shellFinalize(&rc, pSql);
@@ -11563,10 +11916,29 @@
11563
11564 sqlite3_free(zDir);
11565 sqlite3_free(zWhere);
11566 return rc;
11567 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11568
11569
11570 /*
11571 ** Implementation of .ar "create" and "update" commands.
11572 **
@@ -11576,116 +11948,60 @@
11576 ** printed on stdout for each file archived.
11577 **
11578 ** The create command is the same as update, except that it drops
11579 ** any existing "sqlar" table before beginning.
11580 */
11581 static int arCreateUpdate(
11582 ShellState *p, /* Shell state pointer */
11583 sqlite3 *db,
11584 ArCommand *pAr, /* Command arguments and options */
11585 int bUpdate
11586 ){
11587 const char *zSql = "SELECT name, mode, mtime, data FROM fsdir(?, ?)";
11588 const char *zCreate =
11589 "CREATE TABLE IF NOT EXISTS sqlar(\n"
11590 " name TEXT PRIMARY KEY, -- name of the file\n"
11591 " mode INT, -- access permissions\n"
11592 " mtime INT, -- last modification time\n"
11593 " sz INT, -- original file size\n"
11594 " data BLOB -- compressed content\n"
11595 ")";
11596 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
11597 const char *zInsert = "REPLACE INTO sqlar VALUES(?,?,?,?,sqlar_compress(?))";
11598
11599 sqlite3_stmt *pStmt = 0; /* Directory traverser */
11600 sqlite3_stmt *pInsert = 0; /* Compilation of zInsert */
 
 
 
 
 
 
 
 
 
11601 int i; /* For iterating through azFile[] */
11602 int rc; /* Return code */
11603
11604 assert( pAr->bZip==0 );
11605
11606 rc = sqlite3_exec(db, "SAVEPOINT ar;", 0, 0, 0);
11607 if( rc!=SQLITE_OK ) return rc;
11608
11609 if( bUpdate==0 ){
11610 rc = sqlite3_exec(db, zDrop, 0, 0, 0);
11611 if( rc!=SQLITE_OK ) return rc;
11612 }
11613
11614 rc = sqlite3_exec(db, zCreate, 0, 0, 0);
11615 shellPrepare(db, &rc, zInsert, &pInsert);
11616 shellPrepare(db, &rc, zSql, &pStmt);
11617 sqlite3_bind_text(pStmt, 2, pAr->zDir, -1, SQLITE_STATIC);
11618
11619 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
11620 sqlite3_bind_text(pStmt, 1, pAr->azArg[i], -1, SQLITE_STATIC);
11621 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
11622 int sz;
11623 const char *zName = (const char*)sqlite3_column_text(pStmt, 0);
11624 int mode = sqlite3_column_int(pStmt, 1);
11625 unsigned int mtime = sqlite3_column_int(pStmt, 2);
11626
11627 if( pAr->bVerbose ){
11628 raw_printf(p->out, "%s\n", zName);
11629 }
11630
11631 sqlite3_bind_text(pInsert, 1, zName, -1, SQLITE_STATIC);
11632 sqlite3_bind_int(pInsert, 2, mode);
11633 sqlite3_bind_int64(pInsert, 3, (sqlite3_int64)mtime);
11634
11635 if( S_ISDIR(mode) ){
11636 sz = 0;
11637 sqlite3_bind_null(pInsert, 5);
11638 }else{
11639 sqlite3_bind_value(pInsert, 5, sqlite3_column_value(pStmt, 3));
11640 if( S_ISLNK(mode) ){
11641 sz = -1;
11642 }else{
11643 sz = sqlite3_column_bytes(pStmt, 3);
11644 }
11645 }
11646
11647 sqlite3_bind_int(pInsert, 4, sz);
11648 sqlite3_step(pInsert);
11649 rc = sqlite3_reset(pInsert);
11650 }
11651 shellReset(&rc, pStmt);
11652 }
11653
11654 if( rc!=SQLITE_OK ){
11655 sqlite3_exec(db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
11656 }else{
11657 rc = sqlite3_exec(db, "RELEASE ar;", 0, 0, 0);
11658 }
11659 shellFinalize(&rc, pStmt);
11660 shellFinalize(&rc, pInsert);
11661 return rc;
11662 }
11663
11664 /*
11665 ** Implementation of .ar "Create" command.
11666 **
11667 ** Create the "sqlar" table in the database if it does not already exist.
11668 ** Then add each file in the azFile[] array to the archive. Directories
11669 ** are added recursively. If argument bVerbose is non-zero, a message is
11670 ** printed on stdout for each file archived.
11671 */
11672 static int arCreateCommand(
11673 ShellState *p, /* Shell state pointer */
11674 sqlite3 *db,
11675 ArCommand *pAr /* Command arguments and options */
11676 ){
11677 return arCreateUpdate(p, db, pAr, 0);
11678 }
11679
11680 /*
11681 ** Implementation of .ar "Update" command.
11682 */
11683 static int arUpdateCmd(ShellState *p, sqlite3 *db, ArCommand *pAr){
11684 return arCreateUpdate(p, db, pAr, 1);
11685 }
11686
11687
11688 /*
11689 ** Implementation of ".ar" dot command.
11690 */
11691 static int arDotCommand(
@@ -11693,138 +12009,108 @@
11693 char **azArg, /* Array of arguments passed to dot command */
11694 int nArg /* Number of entries in azArg[] */
11695 ){
11696 ArCommand cmd;
11697 int rc;
 
11698 rc = arParseCommand(azArg, nArg, &cmd);
11699 if( rc==SQLITE_OK ){
11700 sqlite3 *db = 0; /* Database handle to use as archive */
11701
11702 if( cmd.bZip ){
 
 
 
 
 
 
11703 if( cmd.zFile==0 ){
11704 raw_printf(stderr, "zip format requires a --file switch\n");
11705 return SQLITE_ERROR;
11706 }else
 
11707 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
11708 raw_printf(stderr, "zip archives are read-only\n");
11709 return SQLITE_ERROR;
 
11710 }
11711 db = pState->db;
11712 }else if( cmd.zFile ){
11713 int flags;
 
11714 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
11715 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
11716 }else{
11717 flags = SQLITE_OPEN_READONLY;
11718 }
11719 rc = sqlite3_open_v2(cmd.zFile, &db, flags, 0);
 
 
 
 
 
 
11720 if( rc!=SQLITE_OK ){
11721 raw_printf(stderr, "cannot open file: %s (%s)\n",
11722 cmd.zFile, sqlite3_errmsg(db)
11723 );
11724 sqlite3_close(db);
11725 return rc;
11726 }
11727 sqlite3_fileio_init(db, 0, 0);
11728 #ifdef SQLITE_HAVE_ZLIB
11729 sqlite3_sqlar_init(db, 0, 0);
11730 #endif
11731 }else{
11732 db = pState->db;
 
 
 
 
 
 
 
 
 
 
 
11733 }
11734
11735 switch( cmd.eCmd ){
11736 case AR_CMD_CREATE:
11737 rc = arCreateCommand(pState, db, &cmd);
11738 break;
11739
11740 case AR_CMD_EXTRACT:
11741 rc = arExtractCommand(pState, db, &cmd);
11742 break;
11743
11744 case AR_CMD_LIST:
11745 rc = arListCommand(pState, db, &cmd);
11746 break;
11747
11748 case AR_CMD_HELP:
11749 arUsage(pState->out);
11750 break;
11751
11752 default:
11753 assert( cmd.eCmd==AR_CMD_UPDATE );
11754 rc = arUpdateCmd(pState, db, &cmd);
11755 break;
11756 }
11757
11758 if( cmd.zFile ){
11759 sqlite3_close(db);
11760 }
11761 }
 
11762
11763 return rc;
11764 }
11765 /* End of the ".archive" or ".ar" command logic
11766 **********************************************************************************/
11767 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
11768
11769 /*
11770 ** Implementation of ".expert" dot command.
11771 */
11772 static int expertDotCommand(
11773 ShellState *pState, /* Current shell tool state */
11774 char **azArg, /* Array of arguments passed to dot command */
11775 int nArg /* Number of entries in azArg[] */
11776 ){
11777 int rc = SQLITE_OK;
11778 char *zErr = 0;
11779 int i;
11780 int iSample = 0;
11781
11782 assert( pState->expert.pExpert==0 );
11783 memset(&pState->expert, 0, sizeof(ExpertInfo));
11784
11785 for(i=1; rc==SQLITE_OK && i<nArg; i++){
11786 char *z = azArg[i];
11787 int n;
11788 if( z[0]=='-' && z[1]=='-' ) z++;
11789 n = strlen30(z);
11790 if( n>=2 && 0==strncmp(z, "-verbose", n) ){
11791 pState->expert.bVerbose = 1;
11792 }
11793 else if( n>=2 && 0==strncmp(z, "-sample", n) ){
11794 if( i==(nArg-1) ){
11795 raw_printf(stderr, "option requires an argument: %s\n", z);
11796 rc = SQLITE_ERROR;
11797 }else{
11798 iSample = (int)integerValue(azArg[++i]);
11799 if( iSample<0 || iSample>100 ){
11800 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
11801 rc = SQLITE_ERROR;
11802 }
11803 }
11804 }
11805 else{
11806 raw_printf(stderr, "unknown option: %s\n", z);
11807 rc = SQLITE_ERROR;
11808 }
11809 }
11810
11811 if( rc==SQLITE_OK ){
11812 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
11813 if( pState->expert.pExpert==0 ){
11814 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
11815 rc = SQLITE_ERROR;
11816 }else{
11817 sqlite3_expert_config(
11818 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
11819 );
11820 }
11821 }
11822
11823 return rc;
11824 }
11825
11826
11827 /*
11828 ** If an input line begins with "." then invoke this routine to
11829 ** process that line.
11830 **
@@ -11835,13 +12121,15 @@
11835 int nArg = 0;
11836 int n, c;
11837 int rc = 0;
11838 char *azArg[50];
11839
 
11840 if( p->expert.pExpert ){
11841 expertFinish(p, 1, 0);
11842 }
 
11843
11844 /* Parse the input line into tokens.
11845 */
11846 while( zLine[h] && nArg<ArraySize(azArg) ){
11847 while( IsSpace(zLine[h]) ){ h++; }
@@ -11868,10 +12156,11 @@
11868 /* Process the input line.
11869 */
11870 if( nArg==0 ) return 0; /* no tokens, no error */
11871 n = strlen30(azArg[0]);
11872 c = azArg[0][0];
 
11873
11874 #ifndef SQLITE_OMIT_AUTHORIZATION
11875 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
11876 if( nArg!=2 ){
11877 raw_printf(stderr, "Usage: .auth ON|OFF\n");
@@ -12202,14 +12491,16 @@
12202 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
12203 p->autoExplain = 1;
12204 }
12205 }else
12206
 
12207 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
12208 open_db(p, 0);
12209 expertDotCommand(p, azArg, nArg);
12210 }else
 
12211
12212 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
12213 ShellState data;
12214 char *zErrMsg = 0;
12215 int doStats = 0;
@@ -12661,11 +12952,11 @@
12661 raw_printf(stderr, "Usage: .log FILENAME\n");
12662 rc = 1;
12663 }else{
12664 const char *zFile = azArg[1];
12665 output_file_close(p->pLog);
12666 p->pLog = output_file_open(zFile);
12667 }
12668 }else
12669
12670 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
12671 const char *zMode = nArg>=2 ? azArg[1] : "";
@@ -12770,30 +13061,54 @@
12770 p->zDbFilename = 0;
12771 open_db(p, 0);
12772 }
12773 }else
12774
12775 if( c=='o'
12776 && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0)
 
12777 ){
12778 const char *zFile = nArg>=2 ? azArg[1] : "stdout";
 
 
 
 
 
 
 
 
12779 if( nArg>2 ){
12780 utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]);
12781 rc = 1;
12782 goto meta_command_exit;
12783 }
12784 if( n>1 && strncmp(azArg[0], "once", n)==0 ){
12785 if( nArg<2 ){
12786 raw_printf(stderr, "Usage: .once FILE\n");
12787 rc = 1;
12788 goto meta_command_exit;
12789 }
12790 p->outCount = 2;
12791 }else{
12792 p->outCount = 0;
12793 }
12794 output_reset(p);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12795 if( zFile[0]=='|' ){
12796 #ifdef SQLITE_OMIT_POPEN
12797 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
12798 rc = 1;
12799 p->out = stdout;
@@ -12806,11 +13121,11 @@
12806 }else{
12807 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
12808 }
12809 #endif
12810 }else{
12811 p->out = output_file_open(zFile);
12812 if( p->out==0 ){
12813 if( strcmp(zFile,"off")!=0 ){
12814 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
12815 }
12816 p->out = stdout;
@@ -13683,11 +13998,11 @@
13683 }else
13684
13685 /* Begin redirecting output to the file "testcase-out.txt" */
13686 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
13687 output_reset(p);
13688 p->out = output_file_open("testcase-out.txt");
13689 if( p->out==0 ){
13690 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
13691 }
13692 if( nArg>=2 ){
13693 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
@@ -13889,11 +14204,11 @@
13889 raw_printf(stderr, "Usage: .trace FILE|off\n");
13890 rc = 1;
13891 goto meta_command_exit;
13892 }
13893 output_file_close(p->traceOut);
13894 p->traceOut = output_file_open(azArg[1]);
13895 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
13896 if( p->traceOut==0 ){
13897 sqlite3_trace_v2(p->db, 0, 0, 0);
13898 }else{
13899 sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
@@ -14219,10 +14534,12 @@
14219 errCnt += runOneSqlLine(p, zSql, in, startline);
14220 nSql = 0;
14221 if( p->outCount ){
14222 output_reset(p);
14223 p->outCount = 0;
 
 
14224 }
14225 }else if( nSql && _all_whitespace(zSql) ){
14226 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
14227 nSql = 0;
14228 }
@@ -14841,11 +15158,14 @@
14841 session_close_all(&data);
14842 sqlite3_close(data.db);
14843 }
14844 sqlite3_free(data.zFreeOnClose);
14845 find_home_dir(1);
 
 
 
14846 #if !SQLITE_SHELL_IS_UTF8
14847 for(i=0; i<argc; i++) sqlite3_free(argv[i]);
14848 sqlite3_free(argv);
14849 #endif
14850 return rc;
14851 }
14852
--- src/shell.c
+++ src/shell.c
@@ -368,10 +368,15 @@
368 /*
369 ** Used to prevent warnings about unused parameters
370 */
371 #define UNUSED_PARAMETER(x) (void)(x)
372
373 /*
374 ** Number of elements in an array
375 */
376 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
377
378 /*
379 ** If the following flag is set, then command execution stops
380 ** at an error if we are not interactive.
381 */
382 static int bail_on_error = 0;
@@ -640,10 +645,69 @@
645 if( zResult && *zResult ) shell_add_history(zResult);
646 #endif
647 }
648 return zResult;
649 }
650
651
652 /*
653 ** Return the value of a hexadecimal digit. Return -1 if the input
654 ** is not a hex digit.
655 */
656 static int hexDigitValue(char c){
657 if( c>='0' && c<='9' ) return c - '0';
658 if( c>='a' && c<='f' ) return c - 'a' + 10;
659 if( c>='A' && c<='F' ) return c - 'A' + 10;
660 return -1;
661 }
662
663 /*
664 ** Interpret zArg as an integer value, possibly with suffixes.
665 */
666 static sqlite3_int64 integerValue(const char *zArg){
667 sqlite3_int64 v = 0;
668 static const struct { char *zSuffix; int iMult; } aMult[] = {
669 { "KiB", 1024 },
670 { "MiB", 1024*1024 },
671 { "GiB", 1024*1024*1024 },
672 { "KB", 1000 },
673 { "MB", 1000000 },
674 { "GB", 1000000000 },
675 { "K", 1000 },
676 { "M", 1000000 },
677 { "G", 1000000000 },
678 };
679 int i;
680 int isNeg = 0;
681 if( zArg[0]=='-' ){
682 isNeg = 1;
683 zArg++;
684 }else if( zArg[0]=='+' ){
685 zArg++;
686 }
687 if( zArg[0]=='0' && zArg[1]=='x' ){
688 int x;
689 zArg += 2;
690 while( (x = hexDigitValue(zArg[0]))>=0 ){
691 v = (v<<4) + x;
692 zArg++;
693 }
694 }else{
695 while( IsDigit(zArg[0]) ){
696 v = v*10 + zArg[0] - '0';
697 zArg++;
698 }
699 }
700 for(i=0; i<ArraySize(aMult); i++){
701 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
702 v *= aMult[i].iMult;
703 break;
704 }
705 }
706 return isNeg? -v : v;
707 }
708
709 /*
710 ** A variable length string to which one can append text.
711 */
712 typedef struct ShellText ShellText;
713 struct ShellText {
@@ -2334,10 +2398,44 @@
2398 }else{
2399 ctxErrorMsg(context, "failed to write file: %s", zFile);
2400 }
2401 }
2402 }
2403
2404 /*
2405 ** SQL function: lsmode(MODE)
2406 **
2407 ** Given a numberic st_mode from stat(), convert it into a human-readable
2408 ** text string in the style of "ls -l".
2409 */
2410 static void lsModeFunc(
2411 sqlite3_context *context,
2412 int argc,
2413 sqlite3_value **argv
2414 ){
2415 int i;
2416 int iMode = sqlite3_value_int(argv[0]);
2417 char z[16];
2418 if( S_ISLNK(iMode) ){
2419 z[0] = 'l';
2420 }else if( S_ISREG(iMode) ){
2421 z[0] = '-';
2422 }else if( S_ISDIR(iMode) ){
2423 z[0] = 'd';
2424 }else{
2425 z[0] = '?';
2426 }
2427 for(i=0; i<3; i++){
2428 int m = (iMode >> ((2-i)*3));
2429 char *a = &z[1 + i*3];
2430 a[0] = (m & 0x4) ? 'r' : '-';
2431 a[1] = (m & 0x2) ? 'w' : '-';
2432 a[2] = (m & 0x1) ? 'x' : '-';
2433 }
2434 z[10] = '\0';
2435 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2436 }
2437
2438 #ifndef SQLITE_OMIT_VIRTUALTABLE
2439
2440 /*
2441 ** Cursor type for recursively iterating through a directory structure.
@@ -2746,10 +2844,14 @@
2844 readfileFunc, 0, 0);
2845 if( rc==SQLITE_OK ){
2846 rc = sqlite3_create_function(db, "writefile", -1, SQLITE_UTF8, 0,
2847 writefileFunc, 0, 0);
2848 }
2849 if( rc==SQLITE_OK ){
2850 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
2851 lsModeFunc, 0, 0);
2852 }
2853 if( rc==SQLITE_OK ){
2854 rc = fsdirRegister(db);
2855 }
2856 return rc;
2857 }
@@ -4879,11 +4981,11 @@
4981 if( pNew==0 ){
4982 rc = SQLITE_NOMEM;
4983 }else{
4984 memset(pNew, 0, sizeof(ZipfileEntry));
4985 pNew->zPath = (char*)&pNew[1];
4986 memcpy(pNew->zPath, &aRec[ZIPFILE_CDS_FIXED_SZ], nFile);
4987 pNew->zPath[nFile] = '\0';
4988 pNew->aCdsEntry = (u8*)&pNew->zPath[nFile+1];
4989 pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
4990 memcpy(pNew->aCdsEntry, aRec, pNew->nCdsEntry);
4991 zipfileAddEntry(pTab, pNew);
@@ -5034,10 +5136,22 @@
5136
5137 parse_error:
5138 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5139 return SQLITE_ERROR;
5140 }
5141
5142 /*
5143 ** Both (const char*) arguments point to nul-terminated strings. Argument
5144 ** nB is the value of strlen(zB). This function returns 0 if the strings are
5145 ** identical, ignoring any trailing '/' character in either path. */
5146 static int zipfileComparePath(const char *zA, const char *zB, int nB){
5147 int nA = (int)strlen(zA);
5148 if( zA[nA-1]=='/' ) nA--;
5149 if( zB[nB-1]=='/' ) nB--;
5150 if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5151 return 1;
5152 }
5153
5154 /*
5155 ** xUpdate method.
5156 */
5157 static int zipfileUpdate(
@@ -5048,19 +5162,20 @@
5162 ){
5163 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5164 int rc = SQLITE_OK; /* Return Code */
5165 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
5166
5167 u32 mode = 0; /* Mode for new entry */
5168 i64 mTime = 0; /* Modification time for new entry */
5169 i64 sz = 0; /* Uncompressed size */
5170 const char *zPath = 0; /* Path for new entry */
5171 int nPath = 0; /* strlen(zPath) */
5172 const u8 *pData = 0; /* Pointer to buffer containing content */
5173 int nData = 0; /* Size of pData buffer in bytes */
5174 int iMethod = 0; /* Compression method for new entry */
5175 u8 *pFree = 0; /* Free this */
5176 char *zFree = 0; /* Also free this */
5177 ZipfileCDS cds; /* New Central Directory Structure entry */
5178
5179 int bIsDir = 0;
5180
5181 int mNull;
@@ -5067,19 +5182,23 @@
5182
5183 assert( pTab->zFile );
5184 assert( pTab->pWriteFd );
5185
5186 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5187 if( nVal>1 ){
5188 return SQLITE_CONSTRAINT;
5189 }else{
5190 i64 iDelete = sqlite3_value_int64(apVal[0]);
5191 ZipfileEntry *p;
5192 for(p=pTab->pFirstEntry; p; p=p->pNext){
5193 if( p->iRowid==iDelete ){
5194 p->bDeleted = 1;
5195 break;
5196 }
5197 }
5198 return SQLITE_OK;
5199 }
5200 }
5201
5202 mNull = (sqlite3_value_type(apVal[5])==SQLITE_NULL ? 0x0 : 0x8) /* sz */
5203 + (sqlite3_value_type(apVal[6])==SQLITE_NULL ? 0x0 : 0x4) /* rawdata */
5204 + (sqlite3_value_type(apVal[7])==SQLITE_NULL ? 0x0 : 0x2) /* data */
@@ -5148,10 +5267,34 @@
5267 mTime = (sqlite3_int64)time(0);
5268 }else{
5269 mTime = sqlite3_value_int64(apVal[4]);
5270 }
5271 }
5272
5273 if( rc==SQLITE_OK && bIsDir ){
5274 /* For a directory, check that the last character in the path is a
5275 ** '/'. This appears to be required for compatibility with info-zip
5276 ** (the unzip command on unix). It does not create directories
5277 ** otherwise. */
5278 if( zPath[nPath-1]!='/' ){
5279 zFree = sqlite3_mprintf("%s/", zPath);
5280 if( zFree==0 ){ rc = SQLITE_NOMEM; }
5281 zPath = (const char*)zFree;
5282 nPath++;
5283 }
5284 }
5285
5286 /* Check that we're not inserting a duplicate entry */
5287 if( rc==SQLITE_OK ){
5288 ZipfileEntry *p;
5289 for(p=pTab->pFirstEntry; p; p=p->pNext){
5290 if( zipfileComparePath(p->zPath, zPath, nPath)==0 ){
5291 rc = SQLITE_CONSTRAINT;
5292 break;
5293 }
5294 }
5295 }
5296
5297 if( rc==SQLITE_OK ){
5298 /* Create the new CDS record. */
5299 memset(&cds, 0, sizeof(cds));
5300 cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
@@ -5176,10 +5319,11 @@
5319 if( rc==SQLITE_OK ){
5320 rc = zipfileAppendEntry(pTab, &cds, zPath, nPath, pData, nData, (u32)mTime);
5321 }
5322
5323 sqlite3_free(pFree);
5324 sqlite3_free(zFree);
5325 return rc;
5326 }
5327
5328 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
5329 u8 *aBuf = pTab->aBuffer;
@@ -5329,11 +5473,10 @@
5473 ){
5474 SQLITE_EXTENSION_INIT2(pApi);
5475 (void)pzErrMsg; /* Unused parameter */
5476 return zipfileRegister(db);
5477 }
 
5478
5479 /************************* End ../ext/misc/zipfile.c ********************/
5480 /************************* Begin ../ext/misc/sqlar.c ******************/
5481 /*
5482 ** 2017-12-17
@@ -5642,10 +5785,12 @@
5785 *************************************************************************
5786 */
5787 #include <assert.h>
5788 #include <string.h>
5789 #include <stdio.h>
5790
5791 #ifndef SQLITE_OMIT_VIRTUALTABLE
5792
5793 /* typedef sqlite3_int64 i64; */
5794 /* typedef sqlite3_uint64 u64; */
5795
5796 typedef struct IdxColumn IdxColumn;
@@ -7151,10 +7296,11 @@
7296 zCols = idxAppendText(&rc, zCols,
7297 "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
7298 );
7299 zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
7300 }
7301 sqlite3_reset(pIndexXInfo);
7302 if( rc==SQLITE_OK ){
7303 if( p->iSample==100 ){
7304 zQuery = sqlite3_mprintf(
7305 "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
7306 );
@@ -7560,10 +7706,12 @@
7706 idxHashClear(&p->hIdx);
7707 sqlite3_free(p->zCandidates);
7708 sqlite3_free(p);
7709 }
7710 }
7711
7712 #endif /* ifndef SQLITE_OMIT_VIRTUAL_TABLE */
7713
7714 /************************* End ../ext/expert/sqlite3expert.c ********************/
7715
7716 #if defined(SQLITE_ENABLE_SESSION)
7717 /*
@@ -7606,26 +7754,31 @@
7754 u8 autoExplain; /* Automatically turn on .explain mode */
7755 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
7756 u8 statsOn; /* True to display memory stats before each finalize */
7757 u8 scanstatsOn; /* True to display scan stats before each finalize */
7758 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
7759 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
7760 int outCount; /* Revert to stdout when reaching zero */
7761 int cnt; /* Number of records displayed so far */
7762 FILE *out; /* Write results here */
7763 FILE *traceOut; /* Output for sqlite3_trace() */
7764 int nErr; /* Number of errors seen */
7765 int mode; /* An output mode setting */
7766 int modePrior; /* Saved mode */
7767 int cMode; /* temporary output mode for the current query */
7768 int normalMode; /* Output mode before ".explain on" */
7769 int writableSchema; /* True if PRAGMA writable_schema=ON */
7770 int showHeader; /* True to show column names in List or Column mode */
7771 int nCheck; /* Number of ".check" commands run */
7772 unsigned shellFlgs; /* Various flags */
7773 char *zDestTable; /* Name of destination table when MODE_Insert */
7774 char *zTempFile; /* Temporary file that might need deleting */
7775 char zTestcase[30]; /* Name of current test case */
7776 char colSeparator[20]; /* Column separator character for several modes */
7777 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
7778 char colSepPrior[20]; /* Saved column separator */
7779 char rowSepPrior[20]; /* Saved row separator */
7780 int colWidth[100]; /* Requested width of each column when in column mode*/
7781 int actualWidth[100]; /* Actual width of each column */
7782 char nullValue[20]; /* The text to print when a NULL comes back from
7783 ** the database */
7784 char outfile[FILENAME_MAX]; /* Filename for *out */
@@ -7719,24 +7872,175 @@
7872 #define SEP_Comma ","
7873 #define SEP_CrLf "\r\n"
7874 #define SEP_Unit "\x1F"
7875 #define SEP_Record "\x1E"
7876
 
 
 
 
 
7877 /*
7878 ** A callback for the sqlite3_log() interface.
7879 */
7880 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
7881 ShellState *p = (ShellState*)pArg;
7882 if( p->pLog==0 ) return;
7883 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
7884 fflush(p->pLog);
7885 }
7886
7887 /*
7888 ** SQL function: shell_putsnl(X)
7889 **
7890 ** Write the text X to the screen (or whatever output is being directed)
7891 ** adding a newline at the end, and then return X.
7892 */
7893 static void shellPutsFunc(
7894 sqlite3_context *pCtx,
7895 int nVal,
7896 sqlite3_value **apVal
7897 ){
7898 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
7899 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
7900 sqlite3_result_value(pCtx, apVal[0]);
7901 }
7902
7903 /*
7904 ** SQL function: edit(VALUE)
7905 ** edit(VALUE,EDITOR)
7906 **
7907 ** These steps:
7908 **
7909 ** (1) Write VALUE into a temporary file.
7910 ** (2) Run program EDITOR on that temporary file.
7911 ** (3) Read the temporary file back and return its content as the result.
7912 ** (4) Delete the temporary file
7913 **
7914 ** If the EDITOR argument is omitted, use the value in the VISUAL
7915 ** environment variable. If still there is no EDITOR, through an error.
7916 **
7917 ** Also throw an error if the EDITOR program returns a non-zero exit code.
7918 */
7919 static void editFunc(
7920 sqlite3_context *context,
7921 int argc,
7922 sqlite3_value **argv
7923 ){
7924 const char *zEditor;
7925 char *zTempFile = 0;
7926 sqlite3 *db;
7927 char *zCmd = 0;
7928 int bBin;
7929 int rc;
7930 FILE *f = 0;
7931 sqlite3_int64 sz;
7932 sqlite3_int64 x;
7933 unsigned char *p = 0;
7934
7935 if( argc==2 ){
7936 zEditor = (const char*)sqlite3_value_text(argv[1]);
7937 }else{
7938 zEditor = getenv("VISUAL");
7939 }
7940 if( zEditor==0 ){
7941 sqlite3_result_error(context, "no editor for edit()", -1);
7942 return;
7943 }
7944 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
7945 sqlite3_result_error(context, "NULL input to edit()", -1);
7946 return;
7947 }
7948 db = sqlite3_context_db_handle(context);
7949 zTempFile = 0;
7950 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
7951 if( zTempFile==0 ){
7952 sqlite3_uint64 r = 0;
7953 sqlite3_randomness(sizeof(r), &r);
7954 zTempFile = sqlite3_mprintf("temp%llx", r);
7955 if( zTempFile==0 ){
7956 sqlite3_result_error_nomem(context);
7957 return;
7958 }
7959 }
7960 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
7961 f = fopen(zTempFile, bBin ? "wb" : "w");
7962 if( f==0 ){
7963 sqlite3_result_error(context, "edit() cannot open temp file", -1);
7964 goto edit_func_end;
7965 }
7966 sz = sqlite3_value_bytes(argv[0]);
7967 if( bBin ){
7968 x = fwrite(sqlite3_value_blob(argv[0]), 1, sz, f);
7969 }else{
7970 x = fwrite(sqlite3_value_text(argv[0]), 1, sz, f);
7971 }
7972 fclose(f);
7973 f = 0;
7974 if( x!=sz ){
7975 sqlite3_result_error(context, "edit() could not write the whole file", -1);
7976 goto edit_func_end;
7977 }
7978 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
7979 if( zCmd==0 ){
7980 sqlite3_result_error_nomem(context);
7981 goto edit_func_end;
7982 }
7983 rc = system(zCmd);
7984 sqlite3_free(zCmd);
7985 if( rc ){
7986 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
7987 goto edit_func_end;
7988 }
7989 f = fopen(zTempFile, bBin ? "rb" : "r");
7990 if( f==0 ){
7991 sqlite3_result_error(context,
7992 "edit() cannot reopen temp file after edit", -1);
7993 goto edit_func_end;
7994 }
7995 fseek(f, 0, SEEK_END);
7996 sz = ftell(f);
7997 rewind(f);
7998 p = sqlite3_malloc64( sz+(bBin==0) );
7999 if( p==0 ){
8000 sqlite3_result_error_nomem(context);
8001 goto edit_func_end;
8002 }
8003 if( bBin ){
8004 x = fread(p, 1, sz, f);
8005 }else{
8006 x = fread(p, 1, sz, f);
8007 p[sz] = 0;
8008 }
8009 fclose(f);
8010 f = 0;
8011 if( x!=sz ){
8012 sqlite3_result_error(context, "could not read back the whole file", -1);
8013 goto edit_func_end;
8014 }
8015 if( bBin ){
8016 sqlite3_result_blob(context, p, sz, sqlite3_free);
8017 }else{
8018 sqlite3_result_text(context, (const char*)p, sz, sqlite3_free);
8019 }
8020 p = 0;
8021
8022 edit_func_end:
8023 if( f ) fclose(f);
8024 unlink(zTempFile);
8025 sqlite3_free(zTempFile);
8026 sqlite3_free(p);
8027 }
8028
8029 /*
8030 ** Save or restore the current output mode
8031 */
8032 static void outputModePush(ShellState *p){
8033 p->modePrior = p->mode;
8034 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
8035 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
8036 }
8037 static void outputModePop(ShellState *p){
8038 p->mode = p->modePrior;
8039 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
8040 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
8041 }
8042
8043 /*
8044 ** Output the given string as a hex-encoded blob (eg. X'1234' )
8045 */
8046 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
@@ -9058,10 +9362,11 @@
9362 } while( rc == SQLITE_ROW );
9363 }
9364 }
9365 }
9366
9367 #ifndef SQLITE_OMIT_VIRTUALTABLE
9368 /*
9369 ** This function is called to process SQL if the previous shell command
9370 ** was ".expert". It passes the SQL in the second argument directly to
9371 ** the sqlite3expert object.
9372 **
@@ -9130,10 +9435,67 @@
9435 sqlite3_expert_destroy(p);
9436 pState->expert.pExpert = 0;
9437 return rc;
9438 }
9439
9440 /*
9441 ** Implementation of ".expert" dot command.
9442 */
9443 static int expertDotCommand(
9444 ShellState *pState, /* Current shell tool state */
9445 char **azArg, /* Array of arguments passed to dot command */
9446 int nArg /* Number of entries in azArg[] */
9447 ){
9448 int rc = SQLITE_OK;
9449 char *zErr = 0;
9450 int i;
9451 int iSample = 0;
9452
9453 assert( pState->expert.pExpert==0 );
9454 memset(&pState->expert, 0, sizeof(ExpertInfo));
9455
9456 for(i=1; rc==SQLITE_OK && i<nArg; i++){
9457 char *z = azArg[i];
9458 int n;
9459 if( z[0]=='-' && z[1]=='-' ) z++;
9460 n = strlen30(z);
9461 if( n>=2 && 0==strncmp(z, "-verbose", n) ){
9462 pState->expert.bVerbose = 1;
9463 }
9464 else if( n>=2 && 0==strncmp(z, "-sample", n) ){
9465 if( i==(nArg-1) ){
9466 raw_printf(stderr, "option requires an argument: %s\n", z);
9467 rc = SQLITE_ERROR;
9468 }else{
9469 iSample = (int)integerValue(azArg[++i]);
9470 if( iSample<0 || iSample>100 ){
9471 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
9472 rc = SQLITE_ERROR;
9473 }
9474 }
9475 }
9476 else{
9477 raw_printf(stderr, "unknown option: %s\n", z);
9478 rc = SQLITE_ERROR;
9479 }
9480 }
9481
9482 if( rc==SQLITE_OK ){
9483 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
9484 if( pState->expert.pExpert==0 ){
9485 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
9486 rc = SQLITE_ERROR;
9487 }else{
9488 sqlite3_expert_config(
9489 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
9490 );
9491 }
9492 }
9493
9494 return rc;
9495 }
9496 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
9497
9498 /*
9499 ** Execute a statement or set of statements. Print
9500 ** any result rows/columns depending on the current mode
9501 ** set via the supplied callback.
@@ -9157,14 +9519,16 @@
9519
9520 if( pzErrMsg ){
9521 *pzErrMsg = NULL;
9522 }
9523
9524 #ifndef SQLITE_OMIT_VIRTUALTABLE
9525 if( pArg->expert.pExpert ){
9526 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
9527 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
9528 }
9529 #endif
9530
9531 while( zSql[0] && (SQLITE_OK == rc) ){
9532 static const char *zStmtSql;
9533 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
9534 if( SQLITE_OK != rc ){
@@ -9587,10 +9951,11 @@
9951 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
9952 " If TABLE specified, only dump tables matching\n"
9953 " LIKE pattern TABLE.\n"
9954 ".echo on|off Turn command echo on or off\n"
9955 ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
9956 ".excel Display the output of next command in a spreadsheet\n"
9957 ".exit Exit this program\n"
9958 ".expert EXPERIMENTAL. Suggest indexes for specified queries\n"
9959 /* Because explain mode comes on automatically now, the ".explain" mode
9960 ** is removed from the help screen. It is still supported for legacy, however */
9961 /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
@@ -9624,14 +9989,16 @@
9989 " list Values delimited by \"|\"\n"
9990 " quote Escape answers as for SQL\n"
9991 " tabs Tab-separated values\n"
9992 " tcl TCL list elements\n"
9993 ".nullvalue STRING Use STRING in place of NULL values\n"
9994 ".once (-e|-x|FILE) Output for the next SQL command only to FILE\n"
9995 " or invoke system text editor (-e) or spreadsheet (-x)\n"
9996 " on the output.\n"
9997 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
9998 " The --new option starts with an empty file\n"
9999 ".output ?FILE? Send output to FILE or stdout\n"
10000 ".print STRING... Print literal STRING\n"
10001 ".prompt MAIN CONTINUE Replace the standard prompts\n"
10002 ".quit Exit this program\n"
10003 ".read FILENAME Execute SQL in FILENAME\n"
10004 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
@@ -9846,10 +10213,16 @@
10213 #endif
10214 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
10215 shellAddSchemaName, 0, 0);
10216 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
10217 shellModuleSchema, 0, 0);
10218 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
10219 shellPutsFunc, 0, 0);
10220 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
10221 editFunc, 0, 0);
10222 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
10223 editFunc, 0, 0);
10224 if( p->openMode==SHELL_OPEN_ZIPFILE ){
10225 char *zSql = sqlite3_mprintf(
10226 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
10227 sqlite3_exec(p->db, zSql, 0, 0, 0);
10228 sqlite3_free(zSql);
@@ -9979,67 +10352,10 @@
10352 z[j] = c;
10353 }
10354 if( j<i ) z[j] = 0;
10355 }
10356
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10357 /*
10358 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
10359 ** for TRUE and FALSE. Return the integer value if appropriate.
10360 */
10361 static int booleanValue(const char *zArg){
@@ -10082,20 +10398,20 @@
10398 /*
10399 ** Try to open an output file. The names "stdout" and "stderr" are
10400 ** recognized and do the right thing. NULL is returned if the output
10401 ** filename is "off".
10402 */
10403 static FILE *output_file_open(const char *zFile, int bTextMode){
10404 FILE *f;
10405 if( strcmp(zFile,"stdout")==0 ){
10406 f = stdout;
10407 }else if( strcmp(zFile, "stderr")==0 ){
10408 f = stderr;
10409 }else if( strcmp(zFile, "off")==0 ){
10410 f = 0;
10411 }else{
10412 f = fopen(zFile, bTextMode ? "w" : "wb");
10413 if( f==0 ){
10414 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
10415 }
10416 }
10417 return f;
@@ -10504,19 +10820,41 @@
10820 }
10821 sqlite3_close(newDb);
10822 }
10823
10824 /*
10825 ** Change the output file back to stdout.
10826 **
10827 ** If the p->doXdgOpen flag is set, that means the output was being
10828 ** redirected to a temporary file named by p->zTempFile. In that case,
10829 ** launch start/open/xdg-open on that temporary file.
10830 */
10831 static void output_reset(ShellState *p){
10832 if( p->outfile[0]=='|' ){
10833 #ifndef SQLITE_OMIT_POPEN
10834 pclose(p->out);
10835 #endif
10836 }else{
10837 output_file_close(p->out);
10838 if( p->doXdgOpen ){
10839 const char *zXdgOpenCmd =
10840 #if defined(_WIN32)
10841 "start";
10842 #elif defined(__APPLE__)
10843 "open";
10844 #else
10845 "xdg-open";
10846 #endif
10847 char *zCmd;
10848 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
10849 if( system(zCmd) ){
10850 utf8_printf(stderr, "Failed: [%s]\n", zCmd);
10851 }
10852 sqlite3_free(zCmd);
10853 outputModePop(p);
10854 p->doXdgOpen = 0;
10855 }
10856 }
10857 p->outfile[0] = 0;
10858 p->out = stdout;
10859 }
10860
@@ -10770,10 +11108,45 @@
11108 #else
11109 rc = unlink(zFilename);
11110 #endif
11111 return rc;
11112 }
11113
11114 /*
11115 ** Try to delete the temporary file (if there is one) and free the
11116 ** memory used to hold the name of the temp file.
11117 */
11118 static void clearTempFile(ShellState *p){
11119 if( p->zTempFile==0 ) return;
11120 if( p->doXdgOpen ) return;
11121 if( shellDeleteFile(p->zTempFile) ) return;
11122 sqlite3_free(p->zTempFile);
11123 p->zTempFile = 0;
11124 }
11125
11126 /*
11127 ** Create a new temp file name with the given suffix.
11128 */
11129 static void newTempFile(ShellState *p, const char *zSuffix){
11130 clearTempFile(p);
11131 sqlite3_free(p->zTempFile);
11132 p->zTempFile = 0;
11133 if( p->db ){
11134 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
11135 }
11136 if( p->zTempFile==0 ){
11137 sqlite3_uint64 r;
11138 sqlite3_randomness(sizeof(r), &r);
11139 p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
11140 }else{
11141 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
11142 }
11143 if( p->zTempFile==0 ){
11144 raw_printf(stderr, "out of memory\n");
11145 exit(1);
11146 }
11147 }
11148
11149
11150 /*
11151 ** The implementation of SQL scalar function fkey_collate_clause(), used
11152 ** by the ".lint fkey-indexes" command. This scalar function is always
@@ -11099,17 +11472,22 @@
11472 /*
11473 ** Structure representing a single ".ar" command.
11474 */
11475 typedef struct ArCommand ArCommand;
11476 struct ArCommand {
11477 u8 eCmd; /* An AR_CMD_* value */
11478 u8 bVerbose; /* True if --verbose */
11479 u8 bZip; /* True if the archive is a ZIP */
11480 u8 bDryRun; /* True if --dry-run */
11481 u8 bAppend; /* True if --append */
11482 int nArg; /* Number of command arguments */
11483 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
11484 const char *zFile; /* --file argument, or NULL */
11485 const char *zDir; /* --directory argument, or NULL */
 
 
 
11486 char **azArg; /* Array of command arguments */
11487 ShellState *p; /* Shell state */
11488 sqlite3 *db; /* Database containing the archive */
11489 };
11490
11491 /*
11492 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
11493 */
@@ -11131,11 +11509,13 @@
11509 " -x, --extract Extract files from archive\n"
11510 "\n"
11511 "And zero or more optional options:\n"
11512 " -v, --verbose Print each filename as it is processed\n"
11513 " -f FILE, --file FILE Operate on archive FILE (default is current db)\n"
11514 " -a FILE, --append FILE Operate on FILE opened using the apndvfs VFS\n"
11515 " -C DIR, --directory DIR Change to directory DIR to read/extract files\n"
11516 " -n, --dryrun Show the SQL that would have occurred\n"
11517 "\n"
11518 "See also: http://sqlite.org/cli.html#sqlar_archive_support\n"
11519 "\n"
11520 );
11521 return SQLITE_ERROR;
@@ -11166,14 +11546,15 @@
11546 #define AR_CMD_HELP 5
11547
11548 /*
11549 ** Other (non-command) switches.
11550 */
11551 #define AR_SWITCH_VERBOSE 6
11552 #define AR_SWITCH_FILE 7
11553 #define AR_SWITCH_DIRECTORY 8
11554 #define AR_SWITCH_APPEND 9
11555 #define AR_SWITCH_DRYRUN 10
11556
11557 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
11558 switch( eSwitch ){
11559 case AR_CMD_CREATE:
11560 case AR_CMD_EXTRACT:
@@ -11184,17 +11565,19 @@
11565 return arErrorMsg("multiple command options");
11566 }
11567 pAr->eCmd = eSwitch;
11568 break;
11569
11570 case AR_SWITCH_DRYRUN:
11571 pAr->bDryRun = 1;
11572 break;
11573 case AR_SWITCH_VERBOSE:
11574 pAr->bVerbose = 1;
11575 break;
11576 case AR_SWITCH_APPEND:
11577 pAr->bAppend = 1;
11578 /* Fall thru into --file */
 
11579 case AR_SWITCH_FILE:
11580 pAr->zFile = zArg;
11581 break;
11582 case AR_SWITCH_DIRECTORY:
11583 pAr->zDir = zArg;
@@ -11214,24 +11597,25 @@
11597 char **azArg, /* Array of arguments passed to dot command */
11598 int nArg, /* Number of entries in azArg[] */
11599 ArCommand *pAr /* Populate this object */
11600 ){
11601 struct ArSwitch {
 
11602 const char *zLong;
11603 char cShort;
11604 u8 eSwitch;
11605 u8 bArg;
11606 } aSwitch[] = {
11607 { "create", 'c', AR_CMD_CREATE, 0 },
11608 { "extract", 'x', AR_CMD_EXTRACT, 0 },
11609 { "list", 't', AR_CMD_LIST, 0 },
11610 { "update", 'u', AR_CMD_UPDATE, 0 },
11611 { "help", 'h', AR_CMD_HELP, 0 },
11612 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
11613 { "file", 'f', AR_SWITCH_FILE, 1 },
11614 { "append", 'a', AR_SWITCH_APPEND, 1 },
11615 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
11616 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
11617 };
11618 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
11619 struct ArSwitch *pEnd = &aSwitch[nSwitch];
11620
11621 if( nArg<=1 ){
@@ -11354,41 +11738,39 @@
11738 **
11739 ** This function strips any trailing '/' characters from each argument.
11740 ** This is consistent with the way the [tar] command seems to work on
11741 ** Linux.
11742 */
11743 static int arCheckEntries(ArCommand *pAr){
11744 int rc = SQLITE_OK;
11745 if( pAr->nArg ){
11746 int i, j;
11747 sqlite3_stmt *pTest = 0;
11748
11749 shellPreparePrintf(pAr->db, &rc, &pTest,
11750 "SELECT name FROM %s WHERE name=$name",
11751 pAr->zSrcTable
11752 );
11753 j = sqlite3_bind_parameter_index(pTest, "$name");
 
 
11754 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
11755 char *z = pAr->azArg[i];
11756 int n = strlen30(z);
11757 int bOk = 0;
11758 while( n>0 && z[n-1]=='/' ) n--;
11759 z[n] = '\0';
11760 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
11761 if( SQLITE_ROW==sqlite3_step(pTest) ){
11762 bOk = 1;
11763 }
11764 shellReset(&rc, pTest);
11765 if( rc==SQLITE_OK && bOk==0 ){
11766 utf8_printf(stderr, "not found in archive: %s\n", z);
11767 rc = SQLITE_ERROR;
11768 }
11769 }
11770 shellFinalize(&rc, pTest);
11771 }
 
11772 return rc;
11773 }
11774
11775 /*
11776 ** Format a WHERE clause that can be used against the "sqlar" table to
@@ -11410,13 +11792,13 @@
11792 int i;
11793 const char *zSep = "";
11794 for(i=0; i<pAr->nArg; i++){
11795 const char *z = pAr->azArg[i];
11796 zWhere = sqlite3_mprintf(
11797 "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
11798 zWhere, zSep, z, strlen30(z)+1, z
11799 );
11800 if( zWhere==0 ){
11801 *pRc = SQLITE_NOMEM;
11802 break;
11803 }
11804 zSep = " OR ";
@@ -11424,107 +11806,75 @@
11806 }
11807 }
11808 *pzWhere = zWhere;
11809 }
11810
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11811 /*
11812 ** Implementation of .ar "lisT" command.
11813 */
11814 static int arListCommand(ArCommand *pAr){
11815 const char *zSql = "SELECT %s FROM %s WHERE %s";
 
11816 const char *azCols[] = {
11817 "name",
11818 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
11819 };
11820
11821 char *zWhere = 0;
11822 sqlite3_stmt *pSql = 0;
11823 int rc;
11824
11825 rc = arCheckEntries(pAr);
11826 arWhereClause(&rc, pAr, &zWhere);
11827
11828 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
11829 pAr->zSrcTable, zWhere);
11830 if( pAr->bDryRun ){
11831 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
11832 }else{
11833 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
11834 if( pAr->bVerbose ){
11835 utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
11836 sqlite3_column_text(pSql, 0),
11837 sqlite3_column_int(pSql, 1),
11838 sqlite3_column_text(pSql, 2),
11839 sqlite3_column_text(pSql, 3)
11840 );
11841 }else{
11842 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
11843 }
11844 }
11845 }
 
11846 shellFinalize(&rc, pSql);
11847 return rc;
11848 }
11849
11850
11851 /*
11852 ** Implementation of .ar "eXtract" command.
11853 */
11854 static int arExtractCommand(ArCommand *pAr){
11855 const char *zSql1 =
11856 "SELECT "
11857 " ($dir || name),"
11858 " writefile(($dir || name), %s, mode, mtime) "
11859 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)";
11860
11861 const char *azExtraArg[] = {
11862 "sqlar_uncompress(data, sz)",
11863 "data"
11864 };
 
 
 
11865
11866 sqlite3_stmt *pSql = 0;
11867 int rc = SQLITE_OK;
11868 char *zDir = 0;
11869 char *zWhere = 0;
11870 int i, j;
11871
11872 /* If arguments are specified, check that they actually exist within
11873 ** the archive before proceeding. And formulate a WHERE clause to
11874 ** match them. */
11875 rc = arCheckEntries(pAr);
11876 arWhereClause(&rc, pAr, &zWhere);
11877
11878 if( rc==SQLITE_OK ){
11879 if( pAr->zDir ){
11880 zDir = sqlite3_mprintf("%s/", pAr->zDir);
@@ -11532,30 +11882,33 @@
11882 zDir = sqlite3_mprintf("");
11883 }
11884 if( zDir==0 ) rc = SQLITE_NOMEM;
11885 }
11886
11887 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
11888 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
11889 );
11890
11891 if( rc==SQLITE_OK ){
11892 j = sqlite3_bind_parameter_index(pSql, "$dir");
11893 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
 
 
11894
11895 /* Run the SELECT statement twice. The first time, writefile() is called
11896 ** for all archive members that should be extracted. The second time,
11897 ** only for the directories. This is because the timestamps for
11898 ** extracted directories must be reset after they are populated (as
11899 ** populating them changes the timestamp). */
11900 for(i=0; i<2; i++){
11901 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
11902 sqlite3_bind_int(pSql, j, i);
11903 if( pAr->bDryRun ){
11904 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
11905 }else{
11906 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
11907 if( i==0 && pAr->bVerbose ){
11908 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
11909 }
11910 }
11911 }
11912 shellReset(&rc, pSql);
11913 }
11914 shellFinalize(&rc, pSql);
@@ -11563,10 +11916,29 @@
11916
11917 sqlite3_free(zDir);
11918 sqlite3_free(zWhere);
11919 return rc;
11920 }
11921
11922 /*
11923 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
11924 */
11925 static int arExecSql(ArCommand *pAr, const char *zSql){
11926 int rc;
11927 if( pAr->bDryRun ){
11928 utf8_printf(pAr->p->out, "%s\n", zSql);
11929 rc = SQLITE_OK;
11930 }else{
11931 char *zErr = 0;
11932 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
11933 if( zErr ){
11934 utf8_printf(stdout, "ERROR: %s\n", zErr);
11935 sqlite3_free(zErr);
11936 }
11937 }
11938 return rc;
11939 }
11940
11941
11942 /*
11943 ** Implementation of .ar "create" and "update" commands.
11944 **
@@ -11576,116 +11948,60 @@
11948 ** printed on stdout for each file archived.
11949 **
11950 ** The create command is the same as update, except that it drops
11951 ** any existing "sqlar" table before beginning.
11952 */
11953 static int arCreateOrUpdateCommand(
 
 
11954 ArCommand *pAr, /* Command arguments and options */
11955 int bUpdate /* true for a --create. false for --update */
11956 ){
 
11957 const char *zCreate =
11958 "CREATE TABLE IF NOT EXISTS sqlar(\n"
11959 " name TEXT PRIMARY KEY, -- name of the file\n"
11960 " mode INT, -- access permissions\n"
11961 " mtime INT, -- last modification time\n"
11962 " sz INT, -- original file size\n"
11963 " data BLOB -- compressed content\n"
11964 ")";
11965 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
11966 const char *zInsertFmt =
11967 "REPLACE INTO sqlar(name,mode,mtime,sz,data)\n"
11968 " SELECT\n"
11969 " %s,\n"
11970 " mode,\n"
11971 " mtime,\n"
11972 " CASE substr(lsmode(mode),1,1)\n"
11973 " WHEN '-' THEN length(data)\n"
11974 " WHEN 'd' THEN 0\n"
11975 " ELSE -1 END,\n"
11976 " CASE WHEN lsmode(mode) LIKE 'd%%' THEN NULL else data END\n"
11977 " FROM fsdir(%Q,%Q)\n"
11978 " WHERE lsmode(mode) NOT LIKE '?%%';";
11979 int i; /* For iterating through azFile[] */
11980 int rc; /* Return code */
11981
11982 rc = arExecSql(pAr, "SAVEPOINT ar;");
11983 if( rc!=SQLITE_OK ) return rc;
11984 if( bUpdate==0 ){
11985 rc = arExecSql(pAr, zDrop);
11986 if( rc!=SQLITE_OK ) return rc;
11987 }
11988 rc = arExecSql(pAr, zCreate);
11989 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
11990 char *zSql = sqlite3_mprintf(zInsertFmt,
11991 pAr->bVerbose ? "shell_putsnl(name)" : "name",
11992 pAr->azArg[i], pAr->zDir);
11993 rc = arExecSql(pAr, zSql);
11994 sqlite3_free(zSql);
11995 }
11996 if( rc!=SQLITE_OK ){
11997 arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
11998 }else{
11999 rc = arExecSql(pAr, "RELEASE ar;");
12000 }
12001 return rc;
12002 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12003
12004 /*
12005 ** Implementation of ".ar" dot command.
12006 */
12007 static int arDotCommand(
@@ -11693,138 +12009,108 @@
12009 char **azArg, /* Array of arguments passed to dot command */
12010 int nArg /* Number of entries in azArg[] */
12011 ){
12012 ArCommand cmd;
12013 int rc;
12014 memset(&cmd, 0, sizeof(cmd));
12015 rc = arParseCommand(azArg, nArg, &cmd);
12016 if( rc==SQLITE_OK ){
12017 int eDbType = SHELL_OPEN_UNSPEC;
12018 cmd.p = pState;
12019 cmd.db = pState->db;
12020 if( cmd.zFile ){
12021 eDbType = deduceDatabaseType(cmd.zFile);
12022 }else{
12023 eDbType = pState->openMode;
12024 }
12025 if( eDbType==SHELL_OPEN_ZIPFILE ){
12026 if( cmd.zFile==0 ){
12027 cmd.zSrcTable = sqlite3_mprintf("zip");
12028 }else{
12029 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
12030 }
12031 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
12032 utf8_printf(stderr, "zip archives are read-only\n");
12033 rc = SQLITE_ERROR;
12034 goto end_ar_command;
12035 }
12036 cmd.bZip = 1;
12037 }else if( cmd.zFile ){
12038 int flags;
12039 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
12040 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
12041 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
12042 }else{
12043 flags = SQLITE_OPEN_READONLY;
12044 }
12045 cmd.db = 0;
12046 if( cmd.bDryRun ){
12047 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
12048 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
12049 }
12050 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
12051 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
12052 if( rc!=SQLITE_OK ){
12053 utf8_printf(stderr, "cannot open file: %s (%s)\n",
12054 cmd.zFile, sqlite3_errmsg(cmd.db)
12055 );
12056 goto end_ar_command;
 
12057 }
12058 sqlite3_fileio_init(cmd.db, 0, 0);
12059 #ifdef SQLITE_HAVE_ZLIB
12060 sqlite3_sqlar_init(cmd.db, 0, 0);
12061 #endif
12062 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
12063 shellPutsFunc, 0, 0);
12064
12065 }
12066 if( cmd.zSrcTable==0 ){
12067 if( cmd.eCmd!=AR_CMD_CREATE
12068 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
12069 ){
12070 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
12071 rc = SQLITE_ERROR;
12072 goto end_ar_command;
12073 }
12074 cmd.zSrcTable = sqlite3_mprintf("sqlar");
12075 }
12076
12077 switch( cmd.eCmd ){
12078 case AR_CMD_CREATE:
12079 rc = arCreateOrUpdateCommand(&cmd, 0);
12080 break;
12081
12082 case AR_CMD_EXTRACT:
12083 rc = arExtractCommand(&cmd);
12084 break;
12085
12086 case AR_CMD_LIST:
12087 rc = arListCommand(&cmd);
12088 break;
12089
12090 case AR_CMD_HELP:
12091 arUsage(pState->out);
12092 break;
12093
12094 default:
12095 assert( cmd.eCmd==AR_CMD_UPDATE );
12096 rc = arCreateOrUpdateCommand(&cmd, 1);
12097 break;
12098 }
12099 }
12100 end_ar_command:
12101 if( cmd.db!=pState->db ){
12102 sqlite3_close(cmd.db);
12103 }
12104 sqlite3_free(cmd.zSrcTable);
12105
12106 return rc;
12107 }
12108 /* End of the ".archive" or ".ar" command logic
12109 **********************************************************************************/
12110 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
12111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12112
12113 /*
12114 ** If an input line begins with "." then invoke this routine to
12115 ** process that line.
12116 **
@@ -11835,13 +12121,15 @@
12121 int nArg = 0;
12122 int n, c;
12123 int rc = 0;
12124 char *azArg[50];
12125
12126 #ifndef SQLITE_OMIT_VIRTUALTABLE
12127 if( p->expert.pExpert ){
12128 expertFinish(p, 1, 0);
12129 }
12130 #endif
12131
12132 /* Parse the input line into tokens.
12133 */
12134 while( zLine[h] && nArg<ArraySize(azArg) ){
12135 while( IsSpace(zLine[h]) ){ h++; }
@@ -11868,10 +12156,11 @@
12156 /* Process the input line.
12157 */
12158 if( nArg==0 ) return 0; /* no tokens, no error */
12159 n = strlen30(azArg[0]);
12160 c = azArg[0][0];
12161 clearTempFile(p);
12162
12163 #ifndef SQLITE_OMIT_AUTHORIZATION
12164 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
12165 if( nArg!=2 ){
12166 raw_printf(stderr, "Usage: .auth ON|OFF\n");
@@ -12202,14 +12491,16 @@
12491 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
12492 p->autoExplain = 1;
12493 }
12494 }else
12495
12496 #ifndef SQLITE_OMIT_VIRTUALTABLE
12497 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
12498 open_db(p, 0);
12499 expertDotCommand(p, azArg, nArg);
12500 }else
12501 #endif
12502
12503 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
12504 ShellState data;
12505 char *zErrMsg = 0;
12506 int doStats = 0;
@@ -12661,11 +12952,11 @@
12952 raw_printf(stderr, "Usage: .log FILENAME\n");
12953 rc = 1;
12954 }else{
12955 const char *zFile = azArg[1];
12956 output_file_close(p->pLog);
12957 p->pLog = output_file_open(zFile, 0);
12958 }
12959 }else
12960
12961 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
12962 const char *zMode = nArg>=2 ? azArg[1] : "";
@@ -12770,30 +13061,54 @@
13061 p->zDbFilename = 0;
13062 open_db(p, 0);
13063 }
13064 }else
13065
13066 if( (c=='o'
13067 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
13068 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
13069 ){
13070 const char *zFile = nArg>=2 ? azArg[1] : "stdout";
13071 int bTxtMode = 0;
13072 if( azArg[0][0]=='e' ){
13073 /* Transform the ".excel" command into ".once -x" */
13074 nArg = 2;
13075 azArg[0] = "once";
13076 zFile = azArg[1] = "-x";
13077 n = 4;
13078 }
13079 if( nArg>2 ){
13080 utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
13081 rc = 1;
13082 goto meta_command_exit;
13083 }
13084 if( n>1 && strncmp(azArg[0], "once", n)==0 ){
13085 if( nArg<2 ){
13086 raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
13087 rc = 1;
13088 goto meta_command_exit;
13089 }
13090 p->outCount = 2;
13091 }else{
13092 p->outCount = 0;
13093 }
13094 output_reset(p);
13095 if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
13096 if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
13097 p->doXdgOpen = 1;
13098 outputModePush(p);
13099 if( zFile[1]=='x' ){
13100 newTempFile(p, "csv");
13101 p->mode = MODE_Csv;
13102 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
13103 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
13104 }else{
13105 newTempFile(p, "txt");
13106 bTxtMode = 1;
13107 }
13108 zFile = p->zTempFile;
13109 }
13110 if( zFile[0]=='|' ){
13111 #ifdef SQLITE_OMIT_POPEN
13112 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
13113 rc = 1;
13114 p->out = stdout;
@@ -12806,11 +13121,11 @@
13121 }else{
13122 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
13123 }
13124 #endif
13125 }else{
13126 p->out = output_file_open(zFile, bTxtMode);
13127 if( p->out==0 ){
13128 if( strcmp(zFile,"off")!=0 ){
13129 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
13130 }
13131 p->out = stdout;
@@ -13683,11 +13998,11 @@
13998 }else
13999
14000 /* Begin redirecting output to the file "testcase-out.txt" */
14001 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
14002 output_reset(p);
14003 p->out = output_file_open("testcase-out.txt", 0);
14004 if( p->out==0 ){
14005 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
14006 }
14007 if( nArg>=2 ){
14008 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
@@ -13889,11 +14204,11 @@
14204 raw_printf(stderr, "Usage: .trace FILE|off\n");
14205 rc = 1;
14206 goto meta_command_exit;
14207 }
14208 output_file_close(p->traceOut);
14209 p->traceOut = output_file_open(azArg[1], 0);
14210 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
14211 if( p->traceOut==0 ){
14212 sqlite3_trace_v2(p->db, 0, 0, 0);
14213 }else{
14214 sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
@@ -14219,10 +14534,12 @@
14534 errCnt += runOneSqlLine(p, zSql, in, startline);
14535 nSql = 0;
14536 if( p->outCount ){
14537 output_reset(p);
14538 p->outCount = 0;
14539 }else{
14540 clearTempFile(p);
14541 }
14542 }else if( nSql && _all_whitespace(zSql) ){
14543 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
14544 nSql = 0;
14545 }
@@ -14841,11 +15158,14 @@
15158 session_close_all(&data);
15159 sqlite3_close(data.db);
15160 }
15161 sqlite3_free(data.zFreeOnClose);
15162 find_home_dir(1);
15163 output_reset(&data);
15164 data.doXdgOpen = 0;
15165 clearTempFile(&data);
15166 #if !SQLITE_SHELL_IS_UTF8
15167 for(i=0; i<argc; i++) sqlite3_free(argv[i]);
15168 sqlite3_free(argv);
15169 #endif
15170 return rc;
15171 }
15172
+29 -21
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1147,11 +1147,11 @@
11471147
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
11481148
** [sqlite_version()] and [sqlite_source_id()].
11491149
*/
11501150
#define SQLITE_VERSION "3.22.0"
11511151
#define SQLITE_VERSION_NUMBER 3022000
1152
-#define SQLITE_SOURCE_ID "2018-01-09 14:27:58 a7446d3217d39c96c884fbfb294dd320378255f3bfb34e35d15ba6d7c6698f53"
1152
+#define SQLITE_SOURCE_ID "2018-01-11 00:38:39 b8d92d8dc239597c6f01a6e572b047f98ce374a8f48257683fa839dde3ec993f"
11531153
11541154
/*
11551155
** CAPI3REF: Run-Time Library Version Numbers
11561156
** KEYWORDS: sqlite3_version sqlite3_sourceid
11571157
**
@@ -25706,11 +25706,11 @@
2570625706
*/
2570725707
SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
2570825708
int n;
2570925709
while( sqlite3Isspace(zStart[0]) ) zStart++;
2571025710
n = (int)(zEnd - zStart);
25711
- while( n>0 && sqlite3Isspace(zStart[n-1]) ) n--;
25711
+ while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
2571225712
return sqlite3DbStrNDup(db, zStart, n);
2571325713
}
2571425714
2571525715
/*
2571625716
** Free any prior content in *pz and replace it with a copy of zNew.
@@ -36557,24 +36557,28 @@
3655736557
return rc;
3655836558
}
3655936559
fd = robust_open(zName, openFlags, openMode);
3656036560
OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
3656136561
assert( !isExclusive || (openFlags & O_CREAT)!=0 );
36562
- if( fd<0 && errno!=EISDIR && isReadWrite ){
36563
- /* Failed to open the file for read/write access. Try read-only. */
36564
- flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
36565
- openFlags &= ~(O_RDWR|O_CREAT);
36566
- flags |= SQLITE_OPEN_READONLY;
36567
- openFlags |= O_RDONLY;
36568
- isReadonly = 1;
36569
- fd = robust_open(zName, openFlags, openMode);
36562
+ if( fd<0 ){
36563
+ if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){
36564
+ /* If unable to create a journal because the directory is not
36565
+ ** writable, change the error code to indicate that. */
36566
+ rc = SQLITE_READONLY_DIRECTORY;
36567
+ }else if( errno!=EISDIR && isReadWrite ){
36568
+ /* Failed to open the file for read/write access. Try read-only. */
36569
+ flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
36570
+ openFlags &= ~(O_RDWR|O_CREAT);
36571
+ flags |= SQLITE_OPEN_READONLY;
36572
+ openFlags |= O_RDONLY;
36573
+ isReadonly = 1;
36574
+ fd = robust_open(zName, openFlags, openMode);
36575
+ }
3657036576
}
3657136577
if( fd<0 ){
36572
- rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
36573
- /* If unable to create a journal, change the error code to
36574
- ** indicate that the directory permissions are wrong. */
36575
- if( isNewJrnl && osAccess(zName, F_OK) ) rc = SQLITE_READONLY_DIRECTORY;
36578
+ int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
36579
+ if( rc==SQLITE_OK ) rc = rc2;
3657636580
goto open_finished;
3657736581
}
3657836582
3657936583
/* If this process is running as root and if creating a new rollback
3658036584
** journal or WAL file, set the ownership of the journal or WAL to be
@@ -103560,11 +103564,11 @@
103560103564
103561103565
/* Locate the end of the CREATE VIEW statement. Make sEnd point to
103562103566
** the end.
103563103567
*/
103564103568
sEnd = pParse->sLastToken;
103565
- assert( sEnd.z[0]!=0 );
103569
+ assert( sEnd.z[0]!=0 || sEnd.n==0 );
103566103570
if( sEnd.z[0]!=';' ){
103567103571
sEnd.z += sEnd.n;
103568103572
}
103569103573
sEnd.n = 0;
103570103574
n = (int)(sEnd.z - pBegin->z);
@@ -141612,12 +141616,15 @@
141612141616
sqlite3ParserARG_FETCH;
141613141617
#define TOKEN yyminor
141614141618
/************ Begin %syntax_error code ****************************************/
141615141619
141616141620
UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
141617
- assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */
141618
- sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
141621
+ if( TOKEN.z[0] ){
141622
+ sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
141623
+ }else{
141624
+ sqlite3ErrorMsg(pParse, "incomplete input");
141625
+ }
141619141626
/************ End %syntax_error code ******************************************/
141620141627
sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
141621141628
}
141622141629
141623141630
/*
@@ -142658,11 +142665,11 @@
142658142665
}else if( lastTokenParsed==0 ){
142659142666
break;
142660142667
}else{
142661142668
tokenType = TK_SEMI;
142662142669
}
142663
- zSql -= n;
142670
+ n = 0;
142664142671
}
142665142672
if( tokenType>=TK_SPACE ){
142666142673
assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
142667142674
if( db->u1.isInterrupted ){
142668142675
pParse->rc = SQLITE_INTERRUPT;
@@ -144453,10 +144460,11 @@
144453144460
case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
144454144461
case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break;
144455144462
case SQLITE_READONLY_CANTINIT: zName = "SQLITE_READONLY_CANTINIT"; break;
144456144463
case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break;
144457144464
case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break;
144465
+ case SQLITE_READONLY_DIRECTORY: zName = "SQLITE_READONLY_DIRECTORY";break;
144458144466
case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
144459144467
case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
144460144468
case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break;
144461144469
case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break;
144462144470
case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break;
@@ -202941,11 +202949,11 @@
202941202949
int nArg, /* Number of args */
202942202950
sqlite3_value **apUnused /* Function arguments */
202943202951
){
202944202952
assert( nArg==0 );
202945202953
UNUSED_PARAM2(nArg, apUnused);
202946
- sqlite3_result_text(pCtx, "fts5: 2018-01-09 14:27:58 a7446d3217d39c96c884fbfb294dd320378255f3bfb34e35d15ba6d7c6698f53", -1, SQLITE_TRANSIENT);
202954
+ sqlite3_result_text(pCtx, "fts5: 2018-01-11 00:38:39 b8d92d8dc239597c6f01a6e572b047f98ce374a8f48257683fa839dde3ec993f", -1, SQLITE_TRANSIENT);
202947202955
}
202948202956
202949202957
static int fts5Init(sqlite3 *db){
202950202958
static const sqlite3_module fts5Mod = {
202951202959
/* iVersion */ 2,
@@ -207209,12 +207217,12 @@
207209207217
}
207210207218
#endif /* SQLITE_CORE */
207211207219
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
207212207220
207213207221
/************** End of stmt.c ************************************************/
207214
-#if __LINE__!=207214
207222
+#if __LINE__!=207222
207215207223
#undef SQLITE_SOURCE_ID
207216
-#define SQLITE_SOURCE_ID "2018-01-09 14:27:58 a7446d3217d39c96c884fbfb294dd320378255f3bfb34e35d15ba6d7c669alt2"
207224
+#define SQLITE_SOURCE_ID "2018-01-11 00:38:39 b8d92d8dc239597c6f01a6e572b047f98ce374a8f48257683fa839dde3ecalt2"
207217207225
#endif
207218207226
/* Return the source-id for this library */
207219207227
SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
207220207228
/************************** End of sqlite3.c ******************************/
207221207229
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1147,11 +1147,11 @@
1147 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1148 ** [sqlite_version()] and [sqlite_source_id()].
1149 */
1150 #define SQLITE_VERSION "3.22.0"
1151 #define SQLITE_VERSION_NUMBER 3022000
1152 #define SQLITE_SOURCE_ID "2018-01-09 14:27:58 a7446d3217d39c96c884fbfb294dd320378255f3bfb34e35d15ba6d7c6698f53"
1153
1154 /*
1155 ** CAPI3REF: Run-Time Library Version Numbers
1156 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1157 **
@@ -25706,11 +25706,11 @@
25706 */
25707 SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
25708 int n;
25709 while( sqlite3Isspace(zStart[0]) ) zStart++;
25710 n = (int)(zEnd - zStart);
25711 while( n>0 && sqlite3Isspace(zStart[n-1]) ) n--;
25712 return sqlite3DbStrNDup(db, zStart, n);
25713 }
25714
25715 /*
25716 ** Free any prior content in *pz and replace it with a copy of zNew.
@@ -36557,24 +36557,28 @@
36557 return rc;
36558 }
36559 fd = robust_open(zName, openFlags, openMode);
36560 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
36561 assert( !isExclusive || (openFlags & O_CREAT)!=0 );
36562 if( fd<0 && errno!=EISDIR && isReadWrite ){
36563 /* Failed to open the file for read/write access. Try read-only. */
36564 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
36565 openFlags &= ~(O_RDWR|O_CREAT);
36566 flags |= SQLITE_OPEN_READONLY;
36567 openFlags |= O_RDONLY;
36568 isReadonly = 1;
36569 fd = robust_open(zName, openFlags, openMode);
 
 
 
 
 
 
36570 }
36571 if( fd<0 ){
36572 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
36573 /* If unable to create a journal, change the error code to
36574 ** indicate that the directory permissions are wrong. */
36575 if( isNewJrnl && osAccess(zName, F_OK) ) rc = SQLITE_READONLY_DIRECTORY;
36576 goto open_finished;
36577 }
36578
36579 /* If this process is running as root and if creating a new rollback
36580 ** journal or WAL file, set the ownership of the journal or WAL to be
@@ -103560,11 +103564,11 @@
103560
103561 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
103562 ** the end.
103563 */
103564 sEnd = pParse->sLastToken;
103565 assert( sEnd.z[0]!=0 );
103566 if( sEnd.z[0]!=';' ){
103567 sEnd.z += sEnd.n;
103568 }
103569 sEnd.n = 0;
103570 n = (int)(sEnd.z - pBegin->z);
@@ -141612,12 +141616,15 @@
141612 sqlite3ParserARG_FETCH;
141613 #define TOKEN yyminor
141614 /************ Begin %syntax_error code ****************************************/
141615
141616 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
141617 assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */
141618 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
 
 
 
141619 /************ End %syntax_error code ******************************************/
141620 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
141621 }
141622
141623 /*
@@ -142658,11 +142665,11 @@
142658 }else if( lastTokenParsed==0 ){
142659 break;
142660 }else{
142661 tokenType = TK_SEMI;
142662 }
142663 zSql -= n;
142664 }
142665 if( tokenType>=TK_SPACE ){
142666 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
142667 if( db->u1.isInterrupted ){
142668 pParse->rc = SQLITE_INTERRUPT;
@@ -144453,10 +144460,11 @@
144453 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
144454 case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break;
144455 case SQLITE_READONLY_CANTINIT: zName = "SQLITE_READONLY_CANTINIT"; break;
144456 case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break;
144457 case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break;
 
144458 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
144459 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
144460 case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break;
144461 case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break;
144462 case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break;
@@ -202941,11 +202949,11 @@
202941 int nArg, /* Number of args */
202942 sqlite3_value **apUnused /* Function arguments */
202943 ){
202944 assert( nArg==0 );
202945 UNUSED_PARAM2(nArg, apUnused);
202946 sqlite3_result_text(pCtx, "fts5: 2018-01-09 14:27:58 a7446d3217d39c96c884fbfb294dd320378255f3bfb34e35d15ba6d7c6698f53", -1, SQLITE_TRANSIENT);
202947 }
202948
202949 static int fts5Init(sqlite3 *db){
202950 static const sqlite3_module fts5Mod = {
202951 /* iVersion */ 2,
@@ -207209,12 +207217,12 @@
207209 }
207210 #endif /* SQLITE_CORE */
207211 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
207212
207213 /************** End of stmt.c ************************************************/
207214 #if __LINE__!=207214
207215 #undef SQLITE_SOURCE_ID
207216 #define SQLITE_SOURCE_ID "2018-01-09 14:27:58 a7446d3217d39c96c884fbfb294dd320378255f3bfb34e35d15ba6d7c669alt2"
207217 #endif
207218 /* Return the source-id for this library */
207219 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
207220 /************************** End of sqlite3.c ******************************/
207221
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1147,11 +1147,11 @@
1147 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1148 ** [sqlite_version()] and [sqlite_source_id()].
1149 */
1150 #define SQLITE_VERSION "3.22.0"
1151 #define SQLITE_VERSION_NUMBER 3022000
1152 #define SQLITE_SOURCE_ID "2018-01-11 00:38:39 b8d92d8dc239597c6f01a6e572b047f98ce374a8f48257683fa839dde3ec993f"
1153
1154 /*
1155 ** CAPI3REF: Run-Time Library Version Numbers
1156 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1157 **
@@ -25706,11 +25706,11 @@
25706 */
25707 SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
25708 int n;
25709 while( sqlite3Isspace(zStart[0]) ) zStart++;
25710 n = (int)(zEnd - zStart);
25711 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
25712 return sqlite3DbStrNDup(db, zStart, n);
25713 }
25714
25715 /*
25716 ** Free any prior content in *pz and replace it with a copy of zNew.
@@ -36557,24 +36557,28 @@
36557 return rc;
36558 }
36559 fd = robust_open(zName, openFlags, openMode);
36560 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
36561 assert( !isExclusive || (openFlags & O_CREAT)!=0 );
36562 if( fd<0 ){
36563 if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){
36564 /* If unable to create a journal because the directory is not
36565 ** writable, change the error code to indicate that. */
36566 rc = SQLITE_READONLY_DIRECTORY;
36567 }else if( errno!=EISDIR && isReadWrite ){
36568 /* Failed to open the file for read/write access. Try read-only. */
36569 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
36570 openFlags &= ~(O_RDWR|O_CREAT);
36571 flags |= SQLITE_OPEN_READONLY;
36572 openFlags |= O_RDONLY;
36573 isReadonly = 1;
36574 fd = robust_open(zName, openFlags, openMode);
36575 }
36576 }
36577 if( fd<0 ){
36578 int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
36579 if( rc==SQLITE_OK ) rc = rc2;
 
 
36580 goto open_finished;
36581 }
36582
36583 /* If this process is running as root and if creating a new rollback
36584 ** journal or WAL file, set the ownership of the journal or WAL to be
@@ -103560,11 +103564,11 @@
103564
103565 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
103566 ** the end.
103567 */
103568 sEnd = pParse->sLastToken;
103569 assert( sEnd.z[0]!=0 || sEnd.n==0 );
103570 if( sEnd.z[0]!=';' ){
103571 sEnd.z += sEnd.n;
103572 }
103573 sEnd.n = 0;
103574 n = (int)(sEnd.z - pBegin->z);
@@ -141612,12 +141616,15 @@
141616 sqlite3ParserARG_FETCH;
141617 #define TOKEN yyminor
141618 /************ Begin %syntax_error code ****************************************/
141619
141620 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
141621 if( TOKEN.z[0] ){
141622 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
141623 }else{
141624 sqlite3ErrorMsg(pParse, "incomplete input");
141625 }
141626 /************ End %syntax_error code ******************************************/
141627 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
141628 }
141629
141630 /*
@@ -142658,11 +142665,11 @@
142665 }else if( lastTokenParsed==0 ){
142666 break;
142667 }else{
142668 tokenType = TK_SEMI;
142669 }
142670 n = 0;
142671 }
142672 if( tokenType>=TK_SPACE ){
142673 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
142674 if( db->u1.isInterrupted ){
142675 pParse->rc = SQLITE_INTERRUPT;
@@ -144453,10 +144460,11 @@
144460 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
144461 case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break;
144462 case SQLITE_READONLY_CANTINIT: zName = "SQLITE_READONLY_CANTINIT"; break;
144463 case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break;
144464 case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break;
144465 case SQLITE_READONLY_DIRECTORY: zName = "SQLITE_READONLY_DIRECTORY";break;
144466 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
144467 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
144468 case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break;
144469 case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break;
144470 case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break;
@@ -202941,11 +202949,11 @@
202949 int nArg, /* Number of args */
202950 sqlite3_value **apUnused /* Function arguments */
202951 ){
202952 assert( nArg==0 );
202953 UNUSED_PARAM2(nArg, apUnused);
202954 sqlite3_result_text(pCtx, "fts5: 2018-01-11 00:38:39 b8d92d8dc239597c6f01a6e572b047f98ce374a8f48257683fa839dde3ec993f", -1, SQLITE_TRANSIENT);
202955 }
202956
202957 static int fts5Init(sqlite3 *db){
202958 static const sqlite3_module fts5Mod = {
202959 /* iVersion */ 2,
@@ -207209,12 +207217,12 @@
207217 }
207218 #endif /* SQLITE_CORE */
207219 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
207220
207221 /************** End of stmt.c ************************************************/
207222 #if __LINE__!=207222
207223 #undef SQLITE_SOURCE_ID
207224 #define SQLITE_SOURCE_ID "2018-01-11 00:38:39 b8d92d8dc239597c6f01a6e572b047f98ce374a8f48257683fa839dde3ecalt2"
207225 #endif
207226 /* Return the source-id for this library */
207227 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
207228 /************************** End of sqlite3.c ******************************/
207229
+1 -1
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123123
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124124
** [sqlite_version()] and [sqlite_source_id()].
125125
*/
126126
#define SQLITE_VERSION "3.22.0"
127127
#define SQLITE_VERSION_NUMBER 3022000
128
-#define SQLITE_SOURCE_ID "2018-01-09 14:27:58 a7446d3217d39c96c884fbfb294dd320378255f3bfb34e35d15ba6d7c6698f53"
128
+#define SQLITE_SOURCE_ID "2018-01-11 00:38:39 b8d92d8dc239597c6f01a6e572b047f98ce374a8f48257683fa839dde3ec993f"
129129
130130
/*
131131
** CAPI3REF: Run-Time Library Version Numbers
132132
** KEYWORDS: sqlite3_version sqlite3_sourceid
133133
**
134134
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124 ** [sqlite_version()] and [sqlite_source_id()].
125 */
126 #define SQLITE_VERSION "3.22.0"
127 #define SQLITE_VERSION_NUMBER 3022000
128 #define SQLITE_SOURCE_ID "2018-01-09 14:27:58 a7446d3217d39c96c884fbfb294dd320378255f3bfb34e35d15ba6d7c6698f53"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
134
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124 ** [sqlite_version()] and [sqlite_source_id()].
125 */
126 #define SQLITE_VERSION "3.22.0"
127 #define SQLITE_VERSION_NUMBER 3022000
128 #define SQLITE_SOURCE_ID "2018-01-11 00:38:39 b8d92d8dc239597c6f01a6e572b047f98ce374a8f48257683fa839dde3ec993f"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
134

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button