Fossil SCM

Update the build-in SQLite to the latest 3.23.0 alpha

drh 2018-03-17 16:29 trunk
Commit ea67a5449a6e53d09ccfb937baf46e017bc2a7119d80db01f0b17a91786477a3
+26 -1
--- src/main.mk
+++ src/main.mk
@@ -560,11 +560,36 @@
560560
-DSQLITE_USE_ZLIB \
561561
-DSQLITE_INTROSPECTION_PRAGMAS \
562562
-DSQLITE_ENABLE_DBPAGE_VTAB
563563
564564
# Setup the options used to compile the included SQLite shell.
565
-SHELL_OPTIONS = -Dmain=sqlite3_shell \
565
+SHELL_OPTIONS = -DNDEBUG=1 \
566
+ -DSQLITE_THREADSAFE=0 \
567
+ -DSQLITE_DEFAULT_MEMSTATUS=0 \
568
+ -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
569
+ -DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
570
+ -DSQLITE_OMIT_DECLTYPE \
571
+ -DSQLITE_OMIT_DEPRECATED \
572
+ -DSQLITE_OMIT_GET_TABLE \
573
+ -DSQLITE_OMIT_PROGRESS_CALLBACK \
574
+ -DSQLITE_OMIT_SHARED_CACHE \
575
+ -DSQLITE_OMIT_LOAD_EXTENSION \
576
+ -DSQLITE_MAX_EXPR_DEPTH=0 \
577
+ -DSQLITE_USE_ALLOCA \
578
+ -DSQLITE_ENABLE_LOCKING_STYLE=0 \
579
+ -DSQLITE_DEFAULT_FILE_FORMAT=4 \
580
+ -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
581
+ -DSQLITE_ENABLE_FTS4 \
582
+ -DSQLITE_ENABLE_FTS3_PARENTHESIS \
583
+ -DSQLITE_ENABLE_DBSTAT_VTAB \
584
+ -DSQLITE_ENABLE_JSON1 \
585
+ -DSQLITE_ENABLE_FTS5 \
586
+ -DSQLITE_ENABLE_STMTVTAB \
587
+ -DSQLITE_USE_ZLIB \
588
+ -DSQLITE_INTROSPECTION_PRAGMAS \
589
+ -DSQLITE_ENABLE_DBPAGE_VTAB \
590
+ -Dmain=sqlite3_shell \
566591
-DSQLITE_SHELL_IS_UTF8=1 \
567592
-DSQLITE_OMIT_LOAD_EXTENSION=1 \
568593
-DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) \
569594
-DSQLITE_SHELL_DBNAME_PROC=fossil_open
570595
571596
--- src/main.mk
+++ src/main.mk
@@ -560,11 +560,36 @@
560 -DSQLITE_USE_ZLIB \
561 -DSQLITE_INTROSPECTION_PRAGMAS \
562 -DSQLITE_ENABLE_DBPAGE_VTAB
563
564 # Setup the options used to compile the included SQLite shell.
565 SHELL_OPTIONS = -Dmain=sqlite3_shell \
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
566 -DSQLITE_SHELL_IS_UTF8=1 \
567 -DSQLITE_OMIT_LOAD_EXTENSION=1 \
568 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) \
569 -DSQLITE_SHELL_DBNAME_PROC=fossil_open
570
571
--- src/main.mk
+++ src/main.mk
@@ -560,11 +560,36 @@
560 -DSQLITE_USE_ZLIB \
561 -DSQLITE_INTROSPECTION_PRAGMAS \
562 -DSQLITE_ENABLE_DBPAGE_VTAB
563
564 # Setup the options used to compile the included SQLite shell.
565 SHELL_OPTIONS = -DNDEBUG=1 \
566 -DSQLITE_THREADSAFE=0 \
567 -DSQLITE_DEFAULT_MEMSTATUS=0 \
568 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
569 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
570 -DSQLITE_OMIT_DECLTYPE \
571 -DSQLITE_OMIT_DEPRECATED \
572 -DSQLITE_OMIT_GET_TABLE \
573 -DSQLITE_OMIT_PROGRESS_CALLBACK \
574 -DSQLITE_OMIT_SHARED_CACHE \
575 -DSQLITE_OMIT_LOAD_EXTENSION \
576 -DSQLITE_MAX_EXPR_DEPTH=0 \
577 -DSQLITE_USE_ALLOCA \
578 -DSQLITE_ENABLE_LOCKING_STYLE=0 \
579 -DSQLITE_DEFAULT_FILE_FORMAT=4 \
580 -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
581 -DSQLITE_ENABLE_FTS4 \
582 -DSQLITE_ENABLE_FTS3_PARENTHESIS \
583 -DSQLITE_ENABLE_DBSTAT_VTAB \
584 -DSQLITE_ENABLE_JSON1 \
585 -DSQLITE_ENABLE_FTS5 \
586 -DSQLITE_ENABLE_STMTVTAB \
587 -DSQLITE_USE_ZLIB \
588 -DSQLITE_INTROSPECTION_PRAGMAS \
589 -DSQLITE_ENABLE_DBPAGE_VTAB \
590 -Dmain=sqlite3_shell \
591 -DSQLITE_SHELL_IS_UTF8=1 \
592 -DSQLITE_OMIT_LOAD_EXTENSION=1 \
593 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) \
594 -DSQLITE_SHELL_DBNAME_PROC=fossil_open
595
596
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -201,17 +201,17 @@
201201
#lappend SQLITE_OPTIONS -DSQLITE_WIN32_NO_ANSI
202202
#lappend SQLITE_OPTIONS -DSQLITE_WINNT_MAX_PATH_CHARS=4096
203203
204204
# Options used to compile the included SQLite shell.
205205
#
206
-set SHELL_OPTIONS {
206
+set SHELL_OPTIONS [concat $SQLITE_OPTIONS {
207207
-Dmain=sqlite3_shell
208208
-DSQLITE_SHELL_IS_UTF8=1
209209
-DSQLITE_OMIT_LOAD_EXTENSION=1
210210
-DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE)
211211
-DSQLITE_SHELL_DBNAME_PROC=fossil_open
212
-}
212
+}]
213213
214214
# miniz (libz drop-in alternative) precompiler flags.
215215
#
216216
set MINIZ_OPTIONS {
217217
-DMINIZ_NO_STDIO
218218
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -201,17 +201,17 @@
201 #lappend SQLITE_OPTIONS -DSQLITE_WIN32_NO_ANSI
202 #lappend SQLITE_OPTIONS -DSQLITE_WINNT_MAX_PATH_CHARS=4096
203
204 # Options used to compile the included SQLite shell.
205 #
206 set SHELL_OPTIONS {
207 -Dmain=sqlite3_shell
208 -DSQLITE_SHELL_IS_UTF8=1
209 -DSQLITE_OMIT_LOAD_EXTENSION=1
210 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE)
211 -DSQLITE_SHELL_DBNAME_PROC=fossil_open
212 }
213
214 # miniz (libz drop-in alternative) precompiler flags.
215 #
216 set MINIZ_OPTIONS {
217 -DMINIZ_NO_STDIO
218
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -201,17 +201,17 @@
201 #lappend SQLITE_OPTIONS -DSQLITE_WIN32_NO_ANSI
202 #lappend SQLITE_OPTIONS -DSQLITE_WINNT_MAX_PATH_CHARS=4096
203
204 # Options used to compile the included SQLite shell.
205 #
206 set SHELL_OPTIONS [concat $SQLITE_OPTIONS {
207 -Dmain=sqlite3_shell
208 -DSQLITE_SHELL_IS_UTF8=1
209 -DSQLITE_OMIT_LOAD_EXTENSION=1
210 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE)
211 -DSQLITE_SHELL_DBNAME_PROC=fossil_open
212 }]
213
214 # miniz (libz drop-in alternative) precompiler flags.
215 #
216 set MINIZ_OPTIONS {
217 -DMINIZ_NO_STDIO
218
+1489 -844
--- src/shell.c
+++ src/shell.c
@@ -995,11 +995,14 @@
995995
996996
/*
997997
** We need several data types from the Windows SDK header.
998998
*/
999999
1000
+#ifndef WIN32_LEAN_AND_MEAN
10001001
#define WIN32_LEAN_AND_MEAN
1002
+#endif
1003
+
10011004
#include "windows.h"
10021005
10031006
/*
10041007
** We need several support functions from the SQLite core.
10051008
*/
@@ -2314,19 +2317,24 @@
23142317
FILETIME lastAccess;
23152318
FILETIME lastWrite;
23162319
SYSTEMTIME currentTime;
23172320
LONGLONG intervals;
23182321
HANDLE hFile;
2322
+ LPWSTR zUnicodeName;
2323
+ extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2324
+
23192325
GetSystemTime(&currentTime);
23202326
SystemTimeToFileTime(&currentTime, &lastAccess);
23212327
intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
23222328
lastWrite.dwLowDateTime = (DWORD)intervals;
23232329
lastWrite.dwHighDateTime = intervals >> 32;
2324
- hFile = CreateFile(
2325
- zFile, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2330
+ zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2331
+ hFile = CreateFileW(
2332
+ zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
23262333
FILE_FLAG_BACKUP_SEMANTICS, NULL
23272334
);
2335
+ sqlite3_free(zUnicodeName);
23282336
if( hFile!=INVALID_HANDLE_VALUE ){
23292337
BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
23302338
CloseHandle(hFile);
23312339
return !bResult;
23322340
}else{
@@ -2530,14 +2538,16 @@
25302538
FsdirLevel *pLvl = &pCur->aLvl[i];
25312539
if( pLvl->pDir ) closedir(pLvl->pDir);
25322540
sqlite3_free(pLvl->zDir);
25332541
}
25342542
sqlite3_free(pCur->zPath);
2543
+ sqlite3_free(pCur->aLvl);
25352544
pCur->aLvl = 0;
25362545
pCur->zPath = 0;
25372546
pCur->zBase = 0;
25382547
pCur->nBase = 0;
2548
+ pCur->nLvl = 0;
25392549
pCur->iLvl = -1;
25402550
pCur->iRowid = 1;
25412551
}
25422552
25432553
/*
@@ -2545,11 +2555,10 @@
25452555
*/
25462556
static int fsdirClose(sqlite3_vtab_cursor *cur){
25472557
fsdir_cursor *pCur = (fsdir_cursor*)cur;
25482558
25492559
fsdirResetCursor(pCur);
2550
- sqlite3_free(pCur->aLvl);
25512560
sqlite3_free(pCur);
25522561
return SQLITE_OK;
25532562
}
25542563
25552564
/*
@@ -3992,33 +4001,52 @@
39924001
SQLITE_EXTENSION_INIT1
39934002
#include <stdio.h>
39944003
#include <string.h>
39954004
#include <assert.h>
39964005
3997
-#include <sys/types.h>
3998
-#include <sys/stat.h>
3999
-#include <fcntl.h>
4000
-#if !defined(_WIN32) && !defined(WIN32)
4001
-# include <unistd.h>
4002
-# include <dirent.h>
4003
-# include <utime.h>
4004
-#else
4005
-# include <io.h>
4006
-#endif
4007
-#include <time.h>
4008
-#include <errno.h>
4009
-
40104006
#include <zlib.h>
40114007
40124008
#ifndef SQLITE_OMIT_VIRTUALTABLE
40134009
40144010
#ifndef SQLITE_AMALGAMATION
4011
+
40154012
/* typedef sqlite3_int64 i64; */
40164013
/* typedef unsigned char u8; */
40174014
typedef unsigned short u16;
40184015
typedef unsigned long u32;
40194016
#define MIN(a,b) ((a)<(b) ? (a) : (b))
4017
+
4018
+#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
4019
+# define ALWAYS(X) (1)
4020
+# define NEVER(X) (0)
4021
+#elif !defined(NDEBUG)
4022
+# define ALWAYS(X) ((X)?1:(assert(0),0))
4023
+# define NEVER(X) ((X)?(assert(0),1):0)
4024
+#else
4025
+# define ALWAYS(X) (X)
4026
+# define NEVER(X) (X)
4027
+#endif
4028
+
4029
+#endif /* SQLITE_AMALGAMATION */
4030
+
4031
+/*
4032
+** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
4033
+**
4034
+** In some ways it would be better to obtain these values from system
4035
+** header files. But, the dependency is undesirable and (a) these
4036
+** have been stable for decades, (b) the values are part of POSIX and
4037
+** are also made explicit in [man stat], and (c) are part of the
4038
+** file format for zip archives.
4039
+*/
4040
+#ifndef S_IFDIR
4041
+# define S_IFDIR 0040000
4042
+#endif
4043
+#ifndef S_IFREG
4044
+# define S_IFREG 0100000
4045
+#endif
4046
+#ifndef S_IFLNK
4047
+# define S_IFLNK 0120000
40204048
#endif
40214049
40224050
static const char ZIPFILE_SCHEMA[] =
40234051
"CREATE TABLE y("
40244052
"name PRIMARY KEY," /* 0: Name of file in zip archive */
@@ -4055,34 +4083,29 @@
40554083
** ZIPFILE_SIGNATURE_CDS:
40564084
** First 4 bytes of a valid CDS record.
40574085
**
40584086
** ZIPFILE_SIGNATURE_LFH:
40594087
** First 4 bytes of a valid LFH record.
4088
+**
4089
+** ZIPFILE_SIGNATURE_EOCD
4090
+** First 4 bytes of a valid EOCD record.
40604091
*/
40614092
#define ZIPFILE_EXTRA_TIMESTAMP 0x5455
40624093
#define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
40634094
#define ZIPFILE_NEWENTRY_REQUIRED 20
40644095
#define ZIPFILE_NEWENTRY_FLAGS 0x800
40654096
#define ZIPFILE_SIGNATURE_CDS 0x02014b50
40664097
#define ZIPFILE_SIGNATURE_LFH 0x04034b50
40674098
#define ZIPFILE_SIGNATURE_EOCD 0x06054b50
4099
+
4100
+/*
4101
+** The sizes of the fixed-size part of each of the three main data
4102
+** structures in a zip archive.
4103
+*/
40684104
#define ZIPFILE_LFH_FIXED_SZ 30
4069
-
4070
-/*
4071
-** Set the error message contained in context ctx to the results of
4072
-** vprintf(zFmt, ...).
4073
-*/
4074
-static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4075
- char *zMsg = 0;
4076
- va_list ap;
4077
- va_start(ap, zFmt);
4078
- zMsg = sqlite3_vmprintf(zFmt, ap);
4079
- sqlite3_result_error(ctx, zMsg, -1);
4080
- sqlite3_free(zMsg);
4081
- va_end(ap);
4082
-}
4083
-
4105
+#define ZIPFILE_EOCD_FIXED_SZ 22
4106
+#define ZIPFILE_CDS_FIXED_SZ 46
40844107
40854108
/*
40864109
*** 4.3.16 End of central directory record:
40874110
***
40884111
*** end of central dir signature 4 bytes (0x06054b50)
@@ -4184,51 +4207,43 @@
41844207
u16 nExtra;
41854208
};
41864209
41874210
typedef struct ZipfileEntry ZipfileEntry;
41884211
struct ZipfileEntry {
4189
- char *zPath; /* Path of zipfile entry */
4190
- u8 *aCdsEntry; /* Buffer containing entire CDS entry */
4191
- int nCdsEntry; /* Size of buffer aCdsEntry[] in bytes */
4192
- int bDeleted; /* True if entry has been deleted */
4212
+ ZipfileCDS cds; /* Parsed CDS record */
4213
+ u32 mUnixTime; /* Modification time, in UNIX format */
4214
+ u8 *aExtra; /* cds.nExtra+cds.nComment bytes of extra data */
4215
+ i64 iDataOff; /* Offset to data in file (if aData==0) */
4216
+ u8 *aData; /* cds.szCompressed bytes of compressed data */
41934217
ZipfileEntry *pNext; /* Next element in in-memory CDS */
41944218
};
41954219
41964220
/*
4197
-** Cursor type for recursively iterating through a directory structure.
4221
+** Cursor type for zipfile tables.
41984222
*/
41994223
typedef struct ZipfileCsr ZipfileCsr;
42004224
struct ZipfileCsr {
42014225
sqlite3_vtab_cursor base; /* Base class - must be first */
42024226
i64 iId; /* Cursor ID */
4203
- int bEof; /* True when at EOF */
4227
+ u8 bEof; /* True when at EOF */
4228
+ u8 bNoop; /* If next xNext() call is no-op */
42044229
42054230
/* Used outside of write transactions */
42064231
FILE *pFile; /* Zip file */
42074232
i64 iNextOff; /* Offset of next record in central directory */
42084233
ZipfileEOCD eocd; /* Parse of central directory record */
42094234
4210
- /* Used inside write transactions */
4211
- ZipfileEntry *pCurrent;
4212
-
4213
- ZipfileCDS cds; /* Central Directory Structure */
4214
- ZipfileLFH lfh; /* Local File Header for current entry */
4215
- i64 iDataOff; /* Offset in zipfile to data */
4216
- u32 mTime; /* Extended mtime value */
4217
- int flags; /* Flags byte (see below for bits) */
4235
+ ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */
4236
+ ZipfileEntry *pCurrent; /* Current entry */
42184237
ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
42194238
};
42204239
4221
-/*
4222
-** Values for ZipfileCsr.flags.
4223
-*/
4224
-#define ZIPFILE_MTIME_VALID 0x0001
4225
-
42264240
typedef struct ZipfileTab ZipfileTab;
42274241
struct ZipfileTab {
42284242
sqlite3_vtab base; /* Base class - must be first */
42294243
char *zFile; /* Zip file this table accesses (may be NULL) */
4244
+ sqlite3 *db; /* Host database connection */
42304245
u8 *aBuffer; /* Temporary buffer used for various tasks */
42314246
42324247
ZipfileCsr *pCsrList; /* List of cursors */
42334248
i64 iNextCsrid;
42344249
@@ -4238,21 +4253,37 @@
42384253
FILE *pWriteFd; /* File handle open on zip archive */
42394254
i64 szCurrent; /* Current size of zip archive */
42404255
i64 szOrig; /* Size of archive at start of transaction */
42414256
};
42424257
4258
+/*
4259
+** Set the error message contained in context ctx to the results of
4260
+** vprintf(zFmt, ...).
4261
+*/
4262
+static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4263
+ char *zMsg = 0;
4264
+ va_list ap;
4265
+ va_start(ap, zFmt);
4266
+ zMsg = sqlite3_vmprintf(zFmt, ap);
4267
+ sqlite3_result_error(ctx, zMsg, -1);
4268
+ sqlite3_free(zMsg);
4269
+ va_end(ap);
4270
+}
4271
+
4272
+/*
4273
+** If string zIn is quoted, dequote it in place. Otherwise, if the string
4274
+** is not quoted, do nothing.
4275
+*/
42434276
static void zipfileDequote(char *zIn){
42444277
char q = zIn[0];
42454278
if( q=='"' || q=='\'' || q=='`' || q=='[' ){
4246
- char c;
42474279
int iIn = 1;
42484280
int iOut = 0;
42494281
if( q=='[' ) q = ']';
4250
- while( (c = zIn[iIn++]) ){
4251
- if( c==q ){
4252
- if( zIn[iIn++]!=q ) break;
4253
- }
4282
+ while( ALWAYS(zIn[iIn]) ){
4283
+ char c = zIn[iIn++];
4284
+ if( c==q && zIn[iIn++]!=q ) break;
42544285
zIn[iOut++] = c;
42554286
}
42564287
zIn[iOut] = '\0';
42574288
}
42584289
}
@@ -4275,10 +4306,25 @@
42754306
int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
42764307
int nFile = 0;
42774308
const char *zFile = 0;
42784309
ZipfileTab *pNew = 0;
42794310
int rc;
4311
+
4312
+ /* If the table name is not "zipfile", require that the argument be
4313
+ ** specified. This stops zipfile tables from being created as:
4314
+ **
4315
+ ** CREATE VIRTUAL TABLE zzz USING zipfile();
4316
+ **
4317
+ ** It does not prevent:
4318
+ **
4319
+ ** CREATE VIRTUAL TABLE zipfile USING zipfile();
4320
+ */
4321
+ assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
4322
+ if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
4323
+ *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
4324
+ return SQLITE_ERROR;
4325
+ }
42804326
42814327
if( argc>3 ){
42824328
zFile = argv[3];
42834329
nFile = (int)strlen(zFile)+1;
42844330
}
@@ -4286,10 +4332,11 @@
42864332
rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
42874333
if( rc==SQLITE_OK ){
42884334
pNew = (ZipfileTab*)sqlite3_malloc(nByte+nFile);
42894335
if( pNew==0 ) return SQLITE_NOMEM;
42904336
memset(pNew, 0, nByte+nFile);
4337
+ pNew->db = db;
42914338
pNew->aBuffer = (u8*)&pNew[1];
42924339
if( zFile ){
42934340
pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
42944341
memcpy(pNew->zFile, zFile, nFile);
42954342
zipfileDequote(pNew->zFile);
@@ -4296,15 +4343,48 @@
42964343
}
42974344
}
42984345
*ppVtab = (sqlite3_vtab*)pNew;
42994346
return rc;
43004347
}
4348
+
4349
+/*
4350
+** Free the ZipfileEntry structure indicated by the only argument.
4351
+*/
4352
+static void zipfileEntryFree(ZipfileEntry *p){
4353
+ if( p ){
4354
+ sqlite3_free(p->cds.zFile);
4355
+ sqlite3_free(p);
4356
+ }
4357
+}
4358
+
4359
+/*
4360
+** Release resources that should be freed at the end of a write
4361
+** transaction.
4362
+*/
4363
+static void zipfileCleanupTransaction(ZipfileTab *pTab){
4364
+ ZipfileEntry *pEntry;
4365
+ ZipfileEntry *pNext;
4366
+
4367
+ if( pTab->pWriteFd ){
4368
+ fclose(pTab->pWriteFd);
4369
+ pTab->pWriteFd = 0;
4370
+ }
4371
+ for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
4372
+ pNext = pEntry->pNext;
4373
+ zipfileEntryFree(pEntry);
4374
+ }
4375
+ pTab->pFirstEntry = 0;
4376
+ pTab->pLastEntry = 0;
4377
+ pTab->szCurrent = 0;
4378
+ pTab->szOrig = 0;
4379
+}
43014380
43024381
/*
43034382
** This method is the destructor for zipfile vtab objects.
43044383
*/
43054384
static int zipfileDisconnect(sqlite3_vtab *pVtab){
4385
+ zipfileCleanupTransaction((ZipfileTab*)pVtab);
43064386
sqlite3_free(pVtab);
43074387
return SQLITE_OK;
43084388
}
43094389
43104390
/*
@@ -4328,16 +4408,24 @@
43284408
/*
43294409
** Reset a cursor back to the state it was in when first returned
43304410
** by zipfileOpen().
43314411
*/
43324412
static void zipfileResetCursor(ZipfileCsr *pCsr){
4333
- sqlite3_free(pCsr->cds.zFile);
4334
- pCsr->cds.zFile = 0;
4413
+ ZipfileEntry *p;
4414
+ ZipfileEntry *pNext;
4415
+
43354416
pCsr->bEof = 0;
43364417
if( pCsr->pFile ){
43374418
fclose(pCsr->pFile);
43384419
pCsr->pFile = 0;
4420
+ zipfileEntryFree(pCsr->pCurrent);
4421
+ pCsr->pCurrent = 0;
4422
+ }
4423
+
4424
+ for(p=pCsr->pFreeEntry; p; p=pNext){
4425
+ pNext = p->pNext;
4426
+ zipfileEntryFree(p);
43394427
}
43404428
}
43414429
43424430
/*
43434431
** Destructor for an ZipfileCsr.
@@ -4347,32 +4435,46 @@
43474435
ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
43484436
ZipfileCsr **pp;
43494437
zipfileResetCursor(pCsr);
43504438
43514439
/* Remove this cursor from the ZipfileTab.pCsrList list. */
4352
- for(pp=&pTab->pCsrList; *pp; pp=&((*pp)->pCsrNext)){
4353
- if( *pp==pCsr ){
4354
- *pp = pCsr->pCsrNext;
4355
- break;
4356
- }
4357
- }
4440
+ for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
4441
+ *pp = pCsr->pCsrNext;
43584442
43594443
sqlite3_free(pCsr);
43604444
return SQLITE_OK;
43614445
}
43624446
43634447
/*
43644448
** Set the error message for the virtual table associated with cursor
43654449
** pCsr to the results of vprintf(zFmt, ...).
43664450
*/
4367
-static void zipfileSetErrmsg(ZipfileCsr *pCsr, const char *zFmt, ...){
4451
+static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
4452
+ va_list ap;
4453
+ va_start(ap, zFmt);
4454
+ sqlite3_free(pTab->base.zErrMsg);
4455
+ pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
4456
+ va_end(ap);
4457
+}
4458
+static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
43684459
va_list ap;
43694460
va_start(ap, zFmt);
4461
+ sqlite3_free(pCsr->base.pVtab->zErrMsg);
43704462
pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
43714463
va_end(ap);
43724464
}
43734465
4466
+/*
4467
+** Read nRead bytes of data from offset iOff of file pFile into buffer
4468
+** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
4469
+** otherwise.
4470
+**
4471
+** If an error does occur, output variable (*pzErrmsg) may be set to point
4472
+** to an English language error message. It is the responsibility of the
4473
+** caller to eventually free this buffer using
4474
+** sqlite3_free().
4475
+*/
43744476
static int zipfileReadData(
43754477
FILE *pFile, /* Read from this file */
43764478
u8 *aRead, /* Read into this buffer */
43774479
int nRead, /* Number of bytes to read */
43784480
i64 iOff, /* Offset to read from */
@@ -4402,24 +4504,38 @@
44024504
}
44034505
pTab->szCurrent += nWrite;
44044506
return SQLITE_OK;
44054507
}
44064508
4509
+/*
4510
+** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
4511
+*/
44074512
static u16 zipfileGetU16(const u8 *aBuf){
44084513
return (aBuf[1] << 8) + aBuf[0];
44094514
}
4515
+
4516
+/*
4517
+** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
4518
+*/
44104519
static u32 zipfileGetU32(const u8 *aBuf){
44114520
return ((u32)(aBuf[3]) << 24)
44124521
+ ((u32)(aBuf[2]) << 16)
44134522
+ ((u32)(aBuf[1]) << 8)
44144523
+ ((u32)(aBuf[0]) << 0);
44154524
}
44164525
4526
+/*
4527
+** Write a 16-bit little endiate integer into buffer aBuf.
4528
+*/
44174529
static void zipfilePutU16(u8 *aBuf, u16 val){
44184530
aBuf[0] = val & 0xFF;
44194531
aBuf[1] = (val>>8) & 0xFF;
44204532
}
4533
+
4534
+/*
4535
+** Write a 32-bit little endiate integer into buffer aBuf.
4536
+*/
44214537
static void zipfilePutU32(u8 *aBuf, u32 val){
44224538
aBuf[0] = val & 0xFF;
44234539
aBuf[1] = (val>>8) & 0xFF;
44244540
aBuf[2] = (val>>16) & 0xFF;
44254541
aBuf[3] = (val>>24) & 0xFF;
@@ -4429,19 +4545,15 @@
44294545
#define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
44304546
44314547
#define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
44324548
#define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
44334549
4434
-static u8* zipfileCsrBuffer(ZipfileCsr *pCsr){
4435
- return ((ZipfileTab*)(pCsr->base.pVtab))->aBuffer;
4436
-}
4437
-
44384550
/*
44394551
** Magic numbers used to read CDS records.
44404552
*/
4441
-#define ZIPFILE_CDS_FIXED_SZ 46
44424553
#define ZIPFILE_CDS_NFILE_OFF 28
4554
+#define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
44434555
44444556
/*
44454557
** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
44464558
** if the record is not well-formed, or SQLITE_OK otherwise.
44474559
*/
@@ -4474,172 +4586,84 @@
44744586
44754587
return rc;
44764588
}
44774589
44784590
/*
4479
-** Read the CDS record for the current entry from disk into pCsr->cds.
4480
-*/
4481
-static int zipfileCsrReadCDS(ZipfileCsr *pCsr){
4482
- char **pzErr = &pCsr->base.pVtab->zErrMsg;
4483
- u8 *aRead;
4484
- int rc = SQLITE_OK;
4485
-
4486
- sqlite3_free(pCsr->cds.zFile);
4487
- pCsr->cds.zFile = 0;
4488
-
4489
- if( pCsr->pCurrent==0 ){
4490
- aRead = zipfileCsrBuffer(pCsr);
4491
- rc = zipfileReadData(
4492
- pCsr->pFile, aRead, ZIPFILE_CDS_FIXED_SZ, pCsr->iNextOff, pzErr
4493
- );
4494
- }else{
4495
- aRead = pCsr->pCurrent->aCdsEntry;
4496
- }
4497
-
4498
- if( rc==SQLITE_OK ){
4499
- rc = zipfileReadCDS(aRead, &pCsr->cds);
4500
- if( rc!=SQLITE_OK ){
4501
- assert( pCsr->pCurrent==0 );
4502
- zipfileSetErrmsg(pCsr,"failed to read CDS at offset %lld",pCsr->iNextOff);
4503
- }else{
4504
- int nRead;
4505
- if( pCsr->pCurrent==0 ){
4506
- nRead = pCsr->cds.nFile + pCsr->cds.nExtra;
4507
- aRead = zipfileCsrBuffer(pCsr);
4508
- pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
4509
- rc = zipfileReadData(pCsr->pFile, aRead, nRead, pCsr->iNextOff, pzErr);
4510
- }else{
4511
- aRead = &aRead[ZIPFILE_CDS_FIXED_SZ];
4512
- }
4513
-
4514
- if( rc==SQLITE_OK ){
4515
- pCsr->cds.zFile = sqlite3_mprintf("%.*s", (int)pCsr->cds.nFile, aRead);
4516
- pCsr->iNextOff += pCsr->cds.nFile;
4517
- pCsr->iNextOff += pCsr->cds.nExtra;
4518
- pCsr->iNextOff += pCsr->cds.nComment;
4519
- }
4520
-
4521
- /* Scan the cds.nExtra bytes of "extra" fields for any that can
4522
- ** be interpreted. The general format of an extra field is:
4523
- **
4524
- ** Header ID 2 bytes
4525
- ** Data Size 2 bytes
4526
- ** Data N bytes
4527
- **
4528
- */
4529
- if( rc==SQLITE_OK ){
4530
- u8 *p = &aRead[pCsr->cds.nFile];
4531
- u8 *pEnd = &p[pCsr->cds.nExtra];
4532
-
4533
- while( p<pEnd ){
4534
- u16 id = zipfileRead16(p);
4535
- u16 nByte = zipfileRead16(p);
4536
-
4537
- switch( id ){
4538
- case ZIPFILE_EXTRA_TIMESTAMP: {
4539
- u8 b = p[0];
4540
- if( b & 0x01 ){ /* 0x01 -> modtime is present */
4541
- pCsr->mTime = zipfileGetU32(&p[1]);
4542
- pCsr->flags |= ZIPFILE_MTIME_VALID;
4543
- }
4544
- break;
4545
- }
4546
- }
4547
-
4548
- p += nByte;
4549
- }
4550
- }
4551
- }
4552
- }
4553
-
4554
- return rc;
4555
-}
4556
-
4557
-static FILE *zipfileGetFd(ZipfileCsr *pCsr){
4558
- if( pCsr->pFile ) return pCsr->pFile;
4559
- return ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
4560
-}
4561
-
4591
+** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
4592
+** if the record is not well-formed, or SQLITE_OK otherwise.
4593
+*/
45624594
static int zipfileReadLFH(
4563
- FILE *pFd,
4564
- i64 iOffset,
4565
- u8 *aTmp,
4566
- ZipfileLFH *pLFH,
4567
- char **pzErr
4568
-){
4569
- u8 *aRead = aTmp;
4570
- static const int szFix = ZIPFILE_LFH_FIXED_SZ;
4571
- int rc;
4572
-
4573
- rc = zipfileReadData(pFd, aRead, szFix, iOffset, pzErr);
4574
- if( rc==SQLITE_OK ){
4575
- u32 sig = zipfileRead32(aRead);
4576
- if( sig!=ZIPFILE_SIGNATURE_LFH ){
4577
- *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", (int)iOffset);
4578
- rc = SQLITE_ERROR;
4579
- }else{
4580
- pLFH->iVersionExtract = zipfileRead16(aRead);
4581
- pLFH->flags = zipfileRead16(aRead);
4582
- pLFH->iCompression = zipfileRead16(aRead);
4583
- pLFH->mTime = zipfileRead16(aRead);
4584
- pLFH->mDate = zipfileRead16(aRead);
4585
- pLFH->crc32 = zipfileRead32(aRead);
4586
- pLFH->szCompressed = zipfileRead32(aRead);
4587
- pLFH->szUncompressed = zipfileRead32(aRead);
4588
- pLFH->nFile = zipfileRead16(aRead);
4589
- pLFH->nExtra = zipfileRead16(aRead);
4590
- assert( aRead==&aTmp[szFix] );
4591
- }
4592
- }
4593
- return rc;
4594
-}
4595
-
4596
-static int zipfileCsrReadLFH(ZipfileCsr *pCsr){
4597
- FILE *pFile = zipfileGetFd(pCsr);
4598
- char **pzErr = &pCsr->base.pVtab->zErrMsg;
4599
- u8 *aRead = zipfileCsrBuffer(pCsr);
4600
- int rc = zipfileReadLFH(pFile, pCsr->cds.iOffset, aRead, &pCsr->lfh, pzErr);
4601
- pCsr->iDataOff = pCsr->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
4602
- pCsr->iDataOff += pCsr->lfh.nFile+pCsr->lfh.nExtra;
4603
- return rc;
4604
-}
4605
-
4606
-
4607
-/*
4608
-** Advance an ZipfileCsr to its next row of output.
4609
-*/
4610
-static int zipfileNext(sqlite3_vtab_cursor *cur){
4611
- ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4612
- int rc = SQLITE_OK;
4613
- pCsr->flags = 0;
4614
-
4615
- if( pCsr->pCurrent==0 ){
4616
- i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
4617
- if( pCsr->iNextOff>=iEof ){
4618
- pCsr->bEof = 1;
4619
- }
4620
- }else{
4621
- assert( pCsr->pFile==0 );
4622
- do {
4623
- pCsr->pCurrent = pCsr->pCurrent->pNext;
4624
- }while( pCsr->pCurrent && pCsr->pCurrent->bDeleted );
4625
- if( pCsr->pCurrent==0 ){
4626
- pCsr->bEof = 1;
4627
- }
4628
- }
4629
-
4630
- if( pCsr->bEof==0 ){
4631
- rc = zipfileCsrReadCDS(pCsr);
4632
- if( rc==SQLITE_OK ){
4633
- rc = zipfileCsrReadLFH(pCsr);
4634
- }
4635
- }
4636
-
4637
- return rc;
4638
-}
4639
-
4640
-/*
4595
+ u8 *aBuffer,
4596
+ ZipfileLFH *pLFH
4597
+){
4598
+ u8 *aRead = aBuffer;
4599
+ int rc = SQLITE_OK;
4600
+
4601
+ u32 sig = zipfileRead32(aRead);
4602
+ if( sig!=ZIPFILE_SIGNATURE_LFH ){
4603
+ rc = SQLITE_ERROR;
4604
+ }else{
4605
+ pLFH->iVersionExtract = zipfileRead16(aRead);
4606
+ pLFH->flags = zipfileRead16(aRead);
4607
+ pLFH->iCompression = zipfileRead16(aRead);
4608
+ pLFH->mTime = zipfileRead16(aRead);
4609
+ pLFH->mDate = zipfileRead16(aRead);
4610
+ pLFH->crc32 = zipfileRead32(aRead);
4611
+ pLFH->szCompressed = zipfileRead32(aRead);
4612
+ pLFH->szUncompressed = zipfileRead32(aRead);
4613
+ pLFH->nFile = zipfileRead16(aRead);
4614
+ pLFH->nExtra = zipfileRead16(aRead);
4615
+ }
4616
+ return rc;
4617
+}
4618
+
4619
+
4620
+/*
4621
+** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
4622
+** Scan through this buffer to find an "extra-timestamp" field. If one
4623
+** exists, extract the 32-bit modification-timestamp from it and store
4624
+** the value in output parameter *pmTime.
4625
+**
4626
+** Zero is returned if no extra-timestamp record could be found (and so
4627
+** *pmTime is left unchanged), or non-zero otherwise.
4628
+**
4629
+** The general format of an extra field is:
4630
+**
4631
+** Header ID 2 bytes
4632
+** Data Size 2 bytes
4633
+** Data N bytes
4634
+*/
4635
+static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
4636
+ int ret = 0;
4637
+ u8 *p = aExtra;
4638
+ u8 *pEnd = &aExtra[nExtra];
4639
+
4640
+ while( p<pEnd ){
4641
+ u16 id = zipfileRead16(p);
4642
+ u16 nByte = zipfileRead16(p);
4643
+
4644
+ switch( id ){
4645
+ case ZIPFILE_EXTRA_TIMESTAMP: {
4646
+ u8 b = p[0];
4647
+ if( b & 0x01 ){ /* 0x01 -> modtime is present */
4648
+ *pmTime = zipfileGetU32(&p[1]);
4649
+ ret = 1;
4650
+ }
4651
+ break;
4652
+ }
4653
+ }
4654
+
4655
+ p += nByte;
4656
+ }
4657
+ return ret;
4658
+}
4659
+
4660
+/*
4661
+** Convert the standard MS-DOS timestamp stored in the mTime and mDate
4662
+** fields of the CDS structure passed as the only argument to a 32-bit
4663
+** UNIX seconds-since-the-epoch timestamp. Return the result.
4664
+**
46414665
** "Standard" MS-DOS time format:
46424666
**
46434667
** File modification time:
46444668
** Bits 00-04: seconds divided by 2
46454669
** Bits 05-10: minute
@@ -4646,48 +4670,241 @@
46464670
** Bits 11-15: hour
46474671
** File modification date:
46484672
** Bits 00-04: day
46494673
** Bits 05-08: month (1-12)
46504674
** Bits 09-15: years from 1980
4651
-*/
4652
-static time_t zipfileMtime(ZipfileCsr *pCsr){
4653
- struct tm t;
4654
- memset(&t, 0, sizeof(t));
4655
- t.tm_sec = (pCsr->cds.mTime & 0x1F)*2;
4656
- t.tm_min = (pCsr->cds.mTime >> 5) & 0x2F;
4657
- t.tm_hour = (pCsr->cds.mTime >> 11) & 0x1F;
4658
-
4659
- t.tm_mday = (pCsr->cds.mDate & 0x1F);
4660
- t.tm_mon = ((pCsr->cds.mDate >> 5) & 0x0F) - 1;
4661
- t.tm_year = 80 + ((pCsr->cds.mDate >> 9) & 0x7F);
4662
-
4663
- return mktime(&t);
4664
-}
4665
-
4666
-static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mTime){
4667
- time_t t = (time_t)mTime;
4668
- struct tm res;
4669
-
4670
-#if !defined(_WIN32) && !defined(WIN32)
4671
- localtime_r(&t, &res);
4672
-#else
4673
- memcpy(&res, localtime(&t), sizeof(struct tm));
4674
-#endif
4675
-
4676
- pCds->mTime = (u16)(
4677
- (res.tm_sec / 2) +
4678
- (res.tm_min << 5) +
4679
- (res.tm_hour << 11));
4680
-
4681
- pCds->mDate = (u16)(
4682
- (res.tm_mday-1) +
4683
- ((res.tm_mon+1) << 5) +
4684
- ((res.tm_year-80) << 9));
4685
-}
4686
-
4675
+**
4676
+** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
4677
+*/
4678
+static u32 zipfileMtime(ZipfileCDS *pCDS){
4679
+ int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
4680
+ int M = ((pCDS->mDate >> 5) & 0x0F);
4681
+ int D = (pCDS->mDate & 0x1F);
4682
+ int B = -13;
4683
+
4684
+ int sec = (pCDS->mTime & 0x1F)*2;
4685
+ int min = (pCDS->mTime >> 5) & 0x3F;
4686
+ int hr = (pCDS->mTime >> 11) & 0x1F;
4687
+ i64 JD;
4688
+
4689
+ /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
4690
+
4691
+ /* Calculate the JD in seconds for noon on the day in question */
4692
+ if( M<3 ){
4693
+ Y = Y-1;
4694
+ M = M+12;
4695
+ }
4696
+ JD = (i64)(24*60*60) * (
4697
+ (int)(365.25 * (Y + 4716))
4698
+ + (int)(30.6001 * (M + 1))
4699
+ + D + B - 1524
4700
+ );
4701
+
4702
+ /* Correct the JD for the time within the day */
4703
+ JD += (hr-12) * 3600 + min * 60 + sec;
4704
+
4705
+ /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
4706
+ return (u32)(JD - (i64)(24405875) * 24*60*6);
4707
+}
4708
+
4709
+/*
4710
+** The opposite of zipfileMtime(). This function populates the mTime and
4711
+** mDate fields of the CDS structure passed as the first argument according
4712
+** to the UNIX timestamp value passed as the second.
4713
+*/
4714
+static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
4715
+ /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
4716
+ i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
4717
+
4718
+ int A, B, C, D, E;
4719
+ int yr, mon, day;
4720
+ int hr, min, sec;
4721
+
4722
+ A = (int)((JD - 1867216.25)/36524.25);
4723
+ A = (int)(JD + 1 + A - (A/4));
4724
+ B = A + 1524;
4725
+ C = (int)((B - 122.1)/365.25);
4726
+ D = (36525*(C&32767))/100;
4727
+ E = (int)((B-D)/30.6001);
4728
+
4729
+ day = B - D - (int)(30.6001*E);
4730
+ mon = (E<14 ? E-1 : E-13);
4731
+ yr = mon>2 ? C-4716 : C-4715;
4732
+
4733
+ hr = (mUnixTime % (24*60*60)) / (60*60);
4734
+ min = (mUnixTime % (60*60)) / 60;
4735
+ sec = (mUnixTime % 60);
4736
+
4737
+ if( yr>=1980 ){
4738
+ pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
4739
+ pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
4740
+ }else{
4741
+ pCds->mDate = pCds->mTime = 0;
4742
+ }
4743
+
4744
+ assert( mUnixTime<315507600
4745
+ || mUnixTime==zipfileMtime(pCds)
4746
+ || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
4747
+ /* || (mUnixTime % 2) */
4748
+ );
4749
+}
4750
+
4751
+/*
4752
+** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
4753
+** size) containing an entire zip archive image. Or, if aBlob is NULL,
4754
+** then pFile is a file-handle open on a zip file. In either case, this
4755
+** function creates a ZipfileEntry object based on the zip archive entry
4756
+** for which the CDS record is at offset iOff.
4757
+**
4758
+** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
4759
+** the new object. Otherwise, an SQLite error code is returned and the
4760
+** final value of (*ppEntry) undefined.
4761
+*/
4762
+static int zipfileGetEntry(
4763
+ ZipfileTab *pTab, /* Store any error message here */
4764
+ const u8 *aBlob, /* Pointer to in-memory file image */
4765
+ int nBlob, /* Size of aBlob[] in bytes */
4766
+ FILE *pFile, /* If aBlob==0, read from this file */
4767
+ i64 iOff, /* Offset of CDS record */
4768
+ ZipfileEntry **ppEntry /* OUT: Pointer to new object */
4769
+){
4770
+ u8 *aRead;
4771
+ char **pzErr = &pTab->base.zErrMsg;
4772
+ int rc = SQLITE_OK;
4773
+
4774
+ if( aBlob==0 ){
4775
+ aRead = pTab->aBuffer;
4776
+ rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
4777
+ }else{
4778
+ aRead = (u8*)&aBlob[iOff];
4779
+ }
4780
+
4781
+ if( rc==SQLITE_OK ){
4782
+ int nAlloc;
4783
+ ZipfileEntry *pNew;
4784
+
4785
+ int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
4786
+ int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
4787
+ nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
4788
+
4789
+ nAlloc = sizeof(ZipfileEntry) + nExtra;
4790
+ if( aBlob ){
4791
+ nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
4792
+ }
4793
+
4794
+ pNew = (ZipfileEntry*)sqlite3_malloc(nAlloc);
4795
+ if( pNew==0 ){
4796
+ rc = SQLITE_NOMEM;
4797
+ }else{
4798
+ memset(pNew, 0, sizeof(ZipfileEntry));
4799
+ rc = zipfileReadCDS(aRead, &pNew->cds);
4800
+ if( rc!=SQLITE_OK ){
4801
+ *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
4802
+ }else if( aBlob==0 ){
4803
+ rc = zipfileReadData(
4804
+ pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
4805
+ );
4806
+ }else{
4807
+ aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
4808
+ }
4809
+ }
4810
+
4811
+ if( rc==SQLITE_OK ){
4812
+ u32 *pt = &pNew->mUnixTime;
4813
+ pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
4814
+ pNew->aExtra = (u8*)&pNew[1];
4815
+ memcpy(pNew->aExtra, &aRead[nFile], nExtra);
4816
+ if( pNew->cds.zFile==0 ){
4817
+ rc = SQLITE_NOMEM;
4818
+ }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
4819
+ pNew->mUnixTime = zipfileMtime(&pNew->cds);
4820
+ }
4821
+ }
4822
+
4823
+ if( rc==SQLITE_OK ){
4824
+ static const int szFix = ZIPFILE_LFH_FIXED_SZ;
4825
+ ZipfileLFH lfh;
4826
+ if( pFile ){
4827
+ rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
4828
+ }else{
4829
+ aRead = (u8*)&aBlob[pNew->cds.iOffset];
4830
+ }
4831
+
4832
+ rc = zipfileReadLFH(aRead, &lfh);
4833
+ if( rc==SQLITE_OK ){
4834
+ pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
4835
+ pNew->iDataOff += lfh.nFile + lfh.nExtra;
4836
+ if( aBlob && pNew->cds.szCompressed ){
4837
+ pNew->aData = &pNew->aExtra[nExtra];
4838
+ memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
4839
+ }
4840
+ }else{
4841
+ *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
4842
+ (int)pNew->cds.iOffset
4843
+ );
4844
+ }
4845
+ }
4846
+
4847
+ if( rc!=SQLITE_OK ){
4848
+ zipfileEntryFree(pNew);
4849
+ }else{
4850
+ *ppEntry = pNew;
4851
+ }
4852
+ }
4853
+
4854
+ return rc;
4855
+}
4856
+
4857
+/*
4858
+** Advance an ZipfileCsr to its next row of output.
4859
+*/
4860
+static int zipfileNext(sqlite3_vtab_cursor *cur){
4861
+ ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4862
+ int rc = SQLITE_OK;
4863
+
4864
+ if( pCsr->pFile ){
4865
+ i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
4866
+ zipfileEntryFree(pCsr->pCurrent);
4867
+ pCsr->pCurrent = 0;
4868
+ if( pCsr->iNextOff>=iEof ){
4869
+ pCsr->bEof = 1;
4870
+ }else{
4871
+ ZipfileEntry *p = 0;
4872
+ ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
4873
+ rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
4874
+ if( rc==SQLITE_OK ){
4875
+ pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
4876
+ pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
4877
+ }
4878
+ pCsr->pCurrent = p;
4879
+ }
4880
+ }else{
4881
+ if( !pCsr->bNoop ){
4882
+ pCsr->pCurrent = pCsr->pCurrent->pNext;
4883
+ }
4884
+ if( pCsr->pCurrent==0 ){
4885
+ pCsr->bEof = 1;
4886
+ }
4887
+ }
4888
+
4889
+ pCsr->bNoop = 0;
4890
+ return rc;
4891
+}
4892
+
4893
+static void zipfileFree(void *p) {
4894
+ sqlite3_free(p);
4895
+}
4896
+
4897
+/*
4898
+** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
4899
+** size is nOut bytes. This function uncompresses the data and sets the
4900
+** return value in context pCtx to the result (a blob).
4901
+**
4902
+** If an error occurs, an error code is left in pCtx instead.
4903
+*/
46874904
static void zipfileInflate(
4688
- sqlite3_context *pCtx, /* Store error here, if any */
4905
+ sqlite3_context *pCtx, /* Store result here */
46894906
const u8 *aIn, /* Compressed data */
46904907
int nIn, /* Size of buffer aIn[] in bytes */
46914908
int nOut /* Expected output size */
46924909
){
46934910
u8 *aRes = sqlite3_malloc(nOut);
@@ -4709,22 +4926,35 @@
47094926
}else{
47104927
err = inflate(&str, Z_NO_FLUSH);
47114928
if( err!=Z_STREAM_END ){
47124929
zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
47134930
}else{
4714
- sqlite3_result_blob(pCtx, aRes, nOut, SQLITE_TRANSIENT);
4931
+ sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
4932
+ aRes = 0;
47154933
}
47164934
}
47174935
sqlite3_free(aRes);
47184936
inflateEnd(&str);
47194937
}
47204938
}
47214939
4940
+/*
4941
+** Buffer aIn (size nIn bytes) contains uncompressed data. This function
4942
+** compresses it and sets (*ppOut) to point to a buffer containing the
4943
+** compressed data. The caller is responsible for eventually calling
4944
+** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
4945
+** is set to the size of buffer (*ppOut) in bytes.
4946
+**
4947
+** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
4948
+** code is returned and an error message left in virtual-table handle
4949
+** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
4950
+** case.
4951
+*/
47224952
static int zipfileDeflate(
4723
- ZipfileTab *pTab, /* Set error message here */
47244953
const u8 *aIn, int nIn, /* Input */
4725
- u8 **ppOut, int *pnOut /* Output */
4954
+ u8 **ppOut, int *pnOut, /* Output */
4955
+ char **pzErr /* OUT: Error message */
47264956
){
47274957
int nAlloc = (int)compressBound(nIn);
47284958
u8 *aOut;
47294959
int rc = SQLITE_OK;
47304960
@@ -4746,11 +4976,11 @@
47464976
if( res==Z_STREAM_END ){
47474977
*ppOut = aOut;
47484978
*pnOut = (int)str.total_out;
47494979
}else{
47504980
sqlite3_free(aOut);
4751
- pTab->base.zErrMsg = sqlite3_mprintf("zipfile: deflate() error");
4981
+ *pzErr = sqlite3_mprintf("zipfile: deflate() error");
47524982
rc = SQLITE_ERROR;
47534983
}
47544984
deflateEnd(&str);
47554985
}
47564986
@@ -4766,121 +4996,134 @@
47664996
sqlite3_vtab_cursor *cur, /* The cursor */
47674997
sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
47684998
int i /* Which column to return */
47694999
){
47705000
ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5001
+ ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
47715002
int rc = SQLITE_OK;
47725003
switch( i ){
47735004
case 0: /* name */
4774
- sqlite3_result_text(ctx, pCsr->cds.zFile, -1, SQLITE_TRANSIENT);
5005
+ sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
47755006
break;
47765007
case 1: /* mode */
47775008
/* TODO: Whether or not the following is correct surely depends on
47785009
** the platform on which the archive was created. */
4779
- sqlite3_result_int(ctx, pCsr->cds.iExternalAttr >> 16);
5010
+ sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
47805011
break;
47815012
case 2: { /* mtime */
4782
- if( pCsr->flags & ZIPFILE_MTIME_VALID ){
4783
- sqlite3_result_int64(ctx, pCsr->mTime);
4784
- }else{
4785
- sqlite3_result_int64(ctx, zipfileMtime(pCsr));
4786
- }
5013
+ sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
47875014
break;
47885015
}
47895016
case 3: { /* sz */
47905017
if( sqlite3_vtab_nochange(ctx)==0 ){
4791
- sqlite3_result_int64(ctx, pCsr->cds.szUncompressed);
5018
+ sqlite3_result_int64(ctx, pCDS->szUncompressed);
47925019
}
47935020
break;
47945021
}
47955022
case 4: /* rawdata */
47965023
if( sqlite3_vtab_nochange(ctx) ) break;
47975024
case 5: { /* data */
4798
- if( i==4 || pCsr->cds.iCompression==0 || pCsr->cds.iCompression==8 ){
4799
- int sz = pCsr->cds.szCompressed;
4800
- int szFinal = pCsr->cds.szUncompressed;
5025
+ if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
5026
+ int sz = pCDS->szCompressed;
5027
+ int szFinal = pCDS->szUncompressed;
48015028
if( szFinal>0 ){
4802
- u8 *aBuf = sqlite3_malloc(sz);
4803
- if( aBuf==0 ){
4804
- rc = SQLITE_NOMEM;
5029
+ u8 *aBuf;
5030
+ u8 *aFree = 0;
5031
+ if( pCsr->pCurrent->aData ){
5032
+ aBuf = pCsr->pCurrent->aData;
48055033
}else{
4806
- FILE *pFile = zipfileGetFd(pCsr);
4807
- rc = zipfileReadData(pFile, aBuf, sz, pCsr->iDataOff,
4808
- &pCsr->base.pVtab->zErrMsg
4809
- );
5034
+ aBuf = aFree = sqlite3_malloc(sz);
5035
+ if( aBuf==0 ){
5036
+ rc = SQLITE_NOMEM;
5037
+ }else{
5038
+ FILE *pFile = pCsr->pFile;
5039
+ if( pFile==0 ){
5040
+ pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
5041
+ }
5042
+ rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
5043
+ &pCsr->base.pVtab->zErrMsg
5044
+ );
5045
+ }
48105046
}
48115047
if( rc==SQLITE_OK ){
4812
- if( i==5 && pCsr->cds.iCompression ){
5048
+ if( i==5 && pCDS->iCompression ){
48135049
zipfileInflate(ctx, aBuf, sz, szFinal);
48145050
}else{
48155051
sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
48165052
}
4817
- sqlite3_free(aBuf);
48185053
}
5054
+ sqlite3_free(aFree);
48195055
}else{
48205056
/* Figure out if this is a directory or a zero-sized file. Consider
48215057
** it to be a directory either if the mode suggests so, or if
48225058
** the final character in the name is '/'. */
4823
- u32 mode = pCsr->cds.iExternalAttr >> 16;
4824
- if( !(mode & S_IFDIR) && pCsr->cds.zFile[pCsr->cds.nFile-1]!='/' ){
5059
+ u32 mode = pCDS->iExternalAttr >> 16;
5060
+ if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
48255061
sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
48265062
}
48275063
}
48285064
}
48295065
break;
48305066
}
48315067
case 6: /* method */
4832
- sqlite3_result_int(ctx, pCsr->cds.iCompression);
5068
+ sqlite3_result_int(ctx, pCDS->iCompression);
48335069
break;
4834
- case 7: /* z */
5070
+ default: /* z */
5071
+ assert( i==7 );
48355072
sqlite3_result_int64(ctx, pCsr->iId);
48365073
break;
48375074
}
48385075
48395076
return rc;
48405077
}
48415078
48425079
/*
4843
-** Return the rowid for the current row.
4844
-*/
4845
-static int zipfileRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
4846
- assert( 0 );
4847
- return SQLITE_OK;
4848
-}
4849
-
4850
-/*
4851
-** Return TRUE if the cursor has been moved off of the last
4852
-** row of output.
5080
+** Return TRUE if the cursor is at EOF.
48535081
*/
48545082
static int zipfileEof(sqlite3_vtab_cursor *cur){
48555083
ZipfileCsr *pCsr = (ZipfileCsr*)cur;
48565084
return pCsr->bEof;
48575085
}
48585086
48595087
/*
5088
+** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
5089
+** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
5090
+** is guaranteed to be a file-handle open on a zip file.
5091
+**
5092
+** This function attempts to locate the EOCD record within the zip archive
5093
+** and populate *pEOCD with the results of decoding it. SQLITE_OK is
5094
+** returned if successful. Otherwise, an SQLite error code is returned and
5095
+** an English language error message may be left in virtual-table pTab.
48605096
*/
48615097
static int zipfileReadEOCD(
48625098
ZipfileTab *pTab, /* Return errors here */
4863
- FILE *pFile, /* Read from this file */
5099
+ const u8 *aBlob, /* Pointer to in-memory file image */
5100
+ int nBlob, /* Size of aBlob[] in bytes */
5101
+ FILE *pFile, /* Read from this file if aBlob==0 */
48645102
ZipfileEOCD *pEOCD /* Object to populate */
48655103
){
48665104
u8 *aRead = pTab->aBuffer; /* Temporary buffer */
4867
- i64 szFile; /* Total size of file in bytes */
48685105
int nRead; /* Bytes to read from file */
4869
- i64 iOff; /* Offset to read from */
4870
- int rc;
4871
-
4872
- fseek(pFile, 0, SEEK_END);
4873
- szFile = (i64)ftell(pFile);
4874
- if( szFile==0 ){
4875
- memset(pEOCD, 0, sizeof(ZipfileEOCD));
4876
- return SQLITE_OK;
4877
- }
4878
- nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
4879
- iOff = szFile - nRead;
4880
-
4881
- rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
5106
+ int rc = SQLITE_OK;
5107
+
5108
+ if( aBlob==0 ){
5109
+ i64 iOff; /* Offset to read from */
5110
+ i64 szFile; /* Total size of file in bytes */
5111
+ fseek(pFile, 0, SEEK_END);
5112
+ szFile = (i64)ftell(pFile);
5113
+ if( szFile==0 ){
5114
+ memset(pEOCD, 0, sizeof(ZipfileEOCD));
5115
+ return SQLITE_OK;
5116
+ }
5117
+ nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
5118
+ iOff = szFile - nRead;
5119
+ rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
5120
+ }else{
5121
+ nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
5122
+ aRead = (u8*)&aBlob[nBlob-nRead];
5123
+ }
5124
+
48825125
if( rc==SQLITE_OK ){
48835126
int i;
48845127
48855128
/* Scan backwards looking for the signature bytes */
48865129
for(i=nRead-20; i>=0; i--){
@@ -4902,21 +5145,63 @@
49025145
pEOCD->iFirstDisk = zipfileRead16(aRead);
49035146
pEOCD->nEntry = zipfileRead16(aRead);
49045147
pEOCD->nEntryTotal = zipfileRead16(aRead);
49055148
pEOCD->nSize = zipfileRead32(aRead);
49065149
pEOCD->iOffset = zipfileRead32(aRead);
4907
-
4908
-#if 0
4909
- printf("iDisk=%d iFirstDisk=%d nEntry=%d "
4910
- "nEntryTotal=%d nSize=%d iOffset=%d",
4911
- (int)pEOCD->iDisk, (int)pEOCD->iFirstDisk, (int)pEOCD->nEntry,
4912
- (int)pEOCD->nEntryTotal, (int)pEOCD->nSize, (int)pEOCD->iOffset
4913
- );
4914
-#endif
49155150
}
49165151
4917
- return SQLITE_OK;
5152
+ return rc;
5153
+}
5154
+
5155
+/*
5156
+** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
5157
+** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
5158
+** to the end of the list. Otherwise, it is added to the list immediately
5159
+** before pBefore (which is guaranteed to be a part of said list).
5160
+*/
5161
+static void zipfileAddEntry(
5162
+ ZipfileTab *pTab,
5163
+ ZipfileEntry *pBefore,
5164
+ ZipfileEntry *pNew
5165
+){
5166
+ assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5167
+ assert( pNew->pNext==0 );
5168
+ if( pBefore==0 ){
5169
+ if( pTab->pFirstEntry==0 ){
5170
+ pTab->pFirstEntry = pTab->pLastEntry = pNew;
5171
+ }else{
5172
+ assert( pTab->pLastEntry->pNext==0 );
5173
+ pTab->pLastEntry->pNext = pNew;
5174
+ pTab->pLastEntry = pNew;
5175
+ }
5176
+ }else{
5177
+ ZipfileEntry **pp;
5178
+ for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5179
+ pNew->pNext = pBefore;
5180
+ *pp = pNew;
5181
+ }
5182
+}
5183
+
5184
+static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
5185
+ ZipfileEOCD eocd;
5186
+ int rc;
5187
+ int i;
5188
+ i64 iOff;
5189
+
5190
+ rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
5191
+ iOff = eocd.iOffset;
5192
+ for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5193
+ ZipfileEntry *pNew = 0;
5194
+ rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
5195
+
5196
+ if( rc==SQLITE_OK ){
5197
+ zipfileAddEntry(pTab, 0, pNew);
5198
+ iOff += ZIPFILE_CDS_FIXED_SZ;
5199
+ iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
5200
+ }
5201
+ }
5202
+ return rc;
49185203
}
49195204
49205205
/*
49215206
** xFilter callback.
49225207
*/
@@ -4925,33 +5210,41 @@
49255210
int idxNum, const char *idxStr,
49265211
int argc, sqlite3_value **argv
49275212
){
49285213
ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
49295214
ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4930
- const char *zFile; /* Zip file to scan */
5215
+ const char *zFile = 0; /* Zip file to scan */
49315216
int rc = SQLITE_OK; /* Return Code */
5217
+ int bInMemory = 0; /* True for an in-memory zipfile */
49325218
49335219
zipfileResetCursor(pCsr);
49345220
49355221
if( pTab->zFile ){
49365222
zFile = pTab->zFile;
49375223
}else if( idxNum==0 ){
4938
- /* Error. This is an eponymous virtual table and the user has not
4939
- ** supplied a file name. */
4940
- zipfileSetErrmsg(pCsr, "table function zipfile() requires an argument");
5224
+ zipfileCursorErr(pCsr, "zipfile() function requires an argument");
49415225
return SQLITE_ERROR;
5226
+ }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
5227
+ const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
5228
+ int nBlob = sqlite3_value_bytes(argv[0]);
5229
+ assert( pTab->pFirstEntry==0 );
5230
+ rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
5231
+ pCsr->pFreeEntry = pTab->pFirstEntry;
5232
+ pTab->pFirstEntry = pTab->pLastEntry = 0;
5233
+ if( rc!=SQLITE_OK ) return rc;
5234
+ bInMemory = 1;
49425235
}else{
49435236
zFile = (const char*)sqlite3_value_text(argv[0]);
49445237
}
49455238
4946
- if( pTab->pWriteFd==0 ){
5239
+ if( 0==pTab->pWriteFd && 0==bInMemory ){
49475240
pCsr->pFile = fopen(zFile, "rb");
49485241
if( pCsr->pFile==0 ){
4949
- zipfileSetErrmsg(pCsr, "cannot open file: %s", zFile);
5242
+ zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
49505243
rc = SQLITE_ERROR;
49515244
}else{
4952
- rc = zipfileReadEOCD(pTab, pCsr->pFile, &pCsr->eocd);
5245
+ rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
49535246
if( rc==SQLITE_OK ){
49545247
if( pCsr->eocd.nEntry==0 ){
49555248
pCsr->bEof = 1;
49565249
}else{
49575250
pCsr->iNextOff = pCsr->eocd.iOffset;
@@ -4958,16 +5251,13 @@
49585251
rc = zipfileNext(cur);
49595252
}
49605253
}
49615254
}
49625255
}else{
4963
- ZipfileEntry e;
4964
- memset(&e, 0, sizeof(e));
4965
- e.pNext = pTab->pFirstEntry;
4966
- pCsr->pCurrent = &e;
5256
+ pCsr->bNoop = 1;
5257
+ pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
49675258
rc = zipfileNext(cur);
4968
- assert( pCsr->pCurrent!=&e );
49695259
}
49705260
49715261
return rc;
49725262
}
49735263
@@ -4999,226 +5289,115 @@
49995289
}
50005290
50015291
return SQLITE_OK;
50025292
}
50035293
5004
-/*
5005
-** Add object pNew to the end of the linked list that begins at
5006
-** ZipfileTab.pFirstEntry and ends with pLastEntry.
5007
-*/
5008
-static void zipfileAddEntry(
5009
- ZipfileTab *pTab,
5010
- ZipfileEntry *pBefore,
5011
- ZipfileEntry *pNew
5012
-){
5013
- assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5014
- assert( pNew->pNext==0 );
5015
- if( pBefore==0 ){
5016
- if( pTab->pFirstEntry==0 ){
5017
- pTab->pFirstEntry = pTab->pLastEntry = pNew;
5018
- }else{
5019
- assert( pTab->pLastEntry->pNext==0 );
5020
- pTab->pLastEntry->pNext = pNew;
5021
- pTab->pLastEntry = pNew;
5022
- }
5023
- }else{
5024
- ZipfileEntry **pp;
5025
- for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5026
- pNew->pNext = pBefore;
5027
- *pp = pNew;
5028
- }
5029
-}
5030
-
5031
-static int zipfileLoadDirectory(ZipfileTab *pTab){
5032
- ZipfileEOCD eocd;
5033
- int rc;
5034
-
5035
- rc = zipfileReadEOCD(pTab, pTab->pWriteFd, &eocd);
5036
- if( rc==SQLITE_OK && eocd.nEntry>0 ){
5037
- int i;
5038
- int iOff = 0;
5039
- u8 *aBuf = sqlite3_malloc(eocd.nSize);
5040
- if( aBuf==0 ){
5041
- rc = SQLITE_NOMEM;
5042
- }else{
5043
- rc = zipfileReadData(
5044
- pTab->pWriteFd, aBuf, eocd.nSize, eocd.iOffset, &pTab->base.zErrMsg
5045
- );
5046
- }
5047
-
5048
- for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5049
- u16 nFile;
5050
- u16 nExtra;
5051
- u16 nComment;
5052
- ZipfileEntry *pNew;
5053
- u8 *aRec = &aBuf[iOff];
5054
-
5055
- nFile = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF]);
5056
- nExtra = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF+2]);
5057
- nComment = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF+4]);
5058
-
5059
- pNew = sqlite3_malloc(
5060
- sizeof(ZipfileEntry)
5061
- + nFile+1
5062
- + ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment
5063
- );
5064
- if( pNew==0 ){
5065
- rc = SQLITE_NOMEM;
5066
- }else{
5067
- memset(pNew, 0, sizeof(ZipfileEntry));
5068
- pNew->zPath = (char*)&pNew[1];
5069
- memcpy(pNew->zPath, &aRec[ZIPFILE_CDS_FIXED_SZ], nFile);
5070
- pNew->zPath[nFile] = '\0';
5071
- pNew->aCdsEntry = (u8*)&pNew->zPath[nFile+1];
5072
- pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
5073
- memcpy(pNew->aCdsEntry, aRec, pNew->nCdsEntry);
5074
- zipfileAddEntry(pTab, 0, pNew);
5075
- }
5076
-
5077
- iOff += ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
5078
- }
5079
-
5080
- sqlite3_free(aBuf);
5081
- }
5082
-
5083
- return rc;
5084
-}
5085
-
5086
-static ZipfileEntry *zipfileNewEntry(
5087
- ZipfileCDS *pCds, /* Values for fixed size part of CDS */
5088
- const char *zPath, /* Path for new entry */
5089
- int nPath, /* strlen(zPath) */
5090
- u32 mTime /* Modification time (or 0) */
5091
-){
5092
- u8 *aWrite;
5093
- ZipfileEntry *pNew;
5094
- pCds->nFile = (u16)nPath;
5095
- pCds->nExtra = mTime ? 9 : 0;
5096
- pNew = (ZipfileEntry*)sqlite3_malloc(
5097
- sizeof(ZipfileEntry) +
5098
- nPath+1 +
5099
- ZIPFILE_CDS_FIXED_SZ + nPath + pCds->nExtra
5100
- );
5101
-
5294
+static ZipfileEntry *zipfileNewEntry(const char *zPath){
5295
+ ZipfileEntry *pNew;
5296
+ pNew = sqlite3_malloc(sizeof(ZipfileEntry));
51025297
if( pNew ){
51035298
memset(pNew, 0, sizeof(ZipfileEntry));
5104
- pNew->zPath = (char*)&pNew[1];
5105
- pNew->aCdsEntry = (u8*)&pNew->zPath[nPath+1];
5106
- pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ + nPath + pCds->nExtra;
5107
- memcpy(pNew->zPath, zPath, nPath+1);
5108
-
5109
- aWrite = pNew->aCdsEntry;
5110
- zipfileWrite32(aWrite, ZIPFILE_SIGNATURE_CDS);
5111
- zipfileWrite16(aWrite, pCds->iVersionMadeBy);
5112
- zipfileWrite16(aWrite, pCds->iVersionExtract);
5113
- zipfileWrite16(aWrite, pCds->flags);
5114
- zipfileWrite16(aWrite, pCds->iCompression);
5115
- zipfileWrite16(aWrite, pCds->mTime);
5116
- zipfileWrite16(aWrite, pCds->mDate);
5117
- zipfileWrite32(aWrite, pCds->crc32);
5118
- zipfileWrite32(aWrite, pCds->szCompressed);
5119
- zipfileWrite32(aWrite, pCds->szUncompressed);
5120
- zipfileWrite16(aWrite, pCds->nFile);
5121
- zipfileWrite16(aWrite, pCds->nExtra);
5122
- zipfileWrite16(aWrite, pCds->nComment); assert( pCds->nComment==0 );
5123
- zipfileWrite16(aWrite, pCds->iDiskStart);
5124
- zipfileWrite16(aWrite, pCds->iInternalAttr);
5125
- zipfileWrite32(aWrite, pCds->iExternalAttr);
5126
- zipfileWrite32(aWrite, pCds->iOffset);
5127
- assert( aWrite==&pNew->aCdsEntry[ZIPFILE_CDS_FIXED_SZ] );
5128
- memcpy(aWrite, zPath, nPath);
5129
- if( pCds->nExtra ){
5130
- aWrite += nPath;
5131
- zipfileWrite16(aWrite, ZIPFILE_EXTRA_TIMESTAMP);
5132
- zipfileWrite16(aWrite, 5);
5133
- *aWrite++ = 0x01;
5134
- zipfileWrite32(aWrite, mTime);
5135
- }
5136
- }
5137
-
5299
+ pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
5300
+ if( pNew->cds.zFile==0 ){
5301
+ sqlite3_free(pNew);
5302
+ pNew = 0;
5303
+ }
5304
+ }
51385305
return pNew;
51395306
}
5307
+
5308
+static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
5309
+ ZipfileCDS *pCds = &pEntry->cds;
5310
+ u8 *a = aBuf;
5311
+
5312
+ pCds->nExtra = 9;
5313
+
5314
+ /* Write the LFH itself */
5315
+ zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
5316
+ zipfileWrite16(a, pCds->iVersionExtract);
5317
+ zipfileWrite16(a, pCds->flags);
5318
+ zipfileWrite16(a, pCds->iCompression);
5319
+ zipfileWrite16(a, pCds->mTime);
5320
+ zipfileWrite16(a, pCds->mDate);
5321
+ zipfileWrite32(a, pCds->crc32);
5322
+ zipfileWrite32(a, pCds->szCompressed);
5323
+ zipfileWrite32(a, pCds->szUncompressed);
5324
+ zipfileWrite16(a, (u16)pCds->nFile);
5325
+ zipfileWrite16(a, pCds->nExtra);
5326
+ assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
5327
+
5328
+ /* Add the file name */
5329
+ memcpy(a, pCds->zFile, (int)pCds->nFile);
5330
+ a += (int)pCds->nFile;
5331
+
5332
+ /* The "extra" data */
5333
+ zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5334
+ zipfileWrite16(a, 5);
5335
+ *a++ = 0x01;
5336
+ zipfileWrite32(a, pEntry->mUnixTime);
5337
+
5338
+ return a-aBuf;
5339
+}
51405340
51415341
static int zipfileAppendEntry(
51425342
ZipfileTab *pTab,
5143
- ZipfileCDS *pCds,
5144
- const char *zPath, /* Path for new entry */
5145
- int nPath, /* strlen(zPath) */
5343
+ ZipfileEntry *pEntry,
51465344
const u8 *pData,
5147
- int nData,
5148
- u32 mTime
5345
+ int nData
51495346
){
51505347
u8 *aBuf = pTab->aBuffer;
5348
+ int nBuf;
51515349
int rc;
51525350
5153
- zipfileWrite32(aBuf, ZIPFILE_SIGNATURE_LFH);
5154
- zipfileWrite16(aBuf, pCds->iVersionExtract);
5155
- zipfileWrite16(aBuf, pCds->flags);
5156
- zipfileWrite16(aBuf, pCds->iCompression);
5157
- zipfileWrite16(aBuf, pCds->mTime);
5158
- zipfileWrite16(aBuf, pCds->mDate);
5159
- zipfileWrite32(aBuf, pCds->crc32);
5160
- zipfileWrite32(aBuf, pCds->szCompressed);
5161
- zipfileWrite32(aBuf, pCds->szUncompressed);
5162
- zipfileWrite16(aBuf, (u16)nPath);
5163
- zipfileWrite16(aBuf, pCds->nExtra);
5164
- assert( aBuf==&pTab->aBuffer[ZIPFILE_LFH_FIXED_SZ] );
5165
- rc = zipfileAppendData(pTab, pTab->aBuffer, (int)(aBuf - pTab->aBuffer));
5166
- if( rc==SQLITE_OK ){
5167
- rc = zipfileAppendData(pTab, (const u8*)zPath, nPath);
5168
- }
5169
-
5170
- if( rc==SQLITE_OK && pCds->nExtra ){
5171
- aBuf = pTab->aBuffer;
5172
- zipfileWrite16(aBuf, ZIPFILE_EXTRA_TIMESTAMP);
5173
- zipfileWrite16(aBuf, 5);
5174
- *aBuf++ = 0x01;
5175
- zipfileWrite32(aBuf, mTime);
5176
- rc = zipfileAppendData(pTab, pTab->aBuffer, 9);
5177
- }
5178
-
5179
- if( rc==SQLITE_OK ){
5351
+ nBuf = zipfileSerializeLFH(pEntry, aBuf);
5352
+ rc = zipfileAppendData(pTab, aBuf, nBuf);
5353
+ if( rc==SQLITE_OK ){
5354
+ pEntry->iDataOff = pTab->szCurrent;
51805355
rc = zipfileAppendData(pTab, pData, nData);
51815356
}
51825357
51835358
return rc;
51845359
}
51855360
51865361
static int zipfileGetMode(
5187
- ZipfileTab *pTab,
51885362
sqlite3_value *pVal,
5189
- u32 defaultMode, /* Value to use if pVal IS NULL */
5190
- u32 *pMode
5363
+ int bIsDir, /* If true, default to directory */
5364
+ u32 *pMode, /* OUT: Mode value */
5365
+ char **pzErr /* OUT: Error message */
51915366
){
51925367
const char *z = (const char*)sqlite3_value_text(pVal);
51935368
u32 mode = 0;
51945369
if( z==0 ){
5195
- mode = defaultMode;
5370
+ mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
51965371
}else if( z[0]>='0' && z[0]<='9' ){
51975372
mode = (unsigned int)sqlite3_value_int(pVal);
51985373
}else{
51995374
const char zTemplate[11] = "-rwxrwxrwx";
52005375
int i;
52015376
if( strlen(z)!=10 ) goto parse_error;
52025377
switch( z[0] ){
52035378
case '-': mode |= S_IFREG; break;
52045379
case 'd': mode |= S_IFDIR; break;
5205
-#if !defined(_WIN32) && !defined(WIN32)
52065380
case 'l': mode |= S_IFLNK; break;
5207
-#endif
52085381
default: goto parse_error;
52095382
}
52105383
for(i=1; i<10; i++){
52115384
if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
52125385
else if( z[i]!='-' ) goto parse_error;
52135386
}
52145387
}
5388
+ if( ((mode & S_IFDIR)==0)==bIsDir ){
5389
+ /* The "mode" attribute is a directory, but data has been specified.
5390
+ ** Or vice-versa - no data but "mode" is a file or symlink. */
5391
+ *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
5392
+ return SQLITE_CONSTRAINT;
5393
+ }
52155394
*pMode = mode;
52165395
return SQLITE_OK;
52175396
52185397
parse_error:
5219
- pTab->base.zErrMsg = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5398
+ *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
52205399
return SQLITE_ERROR;
52215400
}
52225401
52235402
/*
52245403
** Both (const char*) arguments point to nul-terminated strings. Argument
@@ -5229,10 +5408,72 @@
52295408
if( zA[nA-1]=='/' ) nA--;
52305409
if( zB[nB-1]=='/' ) nB--;
52315410
if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
52325411
return 1;
52335412
}
5413
+
5414
+static int zipfileBegin(sqlite3_vtab *pVtab){
5415
+ ZipfileTab *pTab = (ZipfileTab*)pVtab;
5416
+ int rc = SQLITE_OK;
5417
+
5418
+ assert( pTab->pWriteFd==0 );
5419
+
5420
+ /* Open a write fd on the file. Also load the entire central directory
5421
+ ** structure into memory. During the transaction any new file data is
5422
+ ** appended to the archive file, but the central directory is accumulated
5423
+ ** in main-memory until the transaction is committed. */
5424
+ pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5425
+ if( pTab->pWriteFd==0 ){
5426
+ pTab->base.zErrMsg = sqlite3_mprintf(
5427
+ "zipfile: failed to open file %s for writing", pTab->zFile
5428
+ );
5429
+ rc = SQLITE_ERROR;
5430
+ }else{
5431
+ fseek(pTab->pWriteFd, 0, SEEK_END);
5432
+ pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5433
+ rc = zipfileLoadDirectory(pTab, 0, 0);
5434
+ }
5435
+
5436
+ if( rc!=SQLITE_OK ){
5437
+ zipfileCleanupTransaction(pTab);
5438
+ }
5439
+
5440
+ return rc;
5441
+}
5442
+
5443
+/*
5444
+** Return the current time as a 32-bit timestamp in UNIX epoch format (like
5445
+** time(2)).
5446
+*/
5447
+static u32 zipfileTime(void){
5448
+ sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
5449
+ u32 ret;
5450
+ if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
5451
+ i64 ms;
5452
+ pVfs->xCurrentTimeInt64(pVfs, &ms);
5453
+ ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
5454
+ }else{
5455
+ double day;
5456
+ pVfs->xCurrentTime(pVfs, &day);
5457
+ ret = (u32)((day - 2440587.5) * 86400);
5458
+ }
5459
+ return ret;
5460
+}
5461
+
5462
+/*
5463
+** Return a 32-bit timestamp in UNIX epoch format.
5464
+**
5465
+** If the value passed as the only argument is either NULL or an SQL NULL,
5466
+** return the current time. Otherwise, return the value stored in (*pVal)
5467
+** cast to a 32-bit unsigned integer.
5468
+*/
5469
+static u32 zipfileGetTime(sqlite3_value *pVal){
5470
+ if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
5471
+ return zipfileTime();
5472
+ }
5473
+ return (u32)sqlite3_value_int64(pVal);
5474
+}
52345475
52355476
/*
52365477
** xUpdate method.
52375478
*/
52385479
static int zipfileUpdate(
@@ -5244,230 +5485,244 @@
52445485
ZipfileTab *pTab = (ZipfileTab*)pVtab;
52455486
int rc = SQLITE_OK; /* Return Code */
52465487
ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
52475488
52485489
u32 mode = 0; /* Mode for new entry */
5249
- i64 mTime = 0; /* Modification time for new entry */
5490
+ u32 mTime = 0; /* Modification time for new entry */
52505491
i64 sz = 0; /* Uncompressed size */
52515492
const char *zPath = 0; /* Path for new entry */
52525493
int nPath = 0; /* strlen(zPath) */
52535494
const u8 *pData = 0; /* Pointer to buffer containing content */
52545495
int nData = 0; /* Size of pData buffer in bytes */
52555496
int iMethod = 0; /* Compression method for new entry */
52565497
u8 *pFree = 0; /* Free this */
52575498
char *zFree = 0; /* Also free this */
5258
- ZipfileCDS cds; /* New Central Directory Structure entry */
52595499
ZipfileEntry *pOld = 0;
52605500
int bIsDir = 0;
52615501
u32 iCrc32 = 0;
52625502
5263
- assert( pTab->zFile );
5264
- assert( pTab->pWriteFd );
5503
+ if( pTab->pWriteFd==0 ){
5504
+ rc = zipfileBegin(pVtab);
5505
+ if( rc!=SQLITE_OK ) return rc;
5506
+ }
52655507
5508
+ /* If this is a DELETE or UPDATE, find the archive entry to delete. */
52665509
if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
52675510
const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
52685511
int nDelete = (int)strlen(zDelete);
52695512
for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
5270
- if( pOld->bDeleted ) continue;
5271
- if( zipfileComparePath(pOld->zPath, zDelete, nDelete)==0 ){
5272
- pOld->bDeleted = 1;
5513
+ if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
52735514
break;
52745515
}
52755516
assert( pOld->pNext );
52765517
}
5277
- if( nVal==1 ) return SQLITE_OK;
5278
- }
5279
-
5280
- /* Check that "sz" and "rawdata" are both NULL: */
5281
- if( sqlite3_value_type(apVal[5])!=SQLITE_NULL
5282
- || sqlite3_value_type(apVal[6])!=SQLITE_NULL
5283
- ){
5284
- rc = SQLITE_CONSTRAINT;
5285
- }
5286
-
5287
- if( rc==SQLITE_OK ){
5288
- if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5289
- /* data=NULL. A directory */
5290
- bIsDir = 1;
5291
- }else{
5292
- /* Value specified for "data", and possibly "method". This must be
5293
- ** a regular file or a symlink. */
5294
- const u8 *aIn = sqlite3_value_blob(apVal[7]);
5295
- int nIn = sqlite3_value_bytes(apVal[7]);
5296
- int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5297
-
5298
- iMethod = sqlite3_value_int(apVal[8]);
5299
- sz = nIn;
5300
- pData = aIn;
5301
- nData = nIn;
5302
- if( iMethod!=0 && iMethod!=8 ){
5303
- rc = SQLITE_CONSTRAINT;
5304
- }else{
5305
- if( bAuto || iMethod ){
5306
- int nCmp;
5307
- rc = zipfileDeflate(pTab, aIn, nIn, &pFree, &nCmp);
5308
- if( rc==SQLITE_OK ){
5309
- if( iMethod || nCmp<nIn ){
5310
- iMethod = 8;
5311
- pData = pFree;
5312
- nData = nCmp;
5313
- }
5314
- }
5315
- }
5316
- iCrc32 = crc32(0, aIn, nIn);
5317
- }
5318
- }
5319
- }
5320
-
5321
- if( rc==SQLITE_OK ){
5322
- rc = zipfileGetMode(pTab, apVal[3],
5323
- (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644)), &mode
5324
- );
5325
- if( rc==SQLITE_OK && (bIsDir == ((mode & S_IFDIR)==0)) ){
5326
- /* The "mode" attribute is a directory, but data has been specified.
5327
- ** Or vice-versa - no data but "mode" is a file or symlink. */
5328
- rc = SQLITE_CONSTRAINT;
5329
- }
5330
- }
5331
-
5332
- if( rc==SQLITE_OK ){
5333
- zPath = (const char*)sqlite3_value_text(apVal[2]);
5334
- nPath = (int)strlen(zPath);
5335
- if( sqlite3_value_type(apVal[4])==SQLITE_NULL ){
5336
- mTime = (sqlite3_int64)time(0);
5337
- }else{
5338
- mTime = sqlite3_value_int64(apVal[4]);
5339
- }
5340
- }
5341
-
5342
- if( rc==SQLITE_OK && bIsDir ){
5343
- /* For a directory, check that the last character in the path is a
5344
- ** '/'. This appears to be required for compatibility with info-zip
5345
- ** (the unzip command on unix). It does not create directories
5346
- ** otherwise. */
5347
- if( zPath[nPath-1]!='/' ){
5348
- zFree = sqlite3_mprintf("%s/", zPath);
5349
- if( zFree==0 ){ rc = SQLITE_NOMEM; }
5350
- zPath = (const char*)zFree;
5351
- nPath++;
5352
- }
5353
- }
5354
-
5355
- /* Check that we're not inserting a duplicate entry */
5356
- if( rc==SQLITE_OK ){
5357
- ZipfileEntry *p;
5358
- for(p=pTab->pFirstEntry; p; p=p->pNext){
5359
- if( p->bDeleted ) continue;
5360
- if( zipfileComparePath(p->zPath, zPath, nPath)==0 ){
5361
- rc = SQLITE_CONSTRAINT;
5362
- break;
5363
- }
5364
- }
5365
- }
5366
-
5367
- if( rc==SQLITE_OK ){
5368
- /* Create the new CDS record. */
5369
- memset(&cds, 0, sizeof(cds));
5370
- cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5371
- cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5372
- cds.flags = ZIPFILE_NEWENTRY_FLAGS;
5373
- cds.iCompression = (u16)iMethod;
5374
- zipfileMtimeToDos(&cds, (u32)mTime);
5375
- cds.crc32 = iCrc32;
5376
- cds.szCompressed = nData;
5377
- cds.szUncompressed = (u32)sz;
5378
- cds.iExternalAttr = (mode<<16);
5379
- cds.iOffset = (u32)pTab->szCurrent;
5380
- pNew = zipfileNewEntry(&cds, zPath, nPath, (u32)mTime);
5381
- if( pNew==0 ){
5382
- rc = SQLITE_NOMEM;
5383
- }else{
5384
- zipfileAddEntry(pTab, pOld, pNew);
5385
- }
5386
- }
5387
-
5388
- /* Append the new header+file to the archive */
5389
- if( rc==SQLITE_OK ){
5390
- rc = zipfileAppendEntry(pTab, &cds, zPath, nPath, pData, nData, (u32)mTime);
5391
- }
5392
-
5393
- if( rc!=SQLITE_OK && pOld ){
5394
- pOld->bDeleted = 0;
5395
- }
5518
+ }
5519
+
5520
+ if( nVal>1 ){
5521
+ /* Check that "sz" and "rawdata" are both NULL: */
5522
+ if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
5523
+ zipfileTableErr(pTab, "sz must be NULL");
5524
+ rc = SQLITE_CONSTRAINT;
5525
+ }
5526
+ if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
5527
+ zipfileTableErr(pTab, "rawdata must be NULL");
5528
+ rc = SQLITE_CONSTRAINT;
5529
+ }
5530
+
5531
+ if( rc==SQLITE_OK ){
5532
+ if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5533
+ /* data=NULL. A directory */
5534
+ bIsDir = 1;
5535
+ }else{
5536
+ /* Value specified for "data", and possibly "method". This must be
5537
+ ** a regular file or a symlink. */
5538
+ const u8 *aIn = sqlite3_value_blob(apVal[7]);
5539
+ int nIn = sqlite3_value_bytes(apVal[7]);
5540
+ int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5541
+
5542
+ iMethod = sqlite3_value_int(apVal[8]);
5543
+ sz = nIn;
5544
+ pData = aIn;
5545
+ nData = nIn;
5546
+ if( iMethod!=0 && iMethod!=8 ){
5547
+ zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
5548
+ rc = SQLITE_CONSTRAINT;
5549
+ }else{
5550
+ if( bAuto || iMethod ){
5551
+ int nCmp;
5552
+ rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
5553
+ if( rc==SQLITE_OK ){
5554
+ if( iMethod || nCmp<nIn ){
5555
+ iMethod = 8;
5556
+ pData = pFree;
5557
+ nData = nCmp;
5558
+ }
5559
+ }
5560
+ }
5561
+ iCrc32 = crc32(0, aIn, nIn);
5562
+ }
5563
+ }
5564
+ }
5565
+
5566
+ if( rc==SQLITE_OK ){
5567
+ rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
5568
+ }
5569
+
5570
+ if( rc==SQLITE_OK ){
5571
+ zPath = (const char*)sqlite3_value_text(apVal[2]);
5572
+ nPath = (int)strlen(zPath);
5573
+ mTime = zipfileGetTime(apVal[4]);
5574
+ }
5575
+
5576
+ if( rc==SQLITE_OK && bIsDir ){
5577
+ /* For a directory, check that the last character in the path is a
5578
+ ** '/'. This appears to be required for compatibility with info-zip
5579
+ ** (the unzip command on unix). It does not create directories
5580
+ ** otherwise. */
5581
+ if( zPath[nPath-1]!='/' ){
5582
+ zFree = sqlite3_mprintf("%s/", zPath);
5583
+ if( zFree==0 ){ rc = SQLITE_NOMEM; }
5584
+ zPath = (const char*)zFree;
5585
+ nPath++;
5586
+ }
5587
+ }
5588
+
5589
+ /* Check that we're not inserting a duplicate entry */
5590
+ if( pOld==0 && rc==SQLITE_OK ){
5591
+ ZipfileEntry *p;
5592
+ for(p=pTab->pFirstEntry; p; p=p->pNext){
5593
+ if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
5594
+ switch( sqlite3_vtab_on_conflict(pTab->db) ){
5595
+ case SQLITE_IGNORE: {
5596
+ goto zipfile_update_done;
5597
+ }
5598
+ case SQLITE_REPLACE: {
5599
+ pOld = p;
5600
+ break;
5601
+ }
5602
+ default: {
5603
+ zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
5604
+ rc = SQLITE_CONSTRAINT;
5605
+ break;
5606
+ }
5607
+ }
5608
+ break;
5609
+ }
5610
+ }
5611
+ }
5612
+
5613
+ if( rc==SQLITE_OK ){
5614
+ /* Create the new CDS record. */
5615
+ pNew = zipfileNewEntry(zPath);
5616
+ if( pNew==0 ){
5617
+ rc = SQLITE_NOMEM;
5618
+ }else{
5619
+ pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5620
+ pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5621
+ pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
5622
+ pNew->cds.iCompression = (u16)iMethod;
5623
+ zipfileMtimeToDos(&pNew->cds, mTime);
5624
+ pNew->cds.crc32 = iCrc32;
5625
+ pNew->cds.szCompressed = nData;
5626
+ pNew->cds.szUncompressed = (u32)sz;
5627
+ pNew->cds.iExternalAttr = (mode<<16);
5628
+ pNew->cds.iOffset = (u32)pTab->szCurrent;
5629
+ pNew->cds.nFile = (u16)nPath;
5630
+ pNew->mUnixTime = (u32)mTime;
5631
+ rc = zipfileAppendEntry(pTab, pNew, pData, nData);
5632
+ zipfileAddEntry(pTab, pOld, pNew);
5633
+ }
5634
+ }
5635
+ }
5636
+
5637
+ if( rc==SQLITE_OK && pOld ){
5638
+ ZipfileEntry **pp;
5639
+ ZipfileCsr *pCsr;
5640
+ for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
5641
+ if( pCsr->pCurrent==pOld ){
5642
+ pCsr->pCurrent = pOld->pNext;
5643
+ pCsr->bNoop = 1;
5644
+ }
5645
+ }
5646
+ for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
5647
+ *pp = (*pp)->pNext;
5648
+ zipfileEntryFree(pOld);
5649
+ }
5650
+
5651
+zipfile_update_done:
53965652
sqlite3_free(pFree);
53975653
sqlite3_free(zFree);
53985654
return rc;
53995655
}
5656
+
5657
+static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
5658
+ u8 *a = aBuf;
5659
+ zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
5660
+ zipfileWrite16(a, p->iDisk);
5661
+ zipfileWrite16(a, p->iFirstDisk);
5662
+ zipfileWrite16(a, p->nEntry);
5663
+ zipfileWrite16(a, p->nEntryTotal);
5664
+ zipfileWrite32(a, p->nSize);
5665
+ zipfileWrite32(a, p->iOffset);
5666
+ zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/
5667
+
5668
+ return a-aBuf;
5669
+}
54005670
54015671
static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
5402
- u8 *aBuf = pTab->aBuffer;
5403
-
5404
- zipfileWrite32(aBuf, ZIPFILE_SIGNATURE_EOCD);
5405
- zipfileWrite16(aBuf, p->iDisk);
5406
- zipfileWrite16(aBuf, p->iFirstDisk);
5407
- zipfileWrite16(aBuf, p->nEntry);
5408
- zipfileWrite16(aBuf, p->nEntryTotal);
5409
- zipfileWrite32(aBuf, p->nSize);
5410
- zipfileWrite32(aBuf, p->iOffset);
5411
- zipfileWrite16(aBuf, 0); /* Size of trailing comment in bytes*/
5412
-
5413
- assert( (aBuf-pTab->aBuffer)==22 );
5414
- return zipfileAppendData(pTab, pTab->aBuffer, (int)(aBuf - pTab->aBuffer));
5415
-}
5416
-
5417
-static void zipfileCleanupTransaction(ZipfileTab *pTab){
5418
- ZipfileEntry *pEntry;
5419
- ZipfileEntry *pNext;
5420
-
5421
- for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
5422
- pNext = pEntry->pNext;
5423
- sqlite3_free(pEntry);
5424
- }
5425
- pTab->pFirstEntry = 0;
5426
- pTab->pLastEntry = 0;
5427
- fclose(pTab->pWriteFd);
5428
- pTab->pWriteFd = 0;
5429
- pTab->szCurrent = 0;
5430
- pTab->szOrig = 0;
5431
-}
5432
-
5433
-static int zipfileBegin(sqlite3_vtab *pVtab){
5434
- ZipfileTab *pTab = (ZipfileTab*)pVtab;
5435
- int rc = SQLITE_OK;
5436
-
5437
- assert( pTab->pWriteFd==0 );
5438
-
5439
- /* This table is only writable if a default archive path was specified
5440
- ** as part of the CREATE VIRTUAL TABLE statement. */
5441
- if( pTab->zFile==0 ){
5442
- pTab->base.zErrMsg = sqlite3_mprintf(
5443
- "zipfile: writing requires a default archive"
5444
- );
5445
- return SQLITE_ERROR;
5446
- }
5447
-
5448
- /* Open a write fd on the file. Also load the entire central directory
5449
- ** structure into memory. During the transaction any new file data is
5450
- ** appended to the archive file, but the central directory is accumulated
5451
- ** in main-memory until the transaction is committed. */
5452
- pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5453
- if( pTab->pWriteFd==0 ){
5454
- pTab->base.zErrMsg = sqlite3_mprintf(
5455
- "zipfile: failed to open file %s for writing", pTab->zFile
5456
- );
5457
- rc = SQLITE_ERROR;
5672
+ int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
5673
+ assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
5674
+ return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
5675
+}
5676
+
5677
+/*
5678
+** Serialize the CDS structure into buffer aBuf[]. Return the number
5679
+** of bytes written.
5680
+*/
5681
+static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
5682
+ u8 *a = aBuf;
5683
+ ZipfileCDS *pCDS = &pEntry->cds;
5684
+
5685
+ if( pEntry->aExtra==0 ){
5686
+ pCDS->nExtra = 9;
5687
+ }
5688
+
5689
+ zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
5690
+ zipfileWrite16(a, pCDS->iVersionMadeBy);
5691
+ zipfileWrite16(a, pCDS->iVersionExtract);
5692
+ zipfileWrite16(a, pCDS->flags);
5693
+ zipfileWrite16(a, pCDS->iCompression);
5694
+ zipfileWrite16(a, pCDS->mTime);
5695
+ zipfileWrite16(a, pCDS->mDate);
5696
+ zipfileWrite32(a, pCDS->crc32);
5697
+ zipfileWrite32(a, pCDS->szCompressed);
5698
+ zipfileWrite32(a, pCDS->szUncompressed);
5699
+ assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
5700
+ zipfileWrite16(a, pCDS->nFile);
5701
+ zipfileWrite16(a, pCDS->nExtra);
5702
+ zipfileWrite16(a, pCDS->nComment);
5703
+ zipfileWrite16(a, pCDS->iDiskStart);
5704
+ zipfileWrite16(a, pCDS->iInternalAttr);
5705
+ zipfileWrite32(a, pCDS->iExternalAttr);
5706
+ zipfileWrite32(a, pCDS->iOffset);
5707
+
5708
+ memcpy(a, pCDS->zFile, pCDS->nFile);
5709
+ a += pCDS->nFile;
5710
+
5711
+ if( pEntry->aExtra ){
5712
+ int n = (int)pCDS->nExtra + (int)pCDS->nComment;
5713
+ memcpy(a, pEntry->aExtra, n);
5714
+ a += n;
54585715
}else{
5459
- fseek(pTab->pWriteFd, 0, SEEK_END);
5460
- pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5461
- rc = zipfileLoadDirectory(pTab);
5716
+ assert( pCDS->nExtra==9 );
5717
+ zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5718
+ zipfileWrite16(a, 5);
5719
+ *a++ = 0x01;
5720
+ zipfileWrite32(a, pEntry->mUnixTime);
54625721
}
54635722
5464
- if( rc!=SQLITE_OK ){
5465
- zipfileCleanupTransaction(pTab);
5466
- }
5467
-
5468
- return rc;
5723
+ return a-aBuf;
54695724
}
54705725
54715726
static int zipfileCommit(sqlite3_vtab *pVtab){
54725727
ZipfileTab *pTab = (ZipfileTab*)pVtab;
54735728
int rc = SQLITE_OK;
@@ -5475,14 +5730,14 @@
54755730
i64 iOffset = pTab->szCurrent;
54765731
ZipfileEntry *p;
54775732
ZipfileEOCD eocd;
54785733
int nEntry = 0;
54795734
5480
- /* Write out all undeleted entries */
5735
+ /* Write out all entries */
54815736
for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
5482
- if( p->bDeleted ) continue;
5483
- rc = zipfileAppendData(pTab, p->aCdsEntry, p->nCdsEntry);
5737
+ int n = zipfileSerializeCDS(p, pTab->aBuffer);
5738
+ rc = zipfileAppendData(pTab, pTab->aBuffer, n);
54845739
nEntry++;
54855740
}
54865741
54875742
/* Write out the EOCD record */
54885743
eocd.iDisk = 0;
@@ -5519,11 +5774,11 @@
55195774
ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
55205775
assert( argc>0 );
55215776
55225777
pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
55235778
if( pCsr ){
5524
- ZipfileCDS *p = &pCsr->cds;
5779
+ ZipfileCDS *p = &pCsr->pCurrent->cds;
55255780
char *zRes = sqlite3_mprintf("{"
55265781
"\"version-made-by\" : %u, "
55275782
"\"version-to-extract\" : %u, "
55285783
"\"flags\" : %u, "
55295784
"\"compression\" : %u, "
@@ -5556,11 +5811,10 @@
55565811
sqlite3_free(zRes);
55575812
}
55585813
}
55595814
}
55605815
5561
-
55625816
/*
55635817
** xFindFunction method.
55645818
*/
55655819
static int zipfileFindFunction(
55665820
sqlite3_vtab *pVtab, /* Virtual table handle */
@@ -5567,20 +5821,259 @@
55675821
int nArg, /* Number of SQL function arguments */
55685822
const char *zName, /* Name of SQL function */
55695823
void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
55705824
void **ppArg /* OUT: User data for *pxFunc */
55715825
){
5572
- if( nArg>0 ){
5573
- if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
5574
- *pxFunc = zipfileFunctionCds;
5575
- *ppArg = (void*)pVtab;
5576
- return 1;
5577
- }
5578
- }
5579
-
5826
+ if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
5827
+ *pxFunc = zipfileFunctionCds;
5828
+ *ppArg = (void*)pVtab;
5829
+ return 1;
5830
+ }
55805831
return 0;
55815832
}
5833
+
5834
+typedef struct ZipfileBuffer ZipfileBuffer;
5835
+struct ZipfileBuffer {
5836
+ u8 *a; /* Pointer to buffer */
5837
+ int n; /* Size of buffer in bytes */
5838
+ int nAlloc; /* Byte allocated at a[] */
5839
+};
5840
+
5841
+typedef struct ZipfileCtx ZipfileCtx;
5842
+struct ZipfileCtx {
5843
+ int nEntry;
5844
+ ZipfileBuffer body;
5845
+ ZipfileBuffer cds;
5846
+};
5847
+
5848
+static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
5849
+ if( pBuf->n+nByte>pBuf->nAlloc ){
5850
+ u8 *aNew;
5851
+ int nNew = pBuf->n ? pBuf->n*2 : 512;
5852
+ int nReq = pBuf->n + nByte;
5853
+
5854
+ while( nNew<nReq ) nNew = nNew*2;
5855
+ aNew = sqlite3_realloc(pBuf->a, nNew);
5856
+ if( aNew==0 ) return SQLITE_NOMEM;
5857
+ pBuf->a = aNew;
5858
+ pBuf->nAlloc = nNew;
5859
+ }
5860
+ return SQLITE_OK;
5861
+}
5862
+
5863
+/*
5864
+** xStep() callback for the zipfile() aggregate. This can be called in
5865
+** any of the following ways:
5866
+**
5867
+** SELECT zipfile(name,data) ...
5868
+** SELECT zipfile(name,mode,mtime,data) ...
5869
+** SELECT zipfile(name,mode,mtime,data,method) ...
5870
+*/
5871
+void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
5872
+ ZipfileCtx *p; /* Aggregate function context */
5873
+ ZipfileEntry e; /* New entry to add to zip archive */
5874
+
5875
+ sqlite3_value *pName = 0;
5876
+ sqlite3_value *pMode = 0;
5877
+ sqlite3_value *pMtime = 0;
5878
+ sqlite3_value *pData = 0;
5879
+ sqlite3_value *pMethod = 0;
5880
+
5881
+ int bIsDir = 0;
5882
+ u32 mode;
5883
+ int rc = SQLITE_OK;
5884
+ char *zErr = 0;
5885
+
5886
+ int iMethod = -1; /* Compression method to use (0 or 8) */
5887
+
5888
+ const u8 *aData = 0; /* Possibly compressed data for new entry */
5889
+ int nData = 0; /* Size of aData[] in bytes */
5890
+ int szUncompressed = 0; /* Size of data before compression */
5891
+ u8 *aFree = 0; /* Free this before returning */
5892
+ u32 iCrc32 = 0; /* crc32 of uncompressed data */
5893
+
5894
+ char *zName = 0; /* Path (name) of new entry */
5895
+ int nName = 0; /* Size of zName in bytes */
5896
+ char *zFree = 0; /* Free this before returning */
5897
+ int nByte;
5898
+
5899
+ memset(&e, 0, sizeof(e));
5900
+ p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
5901
+ if( p==0 ) return;
5902
+
5903
+ /* Martial the arguments into stack variables */
5904
+ if( nVal!=2 && nVal!=4 && nVal!=5 ){
5905
+ zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
5906
+ rc = SQLITE_ERROR;
5907
+ goto zipfile_step_out;
5908
+ }
5909
+ pName = apVal[0];
5910
+ if( nVal==2 ){
5911
+ pData = apVal[1];
5912
+ }else{
5913
+ pMode = apVal[1];
5914
+ pMtime = apVal[2];
5915
+ pData = apVal[3];
5916
+ if( nVal==5 ){
5917
+ pMethod = apVal[4];
5918
+ }
5919
+ }
5920
+
5921
+ /* Check that the 'name' parameter looks ok. */
5922
+ zName = (char*)sqlite3_value_text(pName);
5923
+ nName = sqlite3_value_bytes(pName);
5924
+ if( zName==0 ){
5925
+ zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
5926
+ rc = SQLITE_ERROR;
5927
+ goto zipfile_step_out;
5928
+ }
5929
+
5930
+ /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
5931
+ ** deflate compression) or NULL (choose automatically). */
5932
+ if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
5933
+ iMethod = (int)sqlite3_value_int64(pMethod);
5934
+ if( iMethod!=0 && iMethod!=8 ){
5935
+ zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
5936
+ rc = SQLITE_ERROR;
5937
+ goto zipfile_step_out;
5938
+ }
5939
+ }
5940
+
5941
+ /* Now inspect the data. If this is NULL, then the new entry must be a
5942
+ ** directory. Otherwise, figure out whether or not the data should
5943
+ ** be deflated or simply stored in the zip archive. */
5944
+ if( sqlite3_value_type(pData)==SQLITE_NULL ){
5945
+ bIsDir = 1;
5946
+ iMethod = 0;
5947
+ }else{
5948
+ aData = sqlite3_value_blob(pData);
5949
+ szUncompressed = nData = sqlite3_value_bytes(pData);
5950
+ iCrc32 = crc32(0, aData, nData);
5951
+ if( iMethod<0 || iMethod==8 ){
5952
+ int nOut = 0;
5953
+ rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
5954
+ if( rc!=SQLITE_OK ){
5955
+ goto zipfile_step_out;
5956
+ }
5957
+ if( iMethod==8 || nOut<nData ){
5958
+ aData = aFree;
5959
+ nData = nOut;
5960
+ iMethod = 8;
5961
+ }else{
5962
+ iMethod = 0;
5963
+ }
5964
+ }
5965
+ }
5966
+
5967
+ /* Decode the "mode" argument. */
5968
+ rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
5969
+ if( rc ) goto zipfile_step_out;
5970
+
5971
+ /* Decode the "mtime" argument. */
5972
+ e.mUnixTime = zipfileGetTime(pMtime);
5973
+
5974
+ /* If this is a directory entry, ensure that there is exactly one '/'
5975
+ ** at the end of the path. Or, if this is not a directory and the path
5976
+ ** ends in '/' it is an error. */
5977
+ if( bIsDir==0 ){
5978
+ if( zName[nName-1]=='/' ){
5979
+ zErr = sqlite3_mprintf("non-directory name must not end with /");
5980
+ rc = SQLITE_ERROR;
5981
+ goto zipfile_step_out;
5982
+ }
5983
+ }else{
5984
+ if( zName[nName-1]!='/' ){
5985
+ zName = zFree = sqlite3_mprintf("%s/", zName);
5986
+ nName++;
5987
+ if( zName==0 ){
5988
+ rc = SQLITE_NOMEM;
5989
+ goto zipfile_step_out;
5990
+ }
5991
+ }else{
5992
+ while( nName>1 && zName[nName-2]=='/' ) nName--;
5993
+ }
5994
+ }
5995
+
5996
+ /* Assemble the ZipfileEntry object for the new zip archive entry */
5997
+ e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5998
+ e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5999
+ e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
6000
+ e.cds.iCompression = (u16)iMethod;
6001
+ zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
6002
+ e.cds.crc32 = iCrc32;
6003
+ e.cds.szCompressed = nData;
6004
+ e.cds.szUncompressed = szUncompressed;
6005
+ e.cds.iExternalAttr = (mode<<16);
6006
+ e.cds.iOffset = p->body.n;
6007
+ e.cds.nFile = (u16)nName;
6008
+ e.cds.zFile = zName;
6009
+
6010
+ /* Append the LFH to the body of the new archive */
6011
+ nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
6012
+ if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
6013
+ p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
6014
+
6015
+ /* Append the data to the body of the new archive */
6016
+ if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6017
+ memcpy(&p->body.a[p->body.n], aData, nData);
6018
+ p->body.n += nData;
6019
+
6020
+ /* Append the CDS record to the directory of the new archive */
6021
+ nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
6022
+ if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
6023
+ p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
6024
+
6025
+ /* Increment the count of entries in the archive */
6026
+ p->nEntry++;
6027
+
6028
+ zipfile_step_out:
6029
+ sqlite3_free(aFree);
6030
+ sqlite3_free(zFree);
6031
+ if( rc ){
6032
+ if( zErr ){
6033
+ sqlite3_result_error(pCtx, zErr, -1);
6034
+ }else{
6035
+ sqlite3_result_error_code(pCtx, rc);
6036
+ }
6037
+ }
6038
+ sqlite3_free(zErr);
6039
+}
6040
+
6041
+/*
6042
+** xFinalize() callback for zipfile aggregate function.
6043
+*/
6044
+void zipfileFinal(sqlite3_context *pCtx){
6045
+ ZipfileCtx *p;
6046
+ ZipfileEOCD eocd;
6047
+ int nZip;
6048
+ u8 *aZip;
6049
+
6050
+ p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6051
+ if( p==0 ) return;
6052
+ if( p->nEntry>0 ){
6053
+ memset(&eocd, 0, sizeof(eocd));
6054
+ eocd.nEntry = (u16)p->nEntry;
6055
+ eocd.nEntryTotal = (u16)p->nEntry;
6056
+ eocd.nSize = p->cds.n;
6057
+ eocd.iOffset = p->body.n;
6058
+
6059
+ nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
6060
+ aZip = (u8*)sqlite3_malloc(nZip);
6061
+ if( aZip==0 ){
6062
+ sqlite3_result_error_nomem(pCtx);
6063
+ }else{
6064
+ memcpy(aZip, p->body.a, p->body.n);
6065
+ memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
6066
+ zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
6067
+ sqlite3_result_blob(pCtx, aZip, nZip, zipfileFree);
6068
+ }
6069
+ }
6070
+
6071
+ sqlite3_free(p->body.a);
6072
+ sqlite3_free(p->cds.a);
6073
+}
6074
+
55826075
55836076
/*
55846077
** Register the "zipfile" virtual table.
55856078
*/
55866079
static int zipfileRegister(sqlite3 *db){
@@ -5595,11 +6088,11 @@
55956088
zipfileClose, /* xClose - close a cursor */
55966089
zipfileFilter, /* xFilter - configure scan constraints */
55976090
zipfileNext, /* xNext - advance a cursor */
55986091
zipfileEof, /* xEof - check for end of scan */
55996092
zipfileColumn, /* xColumn - read data */
5600
- zipfileRowid, /* xRowid - read data */
6093
+ 0, /* xRowid - read data */
56016094
zipfileUpdate, /* xUpdate */
56026095
zipfileBegin, /* xBegin */
56036096
0, /* xSync */
56046097
zipfileCommit, /* xCommit */
56056098
zipfileRollback, /* xRollback */
@@ -5606,12 +6099,15 @@
56066099
zipfileFindFunction, /* xFindMethod */
56076100
0, /* xRename */
56086101
};
56096102
56106103
int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
6104
+ if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
56116105
if( rc==SQLITE_OK ){
5612
- rc = sqlite3_overload_function(db, "zipfile_cds", -1);
6106
+ rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
6107
+ zipfileStep, zipfileFinal
6108
+ );
56136109
}
56146110
return rc;
56156111
}
56166112
#else /* SQLITE_OMIT_VIRTUALTABLE */
56176113
# define zipfileRegister(x) SQLITE_OK
@@ -7976,10 +8472,11 @@
79768472
*/
79778473
#define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
79788474
#define SHELL_OPEN_NORMAL 1 /* Normal database file */
79798475
#define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
79808476
#define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
8477
+#define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
79818478
79828479
/*
79838480
** These are the allowed shellFlgs values
79848481
*/
79858482
#define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
@@ -8082,10 +8579,11 @@
80828579
** If the EDITOR argument is omitted, use the value in the VISUAL
80838580
** environment variable. If still there is no EDITOR, through an error.
80848581
**
80858582
** Also throw an error if the EDITOR program returns a non-zero exit code.
80868583
*/
8584
+#ifndef SQLITE_NOHAVE_SYSTEM
80878585
static void editFunc(
80888586
sqlite3_context *context,
80898587
int argc,
80908588
sqlite3_value **argv
80918589
){
@@ -8179,22 +8677,24 @@
81798677
if( x!=sz ){
81808678
sqlite3_result_error(context, "could not read back the whole file", -1);
81818679
goto edit_func_end;
81828680
}
81838681
if( bBin ){
8184
- sqlite3_result_blob(context, p, sz, sqlite3_free);
8682
+ sqlite3_result_blob64(context, p, sz, sqlite3_free);
81858683
}else{
8186
- sqlite3_result_text(context, (const char*)p, sz, sqlite3_free);
8684
+ sqlite3_result_text64(context, (const char*)p, sz,
8685
+ sqlite3_free, SQLITE_UTF8);
81878686
}
81888687
p = 0;
81898688
81908689
edit_func_end:
81918690
if( f ) fclose(f);
81928691
unlink(zTempFile);
81938692
sqlite3_free(zTempFile);
81948693
sqlite3_free(p);
81958694
}
8695
+#endif /* SQLITE_NOHAVE_SYSTEM */
81968696
81978697
/*
81988698
** Save or restore the current output mode
81998699
*/
82008700
static void outputModePush(ShellState *p){
@@ -9176,33 +9676,58 @@
91769676
ShellState *pArg, /* Pointer to ShellState */
91779677
int bReset /* True to reset the stats */
91789678
){
91799679
int iCur;
91809680
int iHiwtr;
9181
-
9182
- if( pArg && pArg->out ){
9183
- displayStatLine(pArg, "Memory Used:",
9184
- "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
9185
- displayStatLine(pArg, "Number of Outstanding Allocations:",
9186
- "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
9187
- if( pArg->shellFlgs & SHFLG_Pagecache ){
9188
- displayStatLine(pArg, "Number of Pcache Pages Used:",
9189
- "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
9190
- }
9191
- displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
9192
- "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
9193
- displayStatLine(pArg, "Largest Allocation:",
9194
- "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
9195
- displayStatLine(pArg, "Largest Pcache Allocation:",
9196
- "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
9681
+ FILE *out;
9682
+ if( pArg==0 || pArg->out==0 ) return 0;
9683
+ out = pArg->out;
9684
+
9685
+ if( pArg->pStmt && (pArg->statsOn & 2) ){
9686
+ int nCol, i, x;
9687
+ sqlite3_stmt *pStmt = pArg->pStmt;
9688
+ char z[100];
9689
+ nCol = sqlite3_column_count(pStmt);
9690
+ raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
9691
+ for(i=0; i<nCol; i++){
9692
+ sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
9693
+ utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
9694
+#ifndef SQLITE_OMIT_DECLTYPE
9695
+ sqlite3_snprintf(30, z+x, "declared type:");
9696
+ utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
9697
+#endif
9698
+#ifdef SQLITE_ENABLE_COLUMN_METADATA
9699
+ sqlite3_snprintf(30, z+x, "database name:");
9700
+ utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
9701
+ sqlite3_snprintf(30, z+x, "table name:");
9702
+ utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
9703
+ sqlite3_snprintf(30, z+x, "origin name:");
9704
+ utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
9705
+#endif
9706
+ }
9707
+ }
9708
+
9709
+ displayStatLine(pArg, "Memory Used:",
9710
+ "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
9711
+ displayStatLine(pArg, "Number of Outstanding Allocations:",
9712
+ "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
9713
+ if( pArg->shellFlgs & SHFLG_Pagecache ){
9714
+ displayStatLine(pArg, "Number of Pcache Pages Used:",
9715
+ "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
9716
+ }
9717
+ displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
9718
+ "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
9719
+ displayStatLine(pArg, "Largest Allocation:",
9720
+ "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
9721
+ displayStatLine(pArg, "Largest Pcache Allocation:",
9722
+ "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
91979723
#ifdef YYTRACKMAXSTACKDEPTH
9198
- displayStatLine(pArg, "Deepest Parser Stack:",
9199
- "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
9724
+ displayStatLine(pArg, "Deepest Parser Stack:",
9725
+ "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
92009726
#endif
9201
- }
92029727
9203
- if( pArg && pArg->out && db ){
9728
+ if( db ){
92049729
if( pArg->shellFlgs & SHFLG_Lookaside ){
92059730
iHiwtr = iCur = -1;
92069731
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
92079732
&iCur, &iHiwtr, bReset);
92089733
raw_printf(pArg->out,
@@ -9233,29 +9758,38 @@
92339758
raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
92349759
iHiwtr = iCur = -1;
92359760
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
92369761
raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
92379762
iHiwtr = iCur = -1;
9763
+ sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
9764
+ raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
9765
+ iHiwtr = iCur = -1;
92389766
sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
92399767
raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
92409768
iCur);
92419769
iHiwtr = iCur = -1;
92429770
sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
92439771
raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
92449772
iCur);
92459773
}
92469774
9247
- if( pArg && pArg->out && db && pArg->pStmt ){
9775
+ if( pArg->pStmt ){
92489776
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
92499777
bReset);
92509778
raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
92519779
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
92529780
raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
92539781
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
92549782
raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
92559783
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
92569784
raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
9785
+ iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE, bReset);
9786
+ raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
9787
+ iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
9788
+ raw_printf(pArg->out, "Number of times run: %d\n", iCur);
9789
+ iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
9790
+ raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
92579791
}
92589792
92599793
#ifdef __linux__
92609794
displayLinuxIoStats(pArg->out);
92619795
#endif
@@ -9467,69 +10001,61 @@
946710001
/*
946810002
** Run a prepared statement
946910003
*/
947010004
static void exec_prepared_stmt(
947110005
ShellState *pArg, /* Pointer to ShellState */
9472
- sqlite3_stmt *pStmt, /* Statment to run */
9473
- int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */
10006
+ sqlite3_stmt *pStmt /* Statment to run */
947410007
){
947510008
int rc;
947610009
947710010
/* perform the first step. this will tell us if we
947810011
** have a result set or not and how wide it is.
947910012
*/
948010013
rc = sqlite3_step(pStmt);
948110014
/* if we have a result set... */
948210015
if( SQLITE_ROW == rc ){
9483
- /* if we have a callback... */
9484
- if( xCallback ){
9485
- /* allocate space for col name ptr, value ptr, and type */
9486
- int nCol = sqlite3_column_count(pStmt);
9487
- void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
9488
- if( !pData ){
9489
- rc = SQLITE_NOMEM;
9490
- }else{
9491
- char **azCols = (char **)pData; /* Names of result columns */
9492
- char **azVals = &azCols[nCol]; /* Results */
9493
- int *aiTypes = (int *)&azVals[nCol]; /* Result types */
9494
- int i, x;
9495
- assert(sizeof(int) <= sizeof(char *));
9496
- /* save off ptrs to column names */
9497
- for(i=0; i<nCol; i++){
9498
- azCols[i] = (char *)sqlite3_column_name(pStmt, i);
9499
- }
9500
- do{
9501
- /* extract the data and data types */
9502
- for(i=0; i<nCol; i++){
9503
- aiTypes[i] = x = sqlite3_column_type(pStmt, i);
9504
- if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
9505
- azVals[i] = "";
9506
- }else{
9507
- azVals[i] = (char*)sqlite3_column_text(pStmt, i);
9508
- }
9509
- if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
9510
- rc = SQLITE_NOMEM;
9511
- break; /* from for */
9512
- }
9513
- } /* end for */
9514
-
9515
- /* if data and types extracted successfully... */
9516
- if( SQLITE_ROW == rc ){
9517
- /* call the supplied callback with the result row data */
9518
- if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
9519
- rc = SQLITE_ABORT;
9520
- }else{
9521
- rc = sqlite3_step(pStmt);
9522
- }
9523
- }
9524
- } while( SQLITE_ROW == rc );
9525
- sqlite3_free(pData);
9526
- }
9527
- }else{
9528
- do{
9529
- rc = sqlite3_step(pStmt);
9530
- } while( rc == SQLITE_ROW );
10016
+ /* allocate space for col name ptr, value ptr, and type */
10017
+ int nCol = sqlite3_column_count(pStmt);
10018
+ void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
10019
+ if( !pData ){
10020
+ rc = SQLITE_NOMEM;
10021
+ }else{
10022
+ char **azCols = (char **)pData; /* Names of result columns */
10023
+ char **azVals = &azCols[nCol]; /* Results */
10024
+ int *aiTypes = (int *)&azVals[nCol]; /* Result types */
10025
+ int i, x;
10026
+ assert(sizeof(int) <= sizeof(char *));
10027
+ /* save off ptrs to column names */
10028
+ for(i=0; i<nCol; i++){
10029
+ azCols[i] = (char *)sqlite3_column_name(pStmt, i);
10030
+ }
10031
+ do{
10032
+ /* extract the data and data types */
10033
+ for(i=0; i<nCol; i++){
10034
+ aiTypes[i] = x = sqlite3_column_type(pStmt, i);
10035
+ if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
10036
+ azVals[i] = "";
10037
+ }else{
10038
+ azVals[i] = (char*)sqlite3_column_text(pStmt, i);
10039
+ }
10040
+ if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
10041
+ rc = SQLITE_NOMEM;
10042
+ break; /* from for */
10043
+ }
10044
+ } /* end for */
10045
+
10046
+ /* if data and types extracted successfully... */
10047
+ if( SQLITE_ROW == rc ){
10048
+ /* call the supplied callback with the result row data */
10049
+ if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
10050
+ rc = SQLITE_ABORT;
10051
+ }else{
10052
+ rc = sqlite3_step(pStmt);
10053
+ }
10054
+ }
10055
+ } while( SQLITE_ROW == rc );
10056
+ sqlite3_free(pData);
953110057
}
953210058
}
953310059
}
953410060
953510061
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -9671,21 +10197,19 @@
967110197
** This is very similar to SQLite's built-in sqlite3_exec()
967210198
** function except it takes a slightly different callback
967310199
** and callback data argument.
967410200
*/
967510201
static int shell_exec(
9676
- sqlite3 *db, /* An open database */
9677
- const char *zSql, /* SQL to be evaluated */
9678
- int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
9679
- /* (not the same as sqlite3_exec) */
968010202
ShellState *pArg, /* Pointer to ShellState */
10203
+ const char *zSql, /* SQL to be evaluated */
968110204
char **pzErrMsg /* Error msg written here */
968210205
){
968310206
sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
968410207
int rc = SQLITE_OK; /* Return Code */
968510208
int rc2;
968610209
const char *zLeftover; /* Tail of unprocessed SQL */
10210
+ sqlite3 *db = pArg->db;
968710211
968810212
if( pzErrMsg ){
968910213
*pzErrMsg = NULL;
969010214
}
969110215
@@ -9752,11 +10276,11 @@
975210276
zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
975310277
rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
975410278
if( rc==SQLITE_OK ){
975510279
pArg->cMode = MODE_Explain;
975610280
explain_data_prepare(pArg, pExplain);
9757
- exec_prepared_stmt(pArg, pExplain, xCallback);
10281
+ exec_prepared_stmt(pArg, pExplain);
975810282
explain_data_delete(pArg);
975910283
}
976010284
sqlite3_finalize(pExplain);
976110285
sqlite3_free(zEQP);
976210286
}
@@ -9778,11 +10302,11 @@
977810302
if( pArg->cMode==MODE_Explain ){
977910303
explain_data_prepare(pArg, pStmt);
978010304
}
978110305
}
978210306
9783
- exec_prepared_stmt(pArg, pStmt, xCallback);
10307
+ exec_prepared_stmt(pArg, pStmt);
978410308
explain_data_delete(pArg);
978510309
978610310
/* print usage stats if stats on */
978710311
if( pArg && pArg->statsOn ){
978810312
display_stats(db, pArg, 0);
@@ -10041,15 +10565,15 @@
1004110565
1004210566
savedDestTable = p->zDestTable;
1004310567
savedMode = p->mode;
1004410568
p->zDestTable = sTable.z;
1004510569
p->mode = p->cMode = MODE_Insert;
10046
- rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0);
10570
+ rc = shell_exec(p, sSelect.z, 0);
1004710571
if( (rc&0xff)==SQLITE_CORRUPT ){
1004810572
raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
1004910573
toggleSelectOrder(p->db);
10050
- shell_exec(p->db, sSelect.z, shell_callback, p, 0);
10574
+ shell_exec(p, sSelect.z, 0);
1005110575
toggleSelectOrder(p->db);
1005210576
}
1005310577
p->zDestTable = savedDestTable;
1005410578
p->mode = savedMode;
1005510579
freeText(&sTable);
@@ -10162,10 +10686,11 @@
1016210686
".once (-e|-x|FILE) Output for the next SQL command only to FILE\n"
1016310687
" or invoke system text editor (-e) or spreadsheet (-x)\n"
1016410688
" on the output.\n"
1016510689
".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
1016610690
" The --new option starts with an empty file\n"
10691
+ " Other options: --readonly --append --zip\n"
1016710692
".output ?FILE? Send output to FILE or stdout\n"
1016810693
".print STRING... Print literal STRING\n"
1016910694
".prompt MAIN CONTINUE Replace the standard prompts\n"
1017010695
".quit Exit this program\n"
1017110696
".read FILENAME Execute SQL in FILENAME\n"
@@ -10179,14 +10704,18 @@
1017910704
" separator for both the output mode and .import\n"
1018010705
#if defined(SQLITE_ENABLE_SESSION)
1018110706
".session CMD ... Create or control sessions\n"
1018210707
#endif
1018310708
".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n"
10709
+#ifndef SQLITE_NOHAVE_SYSTEM
1018410710
".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
10711
+#endif
1018510712
".show Show the current values for various settings\n"
1018610713
".stats ?on|off? Show stats or turn stats on or off\n"
10714
+#ifndef SQLITE_NOHAVE_SYSTEM
1018710715
".system CMD ARGS... Run CMD ARGS... in a system shell\n"
10716
+#endif
1018810717
".tables ?TABLE? List names of tables\n"
1018910718
" If TABLE specified, only list tables matching\n"
1019010719
" LIKE pattern TABLE.\n"
1019110720
".testcase NAME Begin redirecting output to 'testcase-out.txt'\n"
1019210721
".timeout MS Try opening locked tables for MS milliseconds\n"
@@ -10311,17 +10840,25 @@
1031110840
#endif
1031210841
1031310842
/*
1031410843
** Try to deduce the type of file for zName based on its content. Return
1031510844
** one of the SHELL_OPEN_* constants.
10845
+**
10846
+** If the file does not exist or is empty but its name looks like a ZIP
10847
+** archive and the dfltZip flag is true, then assume it is a ZIP archive.
10848
+** Otherwise, assume an ordinary database regardless of the filename if
10849
+** the type cannot be determined from content.
1031610850
*/
10317
-static int deduceDatabaseType(const char *zName){
10851
+static int deduceDatabaseType(const char *zName, int dfltZip){
1031810852
FILE *f = fopen(zName, "rb");
1031910853
size_t n;
1032010854
int rc = SHELL_OPEN_UNSPEC;
1032110855
char zBuf[100];
10322
- if( f==0 ) return SHELL_OPEN_NORMAL;
10856
+ if( f==0 ){
10857
+ if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ) return SHELL_OPEN_ZIPFILE;
10858
+ return SHELL_OPEN_NORMAL;
10859
+ }
1032310860
fseek(f, -25, SEEK_END);
1032410861
n = fread(zBuf, 25, 1, f);
1032510862
if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
1032610863
rc = SHELL_OPEN_APPENDVFS;
1032710864
}else{
@@ -10328,10 +10865,12 @@
1032810865
fseek(f, -22, SEEK_END);
1032910866
n = fread(zBuf, 22, 1, f);
1033010867
if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
1033110868
&& zBuf[3]==0x06 ){
1033210869
rc = SHELL_OPEN_ZIPFILE;
10870
+ }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
10871
+ return SHELL_OPEN_ZIPFILE;
1033310872
}
1033410873
}
1033510874
fclose(f);
1033610875
return rc;
1033710876
}
@@ -10342,11 +10881,11 @@
1034210881
*/
1034310882
static void open_db(ShellState *p, int keepAlive){
1034410883
if( p->db==0 ){
1034510884
sqlite3_initialize();
1034610885
if( p->openMode==SHELL_OPEN_UNSPEC && access(p->zDbFilename,0)==0 ){
10347
- p->openMode = deduceDatabaseType(p->zDbFilename);
10886
+ p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 0);
1034810887
}
1034910888
switch( p->openMode ){
1035010889
case SHELL_OPEN_APPENDVFS: {
1035110890
sqlite3_open_v2(p->zDbFilename, &p->db,
1035210891
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
@@ -10353,10 +10892,14 @@
1035310892
break;
1035410893
}
1035510894
case SHELL_OPEN_ZIPFILE: {
1035610895
sqlite3_open(":memory:", &p->db);
1035710896
break;
10897
+ }
10898
+ case SHELL_OPEN_READONLY: {
10899
+ sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
10900
+ break;
1035810901
}
1035910902
case SHELL_OPEN_UNSPEC:
1036010903
case SHELL_OPEN_NORMAL: {
1036110904
sqlite3_open(p->zDbFilename, &p->db);
1036210905
break;
@@ -10383,14 +10926,16 @@
1038310926
shellAddSchemaName, 0, 0);
1038410927
sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
1038510928
shellModuleSchema, 0, 0);
1038610929
sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
1038710930
shellPutsFunc, 0, 0);
10931
+#ifndef SQLITE_NOHAVE_SYSTEM
1038810932
sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
1038910933
editFunc, 0, 0);
1039010934
sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
1039110935
editFunc, 0, 0);
10936
+#endif
1039210937
if( p->openMode==SHELL_OPEN_ZIPFILE ){
1039310938
char *zSql = sqlite3_mprintf(
1039410939
"CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
1039510940
sqlite3_exec(p->db, zSql, 0, 0, 0);
1039610941
sqlite3_free(zSql);
@@ -11001,10 +11546,11 @@
1100111546
#ifndef SQLITE_OMIT_POPEN
1100211547
pclose(p->out);
1100311548
#endif
1100411549
}else{
1100511550
output_file_close(p->out);
11551
+#ifndef SQLITE_NOHAVE_SYSTEM
1100611552
if( p->doXdgOpen ){
1100711553
const char *zXdgOpenCmd =
1100811554
#if defined(_WIN32)
1100911555
"start";
1101011556
#elif defined(__APPLE__)
@@ -11019,10 +11565,11 @@
1101911565
}
1102011566
sqlite3_free(zCmd);
1102111567
outputModePop(p);
1102211568
p->doXdgOpen = 0;
1102311569
}
11570
+#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
1102411571
}
1102511572
p->outfile[0] = 0;
1102611573
p->out = stdout;
1102711574
}
1102811575
@@ -12129,44 +12676,84 @@
1212912676
" mtime INT, -- last modification time\n"
1213012677
" sz INT, -- original file size\n"
1213112678
" data BLOB -- compressed content\n"
1213212679
")";
1213312680
const char *zDrop = "DROP TABLE IF EXISTS sqlar";
12134
- const char *zInsertFmt =
12135
- "REPLACE INTO sqlar(name,mode,mtime,sz,data)\n"
12681
+ const char *zInsertFmt[2] = {
12682
+ "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
1213612683
" SELECT\n"
1213712684
" %s,\n"
1213812685
" mode,\n"
1213912686
" mtime,\n"
1214012687
" CASE substr(lsmode(mode),1,1)\n"
1214112688
" WHEN '-' THEN length(data)\n"
1214212689
" WHEN 'd' THEN 0\n"
1214312690
" ELSE -1 END,\n"
12144
- " CASE WHEN lsmode(mode) LIKE 'd%%' THEN NULL else data END\n"
12691
+ " sqlar_compress(data)\n"
12692
+ " FROM fsdir(%Q,%Q)\n"
12693
+ " WHERE lsmode(mode) NOT LIKE '?%%';",
12694
+ "REPLACE INTO %s(name,mode,mtime,data)\n"
12695
+ " SELECT\n"
12696
+ " %s,\n"
12697
+ " mode,\n"
12698
+ " mtime,\n"
12699
+ " data\n"
1214512700
" FROM fsdir(%Q,%Q)\n"
12146
- " WHERE lsmode(mode) NOT LIKE '?%%';";
12701
+ " WHERE lsmode(mode) NOT LIKE '?%%';"
12702
+ };
1214712703
int i; /* For iterating through azFile[] */
1214812704
int rc; /* Return code */
12705
+ const char *zTab = 0; /* SQL table into which to insert */
12706
+ char *zSql;
12707
+ char zTemp[50];
1214912708
12709
+ arExecSql(pAr, "PRAGMA page_size=512");
1215012710
rc = arExecSql(pAr, "SAVEPOINT ar;");
1215112711
if( rc!=SQLITE_OK ) return rc;
12152
- if( bUpdate==0 ){
12153
- rc = arExecSql(pAr, zDrop);
12154
- if( rc!=SQLITE_OK ) return rc;
12712
+ zTemp[0] = 0;
12713
+ if( pAr->bZip ){
12714
+ /* Initialize the zipfile virtual table, if necessary */
12715
+ if( pAr->zFile ){
12716
+ sqlite3_uint64 r;
12717
+ sqlite3_randomness(sizeof(r),&r);
12718
+ sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
12719
+ zTab = zTemp;
12720
+ zSql = sqlite3_mprintf(
12721
+ "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
12722
+ zTab, pAr->zFile
12723
+ );
12724
+ rc = arExecSql(pAr, zSql);
12725
+ sqlite3_free(zSql);
12726
+ }else{
12727
+ zTab = "zip";
12728
+ }
12729
+ }else{
12730
+ /* Initialize the table for an SQLAR */
12731
+ zTab = "sqlar";
12732
+ if( bUpdate==0 ){
12733
+ rc = arExecSql(pAr, zDrop);
12734
+ if( rc!=SQLITE_OK ) goto end_ar_transaction;
12735
+ }
12736
+ rc = arExecSql(pAr, zCreate);
1215512737
}
12156
- rc = arExecSql(pAr, zCreate);
1215712738
for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
12158
- char *zSql = sqlite3_mprintf(zInsertFmt,
12739
+ char *zSql = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
1215912740
pAr->bVerbose ? "shell_putsnl(name)" : "name",
1216012741
pAr->azArg[i], pAr->zDir);
1216112742
rc = arExecSql(pAr, zSql);
1216212743
sqlite3_free(zSql);
1216312744
}
12745
+end_ar_transaction:
1216412746
if( rc!=SQLITE_OK ){
1216512747
arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
1216612748
}else{
1216712749
rc = arExecSql(pAr, "RELEASE ar;");
12750
+ if( pAr->bZip && pAr->zFile ){
12751
+ zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
12752
+ arExecSql(pAr, zSql);
12753
+ sqlite3_free(zSql);
12754
+ }
1216812755
}
1216912756
return rc;
1217012757
}
1217112758
1217212759
/*
@@ -12184,24 +12771,21 @@
1218412771
if( rc==SQLITE_OK ){
1218512772
int eDbType = SHELL_OPEN_UNSPEC;
1218612773
cmd.p = pState;
1218712774
cmd.db = pState->db;
1218812775
if( cmd.zFile ){
12189
- eDbType = deduceDatabaseType(cmd.zFile);
12776
+ eDbType = deduceDatabaseType(cmd.zFile, 1);
1219012777
}else{
1219112778
eDbType = pState->openMode;
1219212779
}
1219312780
if( eDbType==SHELL_OPEN_ZIPFILE ){
12194
- if( cmd.zFile==0 ){
12195
- cmd.zSrcTable = sqlite3_mprintf("zip");
12196
- }else{
12197
- cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
12198
- }
12199
- if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
12200
- utf8_printf(stderr, "zip archives are read-only\n");
12201
- rc = SQLITE_ERROR;
12202
- goto end_ar_command;
12781
+ if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
12782
+ if( cmd.zFile==0 ){
12783
+ cmd.zSrcTable = sqlite3_mprintf("zip");
12784
+ }else{
12785
+ cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
12786
+ }
1220312787
}
1220412788
cmd.bZip = 1;
1220512789
}else if( cmd.zFile ){
1220612790
int flags;
1220712791
if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
@@ -12222,18 +12806,16 @@
1222212806
cmd.zFile, sqlite3_errmsg(cmd.db)
1222312807
);
1222412808
goto end_ar_command;
1222512809
}
1222612810
sqlite3_fileio_init(cmd.db, 0, 0);
12227
-#ifdef SQLITE_HAVE_ZLIB
1222812811
sqlite3_sqlar_init(cmd.db, 0, 0);
12229
-#endif
1223012812
sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
1223112813
shellPutsFunc, 0, 0);
1223212814
1223312815
}
12234
- if( cmd.zSrcTable==0 ){
12816
+ if( cmd.zSrcTable==0 && cmd.bZip==0 ){
1223512817
if( cmd.eCmd!=AR_CMD_CREATE
1223612818
&& sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
1223712819
){
1223812820
utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
1223912821
rc = SQLITE_ERROR;
@@ -12622,11 +13204,11 @@
1262213204
if( strcmp(azArg[1],"full")==0 ){
1262313205
p->autoEQP = AUTOEQP_full;
1262413206
}else if( strcmp(azArg[1],"trigger")==0 ){
1262513207
p->autoEQP = AUTOEQP_trigger;
1262613208
}else{
12627
- p->autoEQP = booleanValue(azArg[1]);
13209
+ p->autoEQP = (u8)booleanValue(azArg[1]);
1262813210
}
1262913211
}else{
1263013212
raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
1263113213
rc = 1;
1263213214
}
@@ -12709,18 +13291,15 @@
1270913291
raw_printf(p->out, "ANALYZE sqlite_master;\n");
1271013292
sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
1271113293
callback, &data, &zErrMsg);
1271213294
data.cMode = data.mode = MODE_Insert;
1271313295
data.zDestTable = "sqlite_stat1";
12714
- shell_exec(p->db, "SELECT * FROM sqlite_stat1",
12715
- shell_callback, &data,&zErrMsg);
13296
+ shell_exec(p, "SELECT * FROM sqlite_stat1", &zErrMsg);
1271613297
data.zDestTable = "sqlite_stat3";
12717
- shell_exec(p->db, "SELECT * FROM sqlite_stat3",
12718
- shell_callback, &data,&zErrMsg);
13298
+ shell_exec(p, "SELECT * FROM sqlite_stat3", &zErrMsg);
1271913299
data.zDestTable = "sqlite_stat4";
12720
- shell_exec(p->db, "SELECT * FROM sqlite_stat4",
12721
- shell_callback, &data, &zErrMsg);
13300
+ shell_exec(p, "SELECT * FROM sqlite_stat4", &zErrMsg);
1272213301
raw_printf(p->out, "ANALYZE sqlite_master;\n");
1272313302
}
1272413303
}else
1272513304
1272613305
if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
@@ -13197,16 +13776,18 @@
1319713776
/* Check for command-line arguments */
1319813777
for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
1319913778
const char *z = azArg[iName];
1320013779
if( optionMatch(z,"new") ){
1320113780
newFlag = 1;
13202
-#ifdef SQLITE_HAVE_ZIP
13781
+#ifdef SQLITE_HAVE_ZLIB
1320313782
}else if( optionMatch(z, "zip") ){
1320413783
p->openMode = SHELL_OPEN_ZIPFILE;
1320513784
#endif
1320613785
}else if( optionMatch(z, "append") ){
1320713786
p->openMode = SHELL_OPEN_APPENDVFS;
13787
+ }else if( optionMatch(z, "readonly") ){
13788
+ p->openMode = SHELL_OPEN_READONLY;
1320813789
}else if( z[0]=='-' ){
1320913790
utf8_printf(stderr, "unknown option: %s\n", z);
1321013791
rc = 1;
1321113792
goto meta_command_exit;
1321213793
}
@@ -13259,10 +13840,11 @@
1325913840
}else{
1326013841
p->outCount = 0;
1326113842
}
1326213843
output_reset(p);
1326313844
if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
13845
+#ifndef SQLITE_NOHAVE_SYSTEM
1326413846
if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
1326513847
p->doXdgOpen = 1;
1326613848
outputModePush(p);
1326713849
if( zFile[1]=='x' ){
1326813850
newTempFile(p, "csv");
@@ -13273,10 +13855,11 @@
1327313855
newTempFile(p, "txt");
1327413856
bTxtMode = 1;
1327513857
}
1327613858
zFile = p->zTempFile;
1327713859
}
13860
+#endif /* SQLITE_NOHAVE_SYSTEM */
1327813861
if( zFile[0]=='|' ){
1327913862
#ifdef SQLITE_OMIT_POPEN
1328013863
raw_printf(stderr, "Error: pipes are not supported in this OS\n");
1328113864
rc = 1;
1328213865
p->out = stdout;
@@ -13392,14 +13975,13 @@
1339213975
rc = 1;
1339313976
}
1339413977
sqlite3_close(pSrc);
1339513978
}else
1339613979
13397
-
1339813980
if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
1339913981
if( nArg==2 ){
13400
- p->scanstatsOn = booleanValue(azArg[1]);
13982
+ p->scanstatsOn = (u8)booleanValue(azArg[1]);
1340113983
#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
1340213984
raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
1340313985
#endif
1340413986
}else{
1340513987
raw_printf(stderr, "Usage: .scanstats on|off\n");
@@ -13983,15 +14565,16 @@
1398314565
freeText(&sQuery);
1398414566
freeText(&sSql);
1398514567
if( bDebug ){
1398614568
utf8_printf(p->out, "%s\n", zSql);
1398714569
}else{
13988
- shell_exec(p->db, zSql, shell_callback, p, 0);
14570
+ shell_exec(p, zSql, 0);
1398914571
}
1399014572
sqlite3_free(zSql);
1399114573
}else
1399214574
14575
+#ifndef SQLITE_NOHAVE_SYSTEM
1399314576
if( c=='s'
1399414577
&& (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
1399514578
){
1399614579
char *zCmd;
1399714580
int i, x;
@@ -14007,10 +14590,11 @@
1400714590
}
1400814591
x = system(zCmd);
1400914592
sqlite3_free(zCmd);
1401014593
if( x ) raw_printf(stderr, "System command returns %d\n", x);
1401114594
}else
14595
+#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
1401214596
1401314597
if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
1401414598
static const char *azBool[] = { "off", "on", "trigger", "full"};
1401514599
int i;
1401614600
if( nArg!=1 ){
@@ -14046,11 +14630,11 @@
1404614630
p->zDbFilename ? p->zDbFilename : "");
1404714631
}else
1404814632
1404914633
if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
1405014634
if( nArg==2 ){
14051
- p->statsOn = booleanValue(azArg[1]);
14635
+ p->statsOn = (u8)booleanValue(azArg[1]);
1405214636
}else if( nArg==1 ){
1405314637
display_stats(p->db, p, 0);
1405414638
}else{
1405514639
raw_printf(stderr, "Usage: .stats ?on|off?\n");
1405614640
rc = 1;
@@ -14586,10 +15170,20 @@
1458615170
return 1; /* SQL Server */
1458715171
}
1458815172
return 0;
1458915173
}
1459015174
15175
+/*
15176
+** We need a default sqlite3_complete() implementation to use in case
15177
+** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
15178
+** any arbitrary text is a complete SQL statement. This is not very
15179
+** user-friendly, but it does seem to work.
15180
+*/
15181
+#ifdef SQLITE_OMIT_COMPLETE
15182
+int sqlite3_complete(const char *zSql){ return 1; }
15183
+#endif
15184
+
1459115185
/*
1459215186
** Return true if zSql is a complete SQL statement. Return false if it
1459315187
** ends in the middle of a string literal or C-style comment.
1459415188
*/
1459515189
static int line_is_complete(char *zSql, int nSql){
@@ -14610,11 +15204,11 @@
1461015204
char *zErrMsg = 0;
1461115205
1461215206
open_db(p, 0);
1461315207
if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
1461415208
BEGIN_TIMER;
14615
- rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
15209
+ rc = shell_exec(p, zSql, &zErrMsg);
1461615210
END_TIMER;
1461715211
if( rc || zErrMsg ){
1461815212
char zPrefix[100];
1461915213
if( in!=0 || !stdin_is_interactive ){
1462015214
sqlite3_snprintf(sizeof(zPrefix), zPrefix,
@@ -14842,10 +15436,14 @@
1484215436
1484315437
/*
1484415438
** Show available command line options
1484515439
*/
1484615440
static const char zOptions[] =
15441
+#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
15442
+ " -A ARGS... run \".archive ARGS\" and exit\n"
15443
+#endif
15444
+ " -append append the database to the end of the file\n"
1484715445
" -ascii set output mode to 'ascii'\n"
1484815446
" -bail stop after hitting an error\n"
1484915447
" -batch force batch I/O\n"
1485015448
" -column set output mode to 'column'\n"
1485115449
" -cmd COMMAND run \"COMMAND\" before reading stdin\n"
@@ -14868,17 +15466,21 @@
1486815466
#endif
1486915467
" -newline SEP set output row separator. Default: '\\n'\n"
1487015468
" -nullvalue TEXT set text string for NULL values. Default ''\n"
1487115469
" -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
1487215470
" -quote set output mode to 'quote'\n"
15471
+ " -readonly open the database read-only\n"
1487315472
" -separator SEP set output column separator. Default: '|'\n"
1487415473
" -stats print memory stats before each finalize\n"
1487515474
" -version show SQLite version\n"
1487615475
" -vfs NAME use NAME as the default VFS\n"
1487715476
#ifdef SQLITE_ENABLE_VFSTRACE
1487815477
" -vfstrace enable tracing of all VFS calls\n"
1487915478
#endif
15479
+#ifdef SQLITE_HAVE_ZLIB
15480
+ " -zip open the file as a ZIP Archive\n"
15481
+#endif
1488015482
;
1488115483
static void usage(int showDetail){
1488215484
utf8_printf(stderr,
1488315485
"Usage: %s [OPTIONS] FILENAME [SQL]\n"
1488415486
"FILENAME is the name of an SQLite database. A new database is created\n"
@@ -14977,25 +15579,43 @@
1497715579
sqlite3_sourceid(), SQLITE_SOURCE_ID);
1497815580
exit(1);
1497915581
}
1498015582
#endif
1498115583
main_init(&data);
15584
+
15585
+ /* On Windows, we must translate command-line arguments into UTF-8.
15586
+ ** The SQLite memory allocator subsystem has to be enabled in order to
15587
+ ** do this. But we want to run an sqlite3_shutdown() afterwards so that
15588
+ ** subsequent sqlite3_config() calls will work. So copy all results into
15589
+ ** memory that does not come from the SQLite memory allocator.
15590
+ */
1498215591
#if !SQLITE_SHELL_IS_UTF8
1498315592
sqlite3_initialize();
14984
- argv = sqlite3_malloc64(sizeof(argv[0])*argc);
15593
+ argv = malloc(sizeof(argv[0])*argc);
1498515594
if( argv==0 ){
1498615595
raw_printf(stderr, "out of memory\n");
1498715596
exit(1);
1498815597
}
1498915598
for(i=0; i<argc; i++){
14990
- argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]);
15599
+ char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
15600
+ int n;
15601
+ if( z==0 ){
15602
+ raw_printf(stderr, "out of memory\n");
15603
+ exit(1);
15604
+ }
15605
+ n = (int)strlen(z);
15606
+ argv[i] = malloc( n+1 );
1499115607
if( argv[i]==0 ){
1499215608
raw_printf(stderr, "out of memory\n");
1499315609
exit(1);
1499415610
}
15611
+ memcpy(argv[i], z, n+1);
15612
+ sqlite3_free(z);
1499515613
}
15614
+ sqlite3_shutdown();
1499615615
#endif
15616
+
1499715617
assert( argc>=1 && argv && argv[0] );
1499815618
Argv0 = argv[0];
1499915619
1500015620
/* Make sure we have a valid signal handler early, before anything
1500115621
** else is done.
@@ -15111,16 +15731,24 @@
1511115731
sqlite3_vfs_register(pVfs, 1);
1511215732
}else{
1511315733
utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
1511415734
exit(1);
1511515735
}
15116
-#ifdef SQLITE_HAVE_ZIP
15736
+#ifdef SQLITE_HAVE_ZLIB
1511715737
}else if( strcmp(z,"-zip")==0 ){
1511815738
data.openMode = SHELL_OPEN_ZIPFILE;
1511915739
#endif
1512015740
}else if( strcmp(z,"-append")==0 ){
1512115741
data.openMode = SHELL_OPEN_APPENDVFS;
15742
+ }else if( strcmp(z,"-readonly")==0 ){
15743
+ data.openMode = SHELL_OPEN_READONLY;
15744
+#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
15745
+ }else if( strncmp(z, "-A",2)==0 ){
15746
+ /* All remaining command-line arguments are passed to the ".archive"
15747
+ ** command, so ignore them */
15748
+ break;
15749
+#endif
1512215750
}
1512315751
}
1512415752
if( data.zDbFilename==0 ){
1512515753
#ifndef SQLITE_OMIT_MEMORYDB
1512615754
data.zDbFilename = ":memory:";
@@ -15170,11 +15798,11 @@
1517015798
}else if( strcmp(z,"-column")==0 ){
1517115799
data.mode = MODE_Column;
1517215800
}else if( strcmp(z,"-csv")==0 ){
1517315801
data.mode = MODE_Csv;
1517415802
memcpy(data.colSeparator,",",2);
15175
-#ifdef SQLITE_HAVE_ZIP
15803
+#ifdef SQLITE_HAVE_ZLIB
1517615804
}else if( strcmp(z,"-zip")==0 ){
1517715805
data.openMode = SHELL_OPEN_ZIPFILE;
1517815806
#endif
1517915807
}else if( strcmp(z,"-append")==0 ){
1518015808
data.openMode = SHELL_OPEN_APPENDVFS;
@@ -15253,19 +15881,36 @@
1525315881
if( z[0]=='.' ){
1525415882
rc = do_meta_command(z, &data);
1525515883
if( rc && bail_on_error ) return rc==2 ? 0 : rc;
1525615884
}else{
1525715885
open_db(&data, 0);
15258
- rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
15886
+ rc = shell_exec(&data, z, &zErrMsg);
1525915887
if( zErrMsg!=0 ){
1526015888
utf8_printf(stderr,"Error: %s\n", zErrMsg);
1526115889
if( bail_on_error ) return rc!=0 ? rc : 1;
1526215890
}else if( rc!=0 ){
1526315891
utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
1526415892
if( bail_on_error ) return rc;
1526515893
}
1526615894
}
15895
+#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
15896
+ }else if( strncmp(z, "-A", 2)==0 ){
15897
+ if( nCmd>0 ){
15898
+ utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
15899
+ " with \"%s\"\n", z);
15900
+ return 1;
15901
+ }
15902
+ open_db(&data, 0);
15903
+ if( z[2] ){
15904
+ argv[i] = &z[2];
15905
+ arDotCommand(&data, argv+(i-1), argc-(i-1));
15906
+ }else{
15907
+ arDotCommand(&data, argv+i, argc-i);
15908
+ }
15909
+ readStdin = 0;
15910
+ break;
15911
+#endif
1526715912
}else{
1526815913
utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
1526915914
raw_printf(stderr,"Use -help for a list of options.\n");
1527015915
return 1;
1527115916
}
@@ -15281,11 +15926,11 @@
1528115926
if( azCmd[i][0]=='.' ){
1528215927
rc = do_meta_command(azCmd[i], &data);
1528315928
if( rc ) return rc==2 ? 0 : rc;
1528415929
}else{
1528515930
open_db(&data, 0);
15286
- rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg);
15931
+ rc = shell_exec(&data, azCmd[i], &zErrMsg);
1528715932
if( zErrMsg!=0 ){
1528815933
utf8_printf(stderr,"Error: %s\n", zErrMsg);
1528915934
return rc!=0 ? rc : 1;
1529015935
}else if( rc!=0 ){
1529115936
utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
@@ -15344,10 +15989,10 @@
1534415989
find_home_dir(1);
1534515990
output_reset(&data);
1534615991
data.doXdgOpen = 0;
1534715992
clearTempFile(&data);
1534815993
#if !SQLITE_SHELL_IS_UTF8
15349
- for(i=0; i<argc; i++) sqlite3_free(argv[i]);
15350
- sqlite3_free(argv);
15994
+ for(i=0; i<argc; i++) free(argv[i]);
15995
+ free(argv);
1535115996
#endif
1535215997
return rc;
1535315998
}
1535415999
--- src/shell.c
+++ src/shell.c
@@ -995,11 +995,14 @@
995
996 /*
997 ** We need several data types from the Windows SDK header.
998 */
999
 
1000 #define WIN32_LEAN_AND_MEAN
 
 
1001 #include "windows.h"
1002
1003 /*
1004 ** We need several support functions from the SQLite core.
1005 */
@@ -2314,19 +2317,24 @@
2314 FILETIME lastAccess;
2315 FILETIME lastWrite;
2316 SYSTEMTIME currentTime;
2317 LONGLONG intervals;
2318 HANDLE hFile;
 
 
 
2319 GetSystemTime(&currentTime);
2320 SystemTimeToFileTime(&currentTime, &lastAccess);
2321 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2322 lastWrite.dwLowDateTime = (DWORD)intervals;
2323 lastWrite.dwHighDateTime = intervals >> 32;
2324 hFile = CreateFile(
2325 zFile, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
 
2326 FILE_FLAG_BACKUP_SEMANTICS, NULL
2327 );
 
2328 if( hFile!=INVALID_HANDLE_VALUE ){
2329 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2330 CloseHandle(hFile);
2331 return !bResult;
2332 }else{
@@ -2530,14 +2538,16 @@
2530 FsdirLevel *pLvl = &pCur->aLvl[i];
2531 if( pLvl->pDir ) closedir(pLvl->pDir);
2532 sqlite3_free(pLvl->zDir);
2533 }
2534 sqlite3_free(pCur->zPath);
 
2535 pCur->aLvl = 0;
2536 pCur->zPath = 0;
2537 pCur->zBase = 0;
2538 pCur->nBase = 0;
 
2539 pCur->iLvl = -1;
2540 pCur->iRowid = 1;
2541 }
2542
2543 /*
@@ -2545,11 +2555,10 @@
2545 */
2546 static int fsdirClose(sqlite3_vtab_cursor *cur){
2547 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2548
2549 fsdirResetCursor(pCur);
2550 sqlite3_free(pCur->aLvl);
2551 sqlite3_free(pCur);
2552 return SQLITE_OK;
2553 }
2554
2555 /*
@@ -3992,33 +4001,52 @@
3992 SQLITE_EXTENSION_INIT1
3993 #include <stdio.h>
3994 #include <string.h>
3995 #include <assert.h>
3996
3997 #include <sys/types.h>
3998 #include <sys/stat.h>
3999 #include <fcntl.h>
4000 #if !defined(_WIN32) && !defined(WIN32)
4001 # include <unistd.h>
4002 # include <dirent.h>
4003 # include <utime.h>
4004 #else
4005 # include <io.h>
4006 #endif
4007 #include <time.h>
4008 #include <errno.h>
4009
4010 #include <zlib.h>
4011
4012 #ifndef SQLITE_OMIT_VIRTUALTABLE
4013
4014 #ifndef SQLITE_AMALGAMATION
 
4015 /* typedef sqlite3_int64 i64; */
4016 /* typedef unsigned char u8; */
4017 typedef unsigned short u16;
4018 typedef unsigned long u32;
4019 #define MIN(a,b) ((a)<(b) ? (a) : (b))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4020 #endif
4021
4022 static const char ZIPFILE_SCHEMA[] =
4023 "CREATE TABLE y("
4024 "name PRIMARY KEY," /* 0: Name of file in zip archive */
@@ -4055,34 +4083,29 @@
4055 ** ZIPFILE_SIGNATURE_CDS:
4056 ** First 4 bytes of a valid CDS record.
4057 **
4058 ** ZIPFILE_SIGNATURE_LFH:
4059 ** First 4 bytes of a valid LFH record.
 
 
 
4060 */
4061 #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
4062 #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
4063 #define ZIPFILE_NEWENTRY_REQUIRED 20
4064 #define ZIPFILE_NEWENTRY_FLAGS 0x800
4065 #define ZIPFILE_SIGNATURE_CDS 0x02014b50
4066 #define ZIPFILE_SIGNATURE_LFH 0x04034b50
4067 #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
 
 
 
 
 
4068 #define ZIPFILE_LFH_FIXED_SZ 30
4069
4070 /*
4071 ** Set the error message contained in context ctx to the results of
4072 ** vprintf(zFmt, ...).
4073 */
4074 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4075 char *zMsg = 0;
4076 va_list ap;
4077 va_start(ap, zFmt);
4078 zMsg = sqlite3_vmprintf(zFmt, ap);
4079 sqlite3_result_error(ctx, zMsg, -1);
4080 sqlite3_free(zMsg);
4081 va_end(ap);
4082 }
4083
4084
4085 /*
4086 *** 4.3.16 End of central directory record:
4087 ***
4088 *** end of central dir signature 4 bytes (0x06054b50)
@@ -4184,51 +4207,43 @@
4184 u16 nExtra;
4185 };
4186
4187 typedef struct ZipfileEntry ZipfileEntry;
4188 struct ZipfileEntry {
4189 char *zPath; /* Path of zipfile entry */
4190 u8 *aCdsEntry; /* Buffer containing entire CDS entry */
4191 int nCdsEntry; /* Size of buffer aCdsEntry[] in bytes */
4192 int bDeleted; /* True if entry has been deleted */
 
4193 ZipfileEntry *pNext; /* Next element in in-memory CDS */
4194 };
4195
4196 /*
4197 ** Cursor type for recursively iterating through a directory structure.
4198 */
4199 typedef struct ZipfileCsr ZipfileCsr;
4200 struct ZipfileCsr {
4201 sqlite3_vtab_cursor base; /* Base class - must be first */
4202 i64 iId; /* Cursor ID */
4203 int bEof; /* True when at EOF */
 
4204
4205 /* Used outside of write transactions */
4206 FILE *pFile; /* Zip file */
4207 i64 iNextOff; /* Offset of next record in central directory */
4208 ZipfileEOCD eocd; /* Parse of central directory record */
4209
4210 /* Used inside write transactions */
4211 ZipfileEntry *pCurrent;
4212
4213 ZipfileCDS cds; /* Central Directory Structure */
4214 ZipfileLFH lfh; /* Local File Header for current entry */
4215 i64 iDataOff; /* Offset in zipfile to data */
4216 u32 mTime; /* Extended mtime value */
4217 int flags; /* Flags byte (see below for bits) */
4218 ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
4219 };
4220
4221 /*
4222 ** Values for ZipfileCsr.flags.
4223 */
4224 #define ZIPFILE_MTIME_VALID 0x0001
4225
4226 typedef struct ZipfileTab ZipfileTab;
4227 struct ZipfileTab {
4228 sqlite3_vtab base; /* Base class - must be first */
4229 char *zFile; /* Zip file this table accesses (may be NULL) */
 
4230 u8 *aBuffer; /* Temporary buffer used for various tasks */
4231
4232 ZipfileCsr *pCsrList; /* List of cursors */
4233 i64 iNextCsrid;
4234
@@ -4238,21 +4253,37 @@
4238 FILE *pWriteFd; /* File handle open on zip archive */
4239 i64 szCurrent; /* Current size of zip archive */
4240 i64 szOrig; /* Size of archive at start of transaction */
4241 };
4242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4243 static void zipfileDequote(char *zIn){
4244 char q = zIn[0];
4245 if( q=='"' || q=='\'' || q=='`' || q=='[' ){
4246 char c;
4247 int iIn = 1;
4248 int iOut = 0;
4249 if( q=='[' ) q = ']';
4250 while( (c = zIn[iIn++]) ){
4251 if( c==q ){
4252 if( zIn[iIn++]!=q ) break;
4253 }
4254 zIn[iOut++] = c;
4255 }
4256 zIn[iOut] = '\0';
4257 }
4258 }
@@ -4275,10 +4306,25 @@
4275 int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
4276 int nFile = 0;
4277 const char *zFile = 0;
4278 ZipfileTab *pNew = 0;
4279 int rc;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4280
4281 if( argc>3 ){
4282 zFile = argv[3];
4283 nFile = (int)strlen(zFile)+1;
4284 }
@@ -4286,10 +4332,11 @@
4286 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
4287 if( rc==SQLITE_OK ){
4288 pNew = (ZipfileTab*)sqlite3_malloc(nByte+nFile);
4289 if( pNew==0 ) return SQLITE_NOMEM;
4290 memset(pNew, 0, nByte+nFile);
 
4291 pNew->aBuffer = (u8*)&pNew[1];
4292 if( zFile ){
4293 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
4294 memcpy(pNew->zFile, zFile, nFile);
4295 zipfileDequote(pNew->zFile);
@@ -4296,15 +4343,48 @@
4296 }
4297 }
4298 *ppVtab = (sqlite3_vtab*)pNew;
4299 return rc;
4300 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4301
4302 /*
4303 ** This method is the destructor for zipfile vtab objects.
4304 */
4305 static int zipfileDisconnect(sqlite3_vtab *pVtab){
 
4306 sqlite3_free(pVtab);
4307 return SQLITE_OK;
4308 }
4309
4310 /*
@@ -4328,16 +4408,24 @@
4328 /*
4329 ** Reset a cursor back to the state it was in when first returned
4330 ** by zipfileOpen().
4331 */
4332 static void zipfileResetCursor(ZipfileCsr *pCsr){
4333 sqlite3_free(pCsr->cds.zFile);
4334 pCsr->cds.zFile = 0;
 
4335 pCsr->bEof = 0;
4336 if( pCsr->pFile ){
4337 fclose(pCsr->pFile);
4338 pCsr->pFile = 0;
 
 
 
 
 
 
 
4339 }
4340 }
4341
4342 /*
4343 ** Destructor for an ZipfileCsr.
@@ -4347,32 +4435,46 @@
4347 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
4348 ZipfileCsr **pp;
4349 zipfileResetCursor(pCsr);
4350
4351 /* Remove this cursor from the ZipfileTab.pCsrList list. */
4352 for(pp=&pTab->pCsrList; *pp; pp=&((*pp)->pCsrNext)){
4353 if( *pp==pCsr ){
4354 *pp = pCsr->pCsrNext;
4355 break;
4356 }
4357 }
4358
4359 sqlite3_free(pCsr);
4360 return SQLITE_OK;
4361 }
4362
4363 /*
4364 ** Set the error message for the virtual table associated with cursor
4365 ** pCsr to the results of vprintf(zFmt, ...).
4366 */
4367 static void zipfileSetErrmsg(ZipfileCsr *pCsr, const char *zFmt, ...){
 
 
 
 
 
 
 
4368 va_list ap;
4369 va_start(ap, zFmt);
 
4370 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
4371 va_end(ap);
4372 }
4373
 
 
 
 
 
 
 
 
 
 
4374 static int zipfileReadData(
4375 FILE *pFile, /* Read from this file */
4376 u8 *aRead, /* Read into this buffer */
4377 int nRead, /* Number of bytes to read */
4378 i64 iOff, /* Offset to read from */
@@ -4402,24 +4504,38 @@
4402 }
4403 pTab->szCurrent += nWrite;
4404 return SQLITE_OK;
4405 }
4406
 
 
 
4407 static u16 zipfileGetU16(const u8 *aBuf){
4408 return (aBuf[1] << 8) + aBuf[0];
4409 }
 
 
 
 
4410 static u32 zipfileGetU32(const u8 *aBuf){
4411 return ((u32)(aBuf[3]) << 24)
4412 + ((u32)(aBuf[2]) << 16)
4413 + ((u32)(aBuf[1]) << 8)
4414 + ((u32)(aBuf[0]) << 0);
4415 }
4416
 
 
 
4417 static void zipfilePutU16(u8 *aBuf, u16 val){
4418 aBuf[0] = val & 0xFF;
4419 aBuf[1] = (val>>8) & 0xFF;
4420 }
 
 
 
 
4421 static void zipfilePutU32(u8 *aBuf, u32 val){
4422 aBuf[0] = val & 0xFF;
4423 aBuf[1] = (val>>8) & 0xFF;
4424 aBuf[2] = (val>>16) & 0xFF;
4425 aBuf[3] = (val>>24) & 0xFF;
@@ -4429,19 +4545,15 @@
4429 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
4430
4431 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
4432 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
4433
4434 static u8* zipfileCsrBuffer(ZipfileCsr *pCsr){
4435 return ((ZipfileTab*)(pCsr->base.pVtab))->aBuffer;
4436 }
4437
4438 /*
4439 ** Magic numbers used to read CDS records.
4440 */
4441 #define ZIPFILE_CDS_FIXED_SZ 46
4442 #define ZIPFILE_CDS_NFILE_OFF 28
 
4443
4444 /*
4445 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
4446 ** if the record is not well-formed, or SQLITE_OK otherwise.
4447 */
@@ -4474,172 +4586,84 @@
4474
4475 return rc;
4476 }
4477
4478 /*
4479 ** Read the CDS record for the current entry from disk into pCsr->cds.
4480 */
4481 static int zipfileCsrReadCDS(ZipfileCsr *pCsr){
4482 char **pzErr = &pCsr->base.pVtab->zErrMsg;
4483 u8 *aRead;
4484 int rc = SQLITE_OK;
4485
4486 sqlite3_free(pCsr->cds.zFile);
4487 pCsr->cds.zFile = 0;
4488
4489 if( pCsr->pCurrent==0 ){
4490 aRead = zipfileCsrBuffer(pCsr);
4491 rc = zipfileReadData(
4492 pCsr->pFile, aRead, ZIPFILE_CDS_FIXED_SZ, pCsr->iNextOff, pzErr
4493 );
4494 }else{
4495 aRead = pCsr->pCurrent->aCdsEntry;
4496 }
4497
4498 if( rc==SQLITE_OK ){
4499 rc = zipfileReadCDS(aRead, &pCsr->cds);
4500 if( rc!=SQLITE_OK ){
4501 assert( pCsr->pCurrent==0 );
4502 zipfileSetErrmsg(pCsr,"failed to read CDS at offset %lld",pCsr->iNextOff);
4503 }else{
4504 int nRead;
4505 if( pCsr->pCurrent==0 ){
4506 nRead = pCsr->cds.nFile + pCsr->cds.nExtra;
4507 aRead = zipfileCsrBuffer(pCsr);
4508 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
4509 rc = zipfileReadData(pCsr->pFile, aRead, nRead, pCsr->iNextOff, pzErr);
4510 }else{
4511 aRead = &aRead[ZIPFILE_CDS_FIXED_SZ];
4512 }
4513
4514 if( rc==SQLITE_OK ){
4515 pCsr->cds.zFile = sqlite3_mprintf("%.*s", (int)pCsr->cds.nFile, aRead);
4516 pCsr->iNextOff += pCsr->cds.nFile;
4517 pCsr->iNextOff += pCsr->cds.nExtra;
4518 pCsr->iNextOff += pCsr->cds.nComment;
4519 }
4520
4521 /* Scan the cds.nExtra bytes of "extra" fields for any that can
4522 ** be interpreted. The general format of an extra field is:
4523 **
4524 ** Header ID 2 bytes
4525 ** Data Size 2 bytes
4526 ** Data N bytes
4527 **
4528 */
4529 if( rc==SQLITE_OK ){
4530 u8 *p = &aRead[pCsr->cds.nFile];
4531 u8 *pEnd = &p[pCsr->cds.nExtra];
4532
4533 while( p<pEnd ){
4534 u16 id = zipfileRead16(p);
4535 u16 nByte = zipfileRead16(p);
4536
4537 switch( id ){
4538 case ZIPFILE_EXTRA_TIMESTAMP: {
4539 u8 b = p[0];
4540 if( b & 0x01 ){ /* 0x01 -> modtime is present */
4541 pCsr->mTime = zipfileGetU32(&p[1]);
4542 pCsr->flags |= ZIPFILE_MTIME_VALID;
4543 }
4544 break;
4545 }
4546 }
4547
4548 p += nByte;
4549 }
4550 }
4551 }
4552 }
4553
4554 return rc;
4555 }
4556
4557 static FILE *zipfileGetFd(ZipfileCsr *pCsr){
4558 if( pCsr->pFile ) return pCsr->pFile;
4559 return ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
4560 }
4561
4562 static int zipfileReadLFH(
4563 FILE *pFd,
4564 i64 iOffset,
4565 u8 *aTmp,
4566 ZipfileLFH *pLFH,
4567 char **pzErr
4568 ){
4569 u8 *aRead = aTmp;
4570 static const int szFix = ZIPFILE_LFH_FIXED_SZ;
4571 int rc;
4572
4573 rc = zipfileReadData(pFd, aRead, szFix, iOffset, pzErr);
4574 if( rc==SQLITE_OK ){
4575 u32 sig = zipfileRead32(aRead);
4576 if( sig!=ZIPFILE_SIGNATURE_LFH ){
4577 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", (int)iOffset);
4578 rc = SQLITE_ERROR;
4579 }else{
4580 pLFH->iVersionExtract = zipfileRead16(aRead);
4581 pLFH->flags = zipfileRead16(aRead);
4582 pLFH->iCompression = zipfileRead16(aRead);
4583 pLFH->mTime = zipfileRead16(aRead);
4584 pLFH->mDate = zipfileRead16(aRead);
4585 pLFH->crc32 = zipfileRead32(aRead);
4586 pLFH->szCompressed = zipfileRead32(aRead);
4587 pLFH->szUncompressed = zipfileRead32(aRead);
4588 pLFH->nFile = zipfileRead16(aRead);
4589 pLFH->nExtra = zipfileRead16(aRead);
4590 assert( aRead==&aTmp[szFix] );
4591 }
4592 }
4593 return rc;
4594 }
4595
4596 static int zipfileCsrReadLFH(ZipfileCsr *pCsr){
4597 FILE *pFile = zipfileGetFd(pCsr);
4598 char **pzErr = &pCsr->base.pVtab->zErrMsg;
4599 u8 *aRead = zipfileCsrBuffer(pCsr);
4600 int rc = zipfileReadLFH(pFile, pCsr->cds.iOffset, aRead, &pCsr->lfh, pzErr);
4601 pCsr->iDataOff = pCsr->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
4602 pCsr->iDataOff += pCsr->lfh.nFile+pCsr->lfh.nExtra;
4603 return rc;
4604 }
4605
4606
4607 /*
4608 ** Advance an ZipfileCsr to its next row of output.
4609 */
4610 static int zipfileNext(sqlite3_vtab_cursor *cur){
4611 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4612 int rc = SQLITE_OK;
4613 pCsr->flags = 0;
4614
4615 if( pCsr->pCurrent==0 ){
4616 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
4617 if( pCsr->iNextOff>=iEof ){
4618 pCsr->bEof = 1;
4619 }
4620 }else{
4621 assert( pCsr->pFile==0 );
4622 do {
4623 pCsr->pCurrent = pCsr->pCurrent->pNext;
4624 }while( pCsr->pCurrent && pCsr->pCurrent->bDeleted );
4625 if( pCsr->pCurrent==0 ){
4626 pCsr->bEof = 1;
4627 }
4628 }
4629
4630 if( pCsr->bEof==0 ){
4631 rc = zipfileCsrReadCDS(pCsr);
4632 if( rc==SQLITE_OK ){
4633 rc = zipfileCsrReadLFH(pCsr);
4634 }
4635 }
4636
4637 return rc;
4638 }
4639
4640 /*
4641 ** "Standard" MS-DOS time format:
4642 **
4643 ** File modification time:
4644 ** Bits 00-04: seconds divided by 2
4645 ** Bits 05-10: minute
@@ -4646,48 +4670,241 @@
4646 ** Bits 11-15: hour
4647 ** File modification date:
4648 ** Bits 00-04: day
4649 ** Bits 05-08: month (1-12)
4650 ** Bits 09-15: years from 1980
4651 */
4652 static time_t zipfileMtime(ZipfileCsr *pCsr){
4653 struct tm t;
4654 memset(&t, 0, sizeof(t));
4655 t.tm_sec = (pCsr->cds.mTime & 0x1F)*2;
4656 t.tm_min = (pCsr->cds.mTime >> 5) & 0x2F;
4657 t.tm_hour = (pCsr->cds.mTime >> 11) & 0x1F;
4658
4659 t.tm_mday = (pCsr->cds.mDate & 0x1F);
4660 t.tm_mon = ((pCsr->cds.mDate >> 5) & 0x0F) - 1;
4661 t.tm_year = 80 + ((pCsr->cds.mDate >> 9) & 0x7F);
4662
4663 return mktime(&t);
4664 }
4665
4666 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mTime){
4667 time_t t = (time_t)mTime;
4668 struct tm res;
4669
4670 #if !defined(_WIN32) && !defined(WIN32)
4671 localtime_r(&t, &res);
4672 #else
4673 memcpy(&res, localtime(&t), sizeof(struct tm));
4674 #endif
4675
4676 pCds->mTime = (u16)(
4677 (res.tm_sec / 2) +
4678 (res.tm_min << 5) +
4679 (res.tm_hour << 11));
4680
4681 pCds->mDate = (u16)(
4682 (res.tm_mday-1) +
4683 ((res.tm_mon+1) << 5) +
4684 ((res.tm_year-80) << 9));
4685 }
4686
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4687 static void zipfileInflate(
4688 sqlite3_context *pCtx, /* Store error here, if any */
4689 const u8 *aIn, /* Compressed data */
4690 int nIn, /* Size of buffer aIn[] in bytes */
4691 int nOut /* Expected output size */
4692 ){
4693 u8 *aRes = sqlite3_malloc(nOut);
@@ -4709,22 +4926,35 @@
4709 }else{
4710 err = inflate(&str, Z_NO_FLUSH);
4711 if( err!=Z_STREAM_END ){
4712 zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
4713 }else{
4714 sqlite3_result_blob(pCtx, aRes, nOut, SQLITE_TRANSIENT);
 
4715 }
4716 }
4717 sqlite3_free(aRes);
4718 inflateEnd(&str);
4719 }
4720 }
4721
 
 
 
 
 
 
 
 
 
 
 
 
4722 static int zipfileDeflate(
4723 ZipfileTab *pTab, /* Set error message here */
4724 const u8 *aIn, int nIn, /* Input */
4725 u8 **ppOut, int *pnOut /* Output */
 
4726 ){
4727 int nAlloc = (int)compressBound(nIn);
4728 u8 *aOut;
4729 int rc = SQLITE_OK;
4730
@@ -4746,11 +4976,11 @@
4746 if( res==Z_STREAM_END ){
4747 *ppOut = aOut;
4748 *pnOut = (int)str.total_out;
4749 }else{
4750 sqlite3_free(aOut);
4751 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: deflate() error");
4752 rc = SQLITE_ERROR;
4753 }
4754 deflateEnd(&str);
4755 }
4756
@@ -4766,121 +4996,134 @@
4766 sqlite3_vtab_cursor *cur, /* The cursor */
4767 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
4768 int i /* Which column to return */
4769 ){
4770 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
 
4771 int rc = SQLITE_OK;
4772 switch( i ){
4773 case 0: /* name */
4774 sqlite3_result_text(ctx, pCsr->cds.zFile, -1, SQLITE_TRANSIENT);
4775 break;
4776 case 1: /* mode */
4777 /* TODO: Whether or not the following is correct surely depends on
4778 ** the platform on which the archive was created. */
4779 sqlite3_result_int(ctx, pCsr->cds.iExternalAttr >> 16);
4780 break;
4781 case 2: { /* mtime */
4782 if( pCsr->flags & ZIPFILE_MTIME_VALID ){
4783 sqlite3_result_int64(ctx, pCsr->mTime);
4784 }else{
4785 sqlite3_result_int64(ctx, zipfileMtime(pCsr));
4786 }
4787 break;
4788 }
4789 case 3: { /* sz */
4790 if( sqlite3_vtab_nochange(ctx)==0 ){
4791 sqlite3_result_int64(ctx, pCsr->cds.szUncompressed);
4792 }
4793 break;
4794 }
4795 case 4: /* rawdata */
4796 if( sqlite3_vtab_nochange(ctx) ) break;
4797 case 5: { /* data */
4798 if( i==4 || pCsr->cds.iCompression==0 || pCsr->cds.iCompression==8 ){
4799 int sz = pCsr->cds.szCompressed;
4800 int szFinal = pCsr->cds.szUncompressed;
4801 if( szFinal>0 ){
4802 u8 *aBuf = sqlite3_malloc(sz);
4803 if( aBuf==0 ){
4804 rc = SQLITE_NOMEM;
 
4805 }else{
4806 FILE *pFile = zipfileGetFd(pCsr);
4807 rc = zipfileReadData(pFile, aBuf, sz, pCsr->iDataOff,
4808 &pCsr->base.pVtab->zErrMsg
4809 );
 
 
 
 
 
 
 
 
4810 }
4811 if( rc==SQLITE_OK ){
4812 if( i==5 && pCsr->cds.iCompression ){
4813 zipfileInflate(ctx, aBuf, sz, szFinal);
4814 }else{
4815 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
4816 }
4817 sqlite3_free(aBuf);
4818 }
 
4819 }else{
4820 /* Figure out if this is a directory or a zero-sized file. Consider
4821 ** it to be a directory either if the mode suggests so, or if
4822 ** the final character in the name is '/'. */
4823 u32 mode = pCsr->cds.iExternalAttr >> 16;
4824 if( !(mode & S_IFDIR) && pCsr->cds.zFile[pCsr->cds.nFile-1]!='/' ){
4825 sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
4826 }
4827 }
4828 }
4829 break;
4830 }
4831 case 6: /* method */
4832 sqlite3_result_int(ctx, pCsr->cds.iCompression);
4833 break;
4834 case 7: /* z */
 
4835 sqlite3_result_int64(ctx, pCsr->iId);
4836 break;
4837 }
4838
4839 return rc;
4840 }
4841
4842 /*
4843 ** Return the rowid for the current row.
4844 */
4845 static int zipfileRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
4846 assert( 0 );
4847 return SQLITE_OK;
4848 }
4849
4850 /*
4851 ** Return TRUE if the cursor has been moved off of the last
4852 ** row of output.
4853 */
4854 static int zipfileEof(sqlite3_vtab_cursor *cur){
4855 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4856 return pCsr->bEof;
4857 }
4858
4859 /*
 
 
 
 
 
 
 
 
4860 */
4861 static int zipfileReadEOCD(
4862 ZipfileTab *pTab, /* Return errors here */
4863 FILE *pFile, /* Read from this file */
 
 
4864 ZipfileEOCD *pEOCD /* Object to populate */
4865 ){
4866 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
4867 i64 szFile; /* Total size of file in bytes */
4868 int nRead; /* Bytes to read from file */
4869 i64 iOff; /* Offset to read from */
4870 int rc;
4871
4872 fseek(pFile, 0, SEEK_END);
4873 szFile = (i64)ftell(pFile);
4874 if( szFile==0 ){
4875 memset(pEOCD, 0, sizeof(ZipfileEOCD));
4876 return SQLITE_OK;
4877 }
4878 nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
4879 iOff = szFile - nRead;
4880
4881 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
 
 
 
 
 
 
4882 if( rc==SQLITE_OK ){
4883 int i;
4884
4885 /* Scan backwards looking for the signature bytes */
4886 for(i=nRead-20; i>=0; i--){
@@ -4902,21 +5145,63 @@
4902 pEOCD->iFirstDisk = zipfileRead16(aRead);
4903 pEOCD->nEntry = zipfileRead16(aRead);
4904 pEOCD->nEntryTotal = zipfileRead16(aRead);
4905 pEOCD->nSize = zipfileRead32(aRead);
4906 pEOCD->iOffset = zipfileRead32(aRead);
4907
4908 #if 0
4909 printf("iDisk=%d iFirstDisk=%d nEntry=%d "
4910 "nEntryTotal=%d nSize=%d iOffset=%d",
4911 (int)pEOCD->iDisk, (int)pEOCD->iFirstDisk, (int)pEOCD->nEntry,
4912 (int)pEOCD->nEntryTotal, (int)pEOCD->nSize, (int)pEOCD->iOffset
4913 );
4914 #endif
4915 }
4916
4917 return SQLITE_OK;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4918 }
4919
4920 /*
4921 ** xFilter callback.
4922 */
@@ -4925,33 +5210,41 @@
4925 int idxNum, const char *idxStr,
4926 int argc, sqlite3_value **argv
4927 ){
4928 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
4929 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4930 const char *zFile; /* Zip file to scan */
4931 int rc = SQLITE_OK; /* Return Code */
 
4932
4933 zipfileResetCursor(pCsr);
4934
4935 if( pTab->zFile ){
4936 zFile = pTab->zFile;
4937 }else if( idxNum==0 ){
4938 /* Error. This is an eponymous virtual table and the user has not
4939 ** supplied a file name. */
4940 zipfileSetErrmsg(pCsr, "table function zipfile() requires an argument");
4941 return SQLITE_ERROR;
 
 
 
 
 
 
 
 
 
4942 }else{
4943 zFile = (const char*)sqlite3_value_text(argv[0]);
4944 }
4945
4946 if( pTab->pWriteFd==0 ){
4947 pCsr->pFile = fopen(zFile, "rb");
4948 if( pCsr->pFile==0 ){
4949 zipfileSetErrmsg(pCsr, "cannot open file: %s", zFile);
4950 rc = SQLITE_ERROR;
4951 }else{
4952 rc = zipfileReadEOCD(pTab, pCsr->pFile, &pCsr->eocd);
4953 if( rc==SQLITE_OK ){
4954 if( pCsr->eocd.nEntry==0 ){
4955 pCsr->bEof = 1;
4956 }else{
4957 pCsr->iNextOff = pCsr->eocd.iOffset;
@@ -4958,16 +5251,13 @@
4958 rc = zipfileNext(cur);
4959 }
4960 }
4961 }
4962 }else{
4963 ZipfileEntry e;
4964 memset(&e, 0, sizeof(e));
4965 e.pNext = pTab->pFirstEntry;
4966 pCsr->pCurrent = &e;
4967 rc = zipfileNext(cur);
4968 assert( pCsr->pCurrent!=&e );
4969 }
4970
4971 return rc;
4972 }
4973
@@ -4999,226 +5289,115 @@
4999 }
5000
5001 return SQLITE_OK;
5002 }
5003
5004 /*
5005 ** Add object pNew to the end of the linked list that begins at
5006 ** ZipfileTab.pFirstEntry and ends with pLastEntry.
5007 */
5008 static void zipfileAddEntry(
5009 ZipfileTab *pTab,
5010 ZipfileEntry *pBefore,
5011 ZipfileEntry *pNew
5012 ){
5013 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5014 assert( pNew->pNext==0 );
5015 if( pBefore==0 ){
5016 if( pTab->pFirstEntry==0 ){
5017 pTab->pFirstEntry = pTab->pLastEntry = pNew;
5018 }else{
5019 assert( pTab->pLastEntry->pNext==0 );
5020 pTab->pLastEntry->pNext = pNew;
5021 pTab->pLastEntry = pNew;
5022 }
5023 }else{
5024 ZipfileEntry **pp;
5025 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5026 pNew->pNext = pBefore;
5027 *pp = pNew;
5028 }
5029 }
5030
5031 static int zipfileLoadDirectory(ZipfileTab *pTab){
5032 ZipfileEOCD eocd;
5033 int rc;
5034
5035 rc = zipfileReadEOCD(pTab, pTab->pWriteFd, &eocd);
5036 if( rc==SQLITE_OK && eocd.nEntry>0 ){
5037 int i;
5038 int iOff = 0;
5039 u8 *aBuf = sqlite3_malloc(eocd.nSize);
5040 if( aBuf==0 ){
5041 rc = SQLITE_NOMEM;
5042 }else{
5043 rc = zipfileReadData(
5044 pTab->pWriteFd, aBuf, eocd.nSize, eocd.iOffset, &pTab->base.zErrMsg
5045 );
5046 }
5047
5048 for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5049 u16 nFile;
5050 u16 nExtra;
5051 u16 nComment;
5052 ZipfileEntry *pNew;
5053 u8 *aRec = &aBuf[iOff];
5054
5055 nFile = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF]);
5056 nExtra = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF+2]);
5057 nComment = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF+4]);
5058
5059 pNew = sqlite3_malloc(
5060 sizeof(ZipfileEntry)
5061 + nFile+1
5062 + ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment
5063 );
5064 if( pNew==0 ){
5065 rc = SQLITE_NOMEM;
5066 }else{
5067 memset(pNew, 0, sizeof(ZipfileEntry));
5068 pNew->zPath = (char*)&pNew[1];
5069 memcpy(pNew->zPath, &aRec[ZIPFILE_CDS_FIXED_SZ], nFile);
5070 pNew->zPath[nFile] = '\0';
5071 pNew->aCdsEntry = (u8*)&pNew->zPath[nFile+1];
5072 pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
5073 memcpy(pNew->aCdsEntry, aRec, pNew->nCdsEntry);
5074 zipfileAddEntry(pTab, 0, pNew);
5075 }
5076
5077 iOff += ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
5078 }
5079
5080 sqlite3_free(aBuf);
5081 }
5082
5083 return rc;
5084 }
5085
5086 static ZipfileEntry *zipfileNewEntry(
5087 ZipfileCDS *pCds, /* Values for fixed size part of CDS */
5088 const char *zPath, /* Path for new entry */
5089 int nPath, /* strlen(zPath) */
5090 u32 mTime /* Modification time (or 0) */
5091 ){
5092 u8 *aWrite;
5093 ZipfileEntry *pNew;
5094 pCds->nFile = (u16)nPath;
5095 pCds->nExtra = mTime ? 9 : 0;
5096 pNew = (ZipfileEntry*)sqlite3_malloc(
5097 sizeof(ZipfileEntry) +
5098 nPath+1 +
5099 ZIPFILE_CDS_FIXED_SZ + nPath + pCds->nExtra
5100 );
5101
5102 if( pNew ){
5103 memset(pNew, 0, sizeof(ZipfileEntry));
5104 pNew->zPath = (char*)&pNew[1];
5105 pNew->aCdsEntry = (u8*)&pNew->zPath[nPath+1];
5106 pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ + nPath + pCds->nExtra;
5107 memcpy(pNew->zPath, zPath, nPath+1);
5108
5109 aWrite = pNew->aCdsEntry;
5110 zipfileWrite32(aWrite, ZIPFILE_SIGNATURE_CDS);
5111 zipfileWrite16(aWrite, pCds->iVersionMadeBy);
5112 zipfileWrite16(aWrite, pCds->iVersionExtract);
5113 zipfileWrite16(aWrite, pCds->flags);
5114 zipfileWrite16(aWrite, pCds->iCompression);
5115 zipfileWrite16(aWrite, pCds->mTime);
5116 zipfileWrite16(aWrite, pCds->mDate);
5117 zipfileWrite32(aWrite, pCds->crc32);
5118 zipfileWrite32(aWrite, pCds->szCompressed);
5119 zipfileWrite32(aWrite, pCds->szUncompressed);
5120 zipfileWrite16(aWrite, pCds->nFile);
5121 zipfileWrite16(aWrite, pCds->nExtra);
5122 zipfileWrite16(aWrite, pCds->nComment); assert( pCds->nComment==0 );
5123 zipfileWrite16(aWrite, pCds->iDiskStart);
5124 zipfileWrite16(aWrite, pCds->iInternalAttr);
5125 zipfileWrite32(aWrite, pCds->iExternalAttr);
5126 zipfileWrite32(aWrite, pCds->iOffset);
5127 assert( aWrite==&pNew->aCdsEntry[ZIPFILE_CDS_FIXED_SZ] );
5128 memcpy(aWrite, zPath, nPath);
5129 if( pCds->nExtra ){
5130 aWrite += nPath;
5131 zipfileWrite16(aWrite, ZIPFILE_EXTRA_TIMESTAMP);
5132 zipfileWrite16(aWrite, 5);
5133 *aWrite++ = 0x01;
5134 zipfileWrite32(aWrite, mTime);
5135 }
5136 }
5137
5138 return pNew;
5139 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5140
5141 static int zipfileAppendEntry(
5142 ZipfileTab *pTab,
5143 ZipfileCDS *pCds,
5144 const char *zPath, /* Path for new entry */
5145 int nPath, /* strlen(zPath) */
5146 const u8 *pData,
5147 int nData,
5148 u32 mTime
5149 ){
5150 u8 *aBuf = pTab->aBuffer;
 
5151 int rc;
5152
5153 zipfileWrite32(aBuf, ZIPFILE_SIGNATURE_LFH);
5154 zipfileWrite16(aBuf, pCds->iVersionExtract);
5155 zipfileWrite16(aBuf, pCds->flags);
5156 zipfileWrite16(aBuf, pCds->iCompression);
5157 zipfileWrite16(aBuf, pCds->mTime);
5158 zipfileWrite16(aBuf, pCds->mDate);
5159 zipfileWrite32(aBuf, pCds->crc32);
5160 zipfileWrite32(aBuf, pCds->szCompressed);
5161 zipfileWrite32(aBuf, pCds->szUncompressed);
5162 zipfileWrite16(aBuf, (u16)nPath);
5163 zipfileWrite16(aBuf, pCds->nExtra);
5164 assert( aBuf==&pTab->aBuffer[ZIPFILE_LFH_FIXED_SZ] );
5165 rc = zipfileAppendData(pTab, pTab->aBuffer, (int)(aBuf - pTab->aBuffer));
5166 if( rc==SQLITE_OK ){
5167 rc = zipfileAppendData(pTab, (const u8*)zPath, nPath);
5168 }
5169
5170 if( rc==SQLITE_OK && pCds->nExtra ){
5171 aBuf = pTab->aBuffer;
5172 zipfileWrite16(aBuf, ZIPFILE_EXTRA_TIMESTAMP);
5173 zipfileWrite16(aBuf, 5);
5174 *aBuf++ = 0x01;
5175 zipfileWrite32(aBuf, mTime);
5176 rc = zipfileAppendData(pTab, pTab->aBuffer, 9);
5177 }
5178
5179 if( rc==SQLITE_OK ){
5180 rc = zipfileAppendData(pTab, pData, nData);
5181 }
5182
5183 return rc;
5184 }
5185
5186 static int zipfileGetMode(
5187 ZipfileTab *pTab,
5188 sqlite3_value *pVal,
5189 u32 defaultMode, /* Value to use if pVal IS NULL */
5190 u32 *pMode
 
5191 ){
5192 const char *z = (const char*)sqlite3_value_text(pVal);
5193 u32 mode = 0;
5194 if( z==0 ){
5195 mode = defaultMode;
5196 }else if( z[0]>='0' && z[0]<='9' ){
5197 mode = (unsigned int)sqlite3_value_int(pVal);
5198 }else{
5199 const char zTemplate[11] = "-rwxrwxrwx";
5200 int i;
5201 if( strlen(z)!=10 ) goto parse_error;
5202 switch( z[0] ){
5203 case '-': mode |= S_IFREG; break;
5204 case 'd': mode |= S_IFDIR; break;
5205 #if !defined(_WIN32) && !defined(WIN32)
5206 case 'l': mode |= S_IFLNK; break;
5207 #endif
5208 default: goto parse_error;
5209 }
5210 for(i=1; i<10; i++){
5211 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
5212 else if( z[i]!='-' ) goto parse_error;
5213 }
5214 }
 
 
 
 
 
 
5215 *pMode = mode;
5216 return SQLITE_OK;
5217
5218 parse_error:
5219 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5220 return SQLITE_ERROR;
5221 }
5222
5223 /*
5224 ** Both (const char*) arguments point to nul-terminated strings. Argument
@@ -5229,10 +5408,72 @@
5229 if( zA[nA-1]=='/' ) nA--;
5230 if( zB[nB-1]=='/' ) nB--;
5231 if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5232 return 1;
5233 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5234
5235 /*
5236 ** xUpdate method.
5237 */
5238 static int zipfileUpdate(
@@ -5244,230 +5485,244 @@
5244 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5245 int rc = SQLITE_OK; /* Return Code */
5246 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
5247
5248 u32 mode = 0; /* Mode for new entry */
5249 i64 mTime = 0; /* Modification time for new entry */
5250 i64 sz = 0; /* Uncompressed size */
5251 const char *zPath = 0; /* Path for new entry */
5252 int nPath = 0; /* strlen(zPath) */
5253 const u8 *pData = 0; /* Pointer to buffer containing content */
5254 int nData = 0; /* Size of pData buffer in bytes */
5255 int iMethod = 0; /* Compression method for new entry */
5256 u8 *pFree = 0; /* Free this */
5257 char *zFree = 0; /* Also free this */
5258 ZipfileCDS cds; /* New Central Directory Structure entry */
5259 ZipfileEntry *pOld = 0;
5260 int bIsDir = 0;
5261 u32 iCrc32 = 0;
5262
5263 assert( pTab->zFile );
5264 assert( pTab->pWriteFd );
 
 
5265
 
5266 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5267 const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
5268 int nDelete = (int)strlen(zDelete);
5269 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
5270 if( pOld->bDeleted ) continue;
5271 if( zipfileComparePath(pOld->zPath, zDelete, nDelete)==0 ){
5272 pOld->bDeleted = 1;
5273 break;
5274 }
5275 assert( pOld->pNext );
5276 }
5277 if( nVal==1 ) return SQLITE_OK;
5278 }
5279
5280 /* Check that "sz" and "rawdata" are both NULL: */
5281 if( sqlite3_value_type(apVal[5])!=SQLITE_NULL
5282 || sqlite3_value_type(apVal[6])!=SQLITE_NULL
5283 ){
5284 rc = SQLITE_CONSTRAINT;
5285 }
5286
5287 if( rc==SQLITE_OK ){
5288 if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5289 /* data=NULL. A directory */
5290 bIsDir = 1;
5291 }else{
5292 /* Value specified for "data", and possibly "method". This must be
5293 ** a regular file or a symlink. */
5294 const u8 *aIn = sqlite3_value_blob(apVal[7]);
5295 int nIn = sqlite3_value_bytes(apVal[7]);
5296 int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5297
5298 iMethod = sqlite3_value_int(apVal[8]);
5299 sz = nIn;
5300 pData = aIn;
5301 nData = nIn;
5302 if( iMethod!=0 && iMethod!=8 ){
5303 rc = SQLITE_CONSTRAINT;
5304 }else{
5305 if( bAuto || iMethod ){
5306 int nCmp;
5307 rc = zipfileDeflate(pTab, aIn, nIn, &pFree, &nCmp);
5308 if( rc==SQLITE_OK ){
5309 if( iMethod || nCmp<nIn ){
5310 iMethod = 8;
5311 pData = pFree;
5312 nData = nCmp;
5313 }
5314 }
5315 }
5316 iCrc32 = crc32(0, aIn, nIn);
5317 }
5318 }
5319 }
5320
5321 if( rc==SQLITE_OK ){
5322 rc = zipfileGetMode(pTab, apVal[3],
5323 (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644)), &mode
5324 );
5325 if( rc==SQLITE_OK && (bIsDir == ((mode & S_IFDIR)==0)) ){
5326 /* The "mode" attribute is a directory, but data has been specified.
5327 ** Or vice-versa - no data but "mode" is a file or symlink. */
5328 rc = SQLITE_CONSTRAINT;
5329 }
5330 }
5331
5332 if( rc==SQLITE_OK ){
5333 zPath = (const char*)sqlite3_value_text(apVal[2]);
5334 nPath = (int)strlen(zPath);
5335 if( sqlite3_value_type(apVal[4])==SQLITE_NULL ){
5336 mTime = (sqlite3_int64)time(0);
5337 }else{
5338 mTime = sqlite3_value_int64(apVal[4]);
5339 }
5340 }
5341
5342 if( rc==SQLITE_OK && bIsDir ){
5343 /* For a directory, check that the last character in the path is a
5344 ** '/'. This appears to be required for compatibility with info-zip
5345 ** (the unzip command on unix). It does not create directories
5346 ** otherwise. */
5347 if( zPath[nPath-1]!='/' ){
5348 zFree = sqlite3_mprintf("%s/", zPath);
5349 if( zFree==0 ){ rc = SQLITE_NOMEM; }
5350 zPath = (const char*)zFree;
5351 nPath++;
5352 }
5353 }
5354
5355 /* Check that we're not inserting a duplicate entry */
5356 if( rc==SQLITE_OK ){
5357 ZipfileEntry *p;
5358 for(p=pTab->pFirstEntry; p; p=p->pNext){
5359 if( p->bDeleted ) continue;
5360 if( zipfileComparePath(p->zPath, zPath, nPath)==0 ){
5361 rc = SQLITE_CONSTRAINT;
5362 break;
5363 }
5364 }
5365 }
5366
5367 if( rc==SQLITE_OK ){
5368 /* Create the new CDS record. */
5369 memset(&cds, 0, sizeof(cds));
5370 cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5371 cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5372 cds.flags = ZIPFILE_NEWENTRY_FLAGS;
5373 cds.iCompression = (u16)iMethod;
5374 zipfileMtimeToDos(&cds, (u32)mTime);
5375 cds.crc32 = iCrc32;
5376 cds.szCompressed = nData;
5377 cds.szUncompressed = (u32)sz;
5378 cds.iExternalAttr = (mode<<16);
5379 cds.iOffset = (u32)pTab->szCurrent;
5380 pNew = zipfileNewEntry(&cds, zPath, nPath, (u32)mTime);
5381 if( pNew==0 ){
5382 rc = SQLITE_NOMEM;
5383 }else{
5384 zipfileAddEntry(pTab, pOld, pNew);
5385 }
5386 }
5387
5388 /* Append the new header+file to the archive */
5389 if( rc==SQLITE_OK ){
5390 rc = zipfileAppendEntry(pTab, &cds, zPath, nPath, pData, nData, (u32)mTime);
5391 }
5392
5393 if( rc!=SQLITE_OK && pOld ){
5394 pOld->bDeleted = 0;
5395 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5396 sqlite3_free(pFree);
5397 sqlite3_free(zFree);
5398 return rc;
5399 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5400
5401 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
5402 u8 *aBuf = pTab->aBuffer;
5403
5404 zipfileWrite32(aBuf, ZIPFILE_SIGNATURE_EOCD);
5405 zipfileWrite16(aBuf, p->iDisk);
5406 zipfileWrite16(aBuf, p->iFirstDisk);
5407 zipfileWrite16(aBuf, p->nEntry);
5408 zipfileWrite16(aBuf, p->nEntryTotal);
5409 zipfileWrite32(aBuf, p->nSize);
5410 zipfileWrite32(aBuf, p->iOffset);
5411 zipfileWrite16(aBuf, 0); /* Size of trailing comment in bytes*/
5412
5413 assert( (aBuf-pTab->aBuffer)==22 );
5414 return zipfileAppendData(pTab, pTab->aBuffer, (int)(aBuf - pTab->aBuffer));
5415 }
5416
5417 static void zipfileCleanupTransaction(ZipfileTab *pTab){
5418 ZipfileEntry *pEntry;
5419 ZipfileEntry *pNext;
5420
5421 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
5422 pNext = pEntry->pNext;
5423 sqlite3_free(pEntry);
5424 }
5425 pTab->pFirstEntry = 0;
5426 pTab->pLastEntry = 0;
5427 fclose(pTab->pWriteFd);
5428 pTab->pWriteFd = 0;
5429 pTab->szCurrent = 0;
5430 pTab->szOrig = 0;
5431 }
5432
5433 static int zipfileBegin(sqlite3_vtab *pVtab){
5434 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5435 int rc = SQLITE_OK;
5436
5437 assert( pTab->pWriteFd==0 );
5438
5439 /* This table is only writable if a default archive path was specified
5440 ** as part of the CREATE VIRTUAL TABLE statement. */
5441 if( pTab->zFile==0 ){
5442 pTab->base.zErrMsg = sqlite3_mprintf(
5443 "zipfile: writing requires a default archive"
5444 );
5445 return SQLITE_ERROR;
5446 }
5447
5448 /* Open a write fd on the file. Also load the entire central directory
5449 ** structure into memory. During the transaction any new file data is
5450 ** appended to the archive file, but the central directory is accumulated
5451 ** in main-memory until the transaction is committed. */
5452 pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5453 if( pTab->pWriteFd==0 ){
5454 pTab->base.zErrMsg = sqlite3_mprintf(
5455 "zipfile: failed to open file %s for writing", pTab->zFile
5456 );
5457 rc = SQLITE_ERROR;
5458 }else{
5459 fseek(pTab->pWriteFd, 0, SEEK_END);
5460 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5461 rc = zipfileLoadDirectory(pTab);
 
 
5462 }
5463
5464 if( rc!=SQLITE_OK ){
5465 zipfileCleanupTransaction(pTab);
5466 }
5467
5468 return rc;
5469 }
5470
5471 static int zipfileCommit(sqlite3_vtab *pVtab){
5472 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5473 int rc = SQLITE_OK;
@@ -5475,14 +5730,14 @@
5475 i64 iOffset = pTab->szCurrent;
5476 ZipfileEntry *p;
5477 ZipfileEOCD eocd;
5478 int nEntry = 0;
5479
5480 /* Write out all undeleted entries */
5481 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
5482 if( p->bDeleted ) continue;
5483 rc = zipfileAppendData(pTab, p->aCdsEntry, p->nCdsEntry);
5484 nEntry++;
5485 }
5486
5487 /* Write out the EOCD record */
5488 eocd.iDisk = 0;
@@ -5519,11 +5774,11 @@
5519 ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
5520 assert( argc>0 );
5521
5522 pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
5523 if( pCsr ){
5524 ZipfileCDS *p = &pCsr->cds;
5525 char *zRes = sqlite3_mprintf("{"
5526 "\"version-made-by\" : %u, "
5527 "\"version-to-extract\" : %u, "
5528 "\"flags\" : %u, "
5529 "\"compression\" : %u, "
@@ -5556,11 +5811,10 @@
5556 sqlite3_free(zRes);
5557 }
5558 }
5559 }
5560
5561
5562 /*
5563 ** xFindFunction method.
5564 */
5565 static int zipfileFindFunction(
5566 sqlite3_vtab *pVtab, /* Virtual table handle */
@@ -5567,20 +5821,259 @@
5567 int nArg, /* Number of SQL function arguments */
5568 const char *zName, /* Name of SQL function */
5569 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
5570 void **ppArg /* OUT: User data for *pxFunc */
5571 ){
5572 if( nArg>0 ){
5573 if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
5574 *pxFunc = zipfileFunctionCds;
5575 *ppArg = (void*)pVtab;
5576 return 1;
5577 }
5578 }
5579
5580 return 0;
5581 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5582
5583 /*
5584 ** Register the "zipfile" virtual table.
5585 */
5586 static int zipfileRegister(sqlite3 *db){
@@ -5595,11 +6088,11 @@
5595 zipfileClose, /* xClose - close a cursor */
5596 zipfileFilter, /* xFilter - configure scan constraints */
5597 zipfileNext, /* xNext - advance a cursor */
5598 zipfileEof, /* xEof - check for end of scan */
5599 zipfileColumn, /* xColumn - read data */
5600 zipfileRowid, /* xRowid - read data */
5601 zipfileUpdate, /* xUpdate */
5602 zipfileBegin, /* xBegin */
5603 0, /* xSync */
5604 zipfileCommit, /* xCommit */
5605 zipfileRollback, /* xRollback */
@@ -5606,12 +6099,15 @@
5606 zipfileFindFunction, /* xFindMethod */
5607 0, /* xRename */
5608 };
5609
5610 int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
 
5611 if( rc==SQLITE_OK ){
5612 rc = sqlite3_overload_function(db, "zipfile_cds", -1);
 
 
5613 }
5614 return rc;
5615 }
5616 #else /* SQLITE_OMIT_VIRTUALTABLE */
5617 # define zipfileRegister(x) SQLITE_OK
@@ -7976,10 +8472,11 @@
7976 */
7977 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
7978 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
7979 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
7980 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
 
7981
7982 /*
7983 ** These are the allowed shellFlgs values
7984 */
7985 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
@@ -8082,10 +8579,11 @@
8082 ** If the EDITOR argument is omitted, use the value in the VISUAL
8083 ** environment variable. If still there is no EDITOR, through an error.
8084 **
8085 ** Also throw an error if the EDITOR program returns a non-zero exit code.
8086 */
 
8087 static void editFunc(
8088 sqlite3_context *context,
8089 int argc,
8090 sqlite3_value **argv
8091 ){
@@ -8179,22 +8677,24 @@
8179 if( x!=sz ){
8180 sqlite3_result_error(context, "could not read back the whole file", -1);
8181 goto edit_func_end;
8182 }
8183 if( bBin ){
8184 sqlite3_result_blob(context, p, sz, sqlite3_free);
8185 }else{
8186 sqlite3_result_text(context, (const char*)p, sz, sqlite3_free);
 
8187 }
8188 p = 0;
8189
8190 edit_func_end:
8191 if( f ) fclose(f);
8192 unlink(zTempFile);
8193 sqlite3_free(zTempFile);
8194 sqlite3_free(p);
8195 }
 
8196
8197 /*
8198 ** Save or restore the current output mode
8199 */
8200 static void outputModePush(ShellState *p){
@@ -9176,33 +9676,58 @@
9176 ShellState *pArg, /* Pointer to ShellState */
9177 int bReset /* True to reset the stats */
9178 ){
9179 int iCur;
9180 int iHiwtr;
9181
9182 if( pArg && pArg->out ){
9183 displayStatLine(pArg, "Memory Used:",
9184 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
9185 displayStatLine(pArg, "Number of Outstanding Allocations:",
9186 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
9187 if( pArg->shellFlgs & SHFLG_Pagecache ){
9188 displayStatLine(pArg, "Number of Pcache Pages Used:",
9189 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
9190 }
9191 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
9192 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
9193 displayStatLine(pArg, "Largest Allocation:",
9194 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
9195 displayStatLine(pArg, "Largest Pcache Allocation:",
9196 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9197 #ifdef YYTRACKMAXSTACKDEPTH
9198 displayStatLine(pArg, "Deepest Parser Stack:",
9199 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
9200 #endif
9201 }
9202
9203 if( pArg && pArg->out && db ){
9204 if( pArg->shellFlgs & SHFLG_Lookaside ){
9205 iHiwtr = iCur = -1;
9206 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
9207 &iCur, &iHiwtr, bReset);
9208 raw_printf(pArg->out,
@@ -9233,29 +9758,38 @@
9233 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
9234 iHiwtr = iCur = -1;
9235 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
9236 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
9237 iHiwtr = iCur = -1;
 
 
 
9238 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
9239 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
9240 iCur);
9241 iHiwtr = iCur = -1;
9242 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
9243 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
9244 iCur);
9245 }
9246
9247 if( pArg && pArg->out && db && pArg->pStmt ){
9248 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
9249 bReset);
9250 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
9251 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
9252 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
9253 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
9254 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
9255 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
9256 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
 
 
 
 
 
 
9257 }
9258
9259 #ifdef __linux__
9260 displayLinuxIoStats(pArg->out);
9261 #endif
@@ -9467,69 +10001,61 @@
9467 /*
9468 ** Run a prepared statement
9469 */
9470 static void exec_prepared_stmt(
9471 ShellState *pArg, /* Pointer to ShellState */
9472 sqlite3_stmt *pStmt, /* Statment to run */
9473 int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */
9474 ){
9475 int rc;
9476
9477 /* perform the first step. this will tell us if we
9478 ** have a result set or not and how wide it is.
9479 */
9480 rc = sqlite3_step(pStmt);
9481 /* if we have a result set... */
9482 if( SQLITE_ROW == rc ){
9483 /* if we have a callback... */
9484 if( xCallback ){
9485 /* allocate space for col name ptr, value ptr, and type */
9486 int nCol = sqlite3_column_count(pStmt);
9487 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
9488 if( !pData ){
9489 rc = SQLITE_NOMEM;
9490 }else{
9491 char **azCols = (char **)pData; /* Names of result columns */
9492 char **azVals = &azCols[nCol]; /* Results */
9493 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
9494 int i, x;
9495 assert(sizeof(int) <= sizeof(char *));
9496 /* save off ptrs to column names */
9497 for(i=0; i<nCol; i++){
9498 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
9499 }
9500 do{
9501 /* extract the data and data types */
9502 for(i=0; i<nCol; i++){
9503 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
9504 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
9505 azVals[i] = "";
9506 }else{
9507 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
9508 }
9509 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
9510 rc = SQLITE_NOMEM;
9511 break; /* from for */
9512 }
9513 } /* end for */
9514
9515 /* if data and types extracted successfully... */
9516 if( SQLITE_ROW == rc ){
9517 /* call the supplied callback with the result row data */
9518 if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
9519 rc = SQLITE_ABORT;
9520 }else{
9521 rc = sqlite3_step(pStmt);
9522 }
9523 }
9524 } while( SQLITE_ROW == rc );
9525 sqlite3_free(pData);
9526 }
9527 }else{
9528 do{
9529 rc = sqlite3_step(pStmt);
9530 } while( rc == SQLITE_ROW );
9531 }
9532 }
9533 }
9534
9535 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -9671,21 +10197,19 @@
9671 ** This is very similar to SQLite's built-in sqlite3_exec()
9672 ** function except it takes a slightly different callback
9673 ** and callback data argument.
9674 */
9675 static int shell_exec(
9676 sqlite3 *db, /* An open database */
9677 const char *zSql, /* SQL to be evaluated */
9678 int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
9679 /* (not the same as sqlite3_exec) */
9680 ShellState *pArg, /* Pointer to ShellState */
 
9681 char **pzErrMsg /* Error msg written here */
9682 ){
9683 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
9684 int rc = SQLITE_OK; /* Return Code */
9685 int rc2;
9686 const char *zLeftover; /* Tail of unprocessed SQL */
 
9687
9688 if( pzErrMsg ){
9689 *pzErrMsg = NULL;
9690 }
9691
@@ -9752,11 +10276,11 @@
9752 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
9753 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
9754 if( rc==SQLITE_OK ){
9755 pArg->cMode = MODE_Explain;
9756 explain_data_prepare(pArg, pExplain);
9757 exec_prepared_stmt(pArg, pExplain, xCallback);
9758 explain_data_delete(pArg);
9759 }
9760 sqlite3_finalize(pExplain);
9761 sqlite3_free(zEQP);
9762 }
@@ -9778,11 +10302,11 @@
9778 if( pArg->cMode==MODE_Explain ){
9779 explain_data_prepare(pArg, pStmt);
9780 }
9781 }
9782
9783 exec_prepared_stmt(pArg, pStmt, xCallback);
9784 explain_data_delete(pArg);
9785
9786 /* print usage stats if stats on */
9787 if( pArg && pArg->statsOn ){
9788 display_stats(db, pArg, 0);
@@ -10041,15 +10565,15 @@
10041
10042 savedDestTable = p->zDestTable;
10043 savedMode = p->mode;
10044 p->zDestTable = sTable.z;
10045 p->mode = p->cMode = MODE_Insert;
10046 rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0);
10047 if( (rc&0xff)==SQLITE_CORRUPT ){
10048 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
10049 toggleSelectOrder(p->db);
10050 shell_exec(p->db, sSelect.z, shell_callback, p, 0);
10051 toggleSelectOrder(p->db);
10052 }
10053 p->zDestTable = savedDestTable;
10054 p->mode = savedMode;
10055 freeText(&sTable);
@@ -10162,10 +10686,11 @@
10162 ".once (-e|-x|FILE) Output for the next SQL command only to FILE\n"
10163 " or invoke system text editor (-e) or spreadsheet (-x)\n"
10164 " on the output.\n"
10165 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
10166 " The --new option starts with an empty file\n"
 
10167 ".output ?FILE? Send output to FILE or stdout\n"
10168 ".print STRING... Print literal STRING\n"
10169 ".prompt MAIN CONTINUE Replace the standard prompts\n"
10170 ".quit Exit this program\n"
10171 ".read FILENAME Execute SQL in FILENAME\n"
@@ -10179,14 +10704,18 @@
10179 " separator for both the output mode and .import\n"
10180 #if defined(SQLITE_ENABLE_SESSION)
10181 ".session CMD ... Create or control sessions\n"
10182 #endif
10183 ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n"
 
10184 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
 
10185 ".show Show the current values for various settings\n"
10186 ".stats ?on|off? Show stats or turn stats on or off\n"
 
10187 ".system CMD ARGS... Run CMD ARGS... in a system shell\n"
 
10188 ".tables ?TABLE? List names of tables\n"
10189 " If TABLE specified, only list tables matching\n"
10190 " LIKE pattern TABLE.\n"
10191 ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n"
10192 ".timeout MS Try opening locked tables for MS milliseconds\n"
@@ -10311,17 +10840,25 @@
10311 #endif
10312
10313 /*
10314 ** Try to deduce the type of file for zName based on its content. Return
10315 ** one of the SHELL_OPEN_* constants.
 
 
 
 
 
10316 */
10317 static int deduceDatabaseType(const char *zName){
10318 FILE *f = fopen(zName, "rb");
10319 size_t n;
10320 int rc = SHELL_OPEN_UNSPEC;
10321 char zBuf[100];
10322 if( f==0 ) return SHELL_OPEN_NORMAL;
 
 
 
10323 fseek(f, -25, SEEK_END);
10324 n = fread(zBuf, 25, 1, f);
10325 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
10326 rc = SHELL_OPEN_APPENDVFS;
10327 }else{
@@ -10328,10 +10865,12 @@
10328 fseek(f, -22, SEEK_END);
10329 n = fread(zBuf, 22, 1, f);
10330 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
10331 && zBuf[3]==0x06 ){
10332 rc = SHELL_OPEN_ZIPFILE;
 
 
10333 }
10334 }
10335 fclose(f);
10336 return rc;
10337 }
@@ -10342,11 +10881,11 @@
10342 */
10343 static void open_db(ShellState *p, int keepAlive){
10344 if( p->db==0 ){
10345 sqlite3_initialize();
10346 if( p->openMode==SHELL_OPEN_UNSPEC && access(p->zDbFilename,0)==0 ){
10347 p->openMode = deduceDatabaseType(p->zDbFilename);
10348 }
10349 switch( p->openMode ){
10350 case SHELL_OPEN_APPENDVFS: {
10351 sqlite3_open_v2(p->zDbFilename, &p->db,
10352 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
@@ -10353,10 +10892,14 @@
10353 break;
10354 }
10355 case SHELL_OPEN_ZIPFILE: {
10356 sqlite3_open(":memory:", &p->db);
10357 break;
 
 
 
 
10358 }
10359 case SHELL_OPEN_UNSPEC:
10360 case SHELL_OPEN_NORMAL: {
10361 sqlite3_open(p->zDbFilename, &p->db);
10362 break;
@@ -10383,14 +10926,16 @@
10383 shellAddSchemaName, 0, 0);
10384 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
10385 shellModuleSchema, 0, 0);
10386 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
10387 shellPutsFunc, 0, 0);
 
10388 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
10389 editFunc, 0, 0);
10390 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
10391 editFunc, 0, 0);
 
10392 if( p->openMode==SHELL_OPEN_ZIPFILE ){
10393 char *zSql = sqlite3_mprintf(
10394 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
10395 sqlite3_exec(p->db, zSql, 0, 0, 0);
10396 sqlite3_free(zSql);
@@ -11001,10 +11546,11 @@
11001 #ifndef SQLITE_OMIT_POPEN
11002 pclose(p->out);
11003 #endif
11004 }else{
11005 output_file_close(p->out);
 
11006 if( p->doXdgOpen ){
11007 const char *zXdgOpenCmd =
11008 #if defined(_WIN32)
11009 "start";
11010 #elif defined(__APPLE__)
@@ -11019,10 +11565,11 @@
11019 }
11020 sqlite3_free(zCmd);
11021 outputModePop(p);
11022 p->doXdgOpen = 0;
11023 }
 
11024 }
11025 p->outfile[0] = 0;
11026 p->out = stdout;
11027 }
11028
@@ -12129,44 +12676,84 @@
12129 " mtime INT, -- last modification time\n"
12130 " sz INT, -- original file size\n"
12131 " data BLOB -- compressed content\n"
12132 ")";
12133 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
12134 const char *zInsertFmt =
12135 "REPLACE INTO sqlar(name,mode,mtime,sz,data)\n"
12136 " SELECT\n"
12137 " %s,\n"
12138 " mode,\n"
12139 " mtime,\n"
12140 " CASE substr(lsmode(mode),1,1)\n"
12141 " WHEN '-' THEN length(data)\n"
12142 " WHEN 'd' THEN 0\n"
12143 " ELSE -1 END,\n"
12144 " CASE WHEN lsmode(mode) LIKE 'd%%' THEN NULL else data END\n"
 
 
 
 
 
 
 
 
12145 " FROM fsdir(%Q,%Q)\n"
12146 " WHERE lsmode(mode) NOT LIKE '?%%';";
 
12147 int i; /* For iterating through azFile[] */
12148 int rc; /* Return code */
 
 
 
12149
 
12150 rc = arExecSql(pAr, "SAVEPOINT ar;");
12151 if( rc!=SQLITE_OK ) return rc;
12152 if( bUpdate==0 ){
12153 rc = arExecSql(pAr, zDrop);
12154 if( rc!=SQLITE_OK ) return rc;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12155 }
12156 rc = arExecSql(pAr, zCreate);
12157 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
12158 char *zSql = sqlite3_mprintf(zInsertFmt,
12159 pAr->bVerbose ? "shell_putsnl(name)" : "name",
12160 pAr->azArg[i], pAr->zDir);
12161 rc = arExecSql(pAr, zSql);
12162 sqlite3_free(zSql);
12163 }
 
12164 if( rc!=SQLITE_OK ){
12165 arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
12166 }else{
12167 rc = arExecSql(pAr, "RELEASE ar;");
 
 
 
 
 
12168 }
12169 return rc;
12170 }
12171
12172 /*
@@ -12184,24 +12771,21 @@
12184 if( rc==SQLITE_OK ){
12185 int eDbType = SHELL_OPEN_UNSPEC;
12186 cmd.p = pState;
12187 cmd.db = pState->db;
12188 if( cmd.zFile ){
12189 eDbType = deduceDatabaseType(cmd.zFile);
12190 }else{
12191 eDbType = pState->openMode;
12192 }
12193 if( eDbType==SHELL_OPEN_ZIPFILE ){
12194 if( cmd.zFile==0 ){
12195 cmd.zSrcTable = sqlite3_mprintf("zip");
12196 }else{
12197 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
12198 }
12199 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
12200 utf8_printf(stderr, "zip archives are read-only\n");
12201 rc = SQLITE_ERROR;
12202 goto end_ar_command;
12203 }
12204 cmd.bZip = 1;
12205 }else if( cmd.zFile ){
12206 int flags;
12207 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
@@ -12222,18 +12806,16 @@
12222 cmd.zFile, sqlite3_errmsg(cmd.db)
12223 );
12224 goto end_ar_command;
12225 }
12226 sqlite3_fileio_init(cmd.db, 0, 0);
12227 #ifdef SQLITE_HAVE_ZLIB
12228 sqlite3_sqlar_init(cmd.db, 0, 0);
12229 #endif
12230 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
12231 shellPutsFunc, 0, 0);
12232
12233 }
12234 if( cmd.zSrcTable==0 ){
12235 if( cmd.eCmd!=AR_CMD_CREATE
12236 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
12237 ){
12238 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
12239 rc = SQLITE_ERROR;
@@ -12622,11 +13204,11 @@
12622 if( strcmp(azArg[1],"full")==0 ){
12623 p->autoEQP = AUTOEQP_full;
12624 }else if( strcmp(azArg[1],"trigger")==0 ){
12625 p->autoEQP = AUTOEQP_trigger;
12626 }else{
12627 p->autoEQP = booleanValue(azArg[1]);
12628 }
12629 }else{
12630 raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
12631 rc = 1;
12632 }
@@ -12709,18 +13291,15 @@
12709 raw_printf(p->out, "ANALYZE sqlite_master;\n");
12710 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
12711 callback, &data, &zErrMsg);
12712 data.cMode = data.mode = MODE_Insert;
12713 data.zDestTable = "sqlite_stat1";
12714 shell_exec(p->db, "SELECT * FROM sqlite_stat1",
12715 shell_callback, &data,&zErrMsg);
12716 data.zDestTable = "sqlite_stat3";
12717 shell_exec(p->db, "SELECT * FROM sqlite_stat3",
12718 shell_callback, &data,&zErrMsg);
12719 data.zDestTable = "sqlite_stat4";
12720 shell_exec(p->db, "SELECT * FROM sqlite_stat4",
12721 shell_callback, &data, &zErrMsg);
12722 raw_printf(p->out, "ANALYZE sqlite_master;\n");
12723 }
12724 }else
12725
12726 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
@@ -13197,16 +13776,18 @@
13197 /* Check for command-line arguments */
13198 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
13199 const char *z = azArg[iName];
13200 if( optionMatch(z,"new") ){
13201 newFlag = 1;
13202 #ifdef SQLITE_HAVE_ZIP
13203 }else if( optionMatch(z, "zip") ){
13204 p->openMode = SHELL_OPEN_ZIPFILE;
13205 #endif
13206 }else if( optionMatch(z, "append") ){
13207 p->openMode = SHELL_OPEN_APPENDVFS;
 
 
13208 }else if( z[0]=='-' ){
13209 utf8_printf(stderr, "unknown option: %s\n", z);
13210 rc = 1;
13211 goto meta_command_exit;
13212 }
@@ -13259,10 +13840,11 @@
13259 }else{
13260 p->outCount = 0;
13261 }
13262 output_reset(p);
13263 if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
 
13264 if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
13265 p->doXdgOpen = 1;
13266 outputModePush(p);
13267 if( zFile[1]=='x' ){
13268 newTempFile(p, "csv");
@@ -13273,10 +13855,11 @@
13273 newTempFile(p, "txt");
13274 bTxtMode = 1;
13275 }
13276 zFile = p->zTempFile;
13277 }
 
13278 if( zFile[0]=='|' ){
13279 #ifdef SQLITE_OMIT_POPEN
13280 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
13281 rc = 1;
13282 p->out = stdout;
@@ -13392,14 +13975,13 @@
13392 rc = 1;
13393 }
13394 sqlite3_close(pSrc);
13395 }else
13396
13397
13398 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
13399 if( nArg==2 ){
13400 p->scanstatsOn = booleanValue(azArg[1]);
13401 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
13402 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
13403 #endif
13404 }else{
13405 raw_printf(stderr, "Usage: .scanstats on|off\n");
@@ -13983,15 +14565,16 @@
13983 freeText(&sQuery);
13984 freeText(&sSql);
13985 if( bDebug ){
13986 utf8_printf(p->out, "%s\n", zSql);
13987 }else{
13988 shell_exec(p->db, zSql, shell_callback, p, 0);
13989 }
13990 sqlite3_free(zSql);
13991 }else
13992
 
13993 if( c=='s'
13994 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
13995 ){
13996 char *zCmd;
13997 int i, x;
@@ -14007,10 +14590,11 @@
14007 }
14008 x = system(zCmd);
14009 sqlite3_free(zCmd);
14010 if( x ) raw_printf(stderr, "System command returns %d\n", x);
14011 }else
 
14012
14013 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
14014 static const char *azBool[] = { "off", "on", "trigger", "full"};
14015 int i;
14016 if( nArg!=1 ){
@@ -14046,11 +14630,11 @@
14046 p->zDbFilename ? p->zDbFilename : "");
14047 }else
14048
14049 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
14050 if( nArg==2 ){
14051 p->statsOn = booleanValue(azArg[1]);
14052 }else if( nArg==1 ){
14053 display_stats(p->db, p, 0);
14054 }else{
14055 raw_printf(stderr, "Usage: .stats ?on|off?\n");
14056 rc = 1;
@@ -14586,10 +15170,20 @@
14586 return 1; /* SQL Server */
14587 }
14588 return 0;
14589 }
14590
 
 
 
 
 
 
 
 
 
 
14591 /*
14592 ** Return true if zSql is a complete SQL statement. Return false if it
14593 ** ends in the middle of a string literal or C-style comment.
14594 */
14595 static int line_is_complete(char *zSql, int nSql){
@@ -14610,11 +15204,11 @@
14610 char *zErrMsg = 0;
14611
14612 open_db(p, 0);
14613 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
14614 BEGIN_TIMER;
14615 rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
14616 END_TIMER;
14617 if( rc || zErrMsg ){
14618 char zPrefix[100];
14619 if( in!=0 || !stdin_is_interactive ){
14620 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
@@ -14842,10 +15436,14 @@
14842
14843 /*
14844 ** Show available command line options
14845 */
14846 static const char zOptions[] =
 
 
 
 
14847 " -ascii set output mode to 'ascii'\n"
14848 " -bail stop after hitting an error\n"
14849 " -batch force batch I/O\n"
14850 " -column set output mode to 'column'\n"
14851 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
@@ -14868,17 +15466,21 @@
14868 #endif
14869 " -newline SEP set output row separator. Default: '\\n'\n"
14870 " -nullvalue TEXT set text string for NULL values. Default ''\n"
14871 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
14872 " -quote set output mode to 'quote'\n"
 
14873 " -separator SEP set output column separator. Default: '|'\n"
14874 " -stats print memory stats before each finalize\n"
14875 " -version show SQLite version\n"
14876 " -vfs NAME use NAME as the default VFS\n"
14877 #ifdef SQLITE_ENABLE_VFSTRACE
14878 " -vfstrace enable tracing of all VFS calls\n"
14879 #endif
 
 
 
14880 ;
14881 static void usage(int showDetail){
14882 utf8_printf(stderr,
14883 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
14884 "FILENAME is the name of an SQLite database. A new database is created\n"
@@ -14977,25 +15579,43 @@
14977 sqlite3_sourceid(), SQLITE_SOURCE_ID);
14978 exit(1);
14979 }
14980 #endif
14981 main_init(&data);
 
 
 
 
 
 
 
14982 #if !SQLITE_SHELL_IS_UTF8
14983 sqlite3_initialize();
14984 argv = sqlite3_malloc64(sizeof(argv[0])*argc);
14985 if( argv==0 ){
14986 raw_printf(stderr, "out of memory\n");
14987 exit(1);
14988 }
14989 for(i=0; i<argc; i++){
14990 argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]);
 
 
 
 
 
 
 
14991 if( argv[i]==0 ){
14992 raw_printf(stderr, "out of memory\n");
14993 exit(1);
14994 }
 
 
14995 }
 
14996 #endif
 
14997 assert( argc>=1 && argv && argv[0] );
14998 Argv0 = argv[0];
14999
15000 /* Make sure we have a valid signal handler early, before anything
15001 ** else is done.
@@ -15111,16 +15731,24 @@
15111 sqlite3_vfs_register(pVfs, 1);
15112 }else{
15113 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
15114 exit(1);
15115 }
15116 #ifdef SQLITE_HAVE_ZIP
15117 }else if( strcmp(z,"-zip")==0 ){
15118 data.openMode = SHELL_OPEN_ZIPFILE;
15119 #endif
15120 }else if( strcmp(z,"-append")==0 ){
15121 data.openMode = SHELL_OPEN_APPENDVFS;
 
 
 
 
 
 
 
 
15122 }
15123 }
15124 if( data.zDbFilename==0 ){
15125 #ifndef SQLITE_OMIT_MEMORYDB
15126 data.zDbFilename = ":memory:";
@@ -15170,11 +15798,11 @@
15170 }else if( strcmp(z,"-column")==0 ){
15171 data.mode = MODE_Column;
15172 }else if( strcmp(z,"-csv")==0 ){
15173 data.mode = MODE_Csv;
15174 memcpy(data.colSeparator,",",2);
15175 #ifdef SQLITE_HAVE_ZIP
15176 }else if( strcmp(z,"-zip")==0 ){
15177 data.openMode = SHELL_OPEN_ZIPFILE;
15178 #endif
15179 }else if( strcmp(z,"-append")==0 ){
15180 data.openMode = SHELL_OPEN_APPENDVFS;
@@ -15253,19 +15881,36 @@
15253 if( z[0]=='.' ){
15254 rc = do_meta_command(z, &data);
15255 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
15256 }else{
15257 open_db(&data, 0);
15258 rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
15259 if( zErrMsg!=0 ){
15260 utf8_printf(stderr,"Error: %s\n", zErrMsg);
15261 if( bail_on_error ) return rc!=0 ? rc : 1;
15262 }else if( rc!=0 ){
15263 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
15264 if( bail_on_error ) return rc;
15265 }
15266 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15267 }else{
15268 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
15269 raw_printf(stderr,"Use -help for a list of options.\n");
15270 return 1;
15271 }
@@ -15281,11 +15926,11 @@
15281 if( azCmd[i][0]=='.' ){
15282 rc = do_meta_command(azCmd[i], &data);
15283 if( rc ) return rc==2 ? 0 : rc;
15284 }else{
15285 open_db(&data, 0);
15286 rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg);
15287 if( zErrMsg!=0 ){
15288 utf8_printf(stderr,"Error: %s\n", zErrMsg);
15289 return rc!=0 ? rc : 1;
15290 }else if( rc!=0 ){
15291 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
@@ -15344,10 +15989,10 @@
15344 find_home_dir(1);
15345 output_reset(&data);
15346 data.doXdgOpen = 0;
15347 clearTempFile(&data);
15348 #if !SQLITE_SHELL_IS_UTF8
15349 for(i=0; i<argc; i++) sqlite3_free(argv[i]);
15350 sqlite3_free(argv);
15351 #endif
15352 return rc;
15353 }
15354
--- src/shell.c
+++ src/shell.c
@@ -995,11 +995,14 @@
995
996 /*
997 ** We need several data types from the Windows SDK header.
998 */
999
1000 #ifndef WIN32_LEAN_AND_MEAN
1001 #define WIN32_LEAN_AND_MEAN
1002 #endif
1003
1004 #include "windows.h"
1005
1006 /*
1007 ** We need several support functions from the SQLite core.
1008 */
@@ -2314,19 +2317,24 @@
2317 FILETIME lastAccess;
2318 FILETIME lastWrite;
2319 SYSTEMTIME currentTime;
2320 LONGLONG intervals;
2321 HANDLE hFile;
2322 LPWSTR zUnicodeName;
2323 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2324
2325 GetSystemTime(&currentTime);
2326 SystemTimeToFileTime(&currentTime, &lastAccess);
2327 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2328 lastWrite.dwLowDateTime = (DWORD)intervals;
2329 lastWrite.dwHighDateTime = intervals >> 32;
2330 zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2331 hFile = CreateFileW(
2332 zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2333 FILE_FLAG_BACKUP_SEMANTICS, NULL
2334 );
2335 sqlite3_free(zUnicodeName);
2336 if( hFile!=INVALID_HANDLE_VALUE ){
2337 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2338 CloseHandle(hFile);
2339 return !bResult;
2340 }else{
@@ -2530,14 +2538,16 @@
2538 FsdirLevel *pLvl = &pCur->aLvl[i];
2539 if( pLvl->pDir ) closedir(pLvl->pDir);
2540 sqlite3_free(pLvl->zDir);
2541 }
2542 sqlite3_free(pCur->zPath);
2543 sqlite3_free(pCur->aLvl);
2544 pCur->aLvl = 0;
2545 pCur->zPath = 0;
2546 pCur->zBase = 0;
2547 pCur->nBase = 0;
2548 pCur->nLvl = 0;
2549 pCur->iLvl = -1;
2550 pCur->iRowid = 1;
2551 }
2552
2553 /*
@@ -2545,11 +2555,10 @@
2555 */
2556 static int fsdirClose(sqlite3_vtab_cursor *cur){
2557 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2558
2559 fsdirResetCursor(pCur);
 
2560 sqlite3_free(pCur);
2561 return SQLITE_OK;
2562 }
2563
2564 /*
@@ -3992,33 +4001,52 @@
4001 SQLITE_EXTENSION_INIT1
4002 #include <stdio.h>
4003 #include <string.h>
4004 #include <assert.h>
4005
 
 
 
 
 
 
 
 
 
 
 
 
 
4006 #include <zlib.h>
4007
4008 #ifndef SQLITE_OMIT_VIRTUALTABLE
4009
4010 #ifndef SQLITE_AMALGAMATION
4011
4012 /* typedef sqlite3_int64 i64; */
4013 /* typedef unsigned char u8; */
4014 typedef unsigned short u16;
4015 typedef unsigned long u32;
4016 #define MIN(a,b) ((a)<(b) ? (a) : (b))
4017
4018 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
4019 # define ALWAYS(X) (1)
4020 # define NEVER(X) (0)
4021 #elif !defined(NDEBUG)
4022 # define ALWAYS(X) ((X)?1:(assert(0),0))
4023 # define NEVER(X) ((X)?(assert(0),1):0)
4024 #else
4025 # define ALWAYS(X) (X)
4026 # define NEVER(X) (X)
4027 #endif
4028
4029 #endif /* SQLITE_AMALGAMATION */
4030
4031 /*
4032 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
4033 **
4034 ** In some ways it would be better to obtain these values from system
4035 ** header files. But, the dependency is undesirable and (a) these
4036 ** have been stable for decades, (b) the values are part of POSIX and
4037 ** are also made explicit in [man stat], and (c) are part of the
4038 ** file format for zip archives.
4039 */
4040 #ifndef S_IFDIR
4041 # define S_IFDIR 0040000
4042 #endif
4043 #ifndef S_IFREG
4044 # define S_IFREG 0100000
4045 #endif
4046 #ifndef S_IFLNK
4047 # define S_IFLNK 0120000
4048 #endif
4049
4050 static const char ZIPFILE_SCHEMA[] =
4051 "CREATE TABLE y("
4052 "name PRIMARY KEY," /* 0: Name of file in zip archive */
@@ -4055,34 +4083,29 @@
4083 ** ZIPFILE_SIGNATURE_CDS:
4084 ** First 4 bytes of a valid CDS record.
4085 **
4086 ** ZIPFILE_SIGNATURE_LFH:
4087 ** First 4 bytes of a valid LFH record.
4088 **
4089 ** ZIPFILE_SIGNATURE_EOCD
4090 ** First 4 bytes of a valid EOCD record.
4091 */
4092 #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
4093 #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
4094 #define ZIPFILE_NEWENTRY_REQUIRED 20
4095 #define ZIPFILE_NEWENTRY_FLAGS 0x800
4096 #define ZIPFILE_SIGNATURE_CDS 0x02014b50
4097 #define ZIPFILE_SIGNATURE_LFH 0x04034b50
4098 #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
4099
4100 /*
4101 ** The sizes of the fixed-size part of each of the three main data
4102 ** structures in a zip archive.
4103 */
4104 #define ZIPFILE_LFH_FIXED_SZ 30
4105 #define ZIPFILE_EOCD_FIXED_SZ 22
4106 #define ZIPFILE_CDS_FIXED_SZ 46
 
 
 
 
 
 
 
 
 
 
 
 
 
4107
4108 /*
4109 *** 4.3.16 End of central directory record:
4110 ***
4111 *** end of central dir signature 4 bytes (0x06054b50)
@@ -4184,51 +4207,43 @@
4207 u16 nExtra;
4208 };
4209
4210 typedef struct ZipfileEntry ZipfileEntry;
4211 struct ZipfileEntry {
4212 ZipfileCDS cds; /* Parsed CDS record */
4213 u32 mUnixTime; /* Modification time, in UNIX format */
4214 u8 *aExtra; /* cds.nExtra+cds.nComment bytes of extra data */
4215 i64 iDataOff; /* Offset to data in file (if aData==0) */
4216 u8 *aData; /* cds.szCompressed bytes of compressed data */
4217 ZipfileEntry *pNext; /* Next element in in-memory CDS */
4218 };
4219
4220 /*
4221 ** Cursor type for zipfile tables.
4222 */
4223 typedef struct ZipfileCsr ZipfileCsr;
4224 struct ZipfileCsr {
4225 sqlite3_vtab_cursor base; /* Base class - must be first */
4226 i64 iId; /* Cursor ID */
4227 u8 bEof; /* True when at EOF */
4228 u8 bNoop; /* If next xNext() call is no-op */
4229
4230 /* Used outside of write transactions */
4231 FILE *pFile; /* Zip file */
4232 i64 iNextOff; /* Offset of next record in central directory */
4233 ZipfileEOCD eocd; /* Parse of central directory record */
4234
4235 ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */
4236 ZipfileEntry *pCurrent; /* Current entry */
 
 
 
 
 
 
4237 ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
4238 };
4239
 
 
 
 
 
4240 typedef struct ZipfileTab ZipfileTab;
4241 struct ZipfileTab {
4242 sqlite3_vtab base; /* Base class - must be first */
4243 char *zFile; /* Zip file this table accesses (may be NULL) */
4244 sqlite3 *db; /* Host database connection */
4245 u8 *aBuffer; /* Temporary buffer used for various tasks */
4246
4247 ZipfileCsr *pCsrList; /* List of cursors */
4248 i64 iNextCsrid;
4249
@@ -4238,21 +4253,37 @@
4253 FILE *pWriteFd; /* File handle open on zip archive */
4254 i64 szCurrent; /* Current size of zip archive */
4255 i64 szOrig; /* Size of archive at start of transaction */
4256 };
4257
4258 /*
4259 ** Set the error message contained in context ctx to the results of
4260 ** vprintf(zFmt, ...).
4261 */
4262 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4263 char *zMsg = 0;
4264 va_list ap;
4265 va_start(ap, zFmt);
4266 zMsg = sqlite3_vmprintf(zFmt, ap);
4267 sqlite3_result_error(ctx, zMsg, -1);
4268 sqlite3_free(zMsg);
4269 va_end(ap);
4270 }
4271
4272 /*
4273 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
4274 ** is not quoted, do nothing.
4275 */
4276 static void zipfileDequote(char *zIn){
4277 char q = zIn[0];
4278 if( q=='"' || q=='\'' || q=='`' || q=='[' ){
 
4279 int iIn = 1;
4280 int iOut = 0;
4281 if( q=='[' ) q = ']';
4282 while( ALWAYS(zIn[iIn]) ){
4283 char c = zIn[iIn++];
4284 if( c==q && zIn[iIn++]!=q ) break;
 
4285 zIn[iOut++] = c;
4286 }
4287 zIn[iOut] = '\0';
4288 }
4289 }
@@ -4275,10 +4306,25 @@
4306 int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
4307 int nFile = 0;
4308 const char *zFile = 0;
4309 ZipfileTab *pNew = 0;
4310 int rc;
4311
4312 /* If the table name is not "zipfile", require that the argument be
4313 ** specified. This stops zipfile tables from being created as:
4314 **
4315 ** CREATE VIRTUAL TABLE zzz USING zipfile();
4316 **
4317 ** It does not prevent:
4318 **
4319 ** CREATE VIRTUAL TABLE zipfile USING zipfile();
4320 */
4321 assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
4322 if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
4323 *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
4324 return SQLITE_ERROR;
4325 }
4326
4327 if( argc>3 ){
4328 zFile = argv[3];
4329 nFile = (int)strlen(zFile)+1;
4330 }
@@ -4286,10 +4332,11 @@
4332 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
4333 if( rc==SQLITE_OK ){
4334 pNew = (ZipfileTab*)sqlite3_malloc(nByte+nFile);
4335 if( pNew==0 ) return SQLITE_NOMEM;
4336 memset(pNew, 0, nByte+nFile);
4337 pNew->db = db;
4338 pNew->aBuffer = (u8*)&pNew[1];
4339 if( zFile ){
4340 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
4341 memcpy(pNew->zFile, zFile, nFile);
4342 zipfileDequote(pNew->zFile);
@@ -4296,15 +4343,48 @@
4343 }
4344 }
4345 *ppVtab = (sqlite3_vtab*)pNew;
4346 return rc;
4347 }
4348
4349 /*
4350 ** Free the ZipfileEntry structure indicated by the only argument.
4351 */
4352 static void zipfileEntryFree(ZipfileEntry *p){
4353 if( p ){
4354 sqlite3_free(p->cds.zFile);
4355 sqlite3_free(p);
4356 }
4357 }
4358
4359 /*
4360 ** Release resources that should be freed at the end of a write
4361 ** transaction.
4362 */
4363 static void zipfileCleanupTransaction(ZipfileTab *pTab){
4364 ZipfileEntry *pEntry;
4365 ZipfileEntry *pNext;
4366
4367 if( pTab->pWriteFd ){
4368 fclose(pTab->pWriteFd);
4369 pTab->pWriteFd = 0;
4370 }
4371 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
4372 pNext = pEntry->pNext;
4373 zipfileEntryFree(pEntry);
4374 }
4375 pTab->pFirstEntry = 0;
4376 pTab->pLastEntry = 0;
4377 pTab->szCurrent = 0;
4378 pTab->szOrig = 0;
4379 }
4380
4381 /*
4382 ** This method is the destructor for zipfile vtab objects.
4383 */
4384 static int zipfileDisconnect(sqlite3_vtab *pVtab){
4385 zipfileCleanupTransaction((ZipfileTab*)pVtab);
4386 sqlite3_free(pVtab);
4387 return SQLITE_OK;
4388 }
4389
4390 /*
@@ -4328,16 +4408,24 @@
4408 /*
4409 ** Reset a cursor back to the state it was in when first returned
4410 ** by zipfileOpen().
4411 */
4412 static void zipfileResetCursor(ZipfileCsr *pCsr){
4413 ZipfileEntry *p;
4414 ZipfileEntry *pNext;
4415
4416 pCsr->bEof = 0;
4417 if( pCsr->pFile ){
4418 fclose(pCsr->pFile);
4419 pCsr->pFile = 0;
4420 zipfileEntryFree(pCsr->pCurrent);
4421 pCsr->pCurrent = 0;
4422 }
4423
4424 for(p=pCsr->pFreeEntry; p; p=pNext){
4425 pNext = p->pNext;
4426 zipfileEntryFree(p);
4427 }
4428 }
4429
4430 /*
4431 ** Destructor for an ZipfileCsr.
@@ -4347,32 +4435,46 @@
4435 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
4436 ZipfileCsr **pp;
4437 zipfileResetCursor(pCsr);
4438
4439 /* Remove this cursor from the ZipfileTab.pCsrList list. */
4440 for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
4441 *pp = pCsr->pCsrNext;
 
 
 
 
4442
4443 sqlite3_free(pCsr);
4444 return SQLITE_OK;
4445 }
4446
4447 /*
4448 ** Set the error message for the virtual table associated with cursor
4449 ** pCsr to the results of vprintf(zFmt, ...).
4450 */
4451 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
4452 va_list ap;
4453 va_start(ap, zFmt);
4454 sqlite3_free(pTab->base.zErrMsg);
4455 pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
4456 va_end(ap);
4457 }
4458 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
4459 va_list ap;
4460 va_start(ap, zFmt);
4461 sqlite3_free(pCsr->base.pVtab->zErrMsg);
4462 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
4463 va_end(ap);
4464 }
4465
4466 /*
4467 ** Read nRead bytes of data from offset iOff of file pFile into buffer
4468 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
4469 ** otherwise.
4470 **
4471 ** If an error does occur, output variable (*pzErrmsg) may be set to point
4472 ** to an English language error message. It is the responsibility of the
4473 ** caller to eventually free this buffer using
4474 ** sqlite3_free().
4475 */
4476 static int zipfileReadData(
4477 FILE *pFile, /* Read from this file */
4478 u8 *aRead, /* Read into this buffer */
4479 int nRead, /* Number of bytes to read */
4480 i64 iOff, /* Offset to read from */
@@ -4402,24 +4504,38 @@
4504 }
4505 pTab->szCurrent += nWrite;
4506 return SQLITE_OK;
4507 }
4508
4509 /*
4510 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
4511 */
4512 static u16 zipfileGetU16(const u8 *aBuf){
4513 return (aBuf[1] << 8) + aBuf[0];
4514 }
4515
4516 /*
4517 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
4518 */
4519 static u32 zipfileGetU32(const u8 *aBuf){
4520 return ((u32)(aBuf[3]) << 24)
4521 + ((u32)(aBuf[2]) << 16)
4522 + ((u32)(aBuf[1]) << 8)
4523 + ((u32)(aBuf[0]) << 0);
4524 }
4525
4526 /*
4527 ** Write a 16-bit little endiate integer into buffer aBuf.
4528 */
4529 static void zipfilePutU16(u8 *aBuf, u16 val){
4530 aBuf[0] = val & 0xFF;
4531 aBuf[1] = (val>>8) & 0xFF;
4532 }
4533
4534 /*
4535 ** Write a 32-bit little endiate integer into buffer aBuf.
4536 */
4537 static void zipfilePutU32(u8 *aBuf, u32 val){
4538 aBuf[0] = val & 0xFF;
4539 aBuf[1] = (val>>8) & 0xFF;
4540 aBuf[2] = (val>>16) & 0xFF;
4541 aBuf[3] = (val>>24) & 0xFF;
@@ -4429,19 +4545,15 @@
4545 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
4546
4547 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
4548 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
4549
 
 
 
 
4550 /*
4551 ** Magic numbers used to read CDS records.
4552 */
 
4553 #define ZIPFILE_CDS_NFILE_OFF 28
4554 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
4555
4556 /*
4557 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
4558 ** if the record is not well-formed, or SQLITE_OK otherwise.
4559 */
@@ -4474,172 +4586,84 @@
4586
4587 return rc;
4588 }
4589
4590 /*
4591 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
4592 ** if the record is not well-formed, or SQLITE_OK otherwise.
4593 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4594 static int zipfileReadLFH(
4595 u8 *aBuffer,
4596 ZipfileLFH *pLFH
4597 ){
4598 u8 *aRead = aBuffer;
4599 int rc = SQLITE_OK;
4600
4601 u32 sig = zipfileRead32(aRead);
4602 if( sig!=ZIPFILE_SIGNATURE_LFH ){
4603 rc = SQLITE_ERROR;
4604 }else{
4605 pLFH->iVersionExtract = zipfileRead16(aRead);
4606 pLFH->flags = zipfileRead16(aRead);
4607 pLFH->iCompression = zipfileRead16(aRead);
4608 pLFH->mTime = zipfileRead16(aRead);
4609 pLFH->mDate = zipfileRead16(aRead);
4610 pLFH->crc32 = zipfileRead32(aRead);
4611 pLFH->szCompressed = zipfileRead32(aRead);
4612 pLFH->szUncompressed = zipfileRead32(aRead);
4613 pLFH->nFile = zipfileRead16(aRead);
4614 pLFH->nExtra = zipfileRead16(aRead);
4615 }
4616 return rc;
4617 }
4618
4619
4620 /*
4621 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
4622 ** Scan through this buffer to find an "extra-timestamp" field. If one
4623 ** exists, extract the 32-bit modification-timestamp from it and store
4624 ** the value in output parameter *pmTime.
4625 **
4626 ** Zero is returned if no extra-timestamp record could be found (and so
4627 ** *pmTime is left unchanged), or non-zero otherwise.
4628 **
4629 ** The general format of an extra field is:
4630 **
4631 ** Header ID 2 bytes
4632 ** Data Size 2 bytes
4633 ** Data N bytes
4634 */
4635 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
4636 int ret = 0;
4637 u8 *p = aExtra;
4638 u8 *pEnd = &aExtra[nExtra];
4639
4640 while( p<pEnd ){
4641 u16 id = zipfileRead16(p);
4642 u16 nByte = zipfileRead16(p);
4643
4644 switch( id ){
4645 case ZIPFILE_EXTRA_TIMESTAMP: {
4646 u8 b = p[0];
4647 if( b & 0x01 ){ /* 0x01 -> modtime is present */
4648 *pmTime = zipfileGetU32(&p[1]);
4649 ret = 1;
4650 }
4651 break;
4652 }
4653 }
4654
4655 p += nByte;
4656 }
4657 return ret;
4658 }
4659
4660 /*
4661 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
4662 ** fields of the CDS structure passed as the only argument to a 32-bit
4663 ** UNIX seconds-since-the-epoch timestamp. Return the result.
4664 **
 
 
 
 
 
 
 
 
4665 ** "Standard" MS-DOS time format:
4666 **
4667 ** File modification time:
4668 ** Bits 00-04: seconds divided by 2
4669 ** Bits 05-10: minute
@@ -4646,48 +4670,241 @@
4670 ** Bits 11-15: hour
4671 ** File modification date:
4672 ** Bits 00-04: day
4673 ** Bits 05-08: month (1-12)
4674 ** Bits 09-15: years from 1980
4675 **
4676 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
4677 */
4678 static u32 zipfileMtime(ZipfileCDS *pCDS){
4679 int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
4680 int M = ((pCDS->mDate >> 5) & 0x0F);
4681 int D = (pCDS->mDate & 0x1F);
4682 int B = -13;
4683
4684 int sec = (pCDS->mTime & 0x1F)*2;
4685 int min = (pCDS->mTime >> 5) & 0x3F;
4686 int hr = (pCDS->mTime >> 11) & 0x1F;
4687 i64 JD;
4688
4689 /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
4690
4691 /* Calculate the JD in seconds for noon on the day in question */
4692 if( M<3 ){
4693 Y = Y-1;
4694 M = M+12;
4695 }
4696 JD = (i64)(24*60*60) * (
4697 (int)(365.25 * (Y + 4716))
4698 + (int)(30.6001 * (M + 1))
4699 + D + B - 1524
4700 );
4701
4702 /* Correct the JD for the time within the day */
4703 JD += (hr-12) * 3600 + min * 60 + sec;
4704
4705 /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
4706 return (u32)(JD - (i64)(24405875) * 24*60*6);
4707 }
4708
4709 /*
4710 ** The opposite of zipfileMtime(). This function populates the mTime and
4711 ** mDate fields of the CDS structure passed as the first argument according
4712 ** to the UNIX timestamp value passed as the second.
4713 */
4714 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
4715 /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
4716 i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
4717
4718 int A, B, C, D, E;
4719 int yr, mon, day;
4720 int hr, min, sec;
4721
4722 A = (int)((JD - 1867216.25)/36524.25);
4723 A = (int)(JD + 1 + A - (A/4));
4724 B = A + 1524;
4725 C = (int)((B - 122.1)/365.25);
4726 D = (36525*(C&32767))/100;
4727 E = (int)((B-D)/30.6001);
4728
4729 day = B - D - (int)(30.6001*E);
4730 mon = (E<14 ? E-1 : E-13);
4731 yr = mon>2 ? C-4716 : C-4715;
4732
4733 hr = (mUnixTime % (24*60*60)) / (60*60);
4734 min = (mUnixTime % (60*60)) / 60;
4735 sec = (mUnixTime % 60);
4736
4737 if( yr>=1980 ){
4738 pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
4739 pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
4740 }else{
4741 pCds->mDate = pCds->mTime = 0;
4742 }
4743
4744 assert( mUnixTime<315507600
4745 || mUnixTime==zipfileMtime(pCds)
4746 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
4747 /* || (mUnixTime % 2) */
4748 );
4749 }
4750
4751 /*
4752 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
4753 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
4754 ** then pFile is a file-handle open on a zip file. In either case, this
4755 ** function creates a ZipfileEntry object based on the zip archive entry
4756 ** for which the CDS record is at offset iOff.
4757 **
4758 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
4759 ** the new object. Otherwise, an SQLite error code is returned and the
4760 ** final value of (*ppEntry) undefined.
4761 */
4762 static int zipfileGetEntry(
4763 ZipfileTab *pTab, /* Store any error message here */
4764 const u8 *aBlob, /* Pointer to in-memory file image */
4765 int nBlob, /* Size of aBlob[] in bytes */
4766 FILE *pFile, /* If aBlob==0, read from this file */
4767 i64 iOff, /* Offset of CDS record */
4768 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
4769 ){
4770 u8 *aRead;
4771 char **pzErr = &pTab->base.zErrMsg;
4772 int rc = SQLITE_OK;
4773
4774 if( aBlob==0 ){
4775 aRead = pTab->aBuffer;
4776 rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
4777 }else{
4778 aRead = (u8*)&aBlob[iOff];
4779 }
4780
4781 if( rc==SQLITE_OK ){
4782 int nAlloc;
4783 ZipfileEntry *pNew;
4784
4785 int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
4786 int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
4787 nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
4788
4789 nAlloc = sizeof(ZipfileEntry) + nExtra;
4790 if( aBlob ){
4791 nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
4792 }
4793
4794 pNew = (ZipfileEntry*)sqlite3_malloc(nAlloc);
4795 if( pNew==0 ){
4796 rc = SQLITE_NOMEM;
4797 }else{
4798 memset(pNew, 0, sizeof(ZipfileEntry));
4799 rc = zipfileReadCDS(aRead, &pNew->cds);
4800 if( rc!=SQLITE_OK ){
4801 *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
4802 }else if( aBlob==0 ){
4803 rc = zipfileReadData(
4804 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
4805 );
4806 }else{
4807 aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
4808 }
4809 }
4810
4811 if( rc==SQLITE_OK ){
4812 u32 *pt = &pNew->mUnixTime;
4813 pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
4814 pNew->aExtra = (u8*)&pNew[1];
4815 memcpy(pNew->aExtra, &aRead[nFile], nExtra);
4816 if( pNew->cds.zFile==0 ){
4817 rc = SQLITE_NOMEM;
4818 }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
4819 pNew->mUnixTime = zipfileMtime(&pNew->cds);
4820 }
4821 }
4822
4823 if( rc==SQLITE_OK ){
4824 static const int szFix = ZIPFILE_LFH_FIXED_SZ;
4825 ZipfileLFH lfh;
4826 if( pFile ){
4827 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
4828 }else{
4829 aRead = (u8*)&aBlob[pNew->cds.iOffset];
4830 }
4831
4832 rc = zipfileReadLFH(aRead, &lfh);
4833 if( rc==SQLITE_OK ){
4834 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
4835 pNew->iDataOff += lfh.nFile + lfh.nExtra;
4836 if( aBlob && pNew->cds.szCompressed ){
4837 pNew->aData = &pNew->aExtra[nExtra];
4838 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
4839 }
4840 }else{
4841 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
4842 (int)pNew->cds.iOffset
4843 );
4844 }
4845 }
4846
4847 if( rc!=SQLITE_OK ){
4848 zipfileEntryFree(pNew);
4849 }else{
4850 *ppEntry = pNew;
4851 }
4852 }
4853
4854 return rc;
4855 }
4856
4857 /*
4858 ** Advance an ZipfileCsr to its next row of output.
4859 */
4860 static int zipfileNext(sqlite3_vtab_cursor *cur){
4861 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4862 int rc = SQLITE_OK;
4863
4864 if( pCsr->pFile ){
4865 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
4866 zipfileEntryFree(pCsr->pCurrent);
4867 pCsr->pCurrent = 0;
4868 if( pCsr->iNextOff>=iEof ){
4869 pCsr->bEof = 1;
4870 }else{
4871 ZipfileEntry *p = 0;
4872 ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
4873 rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
4874 if( rc==SQLITE_OK ){
4875 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
4876 pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
4877 }
4878 pCsr->pCurrent = p;
4879 }
4880 }else{
4881 if( !pCsr->bNoop ){
4882 pCsr->pCurrent = pCsr->pCurrent->pNext;
4883 }
4884 if( pCsr->pCurrent==0 ){
4885 pCsr->bEof = 1;
4886 }
4887 }
4888
4889 pCsr->bNoop = 0;
4890 return rc;
4891 }
4892
4893 static void zipfileFree(void *p) {
4894 sqlite3_free(p);
4895 }
4896
4897 /*
4898 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
4899 ** size is nOut bytes. This function uncompresses the data and sets the
4900 ** return value in context pCtx to the result (a blob).
4901 **
4902 ** If an error occurs, an error code is left in pCtx instead.
4903 */
4904 static void zipfileInflate(
4905 sqlite3_context *pCtx, /* Store result here */
4906 const u8 *aIn, /* Compressed data */
4907 int nIn, /* Size of buffer aIn[] in bytes */
4908 int nOut /* Expected output size */
4909 ){
4910 u8 *aRes = sqlite3_malloc(nOut);
@@ -4709,22 +4926,35 @@
4926 }else{
4927 err = inflate(&str, Z_NO_FLUSH);
4928 if( err!=Z_STREAM_END ){
4929 zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
4930 }else{
4931 sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
4932 aRes = 0;
4933 }
4934 }
4935 sqlite3_free(aRes);
4936 inflateEnd(&str);
4937 }
4938 }
4939
4940 /*
4941 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
4942 ** compresses it and sets (*ppOut) to point to a buffer containing the
4943 ** compressed data. The caller is responsible for eventually calling
4944 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
4945 ** is set to the size of buffer (*ppOut) in bytes.
4946 **
4947 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
4948 ** code is returned and an error message left in virtual-table handle
4949 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
4950 ** case.
4951 */
4952 static int zipfileDeflate(
 
4953 const u8 *aIn, int nIn, /* Input */
4954 u8 **ppOut, int *pnOut, /* Output */
4955 char **pzErr /* OUT: Error message */
4956 ){
4957 int nAlloc = (int)compressBound(nIn);
4958 u8 *aOut;
4959 int rc = SQLITE_OK;
4960
@@ -4746,11 +4976,11 @@
4976 if( res==Z_STREAM_END ){
4977 *ppOut = aOut;
4978 *pnOut = (int)str.total_out;
4979 }else{
4980 sqlite3_free(aOut);
4981 *pzErr = sqlite3_mprintf("zipfile: deflate() error");
4982 rc = SQLITE_ERROR;
4983 }
4984 deflateEnd(&str);
4985 }
4986
@@ -4766,121 +4996,134 @@
4996 sqlite3_vtab_cursor *cur, /* The cursor */
4997 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
4998 int i /* Which column to return */
4999 ){
5000 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5001 ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
5002 int rc = SQLITE_OK;
5003 switch( i ){
5004 case 0: /* name */
5005 sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
5006 break;
5007 case 1: /* mode */
5008 /* TODO: Whether or not the following is correct surely depends on
5009 ** the platform on which the archive was created. */
5010 sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
5011 break;
5012 case 2: { /* mtime */
5013 sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
 
 
 
 
5014 break;
5015 }
5016 case 3: { /* sz */
5017 if( sqlite3_vtab_nochange(ctx)==0 ){
5018 sqlite3_result_int64(ctx, pCDS->szUncompressed);
5019 }
5020 break;
5021 }
5022 case 4: /* rawdata */
5023 if( sqlite3_vtab_nochange(ctx) ) break;
5024 case 5: { /* data */
5025 if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
5026 int sz = pCDS->szCompressed;
5027 int szFinal = pCDS->szUncompressed;
5028 if( szFinal>0 ){
5029 u8 *aBuf;
5030 u8 *aFree = 0;
5031 if( pCsr->pCurrent->aData ){
5032 aBuf = pCsr->pCurrent->aData;
5033 }else{
5034 aBuf = aFree = sqlite3_malloc(sz);
5035 if( aBuf==0 ){
5036 rc = SQLITE_NOMEM;
5037 }else{
5038 FILE *pFile = pCsr->pFile;
5039 if( pFile==0 ){
5040 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
5041 }
5042 rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
5043 &pCsr->base.pVtab->zErrMsg
5044 );
5045 }
5046 }
5047 if( rc==SQLITE_OK ){
5048 if( i==5 && pCDS->iCompression ){
5049 zipfileInflate(ctx, aBuf, sz, szFinal);
5050 }else{
5051 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
5052 }
 
5053 }
5054 sqlite3_free(aFree);
5055 }else{
5056 /* Figure out if this is a directory or a zero-sized file. Consider
5057 ** it to be a directory either if the mode suggests so, or if
5058 ** the final character in the name is '/'. */
5059 u32 mode = pCDS->iExternalAttr >> 16;
5060 if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
5061 sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
5062 }
5063 }
5064 }
5065 break;
5066 }
5067 case 6: /* method */
5068 sqlite3_result_int(ctx, pCDS->iCompression);
5069 break;
5070 default: /* z */
5071 assert( i==7 );
5072 sqlite3_result_int64(ctx, pCsr->iId);
5073 break;
5074 }
5075
5076 return rc;
5077 }
5078
5079 /*
5080 ** Return TRUE if the cursor is at EOF.
 
 
 
 
 
 
 
 
 
5081 */
5082 static int zipfileEof(sqlite3_vtab_cursor *cur){
5083 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5084 return pCsr->bEof;
5085 }
5086
5087 /*
5088 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
5089 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
5090 ** is guaranteed to be a file-handle open on a zip file.
5091 **
5092 ** This function attempts to locate the EOCD record within the zip archive
5093 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
5094 ** returned if successful. Otherwise, an SQLite error code is returned and
5095 ** an English language error message may be left in virtual-table pTab.
5096 */
5097 static int zipfileReadEOCD(
5098 ZipfileTab *pTab, /* Return errors here */
5099 const u8 *aBlob, /* Pointer to in-memory file image */
5100 int nBlob, /* Size of aBlob[] in bytes */
5101 FILE *pFile, /* Read from this file if aBlob==0 */
5102 ZipfileEOCD *pEOCD /* Object to populate */
5103 ){
5104 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
 
5105 int nRead; /* Bytes to read from file */
5106 int rc = SQLITE_OK;
5107
5108 if( aBlob==0 ){
5109 i64 iOff; /* Offset to read from */
5110 i64 szFile; /* Total size of file in bytes */
5111 fseek(pFile, 0, SEEK_END);
5112 szFile = (i64)ftell(pFile);
5113 if( szFile==0 ){
5114 memset(pEOCD, 0, sizeof(ZipfileEOCD));
5115 return SQLITE_OK;
5116 }
5117 nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
5118 iOff = szFile - nRead;
5119 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
5120 }else{
5121 nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
5122 aRead = (u8*)&aBlob[nBlob-nRead];
5123 }
5124
5125 if( rc==SQLITE_OK ){
5126 int i;
5127
5128 /* Scan backwards looking for the signature bytes */
5129 for(i=nRead-20; i>=0; i--){
@@ -4902,21 +5145,63 @@
5145 pEOCD->iFirstDisk = zipfileRead16(aRead);
5146 pEOCD->nEntry = zipfileRead16(aRead);
5147 pEOCD->nEntryTotal = zipfileRead16(aRead);
5148 pEOCD->nSize = zipfileRead32(aRead);
5149 pEOCD->iOffset = zipfileRead32(aRead);
 
 
 
 
 
 
 
 
5150 }
5151
5152 return rc;
5153 }
5154
5155 /*
5156 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
5157 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
5158 ** to the end of the list. Otherwise, it is added to the list immediately
5159 ** before pBefore (which is guaranteed to be a part of said list).
5160 */
5161 static void zipfileAddEntry(
5162 ZipfileTab *pTab,
5163 ZipfileEntry *pBefore,
5164 ZipfileEntry *pNew
5165 ){
5166 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5167 assert( pNew->pNext==0 );
5168 if( pBefore==0 ){
5169 if( pTab->pFirstEntry==0 ){
5170 pTab->pFirstEntry = pTab->pLastEntry = pNew;
5171 }else{
5172 assert( pTab->pLastEntry->pNext==0 );
5173 pTab->pLastEntry->pNext = pNew;
5174 pTab->pLastEntry = pNew;
5175 }
5176 }else{
5177 ZipfileEntry **pp;
5178 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5179 pNew->pNext = pBefore;
5180 *pp = pNew;
5181 }
5182 }
5183
5184 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
5185 ZipfileEOCD eocd;
5186 int rc;
5187 int i;
5188 i64 iOff;
5189
5190 rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
5191 iOff = eocd.iOffset;
5192 for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5193 ZipfileEntry *pNew = 0;
5194 rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
5195
5196 if( rc==SQLITE_OK ){
5197 zipfileAddEntry(pTab, 0, pNew);
5198 iOff += ZIPFILE_CDS_FIXED_SZ;
5199 iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
5200 }
5201 }
5202 return rc;
5203 }
5204
5205 /*
5206 ** xFilter callback.
5207 */
@@ -4925,33 +5210,41 @@
5210 int idxNum, const char *idxStr,
5211 int argc, sqlite3_value **argv
5212 ){
5213 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
5214 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5215 const char *zFile = 0; /* Zip file to scan */
5216 int rc = SQLITE_OK; /* Return Code */
5217 int bInMemory = 0; /* True for an in-memory zipfile */
5218
5219 zipfileResetCursor(pCsr);
5220
5221 if( pTab->zFile ){
5222 zFile = pTab->zFile;
5223 }else if( idxNum==0 ){
5224 zipfileCursorErr(pCsr, "zipfile() function requires an argument");
 
 
5225 return SQLITE_ERROR;
5226 }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
5227 const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
5228 int nBlob = sqlite3_value_bytes(argv[0]);
5229 assert( pTab->pFirstEntry==0 );
5230 rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
5231 pCsr->pFreeEntry = pTab->pFirstEntry;
5232 pTab->pFirstEntry = pTab->pLastEntry = 0;
5233 if( rc!=SQLITE_OK ) return rc;
5234 bInMemory = 1;
5235 }else{
5236 zFile = (const char*)sqlite3_value_text(argv[0]);
5237 }
5238
5239 if( 0==pTab->pWriteFd && 0==bInMemory ){
5240 pCsr->pFile = fopen(zFile, "rb");
5241 if( pCsr->pFile==0 ){
5242 zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
5243 rc = SQLITE_ERROR;
5244 }else{
5245 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
5246 if( rc==SQLITE_OK ){
5247 if( pCsr->eocd.nEntry==0 ){
5248 pCsr->bEof = 1;
5249 }else{
5250 pCsr->iNextOff = pCsr->eocd.iOffset;
@@ -4958,16 +5251,13 @@
5251 rc = zipfileNext(cur);
5252 }
5253 }
5254 }
5255 }else{
5256 pCsr->bNoop = 1;
5257 pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
 
 
5258 rc = zipfileNext(cur);
 
5259 }
5260
5261 return rc;
5262 }
5263
@@ -4999,226 +5289,115 @@
5289 }
5290
5291 return SQLITE_OK;
5292 }
5293
5294 static ZipfileEntry *zipfileNewEntry(const char *zPath){
5295 ZipfileEntry *pNew;
5296 pNew = sqlite3_malloc(sizeof(ZipfileEntry));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5297 if( pNew ){
5298 memset(pNew, 0, sizeof(ZipfileEntry));
5299 pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
5300 if( pNew->cds.zFile==0 ){
5301 sqlite3_free(pNew);
5302 pNew = 0;
5303 }
5304 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5305 return pNew;
5306 }
5307
5308 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
5309 ZipfileCDS *pCds = &pEntry->cds;
5310 u8 *a = aBuf;
5311
5312 pCds->nExtra = 9;
5313
5314 /* Write the LFH itself */
5315 zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
5316 zipfileWrite16(a, pCds->iVersionExtract);
5317 zipfileWrite16(a, pCds->flags);
5318 zipfileWrite16(a, pCds->iCompression);
5319 zipfileWrite16(a, pCds->mTime);
5320 zipfileWrite16(a, pCds->mDate);
5321 zipfileWrite32(a, pCds->crc32);
5322 zipfileWrite32(a, pCds->szCompressed);
5323 zipfileWrite32(a, pCds->szUncompressed);
5324 zipfileWrite16(a, (u16)pCds->nFile);
5325 zipfileWrite16(a, pCds->nExtra);
5326 assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
5327
5328 /* Add the file name */
5329 memcpy(a, pCds->zFile, (int)pCds->nFile);
5330 a += (int)pCds->nFile;
5331
5332 /* The "extra" data */
5333 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5334 zipfileWrite16(a, 5);
5335 *a++ = 0x01;
5336 zipfileWrite32(a, pEntry->mUnixTime);
5337
5338 return a-aBuf;
5339 }
5340
5341 static int zipfileAppendEntry(
5342 ZipfileTab *pTab,
5343 ZipfileEntry *pEntry,
 
 
5344 const u8 *pData,
5345 int nData
 
5346 ){
5347 u8 *aBuf = pTab->aBuffer;
5348 int nBuf;
5349 int rc;
5350
5351 nBuf = zipfileSerializeLFH(pEntry, aBuf);
5352 rc = zipfileAppendData(pTab, aBuf, nBuf);
5353 if( rc==SQLITE_OK ){
5354 pEntry->iDataOff = pTab->szCurrent;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5355 rc = zipfileAppendData(pTab, pData, nData);
5356 }
5357
5358 return rc;
5359 }
5360
5361 static int zipfileGetMode(
 
5362 sqlite3_value *pVal,
5363 int bIsDir, /* If true, default to directory */
5364 u32 *pMode, /* OUT: Mode value */
5365 char **pzErr /* OUT: Error message */
5366 ){
5367 const char *z = (const char*)sqlite3_value_text(pVal);
5368 u32 mode = 0;
5369 if( z==0 ){
5370 mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
5371 }else if( z[0]>='0' && z[0]<='9' ){
5372 mode = (unsigned int)sqlite3_value_int(pVal);
5373 }else{
5374 const char zTemplate[11] = "-rwxrwxrwx";
5375 int i;
5376 if( strlen(z)!=10 ) goto parse_error;
5377 switch( z[0] ){
5378 case '-': mode |= S_IFREG; break;
5379 case 'd': mode |= S_IFDIR; break;
 
5380 case 'l': mode |= S_IFLNK; break;
 
5381 default: goto parse_error;
5382 }
5383 for(i=1; i<10; i++){
5384 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
5385 else if( z[i]!='-' ) goto parse_error;
5386 }
5387 }
5388 if( ((mode & S_IFDIR)==0)==bIsDir ){
5389 /* The "mode" attribute is a directory, but data has been specified.
5390 ** Or vice-versa - no data but "mode" is a file or symlink. */
5391 *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
5392 return SQLITE_CONSTRAINT;
5393 }
5394 *pMode = mode;
5395 return SQLITE_OK;
5396
5397 parse_error:
5398 *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5399 return SQLITE_ERROR;
5400 }
5401
5402 /*
5403 ** Both (const char*) arguments point to nul-terminated strings. Argument
@@ -5229,10 +5408,72 @@
5408 if( zA[nA-1]=='/' ) nA--;
5409 if( zB[nB-1]=='/' ) nB--;
5410 if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5411 return 1;
5412 }
5413
5414 static int zipfileBegin(sqlite3_vtab *pVtab){
5415 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5416 int rc = SQLITE_OK;
5417
5418 assert( pTab->pWriteFd==0 );
5419
5420 /* Open a write fd on the file. Also load the entire central directory
5421 ** structure into memory. During the transaction any new file data is
5422 ** appended to the archive file, but the central directory is accumulated
5423 ** in main-memory until the transaction is committed. */
5424 pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5425 if( pTab->pWriteFd==0 ){
5426 pTab->base.zErrMsg = sqlite3_mprintf(
5427 "zipfile: failed to open file %s for writing", pTab->zFile
5428 );
5429 rc = SQLITE_ERROR;
5430 }else{
5431 fseek(pTab->pWriteFd, 0, SEEK_END);
5432 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5433 rc = zipfileLoadDirectory(pTab, 0, 0);
5434 }
5435
5436 if( rc!=SQLITE_OK ){
5437 zipfileCleanupTransaction(pTab);
5438 }
5439
5440 return rc;
5441 }
5442
5443 /*
5444 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
5445 ** time(2)).
5446 */
5447 static u32 zipfileTime(void){
5448 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
5449 u32 ret;
5450 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
5451 i64 ms;
5452 pVfs->xCurrentTimeInt64(pVfs, &ms);
5453 ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
5454 }else{
5455 double day;
5456 pVfs->xCurrentTime(pVfs, &day);
5457 ret = (u32)((day - 2440587.5) * 86400);
5458 }
5459 return ret;
5460 }
5461
5462 /*
5463 ** Return a 32-bit timestamp in UNIX epoch format.
5464 **
5465 ** If the value passed as the only argument is either NULL or an SQL NULL,
5466 ** return the current time. Otherwise, return the value stored in (*pVal)
5467 ** cast to a 32-bit unsigned integer.
5468 */
5469 static u32 zipfileGetTime(sqlite3_value *pVal){
5470 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
5471 return zipfileTime();
5472 }
5473 return (u32)sqlite3_value_int64(pVal);
5474 }
5475
5476 /*
5477 ** xUpdate method.
5478 */
5479 static int zipfileUpdate(
@@ -5244,230 +5485,244 @@
5485 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5486 int rc = SQLITE_OK; /* Return Code */
5487 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
5488
5489 u32 mode = 0; /* Mode for new entry */
5490 u32 mTime = 0; /* Modification time for new entry */
5491 i64 sz = 0; /* Uncompressed size */
5492 const char *zPath = 0; /* Path for new entry */
5493 int nPath = 0; /* strlen(zPath) */
5494 const u8 *pData = 0; /* Pointer to buffer containing content */
5495 int nData = 0; /* Size of pData buffer in bytes */
5496 int iMethod = 0; /* Compression method for new entry */
5497 u8 *pFree = 0; /* Free this */
5498 char *zFree = 0; /* Also free this */
 
5499 ZipfileEntry *pOld = 0;
5500 int bIsDir = 0;
5501 u32 iCrc32 = 0;
5502
5503 if( pTab->pWriteFd==0 ){
5504 rc = zipfileBegin(pVtab);
5505 if( rc!=SQLITE_OK ) return rc;
5506 }
5507
5508 /* If this is a DELETE or UPDATE, find the archive entry to delete. */
5509 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5510 const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
5511 int nDelete = (int)strlen(zDelete);
5512 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
5513 if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
 
 
5514 break;
5515 }
5516 assert( pOld->pNext );
5517 }
5518 }
5519
5520 if( nVal>1 ){
5521 /* Check that "sz" and "rawdata" are both NULL: */
5522 if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
5523 zipfileTableErr(pTab, "sz must be NULL");
5524 rc = SQLITE_CONSTRAINT;
5525 }
5526 if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
5527 zipfileTableErr(pTab, "rawdata must be NULL");
5528 rc = SQLITE_CONSTRAINT;
5529 }
5530
5531 if( rc==SQLITE_OK ){
5532 if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5533 /* data=NULL. A directory */
5534 bIsDir = 1;
5535 }else{
5536 /* Value specified for "data", and possibly "method". This must be
5537 ** a regular file or a symlink. */
5538 const u8 *aIn = sqlite3_value_blob(apVal[7]);
5539 int nIn = sqlite3_value_bytes(apVal[7]);
5540 int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5541
5542 iMethod = sqlite3_value_int(apVal[8]);
5543 sz = nIn;
5544 pData = aIn;
5545 nData = nIn;
5546 if( iMethod!=0 && iMethod!=8 ){
5547 zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
5548 rc = SQLITE_CONSTRAINT;
5549 }else{
5550 if( bAuto || iMethod ){
5551 int nCmp;
5552 rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
5553 if( rc==SQLITE_OK ){
5554 if( iMethod || nCmp<nIn ){
5555 iMethod = 8;
5556 pData = pFree;
5557 nData = nCmp;
5558 }
5559 }
5560 }
5561 iCrc32 = crc32(0, aIn, nIn);
5562 }
5563 }
5564 }
5565
5566 if( rc==SQLITE_OK ){
5567 rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
5568 }
5569
5570 if( rc==SQLITE_OK ){
5571 zPath = (const char*)sqlite3_value_text(apVal[2]);
5572 nPath = (int)strlen(zPath);
5573 mTime = zipfileGetTime(apVal[4]);
5574 }
5575
5576 if( rc==SQLITE_OK && bIsDir ){
5577 /* For a directory, check that the last character in the path is a
5578 ** '/'. This appears to be required for compatibility with info-zip
5579 ** (the unzip command on unix). It does not create directories
5580 ** otherwise. */
5581 if( zPath[nPath-1]!='/' ){
5582 zFree = sqlite3_mprintf("%s/", zPath);
5583 if( zFree==0 ){ rc = SQLITE_NOMEM; }
5584 zPath = (const char*)zFree;
5585 nPath++;
5586 }
5587 }
5588
5589 /* Check that we're not inserting a duplicate entry */
5590 if( pOld==0 && rc==SQLITE_OK ){
5591 ZipfileEntry *p;
5592 for(p=pTab->pFirstEntry; p; p=p->pNext){
5593 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
5594 switch( sqlite3_vtab_on_conflict(pTab->db) ){
5595 case SQLITE_IGNORE: {
5596 goto zipfile_update_done;
5597 }
5598 case SQLITE_REPLACE: {
5599 pOld = p;
5600 break;
5601 }
5602 default: {
5603 zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
5604 rc = SQLITE_CONSTRAINT;
5605 break;
5606 }
5607 }
5608 break;
5609 }
5610 }
5611 }
5612
5613 if( rc==SQLITE_OK ){
5614 /* Create the new CDS record. */
5615 pNew = zipfileNewEntry(zPath);
5616 if( pNew==0 ){
5617 rc = SQLITE_NOMEM;
5618 }else{
5619 pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5620 pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5621 pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
5622 pNew->cds.iCompression = (u16)iMethod;
5623 zipfileMtimeToDos(&pNew->cds, mTime);
5624 pNew->cds.crc32 = iCrc32;
5625 pNew->cds.szCompressed = nData;
5626 pNew->cds.szUncompressed = (u32)sz;
5627 pNew->cds.iExternalAttr = (mode<<16);
5628 pNew->cds.iOffset = (u32)pTab->szCurrent;
5629 pNew->cds.nFile = (u16)nPath;
5630 pNew->mUnixTime = (u32)mTime;
5631 rc = zipfileAppendEntry(pTab, pNew, pData, nData);
5632 zipfileAddEntry(pTab, pOld, pNew);
5633 }
5634 }
5635 }
5636
5637 if( rc==SQLITE_OK && pOld ){
5638 ZipfileEntry **pp;
5639 ZipfileCsr *pCsr;
5640 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
5641 if( pCsr->pCurrent==pOld ){
5642 pCsr->pCurrent = pOld->pNext;
5643 pCsr->bNoop = 1;
5644 }
5645 }
5646 for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
5647 *pp = (*pp)->pNext;
5648 zipfileEntryFree(pOld);
5649 }
5650
5651 zipfile_update_done:
5652 sqlite3_free(pFree);
5653 sqlite3_free(zFree);
5654 return rc;
5655 }
5656
5657 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
5658 u8 *a = aBuf;
5659 zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
5660 zipfileWrite16(a, p->iDisk);
5661 zipfileWrite16(a, p->iFirstDisk);
5662 zipfileWrite16(a, p->nEntry);
5663 zipfileWrite16(a, p->nEntryTotal);
5664 zipfileWrite32(a, p->nSize);
5665 zipfileWrite32(a, p->iOffset);
5666 zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/
5667
5668 return a-aBuf;
5669 }
5670
5671 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
5672 int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
5673 assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
5674 return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
5675 }
5676
5677 /*
5678 ** Serialize the CDS structure into buffer aBuf[]. Return the number
5679 ** of bytes written.
5680 */
5681 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
5682 u8 *a = aBuf;
5683 ZipfileCDS *pCDS = &pEntry->cds;
5684
5685 if( pEntry->aExtra==0 ){
5686 pCDS->nExtra = 9;
5687 }
5688
5689 zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
5690 zipfileWrite16(a, pCDS->iVersionMadeBy);
5691 zipfileWrite16(a, pCDS->iVersionExtract);
5692 zipfileWrite16(a, pCDS->flags);
5693 zipfileWrite16(a, pCDS->iCompression);
5694 zipfileWrite16(a, pCDS->mTime);
5695 zipfileWrite16(a, pCDS->mDate);
5696 zipfileWrite32(a, pCDS->crc32);
5697 zipfileWrite32(a, pCDS->szCompressed);
5698 zipfileWrite32(a, pCDS->szUncompressed);
5699 assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
5700 zipfileWrite16(a, pCDS->nFile);
5701 zipfileWrite16(a, pCDS->nExtra);
5702 zipfileWrite16(a, pCDS->nComment);
5703 zipfileWrite16(a, pCDS->iDiskStart);
5704 zipfileWrite16(a, pCDS->iInternalAttr);
5705 zipfileWrite32(a, pCDS->iExternalAttr);
5706 zipfileWrite32(a, pCDS->iOffset);
5707
5708 memcpy(a, pCDS->zFile, pCDS->nFile);
5709 a += pCDS->nFile;
5710
5711 if( pEntry->aExtra ){
5712 int n = (int)pCDS->nExtra + (int)pCDS->nComment;
5713 memcpy(a, pEntry->aExtra, n);
5714 a += n;
 
 
 
 
 
 
 
 
 
 
 
 
 
5715 }else{
5716 assert( pCDS->nExtra==9 );
5717 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5718 zipfileWrite16(a, 5);
5719 *a++ = 0x01;
5720 zipfileWrite32(a, pEntry->mUnixTime);
5721 }
5722
5723 return a-aBuf;
 
 
 
 
5724 }
5725
5726 static int zipfileCommit(sqlite3_vtab *pVtab){
5727 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5728 int rc = SQLITE_OK;
@@ -5475,14 +5730,14 @@
5730 i64 iOffset = pTab->szCurrent;
5731 ZipfileEntry *p;
5732 ZipfileEOCD eocd;
5733 int nEntry = 0;
5734
5735 /* Write out all entries */
5736 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
5737 int n = zipfileSerializeCDS(p, pTab->aBuffer);
5738 rc = zipfileAppendData(pTab, pTab->aBuffer, n);
5739 nEntry++;
5740 }
5741
5742 /* Write out the EOCD record */
5743 eocd.iDisk = 0;
@@ -5519,11 +5774,11 @@
5774 ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
5775 assert( argc>0 );
5776
5777 pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
5778 if( pCsr ){
5779 ZipfileCDS *p = &pCsr->pCurrent->cds;
5780 char *zRes = sqlite3_mprintf("{"
5781 "\"version-made-by\" : %u, "
5782 "\"version-to-extract\" : %u, "
5783 "\"flags\" : %u, "
5784 "\"compression\" : %u, "
@@ -5556,11 +5811,10 @@
5811 sqlite3_free(zRes);
5812 }
5813 }
5814 }
5815
 
5816 /*
5817 ** xFindFunction method.
5818 */
5819 static int zipfileFindFunction(
5820 sqlite3_vtab *pVtab, /* Virtual table handle */
@@ -5567,20 +5821,259 @@
5821 int nArg, /* Number of SQL function arguments */
5822 const char *zName, /* Name of SQL function */
5823 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
5824 void **ppArg /* OUT: User data for *pxFunc */
5825 ){
5826 if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
5827 *pxFunc = zipfileFunctionCds;
5828 *ppArg = (void*)pVtab;
5829 return 1;
5830 }
 
 
 
5831 return 0;
5832 }
5833
5834 typedef struct ZipfileBuffer ZipfileBuffer;
5835 struct ZipfileBuffer {
5836 u8 *a; /* Pointer to buffer */
5837 int n; /* Size of buffer in bytes */
5838 int nAlloc; /* Byte allocated at a[] */
5839 };
5840
5841 typedef struct ZipfileCtx ZipfileCtx;
5842 struct ZipfileCtx {
5843 int nEntry;
5844 ZipfileBuffer body;
5845 ZipfileBuffer cds;
5846 };
5847
5848 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
5849 if( pBuf->n+nByte>pBuf->nAlloc ){
5850 u8 *aNew;
5851 int nNew = pBuf->n ? pBuf->n*2 : 512;
5852 int nReq = pBuf->n + nByte;
5853
5854 while( nNew<nReq ) nNew = nNew*2;
5855 aNew = sqlite3_realloc(pBuf->a, nNew);
5856 if( aNew==0 ) return SQLITE_NOMEM;
5857 pBuf->a = aNew;
5858 pBuf->nAlloc = nNew;
5859 }
5860 return SQLITE_OK;
5861 }
5862
5863 /*
5864 ** xStep() callback for the zipfile() aggregate. This can be called in
5865 ** any of the following ways:
5866 **
5867 ** SELECT zipfile(name,data) ...
5868 ** SELECT zipfile(name,mode,mtime,data) ...
5869 ** SELECT zipfile(name,mode,mtime,data,method) ...
5870 */
5871 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
5872 ZipfileCtx *p; /* Aggregate function context */
5873 ZipfileEntry e; /* New entry to add to zip archive */
5874
5875 sqlite3_value *pName = 0;
5876 sqlite3_value *pMode = 0;
5877 sqlite3_value *pMtime = 0;
5878 sqlite3_value *pData = 0;
5879 sqlite3_value *pMethod = 0;
5880
5881 int bIsDir = 0;
5882 u32 mode;
5883 int rc = SQLITE_OK;
5884 char *zErr = 0;
5885
5886 int iMethod = -1; /* Compression method to use (0 or 8) */
5887
5888 const u8 *aData = 0; /* Possibly compressed data for new entry */
5889 int nData = 0; /* Size of aData[] in bytes */
5890 int szUncompressed = 0; /* Size of data before compression */
5891 u8 *aFree = 0; /* Free this before returning */
5892 u32 iCrc32 = 0; /* crc32 of uncompressed data */
5893
5894 char *zName = 0; /* Path (name) of new entry */
5895 int nName = 0; /* Size of zName in bytes */
5896 char *zFree = 0; /* Free this before returning */
5897 int nByte;
5898
5899 memset(&e, 0, sizeof(e));
5900 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
5901 if( p==0 ) return;
5902
5903 /* Martial the arguments into stack variables */
5904 if( nVal!=2 && nVal!=4 && nVal!=5 ){
5905 zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
5906 rc = SQLITE_ERROR;
5907 goto zipfile_step_out;
5908 }
5909 pName = apVal[0];
5910 if( nVal==2 ){
5911 pData = apVal[1];
5912 }else{
5913 pMode = apVal[1];
5914 pMtime = apVal[2];
5915 pData = apVal[3];
5916 if( nVal==5 ){
5917 pMethod = apVal[4];
5918 }
5919 }
5920
5921 /* Check that the 'name' parameter looks ok. */
5922 zName = (char*)sqlite3_value_text(pName);
5923 nName = sqlite3_value_bytes(pName);
5924 if( zName==0 ){
5925 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
5926 rc = SQLITE_ERROR;
5927 goto zipfile_step_out;
5928 }
5929
5930 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
5931 ** deflate compression) or NULL (choose automatically). */
5932 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
5933 iMethod = (int)sqlite3_value_int64(pMethod);
5934 if( iMethod!=0 && iMethod!=8 ){
5935 zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
5936 rc = SQLITE_ERROR;
5937 goto zipfile_step_out;
5938 }
5939 }
5940
5941 /* Now inspect the data. If this is NULL, then the new entry must be a
5942 ** directory. Otherwise, figure out whether or not the data should
5943 ** be deflated or simply stored in the zip archive. */
5944 if( sqlite3_value_type(pData)==SQLITE_NULL ){
5945 bIsDir = 1;
5946 iMethod = 0;
5947 }else{
5948 aData = sqlite3_value_blob(pData);
5949 szUncompressed = nData = sqlite3_value_bytes(pData);
5950 iCrc32 = crc32(0, aData, nData);
5951 if( iMethod<0 || iMethod==8 ){
5952 int nOut = 0;
5953 rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
5954 if( rc!=SQLITE_OK ){
5955 goto zipfile_step_out;
5956 }
5957 if( iMethod==8 || nOut<nData ){
5958 aData = aFree;
5959 nData = nOut;
5960 iMethod = 8;
5961 }else{
5962 iMethod = 0;
5963 }
5964 }
5965 }
5966
5967 /* Decode the "mode" argument. */
5968 rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
5969 if( rc ) goto zipfile_step_out;
5970
5971 /* Decode the "mtime" argument. */
5972 e.mUnixTime = zipfileGetTime(pMtime);
5973
5974 /* If this is a directory entry, ensure that there is exactly one '/'
5975 ** at the end of the path. Or, if this is not a directory and the path
5976 ** ends in '/' it is an error. */
5977 if( bIsDir==0 ){
5978 if( zName[nName-1]=='/' ){
5979 zErr = sqlite3_mprintf("non-directory name must not end with /");
5980 rc = SQLITE_ERROR;
5981 goto zipfile_step_out;
5982 }
5983 }else{
5984 if( zName[nName-1]!='/' ){
5985 zName = zFree = sqlite3_mprintf("%s/", zName);
5986 nName++;
5987 if( zName==0 ){
5988 rc = SQLITE_NOMEM;
5989 goto zipfile_step_out;
5990 }
5991 }else{
5992 while( nName>1 && zName[nName-2]=='/' ) nName--;
5993 }
5994 }
5995
5996 /* Assemble the ZipfileEntry object for the new zip archive entry */
5997 e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5998 e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5999 e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
6000 e.cds.iCompression = (u16)iMethod;
6001 zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
6002 e.cds.crc32 = iCrc32;
6003 e.cds.szCompressed = nData;
6004 e.cds.szUncompressed = szUncompressed;
6005 e.cds.iExternalAttr = (mode<<16);
6006 e.cds.iOffset = p->body.n;
6007 e.cds.nFile = (u16)nName;
6008 e.cds.zFile = zName;
6009
6010 /* Append the LFH to the body of the new archive */
6011 nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
6012 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
6013 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
6014
6015 /* Append the data to the body of the new archive */
6016 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6017 memcpy(&p->body.a[p->body.n], aData, nData);
6018 p->body.n += nData;
6019
6020 /* Append the CDS record to the directory of the new archive */
6021 nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
6022 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
6023 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
6024
6025 /* Increment the count of entries in the archive */
6026 p->nEntry++;
6027
6028 zipfile_step_out:
6029 sqlite3_free(aFree);
6030 sqlite3_free(zFree);
6031 if( rc ){
6032 if( zErr ){
6033 sqlite3_result_error(pCtx, zErr, -1);
6034 }else{
6035 sqlite3_result_error_code(pCtx, rc);
6036 }
6037 }
6038 sqlite3_free(zErr);
6039 }
6040
6041 /*
6042 ** xFinalize() callback for zipfile aggregate function.
6043 */
6044 void zipfileFinal(sqlite3_context *pCtx){
6045 ZipfileCtx *p;
6046 ZipfileEOCD eocd;
6047 int nZip;
6048 u8 *aZip;
6049
6050 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6051 if( p==0 ) return;
6052 if( p->nEntry>0 ){
6053 memset(&eocd, 0, sizeof(eocd));
6054 eocd.nEntry = (u16)p->nEntry;
6055 eocd.nEntryTotal = (u16)p->nEntry;
6056 eocd.nSize = p->cds.n;
6057 eocd.iOffset = p->body.n;
6058
6059 nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
6060 aZip = (u8*)sqlite3_malloc(nZip);
6061 if( aZip==0 ){
6062 sqlite3_result_error_nomem(pCtx);
6063 }else{
6064 memcpy(aZip, p->body.a, p->body.n);
6065 memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
6066 zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
6067 sqlite3_result_blob(pCtx, aZip, nZip, zipfileFree);
6068 }
6069 }
6070
6071 sqlite3_free(p->body.a);
6072 sqlite3_free(p->cds.a);
6073 }
6074
6075
6076 /*
6077 ** Register the "zipfile" virtual table.
6078 */
6079 static int zipfileRegister(sqlite3 *db){
@@ -5595,11 +6088,11 @@
6088 zipfileClose, /* xClose - close a cursor */
6089 zipfileFilter, /* xFilter - configure scan constraints */
6090 zipfileNext, /* xNext - advance a cursor */
6091 zipfileEof, /* xEof - check for end of scan */
6092 zipfileColumn, /* xColumn - read data */
6093 0, /* xRowid - read data */
6094 zipfileUpdate, /* xUpdate */
6095 zipfileBegin, /* xBegin */
6096 0, /* xSync */
6097 zipfileCommit, /* xCommit */
6098 zipfileRollback, /* xRollback */
@@ -5606,12 +6099,15 @@
6099 zipfileFindFunction, /* xFindMethod */
6100 0, /* xRename */
6101 };
6102
6103 int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
6104 if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
6105 if( rc==SQLITE_OK ){
6106 rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
6107 zipfileStep, zipfileFinal
6108 );
6109 }
6110 return rc;
6111 }
6112 #else /* SQLITE_OMIT_VIRTUALTABLE */
6113 # define zipfileRegister(x) SQLITE_OK
@@ -7976,10 +8472,11 @@
8472 */
8473 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
8474 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
8475 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
8476 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
8477 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
8478
8479 /*
8480 ** These are the allowed shellFlgs values
8481 */
8482 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
@@ -8082,10 +8579,11 @@
8579 ** If the EDITOR argument is omitted, use the value in the VISUAL
8580 ** environment variable. If still there is no EDITOR, through an error.
8581 **
8582 ** Also throw an error if the EDITOR program returns a non-zero exit code.
8583 */
8584 #ifndef SQLITE_NOHAVE_SYSTEM
8585 static void editFunc(
8586 sqlite3_context *context,
8587 int argc,
8588 sqlite3_value **argv
8589 ){
@@ -8179,22 +8677,24 @@
8677 if( x!=sz ){
8678 sqlite3_result_error(context, "could not read back the whole file", -1);
8679 goto edit_func_end;
8680 }
8681 if( bBin ){
8682 sqlite3_result_blob64(context, p, sz, sqlite3_free);
8683 }else{
8684 sqlite3_result_text64(context, (const char*)p, sz,
8685 sqlite3_free, SQLITE_UTF8);
8686 }
8687 p = 0;
8688
8689 edit_func_end:
8690 if( f ) fclose(f);
8691 unlink(zTempFile);
8692 sqlite3_free(zTempFile);
8693 sqlite3_free(p);
8694 }
8695 #endif /* SQLITE_NOHAVE_SYSTEM */
8696
8697 /*
8698 ** Save or restore the current output mode
8699 */
8700 static void outputModePush(ShellState *p){
@@ -9176,33 +9676,58 @@
9676 ShellState *pArg, /* Pointer to ShellState */
9677 int bReset /* True to reset the stats */
9678 ){
9679 int iCur;
9680 int iHiwtr;
9681 FILE *out;
9682 if( pArg==0 || pArg->out==0 ) return 0;
9683 out = pArg->out;
9684
9685 if( pArg->pStmt && (pArg->statsOn & 2) ){
9686 int nCol, i, x;
9687 sqlite3_stmt *pStmt = pArg->pStmt;
9688 char z[100];
9689 nCol = sqlite3_column_count(pStmt);
9690 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
9691 for(i=0; i<nCol; i++){
9692 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
9693 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
9694 #ifndef SQLITE_OMIT_DECLTYPE
9695 sqlite3_snprintf(30, z+x, "declared type:");
9696 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
9697 #endif
9698 #ifdef SQLITE_ENABLE_COLUMN_METADATA
9699 sqlite3_snprintf(30, z+x, "database name:");
9700 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
9701 sqlite3_snprintf(30, z+x, "table name:");
9702 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
9703 sqlite3_snprintf(30, z+x, "origin name:");
9704 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
9705 #endif
9706 }
9707 }
9708
9709 displayStatLine(pArg, "Memory Used:",
9710 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
9711 displayStatLine(pArg, "Number of Outstanding Allocations:",
9712 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
9713 if( pArg->shellFlgs & SHFLG_Pagecache ){
9714 displayStatLine(pArg, "Number of Pcache Pages Used:",
9715 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
9716 }
9717 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
9718 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
9719 displayStatLine(pArg, "Largest Allocation:",
9720 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
9721 displayStatLine(pArg, "Largest Pcache Allocation:",
9722 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
9723 #ifdef YYTRACKMAXSTACKDEPTH
9724 displayStatLine(pArg, "Deepest Parser Stack:",
9725 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
9726 #endif
 
9727
9728 if( db ){
9729 if( pArg->shellFlgs & SHFLG_Lookaside ){
9730 iHiwtr = iCur = -1;
9731 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
9732 &iCur, &iHiwtr, bReset);
9733 raw_printf(pArg->out,
@@ -9233,29 +9758,38 @@
9758 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
9759 iHiwtr = iCur = -1;
9760 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
9761 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
9762 iHiwtr = iCur = -1;
9763 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
9764 raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
9765 iHiwtr = iCur = -1;
9766 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
9767 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
9768 iCur);
9769 iHiwtr = iCur = -1;
9770 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
9771 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
9772 iCur);
9773 }
9774
9775 if( pArg->pStmt ){
9776 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
9777 bReset);
9778 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
9779 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
9780 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
9781 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
9782 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
9783 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
9784 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
9785 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE, bReset);
9786 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
9787 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
9788 raw_printf(pArg->out, "Number of times run: %d\n", iCur);
9789 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
9790 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
9791 }
9792
9793 #ifdef __linux__
9794 displayLinuxIoStats(pArg->out);
9795 #endif
@@ -9467,69 +10001,61 @@
10001 /*
10002 ** Run a prepared statement
10003 */
10004 static void exec_prepared_stmt(
10005 ShellState *pArg, /* Pointer to ShellState */
10006 sqlite3_stmt *pStmt /* Statment to run */
 
10007 ){
10008 int rc;
10009
10010 /* perform the first step. this will tell us if we
10011 ** have a result set or not and how wide it is.
10012 */
10013 rc = sqlite3_step(pStmt);
10014 /* if we have a result set... */
10015 if( SQLITE_ROW == rc ){
10016 /* allocate space for col name ptr, value ptr, and type */
10017 int nCol = sqlite3_column_count(pStmt);
10018 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
10019 if( !pData ){
10020 rc = SQLITE_NOMEM;
10021 }else{
10022 char **azCols = (char **)pData; /* Names of result columns */
10023 char **azVals = &azCols[nCol]; /* Results */
10024 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
10025 int i, x;
10026 assert(sizeof(int) <= sizeof(char *));
10027 /* save off ptrs to column names */
10028 for(i=0; i<nCol; i++){
10029 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
10030 }
10031 do{
10032 /* extract the data and data types */
10033 for(i=0; i<nCol; i++){
10034 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
10035 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
10036 azVals[i] = "";
10037 }else{
10038 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
10039 }
10040 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
10041 rc = SQLITE_NOMEM;
10042 break; /* from for */
10043 }
10044 } /* end for */
10045
10046 /* if data and types extracted successfully... */
10047 if( SQLITE_ROW == rc ){
10048 /* call the supplied callback with the result row data */
10049 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
10050 rc = SQLITE_ABORT;
10051 }else{
10052 rc = sqlite3_step(pStmt);
10053 }
10054 }
10055 } while( SQLITE_ROW == rc );
10056 sqlite3_free(pData);
 
 
 
 
 
 
 
10057 }
10058 }
10059 }
10060
10061 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -9671,21 +10197,19 @@
10197 ** This is very similar to SQLite's built-in sqlite3_exec()
10198 ** function except it takes a slightly different callback
10199 ** and callback data argument.
10200 */
10201 static int shell_exec(
 
 
 
 
10202 ShellState *pArg, /* Pointer to ShellState */
10203 const char *zSql, /* SQL to be evaluated */
10204 char **pzErrMsg /* Error msg written here */
10205 ){
10206 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
10207 int rc = SQLITE_OK; /* Return Code */
10208 int rc2;
10209 const char *zLeftover; /* Tail of unprocessed SQL */
10210 sqlite3 *db = pArg->db;
10211
10212 if( pzErrMsg ){
10213 *pzErrMsg = NULL;
10214 }
10215
@@ -9752,11 +10276,11 @@
10276 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
10277 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10278 if( rc==SQLITE_OK ){
10279 pArg->cMode = MODE_Explain;
10280 explain_data_prepare(pArg, pExplain);
10281 exec_prepared_stmt(pArg, pExplain);
10282 explain_data_delete(pArg);
10283 }
10284 sqlite3_finalize(pExplain);
10285 sqlite3_free(zEQP);
10286 }
@@ -9778,11 +10302,11 @@
10302 if( pArg->cMode==MODE_Explain ){
10303 explain_data_prepare(pArg, pStmt);
10304 }
10305 }
10306
10307 exec_prepared_stmt(pArg, pStmt);
10308 explain_data_delete(pArg);
10309
10310 /* print usage stats if stats on */
10311 if( pArg && pArg->statsOn ){
10312 display_stats(db, pArg, 0);
@@ -10041,15 +10565,15 @@
10565
10566 savedDestTable = p->zDestTable;
10567 savedMode = p->mode;
10568 p->zDestTable = sTable.z;
10569 p->mode = p->cMode = MODE_Insert;
10570 rc = shell_exec(p, sSelect.z, 0);
10571 if( (rc&0xff)==SQLITE_CORRUPT ){
10572 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
10573 toggleSelectOrder(p->db);
10574 shell_exec(p, sSelect.z, 0);
10575 toggleSelectOrder(p->db);
10576 }
10577 p->zDestTable = savedDestTable;
10578 p->mode = savedMode;
10579 freeText(&sTable);
@@ -10162,10 +10686,11 @@
10686 ".once (-e|-x|FILE) Output for the next SQL command only to FILE\n"
10687 " or invoke system text editor (-e) or spreadsheet (-x)\n"
10688 " on the output.\n"
10689 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
10690 " The --new option starts with an empty file\n"
10691 " Other options: --readonly --append --zip\n"
10692 ".output ?FILE? Send output to FILE or stdout\n"
10693 ".print STRING... Print literal STRING\n"
10694 ".prompt MAIN CONTINUE Replace the standard prompts\n"
10695 ".quit Exit this program\n"
10696 ".read FILENAME Execute SQL in FILENAME\n"
@@ -10179,14 +10704,18 @@
10704 " separator for both the output mode and .import\n"
10705 #if defined(SQLITE_ENABLE_SESSION)
10706 ".session CMD ... Create or control sessions\n"
10707 #endif
10708 ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n"
10709 #ifndef SQLITE_NOHAVE_SYSTEM
10710 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
10711 #endif
10712 ".show Show the current values for various settings\n"
10713 ".stats ?on|off? Show stats or turn stats on or off\n"
10714 #ifndef SQLITE_NOHAVE_SYSTEM
10715 ".system CMD ARGS... Run CMD ARGS... in a system shell\n"
10716 #endif
10717 ".tables ?TABLE? List names of tables\n"
10718 " If TABLE specified, only list tables matching\n"
10719 " LIKE pattern TABLE.\n"
10720 ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n"
10721 ".timeout MS Try opening locked tables for MS milliseconds\n"
@@ -10311,17 +10840,25 @@
10840 #endif
10841
10842 /*
10843 ** Try to deduce the type of file for zName based on its content. Return
10844 ** one of the SHELL_OPEN_* constants.
10845 **
10846 ** If the file does not exist or is empty but its name looks like a ZIP
10847 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
10848 ** Otherwise, assume an ordinary database regardless of the filename if
10849 ** the type cannot be determined from content.
10850 */
10851 static int deduceDatabaseType(const char *zName, int dfltZip){
10852 FILE *f = fopen(zName, "rb");
10853 size_t n;
10854 int rc = SHELL_OPEN_UNSPEC;
10855 char zBuf[100];
10856 if( f==0 ){
10857 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ) return SHELL_OPEN_ZIPFILE;
10858 return SHELL_OPEN_NORMAL;
10859 }
10860 fseek(f, -25, SEEK_END);
10861 n = fread(zBuf, 25, 1, f);
10862 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
10863 rc = SHELL_OPEN_APPENDVFS;
10864 }else{
@@ -10328,10 +10865,12 @@
10865 fseek(f, -22, SEEK_END);
10866 n = fread(zBuf, 22, 1, f);
10867 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
10868 && zBuf[3]==0x06 ){
10869 rc = SHELL_OPEN_ZIPFILE;
10870 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
10871 return SHELL_OPEN_ZIPFILE;
10872 }
10873 }
10874 fclose(f);
10875 return rc;
10876 }
@@ -10342,11 +10881,11 @@
10881 */
10882 static void open_db(ShellState *p, int keepAlive){
10883 if( p->db==0 ){
10884 sqlite3_initialize();
10885 if( p->openMode==SHELL_OPEN_UNSPEC && access(p->zDbFilename,0)==0 ){
10886 p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 0);
10887 }
10888 switch( p->openMode ){
10889 case SHELL_OPEN_APPENDVFS: {
10890 sqlite3_open_v2(p->zDbFilename, &p->db,
10891 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
@@ -10353,10 +10892,14 @@
10892 break;
10893 }
10894 case SHELL_OPEN_ZIPFILE: {
10895 sqlite3_open(":memory:", &p->db);
10896 break;
10897 }
10898 case SHELL_OPEN_READONLY: {
10899 sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
10900 break;
10901 }
10902 case SHELL_OPEN_UNSPEC:
10903 case SHELL_OPEN_NORMAL: {
10904 sqlite3_open(p->zDbFilename, &p->db);
10905 break;
@@ -10383,14 +10926,16 @@
10926 shellAddSchemaName, 0, 0);
10927 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
10928 shellModuleSchema, 0, 0);
10929 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
10930 shellPutsFunc, 0, 0);
10931 #ifndef SQLITE_NOHAVE_SYSTEM
10932 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
10933 editFunc, 0, 0);
10934 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
10935 editFunc, 0, 0);
10936 #endif
10937 if( p->openMode==SHELL_OPEN_ZIPFILE ){
10938 char *zSql = sqlite3_mprintf(
10939 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
10940 sqlite3_exec(p->db, zSql, 0, 0, 0);
10941 sqlite3_free(zSql);
@@ -11001,10 +11546,11 @@
11546 #ifndef SQLITE_OMIT_POPEN
11547 pclose(p->out);
11548 #endif
11549 }else{
11550 output_file_close(p->out);
11551 #ifndef SQLITE_NOHAVE_SYSTEM
11552 if( p->doXdgOpen ){
11553 const char *zXdgOpenCmd =
11554 #if defined(_WIN32)
11555 "start";
11556 #elif defined(__APPLE__)
@@ -11019,10 +11565,11 @@
11565 }
11566 sqlite3_free(zCmd);
11567 outputModePop(p);
11568 p->doXdgOpen = 0;
11569 }
11570 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
11571 }
11572 p->outfile[0] = 0;
11573 p->out = stdout;
11574 }
11575
@@ -12129,44 +12676,84 @@
12676 " mtime INT, -- last modification time\n"
12677 " sz INT, -- original file size\n"
12678 " data BLOB -- compressed content\n"
12679 ")";
12680 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
12681 const char *zInsertFmt[2] = {
12682 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
12683 " SELECT\n"
12684 " %s,\n"
12685 " mode,\n"
12686 " mtime,\n"
12687 " CASE substr(lsmode(mode),1,1)\n"
12688 " WHEN '-' THEN length(data)\n"
12689 " WHEN 'd' THEN 0\n"
12690 " ELSE -1 END,\n"
12691 " sqlar_compress(data)\n"
12692 " FROM fsdir(%Q,%Q)\n"
12693 " WHERE lsmode(mode) NOT LIKE '?%%';",
12694 "REPLACE INTO %s(name,mode,mtime,data)\n"
12695 " SELECT\n"
12696 " %s,\n"
12697 " mode,\n"
12698 " mtime,\n"
12699 " data\n"
12700 " FROM fsdir(%Q,%Q)\n"
12701 " WHERE lsmode(mode) NOT LIKE '?%%';"
12702 };
12703 int i; /* For iterating through azFile[] */
12704 int rc; /* Return code */
12705 const char *zTab = 0; /* SQL table into which to insert */
12706 char *zSql;
12707 char zTemp[50];
12708
12709 arExecSql(pAr, "PRAGMA page_size=512");
12710 rc = arExecSql(pAr, "SAVEPOINT ar;");
12711 if( rc!=SQLITE_OK ) return rc;
12712 zTemp[0] = 0;
12713 if( pAr->bZip ){
12714 /* Initialize the zipfile virtual table, if necessary */
12715 if( pAr->zFile ){
12716 sqlite3_uint64 r;
12717 sqlite3_randomness(sizeof(r),&r);
12718 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
12719 zTab = zTemp;
12720 zSql = sqlite3_mprintf(
12721 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
12722 zTab, pAr->zFile
12723 );
12724 rc = arExecSql(pAr, zSql);
12725 sqlite3_free(zSql);
12726 }else{
12727 zTab = "zip";
12728 }
12729 }else{
12730 /* Initialize the table for an SQLAR */
12731 zTab = "sqlar";
12732 if( bUpdate==0 ){
12733 rc = arExecSql(pAr, zDrop);
12734 if( rc!=SQLITE_OK ) goto end_ar_transaction;
12735 }
12736 rc = arExecSql(pAr, zCreate);
12737 }
 
12738 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
12739 char *zSql = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
12740 pAr->bVerbose ? "shell_putsnl(name)" : "name",
12741 pAr->azArg[i], pAr->zDir);
12742 rc = arExecSql(pAr, zSql);
12743 sqlite3_free(zSql);
12744 }
12745 end_ar_transaction:
12746 if( rc!=SQLITE_OK ){
12747 arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
12748 }else{
12749 rc = arExecSql(pAr, "RELEASE ar;");
12750 if( pAr->bZip && pAr->zFile ){
12751 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
12752 arExecSql(pAr, zSql);
12753 sqlite3_free(zSql);
12754 }
12755 }
12756 return rc;
12757 }
12758
12759 /*
@@ -12184,24 +12771,21 @@
12771 if( rc==SQLITE_OK ){
12772 int eDbType = SHELL_OPEN_UNSPEC;
12773 cmd.p = pState;
12774 cmd.db = pState->db;
12775 if( cmd.zFile ){
12776 eDbType = deduceDatabaseType(cmd.zFile, 1);
12777 }else{
12778 eDbType = pState->openMode;
12779 }
12780 if( eDbType==SHELL_OPEN_ZIPFILE ){
12781 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
12782 if( cmd.zFile==0 ){
12783 cmd.zSrcTable = sqlite3_mprintf("zip");
12784 }else{
12785 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
12786 }
 
 
 
12787 }
12788 cmd.bZip = 1;
12789 }else if( cmd.zFile ){
12790 int flags;
12791 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
@@ -12222,18 +12806,16 @@
12806 cmd.zFile, sqlite3_errmsg(cmd.db)
12807 );
12808 goto end_ar_command;
12809 }
12810 sqlite3_fileio_init(cmd.db, 0, 0);
 
12811 sqlite3_sqlar_init(cmd.db, 0, 0);
 
12812 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
12813 shellPutsFunc, 0, 0);
12814
12815 }
12816 if( cmd.zSrcTable==0 && cmd.bZip==0 ){
12817 if( cmd.eCmd!=AR_CMD_CREATE
12818 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
12819 ){
12820 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
12821 rc = SQLITE_ERROR;
@@ -12622,11 +13204,11 @@
13204 if( strcmp(azArg[1],"full")==0 ){
13205 p->autoEQP = AUTOEQP_full;
13206 }else if( strcmp(azArg[1],"trigger")==0 ){
13207 p->autoEQP = AUTOEQP_trigger;
13208 }else{
13209 p->autoEQP = (u8)booleanValue(azArg[1]);
13210 }
13211 }else{
13212 raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
13213 rc = 1;
13214 }
@@ -12709,18 +13291,15 @@
13291 raw_printf(p->out, "ANALYZE sqlite_master;\n");
13292 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
13293 callback, &data, &zErrMsg);
13294 data.cMode = data.mode = MODE_Insert;
13295 data.zDestTable = "sqlite_stat1";
13296 shell_exec(p, "SELECT * FROM sqlite_stat1", &zErrMsg);
 
13297 data.zDestTable = "sqlite_stat3";
13298 shell_exec(p, "SELECT * FROM sqlite_stat3", &zErrMsg);
 
13299 data.zDestTable = "sqlite_stat4";
13300 shell_exec(p, "SELECT * FROM sqlite_stat4", &zErrMsg);
 
13301 raw_printf(p->out, "ANALYZE sqlite_master;\n");
13302 }
13303 }else
13304
13305 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
@@ -13197,16 +13776,18 @@
13776 /* Check for command-line arguments */
13777 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
13778 const char *z = azArg[iName];
13779 if( optionMatch(z,"new") ){
13780 newFlag = 1;
13781 #ifdef SQLITE_HAVE_ZLIB
13782 }else if( optionMatch(z, "zip") ){
13783 p->openMode = SHELL_OPEN_ZIPFILE;
13784 #endif
13785 }else if( optionMatch(z, "append") ){
13786 p->openMode = SHELL_OPEN_APPENDVFS;
13787 }else if( optionMatch(z, "readonly") ){
13788 p->openMode = SHELL_OPEN_READONLY;
13789 }else if( z[0]=='-' ){
13790 utf8_printf(stderr, "unknown option: %s\n", z);
13791 rc = 1;
13792 goto meta_command_exit;
13793 }
@@ -13259,10 +13840,11 @@
13840 }else{
13841 p->outCount = 0;
13842 }
13843 output_reset(p);
13844 if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
13845 #ifndef SQLITE_NOHAVE_SYSTEM
13846 if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
13847 p->doXdgOpen = 1;
13848 outputModePush(p);
13849 if( zFile[1]=='x' ){
13850 newTempFile(p, "csv");
@@ -13273,10 +13855,11 @@
13855 newTempFile(p, "txt");
13856 bTxtMode = 1;
13857 }
13858 zFile = p->zTempFile;
13859 }
13860 #endif /* SQLITE_NOHAVE_SYSTEM */
13861 if( zFile[0]=='|' ){
13862 #ifdef SQLITE_OMIT_POPEN
13863 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
13864 rc = 1;
13865 p->out = stdout;
@@ -13392,14 +13975,13 @@
13975 rc = 1;
13976 }
13977 sqlite3_close(pSrc);
13978 }else
13979
 
13980 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
13981 if( nArg==2 ){
13982 p->scanstatsOn = (u8)booleanValue(azArg[1]);
13983 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
13984 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
13985 #endif
13986 }else{
13987 raw_printf(stderr, "Usage: .scanstats on|off\n");
@@ -13983,15 +14565,16 @@
14565 freeText(&sQuery);
14566 freeText(&sSql);
14567 if( bDebug ){
14568 utf8_printf(p->out, "%s\n", zSql);
14569 }else{
14570 shell_exec(p, zSql, 0);
14571 }
14572 sqlite3_free(zSql);
14573 }else
14574
14575 #ifndef SQLITE_NOHAVE_SYSTEM
14576 if( c=='s'
14577 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
14578 ){
14579 char *zCmd;
14580 int i, x;
@@ -14007,10 +14590,11 @@
14590 }
14591 x = system(zCmd);
14592 sqlite3_free(zCmd);
14593 if( x ) raw_printf(stderr, "System command returns %d\n", x);
14594 }else
14595 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
14596
14597 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
14598 static const char *azBool[] = { "off", "on", "trigger", "full"};
14599 int i;
14600 if( nArg!=1 ){
@@ -14046,11 +14630,11 @@
14630 p->zDbFilename ? p->zDbFilename : "");
14631 }else
14632
14633 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
14634 if( nArg==2 ){
14635 p->statsOn = (u8)booleanValue(azArg[1]);
14636 }else if( nArg==1 ){
14637 display_stats(p->db, p, 0);
14638 }else{
14639 raw_printf(stderr, "Usage: .stats ?on|off?\n");
14640 rc = 1;
@@ -14586,10 +15170,20 @@
15170 return 1; /* SQL Server */
15171 }
15172 return 0;
15173 }
15174
15175 /*
15176 ** We need a default sqlite3_complete() implementation to use in case
15177 ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
15178 ** any arbitrary text is a complete SQL statement. This is not very
15179 ** user-friendly, but it does seem to work.
15180 */
15181 #ifdef SQLITE_OMIT_COMPLETE
15182 int sqlite3_complete(const char *zSql){ return 1; }
15183 #endif
15184
15185 /*
15186 ** Return true if zSql is a complete SQL statement. Return false if it
15187 ** ends in the middle of a string literal or C-style comment.
15188 */
15189 static int line_is_complete(char *zSql, int nSql){
@@ -14610,11 +15204,11 @@
15204 char *zErrMsg = 0;
15205
15206 open_db(p, 0);
15207 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
15208 BEGIN_TIMER;
15209 rc = shell_exec(p, zSql, &zErrMsg);
15210 END_TIMER;
15211 if( rc || zErrMsg ){
15212 char zPrefix[100];
15213 if( in!=0 || !stdin_is_interactive ){
15214 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
@@ -14842,10 +15436,14 @@
15436
15437 /*
15438 ** Show available command line options
15439 */
15440 static const char zOptions[] =
15441 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
15442 " -A ARGS... run \".archive ARGS\" and exit\n"
15443 #endif
15444 " -append append the database to the end of the file\n"
15445 " -ascii set output mode to 'ascii'\n"
15446 " -bail stop after hitting an error\n"
15447 " -batch force batch I/O\n"
15448 " -column set output mode to 'column'\n"
15449 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
@@ -14868,17 +15466,21 @@
15466 #endif
15467 " -newline SEP set output row separator. Default: '\\n'\n"
15468 " -nullvalue TEXT set text string for NULL values. Default ''\n"
15469 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
15470 " -quote set output mode to 'quote'\n"
15471 " -readonly open the database read-only\n"
15472 " -separator SEP set output column separator. Default: '|'\n"
15473 " -stats print memory stats before each finalize\n"
15474 " -version show SQLite version\n"
15475 " -vfs NAME use NAME as the default VFS\n"
15476 #ifdef SQLITE_ENABLE_VFSTRACE
15477 " -vfstrace enable tracing of all VFS calls\n"
15478 #endif
15479 #ifdef SQLITE_HAVE_ZLIB
15480 " -zip open the file as a ZIP Archive\n"
15481 #endif
15482 ;
15483 static void usage(int showDetail){
15484 utf8_printf(stderr,
15485 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
15486 "FILENAME is the name of an SQLite database. A new database is created\n"
@@ -14977,25 +15579,43 @@
15579 sqlite3_sourceid(), SQLITE_SOURCE_ID);
15580 exit(1);
15581 }
15582 #endif
15583 main_init(&data);
15584
15585 /* On Windows, we must translate command-line arguments into UTF-8.
15586 ** The SQLite memory allocator subsystem has to be enabled in order to
15587 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
15588 ** subsequent sqlite3_config() calls will work. So copy all results into
15589 ** memory that does not come from the SQLite memory allocator.
15590 */
15591 #if !SQLITE_SHELL_IS_UTF8
15592 sqlite3_initialize();
15593 argv = malloc(sizeof(argv[0])*argc);
15594 if( argv==0 ){
15595 raw_printf(stderr, "out of memory\n");
15596 exit(1);
15597 }
15598 for(i=0; i<argc; i++){
15599 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
15600 int n;
15601 if( z==0 ){
15602 raw_printf(stderr, "out of memory\n");
15603 exit(1);
15604 }
15605 n = (int)strlen(z);
15606 argv[i] = malloc( n+1 );
15607 if( argv[i]==0 ){
15608 raw_printf(stderr, "out of memory\n");
15609 exit(1);
15610 }
15611 memcpy(argv[i], z, n+1);
15612 sqlite3_free(z);
15613 }
15614 sqlite3_shutdown();
15615 #endif
15616
15617 assert( argc>=1 && argv && argv[0] );
15618 Argv0 = argv[0];
15619
15620 /* Make sure we have a valid signal handler early, before anything
15621 ** else is done.
@@ -15111,16 +15731,24 @@
15731 sqlite3_vfs_register(pVfs, 1);
15732 }else{
15733 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
15734 exit(1);
15735 }
15736 #ifdef SQLITE_HAVE_ZLIB
15737 }else if( strcmp(z,"-zip")==0 ){
15738 data.openMode = SHELL_OPEN_ZIPFILE;
15739 #endif
15740 }else if( strcmp(z,"-append")==0 ){
15741 data.openMode = SHELL_OPEN_APPENDVFS;
15742 }else if( strcmp(z,"-readonly")==0 ){
15743 data.openMode = SHELL_OPEN_READONLY;
15744 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
15745 }else if( strncmp(z, "-A",2)==0 ){
15746 /* All remaining command-line arguments are passed to the ".archive"
15747 ** command, so ignore them */
15748 break;
15749 #endif
15750 }
15751 }
15752 if( data.zDbFilename==0 ){
15753 #ifndef SQLITE_OMIT_MEMORYDB
15754 data.zDbFilename = ":memory:";
@@ -15170,11 +15798,11 @@
15798 }else if( strcmp(z,"-column")==0 ){
15799 data.mode = MODE_Column;
15800 }else if( strcmp(z,"-csv")==0 ){
15801 data.mode = MODE_Csv;
15802 memcpy(data.colSeparator,",",2);
15803 #ifdef SQLITE_HAVE_ZLIB
15804 }else if( strcmp(z,"-zip")==0 ){
15805 data.openMode = SHELL_OPEN_ZIPFILE;
15806 #endif
15807 }else if( strcmp(z,"-append")==0 ){
15808 data.openMode = SHELL_OPEN_APPENDVFS;
@@ -15253,19 +15881,36 @@
15881 if( z[0]=='.' ){
15882 rc = do_meta_command(z, &data);
15883 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
15884 }else{
15885 open_db(&data, 0);
15886 rc = shell_exec(&data, z, &zErrMsg);
15887 if( zErrMsg!=0 ){
15888 utf8_printf(stderr,"Error: %s\n", zErrMsg);
15889 if( bail_on_error ) return rc!=0 ? rc : 1;
15890 }else if( rc!=0 ){
15891 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
15892 if( bail_on_error ) return rc;
15893 }
15894 }
15895 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
15896 }else if( strncmp(z, "-A", 2)==0 ){
15897 if( nCmd>0 ){
15898 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
15899 " with \"%s\"\n", z);
15900 return 1;
15901 }
15902 open_db(&data, 0);
15903 if( z[2] ){
15904 argv[i] = &z[2];
15905 arDotCommand(&data, argv+(i-1), argc-(i-1));
15906 }else{
15907 arDotCommand(&data, argv+i, argc-i);
15908 }
15909 readStdin = 0;
15910 break;
15911 #endif
15912 }else{
15913 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
15914 raw_printf(stderr,"Use -help for a list of options.\n");
15915 return 1;
15916 }
@@ -15281,11 +15926,11 @@
15926 if( azCmd[i][0]=='.' ){
15927 rc = do_meta_command(azCmd[i], &data);
15928 if( rc ) return rc==2 ? 0 : rc;
15929 }else{
15930 open_db(&data, 0);
15931 rc = shell_exec(&data, azCmd[i], &zErrMsg);
15932 if( zErrMsg!=0 ){
15933 utf8_printf(stderr,"Error: %s\n", zErrMsg);
15934 return rc!=0 ? rc : 1;
15935 }else if( rc!=0 ){
15936 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
@@ -15344,10 +15989,10 @@
15989 find_home_dir(1);
15990 output_reset(&data);
15991 data.doXdgOpen = 0;
15992 clearTempFile(&data);
15993 #if !SQLITE_SHELL_IS_UTF8
15994 for(i=0; i<argc; i++) free(argv[i]);
15995 free(argv);
15996 #endif
15997 return rc;
15998 }
15999
+1884 -639
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.22.0. By combining all the individual C code files into this
3
+** version 3.23.0. By combining all the individual C code files into this
44
** single large file, the entire code can be compiled as a single translation
55
** unit. This allows many compilers to do optimizations that would not be
66
** possible if the files were compiled separately. Performance improvements
77
** of 5% or more are commonly seen when SQLite is compiled as a single
88
** translation unit.
@@ -1145,13 +1145,13 @@
11451145
**
11461146
** See also: [sqlite3_libversion()],
11471147
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
11481148
** [sqlite_version()] and [sqlite_source_id()].
11491149
*/
1150
-#define SQLITE_VERSION "3.22.0"
1151
-#define SQLITE_VERSION_NUMBER 3022000
1152
-#define SQLITE_SOURCE_ID "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d"
1150
+#define SQLITE_VERSION "3.23.0"
1151
+#define SQLITE_VERSION_NUMBER 3023000
1152
+#define SQLITE_SOURCE_ID "2018-03-17 16:26:36 442e816b5fed80ebeb58c7c0ab9c2ef999bf488519bf5da670e9cec477034540"
11531153
11541154
/*
11551155
** CAPI3REF: Run-Time Library Version Numbers
11561156
** KEYWORDS: sqlite3_version sqlite3_sourceid
11571157
**
@@ -3518,20 +3518,20 @@
35183518
/*
35193519
** CAPI3REF: Formatted String Printing Functions
35203520
**
35213521
** These routines are work-alikes of the "printf()" family of functions
35223522
** from the standard C library.
3523
-** These routines understand most of the common K&R formatting options,
3524
-** plus some additional non-standard formats, detailed below.
3525
-** Note that some of the more obscure formatting options from recent
3526
-** C-library standards are omitted from this implementation.
3523
+** These routines understand most of the common formatting options from
3524
+** the standard library printf()
3525
+** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3526
+** See the [built-in printf()] documentation for details.
35273527
**
35283528
** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
3529
-** results into memory obtained from [sqlite3_malloc()].
3529
+** results into memory obtained from [sqlite3_malloc64()].
35303530
** The strings returned by these two routines should be
35313531
** released by [sqlite3_free()]. ^Both routines return a
3532
-** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
3532
+** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
35333533
** memory to hold the resulting string.
35343534
**
35353535
** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
35363536
** the standard C library. The result is written into the
35373537
** buffer supplied as the second parameter whose size is given by
@@ -3551,75 +3551,11 @@
35513551
** the zero terminator. So the longest string that can be completely
35523552
** written will be n-1 characters.
35533553
**
35543554
** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
35553555
**
3556
-** These routines all implement some additional formatting
3557
-** options that are useful for constructing SQL statements.
3558
-** All of the usual printf() formatting options apply. In addition, there
3559
-** is are "%q", "%Q", "%w" and "%z" options.
3560
-**
3561
-** ^(The %q option works like %s in that it substitutes a nul-terminated
3562
-** string from the argument list. But %q also doubles every '\'' character.
3563
-** %q is designed for use inside a string literal.)^ By doubling each '\''
3564
-** character it escapes that character and allows it to be inserted into
3565
-** the string.
3566
-**
3567
-** For example, assume the string variable zText contains text as follows:
3568
-**
3569
-** <blockquote><pre>
3570
-** char *zText = "It's a happy day!";
3571
-** </pre></blockquote>
3572
-**
3573
-** One can use this text in an SQL statement as follows:
3574
-**
3575
-** <blockquote><pre>
3576
-** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
3577
-** sqlite3_exec(db, zSQL, 0, 0, 0);
3578
-** sqlite3_free(zSQL);
3579
-** </pre></blockquote>
3580
-**
3581
-** Because the %q format string is used, the '\'' character in zText
3582
-** is escaped and the SQL generated is as follows:
3583
-**
3584
-** <blockquote><pre>
3585
-** INSERT INTO table1 VALUES('It''s a happy day!')
3586
-** </pre></blockquote>
3587
-**
3588
-** This is correct. Had we used %s instead of %q, the generated SQL
3589
-** would have looked like this:
3590
-**
3591
-** <blockquote><pre>
3592
-** INSERT INTO table1 VALUES('It's a happy day!');
3593
-** </pre></blockquote>
3594
-**
3595
-** This second example is an SQL syntax error. As a general rule you should
3596
-** always use %q instead of %s when inserting text into a string literal.
3597
-**
3598
-** ^(The %Q option works like %q except it also adds single quotes around
3599
-** the outside of the total string. Additionally, if the parameter in the
3600
-** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
3601
-** single quotes).)^ So, for example, one could say:
3602
-**
3603
-** <blockquote><pre>
3604
-** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
3605
-** sqlite3_exec(db, zSQL, 0, 0, 0);
3606
-** sqlite3_free(zSQL);
3607
-** </pre></blockquote>
3608
-**
3609
-** The code above will render a correct SQL statement in the zSQL
3610
-** variable even if the zText variable is a NULL pointer.
3611
-**
3612
-** ^(The "%w" formatting option is like "%q" except that it expects to
3613
-** be contained within double-quotes instead of single quotes, and it
3614
-** escapes the double-quote character instead of the single-quote
3615
-** character.)^ The "%w" formatting option is intended for safely inserting
3616
-** table and column names into a constructed SQL statement.
3617
-**
3618
-** ^(The "%z" formatting option works like "%s" but with the
3619
-** addition that after the string has been read and copied into
3620
-** the result, [sqlite3_free()] is called on the input string.)^
3556
+** See also: [built-in printf()], [printf() SQL function]
36213557
*/
36223558
SQLITE_API char *sqlite3_mprintf(const char*,...);
36233559
SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
36243560
SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
36253561
SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
@@ -4681,17 +4617,17 @@
46814617
** ^The specific value of WHERE-clause [parameter] might influence the
46824618
** choice of query plan if the parameter is the left-hand side of a [LIKE]
46834619
** or [GLOB] operator or if the parameter is compared to an indexed column
46844620
** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
46854621
** </li>
4622
+** </ol>
46864623
**
46874624
** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
46884625
** the extra prepFlags parameter, which is a bit array consisting of zero or
46894626
** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
46904627
** sqlite3_prepare_v2() interface works exactly the same as
46914628
** sqlite3_prepare_v3() with a zero prepFlags parameter.
4692
-** </ol>
46934629
*/
46944630
SQLITE_API int sqlite3_prepare(
46954631
sqlite3 *db, /* Database handle */
46964632
const char *zSql, /* SQL statement, UTF-8 encoded */
46974633
int nByte, /* Maximum length of zSql in bytes. */
@@ -8315,10 +8251,19 @@
83158251
** transaction rollback or database recovery operations are not included.
83168252
** If an IO or other error occurs while writing a page to disk, the effect
83178253
** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
83188254
** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
83198255
** </dd>
8256
+**
8257
+** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
8258
+** <dd>This parameter returns the number of dirty cache entries that have
8259
+** been written to disk in the middle of a transaction due to the page
8260
+** cache overflowing. Transactions are more efficient if they are written
8261
+** to disk all at once. When pages spill mid-transaction, that introduces
8262
+** additional overhead. This parameter can be used help identify
8263
+** inefficiencies that can be resolve by increasing the cache size.
8264
+** </dd>
83208265
**
83218266
** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
83228267
** <dd>This parameter returns zero for the current value if and only if
83238268
** all foreign key constraints (deferred or immediate) have been
83248269
** resolved.)^ ^The highwater mark is always 0.
@@ -8335,11 +8280,12 @@
83358280
#define SQLITE_DBSTATUS_CACHE_HIT 7
83368281
#define SQLITE_DBSTATUS_CACHE_MISS 8
83378282
#define SQLITE_DBSTATUS_CACHE_WRITE 9
83388283
#define SQLITE_DBSTATUS_DEFERRED_FKS 10
83398284
#define SQLITE_DBSTATUS_CACHE_USED_SHARED 11
8340
-#define SQLITE_DBSTATUS_MAX 11 /* Largest defined DBSTATUS */
8285
+#define SQLITE_DBSTATUS_CACHE_SPILL 12
8286
+#define SQLITE_DBSTATUS_MAX 12 /* Largest defined DBSTATUS */
83418287
83428288
83438289
/*
83448290
** CAPI3REF: Prepared Statement Status
83458291
** METHOD: sqlite3_stmt
@@ -9815,10 +9761,132 @@
98159761
**
98169762
** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
98179763
*/
98189764
SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
98199765
9766
+/*
9767
+** CAPI3REF: Serialize a database
9768
+**
9769
+** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
9770
+** that is a serialization of the S database on [database connection] D.
9771
+** If P is not a NULL pointer, then the size of the database in bytes
9772
+** is written into *P.
9773
+**
9774
+** For an ordinary on-disk database file, the serialization is just a
9775
+** copy of the disk file. For an in-memory database or a "TEMP" database,
9776
+** the serialization is the same sequence of bytes which would be written
9777
+** to disk if that database where backed up to disk.
9778
+**
9779
+** The usual case is that sqlite3_serialize() copies the serialization of
9780
+** the database into memory obtained from [sqlite3_malloc64()] and returns
9781
+** a pointer to that memory. The caller is responsible for freeing the
9782
+** returned value to avoid a memory leak. However, if the F argument
9783
+** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
9784
+** are made, and the sqlite3_serialize() function will return a pointer
9785
+** to the contiguous memory representation of the database that SQLite
9786
+** is currently using for that database, or NULL if the no such contiguous
9787
+** memory representation of the database exists. A contiguous memory
9788
+** representation of the database will usually only exist if there has
9789
+** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
9790
+** values of D and S.
9791
+** The size of the database is written into *P even if the
9792
+** SQLITE_SERIALIZE_NOCOPY bit is set but no contigious copy
9793
+** of the database exists.
9794
+**
9795
+** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
9796
+** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
9797
+** allocation error occurs.
9798
+**
9799
+** This interface is only available if SQLite is compiled with the
9800
+** [SQLITE_ENABLE_DESERIALIZE] option.
9801
+*/
9802
+SQLITE_API unsigned char *sqlite3_serialize(
9803
+ sqlite3 *db, /* The database connection */
9804
+ const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */
9805
+ sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
9806
+ unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */
9807
+);
9808
+
9809
+/*
9810
+** CAPI3REF: Flags for sqlite3_serialize
9811
+**
9812
+** Zero or more of the following constants can be OR-ed together for
9813
+** the F argument to [sqlite3_serialize(D,S,P,F)].
9814
+**
9815
+** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
9816
+** a pointer to contiguous in-memory database that it is currently using,
9817
+** without making a copy of the database. If SQLite is not currently using
9818
+** a contiguous in-memory database, then this option causes
9819
+** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be
9820
+** using a contiguous in-memory database if it has been initialized by a
9821
+** prior call to [sqlite3_deserialize()].
9822
+*/
9823
+#define SQLITE_SERIALIZE_NOCOPY 0x001 /* Do no memory allocations */
9824
+
9825
+/*
9826
+** CAPI3REF: Deserialize a database
9827
+**
9828
+** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
9829
+** [database connection] D to disconnection from database S and then
9830
+** reopen S as an in-memory database based on the serialization contained
9831
+** in P. The serialized database P is N bytes in size. M is the size of
9832
+** the buffer P, which might be larger than N. If M is larger than N, and
9833
+** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
9834
+** permitted to add content to the in-memory database as long as the total
9835
+** size does not exceed M bytes.
9836
+**
9837
+** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
9838
+** invoke sqlite3_free() on the serialization buffer when the database
9839
+** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
9840
+** SQLite will try to increase the buffer size using sqlite3_realloc64()
9841
+** if writes on the database cause it to grow larger than M bytes.
9842
+**
9843
+** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
9844
+** database is currently in a read transaction or is involved in a backup
9845
+** operation.
9846
+**
9847
+** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
9848
+** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
9849
+** [sqlite3_free()] is invoked on argument P prior to returning.
9850
+**
9851
+** This interface is only available if SQLite is compiled with the
9852
+** [SQLITE_ENABLE_DESERIALIZE] option.
9853
+*/
9854
+SQLITE_API int sqlite3_deserialize(
9855
+ sqlite3 *db, /* The database connection */
9856
+ const char *zSchema, /* Which DB to reopen with the deserialization */
9857
+ unsigned char *pData, /* The serialized database content */
9858
+ sqlite3_int64 szDb, /* Number bytes in the deserialization */
9859
+ sqlite3_int64 szBuf, /* Total size of buffer pData[] */
9860
+ unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
9861
+);
9862
+
9863
+/*
9864
+** CAPI3REF: Flags for sqlite3_deserialize()
9865
+**
9866
+** The following are allowed values for 6th argument (the F argument) to
9867
+** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
9868
+**
9869
+** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
9870
+** in the P argument is held in memory obtained from [sqlite3_malloc64()]
9871
+** and that SQLite should take ownership of this memory and automatically
9872
+** free it when it has finished using it. Without this flag, the caller
9873
+** is resposible for freeing any dynamically allocated memory.
9874
+**
9875
+** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
9876
+** grow the size of the database using calls to [sqlite3_realloc64()]. This
9877
+** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
9878
+** Without this flag, the deserialized database cannot increase in size beyond
9879
+** the number of bytes specified by the M parameter.
9880
+**
9881
+** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
9882
+** should be treated as read-only.
9883
+*/
9884
+#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
9885
+#define SQLITE_DESERIALIZE_RESIZEABLE 2 /* Resize using sqlite3_realloc64() */
9886
+#define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
9887
+
98209888
/*
98219889
** Undo the hack that converts floating point types to integer for
98229890
** builds on processors without floating point support.
98239891
*/
98249892
#ifdef SQLITE_OMIT_FLOATING_POINT
@@ -9962,20 +10030,27 @@
996210030
#endif
996310031
996410032
996510033
/*
996610034
** CAPI3REF: Session Object Handle
10035
+**
10036
+** An instance of this object is a [session] that can be used to
10037
+** record changes to a database.
996710038
*/
996810039
typedef struct sqlite3_session sqlite3_session;
996910040
997010041
/*
997110042
** CAPI3REF: Changeset Iterator Handle
10043
+**
10044
+** An instance of this object acts as a cursor for iterating
10045
+** over the elements of a [changeset] or [patchset].
997210046
*/
997310047
typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
997410048
997510049
/*
997610050
** CAPI3REF: Create A New Session Object
10051
+** CONSTRUCTOR: sqlite3_session
997710052
**
997810053
** Create a new session object attached to database handle db. If successful,
997910054
** a pointer to the new object is written to *ppSession and SQLITE_OK is
998010055
** returned. If an error occurs, *ppSession is set to NULL and an SQLite
998110056
** error code (e.g. SQLITE_NOMEM) is returned.
@@ -10008,10 +10083,11 @@
1000810083
sqlite3_session **ppSession /* OUT: New session object */
1000910084
);
1001010085
1001110086
/*
1001210087
** CAPI3REF: Delete A Session Object
10088
+** DESTRUCTOR: sqlite3_session
1001310089
**
1001410090
** Delete a session object previously allocated using
1001510091
** [sqlite3session_create()]. Once a session object has been deleted, the
1001610092
** results of attempting to use pSession with any other session module
1001710093
** function are undefined.
@@ -10023,10 +10099,11 @@
1002310099
SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
1002410100
1002510101
1002610102
/*
1002710103
** CAPI3REF: Enable Or Disable A Session Object
10104
+** METHOD: sqlite3_session
1002810105
**
1002910106
** Enable or disable the recording of changes by a session object. When
1003010107
** enabled, a session object records changes made to the database. When
1003110108
** disabled - it does not. A newly created session object is enabled.
1003210109
** Refer to the documentation for [sqlite3session_changeset()] for further
@@ -10042,10 +10119,11 @@
1004210119
*/
1004310120
SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
1004410121
1004510122
/*
1004610123
** CAPI3REF: Set Or Clear the Indirect Change Flag
10124
+** METHOD: sqlite3_session
1004710125
**
1004810126
** Each change recorded by a session object is marked as either direct or
1004910127
** indirect. A change is marked as indirect if either:
1005010128
**
1005110129
** <ul>
@@ -10071,10 +10149,11 @@
1007110149
*/
1007210150
SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
1007310151
1007410152
/*
1007510153
** CAPI3REF: Attach A Table To A Session Object
10154
+** METHOD: sqlite3_session
1007610155
**
1007710156
** If argument zTab is not NULL, then it is the name of a table to attach
1007810157
** to the session object passed as the first argument. All subsequent changes
1007910158
** made to the table while the session object is enabled will be recorded. See
1008010159
** documentation for [sqlite3session_changeset()] for further details.
@@ -10133,10 +10212,11 @@
1013310212
const char *zTab /* Table name */
1013410213
);
1013510214
1013610215
/*
1013710216
** CAPI3REF: Set a table filter on a Session Object.
10217
+** METHOD: sqlite3_session
1013810218
**
1013910219
** The second argument (xFilter) is the "filter callback". For changes to rows
1014010220
** in tables that are not attached to the Session object, the filter is called
1014110221
** to determine whether changes to the table's rows should be tracked or not.
1014210222
** If xFilter returns 0, changes is not tracked. Note that once a table is
@@ -10151,10 +10231,11 @@
1015110231
void *pCtx /* First argument passed to xFilter */
1015210232
);
1015310233
1015410234
/*
1015510235
** CAPI3REF: Generate A Changeset From A Session Object
10236
+** METHOD: sqlite3_session
1015610237
**
1015710238
** Obtain a changeset containing changes to the tables attached to the
1015810239
** session object passed as the first argument. If successful,
1015910240
** set *ppChangeset to point to a buffer containing the changeset
1016010241
** and *pnChangeset to the size of the changeset in bytes before returning
@@ -10260,11 +10341,12 @@
1026010341
int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
1026110342
void **ppChangeset /* OUT: Buffer containing changeset */
1026210343
);
1026310344
1026410345
/*
10265
-** CAPI3REF: Load The Difference Between Tables Into A Session
10346
+** CAPI3REF: Load The Difference Between Tables Into A Session
10347
+** METHOD: sqlite3_session
1026610348
**
1026710349
** If it is not already attached to the session object passed as the first
1026810350
** argument, this function attaches table zTbl in the same manner as the
1026910351
** [sqlite3session_attach()] function. If zTbl does not exist, or if it
1027010352
** does not have a primary key, this function is a no-op (but does not return
@@ -10325,10 +10407,11 @@
1032510407
);
1032610408
1032710409
1032810410
/*
1032910411
** CAPI3REF: Generate A Patchset From A Session Object
10412
+** METHOD: sqlite3_session
1033010413
**
1033110414
** The differences between a patchset and a changeset are that:
1033210415
**
1033310416
** <ul>
1033410417
** <li> DELETE records consist of the primary key fields only. The
@@ -10376,10 +10459,11 @@
1037610459
*/
1037710460
SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
1037810461
1037910462
/*
1038010463
** CAPI3REF: Create An Iterator To Traverse A Changeset
10464
+** CONSTRUCTOR: sqlite3_changeset_iter
1038110465
**
1038210466
** Create an iterator used to iterate through the contents of a changeset.
1038310467
** If successful, *pp is set to point to the iterator handle and SQLITE_OK
1038410468
** is returned. Otherwise, if an error occurs, *pp is set to zero and an
1038510469
** SQLite error code is returned.
@@ -10416,10 +10500,11 @@
1041610500
);
1041710501
1041810502
1041910503
/*
1042010504
** CAPI3REF: Advance A Changeset Iterator
10505
+** METHOD: sqlite3_changeset_iter
1042110506
**
1042210507
** This function may only be used with iterators created by function
1042310508
** [sqlite3changeset_start()]. If it is called on an iterator passed to
1042410509
** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
1042510510
** is returned and the call has no effect.
@@ -10440,10 +10525,11 @@
1044010525
*/
1044110526
SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
1044210527
1044310528
/*
1044410529
** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
10530
+** METHOD: sqlite3_changeset_iter
1044510531
**
1044610532
** The pIter argument passed to this function may either be an iterator
1044710533
** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
1044810534
** created by [sqlite3changeset_start()]. In the latter case, the most recent
1044910535
** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
@@ -10474,10 +10560,11 @@
1047410560
int *pbIndirect /* OUT: True for an 'indirect' change */
1047510561
);
1047610562
1047710563
/*
1047810564
** CAPI3REF: Obtain The Primary Key Definition Of A Table
10565
+** METHOD: sqlite3_changeset_iter
1047910566
**
1048010567
** For each modified table, a changeset includes the following:
1048110568
**
1048210569
** <ul>
1048310570
** <li> The number of columns in the table, and
@@ -10505,10 +10592,11 @@
1050510592
int *pnCol /* OUT: Number of entries in output array */
1050610593
);
1050710594
1050810595
/*
1050910596
** CAPI3REF: Obtain old.* Values From A Changeset Iterator
10597
+** METHOD: sqlite3_changeset_iter
1051010598
**
1051110599
** The pIter argument passed to this function may either be an iterator
1051210600
** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
1051310601
** created by [sqlite3changeset_start()]. In the latter case, the most recent
1051410602
** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
@@ -10535,10 +10623,11 @@
1053510623
sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
1053610624
);
1053710625
1053810626
/*
1053910627
** CAPI3REF: Obtain new.* Values From A Changeset Iterator
10628
+** METHOD: sqlite3_changeset_iter
1054010629
**
1054110630
** The pIter argument passed to this function may either be an iterator
1054210631
** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
1054310632
** created by [sqlite3changeset_start()]. In the latter case, the most recent
1054410633
** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
@@ -10568,10 +10657,11 @@
1056810657
sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
1056910658
);
1057010659
1057110660
/*
1057210661
** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
10662
+** METHOD: sqlite3_changeset_iter
1057310663
**
1057410664
** This function should only be used with iterator objects passed to a
1057510665
** conflict-handler callback by [sqlite3changeset_apply()] with either
1057610666
** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
1057710667
** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
@@ -10595,10 +10685,11 @@
1059510685
sqlite3_value **ppValue /* OUT: Value from conflicting row */
1059610686
);
1059710687
1059810688
/*
1059910689
** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
10690
+** METHOD: sqlite3_changeset_iter
1060010691
**
1060110692
** This function may only be called with an iterator passed to an
1060210693
** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
1060310694
** it sets the output variable to the total number of known foreign key
1060410695
** violations in the destination database and returns SQLITE_OK.
@@ -10611,10 +10702,11 @@
1061110702
);
1061210703
1061310704
1061410705
/*
1061510706
** CAPI3REF: Finalize A Changeset Iterator
10707
+** METHOD: sqlite3_changeset_iter
1061610708
**
1061710709
** This function is used to finalize an iterator allocated with
1061810710
** [sqlite3changeset_start()].
1061910711
**
1062010712
** This function should only be called on iterators created using the
@@ -10627,18 +10719,20 @@
1062710719
** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
1062810720
** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
1062910721
** to that error is returned by this function. Otherwise, SQLITE_OK is
1063010722
** returned. This is to allow the following pattern (pseudo-code):
1063110723
**
10724
+** <pre>
1063210725
** sqlite3changeset_start();
1063310726
** while( SQLITE_ROW==sqlite3changeset_next() ){
1063410727
** // Do something with change.
1063510728
** }
1063610729
** rc = sqlite3changeset_finalize();
1063710730
** if( rc!=SQLITE_OK ){
1063810731
** // An error has occurred
1063910732
** }
10733
+** </pre>
1064010734
*/
1064110735
SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
1064210736
1064310737
/*
1064410738
** CAPI3REF: Invert A Changeset
@@ -10682,10 +10776,11 @@
1068210776
**
1068310777
** This function combines the two input changesets using an
1068410778
** sqlite3_changegroup object. Calling it produces similar results as the
1068510779
** following code fragment:
1068610780
**
10781
+** <pre>
1068710782
** sqlite3_changegroup *pGrp;
1068810783
** rc = sqlite3_changegroup_new(&pGrp);
1068910784
** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
1069010785
** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
1069110786
** if( rc==SQLITE_OK ){
@@ -10692,10 +10787,11 @@
1069210787
** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
1069310788
** }else{
1069410789
** *ppOut = 0;
1069510790
** *pnOut = 0;
1069610791
** }
10792
+** </pre>
1069710793
**
1069810794
** Refer to the sqlite3_changegroup documentation below for details.
1069910795
*/
1070010796
SQLITE_API int sqlite3changeset_concat(
1070110797
int nA, /* Number of bytes in buffer pA */
@@ -10707,15 +10803,19 @@
1070710803
);
1070810804
1070910805
1071010806
/*
1071110807
** CAPI3REF: Changegroup Handle
10808
+**
10809
+** A changegroup is an object used to combine two or more
10810
+** [changesets] or [patchsets]
1071210811
*/
1071310812
typedef struct sqlite3_changegroup sqlite3_changegroup;
1071410813
1071510814
/*
1071610815
** CAPI3REF: Create A New Changegroup Object
10816
+** CONSTRUCTOR: sqlite3_changegroup
1071710817
**
1071810818
** An sqlite3_changegroup object is used to combine two or more changesets
1071910819
** (or patchsets) into a single changeset (or patchset). A single changegroup
1072010820
** object may combine changesets or patchsets, but not both. The output is
1072110821
** always in the same format as the input.
@@ -10749,10 +10849,11 @@
1074910849
*/
1075010850
SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
1075110851
1075210852
/*
1075310853
** CAPI3REF: Add A Changeset To A Changegroup
10854
+** METHOD: sqlite3_changegroup
1075410855
**
1075510856
** Add all changes within the changeset (or patchset) in buffer pData (size
1075610857
** nData bytes) to the changegroup.
1075710858
**
1075810859
** If the buffer contains a patchset, then all prior calls to this function
@@ -10826,10 +10927,11 @@
1082610927
*/
1082710928
SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
1082810929
1082910930
/*
1083010931
** CAPI3REF: Obtain A Composite Changeset From A Changegroup
10932
+** METHOD: sqlite3_changegroup
1083110933
**
1083210934
** Obtain a buffer containing a changeset (or patchset) representing the
1083310935
** current contents of the changegroup. If the inputs to the changegroup
1083410936
** were themselves changesets, the output is a changeset. Or, if the
1083510937
** inputs were patchsets, the output is also a patchset.
@@ -10856,10 +10958,11 @@
1085610958
void **ppData /* OUT: Pointer to output buffer */
1085710959
);
1085810960
1085910961
/*
1086010962
** CAPI3REF: Delete A Changegroup Object
10963
+** DESTRUCTOR: sqlite3_changegroup
1086110964
*/
1086210965
SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
1086310966
1086410967
/*
1086510968
** CAPI3REF: Apply A Changeset To A Database
@@ -12671,27 +12774,29 @@
1267112774
#define TK_THEN 138
1267212775
#define TK_ELSE 139
1267312776
#define TK_INDEX 140
1267412777
#define TK_ALTER 141
1267512778
#define TK_ADD 142
12676
-#define TK_ISNOT 143
12677
-#define TK_FUNCTION 144
12678
-#define TK_COLUMN 145
12679
-#define TK_AGG_FUNCTION 146
12680
-#define TK_AGG_COLUMN 147
12681
-#define TK_UMINUS 148
12682
-#define TK_UPLUS 149
12683
-#define TK_REGISTER 150
12684
-#define TK_VECTOR 151
12685
-#define TK_SELECT_COLUMN 152
12686
-#define TK_IF_NULL_ROW 153
12687
-#define TK_ASTERISK 154
12688
-#define TK_SPAN 155
12689
-#define TK_END_OF_FILE 156
12690
-#define TK_UNCLOSED_STRING 157
12691
-#define TK_SPACE 158
12692
-#define TK_ILLEGAL 159
12779
+#define TK_TRUEFALSE 143
12780
+#define TK_ISNOT 144
12781
+#define TK_FUNCTION 145
12782
+#define TK_COLUMN 146
12783
+#define TK_AGG_FUNCTION 147
12784
+#define TK_AGG_COLUMN 148
12785
+#define TK_UMINUS 149
12786
+#define TK_UPLUS 150
12787
+#define TK_TRUTH 151
12788
+#define TK_REGISTER 152
12789
+#define TK_VECTOR 153
12790
+#define TK_SELECT_COLUMN 154
12791
+#define TK_IF_NULL_ROW 155
12792
+#define TK_ASTERISK 156
12793
+#define TK_SPAN 157
12794
+#define TK_END_OF_FILE 158
12795
+#define TK_UNCLOSED_STRING 159
12796
+#define TK_SPACE 160
12797
+#define TK_ILLEGAL 161
1269312798
1269412799
/* The token codes above must all fit in 8 bits */
1269512800
#define TKFLG_MASK 0xff
1269612801
1269712802
/* Flags that can be added to a token code when it is not
@@ -13933,84 +14038,85 @@
1393314038
#define OP_Divide 91 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
1393414039
#define OP_Remainder 92 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
1393514040
#define OP_Concat 93 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
1393614041
#define OP_Compare 94 /* synopsis: r[P1@P3] <-> r[P2@P3] */
1393714042
#define OP_BitNot 95 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
13938
-#define OP_Offset 96 /* synopsis: r[P3] = sqlite_offset(P1) */
14043
+#define OP_IsTrue 96 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
1393914044
#define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */
13940
-#define OP_Column 98 /* synopsis: r[P3]=PX */
13941
-#define OP_Affinity 99 /* synopsis: affinity(r[P1@P2]) */
13942
-#define OP_MakeRecord 100 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
13943
-#define OP_Count 101 /* synopsis: r[P2]=count() */
13944
-#define OP_ReadCookie 102
13945
-#define OP_SetCookie 103
13946
-#define OP_ReopenIdx 104 /* synopsis: root=P2 iDb=P3 */
13947
-#define OP_OpenRead 105 /* synopsis: root=P2 iDb=P3 */
13948
-#define OP_OpenWrite 106 /* synopsis: root=P2 iDb=P3 */
13949
-#define OP_OpenDup 107
13950
-#define OP_OpenAutoindex 108 /* synopsis: nColumn=P2 */
13951
-#define OP_OpenEphemeral 109 /* synopsis: nColumn=P2 */
13952
-#define OP_SorterOpen 110
13953
-#define OP_SequenceTest 111 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
13954
-#define OP_OpenPseudo 112 /* synopsis: P3 columns in r[P2] */
13955
-#define OP_Close 113
13956
-#define OP_ColumnsUsed 114
13957
-#define OP_Sequence 115 /* synopsis: r[P2]=cursor[P1].ctr++ */
13958
-#define OP_NewRowid 116 /* synopsis: r[P2]=rowid */
13959
-#define OP_Insert 117 /* synopsis: intkey=r[P3] data=r[P2] */
13960
-#define OP_InsertInt 118 /* synopsis: intkey=P3 data=r[P2] */
13961
-#define OP_Delete 119
13962
-#define OP_ResetCount 120
13963
-#define OP_SorterCompare 121 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
13964
-#define OP_SorterData 122 /* synopsis: r[P2]=data */
13965
-#define OP_RowData 123 /* synopsis: r[P2]=data */
13966
-#define OP_Rowid 124 /* synopsis: r[P2]=rowid */
13967
-#define OP_NullRow 125
13968
-#define OP_SeekEnd 126
13969
-#define OP_SorterInsert 127 /* synopsis: key=r[P2] */
13970
-#define OP_IdxInsert 128 /* synopsis: key=r[P2] */
13971
-#define OP_IdxDelete 129 /* synopsis: key=r[P2@P3] */
13972
-#define OP_DeferredSeek 130 /* synopsis: Move P3 to P1.rowid if needed */
13973
-#define OP_IdxRowid 131 /* synopsis: r[P2]=rowid */
14045
+#define OP_Offset 98 /* synopsis: r[P3] = sqlite_offset(P1) */
14046
+#define OP_Column 99 /* synopsis: r[P3]=PX */
14047
+#define OP_Affinity 100 /* synopsis: affinity(r[P1@P2]) */
14048
+#define OP_MakeRecord 101 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
14049
+#define OP_Count 102 /* synopsis: r[P2]=count() */
14050
+#define OP_ReadCookie 103
14051
+#define OP_SetCookie 104
14052
+#define OP_ReopenIdx 105 /* synopsis: root=P2 iDb=P3 */
14053
+#define OP_OpenRead 106 /* synopsis: root=P2 iDb=P3 */
14054
+#define OP_OpenWrite 107 /* synopsis: root=P2 iDb=P3 */
14055
+#define OP_OpenDup 108
14056
+#define OP_OpenAutoindex 109 /* synopsis: nColumn=P2 */
14057
+#define OP_OpenEphemeral 110 /* synopsis: nColumn=P2 */
14058
+#define OP_SorterOpen 111
14059
+#define OP_SequenceTest 112 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
14060
+#define OP_OpenPseudo 113 /* synopsis: P3 columns in r[P2] */
14061
+#define OP_Close 114
14062
+#define OP_ColumnsUsed 115
14063
+#define OP_Sequence 116 /* synopsis: r[P2]=cursor[P1].ctr++ */
14064
+#define OP_NewRowid 117 /* synopsis: r[P2]=rowid */
14065
+#define OP_Insert 118 /* synopsis: intkey=r[P3] data=r[P2] */
14066
+#define OP_InsertInt 119 /* synopsis: intkey=P3 data=r[P2] */
14067
+#define OP_Delete 120
14068
+#define OP_ResetCount 121
14069
+#define OP_SorterCompare 122 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
14070
+#define OP_SorterData 123 /* synopsis: r[P2]=data */
14071
+#define OP_RowData 124 /* synopsis: r[P2]=data */
14072
+#define OP_Rowid 125 /* synopsis: r[P2]=rowid */
14073
+#define OP_NullRow 126
14074
+#define OP_SeekEnd 127
14075
+#define OP_SorterInsert 128 /* synopsis: key=r[P2] */
14076
+#define OP_IdxInsert 129 /* synopsis: key=r[P2] */
14077
+#define OP_IdxDelete 130 /* synopsis: key=r[P2@P3] */
14078
+#define OP_DeferredSeek 131 /* synopsis: Move P3 to P1.rowid if needed */
1397414079
#define OP_Real 132 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
13975
-#define OP_Destroy 133
13976
-#define OP_Clear 134
13977
-#define OP_ResetSorter 135
13978
-#define OP_CreateBtree 136 /* synopsis: r[P2]=root iDb=P1 flags=P3 */
13979
-#define OP_SqlExec 137
13980
-#define OP_ParseSchema 138
13981
-#define OP_LoadAnalysis 139
13982
-#define OP_DropTable 140
13983
-#define OP_DropIndex 141
13984
-#define OP_DropTrigger 142
13985
-#define OP_IntegrityCk 143
13986
-#define OP_RowSetAdd 144 /* synopsis: rowset(P1)=r[P2] */
13987
-#define OP_Param 145
13988
-#define OP_FkCounter 146 /* synopsis: fkctr[P1]+=P2 */
13989
-#define OP_MemMax 147 /* synopsis: r[P1]=max(r[P1],r[P2]) */
13990
-#define OP_OffsetLimit 148 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
13991
-#define OP_AggStep0 149 /* synopsis: accum=r[P3] step(r[P2@P5]) */
13992
-#define OP_AggStep 150 /* synopsis: accum=r[P3] step(r[P2@P5]) */
13993
-#define OP_AggFinal 151 /* synopsis: accum=r[P1] N=P2 */
13994
-#define OP_Expire 152
13995
-#define OP_TableLock 153 /* synopsis: iDb=P1 root=P2 write=P3 */
13996
-#define OP_VBegin 154
13997
-#define OP_VCreate 155
13998
-#define OP_VDestroy 156
13999
-#define OP_VOpen 157
14000
-#define OP_VColumn 158 /* synopsis: r[P3]=vcolumn(P2) */
14001
-#define OP_VRename 159
14002
-#define OP_Pagecount 160
14003
-#define OP_MaxPgcnt 161
14004
-#define OP_PureFunc0 162
14005
-#define OP_Function0 163 /* synopsis: r[P3]=func(r[P2@P5]) */
14006
-#define OP_PureFunc 164
14007
-#define OP_Function 165 /* synopsis: r[P3]=func(r[P2@P5]) */
14008
-#define OP_Trace 166
14009
-#define OP_CursorHint 167
14010
-#define OP_Noop 168
14011
-#define OP_Explain 169
14080
+#define OP_IdxRowid 133 /* synopsis: r[P2]=rowid */
14081
+#define OP_Destroy 134
14082
+#define OP_Clear 135
14083
+#define OP_ResetSorter 136
14084
+#define OP_CreateBtree 137 /* synopsis: r[P2]=root iDb=P1 flags=P3 */
14085
+#define OP_SqlExec 138
14086
+#define OP_ParseSchema 139
14087
+#define OP_LoadAnalysis 140
14088
+#define OP_DropTable 141
14089
+#define OP_DropIndex 142
14090
+#define OP_DropTrigger 143
14091
+#define OP_IntegrityCk 144
14092
+#define OP_RowSetAdd 145 /* synopsis: rowset(P1)=r[P2] */
14093
+#define OP_Param 146
14094
+#define OP_FkCounter 147 /* synopsis: fkctr[P1]+=P2 */
14095
+#define OP_MemMax 148 /* synopsis: r[P1]=max(r[P1],r[P2]) */
14096
+#define OP_OffsetLimit 149 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
14097
+#define OP_AggStep0 150 /* synopsis: accum=r[P3] step(r[P2@P5]) */
14098
+#define OP_AggStep 151 /* synopsis: accum=r[P3] step(r[P2@P5]) */
14099
+#define OP_AggFinal 152 /* synopsis: accum=r[P1] N=P2 */
14100
+#define OP_Expire 153
14101
+#define OP_TableLock 154 /* synopsis: iDb=P1 root=P2 write=P3 */
14102
+#define OP_VBegin 155
14103
+#define OP_VCreate 156
14104
+#define OP_VDestroy 157
14105
+#define OP_VOpen 158
14106
+#define OP_VColumn 159 /* synopsis: r[P3]=vcolumn(P2) */
14107
+#define OP_VRename 160
14108
+#define OP_Pagecount 161
14109
+#define OP_MaxPgcnt 162
14110
+#define OP_PureFunc0 163
14111
+#define OP_Function0 164 /* synopsis: r[P3]=func(r[P2@P5]) */
14112
+#define OP_PureFunc 165
14113
+#define OP_Function 166 /* synopsis: r[P3]=func(r[P2@P5]) */
14114
+#define OP_Trace 167
14115
+#define OP_CursorHint 168
14116
+#define OP_Noop 169
14117
+#define OP_Explain 170
1401214118
1401314119
/* Properties such as "out2" or "jump" that are specified in
1401414120
** comments following the "case" for each opcode in the vdbe.c
1401514121
** are encoded into bitvectors as follows:
1401614122
*/
@@ -14031,20 +14137,20 @@
1403114137
/* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x01, 0x01, 0x01, 0x02,\
1403214138
/* 64 */ 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00,\
1403314139
/* 72 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
1403414140
/* 80 */ 0x02, 0x02, 0x02, 0x00, 0x26, 0x26, 0x26, 0x26,\
1403514141
/* 88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
14036
-/* 96 */ 0x20, 0x10, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
14142
+/* 96 */ 0x12, 0x10, 0x20, 0x00, 0x00, 0x00, 0x10, 0x10,\
1403714143
/* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14038
-/* 112 */ 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00,\
14039
-/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x04,\
14040
-/* 128 */ 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00,\
14041
-/* 136 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14042
-/* 144 */ 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00,\
14144
+/* 112 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
14145
+/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,\
14146
+/* 128 */ 0x04, 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00,\
14147
+/* 136 */ 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14148
+/* 144 */ 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00,\
1404314149
/* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14044
-/* 160 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14045
-/* 168 */ 0x00, 0x00,}
14150
+/* 160 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
14151
+/* 168 */ 0x00, 0x00, 0x00,}
1404614152
1404714153
/* The sqlite3P2Values() routine is able to run faster if it knows
1404814154
** the value of the largest JUMP opcode. The smaller the maximum
1404914155
** JUMP opcode the better, so the mkopcodeh.tcl script that
1405014156
** generated this include file strives to group all JUMP opcodes
@@ -15246,12 +15352,13 @@
1524615352
int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
1524715353
struct sqlite3InitInfo { /* Information used during initialization */
1524815354
int newTnum; /* Rootpage of table being initialized */
1524915355
u8 iDb; /* Which db file is being initialized */
1525015356
u8 busy; /* TRUE if currently initializing */
15251
- u8 orphanTrigger; /* Last statement is orphaned TEMP trigger */
15252
- u8 imposterTable; /* Building an imposter table */
15357
+ unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
15358
+ unsigned imposterTable : 1; /* Building an imposter table */
15359
+ unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
1525315360
} init;
1525415361
int nVdbeActive; /* Number of VDBEs currently running */
1525515362
int nVdbeRead; /* Number of active VDBEs that read or write */
1525615363
int nVdbeWrite; /* Number of active VDBEs that read and write */
1525715364
int nVdbeExec; /* Number of nested calls to VdbeExec() */
@@ -15635,10 +15742,11 @@
1563515742
/* Allowed values for Column.colFlags:
1563615743
*/
1563715744
#define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
1563815745
#define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */
1563915746
#define COLFLAG_HASTYPE 0x0004 /* Type name follows column name */
15747
+#define COLFLAG_UNIQUE 0x0008 /* Column def contains "UNIQUE" or "PK" */
1564015748
1564115749
/*
1564215750
** A "Collating Sequence" is defined by an instance of the following
1564315751
** structure. Conceptually, a collating sequence consists of a name and
1564415752
** a comparison routine that defines the order of that sequence.
@@ -17719,10 +17827,12 @@
1771917827
SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
1772017828
SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
1772117829
SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
1772217830
SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
1772317831
SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
17832
+SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
17833
+SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
1772417834
SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
1772517835
SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
1772617836
SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
1772717837
SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
1772817838
SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
@@ -17900,10 +18010,14 @@
1790018010
SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
1790118011
1790218012
#if defined(SQLITE_NEED_ERR_NAME)
1790318013
SQLITE_PRIVATE const char *sqlite3ErrName(int);
1790418014
#endif
18015
+
18016
+#ifdef SQLITE_ENABLE_DESERIALIZE
18017
+SQLITE_PRIVATE int sqlite3MemdbInit(void);
18018
+#endif
1790518019
1790618020
SQLITE_PRIVATE const char *sqlite3ErrStr(int);
1790718021
SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
1790818022
SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
1790918023
SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
@@ -17949,10 +18063,13 @@
1794918063
SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
1795018064
#ifndef SQLITE_OMIT_WSD
1795118065
SQLITE_PRIVATE int sqlite3PendingByte;
1795218066
#endif
1795318067
#endif
18068
+#ifdef VDBE_PROFILE
18069
+SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt;
18070
+#endif
1795418071
SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
1795518072
SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
1795618073
SQLITE_PRIVATE void sqlite3AlterFunctions(void);
1795718074
SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
1795818075
SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
@@ -18572,10 +18689,17 @@
1857218689
SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
1857318690
{ "0", 1 },
1857418691
{ "1", 1 }
1857518692
};
1857618693
18694
+#ifdef VDBE_PROFILE
18695
+/*
18696
+** The following performance counter can be used in place of
18697
+** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
18698
+*/
18699
+SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt = 0;
18700
+#endif
1857718701
1857818702
/*
1857918703
** The value of the "pending" byte must be 0x40000000 (1 byte past the
1858018704
** 1-gibabyte boundary) in a compatible database. SQLite never uses
1858118705
** the database page that contains the pending byte. It never attempts
@@ -18855,12 +18979,10 @@
1885518979
** representations of the value stored in the Mem struct.
1885618980
**
1885718981
** If the MEM_Null flag is set, then the value is an SQL NULL value.
1885818982
** For a pointer type created using sqlite3_bind_pointer() or
1885918983
** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
18860
-** If both MEM_Null and MEM_Zero are set, that means that the value is
18861
-** an unchanging column value from VColumn.
1886218984
**
1886318985
** If the MEM_Str flag is set then Mem.z points at a string representation.
1886418986
** Usually this is encoded in the same unicode encoding as the main
1886518987
** database (see below for exceptions). If the MEM_Term flag is also
1886618988
** set, then the string is nul terminated. The MEM_Int and MEM_Real
@@ -18950,11 +19072,10 @@
1895019072
Mem *pMem; /* Memory cell used to store aggregate context */
1895119073
Vdbe *pVdbe; /* The VM that owns this context */
1895219074
int iOp; /* Instruction number of OP_Function */
1895319075
int isError; /* Error code returned by the function. */
1895419076
u8 skipFlag; /* Skip accumulator loading if true */
18955
- u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */
1895619077
u8 argc; /* Number of arguments */
1895719078
sqlite3_value *argv[1]; /* Argument set */
1895819079
};
1895919080
1896019081
/* A bitfield type for use inside of structures. Always follow with :N where
@@ -19120,10 +19241,11 @@
1912019241
SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
1912119242
SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
1912219243
SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
1912319244
SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
1912419245
SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
19246
+SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
1912519247
SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
1912619248
SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
1912719249
SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
1912819250
SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
1912919251
SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
@@ -19513,10 +19635,13 @@
1951319635
/*
1951419636
** Set *pCurrent to the total cache hits or misses encountered by all
1951519637
** pagers the database handle is connected to. *pHighwater is always set
1951619638
** to zero.
1951719639
*/
19640
+ case SQLITE_DBSTATUS_CACHE_SPILL:
19641
+ op = SQLITE_DBSTATUS_CACHE_WRITE+1;
19642
+ /* Fall through into the next case */
1951819643
case SQLITE_DBSTATUS_CACHE_HIT:
1951919644
case SQLITE_DBSTATUS_CACHE_MISS:
1952019645
case SQLITE_DBSTATUS_CACHE_WRITE:{
1952119646
int i;
1952219647
int nRet = 0;
@@ -24106,15 +24231,16 @@
2410624231
volatile pthread_t owner; /* Thread that is within this mutex */
2410724232
int trace; /* True to trace changes */
2410824233
#endif
2410924234
};
2411024235
#if SQLITE_MUTEX_NREF
24111
-#define SQLITE3_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER,0,0,(pthread_t)0,0}
24236
+# define SQLITE3_MUTEX_INITIALIZER(id) \
24237
+ {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
2411224238
#elif defined(SQLITE_ENABLE_API_ARMOR)
24113
-#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0 }
24239
+# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
2411424240
#else
24115
-#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
24241
+#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
2411624242
#endif
2411724243
2411824244
/*
2411924245
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
2412024246
** intended for use only inside assert() statements. On some platforms,
@@ -24207,22 +24333,22 @@
2420724333
** mutex types, the same mutex is returned on every call that has
2420824334
** the same type number.
2420924335
*/
2421024336
static sqlite3_mutex *pthreadMutexAlloc(int iType){
2421124337
static sqlite3_mutex staticMutexes[] = {
24212
- SQLITE3_MUTEX_INITIALIZER,
24213
- SQLITE3_MUTEX_INITIALIZER,
24214
- SQLITE3_MUTEX_INITIALIZER,
24215
- SQLITE3_MUTEX_INITIALIZER,
24216
- SQLITE3_MUTEX_INITIALIZER,
24217
- SQLITE3_MUTEX_INITIALIZER,
24218
- SQLITE3_MUTEX_INITIALIZER,
24219
- SQLITE3_MUTEX_INITIALIZER,
24220
- SQLITE3_MUTEX_INITIALIZER,
24221
- SQLITE3_MUTEX_INITIALIZER,
24222
- SQLITE3_MUTEX_INITIALIZER,
24223
- SQLITE3_MUTEX_INITIALIZER
24338
+ SQLITE3_MUTEX_INITIALIZER(2),
24339
+ SQLITE3_MUTEX_INITIALIZER(3),
24340
+ SQLITE3_MUTEX_INITIALIZER(4),
24341
+ SQLITE3_MUTEX_INITIALIZER(5),
24342
+ SQLITE3_MUTEX_INITIALIZER(6),
24343
+ SQLITE3_MUTEX_INITIALIZER(7),
24344
+ SQLITE3_MUTEX_INITIALIZER(8),
24345
+ SQLITE3_MUTEX_INITIALIZER(9),
24346
+ SQLITE3_MUTEX_INITIALIZER(10),
24347
+ SQLITE3_MUTEX_INITIALIZER(11),
24348
+ SQLITE3_MUTEX_INITIALIZER(12),
24349
+ SQLITE3_MUTEX_INITIALIZER(13)
2422424350
};
2422524351
sqlite3_mutex *p;
2422624352
switch( iType ){
2422724353
case SQLITE_MUTEX_RECURSIVE: {
2422824354
p = sqlite3MallocZero( sizeof(*p) );
@@ -24237,17 +24363,23 @@
2423724363
pthread_mutexattr_init(&recursiveAttr);
2423824364
pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
2423924365
pthread_mutex_init(&p->mutex, &recursiveAttr);
2424024366
pthread_mutexattr_destroy(&recursiveAttr);
2424124367
#endif
24368
+#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
24369
+ p->id = SQLITE_MUTEX_RECURSIVE;
24370
+#endif
2424224371
}
2424324372
break;
2424424373
}
2424524374
case SQLITE_MUTEX_FAST: {
2424624375
p = sqlite3MallocZero( sizeof(*p) );
2424724376
if( p ){
2424824377
pthread_mutex_init(&p->mutex, 0);
24378
+#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
24379
+ p->id = SQLITE_MUTEX_FAST;
24380
+#endif
2424924381
}
2425024382
break;
2425124383
}
2425224384
default: {
2425324385
#ifdef SQLITE_ENABLE_API_ARMOR
@@ -24259,11 +24391,11 @@
2425924391
p = &staticMutexes[iType-2];
2426024392
break;
2426124393
}
2426224394
}
2426324395
#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
24264
- if( p ) p->id = iType;
24396
+ assert( p==0 || p->id==iType );
2426524397
#endif
2426624398
return p;
2426724399
}
2426824400
2426924401
@@ -24776,11 +24908,11 @@
2477624908
CRITICAL_SECTION mutex; /* Mutex controlling the lock */
2477724909
int id; /* Mutex type */
2477824910
#ifdef SQLITE_DEBUG
2477924911
volatile int nRef; /* Number of enterances */
2478024912
volatile DWORD owner; /* Thread holding this mutex */
24781
- volatile int trace; /* True to trace changes */
24913
+ volatile LONG trace; /* True to trace changes */
2478224914
#endif
2478324915
};
2478424916
2478524917
/*
2478624918
** These are the initializer values used when declaring a "static" mutex
@@ -24788,14 +24920,14 @@
2478824920
** on the Win32 platform.
2478924921
*/
2479024922
#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
2479124923
2479224924
#ifdef SQLITE_DEBUG
24793
-#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
24925
+#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \
2479424926
0L, (DWORD)0, 0 }
2479524927
#else
24796
-#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
24928
+#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id }
2479724929
#endif
2479824930
2479924931
#ifdef SQLITE_DEBUG
2480024932
/*
2480124933
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
@@ -24834,22 +24966,22 @@
2483424966
2483524967
/*
2483624968
** Initialize and deinitialize the mutex subsystem.
2483724969
*/
2483824970
static sqlite3_mutex winMutex_staticMutexes[] = {
24839
- SQLITE3_MUTEX_INITIALIZER,
24840
- SQLITE3_MUTEX_INITIALIZER,
24841
- SQLITE3_MUTEX_INITIALIZER,
24842
- SQLITE3_MUTEX_INITIALIZER,
24843
- SQLITE3_MUTEX_INITIALIZER,
24844
- SQLITE3_MUTEX_INITIALIZER,
24845
- SQLITE3_MUTEX_INITIALIZER,
24846
- SQLITE3_MUTEX_INITIALIZER,
24847
- SQLITE3_MUTEX_INITIALIZER,
24848
- SQLITE3_MUTEX_INITIALIZER,
24849
- SQLITE3_MUTEX_INITIALIZER,
24850
- SQLITE3_MUTEX_INITIALIZER
24971
+ SQLITE3_MUTEX_INITIALIZER(2),
24972
+ SQLITE3_MUTEX_INITIALIZER(3),
24973
+ SQLITE3_MUTEX_INITIALIZER(4),
24974
+ SQLITE3_MUTEX_INITIALIZER(5),
24975
+ SQLITE3_MUTEX_INITIALIZER(6),
24976
+ SQLITE3_MUTEX_INITIALIZER(7),
24977
+ SQLITE3_MUTEX_INITIALIZER(8),
24978
+ SQLITE3_MUTEX_INITIALIZER(9),
24979
+ SQLITE3_MUTEX_INITIALIZER(10),
24980
+ SQLITE3_MUTEX_INITIALIZER(11),
24981
+ SQLITE3_MUTEX_INITIALIZER(12),
24982
+ SQLITE3_MUTEX_INITIALIZER(13)
2485124983
};
2485224984
2485324985
static int winMutex_isInit = 0;
2485424986
static int winMutex_isNt = -1; /* <0 means "need to query" */
2485524987
@@ -24975,19 +25107,19 @@
2497525107
(void)SQLITE_MISUSE_BKPT;
2497625108
return 0;
2497725109
}
2497825110
#endif
2497925111
p = &winMutex_staticMutexes[iType-2];
24980
- p->id = iType;
2498125112
#ifdef SQLITE_DEBUG
2498225113
#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
24983
- p->trace = 1;
25114
+ InterlockedCompareExchange(&p->trace, 1, 0);
2498425115
#endif
2498525116
#endif
2498625117
break;
2498725118
}
2498825119
}
25120
+ assert( p==0 || p->id==iType );
2498925121
return p;
2499025122
}
2499125123
2499225124
2499325125
/*
@@ -26062,10 +26194,15 @@
2606226194
etByte flag_rtz; /* True if trailing zeros should be removed */
2606326195
#endif
2606426196
PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
2606526197
char buf[etBUFSIZE]; /* Conversion buffer */
2606626198
26199
+ /* pAccum never starts out with an empty buffer that was obtained from
26200
+ ** malloc(). This precondition is required by the mprintf("%z...")
26201
+ ** optimization. */
26202
+ assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
26203
+
2606726204
bufpt = 0;
2606826205
if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
2606926206
pArgList = va_arg(ap, PrintfArguments*);
2607026207
bArgList = 1;
2607126208
}else{
@@ -26480,26 +26617,56 @@
2648026617
length = 1;
2648126618
break;
2648226619
case etCHARX:
2648326620
if( bArgList ){
2648426621
bufpt = getTextArg(pArgList);
26485
- c = bufpt ? bufpt[0] : 0;
26622
+ length = 1;
26623
+ if( bufpt ){
26624
+ buf[0] = c = *(bufpt++);
26625
+ if( (c&0xc0)==0xc0 ){
26626
+ while( length<4 && (bufpt[0]&0xc0)==0x80 ){
26627
+ buf[length++] = *(bufpt++);
26628
+ }
26629
+ }
26630
+ }else{
26631
+ buf[0] = 0;
26632
+ }
2648626633
}else{
26487
- c = va_arg(ap,int);
26634
+ unsigned int ch = va_arg(ap,unsigned int);
26635
+ if( ch<0x00080 ){
26636
+ buf[0] = ch & 0xff;
26637
+ length = 1;
26638
+ }else if( ch<0x00800 ){
26639
+ buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
26640
+ buf[1] = 0x80 + (u8)(ch & 0x3f);
26641
+ length = 2;
26642
+ }else if( ch<0x10000 ){
26643
+ buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
26644
+ buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
26645
+ buf[2] = 0x80 + (u8)(ch & 0x3f);
26646
+ length = 3;
26647
+ }else{
26648
+ buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
26649
+ buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
26650
+ buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
26651
+ buf[3] = 0x80 + (u8)(ch & 0x3f);
26652
+ length = 4;
26653
+ }
2648826654
}
2648926655
if( precision>1 ){
2649026656
width -= precision-1;
2649126657
if( width>1 && !flag_leftjustify ){
2649226658
sqlite3AppendChar(pAccum, width-1, ' ');
2649326659
width = 0;
2649426660
}
26495
- sqlite3AppendChar(pAccum, precision-1, c);
26661
+ while( precision-- > 1 ){
26662
+ sqlite3StrAccumAppend(pAccum, buf, length);
26663
+ }
2649626664
}
26497
- length = 1;
26498
- buf[0] = c;
2649926665
bufpt = buf;
26500
- break;
26666
+ flag_altform2 = 1;
26667
+ goto adjust_width_for_utf8;
2650126668
case etSTRING:
2650226669
case etDYNSTRING:
2650326670
if( bArgList ){
2650426671
bufpt = getTextArg(pArgList);
2650526672
xtype = etSTRING;
@@ -26507,21 +26674,49 @@
2650726674
bufpt = va_arg(ap,char*);
2650826675
}
2650926676
if( bufpt==0 ){
2651026677
bufpt = "";
2651126678
}else if( xtype==etDYNSTRING ){
26679
+ if( pAccum->nChar==0 && pAccum->mxAlloc && width==0 && precision<0 ){
26680
+ /* Special optimization for sqlite3_mprintf("%z..."):
26681
+ ** Extend an existing memory allocation rather than creating
26682
+ ** a new one. */
26683
+ assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
26684
+ pAccum->zText = bufpt;
26685
+ pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
26686
+ pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
26687
+ pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
26688
+ length = 0;
26689
+ break;
26690
+ }
2651226691
zExtra = bufpt;
2651326692
}
2651426693
if( precision>=0 ){
26515
- for(length=0; length<precision && bufpt[length]; length++){}
26694
+ if( flag_altform2 ){
26695
+ /* Set length to the number of bytes needed in order to display
26696
+ ** precision characters */
26697
+ unsigned char *z = (unsigned char*)bufpt;
26698
+ while( precision-- > 0 && z[0] ){
26699
+ SQLITE_SKIP_UTF8(z);
26700
+ }
26701
+ length = (int)(z - (unsigned char*)bufpt);
26702
+ }else{
26703
+ for(length=0; length<precision && bufpt[length]; length++){}
26704
+ }
2651626705
}else{
2651726706
length = 0x7fffffff & (int)strlen(bufpt);
2651826707
}
26708
+ adjust_width_for_utf8:
26709
+ if( flag_altform2 && width>0 ){
26710
+ /* Adjust width to account for extra bytes in UTF-8 characters */
26711
+ int ii = length - 1;
26712
+ while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
26713
+ }
2651926714
break;
26520
- case etSQLESCAPE: /* Escape ' characters */
26521
- case etSQLESCAPE2: /* Escape ' and enclose in '...' */
26522
- case etSQLESCAPE3: { /* Escape " characters */
26715
+ case etSQLESCAPE: /* %q: Escape ' characters */
26716
+ case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */
26717
+ case etSQLESCAPE3: { /* %w: Escape " characters */
2652326718
int i, j, k, n, isnull;
2652426719
int needQuote;
2652526720
char ch;
2652626721
char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
2652726722
char *escarg;
@@ -26531,13 +26726,21 @@
2653126726
}else{
2653226727
escarg = va_arg(ap,char*);
2653326728
}
2653426729
isnull = escarg==0;
2653526730
if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
26731
+ /* For %q, %Q, and %w, the precision is the number of byte (or
26732
+ ** characters if the ! flags is present) to use from the input.
26733
+ ** Because of the extra quoting characters inserted, the number
26734
+ ** of output characters may be larger than the precision.
26735
+ */
2653626736
k = precision;
2653726737
for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
2653826738
if( ch==q ) n++;
26739
+ if( flag_altform2 && (ch&0xc0)==0xc0 ){
26740
+ while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
26741
+ }
2653926742
}
2654026743
needQuote = !isnull && xtype==etSQLESCAPE2;
2654126744
n += i + 3;
2654226745
if( n>etBUFSIZE ){
2654326746
bufpt = zExtra = sqlite3Malloc( n );
@@ -26556,14 +26759,11 @@
2655626759
if( ch==q ) bufpt[j++] = ch;
2655726760
}
2655826761
if( needQuote ) bufpt[j++] = q;
2655926762
bufpt[j] = 0;
2656026763
length = j;
26561
- /* The precision in %q and %Q means how many input characters to
26562
- ** consume, not the length of the output...
26563
- ** if( precision>=0 && precision<length ) length = precision; */
26564
- break;
26764
+ goto adjust_width_for_utf8;
2656526765
}
2656626766
case etTOKEN: {
2656726767
Token *pToken;
2656826768
if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
2656926769
pToken = va_arg(ap, Token*);
@@ -26598,11 +26798,14 @@
2659826798
}
2659926799
}/* End switch over the format type */
2660026800
/*
2660126801
** The text of the conversion is pointed to by "bufpt" and is
2660226802
** "length" characters long. The field width is "width". Do
26603
- ** the output.
26803
+ ** the output. Both length and width are in bytes, not characters,
26804
+ ** at this point. If the "!" flag was present on string conversions
26805
+ ** indicating that width and precision should be expressed in characters,
26806
+ ** then the values have been translated prior to reaching this point.
2660426807
*/
2660526808
width -= length;
2660626809
if( width>0 ){
2660726810
if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
2660826811
sqlite3StrAccumAppend(pAccum, bufpt, length);
@@ -27267,10 +27470,15 @@
2726727470
break;
2726827471
}
2726927472
case TK_NULL: {
2727027473
sqlite3TreeViewLine(pView,"NULL");
2727127474
break;
27475
+ }
27476
+ case TK_TRUEFALSE: {
27477
+ sqlite3TreeViewLine(pView,
27478
+ sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
27479
+ break;
2727227480
}
2727327481
#ifndef SQLITE_OMIT_BLOB_LITERAL
2727427482
case TK_BLOB: {
2727527483
sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
2727627484
break;
@@ -27323,10 +27531,23 @@
2732327531
case TK_UPLUS: zUniOp = "UPLUS"; break;
2732427532
case TK_BITNOT: zUniOp = "BITNOT"; break;
2732527533
case TK_NOT: zUniOp = "NOT"; break;
2732627534
case TK_ISNULL: zUniOp = "ISNULL"; break;
2732727535
case TK_NOTNULL: zUniOp = "NOTNULL"; break;
27536
+
27537
+ case TK_TRUTH: {
27538
+ int x;
27539
+ const char *azOp[] = {
27540
+ "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
27541
+ };
27542
+ assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
27543
+ assert( pExpr->pRight );
27544
+ assert( pExpr->pRight->op==TK_TRUEFALSE );
27545
+ x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
27546
+ zUniOp = azOp[x];
27547
+ break;
27548
+ }
2732827549
2732927550
case TK_SPAN: {
2733027551
sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
2733127552
sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
2733227553
break;
@@ -29061,11 +29282,11 @@
2906129282
** routine does *not* accept hexadecimal notation.
2906229283
**
2906329284
** Returns:
2906429285
**
2906529286
** 0 Successful transformation. Fits in a 64-bit signed integer.
29066
-** 1 Excess text after the integer value
29287
+** 1 Excess non-space text after the integer value
2906729288
** 2 Integer too large for a 64-bit signed integer or is malformed
2906829289
** 3 Special case of 9223372036854775808
2906929290
**
2907029291
** length is the number of bytes in the string (bytes, not characters).
2907129292
** The string is not necessarily zero-terminated. The encoding is
@@ -29104,51 +29325,61 @@
2910429325
zStart = zNum;
2910529326
while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
2910629327
for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
2910729328
u = u*10 + c - '0';
2910829329
}
29330
+ testcase( i==18*incr );
29331
+ testcase( i==19*incr );
29332
+ testcase( i==20*incr );
2910929333
if( u>LARGEST_INT64 ){
29334
+ /* This test and assignment is needed only to suppress UB warnings
29335
+ ** from clang and -fsanitize=undefined. This test and assignment make
29336
+ ** the code a little larger and slower, and no harm comes from omitting
29337
+ ** them, but we must appaise the undefined-behavior pharisees. */
2911029338
*pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
2911129339
}else if( neg ){
2911229340
*pNum = -(i64)u;
2911329341
}else{
2911429342
*pNum = (i64)u;
2911529343
}
29116
- testcase( i==18 );
29117
- testcase( i==19 );
29118
- testcase( i==20 );
29119
- if( &zNum[i]<zEnd /* Extra bytes at the end */
29120
- || (i==0 && zStart==zNum) /* No digits */
29344
+ rc = 0;
29345
+ if( (i==0 && zStart==zNum) /* No digits */
2912129346
|| nonNum /* UTF16 with high-order bytes non-zero */
2912229347
){
2912329348
rc = 1;
29124
- }else{
29125
- rc = 0;
29126
- }
29127
- if( i>19*incr ){ /* Too many digits */
29128
- /* zNum is empty or contains non-numeric text or is longer
29129
- ** than 19 digits (thus guaranteeing that it is too large) */
29130
- return 2;
29131
- }else if( i<19*incr ){
29349
+ }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */
29350
+ int jj = i;
29351
+ do{
29352
+ if( !sqlite3Isspace(zNum[jj]) ){
29353
+ rc = 1; /* Extra non-space text after the integer */
29354
+ break;
29355
+ }
29356
+ jj += incr;
29357
+ }while( &zNum[jj]<zEnd );
29358
+ }
29359
+ if( i<19*incr ){
2913229360
/* Less than 19 digits, so we know that it fits in 64 bits */
2913329361
assert( u<=LARGEST_INT64 );
2913429362
return rc;
2913529363
}else{
2913629364
/* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
29137
- c = compare2pow63(zNum, incr);
29365
+ c = i>19*incr ? 1 : compare2pow63(zNum, incr);
2913829366
if( c<0 ){
2913929367
/* zNum is less than 9223372036854775808 so it fits */
2914029368
assert( u<=LARGEST_INT64 );
2914129369
return rc;
29142
- }else if( c>0 ){
29143
- /* zNum is greater than 9223372036854775808 so it overflows */
29144
- return 2;
2914529370
}else{
29146
- /* zNum is exactly 9223372036854775808. Fits if negative. The
29147
- ** special case 2 overflow if positive */
29148
- assert( u-1==LARGEST_INT64 );
29149
- return neg ? rc : 3;
29371
+ *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
29372
+ if( c>0 ){
29373
+ /* zNum is greater than 9223372036854775808 so it overflows */
29374
+ return 2;
29375
+ }else{
29376
+ /* zNum is exactly 9223372036854775808. Fits if negative. The
29377
+ ** special case 2 overflow if positive */
29378
+ assert( u-1==LARGEST_INT64 );
29379
+ return neg ? rc : 3;
29380
+ }
2915029381
}
2915129382
}
2915229383
}
2915329384
2915429385
/*
@@ -30461,84 +30692,85 @@
3046130692
/* 91 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
3046230693
/* 92 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
3046330694
/* 93 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
3046430695
/* 94 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
3046530696
/* 95 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
30466
- /* 96 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"),
30697
+ /* 96 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
3046730698
/* 97 */ "String8" OpHelp("r[P2]='P4'"),
30468
- /* 98 */ "Column" OpHelp("r[P3]=PX"),
30469
- /* 99 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
30470
- /* 100 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
30471
- /* 101 */ "Count" OpHelp("r[P2]=count()"),
30472
- /* 102 */ "ReadCookie" OpHelp(""),
30473
- /* 103 */ "SetCookie" OpHelp(""),
30474
- /* 104 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
30475
- /* 105 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
30476
- /* 106 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
30477
- /* 107 */ "OpenDup" OpHelp(""),
30478
- /* 108 */ "OpenAutoindex" OpHelp("nColumn=P2"),
30479
- /* 109 */ "OpenEphemeral" OpHelp("nColumn=P2"),
30480
- /* 110 */ "SorterOpen" OpHelp(""),
30481
- /* 111 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
30482
- /* 112 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
30483
- /* 113 */ "Close" OpHelp(""),
30484
- /* 114 */ "ColumnsUsed" OpHelp(""),
30485
- /* 115 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
30486
- /* 116 */ "NewRowid" OpHelp("r[P2]=rowid"),
30487
- /* 117 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
30488
- /* 118 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
30489
- /* 119 */ "Delete" OpHelp(""),
30490
- /* 120 */ "ResetCount" OpHelp(""),
30491
- /* 121 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
30492
- /* 122 */ "SorterData" OpHelp("r[P2]=data"),
30493
- /* 123 */ "RowData" OpHelp("r[P2]=data"),
30494
- /* 124 */ "Rowid" OpHelp("r[P2]=rowid"),
30495
- /* 125 */ "NullRow" OpHelp(""),
30496
- /* 126 */ "SeekEnd" OpHelp(""),
30497
- /* 127 */ "SorterInsert" OpHelp("key=r[P2]"),
30498
- /* 128 */ "IdxInsert" OpHelp("key=r[P2]"),
30499
- /* 129 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
30500
- /* 130 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"),
30501
- /* 131 */ "IdxRowid" OpHelp("r[P2]=rowid"),
30699
+ /* 98 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"),
30700
+ /* 99 */ "Column" OpHelp("r[P3]=PX"),
30701
+ /* 100 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
30702
+ /* 101 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
30703
+ /* 102 */ "Count" OpHelp("r[P2]=count()"),
30704
+ /* 103 */ "ReadCookie" OpHelp(""),
30705
+ /* 104 */ "SetCookie" OpHelp(""),
30706
+ /* 105 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
30707
+ /* 106 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
30708
+ /* 107 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
30709
+ /* 108 */ "OpenDup" OpHelp(""),
30710
+ /* 109 */ "OpenAutoindex" OpHelp("nColumn=P2"),
30711
+ /* 110 */ "OpenEphemeral" OpHelp("nColumn=P2"),
30712
+ /* 111 */ "SorterOpen" OpHelp(""),
30713
+ /* 112 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
30714
+ /* 113 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
30715
+ /* 114 */ "Close" OpHelp(""),
30716
+ /* 115 */ "ColumnsUsed" OpHelp(""),
30717
+ /* 116 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
30718
+ /* 117 */ "NewRowid" OpHelp("r[P2]=rowid"),
30719
+ /* 118 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
30720
+ /* 119 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
30721
+ /* 120 */ "Delete" OpHelp(""),
30722
+ /* 121 */ "ResetCount" OpHelp(""),
30723
+ /* 122 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
30724
+ /* 123 */ "SorterData" OpHelp("r[P2]=data"),
30725
+ /* 124 */ "RowData" OpHelp("r[P2]=data"),
30726
+ /* 125 */ "Rowid" OpHelp("r[P2]=rowid"),
30727
+ /* 126 */ "NullRow" OpHelp(""),
30728
+ /* 127 */ "SeekEnd" OpHelp(""),
30729
+ /* 128 */ "SorterInsert" OpHelp("key=r[P2]"),
30730
+ /* 129 */ "IdxInsert" OpHelp("key=r[P2]"),
30731
+ /* 130 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
30732
+ /* 131 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"),
3050230733
/* 132 */ "Real" OpHelp("r[P2]=P4"),
30503
- /* 133 */ "Destroy" OpHelp(""),
30504
- /* 134 */ "Clear" OpHelp(""),
30505
- /* 135 */ "ResetSorter" OpHelp(""),
30506
- /* 136 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"),
30507
- /* 137 */ "SqlExec" OpHelp(""),
30508
- /* 138 */ "ParseSchema" OpHelp(""),
30509
- /* 139 */ "LoadAnalysis" OpHelp(""),
30510
- /* 140 */ "DropTable" OpHelp(""),
30511
- /* 141 */ "DropIndex" OpHelp(""),
30512
- /* 142 */ "DropTrigger" OpHelp(""),
30513
- /* 143 */ "IntegrityCk" OpHelp(""),
30514
- /* 144 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
30515
- /* 145 */ "Param" OpHelp(""),
30516
- /* 146 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
30517
- /* 147 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
30518
- /* 148 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
30519
- /* 149 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"),
30520
- /* 150 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
30521
- /* 151 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
30522
- /* 152 */ "Expire" OpHelp(""),
30523
- /* 153 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
30524
- /* 154 */ "VBegin" OpHelp(""),
30525
- /* 155 */ "VCreate" OpHelp(""),
30526
- /* 156 */ "VDestroy" OpHelp(""),
30527
- /* 157 */ "VOpen" OpHelp(""),
30528
- /* 158 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
30529
- /* 159 */ "VRename" OpHelp(""),
30530
- /* 160 */ "Pagecount" OpHelp(""),
30531
- /* 161 */ "MaxPgcnt" OpHelp(""),
30532
- /* 162 */ "PureFunc0" OpHelp(""),
30533
- /* 163 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
30534
- /* 164 */ "PureFunc" OpHelp(""),
30535
- /* 165 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
30536
- /* 166 */ "Trace" OpHelp(""),
30537
- /* 167 */ "CursorHint" OpHelp(""),
30538
- /* 168 */ "Noop" OpHelp(""),
30539
- /* 169 */ "Explain" OpHelp(""),
30734
+ /* 133 */ "IdxRowid" OpHelp("r[P2]=rowid"),
30735
+ /* 134 */ "Destroy" OpHelp(""),
30736
+ /* 135 */ "Clear" OpHelp(""),
30737
+ /* 136 */ "ResetSorter" OpHelp(""),
30738
+ /* 137 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"),
30739
+ /* 138 */ "SqlExec" OpHelp(""),
30740
+ /* 139 */ "ParseSchema" OpHelp(""),
30741
+ /* 140 */ "LoadAnalysis" OpHelp(""),
30742
+ /* 141 */ "DropTable" OpHelp(""),
30743
+ /* 142 */ "DropIndex" OpHelp(""),
30744
+ /* 143 */ "DropTrigger" OpHelp(""),
30745
+ /* 144 */ "IntegrityCk" OpHelp(""),
30746
+ /* 145 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
30747
+ /* 146 */ "Param" OpHelp(""),
30748
+ /* 147 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
30749
+ /* 148 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
30750
+ /* 149 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
30751
+ /* 150 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"),
30752
+ /* 151 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
30753
+ /* 152 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
30754
+ /* 153 */ "Expire" OpHelp(""),
30755
+ /* 154 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
30756
+ /* 155 */ "VBegin" OpHelp(""),
30757
+ /* 156 */ "VCreate" OpHelp(""),
30758
+ /* 157 */ "VDestroy" OpHelp(""),
30759
+ /* 158 */ "VOpen" OpHelp(""),
30760
+ /* 159 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
30761
+ /* 160 */ "VRename" OpHelp(""),
30762
+ /* 161 */ "Pagecount" OpHelp(""),
30763
+ /* 162 */ "MaxPgcnt" OpHelp(""),
30764
+ /* 163 */ "PureFunc0" OpHelp(""),
30765
+ /* 164 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
30766
+ /* 165 */ "PureFunc" OpHelp(""),
30767
+ /* 166 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
30768
+ /* 167 */ "Trace" OpHelp(""),
30769
+ /* 168 */ "CursorHint" OpHelp(""),
30770
+ /* 169 */ "Noop" OpHelp(""),
30771
+ /* 170 */ "Explain" OpHelp(""),
3054030772
};
3054130773
return azName[i];
3054230774
}
3054330775
#endif
3054430776
@@ -31210,11 +31442,15 @@
3121031442
#else
3121131443
{ "fchown", (sqlite3_syscall_ptr)0, 0 },
3121231444
#endif
3121331445
#define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
3121431446
31447
+#if defined(HAVE_FCHOWN)
3121531448
{ "geteuid", (sqlite3_syscall_ptr)geteuid, 0 },
31449
+#else
31450
+ { "geteuid", (sqlite3_syscall_ptr)0, 0 },
31451
+#endif
3121631452
#define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent)
3121731453
3121831454
#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
3121931455
{ "mmap", (sqlite3_syscall_ptr)mmap, 0 },
3122031456
#else
@@ -31438,19 +31674,20 @@
3143831674
**
3143931675
** unixEnterMutex()
3144031676
** assert( unixMutexHeld() );
3144131677
** unixEnterLeave()
3144231678
*/
31679
+static sqlite3_mutex *unixBigLock = 0;
3144331680
static void unixEnterMutex(void){
31444
- sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
31681
+ sqlite3_mutex_enter(unixBigLock);
3144531682
}
3144631683
static void unixLeaveMutex(void){
31447
- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
31684
+ sqlite3_mutex_leave(unixBigLock);
3144831685
}
3144931686
#ifdef SQLITE_DEBUG
3145031687
static int unixMutexHeld(void) {
31451
- return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
31688
+ return sqlite3_mutex_held(unixBigLock);
3145231689
}
3145331690
#endif
3145431691
3145531692
3145631693
#ifdef SQLITE_HAVE_OS_TRACE
@@ -34917,16 +35154,14 @@
3491735154
/* Locks are within range */
3491835155
assert( n>=1 && n<=SQLITE_SHM_NLOCK );
3491935156
3492035157
if( pShmNode->h>=0 ){
3492135158
/* Initialize the locking parameters */
34922
- memset(&f, 0, sizeof(f));
3492335159
f.l_type = lockType;
3492435160
f.l_whence = SEEK_SET;
3492535161
f.l_start = ofst;
3492635162
f.l_len = n;
34927
-
3492835163
rc = osFcntl(pShmNode->h, F_SETLK, &f);
3492935164
rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
3493035165
}
3493135166
3493235167
/* Update the global lock state and do debug tracing */
@@ -36588,11 +36823,10 @@
3658836823
*/
3658936824
if( randomnessPid!=osGetpid(0) ){
3659036825
randomnessPid = osGetpid(0);
3659136826
sqlite3_randomness(0,0);
3659236827
}
36593
-
3659436828
memset(p, 0, sizeof(unixFile));
3659536829
3659636830
if( eType==SQLITE_OPEN_MAIN_DB ){
3659736831
UnixUnusedFd *pUnused;
3659836832
pUnused = findReusableFd(zName, flags);
@@ -38463,10 +38697,11 @@
3846338697
3846438698
/* Register all VFSes defined in the aVfs[] array */
3846538699
for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
3846638700
sqlite3_vfs_register(&aVfs[i], i==0);
3846738701
}
38702
+ unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
3846838703
return SQLITE_OK;
3846938704
}
3847038705
3847138706
/*
3847238707
** Shutdown the operating system interface.
@@ -38474,10 +38709,11 @@
3847438709
** Some operating systems might need to do some cleanup in this routine,
3847538710
** to release dynamically allocated objects. But not on unix.
3847638711
** This routine is a no-op for unix.
3847738712
*/
3847838713
SQLITE_API int sqlite3_os_end(void){
38714
+ unixBigLock = 0;
3847938715
return SQLITE_OK;
3848038716
}
3848138717
3848238718
#endif /* SQLITE_OS_UNIX */
3848338719
@@ -42312,19 +42548,20 @@
4231242548
**
4231342549
** winShmEnterMutex()
4231442550
** assert( winShmMutexHeld() );
4231542551
** winShmLeaveMutex()
4231642552
*/
42553
+static sqlite3_mutex *winBigLock = 0;
4231742554
static void winShmEnterMutex(void){
42318
- sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
42555
+ sqlite3_mutex_enter(winBigLock);
4231942556
}
4232042557
static void winShmLeaveMutex(void){
42321
- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
42558
+ sqlite3_mutex_leave(winBigLock);
4232242559
}
4232342560
#ifndef NDEBUG
4232442561
static int winShmMutexHeld(void) {
42325
- return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
42562
+ return sqlite3_mutex_held(winBigLock);
4232642563
}
4232742564
#endif
4232842565
4232942566
/*
4233042567
** Object used to represent a single file opened and mmapped to provide
@@ -44742,10 +44979,14 @@
4474244979
sqlite3_vfs_register(&winNolockVfs, 0);
4474344980
4474444981
#if defined(SQLITE_WIN32_HAS_WIDE)
4474544982
sqlite3_vfs_register(&winLongPathNolockVfs, 0);
4474644983
#endif
44984
+
44985
+#ifndef SQLITE_OMIT_WAL
44986
+ winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
44987
+#endif
4474744988
4474844989
return SQLITE_OK;
4474944990
}
4475044991
4475144992
SQLITE_API int sqlite3_os_end(void){
@@ -44753,16 +44994,613 @@
4475344994
if( sleepObj!=NULL ){
4475444995
osCloseHandle(sleepObj);
4475544996
sleepObj = NULL;
4475644997
}
4475744998
#endif
44999
+
45000
+#ifndef SQLITE_OMIT_WAL
45001
+ winBigLock = 0;
45002
+#endif
45003
+
4475845004
return SQLITE_OK;
4475945005
}
4476045006
4476145007
#endif /* SQLITE_OS_WIN */
4476245008
4476345009
/************** End of os_win.c **********************************************/
45010
+/************** Begin file memdb.c *******************************************/
45011
+/*
45012
+** 2016-09-07
45013
+**
45014
+** The author disclaims copyright to this source code. In place of
45015
+** a legal notice, here is a blessing:
45016
+**
45017
+** May you do good and not evil.
45018
+** May you find forgiveness for yourself and forgive others.
45019
+** May you share freely, never taking more than you give.
45020
+**
45021
+******************************************************************************
45022
+**
45023
+** This file implements in-memory VFS. A database is held as a contiguous
45024
+** block of memory.
45025
+**
45026
+** This file also implements interface sqlite3_serialize() and
45027
+** sqlite3_deserialize().
45028
+*/
45029
+#ifdef SQLITE_ENABLE_DESERIALIZE
45030
+/* #include "sqliteInt.h" */
45031
+
45032
+/*
45033
+** Forward declaration of objects used by this utility
45034
+*/
45035
+typedef struct sqlite3_vfs MemVfs;
45036
+typedef struct MemFile MemFile;
45037
+
45038
+/* Access to a lower-level VFS that (might) implement dynamic loading,
45039
+** access to randomness, etc.
45040
+*/
45041
+#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
45042
+
45043
+/* An open file */
45044
+struct MemFile {
45045
+ sqlite3_file base; /* IO methods */
45046
+ sqlite3_int64 sz; /* Size of the file */
45047
+ sqlite3_int64 szMax; /* Space allocated to aData */
45048
+ unsigned char *aData; /* content of the file */
45049
+ int nMmap; /* Number of memory mapped pages */
45050
+ unsigned mFlags; /* Flags */
45051
+ int eLock; /* Most recent lock against this file */
45052
+};
45053
+
45054
+/*
45055
+** Methods for MemFile
45056
+*/
45057
+static int memdbClose(sqlite3_file*);
45058
+static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
45059
+static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
45060
+static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
45061
+static int memdbSync(sqlite3_file*, int flags);
45062
+static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
45063
+static int memdbLock(sqlite3_file*, int);
45064
+/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
45065
+static int memdbFileControl(sqlite3_file*, int op, void *pArg);
45066
+/* static int memdbSectorSize(sqlite3_file*); // not used */
45067
+static int memdbDeviceCharacteristics(sqlite3_file*);
45068
+static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
45069
+static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
45070
+
45071
+/*
45072
+** Methods for MemVfs
45073
+*/
45074
+static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
45075
+/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
45076
+static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
45077
+static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
45078
+static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
45079
+static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
45080
+static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
45081
+static void memdbDlClose(sqlite3_vfs*, void*);
45082
+static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
45083
+static int memdbSleep(sqlite3_vfs*, int microseconds);
45084
+/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
45085
+static int memdbGetLastError(sqlite3_vfs*, int, char *);
45086
+static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
45087
+
45088
+static sqlite3_vfs memdb_vfs = {
45089
+ 2, /* iVersion */
45090
+ 0, /* szOsFile (set when registered) */
45091
+ 1024, /* mxPathname */
45092
+ 0, /* pNext */
45093
+ "memdb", /* zName */
45094
+ 0, /* pAppData (set when registered) */
45095
+ memdbOpen, /* xOpen */
45096
+ 0, /* memdbDelete, */ /* xDelete */
45097
+ memdbAccess, /* xAccess */
45098
+ memdbFullPathname, /* xFullPathname */
45099
+ memdbDlOpen, /* xDlOpen */
45100
+ memdbDlError, /* xDlError */
45101
+ memdbDlSym, /* xDlSym */
45102
+ memdbDlClose, /* xDlClose */
45103
+ memdbRandomness, /* xRandomness */
45104
+ memdbSleep, /* xSleep */
45105
+ 0, /* memdbCurrentTime, */ /* xCurrentTime */
45106
+ memdbGetLastError, /* xGetLastError */
45107
+ memdbCurrentTimeInt64 /* xCurrentTimeInt64 */
45108
+};
45109
+
45110
+static const sqlite3_io_methods memdb_io_methods = {
45111
+ 3, /* iVersion */
45112
+ memdbClose, /* xClose */
45113
+ memdbRead, /* xRead */
45114
+ memdbWrite, /* xWrite */
45115
+ memdbTruncate, /* xTruncate */
45116
+ memdbSync, /* xSync */
45117
+ memdbFileSize, /* xFileSize */
45118
+ memdbLock, /* xLock */
45119
+ memdbLock, /* xUnlock - same as xLock in this case */
45120
+ 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
45121
+ memdbFileControl, /* xFileControl */
45122
+ 0, /* memdbSectorSize,*/ /* xSectorSize */
45123
+ memdbDeviceCharacteristics, /* xDeviceCharacteristics */
45124
+ 0, /* xShmMap */
45125
+ 0, /* xShmLock */
45126
+ 0, /* xShmBarrier */
45127
+ 0, /* xShmUnmap */
45128
+ memdbFetch, /* xFetch */
45129
+ memdbUnfetch /* xUnfetch */
45130
+};
45131
+
45132
+
45133
+
45134
+/*
45135
+** Close an memdb-file.
45136
+**
45137
+** The pData pointer is owned by the application, so there is nothing
45138
+** to free.
45139
+*/
45140
+static int memdbClose(sqlite3_file *pFile){
45141
+ MemFile *p = (MemFile *)pFile;
45142
+ if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ) sqlite3_free(p->aData);
45143
+ return SQLITE_OK;
45144
+}
45145
+
45146
+/*
45147
+** Read data from an memdb-file.
45148
+*/
45149
+static int memdbRead(
45150
+ sqlite3_file *pFile,
45151
+ void *zBuf,
45152
+ int iAmt,
45153
+ sqlite_int64 iOfst
45154
+){
45155
+ MemFile *p = (MemFile *)pFile;
45156
+ if( iOfst+iAmt>p->sz ){
45157
+ memset(zBuf, 0, iAmt);
45158
+ if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
45159
+ return SQLITE_IOERR_SHORT_READ;
45160
+ }
45161
+ memcpy(zBuf, p->aData+iOfst, iAmt);
45162
+ return SQLITE_OK;
45163
+}
45164
+
45165
+/*
45166
+** Try to enlarge the memory allocation to hold at least sz bytes
45167
+*/
45168
+static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){
45169
+ unsigned char *pNew;
45170
+ if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){
45171
+ return SQLITE_FULL;
45172
+ }
45173
+ pNew = sqlite3_realloc64(p->aData, newSz);
45174
+ if( pNew==0 ) return SQLITE_NOMEM;
45175
+ p->aData = pNew;
45176
+ p->szMax = newSz;
45177
+ return SQLITE_OK;
45178
+}
45179
+
45180
+/*
45181
+** Write data to an memdb-file.
45182
+*/
45183
+static int memdbWrite(
45184
+ sqlite3_file *pFile,
45185
+ const void *z,
45186
+ int iAmt,
45187
+ sqlite_int64 iOfst
45188
+){
45189
+ MemFile *p = (MemFile *)pFile;
45190
+ if( iOfst+iAmt>p->sz ){
45191
+ int rc;
45192
+ if( iOfst+iAmt>p->szMax
45193
+ && (rc = memdbEnlarge(p, (iOfst+iAmt)*2))!=SQLITE_OK
45194
+ ){
45195
+ return rc;
45196
+ }
45197
+ if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
45198
+ p->sz = iOfst+iAmt;
45199
+ }
45200
+ memcpy(p->aData+iOfst, z, iAmt);
45201
+ return SQLITE_OK;
45202
+}
45203
+
45204
+/*
45205
+** Truncate an memdb-file.
45206
+**
45207
+** In rollback mode (which is always the case for memdb, as it does not
45208
+** support WAL mode) the truncate() method is only used to reduce
45209
+** the size of a file, never to increase the size.
45210
+*/
45211
+static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
45212
+ MemFile *p = (MemFile *)pFile;
45213
+ if( NEVER(size>p->sz) ) return SQLITE_FULL;
45214
+ p->sz = size;
45215
+ return SQLITE_OK;
45216
+}
45217
+
45218
+/*
45219
+** Sync an memdb-file.
45220
+*/
45221
+static int memdbSync(sqlite3_file *pFile, int flags){
45222
+ return SQLITE_OK;
45223
+}
45224
+
45225
+/*
45226
+** Return the current file-size of an memdb-file.
45227
+*/
45228
+static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
45229
+ MemFile *p = (MemFile *)pFile;
45230
+ *pSize = p->sz;
45231
+ return SQLITE_OK;
45232
+}
45233
+
45234
+/*
45235
+** Lock an memdb-file.
45236
+*/
45237
+static int memdbLock(sqlite3_file *pFile, int eLock){
45238
+ MemFile *p = (MemFile *)pFile;
45239
+ p->eLock = eLock;
45240
+ return SQLITE_OK;
45241
+}
45242
+
45243
+#if 0 /* Never used because memdbAccess() always returns false */
45244
+/*
45245
+** Check if another file-handle holds a RESERVED lock on an memdb-file.
45246
+*/
45247
+static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
45248
+ *pResOut = 0;
45249
+ return SQLITE_OK;
45250
+}
45251
+#endif
45252
+
45253
+/*
45254
+** File control method. For custom operations on an memdb-file.
45255
+*/
45256
+static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
45257
+ MemFile *p = (MemFile *)pFile;
45258
+ int rc = SQLITE_NOTFOUND;
45259
+ if( op==SQLITE_FCNTL_VFSNAME ){
45260
+ *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
45261
+ rc = SQLITE_OK;
45262
+ }
45263
+ return rc;
45264
+}
45265
+
45266
+#if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
45267
+/*
45268
+** Return the sector-size in bytes for an memdb-file.
45269
+*/
45270
+static int memdbSectorSize(sqlite3_file *pFile){
45271
+ return 1024;
45272
+}
45273
+#endif
45274
+
45275
+/*
45276
+** Return the device characteristic flags supported by an memdb-file.
45277
+*/
45278
+static int memdbDeviceCharacteristics(sqlite3_file *pFile){
45279
+ return SQLITE_IOCAP_ATOMIC |
45280
+ SQLITE_IOCAP_POWERSAFE_OVERWRITE |
45281
+ SQLITE_IOCAP_SAFE_APPEND |
45282
+ SQLITE_IOCAP_SEQUENTIAL;
45283
+}
45284
+
45285
+/* Fetch a page of a memory-mapped file */
45286
+static int memdbFetch(
45287
+ sqlite3_file *pFile,
45288
+ sqlite3_int64 iOfst,
45289
+ int iAmt,
45290
+ void **pp
45291
+){
45292
+ MemFile *p = (MemFile *)pFile;
45293
+ p->nMmap++;
45294
+ *pp = (void*)(p->aData + iOfst);
45295
+ return SQLITE_OK;
45296
+}
45297
+
45298
+/* Release a memory-mapped page */
45299
+static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
45300
+ MemFile *p = (MemFile *)pFile;
45301
+ p->nMmap--;
45302
+ return SQLITE_OK;
45303
+}
45304
+
45305
+/*
45306
+** Open an mem file handle.
45307
+*/
45308
+static int memdbOpen(
45309
+ sqlite3_vfs *pVfs,
45310
+ const char *zName,
45311
+ sqlite3_file *pFile,
45312
+ int flags,
45313
+ int *pOutFlags
45314
+){
45315
+ MemFile *p = (MemFile*)pFile;
45316
+ if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
45317
+ return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags);
45318
+ }
45319
+ memset(p, 0, sizeof(*p));
45320
+ p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
45321
+ assert( pOutFlags!=0 ); /* True because flags==SQLITE_OPEN_MAIN_DB */
45322
+ *pOutFlags = flags | SQLITE_OPEN_MEMORY;
45323
+ p->base.pMethods = &memdb_io_methods;
45324
+ return SQLITE_OK;
45325
+}
45326
+
45327
+#if 0 /* Only used to delete rollback journals, master journals, and WAL
45328
+ ** files, none of which exist in memdb. So this routine is never used */
45329
+/*
45330
+** Delete the file located at zPath. If the dirSync argument is true,
45331
+** ensure the file-system modifications are synced to disk before
45332
+** returning.
45333
+*/
45334
+static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
45335
+ return SQLITE_IOERR_DELETE;
45336
+}
45337
+#endif
45338
+
45339
+/*
45340
+** Test for access permissions. Return true if the requested permission
45341
+** is available, or false otherwise.
45342
+**
45343
+** With memdb, no files ever exist on disk. So always return false.
45344
+*/
45345
+static int memdbAccess(
45346
+ sqlite3_vfs *pVfs,
45347
+ const char *zPath,
45348
+ int flags,
45349
+ int *pResOut
45350
+){
45351
+ *pResOut = 0;
45352
+ return SQLITE_OK;
45353
+}
45354
+
45355
+/*
45356
+** Populate buffer zOut with the full canonical pathname corresponding
45357
+** to the pathname in zPath. zOut is guaranteed to point to a buffer
45358
+** of at least (INST_MAX_PATHNAME+1) bytes.
45359
+*/
45360
+static int memdbFullPathname(
45361
+ sqlite3_vfs *pVfs,
45362
+ const char *zPath,
45363
+ int nOut,
45364
+ char *zOut
45365
+){
45366
+ sqlite3_snprintf(nOut, zOut, "%s", zPath);
45367
+ return SQLITE_OK;
45368
+}
45369
+
45370
+/*
45371
+** Open the dynamic library located at zPath and return a handle.
45372
+*/
45373
+static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
45374
+ return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
45375
+}
45376
+
45377
+/*
45378
+** Populate the buffer zErrMsg (size nByte bytes) with a human readable
45379
+** utf-8 string describing the most recent error encountered associated
45380
+** with dynamic libraries.
45381
+*/
45382
+static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
45383
+ ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
45384
+}
45385
+
45386
+/*
45387
+** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
45388
+*/
45389
+static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
45390
+ return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
45391
+}
45392
+
45393
+/*
45394
+** Close the dynamic library handle pHandle.
45395
+*/
45396
+static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
45397
+ ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
45398
+}
45399
+
45400
+/*
45401
+** Populate the buffer pointed to by zBufOut with nByte bytes of
45402
+** random data.
45403
+*/
45404
+static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
45405
+ return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
45406
+}
45407
+
45408
+/*
45409
+** Sleep for nMicro microseconds. Return the number of microseconds
45410
+** actually slept.
45411
+*/
45412
+static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
45413
+ return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
45414
+}
45415
+
45416
+#if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */
45417
+/*
45418
+** Return the current time as a Julian Day number in *pTimeOut.
45419
+*/
45420
+static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
45421
+ return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
45422
+}
45423
+#endif
45424
+
45425
+static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
45426
+ return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
45427
+}
45428
+static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
45429
+ return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
45430
+}
45431
+
45432
+/*
45433
+** Translate a database connection pointer and schema name into a
45434
+** MemFile pointer.
45435
+*/
45436
+static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
45437
+ MemFile *p = 0;
45438
+ int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
45439
+ if( rc ) return 0;
45440
+ if( p->base.pMethods!=&memdb_io_methods ) return 0;
45441
+ return p;
45442
+}
45443
+
45444
+/*
45445
+** Return the serialization of a database
45446
+*/
45447
+SQLITE_API unsigned char *sqlite3_serialize(
45448
+ sqlite3 *db, /* The database connection */
45449
+ const char *zSchema, /* Which database within the connection */
45450
+ sqlite3_int64 *piSize, /* Write size here, if not NULL */
45451
+ unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */
45452
+){
45453
+ MemFile *p;
45454
+ int iDb;
45455
+ Btree *pBt;
45456
+ sqlite3_int64 sz;
45457
+ int szPage = 0;
45458
+ sqlite3_stmt *pStmt = 0;
45459
+ unsigned char *pOut;
45460
+ char *zSql;
45461
+ int rc;
45462
+
45463
+#ifdef SQLITE_ENABLE_API_ARMOR
45464
+ if( !sqlite3SafetyCheckOk(db) ){
45465
+ (void)SQLITE_MISUSE_BKPT;
45466
+ return 0;
45467
+ }
45468
+#endif
45469
+
45470
+ if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
45471
+ p = memdbFromDbSchema(db, zSchema);
45472
+ iDb = sqlite3FindDbName(db, zSchema);
45473
+ if( piSize ) *piSize = -1;
45474
+ if( iDb<0 ) return 0;
45475
+ if( p ){
45476
+ if( piSize ) *piSize = p->sz;
45477
+ if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
45478
+ pOut = p->aData;
45479
+ }else{
45480
+ pOut = sqlite3_malloc64( p->sz );
45481
+ if( pOut ) memcpy(pOut, p->aData, p->sz);
45482
+ }
45483
+ return pOut;
45484
+ }
45485
+ pBt = db->aDb[iDb].pBt;
45486
+ if( pBt==0 ) return 0;
45487
+ szPage = sqlite3BtreeGetPageSize(pBt);
45488
+ zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
45489
+ rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
45490
+ sqlite3_free(zSql);
45491
+ if( rc ) return 0;
45492
+ rc = sqlite3_step(pStmt);
45493
+ if( rc!=SQLITE_ROW ){
45494
+ pOut = 0;
45495
+ }else{
45496
+ sz = sqlite3_column_int64(pStmt, 0)*szPage;
45497
+ if( piSize ) *piSize = sz;
45498
+ if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
45499
+ pOut = 0;
45500
+ }else{
45501
+ pOut = sqlite3_malloc64( sz );
45502
+ if( pOut ){
45503
+ int nPage = sqlite3_column_int(pStmt, 0);
45504
+ Pager *pPager = sqlite3BtreePager(pBt);
45505
+ int pgno;
45506
+ for(pgno=1; pgno<=nPage; pgno++){
45507
+ DbPage *pPage = 0;
45508
+ unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
45509
+ rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
45510
+ if( rc==SQLITE_OK ){
45511
+ memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
45512
+ }else{
45513
+ memset(pTo, 0, szPage);
45514
+ }
45515
+ sqlite3PagerUnref(pPage);
45516
+ }
45517
+ }
45518
+ }
45519
+ }
45520
+ sqlite3_finalize(pStmt);
45521
+ return pOut;
45522
+}
45523
+
45524
+/* Convert zSchema to a MemDB and initialize its content.
45525
+*/
45526
+SQLITE_API int sqlite3_deserialize(
45527
+ sqlite3 *db, /* The database connection */
45528
+ const char *zSchema, /* Which DB to reopen with the deserialization */
45529
+ unsigned char *pData, /* The serialized database content */
45530
+ sqlite3_int64 szDb, /* Number bytes in the deserialization */
45531
+ sqlite3_int64 szBuf, /* Total size of buffer pData[] */
45532
+ unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
45533
+){
45534
+ MemFile *p;
45535
+ char *zSql;
45536
+ sqlite3_stmt *pStmt = 0;
45537
+ int rc;
45538
+ int iDb;
45539
+
45540
+#ifdef SQLITE_ENABLE_API_ARMOR
45541
+ if( !sqlite3SafetyCheckOk(db) ){
45542
+ return SQLITE_MISUSE_BKPT;
45543
+ }
45544
+ if( szDb<0 ) return SQLITE_MISUSE_BKPT;
45545
+ if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
45546
+#endif
45547
+
45548
+ sqlite3_mutex_enter(db->mutex);
45549
+ if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
45550
+ iDb = sqlite3FindDbName(db, zSchema);
45551
+ if( iDb<0 ){
45552
+ rc = SQLITE_ERROR;
45553
+ goto end_deserialize;
45554
+ }
45555
+ zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
45556
+ rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
45557
+ sqlite3_free(zSql);
45558
+ if( rc ) goto end_deserialize;
45559
+ db->init.iDb = (u8)iDb;
45560
+ db->init.reopenMemdb = 1;
45561
+ rc = sqlite3_step(pStmt);
45562
+ db->init.reopenMemdb = 0;
45563
+ if( rc!=SQLITE_DONE ){
45564
+ rc = SQLITE_ERROR;
45565
+ goto end_deserialize;
45566
+ }
45567
+ p = memdbFromDbSchema(db, zSchema);
45568
+ if( p==0 ){
45569
+ rc = SQLITE_ERROR;
45570
+ }else{
45571
+ p->aData = pData;
45572
+ p->sz = szDb;
45573
+ p->szMax = szBuf;
45574
+ p->mFlags = mFlags;
45575
+ rc = SQLITE_OK;
45576
+ }
45577
+
45578
+end_deserialize:
45579
+ sqlite3_finalize(pStmt);
45580
+ sqlite3_mutex_leave(db->mutex);
45581
+ return rc;
45582
+}
45583
+
45584
+/*
45585
+** This routine is called when the extension is loaded.
45586
+** Register the new VFS.
45587
+*/
45588
+SQLITE_PRIVATE int sqlite3MemdbInit(void){
45589
+ sqlite3_vfs *pLower = sqlite3_vfs_find(0);
45590
+ int sz = pLower->szOsFile;
45591
+ memdb_vfs.pAppData = pLower;
45592
+ /* In all known configurations of SQLite, the size of a default
45593
+ ** sqlite3_file is greater than the size of a memdb sqlite3_file.
45594
+ ** Should that ever change, remove the following NEVER() */
45595
+ if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile);
45596
+ memdb_vfs.szOsFile = sz;
45597
+ return sqlite3_vfs_register(&memdb_vfs, 0);
45598
+}
45599
+#endif /* SQLITE_ENABLE_DESERIALIZE */
45600
+
45601
+/************** End of memdb.c ***********************************************/
4476445602
/************** Begin file bitvec.c ******************************************/
4476545603
/*
4476645604
** 2008 February 16
4476745605
**
4476845606
** The author disclaims copyright to this source code. In place of
@@ -45607,11 +46445,11 @@
4560746445
int rc;
4560846446
#ifdef SQLITE_LOG_CACHE_SPILL
4560946447
sqlite3_log(SQLITE_FULL,
4561046448
"spill page %d making room for %d - cache used: %d/%d",
4561146449
pPg->pgno, pgno,
45612
- sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
46450
+ sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
4561346451
numberOfCachePages(pCache));
4561446452
#endif
4561546453
pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
4561646454
rc = pCache->xStress(pCache->pStress, pPg);
4561746455
pcacheDump(pCache);
@@ -48663,11 +49501,11 @@
4866349501
i64 journalSizeLimit; /* Size limit for persistent journal files */
4866449502
char *zFilename; /* Name of the database file */
4866549503
char *zJournal; /* Name of the journal file */
4866649504
int (*xBusyHandler)(void*); /* Function to call when busy */
4866749505
void *pBusyHandlerArg; /* Context argument for xBusyHandler */
48668
- int aStat[3]; /* Total cache hits, misses and writes */
49506
+ int aStat[4]; /* Total cache hits, misses, writes, spills */
4866949507
#ifdef SQLITE_TEST
4867049508
int nRead; /* Database pages read */
4867149509
#endif
4867249510
void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
4867349511
int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
@@ -48691,10 +49529,11 @@
4869149529
** or CACHE_WRITE to sqlite3_db_status().
4869249530
*/
4869349531
#define PAGER_STAT_HIT 0
4869449532
#define PAGER_STAT_MISS 1
4869549533
#define PAGER_STAT_WRITE 2
49534
+#define PAGER_STAT_SPILL 3
4869649535
4869749536
/*
4869849537
** The following global variables hold counters used for
4869949538
** testing purposes only. These variables do not exist in
4870049539
** a non-testing build. These variables are not thread-safe.
@@ -49177,11 +50016,11 @@
4917750016
#else
4917850017
UNUSED_PARAMETER(pPager);
4917950018
#endif
4918050019
4918150020
#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
49182
- if( dc&SQLITE_IOCAP_BATCH_ATOMIC ){
50021
+ if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
4918350022
return -1;
4918450023
}
4918550024
#endif
4918650025
4918750026
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
@@ -52066,10 +52905,34 @@
5206652905
pNext = p->pDirty;
5206752906
sqlite3_free(p);
5206852907
}
5206952908
}
5207052909
52910
+/* Verify that the database file has not be deleted or renamed out from
52911
+** under the pager. Return SQLITE_OK if the database is still where it ought
52912
+** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
52913
+** code from sqlite3OsAccess()) if the database has gone missing.
52914
+*/
52915
+static int databaseIsUnmoved(Pager *pPager){
52916
+ int bHasMoved = 0;
52917
+ int rc;
52918
+
52919
+ if( pPager->tempFile ) return SQLITE_OK;
52920
+ if( pPager->dbSize==0 ) return SQLITE_OK;
52921
+ assert( pPager->zFilename && pPager->zFilename[0] );
52922
+ rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
52923
+ if( rc==SQLITE_NOTFOUND ){
52924
+ /* If the HAS_MOVED file-control is unimplemented, assume that the file
52925
+ ** has not been moved. That is the historical behavior of SQLite: prior to
52926
+ ** version 3.8.3, it never checked */
52927
+ rc = SQLITE_OK;
52928
+ }else if( rc==SQLITE_OK && bHasMoved ){
52929
+ rc = SQLITE_READONLY_DBMOVED;
52930
+ }
52931
+ return rc;
52932
+}
52933
+
5207152934
5207252935
/*
5207352936
** Shutdown the page cache. Free all memory and close all files.
5207452937
**
5207552938
** If a transaction was in progress when this routine is called, that
@@ -52082,25 +52945,30 @@
5208252945
** is made to roll it back. If an error occurs during the rollback
5208352946
** a hot journal may be left in the filesystem but no error is returned
5208452947
** to the caller.
5208552948
*/
5208652949
SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
52087
- u8 *pTmp = (u8 *)pPager->pTmpSpace;
52088
-
52950
+ u8 *pTmp = (u8*)pPager->pTmpSpace;
5208952951
assert( db || pagerUseWal(pPager)==0 );
5209052952
assert( assert_pager_state(pPager) );
5209152953
disable_simulated_io_errors();
5209252954
sqlite3BeginBenignMalloc();
5209352955
pagerFreeMapHdrs(pPager);
5209452956
/* pPager->errCode = 0; */
5209552957
pPager->exclusiveMode = 0;
5209652958
#ifndef SQLITE_OMIT_WAL
52097
- assert( db || pPager->pWal==0 );
52098
- sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,
52099
- (db && (db->flags & SQLITE_NoCkptOnClose) ? 0 : pTmp)
52100
- );
52101
- pPager->pWal = 0;
52959
+ {
52960
+ u8 *a = 0;
52961
+ assert( db || pPager->pWal==0 );
52962
+ if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
52963
+ && SQLITE_OK==databaseIsUnmoved(pPager)
52964
+ ){
52965
+ a = pTmp;
52966
+ }
52967
+ sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
52968
+ pPager->pWal = 0;
52969
+ }
5210252970
#endif
5210352971
pager_reset(pPager);
5210452972
if( MEMDB ){
5210552973
pager_unlock(pPager);
5210652974
}else{
@@ -52553,10 +53421,11 @@
5255353421
|| (pPg->flags & PGHDR_NEED_SYNC)!=0)
5255453422
){
5255553423
return SQLITE_OK;
5255653424
}
5255753425
53426
+ pPager->aStat[PAGER_STAT_SPILL]++;
5255853427
pPg->pDirty = 0;
5255953428
if( pagerUseWal(pPager) ){
5256053429
/* Write a single frame for this page to the log. */
5256153430
rc = subjournalPageIfRequired(pPg);
5256253431
if( rc==SQLITE_OK ){
@@ -52658,10 +53527,15 @@
5265853527
u8 *pPtr;
5265953528
Pager *pPager = 0; /* Pager object to allocate and return */
5266053529
int rc = SQLITE_OK; /* Return code */
5266153530
int tempFile = 0; /* True for temp files (incl. in-memory files) */
5266253531
int memDb = 0; /* True if this is an in-memory file */
53532
+#ifdef SQLITE_ENABLE_DESERIALIZE
53533
+ int memJM = 0; /* Memory journal mode */
53534
+#else
53535
+# define memJM 0
53536
+#endif
5266353537
int readOnly = 0; /* True if this is a read-only file */
5266453538
int journalFileSize; /* Bytes to allocate for each journal fd */
5266553539
char *zPathname = 0; /* Full path to database file */
5266653540
int nPathname = 0; /* Number of bytes in zPathname */
5266753541
int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
@@ -52785,11 +53659,14 @@
5278553659
*/
5278653660
if( zFilename && zFilename[0] ){
5278753661
int fout = 0; /* VFS flags returned by xOpen() */
5278853662
rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
5278953663
assert( !memDb );
52790
- readOnly = (fout&SQLITE_OPEN_READONLY);
53664
+#ifdef SQLITE_ENABLE_DESERIALIZE
53665
+ memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
53666
+#endif
53667
+ readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
5279153668
5279253669
/* If the file was successfully opened for read/write access,
5279353670
** choose a default page size in case we have to create the
5279453671
** database file. The default page size is the maximum of:
5279553672
**
@@ -52916,11 +53793,11 @@
5291653793
pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
5291753794
assert( isOpen(pPager->fd) || tempFile );
5291853795
setSectorSize(pPager);
5291953796
if( !useJournal ){
5292053797
pPager->journalMode = PAGER_JOURNALMODE_OFF;
52921
- }else if( memDb ){
53798
+ }else if( memDb || memJM ){
5292253799
pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
5292353800
}
5292453801
/* pPager->xBusyHandler = 0; */
5292553802
/* pPager->pBusyHandlerArg = 0; */
5292653803
pPager->xReiniter = xReinit;
@@ -52931,34 +53808,10 @@
5293153808
*ppPager = pPager;
5293253809
return SQLITE_OK;
5293353810
}
5293453811
5293553812
52936
-/* Verify that the database file has not be deleted or renamed out from
52937
-** under the pager. Return SQLITE_OK if the database is still were it ought
52938
-** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
52939
-** code from sqlite3OsAccess()) if the database has gone missing.
52940
-*/
52941
-static int databaseIsUnmoved(Pager *pPager){
52942
- int bHasMoved = 0;
52943
- int rc;
52944
-
52945
- if( pPager->tempFile ) return SQLITE_OK;
52946
- if( pPager->dbSize==0 ) return SQLITE_OK;
52947
- assert( pPager->zFilename && pPager->zFilename[0] );
52948
- rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
52949
- if( rc==SQLITE_NOTFOUND ){
52950
- /* If the HAS_MOVED file-control is unimplemented, assume that the file
52951
- ** has not been moved. That is the historical behavior of SQLite: prior to
52952
- ** version 3.8.3, it never checked */
52953
- rc = SQLITE_OK;
52954
- }else if( rc==SQLITE_OK && bHasMoved ){
52955
- rc = SQLITE_READONLY_DBMOVED;
52956
- }
52957
- return rc;
52958
-}
52959
-
5296053813
5296153814
/*
5296253815
** This function is called after transitioning from PAGER_UNLOCK to
5296353816
** PAGER_SHARED state. It tests if there is a hot journal present in
5296453817
** the file-system for the given pager. A hot journal is one that
@@ -54463,12 +55316,13 @@
5446355316
}
5446455317
rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
5446555318
if( bBatch ){
5446655319
if( rc==SQLITE_OK ){
5446755320
rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
54468
- }else{
54469
- sqlite3OsFileControl(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
55321
+ }
55322
+ if( rc!=SQLITE_OK ){
55323
+ sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
5447055324
}
5447155325
}
5447255326
5447355327
if( rc!=SQLITE_OK ){
5447455328
assert( rc!=SQLITE_IOERR_BLOCKED );
@@ -54688,30 +55542,37 @@
5468855542
return a;
5468955543
}
5469055544
#endif
5469155545
5469255546
/*
54693
-** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
54694
-** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
55547
+** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
55548
+** or _WRITE+1. The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
55549
+** of SQLITE_DBSTATUS_CACHE_SPILL. The _SPILL case is not contiguous because
55550
+** it was added later.
55551
+**
55552
+** Before returning, *pnVal is incremented by the
5469555553
** current cache hit or miss count, according to the value of eStat. If the
5469655554
** reset parameter is non-zero, the cache hit or miss count is zeroed before
5469755555
** returning.
5469855556
*/
5469955557
SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
5470055558
5470155559
assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
5470255560
|| eStat==SQLITE_DBSTATUS_CACHE_MISS
5470355561
|| eStat==SQLITE_DBSTATUS_CACHE_WRITE
55562
+ || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
5470455563
);
5470555564
5470655565
assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
5470755566
assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
54708
- assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
55567
+ assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
55568
+ && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
5470955569
54710
- *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
55570
+ eStat -= SQLITE_DBSTATUS_CACHE_HIT;
55571
+ *pnVal += pPager->aStat[eStat];
5471155572
if( reset ){
54712
- pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
55573
+ pPager->aStat[eStat] = 0;
5471355574
}
5471455575
}
5471555576
5471655577
/*
5471755578
** Return true if this is an in-memory or temp-file backed pager.
@@ -56153,11 +57014,15 @@
5615357014
**
5615457015
** If this call is successful, *ppPage is set to point to the wal-index
5615557016
** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
5615657017
** then an SQLite error code is returned and *ppPage is set to 0.
5615757018
*/
56158
-static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
57019
+static SQLITE_NOINLINE int walIndexPageRealloc(
57020
+ Wal *pWal, /* The WAL context */
57021
+ int iPage, /* The page we seek */
57022
+ volatile u32 **ppPage /* Write the page pointer here */
57023
+){
5615957024
int rc = SQLITE_OK;
5616057025
5616157026
/* Enlarge the pWal->apWiData[] array if required */
5616257027
if( pWal->nWiData<=iPage ){
5616357028
int nByte = sizeof(u32*)*(iPage+1);
@@ -56172,32 +57037,41 @@
5617257037
pWal->apWiData = apNew;
5617357038
pWal->nWiData = iPage+1;
5617457039
}
5617557040
5617657041
/* Request a pointer to the required page from the VFS */
56177
- if( pWal->apWiData[iPage]==0 ){
56178
- if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
56179
- pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
56180
- if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
56181
- }else{
56182
- rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
56183
- pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
56184
- );
56185
- assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
56186
- testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
56187
- if( (rc&0xff)==SQLITE_READONLY ){
56188
- pWal->readOnly |= WAL_SHM_RDONLY;
56189
- if( rc==SQLITE_READONLY ){
56190
- rc = SQLITE_OK;
56191
- }
57042
+ assert( pWal->apWiData[iPage]==0 );
57043
+ if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
57044
+ pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
57045
+ if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
57046
+ }else{
57047
+ rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
57048
+ pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
57049
+ );
57050
+ assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
57051
+ testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
57052
+ if( (rc&0xff)==SQLITE_READONLY ){
57053
+ pWal->readOnly |= WAL_SHM_RDONLY;
57054
+ if( rc==SQLITE_READONLY ){
57055
+ rc = SQLITE_OK;
5619257056
}
5619357057
}
5619457058
}
5619557059
5619657060
*ppPage = pWal->apWiData[iPage];
5619757061
assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
5619857062
return rc;
57063
+}
57064
+static int walIndexPage(
57065
+ Wal *pWal, /* The WAL context */
57066
+ int iPage, /* The page we seek */
57067
+ volatile u32 **ppPage /* Write the page pointer here */
57068
+){
57069
+ if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
57070
+ return walIndexPageRealloc(pWal, iPage, ppPage);
57071
+ }
57072
+ return SQLITE_OK;
5619957073
}
5620057074
5620157075
/*
5620257076
** Return a pointer to the WalCkptInfo structure in the wal-index.
5620357077
*/
@@ -57171,21 +58045,22 @@
5717158045
sqlite3_free(p);
5717258046
}
5717358047
5717458048
/*
5717558049
** Construct a WalInterator object that can be used to loop over all
57176
-** pages in the WAL in ascending order. The caller must hold the checkpoint
57177
-** lock.
58050
+** pages in the WAL following frame nBackfill in ascending order. Frames
58051
+** nBackfill or earlier may be included - excluding them is an optimization
58052
+** only. The caller must hold the checkpoint lock.
5717858053
**
5717958054
** On success, make *pp point to the newly allocated WalInterator object
5718058055
** return SQLITE_OK. Otherwise, return an error code. If this routine
5718158056
** returns an error, the value of *pp is undefined.
5718258057
**
5718358058
** The calling routine should invoke walIteratorFree() to destroy the
5718458059
** WalIterator object when it has finished with it.
5718558060
*/
57186
-static int walIteratorInit(Wal *pWal, WalIterator **pp){
58061
+static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
5718758062
WalIterator *p; /* Return value */
5718858063
int nSegment; /* Number of segments to merge */
5718958064
u32 iLast; /* Last frame in log */
5719058065
int nByte; /* Number of bytes to allocate */
5719158066
int i; /* Iterator variable */
@@ -57218,11 +58093,11 @@
5721858093
);
5721958094
if( !aTmp ){
5722058095
rc = SQLITE_NOMEM_BKPT;
5722158096
}
5722258097
57223
- for(i=0; rc==SQLITE_OK && i<nSegment; i++){
58098
+ for(i=walFramePage(nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
5722458099
volatile ht_slot *aHash;
5722558100
u32 iZero;
5722658101
volatile u32 *aPgno;
5722758102
5722858103
rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
@@ -57252,10 +58127,11 @@
5725258127
}
5725358128
sqlite3_free(aTmp);
5725458129
5725558130
if( rc!=SQLITE_OK ){
5725658131
walIteratorFree(p);
58132
+ p = 0;
5725758133
}
5725858134
*pp = p;
5725958135
return rc;
5726058136
}
5726158137
@@ -57374,17 +58250,10 @@
5737458250
testcase( szPage<=32768 );
5737558251
testcase( szPage>=65536 );
5737658252
pInfo = walCkptInfo(pWal);
5737758253
if( pInfo->nBackfill<pWal->hdr.mxFrame ){
5737858254
57379
- /* Allocate the iterator */
57380
- rc = walIteratorInit(pWal, &pIter);
57381
- if( rc!=SQLITE_OK ){
57382
- return rc;
57383
- }
57384
- assert( pIter );
57385
-
5738658255
/* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
5738758256
** in the SQLITE_CHECKPOINT_PASSIVE mode. */
5738858257
assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
5738958258
5739058259
/* Compute in mxSafeFrame the index of the last frame of the WAL that is
@@ -57417,11 +58286,17 @@
5741758286
goto walcheckpoint_out;
5741858287
}
5741958288
}
5742058289
}
5742158290
57422
- if( pInfo->nBackfill<mxSafeFrame
58291
+ /* Allocate the iterator */
58292
+ if( pInfo->nBackfill<mxSafeFrame ){
58293
+ rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
58294
+ assert( rc==SQLITE_OK || pIter==0 );
58295
+ }
58296
+
58297
+ if( pIter
5742358298
&& (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK
5742458299
){
5742558300
i64 nSize; /* Current size of database file */
5742658301
u32 nBackfill = pInfo->nBackfill;
5742758302
@@ -58467,11 +59342,11 @@
5846759342
** (iFrame<=iLast):
5846859343
** This condition filters out entries that were added to the hash
5846959344
** table after the current read-transaction had started.
5847059345
*/
5847159346
iMinHash = walFramePage(pWal->minFrame);
58472
- for(iHash=walFramePage(iLast); iHash>=iMinHash && iRead==0; iHash--){
59347
+ for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
5847359348
volatile ht_slot *aHash; /* Pointer to hash table */
5847459349
volatile u32 *aPgno; /* Pointer to array of page numbers */
5847559350
u32 iZero; /* Frame number corresponding to aPgno[0] */
5847659351
int iKey; /* Hash slot index */
5847759352
int nCollide; /* Number of hash collisions remaining */
@@ -58490,10 +59365,11 @@
5849059365
}
5849159366
if( (nCollide--)==0 ){
5849259367
return SQLITE_CORRUPT_BKPT;
5849359368
}
5849459369
}
59370
+ if( iRead ) break;
5849559371
}
5849659372
5849759373
#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
5849859374
/* If expensive assert() statements are available, do a linear search
5849959375
** of the wal-index file content. Make sure the results agree with the
@@ -59904,24 +60780,24 @@
5990460780
struct BtCursor {
5990560781
u8 eState; /* One of the CURSOR_XXX constants (see below) */
5990660782
u8 curFlags; /* zero or more BTCF_* flags defined below */
5990760783
u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */
5990860784
u8 hints; /* As configured by CursorSetHints() */
59909
- int nOvflAlloc; /* Allocated size of aOverflow[] array */
59910
- Btree *pBtree; /* The Btree to which this cursor belongs */
59911
- BtShared *pBt; /* The BtShared this cursor points to */
59912
- BtCursor *pNext; /* Forms a linked list of all cursors */
59913
- Pgno *aOverflow; /* Cache of overflow page locations */
59914
- CellInfo info; /* A parse of the cell we are pointing at */
59915
- i64 nKey; /* Size of pKey, or last integer key */
59916
- void *pKey; /* Saved key that was cursor last known position */
59917
- Pgno pgnoRoot; /* The root page of this tree */
5991860785
int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
5991960786
** Error code if eState==CURSOR_FAULT */
60787
+ Btree *pBtree; /* The Btree to which this cursor belongs */
60788
+ Pgno *aOverflow; /* Cache of overflow page locations */
60789
+ void *pKey; /* Saved key that was cursor last known position */
5992060790
/* All fields above are zeroed when the cursor is allocated. See
5992160791
** sqlite3BtreeCursorZero(). Fields that follow must be manually
5992260792
** initialized. */
60793
+#define BTCURSOR_FIRST_UNINIT pBt /* Name of first uninitialized field */
60794
+ BtShared *pBt; /* The BtShared this cursor points to */
60795
+ BtCursor *pNext; /* Forms a linked list of all cursors */
60796
+ CellInfo info; /* A parse of the cell we are pointing at */
60797
+ i64 nKey; /* Size of pKey, or last integer key */
60798
+ Pgno pgnoRoot; /* The root page of this tree */
5992360799
i8 iPage; /* Index of current page in apPage */
5992460800
u8 curIntKey; /* Value of apPage[0]->intKey */
5992560801
u16 ix; /* Current index for apPage[iPage] */
5992660802
u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
5992760803
struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */
@@ -59967,12 +60843,12 @@
5996760843
** on a different connection that shares the BtShared cache with this
5996860844
** cursor. The error has left the cache in an inconsistent state.
5996960845
** Do nothing else with this cursor. Any attempt to use the cursor
5997060846
** should return the error code stored in BtCursor.skipNext
5997160847
*/
59972
-#define CURSOR_INVALID 0
59973
-#define CURSOR_VALID 1
60848
+#define CURSOR_VALID 0
60849
+#define CURSOR_INVALID 1
5997460850
#define CURSOR_SKIPNEXT 2
5997560851
#define CURSOR_REQUIRESEEK 3
5997660852
#define CURSOR_FAULT 4
5997760853
5997860854
/*
@@ -64746,11 +65622,11 @@
6474665622
** to zero. But it turns out that the apPage[] and aiIdx[] arrays
6474765623
** do not need to be zeroed and they are large, so we can save a lot
6474865624
** of run-time by skipping the initialization of those elements.
6474965625
*/
6475065626
SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
64751
- memset(p, 0, offsetof(BtCursor, iPage));
65627
+ memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
6475265628
}
6475365629
6475465630
/*
6475565631
** Close a cursor. The read lock on the database file is released
6475665632
** when the last cursor is closed.
@@ -64789,15 +65665,23 @@
6478965665
**
6479065666
** BtCursor.info is a cache of the information in the current cell.
6479165667
** Using this cache reduces the number of calls to btreeParseCell().
6479265668
*/
6479365669
#ifndef NDEBUG
65670
+ static int cellInfoEqual(CellInfo *a, CellInfo *b){
65671
+ if( a->nKey!=b->nKey ) return 0;
65672
+ if( a->pPayload!=b->pPayload ) return 0;
65673
+ if( a->nPayload!=b->nPayload ) return 0;
65674
+ if( a->nLocal!=b->nLocal ) return 0;
65675
+ if( a->nSize!=b->nSize ) return 0;
65676
+ return 1;
65677
+ }
6479465678
static void assertCellInfo(BtCursor *pCur){
6479565679
CellInfo info;
6479665680
memset(&info, 0, sizeof(info));
6479765681
btreeParseCell(pCur->pPage, pCur->ix, &info);
64798
- assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
65682
+ assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
6479965683
}
6480065684
#else
6480165685
#define assertCellInfo(x)
6480265686
#endif
6480365687
static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
@@ -65069,18 +65953,19 @@
6506965953
** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
6507065954
** means "not yet known" (the cache is lazily populated).
6507165955
*/
6507265956
if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
6507365957
int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
65074
- if( nOvfl>pCur->nOvflAlloc ){
65958
+ if( pCur->aOverflow==0
65959
+ || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
65960
+ ){
6507565961
Pgno *aNew = (Pgno*)sqlite3Realloc(
6507665962
pCur->aOverflow, nOvfl*2*sizeof(Pgno)
6507765963
);
6507865964
if( aNew==0 ){
6507965965
return SQLITE_NOMEM_BKPT;
6508065966
}else{
65081
- pCur->nOvflAlloc = nOvfl*2;
6508265967
pCur->aOverflow = aNew;
6508365968
}
6508465969
}
6508565970
memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
6508665971
pCur->curFlags |= BTCF_ValidOvfl;
@@ -66590,13 +67475,12 @@
6659067475
*pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
6659167476
}
6659267477
}
6659367478
6659467479
/*
66595
-** Free any overflow pages associated with the given Cell. Write the
66596
-** local Cell size (the number of bytes on the original page, omitting
66597
-** overflow) into *pnSize.
67480
+** Free any overflow pages associated with the given Cell. Store
67481
+** size information about the cell in pInfo.
6659867482
*/
6659967483
static int clearCell(
6660067484
MemPage *pPage, /* The page that contains the Cell */
6660167485
unsigned char *pCell, /* First byte of the Cell */
6660267486
CellInfo *pInfo /* Size information about the cell */
@@ -67796,11 +68680,11 @@
6779668680
rc = SQLITE_CORRUPT_BKPT;
6779768681
goto balance_cleanup;
6779868682
}
6779968683
6780068684
/* Load b.apCell[] with pointers to all cells in pOld. If pOld
67801
- ** constains overflow cells, include them in the b.apCell[] array
68685
+ ** contains overflow cells, include them in the b.apCell[] array
6780268686
** in the correct spot.
6780368687
**
6780468688
** Note that when there are multiple overflow cells, it is always the
6780568689
** case that they are sequential and adjacent. This invariant arises
6780668690
** because multiple overflows can only occurs when inserting divider
@@ -71281,10 +72165,55 @@
7128172165
}
7128272166
return 1;
7128372167
}
7128472168
#endif
7128572169
72170
+#ifdef SQLITE_DEBUG
72171
+/*
72172
+** Check that string value of pMem agrees with its integer or real value.
72173
+**
72174
+** A single int or real value always converts to the same strings. But
72175
+** many different strings can be converted into the same int or real.
72176
+** If a table contains a numeric value and an index is based on the
72177
+** corresponding string value, then it is important that the string be
72178
+** derived from the numeric value, not the other way around, to ensure
72179
+** that the index and table are consistent. See ticket
72180
+** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
72181
+** an example.
72182
+**
72183
+** This routine looks at pMem to verify that if it has both a numeric
72184
+** representation and a string representation then the string rep has
72185
+** been derived from the numeric and not the other way around. It returns
72186
+** true if everything is ok and false if there is a problem.
72187
+**
72188
+** This routine is for use inside of assert() statements only.
72189
+*/
72190
+SQLITE_PRIVATE int sqlite3VdbeMemConsistentDualRep(Mem *p){
72191
+ char zBuf[100];
72192
+ char *z;
72193
+ int i, j, incr;
72194
+ if( (p->flags & MEM_Str)==0 ) return 1;
72195
+ if( (p->flags & (MEM_Int|MEM_Real))==0 ) return 1;
72196
+ if( p->flags & MEM_Int ){
72197
+ sqlite3_snprintf(sizeof(zBuf),zBuf,"%lld",p->u.i);
72198
+ }else{
72199
+ sqlite3_snprintf(sizeof(zBuf),zBuf,"%!.15g",p->u.r);
72200
+ }
72201
+ z = p->z;
72202
+ i = j = 0;
72203
+ incr = 1;
72204
+ if( p->enc!=SQLITE_UTF8 ){
72205
+ incr = 2;
72206
+ if( p->enc==SQLITE_UTF16BE ) z++;
72207
+ }
72208
+ while( zBuf[j] ){
72209
+ if( zBuf[j++]!=z[i] ) return 0;
72210
+ i += incr;
72211
+ }
72212
+ return 1;
72213
+}
72214
+#endif /* SQLITE_DEBUG */
7128672215
7128772216
/*
7128872217
** If pMem is an object with a valid string representation, this routine
7128972218
** ensures the internal encoding for the string representation is
7129072219
** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
@@ -71714,10 +72643,20 @@
7171472643
}else{
7171572644
/* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
7171672645
return (double)0;
7171772646
}
7171872647
}
72648
+
72649
+/*
72650
+** Return 1 if pMem represents true, and return 0 if pMem represents false.
72651
+** Return the value ifNull if pMem is NULL.
72652
+*/
72653
+SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
72654
+ if( pMem->flags & MEM_Int ) return pMem->u.i!=0;
72655
+ if( pMem->flags & MEM_Null ) return ifNull;
72656
+ return sqlite3VdbeRealValue(pMem)!=0.0;
72657
+}
7171972658
7172072659
/*
7172172660
** The MEM structure is already a MEM_Real. Try to also make it a
7172272661
** MEM_Int if we can.
7172372662
*/
@@ -71769,10 +72708,22 @@
7176972708
7177072709
pMem->u.r = sqlite3VdbeRealValue(pMem);
7177172710
MemSetTypeFlag(pMem, MEM_Real);
7177272711
return SQLITE_OK;
7177372712
}
72713
+
72714
+/* Compare a floating point value to an integer. Return true if the two
72715
+** values are the same within the precision of the floating point value.
72716
+**
72717
+** For some versions of GCC on 32-bit machines, if you do the more obvious
72718
+** comparison of "r1==(double)i" you sometimes get an answer of false even
72719
+** though the r1 and (double)i values are bit-for-bit the same.
72720
+*/
72721
+static int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
72722
+ double r2 = (double)i;
72723
+ return memcmp(&r1, &r2, sizeof(r1))==0;
72724
+}
7177472725
7177572726
/*
7177672727
** Convert pMem so that it has types MEM_Real or MEM_Int or both.
7177772728
** Invalidate any prior representations.
7177872729
**
@@ -71789,11 +72740,11 @@
7178972740
if( rc==0 ){
7179072741
MemSetTypeFlag(pMem, MEM_Int);
7179172742
}else{
7179272743
i64 i = pMem->u.i;
7179372744
sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
71794
- if( rc==1 && pMem->u.r==(double)i ){
72745
+ if( rc==1 && sqlite3RealSameAsInt(pMem->u.r, i) ){
7179572746
pMem->u.i = i;
7179672747
MemSetTypeFlag(pMem, MEM_Int);
7179772748
}else{
7179872749
MemSetTypeFlag(pMem, MEM_Real);
7179972750
}
@@ -72272,10 +73223,11 @@
7227273223
assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
7227373224
}
7227473225
assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
7227573226
|| pVal->db->mallocFailed );
7227673227
if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
73228
+ assert( sqlite3VdbeMemConsistentDualRep(pVal) );
7227773229
return pVal->z;
7227873230
}else{
7227973231
return 0;
7228073232
}
7228173233
}
@@ -72294,10 +73246,11 @@
7229473246
if( !pVal ) return 0;
7229573247
assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
7229673248
assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
7229773249
assert( (pVal->flags & MEM_RowSet)==0 );
7229873250
if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
73251
+ assert( sqlite3VdbeMemConsistentDualRep(pVal) );
7229973252
return pVal->z;
7230073253
}
7230173254
if( pVal->flags&MEM_Null ){
7230273255
return 0;
7230373256
}
@@ -78075,18 +79028,16 @@
7807579028
sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
7807679029
}
7807779030
SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
7807879031
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
7807979032
pCtx->isError = SQLITE_ERROR;
78080
- pCtx->fErrorOrAux = 1;
7808179033
sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
7808279034
}
7808379035
#ifndef SQLITE_OMIT_UTF16
7808479036
SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
7808579037
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
7808679038
pCtx->isError = SQLITE_ERROR;
78087
- pCtx->fErrorOrAux = 1;
7808879039
sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
7808979040
}
7809079041
#endif
7809179042
SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
7809279043
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
@@ -78188,12 +79139,11 @@
7818879139
}
7818979140
sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
7819079141
return SQLITE_OK;
7819179142
}
7819279143
SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
78193
- pCtx->isError = errCode;
78194
- pCtx->fErrorOrAux = 1;
79144
+ pCtx->isError = errCode ? errCode : -1;
7819579145
#ifdef SQLITE_DEBUG
7819679146
if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
7819779147
#endif
7819879148
if( pCtx->pOut->flags & MEM_Null ){
7819979149
sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
@@ -78203,21 +79153,19 @@
7820379153
7820479154
/* Force an SQLITE_TOOBIG error. */
7820579155
SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
7820679156
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
7820779157
pCtx->isError = SQLITE_TOOBIG;
78208
- pCtx->fErrorOrAux = 1;
7820979158
sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
7821079159
SQLITE_UTF8, SQLITE_STATIC);
7821179160
}
7821279161
7821379162
/* An SQLITE_NOMEM error. */
7821479163
SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
7821579164
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
7821679165
sqlite3VdbeMemSetNull(pCtx->pOut);
7821779166
pCtx->isError = SQLITE_NOMEM_BKPT;
78218
- pCtx->fErrorOrAux = 1;
7821979167
sqlite3OomFault(pCtx->pOut->db);
7822079168
}
7822179169
7822279170
/*
7822379171
** This function is called after a transaction has been committed. It
@@ -78620,14 +79568,11 @@
7862079568
if( !pAuxData ) goto failed;
7862179569
pAuxData->iAuxOp = pCtx->iOp;
7862279570
pAuxData->iAuxArg = iArg;
7862379571
pAuxData->pNextAux = pVdbe->pAuxData;
7862479572
pVdbe->pAuxData = pAuxData;
78625
- if( pCtx->fErrorOrAux==0 ){
78626
- pCtx->isError = 0;
78627
- pCtx->fErrorOrAux = 1;
78628
- }
79573
+ if( pCtx->isError==0 ) pCtx->isError = -1;
7862979574
}else if( pAuxData->xDeleteAux ){
7863079575
pAuxData->xDeleteAux(pAuxData->pAux);
7863179576
}
7863279577
7863379578
pAuxData->pAux = pAux;
@@ -79379,11 +80324,13 @@
7937980324
*/
7938080325
SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
7938180326
Vdbe *pVdbe = (Vdbe*)pStmt;
7938280327
u32 v;
7938380328
#ifdef SQLITE_ENABLE_API_ARMOR
79384
- if( !pStmt ){
80329
+ if( !pStmt
80330
+ || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
80331
+ ){
7938580332
(void)SQLITE_MISUSE_BKPT;
7938680333
return 0;
7938780334
}
7938880335
#endif
7938980336
if( op==SQLITE_STMTSTATUS_MEMUSED ){
@@ -80153,10 +81100,15 @@
8015381100
}else{
8015481101
pRec->u.r = rValue;
8015581102
pRec->flags |= MEM_Real;
8015681103
if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
8015781104
}
81105
+ /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
81106
+ ** string representation after computing a numeric equivalent, because the
81107
+ ** string representation might not be the canonical representation for the
81108
+ ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
81109
+ pRec->flags &= ~MEM_Str;
8015881110
}
8015981111
8016081112
/*
8016181113
** Processing is determine by the affinity parameter:
8016281114
**
@@ -80621,11 +81573,11 @@
8062181573
** jumps to abort_due_to_error. */
8062281574
assert( rc==SQLITE_OK );
8062381575
8062481576
assert( pOp>=aOp && pOp<&aOp[p->nOp]);
8062581577
#ifdef VDBE_PROFILE
80626
- start = sqlite3Hwtime();
81578
+ start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
8062781579
#endif
8062881580
nVmStep++;
8062981581
#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
8063081582
if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
8063181583
#endif
@@ -82145,22 +83097,12 @@
8214583097
case OP_And: /* same as TK_AND, in1, in2, out3 */
8214683098
case OP_Or: { /* same as TK_OR, in1, in2, out3 */
8214783099
int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
8214883100
int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
8214983101
82150
- pIn1 = &aMem[pOp->p1];
82151
- if( pIn1->flags & MEM_Null ){
82152
- v1 = 2;
82153
- }else{
82154
- v1 = sqlite3VdbeIntValue(pIn1)!=0;
82155
- }
82156
- pIn2 = &aMem[pOp->p2];
82157
- if( pIn2->flags & MEM_Null ){
82158
- v2 = 2;
82159
- }else{
82160
- v2 = sqlite3VdbeIntValue(pIn2)!=0;
82161
- }
83102
+ v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
83103
+ v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
8216283104
if( pOp->opcode==OP_And ){
8216383105
static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
8216483106
v1 = and_logic[v1*3+v2];
8216583107
}else{
8216683108
static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
@@ -82173,10 +83115,39 @@
8217383115
pOut->u.i = v1;
8217483116
MemSetTypeFlag(pOut, MEM_Int);
8217583117
}
8217683118
break;
8217783119
}
83120
+
83121
+/* Opcode: IsTrue P1 P2 P3 P4 *
83122
+** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
83123
+**
83124
+** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
83125
+** IS NOT FALSE operators.
83126
+**
83127
+** Interpret the value in register P1 as a boolean value. Store that
83128
+** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
83129
+** NULL, then the P3 is stored in register P2. Invert the answer if P4
83130
+** is 1.
83131
+**
83132
+** The logic is summarized like this:
83133
+**
83134
+** <ul>
83135
+** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
83136
+** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
83137
+** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
83138
+** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
83139
+** </ul>
83140
+*/
83141
+case OP_IsTrue: { /* in1, out2 */
83142
+ assert( pOp->p4type==P4_INT32 );
83143
+ assert( pOp->p4.i==0 || pOp->p4.i==1 );
83144
+ assert( pOp->p3==0 || pOp->p3==1 );
83145
+ sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
83146
+ sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
83147
+ break;
83148
+}
8217883149
8217983150
/* Opcode: Not P1 P2 * * *
8218083151
** Synopsis: r[P2]= !r[P1]
8218183152
**
8218283153
** Interpret the value in register P1 as a boolean value. Store the
@@ -82184,14 +83155,14 @@
8218483155
** NULL, then a NULL is stored in P2.
8218583156
*/
8218683157
case OP_Not: { /* same as TK_NOT, in1, out2 */
8218783158
pIn1 = &aMem[pOp->p1];
8218883159
pOut = &aMem[pOp->p2];
82189
- sqlite3VdbeMemSetNull(pOut);
8219083160
if( (pIn1->flags & MEM_Null)==0 ){
82191
- pOut->flags = MEM_Int;
82192
- pOut->u.i = !sqlite3VdbeIntValue(pIn1);
83161
+ sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
83162
+ }else{
83163
+ sqlite3VdbeMemSetNull(pOut);
8219383164
}
8219483165
break;
8219583166
}
8219683167
8219783168
/* Opcode: BitNot P1 P2 * * *
@@ -82254,34 +83225,29 @@
8225483225
**
8225583226
** Jump to P2 if the value in register P1 is true. The value
8225683227
** is considered true if it is numeric and non-zero. If the value
8225783228
** in P1 is NULL then take the jump if and only if P3 is non-zero.
8225883229
*/
83230
+case OP_If: { /* jump, in1 */
83231
+ int c;
83232
+ c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
83233
+ VdbeBranchTaken(c!=0, 2);
83234
+ if( c ) goto jump_to_p2;
83235
+ break;
83236
+}
83237
+
8225983238
/* Opcode: IfNot P1 P2 P3 * *
8226083239
**
8226183240
** Jump to P2 if the value in register P1 is False. The value
8226283241
** is considered false if it has a numeric value of zero. If the value
8226383242
** in P1 is NULL then take the jump if and only if P3 is non-zero.
8226483243
*/
82265
-case OP_If: /* jump, in1 */
8226683244
case OP_IfNot: { /* jump, in1 */
8226783245
int c;
82268
- pIn1 = &aMem[pOp->p1];
82269
- if( pIn1->flags & MEM_Null ){
82270
- c = pOp->p3;
82271
- }else{
82272
-#ifdef SQLITE_OMIT_FLOATING_POINT
82273
- c = sqlite3VdbeIntValue(pIn1)!=0;
82274
-#else
82275
- c = sqlite3VdbeRealValue(pIn1)!=0.0;
82276
-#endif
82277
- if( pOp->opcode==OP_IfNot ) c = !c;
82278
- }
83246
+ c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
8227983247
VdbeBranchTaken(c!=0, 2);
82280
- if( c ){
82281
- goto jump_to_p2;
82282
- }
83248
+ if( c ) goto jump_to_p2;
8228383249
break;
8228483250
}
8228583251
8228683252
/* Opcode: IsNull P1 P2 * * *
8228783253
** Synopsis: if r[P1]==NULL goto P2
@@ -82338,11 +83304,11 @@
8233883304
** pointing.
8233983305
**
8234083306
** P2 is the column number for the argument to the sqlite_offset() function.
8234183307
** This opcode does not use P2 itself, but the P2 value is used by the
8234283308
** code generator. The P1, P2, and P3 operands to this opcode are the
82343
-** as as for OP_Column.
83309
+** same as for OP_Column.
8234483310
**
8234583311
** This opcode is only available if SQLite is compiled with the
8234683312
** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
8234783313
*/
8234883314
case OP_Offset: { /* out3 */
@@ -84246,10 +85212,14 @@
8424685212
v = 0;
8424785213
res = 0;
8424885214
pOut = out2Prerelease(p, pOp);
8424985215
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
8425085216
pC = p->apCsr[pOp->p1];
85217
+ if( !pC->isTable ){
85218
+ rc = SQLITE_CORRUPT_BKPT;
85219
+ goto abort_due_to_error;
85220
+ }
8425185221
assert( pC!=0 );
8425285222
assert( pC->eCurType==CURTYPE_BTREE );
8425385223
assert( pC->uc.pCursor!=0 );
8425485224
{
8425585225
/* The next rowid or record number (different terms for the same
@@ -86182,16 +87152,21 @@
8618287152
assert( pOp->p4type==P4_FUNCDEF );
8618387153
n = pOp->p5;
8618487154
assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
8618587155
assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
8618687156
assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
86187
- pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
87157
+ pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
87158
+ (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
8618887159
if( pCtx==0 ) goto no_mem;
8618987160
pCtx->pMem = 0;
87161
+ pCtx->pOut = (Mem*)&(pCtx->argv[n]);
87162
+ sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
8619087163
pCtx->pFunc = pOp->p4.pFunc;
8619187164
pCtx->iOp = (int)(pOp - aOp);
8619287165
pCtx->pVdbe = p;
87166
+ pCtx->skipFlag = 0;
87167
+ pCtx->isError = 0;
8619387168
pCtx->argc = n;
8619487169
pOp->p4type = P4_FUNCCTX;
8619587170
pOp->p4.pCtx = pCtx;
8619687171
pOp->opcode = OP_AggStep;
8619787172
/* Fall through into OP_AggStep */
@@ -86198,11 +87173,10 @@
8619887173
}
8619987174
case OP_AggStep: {
8620087175
int i;
8620187176
sqlite3_context *pCtx;
8620287177
Mem *pMem;
86203
- Mem t;
8620487178
8620587179
assert( pOp->p4type==P4_FUNCCTX );
8620687180
pCtx = pOp->p4.pCtx;
8620787181
pMem = &aMem[pOp->p3];
8620887182
@@ -86221,30 +87195,32 @@
8622187195
REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
8622287196
}
8622387197
#endif
8622487198
8622587199
pMem->n++;
86226
- sqlite3VdbeMemInit(&t, db, MEM_Null);
86227
- pCtx->pOut = &t;
86228
- pCtx->fErrorOrAux = 0;
86229
- pCtx->skipFlag = 0;
87200
+ assert( pCtx->pOut->flags==MEM_Null );
87201
+ assert( pCtx->isError==0 );
87202
+ assert( pCtx->skipFlag==0 );
8623087203
(pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
86231
- if( pCtx->fErrorOrAux ){
86232
- if( pCtx->isError ){
86233
- sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
87204
+ if( pCtx->isError ){
87205
+ if( pCtx->isError>0 ){
87206
+ sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
8623487207
rc = pCtx->isError;
8623587208
}
86236
- sqlite3VdbeMemRelease(&t);
87209
+ if( pCtx->skipFlag ){
87210
+ assert( pOp[-1].opcode==OP_CollSeq );
87211
+ i = pOp[-1].p1;
87212
+ if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
87213
+ pCtx->skipFlag = 0;
87214
+ }
87215
+ sqlite3VdbeMemRelease(pCtx->pOut);
87216
+ pCtx->pOut->flags = MEM_Null;
87217
+ pCtx->isError = 0;
8623787218
if( rc ) goto abort_due_to_error;
86238
- }else{
86239
- assert( t.flags==MEM_Null );
86240
- }
86241
- if( pCtx->skipFlag ){
86242
- assert( pOp[-1].opcode==OP_CollSeq );
86243
- i = pOp[-1].p1;
86244
- if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
86245
- }
87219
+ }
87220
+ assert( pCtx->pOut->flags==MEM_Null );
87221
+ assert( pCtx->skipFlag==0 );
8624687222
break;
8624787223
}
8624887224
8624987225
/* Opcode: AggFinal P1 P2 * P4 *
8625087226
** Synopsis: accum=r[P1] N=P2
@@ -86727,11 +87703,12 @@
8672787703
}else{
8672887704
MemSetTypeFlag(pDest, MEM_Null);
8672987705
}
8673087706
rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
8673187707
sqlite3VtabImportErrmsg(p, pVtab);
86732
- if( sContext.isError ){
87708
+ if( sContext.isError>0 ){
87709
+ sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
8673387710
rc = sContext.isError;
8673487711
}
8673587712
sqlite3VdbeChangeEncoding(pDest, encoding);
8673687713
REGISTER_TRACE(pOp->p3, pDest);
8673787714
UPDATE_MAX_BLOBSIZE(pDest);
@@ -86992,10 +87969,11 @@
8699287969
if( pCtx==0 ) goto no_mem;
8699387970
pCtx->pOut = 0;
8699487971
pCtx->pFunc = pOp->p4.pFunc;
8699587972
pCtx->iOp = (int)(pOp - aOp);
8699687973
pCtx->pVdbe = p;
87974
+ pCtx->isError = 0;
8699787975
pCtx->argc = n;
8699887976
pOp->p4type = P4_FUNCCTX;
8699987977
pOp->p4.pCtx = pCtx;
8700087978
assert( OP_PureFunc == OP_PureFunc0+2 );
8700187979
assert( OP_Function == OP_Function0+2 );
@@ -87026,20 +88004,21 @@
8702688004
assert( memIsValid(pCtx->argv[i]) );
8702788005
REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
8702888006
}
8702988007
#endif
8703088008
MemSetTypeFlag(pOut, MEM_Null);
87031
- pCtx->fErrorOrAux = 0;
88009
+ assert( pCtx->isError==0 );
8703288010
(*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
8703388011
8703488012
/* If the function returned an error, throw an exception */
87035
- if( pCtx->fErrorOrAux ){
87036
- if( pCtx->isError ){
88013
+ if( pCtx->isError ){
88014
+ if( pCtx->isError>0 ){
8703788015
sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
8703888016
rc = pCtx->isError;
8703988017
}
8704088018
sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
88019
+ pCtx->isError = 0;
8704188020
if( rc ) goto abort_due_to_error;
8704288021
}
8704388022
8704488023
/* Copy the result of the function into register P3 */
8704588024
if( pOut->flags & (MEM_Str|MEM_Blob) ){
@@ -87077,12 +88056,14 @@
8707788056
** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
8707888057
** error is encountered.
8707988058
*/
8708088059
case OP_Trace:
8708188060
case OP_Init: { /* jump */
88061
+ int i;
88062
+#ifndef SQLITE_OMIT_TRACE
8708288063
char *zTrace;
87083
- int i;
88064
+#endif
8708488065
8708588066
/* If the P4 argument is not NULL, then it must be an SQL comment string.
8708688067
** The "--" string is broken up to prevent false-positives with srcck1.c.
8708788068
**
8708888069
** This assert() provides evidence for:
@@ -87195,11 +88176,11 @@
8719588176
*****************************************************************************/
8719688177
}
8719788178
8719888179
#ifdef VDBE_PROFILE
8719988180
{
87200
- u64 endTime = sqlite3Hwtime();
88181
+ u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
8720188182
if( endTime>start ) pOrigOp->cycles += endTime - start;
8720288183
pOrigOp->cnt++;
8720388184
}
8720488185
#endif
8720588186
@@ -91582,14 +92563,20 @@
9158292563
** pExpr.
9158392564
**
9158492565
** Because no reference was made to outer contexts, the pNC->nRef
9158592566
** fields are not changed in any context.
9158692567
*/
91587
- if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
91588
- pExpr->op = TK_STRING;
91589
- pExpr->pTab = 0;
91590
- return WRC_Prune;
92568
+ if( cnt==0 && zTab==0 ){
92569
+ assert( pExpr->op==TK_ID );
92570
+ if( ExprHasProperty(pExpr,EP_DblQuoted) ){
92571
+ pExpr->op = TK_STRING;
92572
+ pExpr->pTab = 0;
92573
+ return WRC_Prune;
92574
+ }
92575
+ if( sqlite3ExprIdToTrueFalse(pExpr) ){
92576
+ return WRC_Prune;
92577
+ }
9159192578
}
9159292579
9159392580
/*
9159492581
** cnt==0 means there was not match. cnt>1 means there were two or
9159592582
** more matches. Either way, we have an error.
@@ -91934,19 +92921,34 @@
9193492921
}
9193592922
case TK_VARIABLE: {
9193692923
notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
9193792924
break;
9193892925
}
92926
+ case TK_IS:
92927
+ case TK_ISNOT: {
92928
+ Expr *pRight;
92929
+ assert( !ExprHasProperty(pExpr, EP_Reduced) );
92930
+ /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
92931
+ ** and "x IS NOT FALSE". */
92932
+ if( (pRight = pExpr->pRight)->op==TK_ID ){
92933
+ int rc = resolveExprStep(pWalker, pRight);
92934
+ if( rc==WRC_Abort ) return WRC_Abort;
92935
+ if( pRight->op==TK_TRUEFALSE ){
92936
+ pExpr->op2 = pExpr->op;
92937
+ pExpr->op = TK_TRUTH;
92938
+ return WRC_Continue;
92939
+ }
92940
+ }
92941
+ /* Fall thru */
92942
+ }
9193992943
case TK_BETWEEN:
9194092944
case TK_EQ:
9194192945
case TK_NE:
9194292946
case TK_LT:
9194392947
case TK_LE:
9194492948
case TK_GT:
91945
- case TK_GE:
91946
- case TK_IS:
91947
- case TK_ISNOT: {
92949
+ case TK_GE: {
9194892950
int nLeft, nRight;
9194992951
if( pParse->db->mallocFailed ) break;
9195092952
assert( pExpr->pLeft!=0 );
9195192953
nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
9195292954
if( pExpr->op==TK_BETWEEN ){
@@ -94416,10 +95418,38 @@
9441695418
SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
9441795419
UNUSED_PARAMETER(NotUsed);
9441895420
pWalker->eCode = 0;
9441995421
return WRC_Abort;
9442095422
}
95423
+
95424
+/*
95425
+** If the input expression is an ID with the name "true" or "false"
95426
+** then convert it into an TK_TRUEFALSE term. Return non-zero if
95427
+** the conversion happened, and zero if the expression is unaltered.
95428
+*/
95429
+SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr *pExpr){
95430
+ assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
95431
+ if( sqlite3StrICmp(pExpr->u.zToken, "true")==0
95432
+ || sqlite3StrICmp(pExpr->u.zToken, "false")==0
95433
+ ){
95434
+ pExpr->op = TK_TRUEFALSE;
95435
+ return 1;
95436
+ }
95437
+ return 0;
95438
+}
95439
+
95440
+/*
95441
+** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE
95442
+** and 0 if it is FALSE.
95443
+*/
95444
+SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr *pExpr){
95445
+ assert( pExpr->op==TK_TRUEFALSE );
95446
+ assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
95447
+ || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
95448
+ return pExpr->u.zToken[4]==0;
95449
+}
95450
+
9442195451
9442295452
/*
9442395453
** These routines are Walker callbacks used to check expressions to
9442495454
** see if they are "constant" for some definition of constant. The
9442595455
** Walker.eCode value determines the type of "constant" we are looking
@@ -94464,10 +95494,16 @@
9446495494
}else{
9446595495
pWalker->eCode = 0;
9446695496
return WRC_Abort;
9446795497
}
9446895498
case TK_ID:
95499
+ /* Convert "true" or "false" in a DEFAULT clause into the
95500
+ ** appropriate TK_TRUEFALSE operator */
95501
+ if( sqlite3ExprIdToTrueFalse(pExpr) ){
95502
+ return WRC_Prune;
95503
+ }
95504
+ /* Fall thru */
9446995505
case TK_COLUMN:
9447095506
case TK_AGG_FUNCTION:
9447195507
case TK_AGG_COLUMN:
9447295508
testcase( pExpr->op==TK_ID );
9447395509
testcase( pExpr->op==TK_COLUMN );
@@ -96227,10 +97263,14 @@
9622797263
pExpr->op2);
9622897264
}
9622997265
case TK_INTEGER: {
9623097266
codeInteger(pParse, pExpr, 0, target);
9623197267
return target;
97268
+ }
97269
+ case TK_TRUEFALSE: {
97270
+ sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target);
97271
+ return target;
9623297272
}
9623397273
#ifndef SQLITE_OMIT_FLOATING_POINT
9623497274
case TK_FLOAT: {
9623597275
assert( !ExprHasProperty(pExpr, EP_IntValue) );
9623697276
codeReal(v, pExpr->u.zToken, 0, target);
@@ -96382,10 +97422,22 @@
9638297422
assert( TK_NOT==OP_Not ); testcase( op==TK_NOT );
9638397423
r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
9638497424
testcase( regFree1==0 );
9638597425
sqlite3VdbeAddOp2(v, op, r1, inReg);
9638697426
break;
97427
+ }
97428
+ case TK_TRUTH: {
97429
+ int isTrue; /* IS TRUE or IS NOT TRUE */
97430
+ int bNormal; /* IS TRUE or IS FALSE */
97431
+ r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
97432
+ testcase( regFree1==0 );
97433
+ isTrue = sqlite3ExprTruthValue(pExpr->pRight);
97434
+ bNormal = pExpr->op2==TK_IS;
97435
+ testcase( isTrue && bNormal);
97436
+ testcase( !isTrue && bNormal);
97437
+ sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal);
97438
+ break;
9638797439
}
9638897440
case TK_ISNULL:
9638997441
case TK_NOTNULL: {
9639097442
int addr;
9639197443
assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
@@ -97157,10 +98209,27 @@
9715798209
}
9715898210
case TK_NOT: {
9715998211
testcase( jumpIfNull==0 );
9716098212
sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
9716198213
break;
98214
+ }
98215
+ case TK_TRUTH: {
98216
+ int isNot; /* IS NOT TRUE or IS NOT FALSE */
98217
+ int isTrue; /* IS TRUE or IS NOT TRUE */
98218
+ testcase( jumpIfNull==0 );
98219
+ isNot = pExpr->op2==TK_ISNOT;
98220
+ isTrue = sqlite3ExprTruthValue(pExpr->pRight);
98221
+ testcase( isTrue && isNot );
98222
+ testcase( !isTrue && isNot );
98223
+ if( isTrue ^ isNot ){
98224
+ sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
98225
+ isNot ? SQLITE_JUMPIFNULL : 0);
98226
+ }else{
98227
+ sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
98228
+ isNot ? SQLITE_JUMPIFNULL : 0);
98229
+ }
98230
+ break;
9716298231
}
9716398232
case TK_IS:
9716498233
case TK_ISNOT:
9716598234
testcase( op==TK_IS );
9716698235
testcase( op==TK_ISNOT );
@@ -97311,10 +98380,30 @@
9731198380
}
9731298381
case TK_NOT: {
9731398382
testcase( jumpIfNull==0 );
9731498383
sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
9731598384
break;
98385
+ }
98386
+ case TK_TRUTH: {
98387
+ int isNot; /* IS NOT TRUE or IS NOT FALSE */
98388
+ int isTrue; /* IS TRUE or IS NOT TRUE */
98389
+ testcase( jumpIfNull==0 );
98390
+ isNot = pExpr->op2==TK_ISNOT;
98391
+ isTrue = sqlite3ExprTruthValue(pExpr->pRight);
98392
+ testcase( isTrue && isNot );
98393
+ testcase( !isTrue && isNot );
98394
+ if( isTrue ^ isNot ){
98395
+ /* IS TRUE and IS NOT FALSE */
98396
+ sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
98397
+ isNot ? 0 : SQLITE_JUMPIFNULL);
98398
+
98399
+ }else{
98400
+ /* IS FALSE and IS NOT TRUE */
98401
+ sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
98402
+ isNot ? 0 : SQLITE_JUMPIFNULL);
98403
+ }
98404
+ break;
9731698405
}
9731798406
case TK_IS:
9731898407
case TK_ISNOT:
9731998408
testcase( pExpr->op==TK_IS );
9732098409
testcase( pExpr->op==TK_ISNOT );
@@ -100822,10 +101911,14 @@
100822101911
**
100823101912
** SELECT sqlite_attach(x, y, z)
100824101913
**
100825101914
** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
100826101915
** third argument.
101916
+**
101917
+** If the db->init.reopenMemdb flags is set, then instead of attaching a
101918
+** new database, close the database on db->init.iDb and reopen it as an
101919
+** empty MemDB.
100827101920
*/
100828101921
static void attachFunc(
100829101922
sqlite3_context *context,
100830101923
int NotUsed,
100831101924
sqlite3_value **argv
@@ -100842,69 +101935,89 @@
100842101935
Db *pNew; /* Db object for the newly attached database */
100843101936
char *zErrDyn = 0;
100844101937
sqlite3_vfs *pVfs;
100845101938
100846101939
UNUSED_PARAMETER(NotUsed);
100847
-
100848101940
zFile = (const char *)sqlite3_value_text(argv[0]);
100849101941
zName = (const char *)sqlite3_value_text(argv[1]);
100850101942
if( zFile==0 ) zFile = "";
100851101943
if( zName==0 ) zName = "";
100852101944
100853
- /* Check for the following errors:
100854
- **
100855
- ** * Too many attached databases,
100856
- ** * Transaction currently open
100857
- ** * Specified database name already being used.
100858
- */
100859
- if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
100860
- zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
100861
- db->aLimit[SQLITE_LIMIT_ATTACHED]
100862
- );
100863
- goto attach_error;
100864
- }
100865
- for(i=0; i<db->nDb; i++){
100866
- char *z = db->aDb[i].zDbSName;
100867
- assert( z && zName );
100868
- if( sqlite3StrICmp(z, zName)==0 ){
100869
- zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
100870
- goto attach_error;
100871
- }
100872
- }
100873
-
100874
- /* Allocate the new entry in the db->aDb[] array and initialize the schema
100875
- ** hash tables.
100876
- */
100877
- if( db->aDb==db->aDbStatic ){
100878
- aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
100879
- if( aNew==0 ) return;
100880
- memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
100881
- }else{
100882
- aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
100883
- if( aNew==0 ) return;
100884
- }
100885
- db->aDb = aNew;
100886
- pNew = &db->aDb[db->nDb];
100887
- memset(pNew, 0, sizeof(*pNew));
100888
-
100889
- /* Open the database file. If the btree is successfully opened, use
100890
- ** it to obtain the database schema. At this point the schema may
100891
- ** or may not be initialized.
100892
- */
100893
- flags = db->openFlags;
100894
- rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
100895
- if( rc!=SQLITE_OK ){
100896
- if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
100897
- sqlite3_result_error(context, zErr, -1);
100898
- sqlite3_free(zErr);
100899
- return;
100900
- }
100901
- assert( pVfs );
100902
- flags |= SQLITE_OPEN_MAIN_DB;
100903
- rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
100904
- sqlite3_free( zPath );
100905
- db->nDb++;
101945
+#ifdef SQLITE_ENABLE_DESERIALIZE
101946
+# define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
101947
+#else
101948
+# define REOPEN_AS_MEMDB(db) (0)
101949
+#endif
101950
+
101951
+ if( REOPEN_AS_MEMDB(db) ){
101952
+ /* This is not a real ATTACH. Instead, this routine is being called
101953
+ ** from sqlite3_deserialize() to close database db->init.iDb and
101954
+ ** reopen it as a MemDB */
101955
+ pVfs = sqlite3_vfs_find("memdb");
101956
+ if( pVfs==0 ) return;
101957
+ pNew = &db->aDb[db->init.iDb];
101958
+ if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
101959
+ pNew->pBt = 0;
101960
+ pNew->pSchema = 0;
101961
+ rc = sqlite3BtreeOpen(pVfs, "x", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
101962
+ }else{
101963
+ /* This is a real ATTACH
101964
+ **
101965
+ ** Check for the following errors:
101966
+ **
101967
+ ** * Too many attached databases,
101968
+ ** * Transaction currently open
101969
+ ** * Specified database name already being used.
101970
+ */
101971
+ if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
101972
+ zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
101973
+ db->aLimit[SQLITE_LIMIT_ATTACHED]
101974
+ );
101975
+ goto attach_error;
101976
+ }
101977
+ for(i=0; i<db->nDb; i++){
101978
+ char *z = db->aDb[i].zDbSName;
101979
+ assert( z && zName );
101980
+ if( sqlite3StrICmp(z, zName)==0 ){
101981
+ zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
101982
+ goto attach_error;
101983
+ }
101984
+ }
101985
+
101986
+ /* Allocate the new entry in the db->aDb[] array and initialize the schema
101987
+ ** hash tables.
101988
+ */
101989
+ if( db->aDb==db->aDbStatic ){
101990
+ aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
101991
+ if( aNew==0 ) return;
101992
+ memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
101993
+ }else{
101994
+ aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
101995
+ if( aNew==0 ) return;
101996
+ }
101997
+ db->aDb = aNew;
101998
+ pNew = &db->aDb[db->nDb];
101999
+ memset(pNew, 0, sizeof(*pNew));
102000
+
102001
+ /* Open the database file. If the btree is successfully opened, use
102002
+ ** it to obtain the database schema. At this point the schema may
102003
+ ** or may not be initialized.
102004
+ */
102005
+ flags = db->openFlags;
102006
+ rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
102007
+ if( rc!=SQLITE_OK ){
102008
+ if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
102009
+ sqlite3_result_error(context, zErr, -1);
102010
+ sqlite3_free(zErr);
102011
+ return;
102012
+ }
102013
+ assert( pVfs );
102014
+ flags |= SQLITE_OPEN_MAIN_DB;
102015
+ rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
102016
+ sqlite3_free( zPath );
102017
+ db->nDb++;
102018
+ }
100906102019
db->skipBtreeMutex = 0;
100907102020
if( rc==SQLITE_CONSTRAINT ){
100908102021
rc = SQLITE_ERROR;
100909102022
zErrDyn = sqlite3MPrintf(db, "database is already attached");
100910102023
}else if( rc==SQLITE_OK ){
@@ -100927,11 +102040,11 @@
100927102040
PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
100928102041
#endif
100929102042
sqlite3BtreeLeave(pNew->pBt);
100930102043
}
100931102044
pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
100932
- pNew->zDbSName = sqlite3DbStrDup(db, zName);
102045
+ if( !REOPEN_AS_MEMDB(db) ) pNew->zDbSName = sqlite3DbStrDup(db, zName);
100933102046
if( rc==SQLITE_OK && pNew->zDbSName==0 ){
100934102047
rc = SQLITE_NOMEM_BKPT;
100935102048
}
100936102049
100937102050
@@ -100967,17 +102080,19 @@
100967102080
}
100968102081
#endif
100969102082
100970102083
/* If the file was opened successfully, read the schema for the new database.
100971102084
** If this fails, or if opening the file failed, then close the file and
100972
- ** remove the entry from the db->aDb[] array. i.e. put everything back the way
100973
- ** we found it.
102085
+ ** remove the entry from the db->aDb[] array. i.e. put everything back the
102086
+ ** way we found it.
100974102087
*/
100975102088
if( rc==SQLITE_OK ){
100976102089
sqlite3BtreeEnterAll(db);
102090
+ db->init.iDb = 0;
100977102091
rc = sqlite3Init(db, &zErrDyn);
100978102092
sqlite3BtreeLeaveAll(db);
102093
+ assert( zErrDyn==0 || rc!=SQLITE_OK );
100979102094
}
100980102095
#ifdef SQLITE_USER_AUTHENTICATION
100981102096
if( rc==SQLITE_OK ){
100982102097
u8 newAuth = 0;
100983102098
rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
@@ -100985,25 +102100,27 @@
100985102100
rc = SQLITE_AUTH_USER;
100986102101
}
100987102102
}
100988102103
#endif
100989102104
if( rc ){
100990
- int iDb = db->nDb - 1;
100991
- assert( iDb>=2 );
100992
- if( db->aDb[iDb].pBt ){
100993
- sqlite3BtreeClose(db->aDb[iDb].pBt);
100994
- db->aDb[iDb].pBt = 0;
100995
- db->aDb[iDb].pSchema = 0;
100996
- }
100997
- sqlite3ResetAllSchemasOfConnection(db);
100998
- db->nDb = iDb;
100999
- if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
101000
- sqlite3OomFault(db);
101001
- sqlite3DbFree(db, zErrDyn);
101002
- zErrDyn = sqlite3MPrintf(db, "out of memory");
101003
- }else if( zErrDyn==0 ){
101004
- zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
102105
+ if( !REOPEN_AS_MEMDB(db) ){
102106
+ int iDb = db->nDb - 1;
102107
+ assert( iDb>=2 );
102108
+ if( db->aDb[iDb].pBt ){
102109
+ sqlite3BtreeClose(db->aDb[iDb].pBt);
102110
+ db->aDb[iDb].pBt = 0;
102111
+ db->aDb[iDb].pSchema = 0;
102112
+ }
102113
+ sqlite3ResetAllSchemasOfConnection(db);
102114
+ db->nDb = iDb;
102115
+ if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
102116
+ sqlite3OomFault(db);
102117
+ sqlite3DbFree(db, zErrDyn);
102118
+ zErrDyn = sqlite3MPrintf(db, "out of memory");
102119
+ }else if( zErrDyn==0 ){
102120
+ zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
102121
+ }
101005102122
}
101006102123
goto attach_error;
101007102124
}
101008102125
101009102126
return;
@@ -101270,10 +102387,18 @@
101270102387
if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
101271102388
return 1;
101272102389
}
101273102390
if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
101274102391
return 1;
102392
+ }
102393
+ if( pSelect->pWith ){
102394
+ int i;
102395
+ for(i=0; i<pSelect->pWith->nCte; i++){
102396
+ if( sqlite3FixSelect(pFix, pSelect->pWith->a[i].pSelect) ){
102397
+ return 1;
102398
+ }
102399
+ }
101275102400
}
101276102401
pSelect = pSelect->pPrior;
101277102402
}
101278102403
return 0;
101279102404
}
@@ -102734,14 +103859,28 @@
102734103859
** been seen on a column. This routine sets the notNull flag on
102735103860
** the column currently under construction.
102736103861
*/
102737103862
SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
102738103863
Table *p;
103864
+ Column *pCol;
102739103865
p = pParse->pNewTable;
102740103866
if( p==0 || NEVER(p->nCol<1) ) return;
102741
- p->aCol[p->nCol-1].notNull = (u8)onError;
103867
+ pCol = &p->aCol[p->nCol-1];
103868
+ pCol->notNull = (u8)onError;
102742103869
p->tabFlags |= TF_HasNotNull;
103870
+
103871
+ /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
103872
+ ** on this column. */
103873
+ if( pCol->colFlags & COLFLAG_UNIQUE ){
103874
+ Index *pIdx;
103875
+ for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
103876
+ assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
103877
+ if( pIdx->aiColumn[0]==p->nCol-1 ){
103878
+ pIdx->uniqNotNull = 1;
103879
+ }
103880
+ }
103881
+ }
102743103882
}
102744103883
102745103884
/*
102746103885
** Scan the column type name zType (length nType) and return the
102747103886
** associated affinity type.
@@ -103472,12 +104611,10 @@
103472104611
}
103473104612
assert( !db->mallocFailed );
103474104613
p = pParse->pNewTable;
103475104614
if( p==0 ) return;
103476104615
103477
- assert( !db->init.busy || !pSelect );
103478
-
103479104616
/* If the db->init.busy is 1 it means we are reading the SQL off the
103480104617
** "sqlite_master" or "sqlite_temp_master" table on the disk.
103481104618
** So do not write to the disk again. Extract the root page number
103482104619
** for the table from the db->init.newTnum field. (The page number
103483104620
** should have been put there by the sqliteOpenCb routine.)
@@ -103484,10 +104621,14 @@
103484104621
**
103485104622
** If the root page number is 1, that means this is the sqlite_master
103486104623
** table itself. So mark it read-only.
103487104624
*/
103488104625
if( db->init.busy ){
104626
+ if( pSelect ){
104627
+ sqlite3ErrorMsg(pParse, "");
104628
+ return;
104629
+ }
103489104630
p->tnum = db->init.newTnum;
103490104631
if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
103491104632
}
103492104633
103493104634
/* Special processing for WITHOUT ROWID Tables */
@@ -104701,11 +105842,13 @@
104701105842
** key out of the last column added to the table under construction.
104702105843
** So create a fake list to simulate this.
104703105844
*/
104704105845
if( pList==0 ){
104705105846
Token prevCol;
104706
- sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName);
105847
+ Column *pCol = &pTab->aCol[pTab->nCol-1];
105848
+ pCol->colFlags |= COLFLAG_UNIQUE;
105849
+ sqlite3TokenInit(&prevCol, pCol->zName);
104707105850
pList = sqlite3ExprListAppend(pParse, 0,
104708105851
sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
104709105852
if( pList==0 ) goto exit_create_index;
104710105853
assert( pList->nExpr==1 );
104711105854
sqlite3ExprListSetSortOrder(pList, sortOrder);
@@ -107546,10 +108689,12 @@
107546108689
/*
107547108690
** Indicate that the accumulator load should be skipped on this
107548108691
** iteration of the aggregate loop.
107549108692
*/
107550108693
static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
108694
+ assert( context->isError<=0 );
108695
+ context->isError = -1;
107551108696
context->skipFlag = 1;
107552108697
}
107553108698
107554108699
/*
107555108700
** Implementation of the non-aggregate min() and max() functions
@@ -107612,12 +108757,10 @@
107612108757
static void lengthFunc(
107613108758
sqlite3_context *context,
107614108759
int argc,
107615108760
sqlite3_value **argv
107616108761
){
107617
- int len;
107618
-
107619108762
assert( argc==1 );
107620108763
UNUSED_PARAMETER(argc);
107621108764
switch( sqlite3_value_type(argv[0]) ){
107622108765
case SQLITE_BLOB:
107623108766
case SQLITE_INTEGER:
@@ -107625,17 +108768,21 @@
107625108768
sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
107626108769
break;
107627108770
}
107628108771
case SQLITE_TEXT: {
107629108772
const unsigned char *z = sqlite3_value_text(argv[0]);
108773
+ const unsigned char *z0;
108774
+ unsigned char c;
107630108775
if( z==0 ) return;
107631
- len = 0;
107632
- while( *z ){
107633
- len++;
107634
- SQLITE_SKIP_UTF8(z);
108776
+ z0 = z;
108777
+ while( (c = *z)!=0 ){
108778
+ z++;
108779
+ if( c>=0xc0 ){
108780
+ while( (*z & 0xc0)==0x80 ){ z++; z0++; }
108781
+ }
107635108782
}
107636
- sqlite3_result_int(context, len);
108783
+ sqlite3_result_int(context, (int)(z-z0));
107637108784
break;
107638108785
}
107639108786
default: {
107640108787
sqlite3_result_null(context);
107641108788
break;
@@ -108706,10 +109853,12 @@
108706109853
int nPattern; /* Size of zPattern */
108707109854
int nRep; /* Size of zRep */
108708109855
i64 nOut; /* Maximum size of zOut */
108709109856
int loopLimit; /* Last zStr[] that might match zPattern[] */
108710109857
int i, j; /* Loop counters */
109858
+ unsigned cntExpand; /* Number zOut expansions */
109859
+ sqlite3 *db = sqlite3_context_db_handle(context);
108711109860
108712109861
assert( argc==3 );
108713109862
UNUSED_PARAMETER(argc);
108714109863
zStr = sqlite3_value_text(argv[0]);
108715109864
if( zStr==0 ) return;
@@ -108737,37 +109886,44 @@
108737109886
zOut = contextMalloc(context, (i64)nOut);
108738109887
if( zOut==0 ){
108739109888
return;
108740109889
}
108741109890
loopLimit = nStr - nPattern;
109891
+ cntExpand = 0;
108742109892
for(i=j=0; i<=loopLimit; i++){
108743109893
if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
108744109894
zOut[j++] = zStr[i];
108745109895
}else{
108746
- u8 *zOld;
108747
- sqlite3 *db = sqlite3_context_db_handle(context);
108748
- nOut += nRep - nPattern;
108749
- testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
108750
- testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
108751
- if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
108752
- sqlite3_result_error_toobig(context);
108753
- sqlite3_free(zOut);
108754
- return;
108755
- }
108756
- zOld = zOut;
108757
- zOut = sqlite3_realloc64(zOut, (int)nOut);
108758
- if( zOut==0 ){
108759
- sqlite3_result_error_nomem(context);
108760
- sqlite3_free(zOld);
108761
- return;
109896
+ if( nRep>nPattern ){
109897
+ nOut += nRep - nPattern;
109898
+ testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
109899
+ testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
109900
+ if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
109901
+ sqlite3_result_error_toobig(context);
109902
+ sqlite3_free(zOut);
109903
+ return;
109904
+ }
109905
+ cntExpand++;
109906
+ if( (cntExpand&(cntExpand-1))==0 ){
109907
+ /* Grow the size of the output buffer only on substitutions
109908
+ ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
109909
+ u8 *zOld;
109910
+ zOld = zOut;
109911
+ zOut = sqlite3_realloc64(zOut, (int)nOut + (nOut - nStr - 1));
109912
+ if( zOut==0 ){
109913
+ sqlite3_result_error_nomem(context);
109914
+ sqlite3_free(zOld);
109915
+ return;
109916
+ }
109917
+ }
108762109918
}
108763109919
memcpy(&zOut[j], zRep, nRep);
108764109920
j += nRep;
108765109921
i += nPattern-1;
108766109922
}
108767109923
}
108768
- assert( j+nStr-i+1==nOut );
109924
+ assert( j+nStr-i+1<=nOut );
108769109925
memcpy(&zOut[j], &zStr[i], nStr-i);
108770109926
j += nStr - i;
108771109927
assert( j<=nOut );
108772109928
zOut[j] = 0;
108773109929
sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
@@ -113826,12 +114982,12 @@
113826114982
#define sqlite3_bind_pointer sqlite3_api->bind_pointer
113827114983
#define sqlite3_result_pointer sqlite3_api->result_pointer
113828114984
#define sqlite3_value_pointer sqlite3_api->value_pointer
113829114985
/* Version 3.22.0 and later */
113830114986
#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
113831
-#define sqlite3_value_nochange sqltie3_api->value_nochange
113832
-#define sqlite3_vtab_collation sqltie3_api->vtab_collation
114987
+#define sqlite3_value_nochange sqlite3_api->value_nochange
114988
+#define sqlite3_vtab_collation sqlite3_api->vtab_collation
113833114989
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
113834114990
113835114991
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
113836114992
/* This case when the file really is being compiled as a loadable
113837114993
** extension */
@@ -117811,11 +118967,11 @@
117811118967
sqlite3 *db = pData->db;
117812118968
if( !db->mallocFailed && (db->flags & SQLITE_WriteSchema)==0 ){
117813118969
char *z;
117814118970
if( zObj==0 ) zObj = "?";
117815118971
z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
117816
- if( zExtra ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
118972
+ if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
117817118973
sqlite3DbFree(db, *pData->pzErrMsg);
117818118974
*pData->pzErrMsg = z;
117819118975
}
117820118976
pData->rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_CORRUPT_BKPT;
117821118977
}
@@ -119950,16 +121106,19 @@
119950121106
addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
119951121107
codeOffset(v, p->iOffset, addrContinue);
119952121108
iSortTab = iTab;
119953121109
bSeq = 1;
119954121110
}
119955
- for(i=0, iCol=nKey+bSeq; i<nSortData; i++){
121111
+ for(i=0, iCol=nKey+bSeq-1; i<nSortData; i++){
121112
+ if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
121113
+ }
121114
+ for(i=nSortData-1; i>=0; i--){
119956121115
int iRead;
119957121116
if( aOutEx[i].u.x.iOrderByCol ){
119958121117
iRead = aOutEx[i].u.x.iOrderByCol-1;
119959121118
}else{
119960
- iRead = iCol++;
121119
+ iRead = iCol--;
119961121120
}
119962121121
sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
119963121122
VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
119964121123
}
119965121124
switch( eDest ){
@@ -127022,12 +128181,12 @@
127022128181
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
127023128182
if( rc!=SQLITE_OK ) return rc;
127024128183
while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
127025128184
const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
127026128185
assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
127027
- if( zSubSql ){
127028
- assert( zSubSql[0]!='S' );
128186
+ assert( sqlite3_strnicmp(zSubSql,"SELECT",6)!=0 || CORRUPT_DB );
128187
+ if( zSubSql && zSubSql[0]!='S' ){
127029128188
rc = execSql(db, pzErrMsg, zSubSql);
127030128189
if( rc!=SQLITE_OK ) break;
127031128190
}
127032128191
}
127033128192
assert( rc!=SQLITE_ROW );
@@ -130561,11 +131720,19 @@
130561131720
assert( pX!=0 );
130562131721
testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
130563131722
if( sqlite3ExprIsVector(pX->pRight) ){
130564131723
r1 = rTemp = sqlite3GetTempReg(pParse);
130565131724
codeExprOrVector(pParse, pX->pRight, r1, 1);
130566
- op = aMoveOp[(pX->op - TK_GT) | 0x0001];
131725
+ testcase( pX->op==TK_GT );
131726
+ testcase( pX->op==TK_GE );
131727
+ testcase( pX->op==TK_LT );
131728
+ testcase( pX->op==TK_LE );
131729
+ op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
131730
+ assert( pX->op!=TK_GT || op==OP_SeekGE );
131731
+ assert( pX->op!=TK_GE || op==OP_SeekGE );
131732
+ assert( pX->op!=TK_LT || op==OP_SeekLE );
131733
+ assert( pX->op!=TK_LE || op==OP_SeekLE );
130567131734
}else{
130568131735
r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
130569131736
disableTerm(pLevel, pStart);
130570131737
op = aMoveOp[(pX->op - TK_GT)];
130571131738
}
@@ -131336,10 +132503,16 @@
131336132503
assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
131337132504
pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
131338132505
WO_EQ|WO_IN|WO_IS, 0);
131339132506
if( pAlt==0 ) continue;
131340132507
if( pAlt->wtFlags & (TERM_CODED) ) continue;
132508
+ if( (pAlt->eOperator & WO_IN)
132509
+ && (pAlt->pExpr->flags & EP_xIsSelect)
132510
+ && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
132511
+ ){
132512
+ continue;
132513
+ }
131341132514
testcase( pAlt->eOperator & WO_EQ );
131342132515
testcase( pAlt->eOperator & WO_IS );
131343132516
testcase( pAlt->eOperator & WO_IN );
131344132517
VdbeModuleComment((v, "begin transitive constraint"));
131345132518
sEAlt = *pAlt->pExpr;
@@ -132250,10 +133423,13 @@
132250133423
if( ALWAYS(pSrc!=0) ){
132251133424
int i;
132252133425
for(i=0; i<pSrc->nSrc; i++){
132253133426
mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
132254133427
mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
133428
+ if( pSrc->a[i].fg.isTabFunc ){
133429
+ mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
133430
+ }
132255133431
}
132256133432
}
132257133433
pS = pS->pPrior;
132258133434
}
132259133435
return mask;
@@ -132662,11 +133838,11 @@
132662133838
transferJoinMarkings(pNew, pExpr);
132663133839
idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
132664133840
exprAnalyze(pSrc, pWC, idxNew);
132665133841
}
132666133842
pTerm = &pWC->a[idxTerm];
132667
- pTerm->wtFlags = TERM_CODED|TERM_VIRTUAL; /* Disable the original */
133843
+ pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
132668133844
pTerm->eOperator = 0;
132669133845
}
132670133846
132671133847
/* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
132672133848
** a virtual term for each vector component. The expression object
@@ -135387,14 +136563,16 @@
135387136563
pNew->wsFlags |= WHERE_COLUMN_EQ;
135388136564
assert( saved_nEq==pNew->u.btree.nEq );
135389136565
if( iCol==XN_ROWID
135390136566
|| (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
135391136567
){
135392
- if( iCol>=0 && pProbe->uniqNotNull==0 ){
135393
- pNew->wsFlags |= WHERE_UNQ_WANTED;
135394
- }else{
136568
+ if( iCol==XN_ROWID || pProbe->uniqNotNull
136569
+ || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
136570
+ ){
135395136571
pNew->wsFlags |= WHERE_ONEROW;
136572
+ }else{
136573
+ pNew->wsFlags |= WHERE_UNQ_WANTED;
135396136574
}
135397136575
}
135398136576
}else if( eOp & WO_ISNULL ){
135399136577
pNew->wsFlags |= WHERE_COLUMN_NULL;
135400136578
}else if( eOp & (WO_GT|WO_GE) ){
@@ -137537,10 +138715,11 @@
137537138715
** FROM ... WHERE random()>0; -- eval random() once per row
137538138716
** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
137539138717
*/
137540138718
for(ii=0; ii<sWLB.pWC->nTerm; ii++){
137541138719
WhereTerm *pT = &sWLB.pWC->a[ii];
138720
+ if( pT->wtFlags & TERM_VIRTUAL ) continue;
137542138721
if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
137543138722
sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
137544138723
pT->wtFlags |= TERM_CODED;
137545138724
}
137546138725
}
@@ -139995,11 +141174,12 @@
139995141174
#if defined(YYCOVERAGE)
139996141175
yycoverage[stateno][iLookAhead] = 1;
139997141176
#endif
139998141177
do{
139999141178
i = yy_shift_ofst[stateno];
140000
- assert( i>=0 && i+YYNTOKEN<=sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) );
141179
+ assert( i>=0 );
141180
+ assert( i+YYNTOKEN<=(int)sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) );
140001141181
assert( iLookAhead!=YYNOCODE );
140002141182
assert( iLookAhead < YYNTOKEN );
140003141183
i += iLookAhead;
140004141184
if( yy_lookahead[i]!=iLookAhead ){
140005141185
#ifdef YYFALLBACK
@@ -140709,10 +141889,14 @@
140709141889
}
140710141890
break;
140711141891
case 34: /* ccons ::= DEFAULT scanpt ID|INDEXED */
140712141892
{
140713141893
Expr *p = tokenExpr(pParse, TK_STRING, yymsp[0].minor.yy0);
141894
+ if( p ){
141895
+ sqlite3ExprIdToTrueFalse(p);
141896
+ testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
141897
+ }
140714141898
sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
140715141899
}
140716141900
break;
140717141901
case 35: /* ccons ::= NOT NULL onconf */
140718141902
{sqlite3AddNotNull(pParse, yymsp[0].minor.yy4);}
@@ -143602,10 +144786,15 @@
143602144786
}
143603144787
if( rc==SQLITE_OK ){
143604144788
sqlite3GlobalConfig.isPCacheInit = 1;
143605144789
rc = sqlite3OsInit();
143606144790
}
144791
+#ifdef SQLITE_ENABLE_DESERIALIZE
144792
+ if( rc==SQLITE_OK ){
144793
+ rc = sqlite3MemdbInit();
144794
+ }
144795
+#endif
143607144796
if( rc==SQLITE_OK ){
143608144797
sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
143609144798
sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
143610144799
sqlite3GlobalConfig.isInit = 1;
143611144800
#ifdef SQLITE_EXTRA_INIT
@@ -143634,11 +144823,11 @@
143634144823
** reason. So we run it once during initialization.
143635144824
*/
143636144825
#ifndef NDEBUG
143637144826
#ifndef SQLITE_OMIT_FLOATING_POINT
143638144827
/* This section of code's only "output" is via assert() statements. */
143639
- if ( rc==SQLITE_OK ){
144828
+ if( rc==SQLITE_OK ){
143640144829
u64 x = (((u64)1)<<63)-1;
143641144830
double y;
143642144831
assert(sizeof(x)==8);
143643144832
assert(sizeof(x)==sizeof(y));
143644144833
memcpy(&y, &x, 8);
@@ -144801,16 +145990,26 @@
144801145990
#endif
144802145991
/* SQLITE_AUTH */ "authorization denied",
144803145992
/* SQLITE_FORMAT */ 0,
144804145993
/* SQLITE_RANGE */ "column index out of range",
144805145994
/* SQLITE_NOTADB */ "file is not a database",
145995
+ /* SQLITE_NOTICE */ "notification message",
145996
+ /* SQLITE_WARNING */ "warning message",
144806145997
};
144807145998
const char *zErr = "unknown error";
144808145999
switch( rc ){
144809146000
case SQLITE_ABORT_ROLLBACK: {
144810146001
zErr = "abort due to ROLLBACK";
144811146002
break;
146003
+ }
146004
+ case SQLITE_ROW: {
146005
+ zErr = "another row available";
146006
+ break;
146007
+ }
146008
+ case SQLITE_DONE: {
146009
+ zErr = "no more rows available";
146010
+ break;
144812146011
}
144813146012
default: {
144814146013
rc &= 0xff;
144815146014
if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
144816146015
zErr = aMsg[rc];
@@ -160794,10 +161993,11 @@
160794161993
if( rc==SQLITE_OK ){
160795161994
sqlite3_bind_int64(pStmt, 1, iBlock);
160796161995
sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
160797161996
sqlite3_step(pStmt);
160798161997
rc = sqlite3_reset(pStmt);
161998
+ sqlite3_bind_null(pStmt, 2);
160799161999
}
160800162000
return rc;
160801162001
}
160802162002
160803162003
/*
@@ -160850,10 +162050,11 @@
160850162050
sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
160851162051
}
160852162052
sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
160853162053
sqlite3_step(pStmt);
160854162054
rc = sqlite3_reset(pStmt);
162055
+ sqlite3_bind_null(pStmt, 6);
160855162056
}
160856162057
return rc;
160857162058
}
160858162059
160859162060
/*
@@ -162329,10 +163530,11 @@
162329163530
}
162330163531
sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
162331163532
sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
162332163533
sqlite3_step(pStmt);
162333163534
*pRC = sqlite3_reset(pStmt);
163535
+ sqlite3_bind_null(pStmt, 2);
162334163536
sqlite3_free(a);
162335163537
}
162336163538
162337163539
/*
162338163540
** Merge the entire database so that there is one segment for each
@@ -163517,10 +164719,11 @@
163517164719
sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
163518164720
sqlite3_bind_int64(pChomp, 3, iAbsLevel);
163519164721
sqlite3_bind_int(pChomp, 4, iIdx);
163520164722
sqlite3_step(pChomp);
163521164723
rc = sqlite3_reset(pChomp);
164724
+ sqlite3_bind_null(pChomp, 2);
163522164725
}
163523164726
}
163524164727
163525164728
sqlite3_free(root.a);
163526164729
sqlite3_free(block.a);
@@ -163596,10 +164799,11 @@
163596164799
if( rc==SQLITE_OK ){
163597164800
sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
163598164801
sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
163599164802
sqlite3_step(pReplace);
163600164803
rc = sqlite3_reset(pReplace);
164804
+ sqlite3_bind_null(pReplace, 2);
163601164805
}
163602164806
163603164807
return rc;
163604164808
}
163605164809
@@ -164410,11 +165614,10 @@
164410165614
sqlite3_value **apVal, /* Array of arguments */
164411165615
sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
164412165616
){
164413165617
Fts3Table *p = (Fts3Table *)pVtab;
164414165618
int rc = SQLITE_OK; /* Return Code */
164415
- int isRemove = 0; /* True for an UPDATE or DELETE */
164416165619
u32 *aSzIns = 0; /* Sizes of inserted documents */
164417165620
u32 *aSzDel = 0; /* Sizes of deleted documents */
164418165621
int nChng = 0; /* Net change in number of documents */
164419165622
int bInsertDone = 0;
164420165623
@@ -164508,11 +165711,10 @@
164508165711
164509165712
/* If this is a DELETE or UPDATE operation, remove the old record. */
164510165713
if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
164511165714
assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
164512165715
rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
164513
- isRemove = 1;
164514165716
}
164515165717
164516165718
/* If this is an INSERT or UPDATE operation, insert the new record. */
164517165719
if( nArg>1 && rc==SQLITE_OK ){
164518165720
int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
@@ -164520,11 +165722,11 @@
164520165722
rc = fts3InsertData(p, apVal, pRowid);
164521165723
if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
164522165724
rc = FTS_CORRUPT_VTAB;
164523165725
}
164524165726
}
164525
- if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
165727
+ if( rc==SQLITE_OK ){
164526165728
rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
164527165729
}
164528165730
if( rc==SQLITE_OK ){
164529165731
assert( p->iPrevDocid==*pRowid );
164530165732
rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
@@ -167828,10 +169030,11 @@
167828169030
}
167829169031
sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
167830169032
sqlite3_step(p);
167831169033
pNode->isDirty = 0;
167832169034
rc = sqlite3_reset(p);
169035
+ sqlite3_bind_null(p, 2);
167833169036
if( pNode->iNode==0 && rc==SQLITE_OK ){
167834169037
pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
167835169038
nodeHashInsert(pRtree, pNode);
167836169039
}
167837169040
}
@@ -179823,11 +181026,11 @@
179823181026
SessionTable *pTab /* Table that change applies to */
179824181027
){
179825181028
int iHash;
179826181029
int bNull = 0;
179827181030
int rc = SQLITE_OK;
179828
- SessionStat1Ctx stat1;
181031
+ SessionStat1Ctx stat1 = {0};
179829181032
179830181033
if( pSession->rc ) return;
179831181034
179832181035
/* Load table details if required */
179833181036
if( sessionInitTable(pSession, pTab) ) return;
@@ -181428,30 +182631,37 @@
181428182631
for(i=0; i<nCol && rc==SQLITE_OK; i++){
181429182632
int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
181430182633
if( abPK && abPK[i]==0 ) continue;
181431182634
rc = sessionInputBuffer(pIn, 9);
181432182635
if( rc==SQLITE_OK ){
181433
- eType = pIn->aData[pIn->iNext++];
181434
- }
181435
-
181436
- assert( apOut[i]==0 );
181437
- if( eType ){
181438
- apOut[i] = sqlite3ValueNew(0);
181439
- if( !apOut[i] ) rc = SQLITE_NOMEM;
182636
+ if( pIn->iNext>=pIn->nData ){
182637
+ rc = SQLITE_CORRUPT_BKPT;
182638
+ }else{
182639
+ eType = pIn->aData[pIn->iNext++];
182640
+ assert( apOut[i]==0 );
182641
+ if( eType ){
182642
+ apOut[i] = sqlite3ValueNew(0);
182643
+ if( !apOut[i] ) rc = SQLITE_NOMEM;
182644
+ }
182645
+ }
181440182646
}
181441182647
181442182648
if( rc==SQLITE_OK ){
181443182649
u8 *aVal = &pIn->aData[pIn->iNext];
181444182650
if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
181445182651
int nByte;
181446182652
pIn->iNext += sessionVarintGet(aVal, &nByte);
181447182653
rc = sessionInputBuffer(pIn, nByte);
181448182654
if( rc==SQLITE_OK ){
181449
- u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
181450
- rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
182655
+ if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
182656
+ rc = SQLITE_CORRUPT_BKPT;
182657
+ }else{
182658
+ u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
182659
+ rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
182660
+ pIn->iNext += nByte;
182661
+ }
181451182662
}
181452
- pIn->iNext += nByte;
181453182663
}
181454182664
if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
181455182665
sqlite3_int64 v = sessionGetI64(aVal);
181456182666
if( eType==SQLITE_INTEGER ){
181457182667
sqlite3VdbeMemSetInt64(apOut[i], v);
@@ -181487,12 +182697,23 @@
181487182697
int nRead = 0;
181488182698
181489182699
rc = sessionInputBuffer(pIn, 9);
181490182700
if( rc==SQLITE_OK ){
181491182701
nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
181492
- rc = sessionInputBuffer(pIn, nRead+nCol+100);
181493
- nRead += nCol;
182702
+ /* The hard upper limit for the number of columns in an SQLite
182703
+ ** database table is, according to sqliteLimit.h, 32676. So
182704
+ ** consider any table-header that purports to have more than 65536
182705
+ ** columns to be corrupt. This is convenient because otherwise,
182706
+ ** if the (nCol>65536) condition below were omitted, a sufficiently
182707
+ ** large value for nCol may cause nRead to wrap around and become
182708
+ ** negative. Leading to a crash. */
182709
+ if( nCol<0 || nCol>65536 ){
182710
+ rc = SQLITE_CORRUPT_BKPT;
182711
+ }else{
182712
+ rc = sessionInputBuffer(pIn, nRead+nCol+100);
182713
+ nRead += nCol;
182714
+ }
181494182715
}
181495182716
181496182717
while( rc==SQLITE_OK ){
181497182718
while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
181498182719
nRead++;
@@ -181565,15 +182786,19 @@
181565182786
rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
181566182787
if( rc==SQLITE_OK ){
181567182788
int nByte;
181568182789
int nVarint;
181569182790
nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
181570
- nCopy -= nVarint;
181571
- p->in.iNext += nVarint;
181572
- nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
181573
- p->tblhdr.nBuf = 0;
181574
- sessionBufferGrow(&p->tblhdr, nByte, &rc);
182791
+ if( p->nCol>0 ){
182792
+ nCopy -= nVarint;
182793
+ p->in.iNext += nVarint;
182794
+ nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
182795
+ p->tblhdr.nBuf = 0;
182796
+ sessionBufferGrow(&p->tblhdr, nByte, &rc);
182797
+ }else{
182798
+ rc = SQLITE_CORRUPT_BKPT;
182799
+ }
181575182800
}
181576182801
181577182802
if( rc==SQLITE_OK ){
181578182803
int iPK = sizeof(sqlite3_value*)*p->nCol*2;
181579182804
memset(p->tblhdr.aBuf, 0, iPK);
@@ -181646,10 +182871,17 @@
181646182871
if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
181647182872
p->in.iCurrent = p->in.iNext;
181648182873
if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
181649182874
op = p->in.aData[p->in.iNext++];
181650182875
}
182876
+
182877
+ if( p->zTab==0 ){
182878
+ /* The first record in the changeset is not a table header. Must be a
182879
+ ** corrupt changeset. */
182880
+ assert( p->in.iNext==1 );
182881
+ return (p->rc = SQLITE_CORRUPT_BKPT);
182882
+ }
181651182883
181652182884
p->op = op;
181653182885
p->bIndirect = p->in.aData[p->in.iNext++];
181654182886
if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
181655182887
return (p->rc = SQLITE_CORRUPT_BKPT);
@@ -181689,13 +182921,13 @@
181689182921
** modified fields are present in the new.* record. The old.* record
181690182922
** is currently completely empty. This block shifts the PK fields from
181691182923
** new.* to old.*, to accommodate the code that reads these arrays. */
181692182924
for(i=0; i<p->nCol; i++){
181693182925
assert( p->apValue[i]==0 );
181694
- assert( p->abPK[i]==0 || p->apValue[i+p->nCol] );
181695182926
if( p->abPK[i] ){
181696182927
p->apValue[i] = p->apValue[i+p->nCol];
182928
+ if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
181697182929
p->apValue[i+p->nCol] = 0;
181698182930
}
181699182931
}
181700182932
}
181701182933
}
@@ -182416,11 +183648,17 @@
182416183648
182417183649
for(i=0; rc==SQLITE_OK && i<nCol; i++){
182418183650
if( !abPK || abPK[i] ){
182419183651
sqlite3_value *pVal;
182420183652
(void)xValue(pIter, i, &pVal);
182421
- rc = sessionBindValue(pStmt, i+1, pVal);
183653
+ if( pVal==0 ){
183654
+ /* The value in the changeset was "undefined". This indicates a
183655
+ ** corrupt changeset blob. */
183656
+ rc = SQLITE_CORRUPT_BKPT;
183657
+ }else{
183658
+ rc = sessionBindValue(pStmt, i+1, pVal);
183659
+ }
182422183660
}
182423183661
}
182424183662
return rc;
182425183663
}
182426183664
@@ -188053,11 +189291,12 @@
188053189291
#if defined(fts5YYCOVERAGE)
188054189292
fts5yycoverage[stateno][iLookAhead] = 1;
188055189293
#endif
188056189294
do{
188057189295
i = fts5yy_shift_ofst[stateno];
188058
- assert( i>=0 && i+fts5YYNFTS5TOKEN<=sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0]) );
189296
+ assert( i>=0 );
189297
+ assert( i+fts5YYNFTS5TOKEN<=(int)sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0]) );
188059189298
assert( iLookAhead!=fts5YYNOCODE );
188060189299
assert( iLookAhead < fts5YYNFTS5TOKEN );
188061189300
i += iLookAhead;
188062189301
if( fts5yy_lookahead[i]!=iLookAhead ){
188063189302
#ifdef fts5YYFALLBACK
@@ -192495,11 +193734,11 @@
192495193734
if( sCtx.pPhrase==0 ){
192496193735
/* This happens when parsing a token or quoted phrase that contains
192497193736
** no token characters at all. (e.g ... MATCH '""'). */
192498193737
sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
192499193738
}else if( sCtx.pPhrase->nTerm ){
192500
- sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = bPrefix;
193739
+ sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
192501193740
}
192502193741
pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
192503193742
}
192504193743
192505193744
return sCtx.pPhrase;
@@ -194958,10 +196197,11 @@
194958196197
194959196198
sqlite3_bind_int64(p->pWriter, 1, iRowid);
194960196199
sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC);
194961196200
sqlite3_step(p->pWriter);
194962196201
p->rc = sqlite3_reset(p->pWriter);
196202
+ sqlite3_bind_null(p->pWriter, 2);
194963196203
}
194964196204
194965196205
/*
194966196206
** Execute the following SQL:
194967196207
**
@@ -196586,10 +197826,11 @@
196586197826
i64 val = sqlite3_column_int(pIdxSelect, 0);
196587197827
iPg = (int)(val>>1);
196588197828
bDlidx = (val & 0x0001);
196589197829
}
196590197830
p->rc = sqlite3_reset(pIdxSelect);
197831
+ sqlite3_bind_null(pIdxSelect, 2);
196591197832
196592197833
if( iPg<pSeg->pgnoFirst ){
196593197834
iPg = pSeg->pgnoFirst;
196594197835
bDlidx = 0;
196595197836
}
@@ -197798,10 +199039,11 @@
197798199039
u8 aBlob[2] = {0xff, 0xff};
197799199040
sqlite3_bind_int(pIdxSelect, 1, iSegid);
197800199041
sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
197801199042
assert( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
197802199043
p->rc = sqlite3_reset(pIdxSelect);
199044
+ sqlite3_bind_null(pIdxSelect, 2);
197803199045
}
197804199046
}
197805199047
#endif
197806199048
}
197807199049
}
@@ -197924,10 +199166,11 @@
197924199166
/* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
197925199167
sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC);
197926199168
sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
197927199169
sqlite3_step(p->pIdxWriter);
197928199170
p->rc = sqlite3_reset(p->pIdxWriter);
199171
+ sqlite3_bind_null(p->pIdxWriter, 2);
197929199172
}
197930199173
pWriter->iBtPage = 0;
197931199174
}
197932199175
197933199176
/*
@@ -203331,11 +204574,11 @@
203331204574
int nArg, /* Number of args */
203332204575
sqlite3_value **apUnused /* Function arguments */
203333204576
){
203334204577
assert( nArg==0 );
203335204578
UNUSED_PARAM2(nArg, apUnused);
203336
- sqlite3_result_text(pCtx, "fts5: 2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d", -1, SQLITE_TRANSIENT);
204579
+ sqlite3_result_text(pCtx, "fts5: 2018-03-16 20:23:01 d75e67654aa9620b9617786553a002f54e8c6dcbbcc58948a06bd98a0916d75a", -1, SQLITE_TRANSIENT);
203337204580
}
203338204581
203339204582
static int fts5Init(sqlite3 *db){
203340204583
static const sqlite3_module fts5Mod = {
203341204584
/* iVersion */ 2,
@@ -203907,10 +205150,11 @@
203907205150
if( rc==SQLITE_OK ){
203908205151
sqlite3_bind_int64(pReplace, 1, iRowid);
203909205152
sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC);
203910205153
sqlite3_step(pReplace);
203911205154
rc = sqlite3_reset(pReplace);
205155
+ sqlite3_bind_null(pReplace, 2);
203912205156
}
203913205157
}
203914205158
return rc;
203915205159
}
203916205160
@@ -204567,10 +205811,11 @@
204567205811
}else{
204568205812
sqlite3_bind_int(pReplace, 2, iVal);
204569205813
}
204570205814
sqlite3_step(pReplace);
204571205815
rc = sqlite3_reset(pReplace);
205816
+ sqlite3_bind_null(pReplace, 1);
204572205817
}
204573205818
if( rc==SQLITE_OK && pVal ){
204574205819
int iNew = p->pConfig->iCookie + 1;
204575205820
rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
204576205821
if( rc==SQLITE_OK ){
@@ -207599,12 +208844,12 @@
207599208844
}
207600208845
#endif /* SQLITE_CORE */
207601208846
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
207602208847
207603208848
/************** End of stmt.c ************************************************/
207604
-#if __LINE__!=207604
208849
+#if __LINE__!=208849
207605208850
#undef SQLITE_SOURCE_ID
207606
-#define SQLITE_SOURCE_ID "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2alt2"
208851
+#define SQLITE_SOURCE_ID "2018-03-17 16:26:36 442e816b5fed80ebeb58c7c0ab9c2ef999bf488519bf5da670e9cec47703alt2"
207607208852
#endif
207608208853
/* Return the source-id for this library */
207609208854
SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
207610208855
/************************** End of sqlite3.c ******************************/
207611208856
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.22.0. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -1145,13 +1145,13 @@
1145 **
1146 ** See also: [sqlite3_libversion()],
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-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d"
1153
1154 /*
1155 ** CAPI3REF: Run-Time Library Version Numbers
1156 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1157 **
@@ -3518,20 +3518,20 @@
3518 /*
3519 ** CAPI3REF: Formatted String Printing Functions
3520 **
3521 ** These routines are work-alikes of the "printf()" family of functions
3522 ** from the standard C library.
3523 ** These routines understand most of the common K&R formatting options,
3524 ** plus some additional non-standard formats, detailed below.
3525 ** Note that some of the more obscure formatting options from recent
3526 ** C-library standards are omitted from this implementation.
3527 **
3528 ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
3529 ** results into memory obtained from [sqlite3_malloc()].
3530 ** The strings returned by these two routines should be
3531 ** released by [sqlite3_free()]. ^Both routines return a
3532 ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
3533 ** memory to hold the resulting string.
3534 **
3535 ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
3536 ** the standard C library. The result is written into the
3537 ** buffer supplied as the second parameter whose size is given by
@@ -3551,75 +3551,11 @@
3551 ** the zero terminator. So the longest string that can be completely
3552 ** written will be n-1 characters.
3553 **
3554 ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
3555 **
3556 ** These routines all implement some additional formatting
3557 ** options that are useful for constructing SQL statements.
3558 ** All of the usual printf() formatting options apply. In addition, there
3559 ** is are "%q", "%Q", "%w" and "%z" options.
3560 **
3561 ** ^(The %q option works like %s in that it substitutes a nul-terminated
3562 ** string from the argument list. But %q also doubles every '\'' character.
3563 ** %q is designed for use inside a string literal.)^ By doubling each '\''
3564 ** character it escapes that character and allows it to be inserted into
3565 ** the string.
3566 **
3567 ** For example, assume the string variable zText contains text as follows:
3568 **
3569 ** <blockquote><pre>
3570 ** char *zText = "It's a happy day!";
3571 ** </pre></blockquote>
3572 **
3573 ** One can use this text in an SQL statement as follows:
3574 **
3575 ** <blockquote><pre>
3576 ** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
3577 ** sqlite3_exec(db, zSQL, 0, 0, 0);
3578 ** sqlite3_free(zSQL);
3579 ** </pre></blockquote>
3580 **
3581 ** Because the %q format string is used, the '\'' character in zText
3582 ** is escaped and the SQL generated is as follows:
3583 **
3584 ** <blockquote><pre>
3585 ** INSERT INTO table1 VALUES('It''s a happy day!')
3586 ** </pre></blockquote>
3587 **
3588 ** This is correct. Had we used %s instead of %q, the generated SQL
3589 ** would have looked like this:
3590 **
3591 ** <blockquote><pre>
3592 ** INSERT INTO table1 VALUES('It's a happy day!');
3593 ** </pre></blockquote>
3594 **
3595 ** This second example is an SQL syntax error. As a general rule you should
3596 ** always use %q instead of %s when inserting text into a string literal.
3597 **
3598 ** ^(The %Q option works like %q except it also adds single quotes around
3599 ** the outside of the total string. Additionally, if the parameter in the
3600 ** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
3601 ** single quotes).)^ So, for example, one could say:
3602 **
3603 ** <blockquote><pre>
3604 ** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
3605 ** sqlite3_exec(db, zSQL, 0, 0, 0);
3606 ** sqlite3_free(zSQL);
3607 ** </pre></blockquote>
3608 **
3609 ** The code above will render a correct SQL statement in the zSQL
3610 ** variable even if the zText variable is a NULL pointer.
3611 **
3612 ** ^(The "%w" formatting option is like "%q" except that it expects to
3613 ** be contained within double-quotes instead of single quotes, and it
3614 ** escapes the double-quote character instead of the single-quote
3615 ** character.)^ The "%w" formatting option is intended for safely inserting
3616 ** table and column names into a constructed SQL statement.
3617 **
3618 ** ^(The "%z" formatting option works like "%s" but with the
3619 ** addition that after the string has been read and copied into
3620 ** the result, [sqlite3_free()] is called on the input string.)^
3621 */
3622 SQLITE_API char *sqlite3_mprintf(const char*,...);
3623 SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
3624 SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
3625 SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
@@ -4681,17 +4617,17 @@
4681 ** ^The specific value of WHERE-clause [parameter] might influence the
4682 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
4683 ** or [GLOB] operator or if the parameter is compared to an indexed column
4684 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
4685 ** </li>
 
4686 **
4687 ** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
4688 ** the extra prepFlags parameter, which is a bit array consisting of zero or
4689 ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
4690 ** sqlite3_prepare_v2() interface works exactly the same as
4691 ** sqlite3_prepare_v3() with a zero prepFlags parameter.
4692 ** </ol>
4693 */
4694 SQLITE_API int sqlite3_prepare(
4695 sqlite3 *db, /* Database handle */
4696 const char *zSql, /* SQL statement, UTF-8 encoded */
4697 int nByte, /* Maximum length of zSql in bytes. */
@@ -8315,10 +8251,19 @@
8315 ** transaction rollback or database recovery operations are not included.
8316 ** If an IO or other error occurs while writing a page to disk, the effect
8317 ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
8318 ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
8319 ** </dd>
 
 
 
 
 
 
 
 
 
8320 **
8321 ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
8322 ** <dd>This parameter returns zero for the current value if and only if
8323 ** all foreign key constraints (deferred or immediate) have been
8324 ** resolved.)^ ^The highwater mark is always 0.
@@ -8335,11 +8280,12 @@
8335 #define SQLITE_DBSTATUS_CACHE_HIT 7
8336 #define SQLITE_DBSTATUS_CACHE_MISS 8
8337 #define SQLITE_DBSTATUS_CACHE_WRITE 9
8338 #define SQLITE_DBSTATUS_DEFERRED_FKS 10
8339 #define SQLITE_DBSTATUS_CACHE_USED_SHARED 11
8340 #define SQLITE_DBSTATUS_MAX 11 /* Largest defined DBSTATUS */
 
8341
8342
8343 /*
8344 ** CAPI3REF: Prepared Statement Status
8345 ** METHOD: sqlite3_stmt
@@ -9815,10 +9761,132 @@
9815 **
9816 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
9817 */
9818 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
9819
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9820 /*
9821 ** Undo the hack that converts floating point types to integer for
9822 ** builds on processors without floating point support.
9823 */
9824 #ifdef SQLITE_OMIT_FLOATING_POINT
@@ -9962,20 +10030,27 @@
9962 #endif
9963
9964
9965 /*
9966 ** CAPI3REF: Session Object Handle
 
 
 
9967 */
9968 typedef struct sqlite3_session sqlite3_session;
9969
9970 /*
9971 ** CAPI3REF: Changeset Iterator Handle
 
 
 
9972 */
9973 typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
9974
9975 /*
9976 ** CAPI3REF: Create A New Session Object
 
9977 **
9978 ** Create a new session object attached to database handle db. If successful,
9979 ** a pointer to the new object is written to *ppSession and SQLITE_OK is
9980 ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
9981 ** error code (e.g. SQLITE_NOMEM) is returned.
@@ -10008,10 +10083,11 @@
10008 sqlite3_session **ppSession /* OUT: New session object */
10009 );
10010
10011 /*
10012 ** CAPI3REF: Delete A Session Object
 
10013 **
10014 ** Delete a session object previously allocated using
10015 ** [sqlite3session_create()]. Once a session object has been deleted, the
10016 ** results of attempting to use pSession with any other session module
10017 ** function are undefined.
@@ -10023,10 +10099,11 @@
10023 SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
10024
10025
10026 /*
10027 ** CAPI3REF: Enable Or Disable A Session Object
 
10028 **
10029 ** Enable or disable the recording of changes by a session object. When
10030 ** enabled, a session object records changes made to the database. When
10031 ** disabled - it does not. A newly created session object is enabled.
10032 ** Refer to the documentation for [sqlite3session_changeset()] for further
@@ -10042,10 +10119,11 @@
10042 */
10043 SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
10044
10045 /*
10046 ** CAPI3REF: Set Or Clear the Indirect Change Flag
 
10047 **
10048 ** Each change recorded by a session object is marked as either direct or
10049 ** indirect. A change is marked as indirect if either:
10050 **
10051 ** <ul>
@@ -10071,10 +10149,11 @@
10071 */
10072 SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
10073
10074 /*
10075 ** CAPI3REF: Attach A Table To A Session Object
 
10076 **
10077 ** If argument zTab is not NULL, then it is the name of a table to attach
10078 ** to the session object passed as the first argument. All subsequent changes
10079 ** made to the table while the session object is enabled will be recorded. See
10080 ** documentation for [sqlite3session_changeset()] for further details.
@@ -10133,10 +10212,11 @@
10133 const char *zTab /* Table name */
10134 );
10135
10136 /*
10137 ** CAPI3REF: Set a table filter on a Session Object.
 
10138 **
10139 ** The second argument (xFilter) is the "filter callback". For changes to rows
10140 ** in tables that are not attached to the Session object, the filter is called
10141 ** to determine whether changes to the table's rows should be tracked or not.
10142 ** If xFilter returns 0, changes is not tracked. Note that once a table is
@@ -10151,10 +10231,11 @@
10151 void *pCtx /* First argument passed to xFilter */
10152 );
10153
10154 /*
10155 ** CAPI3REF: Generate A Changeset From A Session Object
 
10156 **
10157 ** Obtain a changeset containing changes to the tables attached to the
10158 ** session object passed as the first argument. If successful,
10159 ** set *ppChangeset to point to a buffer containing the changeset
10160 ** and *pnChangeset to the size of the changeset in bytes before returning
@@ -10260,11 +10341,12 @@
10260 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
10261 void **ppChangeset /* OUT: Buffer containing changeset */
10262 );
10263
10264 /*
10265 ** CAPI3REF: Load The Difference Between Tables Into A Session
 
10266 **
10267 ** If it is not already attached to the session object passed as the first
10268 ** argument, this function attaches table zTbl in the same manner as the
10269 ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
10270 ** does not have a primary key, this function is a no-op (but does not return
@@ -10325,10 +10407,11 @@
10325 );
10326
10327
10328 /*
10329 ** CAPI3REF: Generate A Patchset From A Session Object
 
10330 **
10331 ** The differences between a patchset and a changeset are that:
10332 **
10333 ** <ul>
10334 ** <li> DELETE records consist of the primary key fields only. The
@@ -10376,10 +10459,11 @@
10376 */
10377 SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
10378
10379 /*
10380 ** CAPI3REF: Create An Iterator To Traverse A Changeset
 
10381 **
10382 ** Create an iterator used to iterate through the contents of a changeset.
10383 ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
10384 ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
10385 ** SQLite error code is returned.
@@ -10416,10 +10500,11 @@
10416 );
10417
10418
10419 /*
10420 ** CAPI3REF: Advance A Changeset Iterator
 
10421 **
10422 ** This function may only be used with iterators created by function
10423 ** [sqlite3changeset_start()]. If it is called on an iterator passed to
10424 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
10425 ** is returned and the call has no effect.
@@ -10440,10 +10525,11 @@
10440 */
10441 SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
10442
10443 /*
10444 ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
 
10445 **
10446 ** The pIter argument passed to this function may either be an iterator
10447 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10448 ** created by [sqlite3changeset_start()]. In the latter case, the most recent
10449 ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
@@ -10474,10 +10560,11 @@
10474 int *pbIndirect /* OUT: True for an 'indirect' change */
10475 );
10476
10477 /*
10478 ** CAPI3REF: Obtain The Primary Key Definition Of A Table
 
10479 **
10480 ** For each modified table, a changeset includes the following:
10481 **
10482 ** <ul>
10483 ** <li> The number of columns in the table, and
@@ -10505,10 +10592,11 @@
10505 int *pnCol /* OUT: Number of entries in output array */
10506 );
10507
10508 /*
10509 ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
 
10510 **
10511 ** The pIter argument passed to this function may either be an iterator
10512 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10513 ** created by [sqlite3changeset_start()]. In the latter case, the most recent
10514 ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
@@ -10535,10 +10623,11 @@
10535 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
10536 );
10537
10538 /*
10539 ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
 
10540 **
10541 ** The pIter argument passed to this function may either be an iterator
10542 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10543 ** created by [sqlite3changeset_start()]. In the latter case, the most recent
10544 ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
@@ -10568,10 +10657,11 @@
10568 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
10569 );
10570
10571 /*
10572 ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
 
10573 **
10574 ** This function should only be used with iterator objects passed to a
10575 ** conflict-handler callback by [sqlite3changeset_apply()] with either
10576 ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
10577 ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
@@ -10595,10 +10685,11 @@
10595 sqlite3_value **ppValue /* OUT: Value from conflicting row */
10596 );
10597
10598 /*
10599 ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
 
10600 **
10601 ** This function may only be called with an iterator passed to an
10602 ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
10603 ** it sets the output variable to the total number of known foreign key
10604 ** violations in the destination database and returns SQLITE_OK.
@@ -10611,10 +10702,11 @@
10611 );
10612
10613
10614 /*
10615 ** CAPI3REF: Finalize A Changeset Iterator
 
10616 **
10617 ** This function is used to finalize an iterator allocated with
10618 ** [sqlite3changeset_start()].
10619 **
10620 ** This function should only be called on iterators created using the
@@ -10627,18 +10719,20 @@
10627 ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
10628 ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
10629 ** to that error is returned by this function. Otherwise, SQLITE_OK is
10630 ** returned. This is to allow the following pattern (pseudo-code):
10631 **
 
10632 ** sqlite3changeset_start();
10633 ** while( SQLITE_ROW==sqlite3changeset_next() ){
10634 ** // Do something with change.
10635 ** }
10636 ** rc = sqlite3changeset_finalize();
10637 ** if( rc!=SQLITE_OK ){
10638 ** // An error has occurred
10639 ** }
 
10640 */
10641 SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
10642
10643 /*
10644 ** CAPI3REF: Invert A Changeset
@@ -10682,10 +10776,11 @@
10682 **
10683 ** This function combines the two input changesets using an
10684 ** sqlite3_changegroup object. Calling it produces similar results as the
10685 ** following code fragment:
10686 **
 
10687 ** sqlite3_changegroup *pGrp;
10688 ** rc = sqlite3_changegroup_new(&pGrp);
10689 ** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
10690 ** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
10691 ** if( rc==SQLITE_OK ){
@@ -10692,10 +10787,11 @@
10692 ** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
10693 ** }else{
10694 ** *ppOut = 0;
10695 ** *pnOut = 0;
10696 ** }
 
10697 **
10698 ** Refer to the sqlite3_changegroup documentation below for details.
10699 */
10700 SQLITE_API int sqlite3changeset_concat(
10701 int nA, /* Number of bytes in buffer pA */
@@ -10707,15 +10803,19 @@
10707 );
10708
10709
10710 /*
10711 ** CAPI3REF: Changegroup Handle
 
 
 
10712 */
10713 typedef struct sqlite3_changegroup sqlite3_changegroup;
10714
10715 /*
10716 ** CAPI3REF: Create A New Changegroup Object
 
10717 **
10718 ** An sqlite3_changegroup object is used to combine two or more changesets
10719 ** (or patchsets) into a single changeset (or patchset). A single changegroup
10720 ** object may combine changesets or patchsets, but not both. The output is
10721 ** always in the same format as the input.
@@ -10749,10 +10849,11 @@
10749 */
10750 SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
10751
10752 /*
10753 ** CAPI3REF: Add A Changeset To A Changegroup
 
10754 **
10755 ** Add all changes within the changeset (or patchset) in buffer pData (size
10756 ** nData bytes) to the changegroup.
10757 **
10758 ** If the buffer contains a patchset, then all prior calls to this function
@@ -10826,10 +10927,11 @@
10826 */
10827 SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
10828
10829 /*
10830 ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
 
10831 **
10832 ** Obtain a buffer containing a changeset (or patchset) representing the
10833 ** current contents of the changegroup. If the inputs to the changegroup
10834 ** were themselves changesets, the output is a changeset. Or, if the
10835 ** inputs were patchsets, the output is also a patchset.
@@ -10856,10 +10958,11 @@
10856 void **ppData /* OUT: Pointer to output buffer */
10857 );
10858
10859 /*
10860 ** CAPI3REF: Delete A Changegroup Object
 
10861 */
10862 SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
10863
10864 /*
10865 ** CAPI3REF: Apply A Changeset To A Database
@@ -12671,27 +12774,29 @@
12671 #define TK_THEN 138
12672 #define TK_ELSE 139
12673 #define TK_INDEX 140
12674 #define TK_ALTER 141
12675 #define TK_ADD 142
12676 #define TK_ISNOT 143
12677 #define TK_FUNCTION 144
12678 #define TK_COLUMN 145
12679 #define TK_AGG_FUNCTION 146
12680 #define TK_AGG_COLUMN 147
12681 #define TK_UMINUS 148
12682 #define TK_UPLUS 149
12683 #define TK_REGISTER 150
12684 #define TK_VECTOR 151
12685 #define TK_SELECT_COLUMN 152
12686 #define TK_IF_NULL_ROW 153
12687 #define TK_ASTERISK 154
12688 #define TK_SPAN 155
12689 #define TK_END_OF_FILE 156
12690 #define TK_UNCLOSED_STRING 157
12691 #define TK_SPACE 158
12692 #define TK_ILLEGAL 159
 
 
12693
12694 /* The token codes above must all fit in 8 bits */
12695 #define TKFLG_MASK 0xff
12696
12697 /* Flags that can be added to a token code when it is not
@@ -13933,84 +14038,85 @@
13933 #define OP_Divide 91 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
13934 #define OP_Remainder 92 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
13935 #define OP_Concat 93 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
13936 #define OP_Compare 94 /* synopsis: r[P1@P3] <-> r[P2@P3] */
13937 #define OP_BitNot 95 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
13938 #define OP_Offset 96 /* synopsis: r[P3] = sqlite_offset(P1) */
13939 #define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */
13940 #define OP_Column 98 /* synopsis: r[P3]=PX */
13941 #define OP_Affinity 99 /* synopsis: affinity(r[P1@P2]) */
13942 #define OP_MakeRecord 100 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
13943 #define OP_Count 101 /* synopsis: r[P2]=count() */
13944 #define OP_ReadCookie 102
13945 #define OP_SetCookie 103
13946 #define OP_ReopenIdx 104 /* synopsis: root=P2 iDb=P3 */
13947 #define OP_OpenRead 105 /* synopsis: root=P2 iDb=P3 */
13948 #define OP_OpenWrite 106 /* synopsis: root=P2 iDb=P3 */
13949 #define OP_OpenDup 107
13950 #define OP_OpenAutoindex 108 /* synopsis: nColumn=P2 */
13951 #define OP_OpenEphemeral 109 /* synopsis: nColumn=P2 */
13952 #define OP_SorterOpen 110
13953 #define OP_SequenceTest 111 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
13954 #define OP_OpenPseudo 112 /* synopsis: P3 columns in r[P2] */
13955 #define OP_Close 113
13956 #define OP_ColumnsUsed 114
13957 #define OP_Sequence 115 /* synopsis: r[P2]=cursor[P1].ctr++ */
13958 #define OP_NewRowid 116 /* synopsis: r[P2]=rowid */
13959 #define OP_Insert 117 /* synopsis: intkey=r[P3] data=r[P2] */
13960 #define OP_InsertInt 118 /* synopsis: intkey=P3 data=r[P2] */
13961 #define OP_Delete 119
13962 #define OP_ResetCount 120
13963 #define OP_SorterCompare 121 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
13964 #define OP_SorterData 122 /* synopsis: r[P2]=data */
13965 #define OP_RowData 123 /* synopsis: r[P2]=data */
13966 #define OP_Rowid 124 /* synopsis: r[P2]=rowid */
13967 #define OP_NullRow 125
13968 #define OP_SeekEnd 126
13969 #define OP_SorterInsert 127 /* synopsis: key=r[P2] */
13970 #define OP_IdxInsert 128 /* synopsis: key=r[P2] */
13971 #define OP_IdxDelete 129 /* synopsis: key=r[P2@P3] */
13972 #define OP_DeferredSeek 130 /* synopsis: Move P3 to P1.rowid if needed */
13973 #define OP_IdxRowid 131 /* synopsis: r[P2]=rowid */
13974 #define OP_Real 132 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
13975 #define OP_Destroy 133
13976 #define OP_Clear 134
13977 #define OP_ResetSorter 135
13978 #define OP_CreateBtree 136 /* synopsis: r[P2]=root iDb=P1 flags=P3 */
13979 #define OP_SqlExec 137
13980 #define OP_ParseSchema 138
13981 #define OP_LoadAnalysis 139
13982 #define OP_DropTable 140
13983 #define OP_DropIndex 141
13984 #define OP_DropTrigger 142
13985 #define OP_IntegrityCk 143
13986 #define OP_RowSetAdd 144 /* synopsis: rowset(P1)=r[P2] */
13987 #define OP_Param 145
13988 #define OP_FkCounter 146 /* synopsis: fkctr[P1]+=P2 */
13989 #define OP_MemMax 147 /* synopsis: r[P1]=max(r[P1],r[P2]) */
13990 #define OP_OffsetLimit 148 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
13991 #define OP_AggStep0 149 /* synopsis: accum=r[P3] step(r[P2@P5]) */
13992 #define OP_AggStep 150 /* synopsis: accum=r[P3] step(r[P2@P5]) */
13993 #define OP_AggFinal 151 /* synopsis: accum=r[P1] N=P2 */
13994 #define OP_Expire 152
13995 #define OP_TableLock 153 /* synopsis: iDb=P1 root=P2 write=P3 */
13996 #define OP_VBegin 154
13997 #define OP_VCreate 155
13998 #define OP_VDestroy 156
13999 #define OP_VOpen 157
14000 #define OP_VColumn 158 /* synopsis: r[P3]=vcolumn(P2) */
14001 #define OP_VRename 159
14002 #define OP_Pagecount 160
14003 #define OP_MaxPgcnt 161
14004 #define OP_PureFunc0 162
14005 #define OP_Function0 163 /* synopsis: r[P3]=func(r[P2@P5]) */
14006 #define OP_PureFunc 164
14007 #define OP_Function 165 /* synopsis: r[P3]=func(r[P2@P5]) */
14008 #define OP_Trace 166
14009 #define OP_CursorHint 167
14010 #define OP_Noop 168
14011 #define OP_Explain 169
 
14012
14013 /* Properties such as "out2" or "jump" that are specified in
14014 ** comments following the "case" for each opcode in the vdbe.c
14015 ** are encoded into bitvectors as follows:
14016 */
@@ -14031,20 +14137,20 @@
14031 /* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x01, 0x01, 0x01, 0x02,\
14032 /* 64 */ 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00,\
14033 /* 72 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
14034 /* 80 */ 0x02, 0x02, 0x02, 0x00, 0x26, 0x26, 0x26, 0x26,\
14035 /* 88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
14036 /* 96 */ 0x20, 0x10, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
14037 /* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14038 /* 112 */ 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00,\
14039 /* 120 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x04,\
14040 /* 128 */ 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00,\
14041 /* 136 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14042 /* 144 */ 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00,\
14043 /* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14044 /* 160 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14045 /* 168 */ 0x00, 0x00,}
14046
14047 /* The sqlite3P2Values() routine is able to run faster if it knows
14048 ** the value of the largest JUMP opcode. The smaller the maximum
14049 ** JUMP opcode the better, so the mkopcodeh.tcl script that
14050 ** generated this include file strives to group all JUMP opcodes
@@ -15246,12 +15352,13 @@
15246 int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
15247 struct sqlite3InitInfo { /* Information used during initialization */
15248 int newTnum; /* Rootpage of table being initialized */
15249 u8 iDb; /* Which db file is being initialized */
15250 u8 busy; /* TRUE if currently initializing */
15251 u8 orphanTrigger; /* Last statement is orphaned TEMP trigger */
15252 u8 imposterTable; /* Building an imposter table */
 
15253 } init;
15254 int nVdbeActive; /* Number of VDBEs currently running */
15255 int nVdbeRead; /* Number of active VDBEs that read or write */
15256 int nVdbeWrite; /* Number of active VDBEs that read and write */
15257 int nVdbeExec; /* Number of nested calls to VdbeExec() */
@@ -15635,10 +15742,11 @@
15635 /* Allowed values for Column.colFlags:
15636 */
15637 #define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
15638 #define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */
15639 #define COLFLAG_HASTYPE 0x0004 /* Type name follows column name */
 
15640
15641 /*
15642 ** A "Collating Sequence" is defined by an instance of the following
15643 ** structure. Conceptually, a collating sequence consists of a name and
15644 ** a comparison routine that defines the order of that sequence.
@@ -17719,10 +17827,12 @@
17719 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
17720 SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
17721 SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
17722 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
17723 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
 
 
17724 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
17725 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
17726 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
17727 SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
17728 SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
@@ -17900,10 +18010,14 @@
17900 SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
17901
17902 #if defined(SQLITE_NEED_ERR_NAME)
17903 SQLITE_PRIVATE const char *sqlite3ErrName(int);
17904 #endif
 
 
 
 
17905
17906 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
17907 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
17908 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
17909 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
@@ -17949,10 +18063,13 @@
17949 SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
17950 #ifndef SQLITE_OMIT_WSD
17951 SQLITE_PRIVATE int sqlite3PendingByte;
17952 #endif
17953 #endif
 
 
 
17954 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
17955 SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
17956 SQLITE_PRIVATE void sqlite3AlterFunctions(void);
17957 SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
17958 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
@@ -18572,10 +18689,17 @@
18572 SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
18573 { "0", 1 },
18574 { "1", 1 }
18575 };
18576
 
 
 
 
 
 
 
18577
18578 /*
18579 ** The value of the "pending" byte must be 0x40000000 (1 byte past the
18580 ** 1-gibabyte boundary) in a compatible database. SQLite never uses
18581 ** the database page that contains the pending byte. It never attempts
@@ -18855,12 +18979,10 @@
18855 ** representations of the value stored in the Mem struct.
18856 **
18857 ** If the MEM_Null flag is set, then the value is an SQL NULL value.
18858 ** For a pointer type created using sqlite3_bind_pointer() or
18859 ** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
18860 ** If both MEM_Null and MEM_Zero are set, that means that the value is
18861 ** an unchanging column value from VColumn.
18862 **
18863 ** If the MEM_Str flag is set then Mem.z points at a string representation.
18864 ** Usually this is encoded in the same unicode encoding as the main
18865 ** database (see below for exceptions). If the MEM_Term flag is also
18866 ** set, then the string is nul terminated. The MEM_Int and MEM_Real
@@ -18950,11 +19072,10 @@
18950 Mem *pMem; /* Memory cell used to store aggregate context */
18951 Vdbe *pVdbe; /* The VM that owns this context */
18952 int iOp; /* Instruction number of OP_Function */
18953 int isError; /* Error code returned by the function. */
18954 u8 skipFlag; /* Skip accumulator loading if true */
18955 u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */
18956 u8 argc; /* Number of arguments */
18957 sqlite3_value *argv[1]; /* Argument set */
18958 };
18959
18960 /* A bitfield type for use inside of structures. Always follow with :N where
@@ -19120,10 +19241,11 @@
19120 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
19121 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
19122 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
19123 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
19124 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
 
19125 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
19126 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
19127 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
19128 SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
19129 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
@@ -19513,10 +19635,13 @@
19513 /*
19514 ** Set *pCurrent to the total cache hits or misses encountered by all
19515 ** pagers the database handle is connected to. *pHighwater is always set
19516 ** to zero.
19517 */
 
 
 
19518 case SQLITE_DBSTATUS_CACHE_HIT:
19519 case SQLITE_DBSTATUS_CACHE_MISS:
19520 case SQLITE_DBSTATUS_CACHE_WRITE:{
19521 int i;
19522 int nRet = 0;
@@ -24106,15 +24231,16 @@
24106 volatile pthread_t owner; /* Thread that is within this mutex */
24107 int trace; /* True to trace changes */
24108 #endif
24109 };
24110 #if SQLITE_MUTEX_NREF
24111 #define SQLITE3_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER,0,0,(pthread_t)0,0}
 
24112 #elif defined(SQLITE_ENABLE_API_ARMOR)
24113 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0 }
24114 #else
24115 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
24116 #endif
24117
24118 /*
24119 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
24120 ** intended for use only inside assert() statements. On some platforms,
@@ -24207,22 +24333,22 @@
24207 ** mutex types, the same mutex is returned on every call that has
24208 ** the same type number.
24209 */
24210 static sqlite3_mutex *pthreadMutexAlloc(int iType){
24211 static sqlite3_mutex staticMutexes[] = {
24212 SQLITE3_MUTEX_INITIALIZER,
24213 SQLITE3_MUTEX_INITIALIZER,
24214 SQLITE3_MUTEX_INITIALIZER,
24215 SQLITE3_MUTEX_INITIALIZER,
24216 SQLITE3_MUTEX_INITIALIZER,
24217 SQLITE3_MUTEX_INITIALIZER,
24218 SQLITE3_MUTEX_INITIALIZER,
24219 SQLITE3_MUTEX_INITIALIZER,
24220 SQLITE3_MUTEX_INITIALIZER,
24221 SQLITE3_MUTEX_INITIALIZER,
24222 SQLITE3_MUTEX_INITIALIZER,
24223 SQLITE3_MUTEX_INITIALIZER
24224 };
24225 sqlite3_mutex *p;
24226 switch( iType ){
24227 case SQLITE_MUTEX_RECURSIVE: {
24228 p = sqlite3MallocZero( sizeof(*p) );
@@ -24237,17 +24363,23 @@
24237 pthread_mutexattr_init(&recursiveAttr);
24238 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
24239 pthread_mutex_init(&p->mutex, &recursiveAttr);
24240 pthread_mutexattr_destroy(&recursiveAttr);
24241 #endif
 
 
 
24242 }
24243 break;
24244 }
24245 case SQLITE_MUTEX_FAST: {
24246 p = sqlite3MallocZero( sizeof(*p) );
24247 if( p ){
24248 pthread_mutex_init(&p->mutex, 0);
 
 
 
24249 }
24250 break;
24251 }
24252 default: {
24253 #ifdef SQLITE_ENABLE_API_ARMOR
@@ -24259,11 +24391,11 @@
24259 p = &staticMutexes[iType-2];
24260 break;
24261 }
24262 }
24263 #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
24264 if( p ) p->id = iType;
24265 #endif
24266 return p;
24267 }
24268
24269
@@ -24776,11 +24908,11 @@
24776 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
24777 int id; /* Mutex type */
24778 #ifdef SQLITE_DEBUG
24779 volatile int nRef; /* Number of enterances */
24780 volatile DWORD owner; /* Thread holding this mutex */
24781 volatile int trace; /* True to trace changes */
24782 #endif
24783 };
24784
24785 /*
24786 ** These are the initializer values used when declaring a "static" mutex
@@ -24788,14 +24920,14 @@
24788 ** on the Win32 platform.
24789 */
24790 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
24791
24792 #ifdef SQLITE_DEBUG
24793 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
24794 0L, (DWORD)0, 0 }
24795 #else
24796 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
24797 #endif
24798
24799 #ifdef SQLITE_DEBUG
24800 /*
24801 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
@@ -24834,22 +24966,22 @@
24834
24835 /*
24836 ** Initialize and deinitialize the mutex subsystem.
24837 */
24838 static sqlite3_mutex winMutex_staticMutexes[] = {
24839 SQLITE3_MUTEX_INITIALIZER,
24840 SQLITE3_MUTEX_INITIALIZER,
24841 SQLITE3_MUTEX_INITIALIZER,
24842 SQLITE3_MUTEX_INITIALIZER,
24843 SQLITE3_MUTEX_INITIALIZER,
24844 SQLITE3_MUTEX_INITIALIZER,
24845 SQLITE3_MUTEX_INITIALIZER,
24846 SQLITE3_MUTEX_INITIALIZER,
24847 SQLITE3_MUTEX_INITIALIZER,
24848 SQLITE3_MUTEX_INITIALIZER,
24849 SQLITE3_MUTEX_INITIALIZER,
24850 SQLITE3_MUTEX_INITIALIZER
24851 };
24852
24853 static int winMutex_isInit = 0;
24854 static int winMutex_isNt = -1; /* <0 means "need to query" */
24855
@@ -24975,19 +25107,19 @@
24975 (void)SQLITE_MISUSE_BKPT;
24976 return 0;
24977 }
24978 #endif
24979 p = &winMutex_staticMutexes[iType-2];
24980 p->id = iType;
24981 #ifdef SQLITE_DEBUG
24982 #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
24983 p->trace = 1;
24984 #endif
24985 #endif
24986 break;
24987 }
24988 }
 
24989 return p;
24990 }
24991
24992
24993 /*
@@ -26062,10 +26194,15 @@
26062 etByte flag_rtz; /* True if trailing zeros should be removed */
26063 #endif
26064 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
26065 char buf[etBUFSIZE]; /* Conversion buffer */
26066
 
 
 
 
 
26067 bufpt = 0;
26068 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
26069 pArgList = va_arg(ap, PrintfArguments*);
26070 bArgList = 1;
26071 }else{
@@ -26480,26 +26617,56 @@
26480 length = 1;
26481 break;
26482 case etCHARX:
26483 if( bArgList ){
26484 bufpt = getTextArg(pArgList);
26485 c = bufpt ? bufpt[0] : 0;
 
 
 
 
 
 
 
 
 
 
26486 }else{
26487 c = va_arg(ap,int);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26488 }
26489 if( precision>1 ){
26490 width -= precision-1;
26491 if( width>1 && !flag_leftjustify ){
26492 sqlite3AppendChar(pAccum, width-1, ' ');
26493 width = 0;
26494 }
26495 sqlite3AppendChar(pAccum, precision-1, c);
 
 
26496 }
26497 length = 1;
26498 buf[0] = c;
26499 bufpt = buf;
26500 break;
 
26501 case etSTRING:
26502 case etDYNSTRING:
26503 if( bArgList ){
26504 bufpt = getTextArg(pArgList);
26505 xtype = etSTRING;
@@ -26507,21 +26674,49 @@
26507 bufpt = va_arg(ap,char*);
26508 }
26509 if( bufpt==0 ){
26510 bufpt = "";
26511 }else if( xtype==etDYNSTRING ){
 
 
 
 
 
 
 
 
 
 
 
 
26512 zExtra = bufpt;
26513 }
26514 if( precision>=0 ){
26515 for(length=0; length<precision && bufpt[length]; length++){}
 
 
 
 
 
 
 
 
 
 
26516 }else{
26517 length = 0x7fffffff & (int)strlen(bufpt);
26518 }
 
 
 
 
 
 
26519 break;
26520 case etSQLESCAPE: /* Escape ' characters */
26521 case etSQLESCAPE2: /* Escape ' and enclose in '...' */
26522 case etSQLESCAPE3: { /* Escape " characters */
26523 int i, j, k, n, isnull;
26524 int needQuote;
26525 char ch;
26526 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
26527 char *escarg;
@@ -26531,13 +26726,21 @@
26531 }else{
26532 escarg = va_arg(ap,char*);
26533 }
26534 isnull = escarg==0;
26535 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
 
 
 
 
 
26536 k = precision;
26537 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
26538 if( ch==q ) n++;
 
 
 
26539 }
26540 needQuote = !isnull && xtype==etSQLESCAPE2;
26541 n += i + 3;
26542 if( n>etBUFSIZE ){
26543 bufpt = zExtra = sqlite3Malloc( n );
@@ -26556,14 +26759,11 @@
26556 if( ch==q ) bufpt[j++] = ch;
26557 }
26558 if( needQuote ) bufpt[j++] = q;
26559 bufpt[j] = 0;
26560 length = j;
26561 /* The precision in %q and %Q means how many input characters to
26562 ** consume, not the length of the output...
26563 ** if( precision>=0 && precision<length ) length = precision; */
26564 break;
26565 }
26566 case etTOKEN: {
26567 Token *pToken;
26568 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
26569 pToken = va_arg(ap, Token*);
@@ -26598,11 +26798,14 @@
26598 }
26599 }/* End switch over the format type */
26600 /*
26601 ** The text of the conversion is pointed to by "bufpt" and is
26602 ** "length" characters long. The field width is "width". Do
26603 ** the output.
 
 
 
26604 */
26605 width -= length;
26606 if( width>0 ){
26607 if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
26608 sqlite3StrAccumAppend(pAccum, bufpt, length);
@@ -27267,10 +27470,15 @@
27267 break;
27268 }
27269 case TK_NULL: {
27270 sqlite3TreeViewLine(pView,"NULL");
27271 break;
 
 
 
 
 
27272 }
27273 #ifndef SQLITE_OMIT_BLOB_LITERAL
27274 case TK_BLOB: {
27275 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
27276 break;
@@ -27323,10 +27531,23 @@
27323 case TK_UPLUS: zUniOp = "UPLUS"; break;
27324 case TK_BITNOT: zUniOp = "BITNOT"; break;
27325 case TK_NOT: zUniOp = "NOT"; break;
27326 case TK_ISNULL: zUniOp = "ISNULL"; break;
27327 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
 
 
 
 
 
 
 
 
 
 
 
 
 
27328
27329 case TK_SPAN: {
27330 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
27331 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
27332 break;
@@ -29061,11 +29282,11 @@
29061 ** routine does *not* accept hexadecimal notation.
29062 **
29063 ** Returns:
29064 **
29065 ** 0 Successful transformation. Fits in a 64-bit signed integer.
29066 ** 1 Excess text after the integer value
29067 ** 2 Integer too large for a 64-bit signed integer or is malformed
29068 ** 3 Special case of 9223372036854775808
29069 **
29070 ** length is the number of bytes in the string (bytes, not characters).
29071 ** The string is not necessarily zero-terminated. The encoding is
@@ -29104,51 +29325,61 @@
29104 zStart = zNum;
29105 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
29106 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
29107 u = u*10 + c - '0';
29108 }
 
 
 
29109 if( u>LARGEST_INT64 ){
 
 
 
 
29110 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
29111 }else if( neg ){
29112 *pNum = -(i64)u;
29113 }else{
29114 *pNum = (i64)u;
29115 }
29116 testcase( i==18 );
29117 testcase( i==19 );
29118 testcase( i==20 );
29119 if( &zNum[i]<zEnd /* Extra bytes at the end */
29120 || (i==0 && zStart==zNum) /* No digits */
29121 || nonNum /* UTF16 with high-order bytes non-zero */
29122 ){
29123 rc = 1;
29124 }else{
29125 rc = 0;
29126 }
29127 if( i>19*incr ){ /* Too many digits */
29128 /* zNum is empty or contains non-numeric text or is longer
29129 ** than 19 digits (thus guaranteeing that it is too large) */
29130 return 2;
29131 }else if( i<19*incr ){
 
 
 
29132 /* Less than 19 digits, so we know that it fits in 64 bits */
29133 assert( u<=LARGEST_INT64 );
29134 return rc;
29135 }else{
29136 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
29137 c = compare2pow63(zNum, incr);
29138 if( c<0 ){
29139 /* zNum is less than 9223372036854775808 so it fits */
29140 assert( u<=LARGEST_INT64 );
29141 return rc;
29142 }else if( c>0 ){
29143 /* zNum is greater than 9223372036854775808 so it overflows */
29144 return 2;
29145 }else{
29146 /* zNum is exactly 9223372036854775808. Fits if negative. The
29147 ** special case 2 overflow if positive */
29148 assert( u-1==LARGEST_INT64 );
29149 return neg ? rc : 3;
 
 
 
 
 
 
29150 }
29151 }
29152 }
29153
29154 /*
@@ -30461,84 +30692,85 @@
30461 /* 91 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
30462 /* 92 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
30463 /* 93 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
30464 /* 94 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
30465 /* 95 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
30466 /* 96 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"),
30467 /* 97 */ "String8" OpHelp("r[P2]='P4'"),
30468 /* 98 */ "Column" OpHelp("r[P3]=PX"),
30469 /* 99 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
30470 /* 100 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
30471 /* 101 */ "Count" OpHelp("r[P2]=count()"),
30472 /* 102 */ "ReadCookie" OpHelp(""),
30473 /* 103 */ "SetCookie" OpHelp(""),
30474 /* 104 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
30475 /* 105 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
30476 /* 106 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
30477 /* 107 */ "OpenDup" OpHelp(""),
30478 /* 108 */ "OpenAutoindex" OpHelp("nColumn=P2"),
30479 /* 109 */ "OpenEphemeral" OpHelp("nColumn=P2"),
30480 /* 110 */ "SorterOpen" OpHelp(""),
30481 /* 111 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
30482 /* 112 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
30483 /* 113 */ "Close" OpHelp(""),
30484 /* 114 */ "ColumnsUsed" OpHelp(""),
30485 /* 115 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
30486 /* 116 */ "NewRowid" OpHelp("r[P2]=rowid"),
30487 /* 117 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
30488 /* 118 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
30489 /* 119 */ "Delete" OpHelp(""),
30490 /* 120 */ "ResetCount" OpHelp(""),
30491 /* 121 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
30492 /* 122 */ "SorterData" OpHelp("r[P2]=data"),
30493 /* 123 */ "RowData" OpHelp("r[P2]=data"),
30494 /* 124 */ "Rowid" OpHelp("r[P2]=rowid"),
30495 /* 125 */ "NullRow" OpHelp(""),
30496 /* 126 */ "SeekEnd" OpHelp(""),
30497 /* 127 */ "SorterInsert" OpHelp("key=r[P2]"),
30498 /* 128 */ "IdxInsert" OpHelp("key=r[P2]"),
30499 /* 129 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
30500 /* 130 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"),
30501 /* 131 */ "IdxRowid" OpHelp("r[P2]=rowid"),
30502 /* 132 */ "Real" OpHelp("r[P2]=P4"),
30503 /* 133 */ "Destroy" OpHelp(""),
30504 /* 134 */ "Clear" OpHelp(""),
30505 /* 135 */ "ResetSorter" OpHelp(""),
30506 /* 136 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"),
30507 /* 137 */ "SqlExec" OpHelp(""),
30508 /* 138 */ "ParseSchema" OpHelp(""),
30509 /* 139 */ "LoadAnalysis" OpHelp(""),
30510 /* 140 */ "DropTable" OpHelp(""),
30511 /* 141 */ "DropIndex" OpHelp(""),
30512 /* 142 */ "DropTrigger" OpHelp(""),
30513 /* 143 */ "IntegrityCk" OpHelp(""),
30514 /* 144 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
30515 /* 145 */ "Param" OpHelp(""),
30516 /* 146 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
30517 /* 147 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
30518 /* 148 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
30519 /* 149 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"),
30520 /* 150 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
30521 /* 151 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
30522 /* 152 */ "Expire" OpHelp(""),
30523 /* 153 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
30524 /* 154 */ "VBegin" OpHelp(""),
30525 /* 155 */ "VCreate" OpHelp(""),
30526 /* 156 */ "VDestroy" OpHelp(""),
30527 /* 157 */ "VOpen" OpHelp(""),
30528 /* 158 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
30529 /* 159 */ "VRename" OpHelp(""),
30530 /* 160 */ "Pagecount" OpHelp(""),
30531 /* 161 */ "MaxPgcnt" OpHelp(""),
30532 /* 162 */ "PureFunc0" OpHelp(""),
30533 /* 163 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
30534 /* 164 */ "PureFunc" OpHelp(""),
30535 /* 165 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
30536 /* 166 */ "Trace" OpHelp(""),
30537 /* 167 */ "CursorHint" OpHelp(""),
30538 /* 168 */ "Noop" OpHelp(""),
30539 /* 169 */ "Explain" OpHelp(""),
 
30540 };
30541 return azName[i];
30542 }
30543 #endif
30544
@@ -31210,11 +31442,15 @@
31210 #else
31211 { "fchown", (sqlite3_syscall_ptr)0, 0 },
31212 #endif
31213 #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
31214
 
31215 { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 },
 
 
 
31216 #define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent)
31217
31218 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
31219 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
31220 #else
@@ -31438,19 +31674,20 @@
31438 **
31439 ** unixEnterMutex()
31440 ** assert( unixMutexHeld() );
31441 ** unixEnterLeave()
31442 */
 
31443 static void unixEnterMutex(void){
31444 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
31445 }
31446 static void unixLeaveMutex(void){
31447 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
31448 }
31449 #ifdef SQLITE_DEBUG
31450 static int unixMutexHeld(void) {
31451 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
31452 }
31453 #endif
31454
31455
31456 #ifdef SQLITE_HAVE_OS_TRACE
@@ -34917,16 +35154,14 @@
34917 /* Locks are within range */
34918 assert( n>=1 && n<=SQLITE_SHM_NLOCK );
34919
34920 if( pShmNode->h>=0 ){
34921 /* Initialize the locking parameters */
34922 memset(&f, 0, sizeof(f));
34923 f.l_type = lockType;
34924 f.l_whence = SEEK_SET;
34925 f.l_start = ofst;
34926 f.l_len = n;
34927
34928 rc = osFcntl(pShmNode->h, F_SETLK, &f);
34929 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
34930 }
34931
34932 /* Update the global lock state and do debug tracing */
@@ -36588,11 +36823,10 @@
36588 */
36589 if( randomnessPid!=osGetpid(0) ){
36590 randomnessPid = osGetpid(0);
36591 sqlite3_randomness(0,0);
36592 }
36593
36594 memset(p, 0, sizeof(unixFile));
36595
36596 if( eType==SQLITE_OPEN_MAIN_DB ){
36597 UnixUnusedFd *pUnused;
36598 pUnused = findReusableFd(zName, flags);
@@ -38463,10 +38697,11 @@
38463
38464 /* Register all VFSes defined in the aVfs[] array */
38465 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
38466 sqlite3_vfs_register(&aVfs[i], i==0);
38467 }
 
38468 return SQLITE_OK;
38469 }
38470
38471 /*
38472 ** Shutdown the operating system interface.
@@ -38474,10 +38709,11 @@
38474 ** Some operating systems might need to do some cleanup in this routine,
38475 ** to release dynamically allocated objects. But not on unix.
38476 ** This routine is a no-op for unix.
38477 */
38478 SQLITE_API int sqlite3_os_end(void){
 
38479 return SQLITE_OK;
38480 }
38481
38482 #endif /* SQLITE_OS_UNIX */
38483
@@ -42312,19 +42548,20 @@
42312 **
42313 ** winShmEnterMutex()
42314 ** assert( winShmMutexHeld() );
42315 ** winShmLeaveMutex()
42316 */
 
42317 static void winShmEnterMutex(void){
42318 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
42319 }
42320 static void winShmLeaveMutex(void){
42321 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
42322 }
42323 #ifndef NDEBUG
42324 static int winShmMutexHeld(void) {
42325 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
42326 }
42327 #endif
42328
42329 /*
42330 ** Object used to represent a single file opened and mmapped to provide
@@ -44742,10 +44979,14 @@
44742 sqlite3_vfs_register(&winNolockVfs, 0);
44743
44744 #if defined(SQLITE_WIN32_HAS_WIDE)
44745 sqlite3_vfs_register(&winLongPathNolockVfs, 0);
44746 #endif
 
 
 
 
44747
44748 return SQLITE_OK;
44749 }
44750
44751 SQLITE_API int sqlite3_os_end(void){
@@ -44753,16 +44994,613 @@
44753 if( sleepObj!=NULL ){
44754 osCloseHandle(sleepObj);
44755 sleepObj = NULL;
44756 }
44757 #endif
 
 
 
 
 
44758 return SQLITE_OK;
44759 }
44760
44761 #endif /* SQLITE_OS_WIN */
44762
44763 /************** End of os_win.c **********************************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44764 /************** Begin file bitvec.c ******************************************/
44765 /*
44766 ** 2008 February 16
44767 **
44768 ** The author disclaims copyright to this source code. In place of
@@ -45607,11 +46445,11 @@
45607 int rc;
45608 #ifdef SQLITE_LOG_CACHE_SPILL
45609 sqlite3_log(SQLITE_FULL,
45610 "spill page %d making room for %d - cache used: %d/%d",
45611 pPg->pgno, pgno,
45612 sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
45613 numberOfCachePages(pCache));
45614 #endif
45615 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
45616 rc = pCache->xStress(pCache->pStress, pPg);
45617 pcacheDump(pCache);
@@ -48663,11 +49501,11 @@
48663 i64 journalSizeLimit; /* Size limit for persistent journal files */
48664 char *zFilename; /* Name of the database file */
48665 char *zJournal; /* Name of the journal file */
48666 int (*xBusyHandler)(void*); /* Function to call when busy */
48667 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
48668 int aStat[3]; /* Total cache hits, misses and writes */
48669 #ifdef SQLITE_TEST
48670 int nRead; /* Database pages read */
48671 #endif
48672 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
48673 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
@@ -48691,10 +49529,11 @@
48691 ** or CACHE_WRITE to sqlite3_db_status().
48692 */
48693 #define PAGER_STAT_HIT 0
48694 #define PAGER_STAT_MISS 1
48695 #define PAGER_STAT_WRITE 2
 
48696
48697 /*
48698 ** The following global variables hold counters used for
48699 ** testing purposes only. These variables do not exist in
48700 ** a non-testing build. These variables are not thread-safe.
@@ -49177,11 +50016,11 @@
49177 #else
49178 UNUSED_PARAMETER(pPager);
49179 #endif
49180
49181 #ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
49182 if( dc&SQLITE_IOCAP_BATCH_ATOMIC ){
49183 return -1;
49184 }
49185 #endif
49186
49187 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
@@ -52066,10 +52905,34 @@
52066 pNext = p->pDirty;
52067 sqlite3_free(p);
52068 }
52069 }
52070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52071
52072 /*
52073 ** Shutdown the page cache. Free all memory and close all files.
52074 **
52075 ** If a transaction was in progress when this routine is called, that
@@ -52082,25 +52945,30 @@
52082 ** is made to roll it back. If an error occurs during the rollback
52083 ** a hot journal may be left in the filesystem but no error is returned
52084 ** to the caller.
52085 */
52086 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
52087 u8 *pTmp = (u8 *)pPager->pTmpSpace;
52088
52089 assert( db || pagerUseWal(pPager)==0 );
52090 assert( assert_pager_state(pPager) );
52091 disable_simulated_io_errors();
52092 sqlite3BeginBenignMalloc();
52093 pagerFreeMapHdrs(pPager);
52094 /* pPager->errCode = 0; */
52095 pPager->exclusiveMode = 0;
52096 #ifndef SQLITE_OMIT_WAL
52097 assert( db || pPager->pWal==0 );
52098 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,
52099 (db && (db->flags & SQLITE_NoCkptOnClose) ? 0 : pTmp)
52100 );
52101 pPager->pWal = 0;
 
 
 
 
 
 
52102 #endif
52103 pager_reset(pPager);
52104 if( MEMDB ){
52105 pager_unlock(pPager);
52106 }else{
@@ -52553,10 +53421,11 @@
52553 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
52554 ){
52555 return SQLITE_OK;
52556 }
52557
 
52558 pPg->pDirty = 0;
52559 if( pagerUseWal(pPager) ){
52560 /* Write a single frame for this page to the log. */
52561 rc = subjournalPageIfRequired(pPg);
52562 if( rc==SQLITE_OK ){
@@ -52658,10 +53527,15 @@
52658 u8 *pPtr;
52659 Pager *pPager = 0; /* Pager object to allocate and return */
52660 int rc = SQLITE_OK; /* Return code */
52661 int tempFile = 0; /* True for temp files (incl. in-memory files) */
52662 int memDb = 0; /* True if this is an in-memory file */
 
 
 
 
 
52663 int readOnly = 0; /* True if this is a read-only file */
52664 int journalFileSize; /* Bytes to allocate for each journal fd */
52665 char *zPathname = 0; /* Full path to database file */
52666 int nPathname = 0; /* Number of bytes in zPathname */
52667 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
@@ -52785,11 +53659,14 @@
52785 */
52786 if( zFilename && zFilename[0] ){
52787 int fout = 0; /* VFS flags returned by xOpen() */
52788 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
52789 assert( !memDb );
52790 readOnly = (fout&SQLITE_OPEN_READONLY);
 
 
 
52791
52792 /* If the file was successfully opened for read/write access,
52793 ** choose a default page size in case we have to create the
52794 ** database file. The default page size is the maximum of:
52795 **
@@ -52916,11 +53793,11 @@
52916 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
52917 assert( isOpen(pPager->fd) || tempFile );
52918 setSectorSize(pPager);
52919 if( !useJournal ){
52920 pPager->journalMode = PAGER_JOURNALMODE_OFF;
52921 }else if( memDb ){
52922 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
52923 }
52924 /* pPager->xBusyHandler = 0; */
52925 /* pPager->pBusyHandlerArg = 0; */
52926 pPager->xReiniter = xReinit;
@@ -52931,34 +53808,10 @@
52931 *ppPager = pPager;
52932 return SQLITE_OK;
52933 }
52934
52935
52936 /* Verify that the database file has not be deleted or renamed out from
52937 ** under the pager. Return SQLITE_OK if the database is still were it ought
52938 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
52939 ** code from sqlite3OsAccess()) if the database has gone missing.
52940 */
52941 static int databaseIsUnmoved(Pager *pPager){
52942 int bHasMoved = 0;
52943 int rc;
52944
52945 if( pPager->tempFile ) return SQLITE_OK;
52946 if( pPager->dbSize==0 ) return SQLITE_OK;
52947 assert( pPager->zFilename && pPager->zFilename[0] );
52948 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
52949 if( rc==SQLITE_NOTFOUND ){
52950 /* If the HAS_MOVED file-control is unimplemented, assume that the file
52951 ** has not been moved. That is the historical behavior of SQLite: prior to
52952 ** version 3.8.3, it never checked */
52953 rc = SQLITE_OK;
52954 }else if( rc==SQLITE_OK && bHasMoved ){
52955 rc = SQLITE_READONLY_DBMOVED;
52956 }
52957 return rc;
52958 }
52959
52960
52961 /*
52962 ** This function is called after transitioning from PAGER_UNLOCK to
52963 ** PAGER_SHARED state. It tests if there is a hot journal present in
52964 ** the file-system for the given pager. A hot journal is one that
@@ -54463,12 +55316,13 @@
54463 }
54464 rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
54465 if( bBatch ){
54466 if( rc==SQLITE_OK ){
54467 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
54468 }else{
54469 sqlite3OsFileControl(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
 
54470 }
54471 }
54472
54473 if( rc!=SQLITE_OK ){
54474 assert( rc!=SQLITE_IOERR_BLOCKED );
@@ -54688,30 +55542,37 @@
54688 return a;
54689 }
54690 #endif
54691
54692 /*
54693 ** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
54694 ** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
 
 
 
 
54695 ** current cache hit or miss count, according to the value of eStat. If the
54696 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
54697 ** returning.
54698 */
54699 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
54700
54701 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
54702 || eStat==SQLITE_DBSTATUS_CACHE_MISS
54703 || eStat==SQLITE_DBSTATUS_CACHE_WRITE
 
54704 );
54705
54706 assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
54707 assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
54708 assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
 
54709
54710 *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
 
54711 if( reset ){
54712 pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
54713 }
54714 }
54715
54716 /*
54717 ** Return true if this is an in-memory or temp-file backed pager.
@@ -56153,11 +57014,15 @@
56153 **
56154 ** If this call is successful, *ppPage is set to point to the wal-index
56155 ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
56156 ** then an SQLite error code is returned and *ppPage is set to 0.
56157 */
56158 static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
 
 
 
 
56159 int rc = SQLITE_OK;
56160
56161 /* Enlarge the pWal->apWiData[] array if required */
56162 if( pWal->nWiData<=iPage ){
56163 int nByte = sizeof(u32*)*(iPage+1);
@@ -56172,32 +57037,41 @@
56172 pWal->apWiData = apNew;
56173 pWal->nWiData = iPage+1;
56174 }
56175
56176 /* Request a pointer to the required page from the VFS */
56177 if( pWal->apWiData[iPage]==0 ){
56178 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
56179 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
56180 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
56181 }else{
56182 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
56183 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
56184 );
56185 assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
56186 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
56187 if( (rc&0xff)==SQLITE_READONLY ){
56188 pWal->readOnly |= WAL_SHM_RDONLY;
56189 if( rc==SQLITE_READONLY ){
56190 rc = SQLITE_OK;
56191 }
56192 }
56193 }
56194 }
56195
56196 *ppPage = pWal->apWiData[iPage];
56197 assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
56198 return rc;
 
 
 
 
 
 
 
 
 
 
56199 }
56200
56201 /*
56202 ** Return a pointer to the WalCkptInfo structure in the wal-index.
56203 */
@@ -57171,21 +58045,22 @@
57171 sqlite3_free(p);
57172 }
57173
57174 /*
57175 ** Construct a WalInterator object that can be used to loop over all
57176 ** pages in the WAL in ascending order. The caller must hold the checkpoint
57177 ** lock.
 
57178 **
57179 ** On success, make *pp point to the newly allocated WalInterator object
57180 ** return SQLITE_OK. Otherwise, return an error code. If this routine
57181 ** returns an error, the value of *pp is undefined.
57182 **
57183 ** The calling routine should invoke walIteratorFree() to destroy the
57184 ** WalIterator object when it has finished with it.
57185 */
57186 static int walIteratorInit(Wal *pWal, WalIterator **pp){
57187 WalIterator *p; /* Return value */
57188 int nSegment; /* Number of segments to merge */
57189 u32 iLast; /* Last frame in log */
57190 int nByte; /* Number of bytes to allocate */
57191 int i; /* Iterator variable */
@@ -57218,11 +58093,11 @@
57218 );
57219 if( !aTmp ){
57220 rc = SQLITE_NOMEM_BKPT;
57221 }
57222
57223 for(i=0; rc==SQLITE_OK && i<nSegment; i++){
57224 volatile ht_slot *aHash;
57225 u32 iZero;
57226 volatile u32 *aPgno;
57227
57228 rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
@@ -57252,10 +58127,11 @@
57252 }
57253 sqlite3_free(aTmp);
57254
57255 if( rc!=SQLITE_OK ){
57256 walIteratorFree(p);
 
57257 }
57258 *pp = p;
57259 return rc;
57260 }
57261
@@ -57374,17 +58250,10 @@
57374 testcase( szPage<=32768 );
57375 testcase( szPage>=65536 );
57376 pInfo = walCkptInfo(pWal);
57377 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
57378
57379 /* Allocate the iterator */
57380 rc = walIteratorInit(pWal, &pIter);
57381 if( rc!=SQLITE_OK ){
57382 return rc;
57383 }
57384 assert( pIter );
57385
57386 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
57387 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
57388 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
57389
57390 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
@@ -57417,11 +58286,17 @@
57417 goto walcheckpoint_out;
57418 }
57419 }
57420 }
57421
57422 if( pInfo->nBackfill<mxSafeFrame
 
 
 
 
 
 
57423 && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK
57424 ){
57425 i64 nSize; /* Current size of database file */
57426 u32 nBackfill = pInfo->nBackfill;
57427
@@ -58467,11 +59342,11 @@
58467 ** (iFrame<=iLast):
58468 ** This condition filters out entries that were added to the hash
58469 ** table after the current read-transaction had started.
58470 */
58471 iMinHash = walFramePage(pWal->minFrame);
58472 for(iHash=walFramePage(iLast); iHash>=iMinHash && iRead==0; iHash--){
58473 volatile ht_slot *aHash; /* Pointer to hash table */
58474 volatile u32 *aPgno; /* Pointer to array of page numbers */
58475 u32 iZero; /* Frame number corresponding to aPgno[0] */
58476 int iKey; /* Hash slot index */
58477 int nCollide; /* Number of hash collisions remaining */
@@ -58490,10 +59365,11 @@
58490 }
58491 if( (nCollide--)==0 ){
58492 return SQLITE_CORRUPT_BKPT;
58493 }
58494 }
 
58495 }
58496
58497 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
58498 /* If expensive assert() statements are available, do a linear search
58499 ** of the wal-index file content. Make sure the results agree with the
@@ -59904,24 +60780,24 @@
59904 struct BtCursor {
59905 u8 eState; /* One of the CURSOR_XXX constants (see below) */
59906 u8 curFlags; /* zero or more BTCF_* flags defined below */
59907 u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */
59908 u8 hints; /* As configured by CursorSetHints() */
59909 int nOvflAlloc; /* Allocated size of aOverflow[] array */
59910 Btree *pBtree; /* The Btree to which this cursor belongs */
59911 BtShared *pBt; /* The BtShared this cursor points to */
59912 BtCursor *pNext; /* Forms a linked list of all cursors */
59913 Pgno *aOverflow; /* Cache of overflow page locations */
59914 CellInfo info; /* A parse of the cell we are pointing at */
59915 i64 nKey; /* Size of pKey, or last integer key */
59916 void *pKey; /* Saved key that was cursor last known position */
59917 Pgno pgnoRoot; /* The root page of this tree */
59918 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
59919 ** Error code if eState==CURSOR_FAULT */
 
 
 
59920 /* All fields above are zeroed when the cursor is allocated. See
59921 ** sqlite3BtreeCursorZero(). Fields that follow must be manually
59922 ** initialized. */
 
 
 
 
 
 
59923 i8 iPage; /* Index of current page in apPage */
59924 u8 curIntKey; /* Value of apPage[0]->intKey */
59925 u16 ix; /* Current index for apPage[iPage] */
59926 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
59927 struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */
@@ -59967,12 +60843,12 @@
59967 ** on a different connection that shares the BtShared cache with this
59968 ** cursor. The error has left the cache in an inconsistent state.
59969 ** Do nothing else with this cursor. Any attempt to use the cursor
59970 ** should return the error code stored in BtCursor.skipNext
59971 */
59972 #define CURSOR_INVALID 0
59973 #define CURSOR_VALID 1
59974 #define CURSOR_SKIPNEXT 2
59975 #define CURSOR_REQUIRESEEK 3
59976 #define CURSOR_FAULT 4
59977
59978 /*
@@ -64746,11 +65622,11 @@
64746 ** to zero. But it turns out that the apPage[] and aiIdx[] arrays
64747 ** do not need to be zeroed and they are large, so we can save a lot
64748 ** of run-time by skipping the initialization of those elements.
64749 */
64750 SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
64751 memset(p, 0, offsetof(BtCursor, iPage));
64752 }
64753
64754 /*
64755 ** Close a cursor. The read lock on the database file is released
64756 ** when the last cursor is closed.
@@ -64789,15 +65665,23 @@
64789 **
64790 ** BtCursor.info is a cache of the information in the current cell.
64791 ** Using this cache reduces the number of calls to btreeParseCell().
64792 */
64793 #ifndef NDEBUG
 
 
 
 
 
 
 
 
64794 static void assertCellInfo(BtCursor *pCur){
64795 CellInfo info;
64796 memset(&info, 0, sizeof(info));
64797 btreeParseCell(pCur->pPage, pCur->ix, &info);
64798 assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
64799 }
64800 #else
64801 #define assertCellInfo(x)
64802 #endif
64803 static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
@@ -65069,18 +65953,19 @@
65069 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
65070 ** means "not yet known" (the cache is lazily populated).
65071 */
65072 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
65073 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
65074 if( nOvfl>pCur->nOvflAlloc ){
 
 
65075 Pgno *aNew = (Pgno*)sqlite3Realloc(
65076 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
65077 );
65078 if( aNew==0 ){
65079 return SQLITE_NOMEM_BKPT;
65080 }else{
65081 pCur->nOvflAlloc = nOvfl*2;
65082 pCur->aOverflow = aNew;
65083 }
65084 }
65085 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
65086 pCur->curFlags |= BTCF_ValidOvfl;
@@ -66590,13 +67475,12 @@
66590 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
66591 }
66592 }
66593
66594 /*
66595 ** Free any overflow pages associated with the given Cell. Write the
66596 ** local Cell size (the number of bytes on the original page, omitting
66597 ** overflow) into *pnSize.
66598 */
66599 static int clearCell(
66600 MemPage *pPage, /* The page that contains the Cell */
66601 unsigned char *pCell, /* First byte of the Cell */
66602 CellInfo *pInfo /* Size information about the cell */
@@ -67796,11 +68680,11 @@
67796 rc = SQLITE_CORRUPT_BKPT;
67797 goto balance_cleanup;
67798 }
67799
67800 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
67801 ** constains overflow cells, include them in the b.apCell[] array
67802 ** in the correct spot.
67803 **
67804 ** Note that when there are multiple overflow cells, it is always the
67805 ** case that they are sequential and adjacent. This invariant arises
67806 ** because multiple overflows can only occurs when inserting divider
@@ -71281,10 +72165,55 @@
71281 }
71282 return 1;
71283 }
71284 #endif
71285
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71286
71287 /*
71288 ** If pMem is an object with a valid string representation, this routine
71289 ** ensures the internal encoding for the string representation is
71290 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
@@ -71714,10 +72643,20 @@
71714 }else{
71715 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
71716 return (double)0;
71717 }
71718 }
 
 
 
 
 
 
 
 
 
 
71719
71720 /*
71721 ** The MEM structure is already a MEM_Real. Try to also make it a
71722 ** MEM_Int if we can.
71723 */
@@ -71769,10 +72708,22 @@
71769
71770 pMem->u.r = sqlite3VdbeRealValue(pMem);
71771 MemSetTypeFlag(pMem, MEM_Real);
71772 return SQLITE_OK;
71773 }
 
 
 
 
 
 
 
 
 
 
 
 
71774
71775 /*
71776 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
71777 ** Invalidate any prior representations.
71778 **
@@ -71789,11 +72740,11 @@
71789 if( rc==0 ){
71790 MemSetTypeFlag(pMem, MEM_Int);
71791 }else{
71792 i64 i = pMem->u.i;
71793 sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
71794 if( rc==1 && pMem->u.r==(double)i ){
71795 pMem->u.i = i;
71796 MemSetTypeFlag(pMem, MEM_Int);
71797 }else{
71798 MemSetTypeFlag(pMem, MEM_Real);
71799 }
@@ -72272,10 +73223,11 @@
72272 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
72273 }
72274 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
72275 || pVal->db->mallocFailed );
72276 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
 
72277 return pVal->z;
72278 }else{
72279 return 0;
72280 }
72281 }
@@ -72294,10 +73246,11 @@
72294 if( !pVal ) return 0;
72295 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
72296 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
72297 assert( (pVal->flags & MEM_RowSet)==0 );
72298 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
 
72299 return pVal->z;
72300 }
72301 if( pVal->flags&MEM_Null ){
72302 return 0;
72303 }
@@ -78075,18 +79028,16 @@
78075 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
78076 }
78077 SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
78078 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78079 pCtx->isError = SQLITE_ERROR;
78080 pCtx->fErrorOrAux = 1;
78081 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
78082 }
78083 #ifndef SQLITE_OMIT_UTF16
78084 SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
78085 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78086 pCtx->isError = SQLITE_ERROR;
78087 pCtx->fErrorOrAux = 1;
78088 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
78089 }
78090 #endif
78091 SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
78092 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
@@ -78188,12 +79139,11 @@
78188 }
78189 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
78190 return SQLITE_OK;
78191 }
78192 SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
78193 pCtx->isError = errCode;
78194 pCtx->fErrorOrAux = 1;
78195 #ifdef SQLITE_DEBUG
78196 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
78197 #endif
78198 if( pCtx->pOut->flags & MEM_Null ){
78199 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
@@ -78203,21 +79153,19 @@
78203
78204 /* Force an SQLITE_TOOBIG error. */
78205 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
78206 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78207 pCtx->isError = SQLITE_TOOBIG;
78208 pCtx->fErrorOrAux = 1;
78209 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
78210 SQLITE_UTF8, SQLITE_STATIC);
78211 }
78212
78213 /* An SQLITE_NOMEM error. */
78214 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
78215 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
78216 sqlite3VdbeMemSetNull(pCtx->pOut);
78217 pCtx->isError = SQLITE_NOMEM_BKPT;
78218 pCtx->fErrorOrAux = 1;
78219 sqlite3OomFault(pCtx->pOut->db);
78220 }
78221
78222 /*
78223 ** This function is called after a transaction has been committed. It
@@ -78620,14 +79568,11 @@
78620 if( !pAuxData ) goto failed;
78621 pAuxData->iAuxOp = pCtx->iOp;
78622 pAuxData->iAuxArg = iArg;
78623 pAuxData->pNextAux = pVdbe->pAuxData;
78624 pVdbe->pAuxData = pAuxData;
78625 if( pCtx->fErrorOrAux==0 ){
78626 pCtx->isError = 0;
78627 pCtx->fErrorOrAux = 1;
78628 }
78629 }else if( pAuxData->xDeleteAux ){
78630 pAuxData->xDeleteAux(pAuxData->pAux);
78631 }
78632
78633 pAuxData->pAux = pAux;
@@ -79379,11 +80324,13 @@
79379 */
79380 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
79381 Vdbe *pVdbe = (Vdbe*)pStmt;
79382 u32 v;
79383 #ifdef SQLITE_ENABLE_API_ARMOR
79384 if( !pStmt ){
 
 
79385 (void)SQLITE_MISUSE_BKPT;
79386 return 0;
79387 }
79388 #endif
79389 if( op==SQLITE_STMTSTATUS_MEMUSED ){
@@ -80153,10 +81100,15 @@
80153 }else{
80154 pRec->u.r = rValue;
80155 pRec->flags |= MEM_Real;
80156 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
80157 }
 
 
 
 
 
80158 }
80159
80160 /*
80161 ** Processing is determine by the affinity parameter:
80162 **
@@ -80621,11 +81573,11 @@
80621 ** jumps to abort_due_to_error. */
80622 assert( rc==SQLITE_OK );
80623
80624 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
80625 #ifdef VDBE_PROFILE
80626 start = sqlite3Hwtime();
80627 #endif
80628 nVmStep++;
80629 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
80630 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
80631 #endif
@@ -82145,22 +83097,12 @@
82145 case OP_And: /* same as TK_AND, in1, in2, out3 */
82146 case OP_Or: { /* same as TK_OR, in1, in2, out3 */
82147 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
82148 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
82149
82150 pIn1 = &aMem[pOp->p1];
82151 if( pIn1->flags & MEM_Null ){
82152 v1 = 2;
82153 }else{
82154 v1 = sqlite3VdbeIntValue(pIn1)!=0;
82155 }
82156 pIn2 = &aMem[pOp->p2];
82157 if( pIn2->flags & MEM_Null ){
82158 v2 = 2;
82159 }else{
82160 v2 = sqlite3VdbeIntValue(pIn2)!=0;
82161 }
82162 if( pOp->opcode==OP_And ){
82163 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
82164 v1 = and_logic[v1*3+v2];
82165 }else{
82166 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
@@ -82173,10 +83115,39 @@
82173 pOut->u.i = v1;
82174 MemSetTypeFlag(pOut, MEM_Int);
82175 }
82176 break;
82177 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82178
82179 /* Opcode: Not P1 P2 * * *
82180 ** Synopsis: r[P2]= !r[P1]
82181 **
82182 ** Interpret the value in register P1 as a boolean value. Store the
@@ -82184,14 +83155,14 @@
82184 ** NULL, then a NULL is stored in P2.
82185 */
82186 case OP_Not: { /* same as TK_NOT, in1, out2 */
82187 pIn1 = &aMem[pOp->p1];
82188 pOut = &aMem[pOp->p2];
82189 sqlite3VdbeMemSetNull(pOut);
82190 if( (pIn1->flags & MEM_Null)==0 ){
82191 pOut->flags = MEM_Int;
82192 pOut->u.i = !sqlite3VdbeIntValue(pIn1);
 
82193 }
82194 break;
82195 }
82196
82197 /* Opcode: BitNot P1 P2 * * *
@@ -82254,34 +83225,29 @@
82254 **
82255 ** Jump to P2 if the value in register P1 is true. The value
82256 ** is considered true if it is numeric and non-zero. If the value
82257 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
82258 */
 
 
 
 
 
 
 
 
82259 /* Opcode: IfNot P1 P2 P3 * *
82260 **
82261 ** Jump to P2 if the value in register P1 is False. The value
82262 ** is considered false if it has a numeric value of zero. If the value
82263 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
82264 */
82265 case OP_If: /* jump, in1 */
82266 case OP_IfNot: { /* jump, in1 */
82267 int c;
82268 pIn1 = &aMem[pOp->p1];
82269 if( pIn1->flags & MEM_Null ){
82270 c = pOp->p3;
82271 }else{
82272 #ifdef SQLITE_OMIT_FLOATING_POINT
82273 c = sqlite3VdbeIntValue(pIn1)!=0;
82274 #else
82275 c = sqlite3VdbeRealValue(pIn1)!=0.0;
82276 #endif
82277 if( pOp->opcode==OP_IfNot ) c = !c;
82278 }
82279 VdbeBranchTaken(c!=0, 2);
82280 if( c ){
82281 goto jump_to_p2;
82282 }
82283 break;
82284 }
82285
82286 /* Opcode: IsNull P1 P2 * * *
82287 ** Synopsis: if r[P1]==NULL goto P2
@@ -82338,11 +83304,11 @@
82338 ** pointing.
82339 **
82340 ** P2 is the column number for the argument to the sqlite_offset() function.
82341 ** This opcode does not use P2 itself, but the P2 value is used by the
82342 ** code generator. The P1, P2, and P3 operands to this opcode are the
82343 ** as as for OP_Column.
82344 **
82345 ** This opcode is only available if SQLite is compiled with the
82346 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
82347 */
82348 case OP_Offset: { /* out3 */
@@ -84246,10 +85212,14 @@
84246 v = 0;
84247 res = 0;
84248 pOut = out2Prerelease(p, pOp);
84249 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
84250 pC = p->apCsr[pOp->p1];
 
 
 
 
84251 assert( pC!=0 );
84252 assert( pC->eCurType==CURTYPE_BTREE );
84253 assert( pC->uc.pCursor!=0 );
84254 {
84255 /* The next rowid or record number (different terms for the same
@@ -86182,16 +87152,21 @@
86182 assert( pOp->p4type==P4_FUNCDEF );
86183 n = pOp->p5;
86184 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
86185 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
86186 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
86187 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
 
86188 if( pCtx==0 ) goto no_mem;
86189 pCtx->pMem = 0;
 
 
86190 pCtx->pFunc = pOp->p4.pFunc;
86191 pCtx->iOp = (int)(pOp - aOp);
86192 pCtx->pVdbe = p;
 
 
86193 pCtx->argc = n;
86194 pOp->p4type = P4_FUNCCTX;
86195 pOp->p4.pCtx = pCtx;
86196 pOp->opcode = OP_AggStep;
86197 /* Fall through into OP_AggStep */
@@ -86198,11 +87173,10 @@
86198 }
86199 case OP_AggStep: {
86200 int i;
86201 sqlite3_context *pCtx;
86202 Mem *pMem;
86203 Mem t;
86204
86205 assert( pOp->p4type==P4_FUNCCTX );
86206 pCtx = pOp->p4.pCtx;
86207 pMem = &aMem[pOp->p3];
86208
@@ -86221,30 +87195,32 @@
86221 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
86222 }
86223 #endif
86224
86225 pMem->n++;
86226 sqlite3VdbeMemInit(&t, db, MEM_Null);
86227 pCtx->pOut = &t;
86228 pCtx->fErrorOrAux = 0;
86229 pCtx->skipFlag = 0;
86230 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
86231 if( pCtx->fErrorOrAux ){
86232 if( pCtx->isError ){
86233 sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
86234 rc = pCtx->isError;
86235 }
86236 sqlite3VdbeMemRelease(&t);
 
 
 
 
 
 
 
 
86237 if( rc ) goto abort_due_to_error;
86238 }else{
86239 assert( t.flags==MEM_Null );
86240 }
86241 if( pCtx->skipFlag ){
86242 assert( pOp[-1].opcode==OP_CollSeq );
86243 i = pOp[-1].p1;
86244 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
86245 }
86246 break;
86247 }
86248
86249 /* Opcode: AggFinal P1 P2 * P4 *
86250 ** Synopsis: accum=r[P1] N=P2
@@ -86727,11 +87703,12 @@
86727 }else{
86728 MemSetTypeFlag(pDest, MEM_Null);
86729 }
86730 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
86731 sqlite3VtabImportErrmsg(p, pVtab);
86732 if( sContext.isError ){
 
86733 rc = sContext.isError;
86734 }
86735 sqlite3VdbeChangeEncoding(pDest, encoding);
86736 REGISTER_TRACE(pOp->p3, pDest);
86737 UPDATE_MAX_BLOBSIZE(pDest);
@@ -86992,10 +87969,11 @@
86992 if( pCtx==0 ) goto no_mem;
86993 pCtx->pOut = 0;
86994 pCtx->pFunc = pOp->p4.pFunc;
86995 pCtx->iOp = (int)(pOp - aOp);
86996 pCtx->pVdbe = p;
 
86997 pCtx->argc = n;
86998 pOp->p4type = P4_FUNCCTX;
86999 pOp->p4.pCtx = pCtx;
87000 assert( OP_PureFunc == OP_PureFunc0+2 );
87001 assert( OP_Function == OP_Function0+2 );
@@ -87026,20 +88004,21 @@
87026 assert( memIsValid(pCtx->argv[i]) );
87027 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
87028 }
87029 #endif
87030 MemSetTypeFlag(pOut, MEM_Null);
87031 pCtx->fErrorOrAux = 0;
87032 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
87033
87034 /* If the function returned an error, throw an exception */
87035 if( pCtx->fErrorOrAux ){
87036 if( pCtx->isError ){
87037 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
87038 rc = pCtx->isError;
87039 }
87040 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
 
87041 if( rc ) goto abort_due_to_error;
87042 }
87043
87044 /* Copy the result of the function into register P3 */
87045 if( pOut->flags & (MEM_Str|MEM_Blob) ){
@@ -87077,12 +88056,14 @@
87077 ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
87078 ** error is encountered.
87079 */
87080 case OP_Trace:
87081 case OP_Init: { /* jump */
 
 
87082 char *zTrace;
87083 int i;
87084
87085 /* If the P4 argument is not NULL, then it must be an SQL comment string.
87086 ** The "--" string is broken up to prevent false-positives with srcck1.c.
87087 **
87088 ** This assert() provides evidence for:
@@ -87195,11 +88176,11 @@
87195 *****************************************************************************/
87196 }
87197
87198 #ifdef VDBE_PROFILE
87199 {
87200 u64 endTime = sqlite3Hwtime();
87201 if( endTime>start ) pOrigOp->cycles += endTime - start;
87202 pOrigOp->cnt++;
87203 }
87204 #endif
87205
@@ -91582,14 +92563,20 @@
91582 ** pExpr.
91583 **
91584 ** Because no reference was made to outer contexts, the pNC->nRef
91585 ** fields are not changed in any context.
91586 */
91587 if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
91588 pExpr->op = TK_STRING;
91589 pExpr->pTab = 0;
91590 return WRC_Prune;
 
 
 
 
 
 
91591 }
91592
91593 /*
91594 ** cnt==0 means there was not match. cnt>1 means there were two or
91595 ** more matches. Either way, we have an error.
@@ -91934,19 +92921,34 @@
91934 }
91935 case TK_VARIABLE: {
91936 notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
91937 break;
91938 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91939 case TK_BETWEEN:
91940 case TK_EQ:
91941 case TK_NE:
91942 case TK_LT:
91943 case TK_LE:
91944 case TK_GT:
91945 case TK_GE:
91946 case TK_IS:
91947 case TK_ISNOT: {
91948 int nLeft, nRight;
91949 if( pParse->db->mallocFailed ) break;
91950 assert( pExpr->pLeft!=0 );
91951 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
91952 if( pExpr->op==TK_BETWEEN ){
@@ -94416,10 +95418,38 @@
94416 SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
94417 UNUSED_PARAMETER(NotUsed);
94418 pWalker->eCode = 0;
94419 return WRC_Abort;
94420 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94421
94422 /*
94423 ** These routines are Walker callbacks used to check expressions to
94424 ** see if they are "constant" for some definition of constant. The
94425 ** Walker.eCode value determines the type of "constant" we are looking
@@ -94464,10 +95494,16 @@
94464 }else{
94465 pWalker->eCode = 0;
94466 return WRC_Abort;
94467 }
94468 case TK_ID:
 
 
 
 
 
 
94469 case TK_COLUMN:
94470 case TK_AGG_FUNCTION:
94471 case TK_AGG_COLUMN:
94472 testcase( pExpr->op==TK_ID );
94473 testcase( pExpr->op==TK_COLUMN );
@@ -96227,10 +97263,14 @@
96227 pExpr->op2);
96228 }
96229 case TK_INTEGER: {
96230 codeInteger(pParse, pExpr, 0, target);
96231 return target;
 
 
 
 
96232 }
96233 #ifndef SQLITE_OMIT_FLOATING_POINT
96234 case TK_FLOAT: {
96235 assert( !ExprHasProperty(pExpr, EP_IntValue) );
96236 codeReal(v, pExpr->u.zToken, 0, target);
@@ -96382,10 +97422,22 @@
96382 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT );
96383 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
96384 testcase( regFree1==0 );
96385 sqlite3VdbeAddOp2(v, op, r1, inReg);
96386 break;
 
 
 
 
 
 
 
 
 
 
 
 
96387 }
96388 case TK_ISNULL:
96389 case TK_NOTNULL: {
96390 int addr;
96391 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
@@ -97157,10 +98209,27 @@
97157 }
97158 case TK_NOT: {
97159 testcase( jumpIfNull==0 );
97160 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
97161 break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97162 }
97163 case TK_IS:
97164 case TK_ISNOT:
97165 testcase( op==TK_IS );
97166 testcase( op==TK_ISNOT );
@@ -97311,10 +98380,30 @@
97311 }
97312 case TK_NOT: {
97313 testcase( jumpIfNull==0 );
97314 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
97315 break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97316 }
97317 case TK_IS:
97318 case TK_ISNOT:
97319 testcase( pExpr->op==TK_IS );
97320 testcase( pExpr->op==TK_ISNOT );
@@ -100822,10 +101911,14 @@
100822 **
100823 ** SELECT sqlite_attach(x, y, z)
100824 **
100825 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
100826 ** third argument.
 
 
 
 
100827 */
100828 static void attachFunc(
100829 sqlite3_context *context,
100830 int NotUsed,
100831 sqlite3_value **argv
@@ -100842,69 +101935,89 @@
100842 Db *pNew; /* Db object for the newly attached database */
100843 char *zErrDyn = 0;
100844 sqlite3_vfs *pVfs;
100845
100846 UNUSED_PARAMETER(NotUsed);
100847
100848 zFile = (const char *)sqlite3_value_text(argv[0]);
100849 zName = (const char *)sqlite3_value_text(argv[1]);
100850 if( zFile==0 ) zFile = "";
100851 if( zName==0 ) zName = "";
100852
100853 /* Check for the following errors:
100854 **
100855 ** * Too many attached databases,
100856 ** * Transaction currently open
100857 ** * Specified database name already being used.
100858 */
100859 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
100860 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
100861 db->aLimit[SQLITE_LIMIT_ATTACHED]
100862 );
100863 goto attach_error;
100864 }
100865 for(i=0; i<db->nDb; i++){
100866 char *z = db->aDb[i].zDbSName;
100867 assert( z && zName );
100868 if( sqlite3StrICmp(z, zName)==0 ){
100869 zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
100870 goto attach_error;
100871 }
100872 }
100873
100874 /* Allocate the new entry in the db->aDb[] array and initialize the schema
100875 ** hash tables.
100876 */
100877 if( db->aDb==db->aDbStatic ){
100878 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
100879 if( aNew==0 ) return;
100880 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
100881 }else{
100882 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
100883 if( aNew==0 ) return;
100884 }
100885 db->aDb = aNew;
100886 pNew = &db->aDb[db->nDb];
100887 memset(pNew, 0, sizeof(*pNew));
100888
100889 /* Open the database file. If the btree is successfully opened, use
100890 ** it to obtain the database schema. At this point the schema may
100891 ** or may not be initialized.
100892 */
100893 flags = db->openFlags;
100894 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
100895 if( rc!=SQLITE_OK ){
100896 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
100897 sqlite3_result_error(context, zErr, -1);
100898 sqlite3_free(zErr);
100899 return;
100900 }
100901 assert( pVfs );
100902 flags |= SQLITE_OPEN_MAIN_DB;
100903 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
100904 sqlite3_free( zPath );
100905 db->nDb++;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100906 db->skipBtreeMutex = 0;
100907 if( rc==SQLITE_CONSTRAINT ){
100908 rc = SQLITE_ERROR;
100909 zErrDyn = sqlite3MPrintf(db, "database is already attached");
100910 }else if( rc==SQLITE_OK ){
@@ -100927,11 +102040,11 @@
100927 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
100928 #endif
100929 sqlite3BtreeLeave(pNew->pBt);
100930 }
100931 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
100932 pNew->zDbSName = sqlite3DbStrDup(db, zName);
100933 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
100934 rc = SQLITE_NOMEM_BKPT;
100935 }
100936
100937
@@ -100967,17 +102080,19 @@
100967 }
100968 #endif
100969
100970 /* If the file was opened successfully, read the schema for the new database.
100971 ** If this fails, or if opening the file failed, then close the file and
100972 ** remove the entry from the db->aDb[] array. i.e. put everything back the way
100973 ** we found it.
100974 */
100975 if( rc==SQLITE_OK ){
100976 sqlite3BtreeEnterAll(db);
 
100977 rc = sqlite3Init(db, &zErrDyn);
100978 sqlite3BtreeLeaveAll(db);
 
100979 }
100980 #ifdef SQLITE_USER_AUTHENTICATION
100981 if( rc==SQLITE_OK ){
100982 u8 newAuth = 0;
100983 rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
@@ -100985,25 +102100,27 @@
100985 rc = SQLITE_AUTH_USER;
100986 }
100987 }
100988 #endif
100989 if( rc ){
100990 int iDb = db->nDb - 1;
100991 assert( iDb>=2 );
100992 if( db->aDb[iDb].pBt ){
100993 sqlite3BtreeClose(db->aDb[iDb].pBt);
100994 db->aDb[iDb].pBt = 0;
100995 db->aDb[iDb].pSchema = 0;
100996 }
100997 sqlite3ResetAllSchemasOfConnection(db);
100998 db->nDb = iDb;
100999 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
101000 sqlite3OomFault(db);
101001 sqlite3DbFree(db, zErrDyn);
101002 zErrDyn = sqlite3MPrintf(db, "out of memory");
101003 }else if( zErrDyn==0 ){
101004 zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
 
 
101005 }
101006 goto attach_error;
101007 }
101008
101009 return;
@@ -101270,10 +102387,18 @@
101270 if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
101271 return 1;
101272 }
101273 if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
101274 return 1;
 
 
 
 
 
 
 
 
101275 }
101276 pSelect = pSelect->pPrior;
101277 }
101278 return 0;
101279 }
@@ -102734,14 +103859,28 @@
102734 ** been seen on a column. This routine sets the notNull flag on
102735 ** the column currently under construction.
102736 */
102737 SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
102738 Table *p;
 
102739 p = pParse->pNewTable;
102740 if( p==0 || NEVER(p->nCol<1) ) return;
102741 p->aCol[p->nCol-1].notNull = (u8)onError;
 
102742 p->tabFlags |= TF_HasNotNull;
 
 
 
 
 
 
 
 
 
 
 
 
102743 }
102744
102745 /*
102746 ** Scan the column type name zType (length nType) and return the
102747 ** associated affinity type.
@@ -103472,12 +104611,10 @@
103472 }
103473 assert( !db->mallocFailed );
103474 p = pParse->pNewTable;
103475 if( p==0 ) return;
103476
103477 assert( !db->init.busy || !pSelect );
103478
103479 /* If the db->init.busy is 1 it means we are reading the SQL off the
103480 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
103481 ** So do not write to the disk again. Extract the root page number
103482 ** for the table from the db->init.newTnum field. (The page number
103483 ** should have been put there by the sqliteOpenCb routine.)
@@ -103484,10 +104621,14 @@
103484 **
103485 ** If the root page number is 1, that means this is the sqlite_master
103486 ** table itself. So mark it read-only.
103487 */
103488 if( db->init.busy ){
 
 
 
 
103489 p->tnum = db->init.newTnum;
103490 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
103491 }
103492
103493 /* Special processing for WITHOUT ROWID Tables */
@@ -104701,11 +105842,13 @@
104701 ** key out of the last column added to the table under construction.
104702 ** So create a fake list to simulate this.
104703 */
104704 if( pList==0 ){
104705 Token prevCol;
104706 sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName);
 
 
104707 pList = sqlite3ExprListAppend(pParse, 0,
104708 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
104709 if( pList==0 ) goto exit_create_index;
104710 assert( pList->nExpr==1 );
104711 sqlite3ExprListSetSortOrder(pList, sortOrder);
@@ -107546,10 +108689,12 @@
107546 /*
107547 ** Indicate that the accumulator load should be skipped on this
107548 ** iteration of the aggregate loop.
107549 */
107550 static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
 
 
107551 context->skipFlag = 1;
107552 }
107553
107554 /*
107555 ** Implementation of the non-aggregate min() and max() functions
@@ -107612,12 +108757,10 @@
107612 static void lengthFunc(
107613 sqlite3_context *context,
107614 int argc,
107615 sqlite3_value **argv
107616 ){
107617 int len;
107618
107619 assert( argc==1 );
107620 UNUSED_PARAMETER(argc);
107621 switch( sqlite3_value_type(argv[0]) ){
107622 case SQLITE_BLOB:
107623 case SQLITE_INTEGER:
@@ -107625,17 +108768,21 @@
107625 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
107626 break;
107627 }
107628 case SQLITE_TEXT: {
107629 const unsigned char *z = sqlite3_value_text(argv[0]);
 
 
107630 if( z==0 ) return;
107631 len = 0;
107632 while( *z ){
107633 len++;
107634 SQLITE_SKIP_UTF8(z);
 
 
107635 }
107636 sqlite3_result_int(context, len);
107637 break;
107638 }
107639 default: {
107640 sqlite3_result_null(context);
107641 break;
@@ -108706,10 +109853,12 @@
108706 int nPattern; /* Size of zPattern */
108707 int nRep; /* Size of zRep */
108708 i64 nOut; /* Maximum size of zOut */
108709 int loopLimit; /* Last zStr[] that might match zPattern[] */
108710 int i, j; /* Loop counters */
 
 
108711
108712 assert( argc==3 );
108713 UNUSED_PARAMETER(argc);
108714 zStr = sqlite3_value_text(argv[0]);
108715 if( zStr==0 ) return;
@@ -108737,37 +109886,44 @@
108737 zOut = contextMalloc(context, (i64)nOut);
108738 if( zOut==0 ){
108739 return;
108740 }
108741 loopLimit = nStr - nPattern;
 
108742 for(i=j=0; i<=loopLimit; i++){
108743 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
108744 zOut[j++] = zStr[i];
108745 }else{
108746 u8 *zOld;
108747 sqlite3 *db = sqlite3_context_db_handle(context);
108748 nOut += nRep - nPattern;
108749 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
108750 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
108751 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
108752 sqlite3_result_error_toobig(context);
108753 sqlite3_free(zOut);
108754 return;
108755 }
108756 zOld = zOut;
108757 zOut = sqlite3_realloc64(zOut, (int)nOut);
108758 if( zOut==0 ){
108759 sqlite3_result_error_nomem(context);
108760 sqlite3_free(zOld);
108761 return;
 
 
 
 
 
 
108762 }
108763 memcpy(&zOut[j], zRep, nRep);
108764 j += nRep;
108765 i += nPattern-1;
108766 }
108767 }
108768 assert( j+nStr-i+1==nOut );
108769 memcpy(&zOut[j], &zStr[i], nStr-i);
108770 j += nStr - i;
108771 assert( j<=nOut );
108772 zOut[j] = 0;
108773 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
@@ -113826,12 +114982,12 @@
113826 #define sqlite3_bind_pointer sqlite3_api->bind_pointer
113827 #define sqlite3_result_pointer sqlite3_api->result_pointer
113828 #define sqlite3_value_pointer sqlite3_api->value_pointer
113829 /* Version 3.22.0 and later */
113830 #define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
113831 #define sqlite3_value_nochange sqltie3_api->value_nochange
113832 #define sqlite3_vtab_collation sqltie3_api->vtab_collation
113833 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
113834
113835 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
113836 /* This case when the file really is being compiled as a loadable
113837 ** extension */
@@ -117811,11 +118967,11 @@
117811 sqlite3 *db = pData->db;
117812 if( !db->mallocFailed && (db->flags & SQLITE_WriteSchema)==0 ){
117813 char *z;
117814 if( zObj==0 ) zObj = "?";
117815 z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
117816 if( zExtra ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
117817 sqlite3DbFree(db, *pData->pzErrMsg);
117818 *pData->pzErrMsg = z;
117819 }
117820 pData->rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_CORRUPT_BKPT;
117821 }
@@ -119950,16 +121106,19 @@
119950 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
119951 codeOffset(v, p->iOffset, addrContinue);
119952 iSortTab = iTab;
119953 bSeq = 1;
119954 }
119955 for(i=0, iCol=nKey+bSeq; i<nSortData; i++){
 
 
 
119956 int iRead;
119957 if( aOutEx[i].u.x.iOrderByCol ){
119958 iRead = aOutEx[i].u.x.iOrderByCol-1;
119959 }else{
119960 iRead = iCol++;
119961 }
119962 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
119963 VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
119964 }
119965 switch( eDest ){
@@ -127022,12 +128181,12 @@
127022 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
127023 if( rc!=SQLITE_OK ) return rc;
127024 while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
127025 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
127026 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
127027 if( zSubSql ){
127028 assert( zSubSql[0]!='S' );
127029 rc = execSql(db, pzErrMsg, zSubSql);
127030 if( rc!=SQLITE_OK ) break;
127031 }
127032 }
127033 assert( rc!=SQLITE_ROW );
@@ -130561,11 +131720,19 @@
130561 assert( pX!=0 );
130562 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
130563 if( sqlite3ExprIsVector(pX->pRight) ){
130564 r1 = rTemp = sqlite3GetTempReg(pParse);
130565 codeExprOrVector(pParse, pX->pRight, r1, 1);
130566 op = aMoveOp[(pX->op - TK_GT) | 0x0001];
 
 
 
 
 
 
 
 
130567 }else{
130568 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
130569 disableTerm(pLevel, pStart);
130570 op = aMoveOp[(pX->op - TK_GT)];
130571 }
@@ -131336,10 +132503,16 @@
131336 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
131337 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
131338 WO_EQ|WO_IN|WO_IS, 0);
131339 if( pAlt==0 ) continue;
131340 if( pAlt->wtFlags & (TERM_CODED) ) continue;
 
 
 
 
 
 
131341 testcase( pAlt->eOperator & WO_EQ );
131342 testcase( pAlt->eOperator & WO_IS );
131343 testcase( pAlt->eOperator & WO_IN );
131344 VdbeModuleComment((v, "begin transitive constraint"));
131345 sEAlt = *pAlt->pExpr;
@@ -132250,10 +133423,13 @@
132250 if( ALWAYS(pSrc!=0) ){
132251 int i;
132252 for(i=0; i<pSrc->nSrc; i++){
132253 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
132254 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
 
 
 
132255 }
132256 }
132257 pS = pS->pPrior;
132258 }
132259 return mask;
@@ -132662,11 +133838,11 @@
132662 transferJoinMarkings(pNew, pExpr);
132663 idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
132664 exprAnalyze(pSrc, pWC, idxNew);
132665 }
132666 pTerm = &pWC->a[idxTerm];
132667 pTerm->wtFlags = TERM_CODED|TERM_VIRTUAL; /* Disable the original */
132668 pTerm->eOperator = 0;
132669 }
132670
132671 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
132672 ** a virtual term for each vector component. The expression object
@@ -135387,14 +136563,16 @@
135387 pNew->wsFlags |= WHERE_COLUMN_EQ;
135388 assert( saved_nEq==pNew->u.btree.nEq );
135389 if( iCol==XN_ROWID
135390 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
135391 ){
135392 if( iCol>=0 && pProbe->uniqNotNull==0 ){
135393 pNew->wsFlags |= WHERE_UNQ_WANTED;
135394 }else{
135395 pNew->wsFlags |= WHERE_ONEROW;
 
 
135396 }
135397 }
135398 }else if( eOp & WO_ISNULL ){
135399 pNew->wsFlags |= WHERE_COLUMN_NULL;
135400 }else if( eOp & (WO_GT|WO_GE) ){
@@ -137537,10 +138715,11 @@
137537 ** FROM ... WHERE random()>0; -- eval random() once per row
137538 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
137539 */
137540 for(ii=0; ii<sWLB.pWC->nTerm; ii++){
137541 WhereTerm *pT = &sWLB.pWC->a[ii];
 
137542 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
137543 sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
137544 pT->wtFlags |= TERM_CODED;
137545 }
137546 }
@@ -139995,11 +141174,12 @@
139995 #if defined(YYCOVERAGE)
139996 yycoverage[stateno][iLookAhead] = 1;
139997 #endif
139998 do{
139999 i = yy_shift_ofst[stateno];
140000 assert( i>=0 && i+YYNTOKEN<=sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) );
 
140001 assert( iLookAhead!=YYNOCODE );
140002 assert( iLookAhead < YYNTOKEN );
140003 i += iLookAhead;
140004 if( yy_lookahead[i]!=iLookAhead ){
140005 #ifdef YYFALLBACK
@@ -140709,10 +141889,14 @@
140709 }
140710 break;
140711 case 34: /* ccons ::= DEFAULT scanpt ID|INDEXED */
140712 {
140713 Expr *p = tokenExpr(pParse, TK_STRING, yymsp[0].minor.yy0);
 
 
 
 
140714 sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
140715 }
140716 break;
140717 case 35: /* ccons ::= NOT NULL onconf */
140718 {sqlite3AddNotNull(pParse, yymsp[0].minor.yy4);}
@@ -143602,10 +144786,15 @@
143602 }
143603 if( rc==SQLITE_OK ){
143604 sqlite3GlobalConfig.isPCacheInit = 1;
143605 rc = sqlite3OsInit();
143606 }
 
 
 
 
 
143607 if( rc==SQLITE_OK ){
143608 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
143609 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
143610 sqlite3GlobalConfig.isInit = 1;
143611 #ifdef SQLITE_EXTRA_INIT
@@ -143634,11 +144823,11 @@
143634 ** reason. So we run it once during initialization.
143635 */
143636 #ifndef NDEBUG
143637 #ifndef SQLITE_OMIT_FLOATING_POINT
143638 /* This section of code's only "output" is via assert() statements. */
143639 if ( rc==SQLITE_OK ){
143640 u64 x = (((u64)1)<<63)-1;
143641 double y;
143642 assert(sizeof(x)==8);
143643 assert(sizeof(x)==sizeof(y));
143644 memcpy(&y, &x, 8);
@@ -144801,16 +145990,26 @@
144801 #endif
144802 /* SQLITE_AUTH */ "authorization denied",
144803 /* SQLITE_FORMAT */ 0,
144804 /* SQLITE_RANGE */ "column index out of range",
144805 /* SQLITE_NOTADB */ "file is not a database",
 
 
144806 };
144807 const char *zErr = "unknown error";
144808 switch( rc ){
144809 case SQLITE_ABORT_ROLLBACK: {
144810 zErr = "abort due to ROLLBACK";
144811 break;
 
 
 
 
 
 
 
 
144812 }
144813 default: {
144814 rc &= 0xff;
144815 if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
144816 zErr = aMsg[rc];
@@ -160794,10 +161993,11 @@
160794 if( rc==SQLITE_OK ){
160795 sqlite3_bind_int64(pStmt, 1, iBlock);
160796 sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
160797 sqlite3_step(pStmt);
160798 rc = sqlite3_reset(pStmt);
 
160799 }
160800 return rc;
160801 }
160802
160803 /*
@@ -160850,10 +162050,11 @@
160850 sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
160851 }
160852 sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
160853 sqlite3_step(pStmt);
160854 rc = sqlite3_reset(pStmt);
 
160855 }
160856 return rc;
160857 }
160858
160859 /*
@@ -162329,10 +163530,11 @@
162329 }
162330 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
162331 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
162332 sqlite3_step(pStmt);
162333 *pRC = sqlite3_reset(pStmt);
 
162334 sqlite3_free(a);
162335 }
162336
162337 /*
162338 ** Merge the entire database so that there is one segment for each
@@ -163517,10 +164719,11 @@
163517 sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
163518 sqlite3_bind_int64(pChomp, 3, iAbsLevel);
163519 sqlite3_bind_int(pChomp, 4, iIdx);
163520 sqlite3_step(pChomp);
163521 rc = sqlite3_reset(pChomp);
 
163522 }
163523 }
163524
163525 sqlite3_free(root.a);
163526 sqlite3_free(block.a);
@@ -163596,10 +164799,11 @@
163596 if( rc==SQLITE_OK ){
163597 sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
163598 sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
163599 sqlite3_step(pReplace);
163600 rc = sqlite3_reset(pReplace);
 
163601 }
163602
163603 return rc;
163604 }
163605
@@ -164410,11 +165614,10 @@
164410 sqlite3_value **apVal, /* Array of arguments */
164411 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
164412 ){
164413 Fts3Table *p = (Fts3Table *)pVtab;
164414 int rc = SQLITE_OK; /* Return Code */
164415 int isRemove = 0; /* True for an UPDATE or DELETE */
164416 u32 *aSzIns = 0; /* Sizes of inserted documents */
164417 u32 *aSzDel = 0; /* Sizes of deleted documents */
164418 int nChng = 0; /* Net change in number of documents */
164419 int bInsertDone = 0;
164420
@@ -164508,11 +165711,10 @@
164508
164509 /* If this is a DELETE or UPDATE operation, remove the old record. */
164510 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
164511 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
164512 rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
164513 isRemove = 1;
164514 }
164515
164516 /* If this is an INSERT or UPDATE operation, insert the new record. */
164517 if( nArg>1 && rc==SQLITE_OK ){
164518 int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
@@ -164520,11 +165722,11 @@
164520 rc = fts3InsertData(p, apVal, pRowid);
164521 if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
164522 rc = FTS_CORRUPT_VTAB;
164523 }
164524 }
164525 if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
164526 rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
164527 }
164528 if( rc==SQLITE_OK ){
164529 assert( p->iPrevDocid==*pRowid );
164530 rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
@@ -167828,10 +169030,11 @@
167828 }
167829 sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
167830 sqlite3_step(p);
167831 pNode->isDirty = 0;
167832 rc = sqlite3_reset(p);
 
167833 if( pNode->iNode==0 && rc==SQLITE_OK ){
167834 pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
167835 nodeHashInsert(pRtree, pNode);
167836 }
167837 }
@@ -179823,11 +181026,11 @@
179823 SessionTable *pTab /* Table that change applies to */
179824 ){
179825 int iHash;
179826 int bNull = 0;
179827 int rc = SQLITE_OK;
179828 SessionStat1Ctx stat1;
179829
179830 if( pSession->rc ) return;
179831
179832 /* Load table details if required */
179833 if( sessionInitTable(pSession, pTab) ) return;
@@ -181428,30 +182631,37 @@
181428 for(i=0; i<nCol && rc==SQLITE_OK; i++){
181429 int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
181430 if( abPK && abPK[i]==0 ) continue;
181431 rc = sessionInputBuffer(pIn, 9);
181432 if( rc==SQLITE_OK ){
181433 eType = pIn->aData[pIn->iNext++];
181434 }
181435
181436 assert( apOut[i]==0 );
181437 if( eType ){
181438 apOut[i] = sqlite3ValueNew(0);
181439 if( !apOut[i] ) rc = SQLITE_NOMEM;
 
 
 
181440 }
181441
181442 if( rc==SQLITE_OK ){
181443 u8 *aVal = &pIn->aData[pIn->iNext];
181444 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
181445 int nByte;
181446 pIn->iNext += sessionVarintGet(aVal, &nByte);
181447 rc = sessionInputBuffer(pIn, nByte);
181448 if( rc==SQLITE_OK ){
181449 u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
181450 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
 
 
 
 
 
181451 }
181452 pIn->iNext += nByte;
181453 }
181454 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
181455 sqlite3_int64 v = sessionGetI64(aVal);
181456 if( eType==SQLITE_INTEGER ){
181457 sqlite3VdbeMemSetInt64(apOut[i], v);
@@ -181487,12 +182697,23 @@
181487 int nRead = 0;
181488
181489 rc = sessionInputBuffer(pIn, 9);
181490 if( rc==SQLITE_OK ){
181491 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
181492 rc = sessionInputBuffer(pIn, nRead+nCol+100);
181493 nRead += nCol;
 
 
 
 
 
 
 
 
 
 
 
181494 }
181495
181496 while( rc==SQLITE_OK ){
181497 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
181498 nRead++;
@@ -181565,15 +182786,19 @@
181565 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
181566 if( rc==SQLITE_OK ){
181567 int nByte;
181568 int nVarint;
181569 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
181570 nCopy -= nVarint;
181571 p->in.iNext += nVarint;
181572 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
181573 p->tblhdr.nBuf = 0;
181574 sessionBufferGrow(&p->tblhdr, nByte, &rc);
 
 
 
 
181575 }
181576
181577 if( rc==SQLITE_OK ){
181578 int iPK = sizeof(sqlite3_value*)*p->nCol*2;
181579 memset(p->tblhdr.aBuf, 0, iPK);
@@ -181646,10 +182871,17 @@
181646 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
181647 p->in.iCurrent = p->in.iNext;
181648 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
181649 op = p->in.aData[p->in.iNext++];
181650 }
 
 
 
 
 
 
 
181651
181652 p->op = op;
181653 p->bIndirect = p->in.aData[p->in.iNext++];
181654 if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
181655 return (p->rc = SQLITE_CORRUPT_BKPT);
@@ -181689,13 +182921,13 @@
181689 ** modified fields are present in the new.* record. The old.* record
181690 ** is currently completely empty. This block shifts the PK fields from
181691 ** new.* to old.*, to accommodate the code that reads these arrays. */
181692 for(i=0; i<p->nCol; i++){
181693 assert( p->apValue[i]==0 );
181694 assert( p->abPK[i]==0 || p->apValue[i+p->nCol] );
181695 if( p->abPK[i] ){
181696 p->apValue[i] = p->apValue[i+p->nCol];
 
181697 p->apValue[i+p->nCol] = 0;
181698 }
181699 }
181700 }
181701 }
@@ -182416,11 +183648,17 @@
182416
182417 for(i=0; rc==SQLITE_OK && i<nCol; i++){
182418 if( !abPK || abPK[i] ){
182419 sqlite3_value *pVal;
182420 (void)xValue(pIter, i, &pVal);
182421 rc = sessionBindValue(pStmt, i+1, pVal);
 
 
 
 
 
 
182422 }
182423 }
182424 return rc;
182425 }
182426
@@ -188053,11 +189291,12 @@
188053 #if defined(fts5YYCOVERAGE)
188054 fts5yycoverage[stateno][iLookAhead] = 1;
188055 #endif
188056 do{
188057 i = fts5yy_shift_ofst[stateno];
188058 assert( i>=0 && i+fts5YYNFTS5TOKEN<=sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0]) );
 
188059 assert( iLookAhead!=fts5YYNOCODE );
188060 assert( iLookAhead < fts5YYNFTS5TOKEN );
188061 i += iLookAhead;
188062 if( fts5yy_lookahead[i]!=iLookAhead ){
188063 #ifdef fts5YYFALLBACK
@@ -192495,11 +193734,11 @@
192495 if( sCtx.pPhrase==0 ){
192496 /* This happens when parsing a token or quoted phrase that contains
192497 ** no token characters at all. (e.g ... MATCH '""'). */
192498 sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
192499 }else if( sCtx.pPhrase->nTerm ){
192500 sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = bPrefix;
192501 }
192502 pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
192503 }
192504
192505 return sCtx.pPhrase;
@@ -194958,10 +196197,11 @@
194958
194959 sqlite3_bind_int64(p->pWriter, 1, iRowid);
194960 sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC);
194961 sqlite3_step(p->pWriter);
194962 p->rc = sqlite3_reset(p->pWriter);
 
194963 }
194964
194965 /*
194966 ** Execute the following SQL:
194967 **
@@ -196586,10 +197826,11 @@
196586 i64 val = sqlite3_column_int(pIdxSelect, 0);
196587 iPg = (int)(val>>1);
196588 bDlidx = (val & 0x0001);
196589 }
196590 p->rc = sqlite3_reset(pIdxSelect);
 
196591
196592 if( iPg<pSeg->pgnoFirst ){
196593 iPg = pSeg->pgnoFirst;
196594 bDlidx = 0;
196595 }
@@ -197798,10 +199039,11 @@
197798 u8 aBlob[2] = {0xff, 0xff};
197799 sqlite3_bind_int(pIdxSelect, 1, iSegid);
197800 sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
197801 assert( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
197802 p->rc = sqlite3_reset(pIdxSelect);
 
197803 }
197804 }
197805 #endif
197806 }
197807 }
@@ -197924,10 +199166,11 @@
197924 /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
197925 sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC);
197926 sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
197927 sqlite3_step(p->pIdxWriter);
197928 p->rc = sqlite3_reset(p->pIdxWriter);
 
197929 }
197930 pWriter->iBtPage = 0;
197931 }
197932
197933 /*
@@ -203331,11 +204574,11 @@
203331 int nArg, /* Number of args */
203332 sqlite3_value **apUnused /* Function arguments */
203333 ){
203334 assert( nArg==0 );
203335 UNUSED_PARAM2(nArg, apUnused);
203336 sqlite3_result_text(pCtx, "fts5: 2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d", -1, SQLITE_TRANSIENT);
203337 }
203338
203339 static int fts5Init(sqlite3 *db){
203340 static const sqlite3_module fts5Mod = {
203341 /* iVersion */ 2,
@@ -203907,10 +205150,11 @@
203907 if( rc==SQLITE_OK ){
203908 sqlite3_bind_int64(pReplace, 1, iRowid);
203909 sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC);
203910 sqlite3_step(pReplace);
203911 rc = sqlite3_reset(pReplace);
 
203912 }
203913 }
203914 return rc;
203915 }
203916
@@ -204567,10 +205811,11 @@
204567 }else{
204568 sqlite3_bind_int(pReplace, 2, iVal);
204569 }
204570 sqlite3_step(pReplace);
204571 rc = sqlite3_reset(pReplace);
 
204572 }
204573 if( rc==SQLITE_OK && pVal ){
204574 int iNew = p->pConfig->iCookie + 1;
204575 rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
204576 if( rc==SQLITE_OK ){
@@ -207599,12 +208844,12 @@
207599 }
207600 #endif /* SQLITE_CORE */
207601 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
207602
207603 /************** End of stmt.c ************************************************/
207604 #if __LINE__!=207604
207605 #undef SQLITE_SOURCE_ID
207606 #define SQLITE_SOURCE_ID "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2alt2"
207607 #endif
207608 /* Return the source-id for this library */
207609 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
207610 /************************** End of sqlite3.c ******************************/
207611
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.23.0. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -1145,13 +1145,13 @@
1145 **
1146 ** See also: [sqlite3_libversion()],
1147 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1148 ** [sqlite_version()] and [sqlite_source_id()].
1149 */
1150 #define SQLITE_VERSION "3.23.0"
1151 #define SQLITE_VERSION_NUMBER 3023000
1152 #define SQLITE_SOURCE_ID "2018-03-17 16:26:36 442e816b5fed80ebeb58c7c0ab9c2ef999bf488519bf5da670e9cec477034540"
1153
1154 /*
1155 ** CAPI3REF: Run-Time Library Version Numbers
1156 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1157 **
@@ -3518,20 +3518,20 @@
3518 /*
3519 ** CAPI3REF: Formatted String Printing Functions
3520 **
3521 ** These routines are work-alikes of the "printf()" family of functions
3522 ** from the standard C library.
3523 ** These routines understand most of the common formatting options from
3524 ** the standard library printf()
3525 ** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3526 ** See the [built-in printf()] documentation for details.
3527 **
3528 ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
3529 ** results into memory obtained from [sqlite3_malloc64()].
3530 ** The strings returned by these two routines should be
3531 ** released by [sqlite3_free()]. ^Both routines return a
3532 ** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
3533 ** memory to hold the resulting string.
3534 **
3535 ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
3536 ** the standard C library. The result is written into the
3537 ** buffer supplied as the second parameter whose size is given by
@@ -3551,75 +3551,11 @@
3551 ** the zero terminator. So the longest string that can be completely
3552 ** written will be n-1 characters.
3553 **
3554 ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
3555 **
3556 ** See also: [built-in printf()], [printf() SQL function]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3557 */
3558 SQLITE_API char *sqlite3_mprintf(const char*,...);
3559 SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
3560 SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
3561 SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
@@ -4681,17 +4617,17 @@
4617 ** ^The specific value of WHERE-clause [parameter] might influence the
4618 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
4619 ** or [GLOB] operator or if the parameter is compared to an indexed column
4620 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
4621 ** </li>
4622 ** </ol>
4623 **
4624 ** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
4625 ** the extra prepFlags parameter, which is a bit array consisting of zero or
4626 ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
4627 ** sqlite3_prepare_v2() interface works exactly the same as
4628 ** sqlite3_prepare_v3() with a zero prepFlags parameter.
 
4629 */
4630 SQLITE_API int sqlite3_prepare(
4631 sqlite3 *db, /* Database handle */
4632 const char *zSql, /* SQL statement, UTF-8 encoded */
4633 int nByte, /* Maximum length of zSql in bytes. */
@@ -8315,10 +8251,19 @@
8251 ** transaction rollback or database recovery operations are not included.
8252 ** If an IO or other error occurs while writing a page to disk, the effect
8253 ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
8254 ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
8255 ** </dd>
8256 **
8257 ** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
8258 ** <dd>This parameter returns the number of dirty cache entries that have
8259 ** been written to disk in the middle of a transaction due to the page
8260 ** cache overflowing. Transactions are more efficient if they are written
8261 ** to disk all at once. When pages spill mid-transaction, that introduces
8262 ** additional overhead. This parameter can be used help identify
8263 ** inefficiencies that can be resolve by increasing the cache size.
8264 ** </dd>
8265 **
8266 ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
8267 ** <dd>This parameter returns zero for the current value if and only if
8268 ** all foreign key constraints (deferred or immediate) have been
8269 ** resolved.)^ ^The highwater mark is always 0.
@@ -8335,11 +8280,12 @@
8280 #define SQLITE_DBSTATUS_CACHE_HIT 7
8281 #define SQLITE_DBSTATUS_CACHE_MISS 8
8282 #define SQLITE_DBSTATUS_CACHE_WRITE 9
8283 #define SQLITE_DBSTATUS_DEFERRED_FKS 10
8284 #define SQLITE_DBSTATUS_CACHE_USED_SHARED 11
8285 #define SQLITE_DBSTATUS_CACHE_SPILL 12
8286 #define SQLITE_DBSTATUS_MAX 12 /* Largest defined DBSTATUS */
8287
8288
8289 /*
8290 ** CAPI3REF: Prepared Statement Status
8291 ** METHOD: sqlite3_stmt
@@ -9815,10 +9761,132 @@
9761 **
9762 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
9763 */
9764 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
9765
9766 /*
9767 ** CAPI3REF: Serialize a database
9768 **
9769 ** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
9770 ** that is a serialization of the S database on [database connection] D.
9771 ** If P is not a NULL pointer, then the size of the database in bytes
9772 ** is written into *P.
9773 **
9774 ** For an ordinary on-disk database file, the serialization is just a
9775 ** copy of the disk file. For an in-memory database or a "TEMP" database,
9776 ** the serialization is the same sequence of bytes which would be written
9777 ** to disk if that database where backed up to disk.
9778 **
9779 ** The usual case is that sqlite3_serialize() copies the serialization of
9780 ** the database into memory obtained from [sqlite3_malloc64()] and returns
9781 ** a pointer to that memory. The caller is responsible for freeing the
9782 ** returned value to avoid a memory leak. However, if the F argument
9783 ** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
9784 ** are made, and the sqlite3_serialize() function will return a pointer
9785 ** to the contiguous memory representation of the database that SQLite
9786 ** is currently using for that database, or NULL if the no such contiguous
9787 ** memory representation of the database exists. A contiguous memory
9788 ** representation of the database will usually only exist if there has
9789 ** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
9790 ** values of D and S.
9791 ** The size of the database is written into *P even if the
9792 ** SQLITE_SERIALIZE_NOCOPY bit is set but no contigious copy
9793 ** of the database exists.
9794 **
9795 ** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
9796 ** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
9797 ** allocation error occurs.
9798 **
9799 ** This interface is only available if SQLite is compiled with the
9800 ** [SQLITE_ENABLE_DESERIALIZE] option.
9801 */
9802 SQLITE_API unsigned char *sqlite3_serialize(
9803 sqlite3 *db, /* The database connection */
9804 const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */
9805 sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
9806 unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */
9807 );
9808
9809 /*
9810 ** CAPI3REF: Flags for sqlite3_serialize
9811 **
9812 ** Zero or more of the following constants can be OR-ed together for
9813 ** the F argument to [sqlite3_serialize(D,S,P,F)].
9814 **
9815 ** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
9816 ** a pointer to contiguous in-memory database that it is currently using,
9817 ** without making a copy of the database. If SQLite is not currently using
9818 ** a contiguous in-memory database, then this option causes
9819 ** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be
9820 ** using a contiguous in-memory database if it has been initialized by a
9821 ** prior call to [sqlite3_deserialize()].
9822 */
9823 #define SQLITE_SERIALIZE_NOCOPY 0x001 /* Do no memory allocations */
9824
9825 /*
9826 ** CAPI3REF: Deserialize a database
9827 **
9828 ** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
9829 ** [database connection] D to disconnection from database S and then
9830 ** reopen S as an in-memory database based on the serialization contained
9831 ** in P. The serialized database P is N bytes in size. M is the size of
9832 ** the buffer P, which might be larger than N. If M is larger than N, and
9833 ** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
9834 ** permitted to add content to the in-memory database as long as the total
9835 ** size does not exceed M bytes.
9836 **
9837 ** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
9838 ** invoke sqlite3_free() on the serialization buffer when the database
9839 ** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
9840 ** SQLite will try to increase the buffer size using sqlite3_realloc64()
9841 ** if writes on the database cause it to grow larger than M bytes.
9842 **
9843 ** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
9844 ** database is currently in a read transaction or is involved in a backup
9845 ** operation.
9846 **
9847 ** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
9848 ** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
9849 ** [sqlite3_free()] is invoked on argument P prior to returning.
9850 **
9851 ** This interface is only available if SQLite is compiled with the
9852 ** [SQLITE_ENABLE_DESERIALIZE] option.
9853 */
9854 SQLITE_API int sqlite3_deserialize(
9855 sqlite3 *db, /* The database connection */
9856 const char *zSchema, /* Which DB to reopen with the deserialization */
9857 unsigned char *pData, /* The serialized database content */
9858 sqlite3_int64 szDb, /* Number bytes in the deserialization */
9859 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
9860 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
9861 );
9862
9863 /*
9864 ** CAPI3REF: Flags for sqlite3_deserialize()
9865 **
9866 ** The following are allowed values for 6th argument (the F argument) to
9867 ** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
9868 **
9869 ** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
9870 ** in the P argument is held in memory obtained from [sqlite3_malloc64()]
9871 ** and that SQLite should take ownership of this memory and automatically
9872 ** free it when it has finished using it. Without this flag, the caller
9873 ** is resposible for freeing any dynamically allocated memory.
9874 **
9875 ** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
9876 ** grow the size of the database using calls to [sqlite3_realloc64()]. This
9877 ** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
9878 ** Without this flag, the deserialized database cannot increase in size beyond
9879 ** the number of bytes specified by the M parameter.
9880 **
9881 ** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
9882 ** should be treated as read-only.
9883 */
9884 #define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
9885 #define SQLITE_DESERIALIZE_RESIZEABLE 2 /* Resize using sqlite3_realloc64() */
9886 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
9887
9888 /*
9889 ** Undo the hack that converts floating point types to integer for
9890 ** builds on processors without floating point support.
9891 */
9892 #ifdef SQLITE_OMIT_FLOATING_POINT
@@ -9962,20 +10030,27 @@
10030 #endif
10031
10032
10033 /*
10034 ** CAPI3REF: Session Object Handle
10035 **
10036 ** An instance of this object is a [session] that can be used to
10037 ** record changes to a database.
10038 */
10039 typedef struct sqlite3_session sqlite3_session;
10040
10041 /*
10042 ** CAPI3REF: Changeset Iterator Handle
10043 **
10044 ** An instance of this object acts as a cursor for iterating
10045 ** over the elements of a [changeset] or [patchset].
10046 */
10047 typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
10048
10049 /*
10050 ** CAPI3REF: Create A New Session Object
10051 ** CONSTRUCTOR: sqlite3_session
10052 **
10053 ** Create a new session object attached to database handle db. If successful,
10054 ** a pointer to the new object is written to *ppSession and SQLITE_OK is
10055 ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
10056 ** error code (e.g. SQLITE_NOMEM) is returned.
@@ -10008,10 +10083,11 @@
10083 sqlite3_session **ppSession /* OUT: New session object */
10084 );
10085
10086 /*
10087 ** CAPI3REF: Delete A Session Object
10088 ** DESTRUCTOR: sqlite3_session
10089 **
10090 ** Delete a session object previously allocated using
10091 ** [sqlite3session_create()]. Once a session object has been deleted, the
10092 ** results of attempting to use pSession with any other session module
10093 ** function are undefined.
@@ -10023,10 +10099,11 @@
10099 SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
10100
10101
10102 /*
10103 ** CAPI3REF: Enable Or Disable A Session Object
10104 ** METHOD: sqlite3_session
10105 **
10106 ** Enable or disable the recording of changes by a session object. When
10107 ** enabled, a session object records changes made to the database. When
10108 ** disabled - it does not. A newly created session object is enabled.
10109 ** Refer to the documentation for [sqlite3session_changeset()] for further
@@ -10042,10 +10119,11 @@
10119 */
10120 SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
10121
10122 /*
10123 ** CAPI3REF: Set Or Clear the Indirect Change Flag
10124 ** METHOD: sqlite3_session
10125 **
10126 ** Each change recorded by a session object is marked as either direct or
10127 ** indirect. A change is marked as indirect if either:
10128 **
10129 ** <ul>
@@ -10071,10 +10149,11 @@
10149 */
10150 SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
10151
10152 /*
10153 ** CAPI3REF: Attach A Table To A Session Object
10154 ** METHOD: sqlite3_session
10155 **
10156 ** If argument zTab is not NULL, then it is the name of a table to attach
10157 ** to the session object passed as the first argument. All subsequent changes
10158 ** made to the table while the session object is enabled will be recorded. See
10159 ** documentation for [sqlite3session_changeset()] for further details.
@@ -10133,10 +10212,11 @@
10212 const char *zTab /* Table name */
10213 );
10214
10215 /*
10216 ** CAPI3REF: Set a table filter on a Session Object.
10217 ** METHOD: sqlite3_session
10218 **
10219 ** The second argument (xFilter) is the "filter callback". For changes to rows
10220 ** in tables that are not attached to the Session object, the filter is called
10221 ** to determine whether changes to the table's rows should be tracked or not.
10222 ** If xFilter returns 0, changes is not tracked. Note that once a table is
@@ -10151,10 +10231,11 @@
10231 void *pCtx /* First argument passed to xFilter */
10232 );
10233
10234 /*
10235 ** CAPI3REF: Generate A Changeset From A Session Object
10236 ** METHOD: sqlite3_session
10237 **
10238 ** Obtain a changeset containing changes to the tables attached to the
10239 ** session object passed as the first argument. If successful,
10240 ** set *ppChangeset to point to a buffer containing the changeset
10241 ** and *pnChangeset to the size of the changeset in bytes before returning
@@ -10260,11 +10341,12 @@
10341 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
10342 void **ppChangeset /* OUT: Buffer containing changeset */
10343 );
10344
10345 /*
10346 ** CAPI3REF: Load The Difference Between Tables Into A Session
10347 ** METHOD: sqlite3_session
10348 **
10349 ** If it is not already attached to the session object passed as the first
10350 ** argument, this function attaches table zTbl in the same manner as the
10351 ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
10352 ** does not have a primary key, this function is a no-op (but does not return
@@ -10325,10 +10407,11 @@
10407 );
10408
10409
10410 /*
10411 ** CAPI3REF: Generate A Patchset From A Session Object
10412 ** METHOD: sqlite3_session
10413 **
10414 ** The differences between a patchset and a changeset are that:
10415 **
10416 ** <ul>
10417 ** <li> DELETE records consist of the primary key fields only. The
@@ -10376,10 +10459,11 @@
10459 */
10460 SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
10461
10462 /*
10463 ** CAPI3REF: Create An Iterator To Traverse A Changeset
10464 ** CONSTRUCTOR: sqlite3_changeset_iter
10465 **
10466 ** Create an iterator used to iterate through the contents of a changeset.
10467 ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
10468 ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
10469 ** SQLite error code is returned.
@@ -10416,10 +10500,11 @@
10500 );
10501
10502
10503 /*
10504 ** CAPI3REF: Advance A Changeset Iterator
10505 ** METHOD: sqlite3_changeset_iter
10506 **
10507 ** This function may only be used with iterators created by function
10508 ** [sqlite3changeset_start()]. If it is called on an iterator passed to
10509 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
10510 ** is returned and the call has no effect.
@@ -10440,10 +10525,11 @@
10525 */
10526 SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
10527
10528 /*
10529 ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
10530 ** METHOD: sqlite3_changeset_iter
10531 **
10532 ** The pIter argument passed to this function may either be an iterator
10533 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10534 ** created by [sqlite3changeset_start()]. In the latter case, the most recent
10535 ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
@@ -10474,10 +10560,11 @@
10560 int *pbIndirect /* OUT: True for an 'indirect' change */
10561 );
10562
10563 /*
10564 ** CAPI3REF: Obtain The Primary Key Definition Of A Table
10565 ** METHOD: sqlite3_changeset_iter
10566 **
10567 ** For each modified table, a changeset includes the following:
10568 **
10569 ** <ul>
10570 ** <li> The number of columns in the table, and
@@ -10505,10 +10592,11 @@
10592 int *pnCol /* OUT: Number of entries in output array */
10593 );
10594
10595 /*
10596 ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
10597 ** METHOD: sqlite3_changeset_iter
10598 **
10599 ** The pIter argument passed to this function may either be an iterator
10600 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10601 ** created by [sqlite3changeset_start()]. In the latter case, the most recent
10602 ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
@@ -10535,10 +10623,11 @@
10623 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
10624 );
10625
10626 /*
10627 ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
10628 ** METHOD: sqlite3_changeset_iter
10629 **
10630 ** The pIter argument passed to this function may either be an iterator
10631 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10632 ** created by [sqlite3changeset_start()]. In the latter case, the most recent
10633 ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
@@ -10568,10 +10657,11 @@
10657 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
10658 );
10659
10660 /*
10661 ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
10662 ** METHOD: sqlite3_changeset_iter
10663 **
10664 ** This function should only be used with iterator objects passed to a
10665 ** conflict-handler callback by [sqlite3changeset_apply()] with either
10666 ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
10667 ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
@@ -10595,10 +10685,11 @@
10685 sqlite3_value **ppValue /* OUT: Value from conflicting row */
10686 );
10687
10688 /*
10689 ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
10690 ** METHOD: sqlite3_changeset_iter
10691 **
10692 ** This function may only be called with an iterator passed to an
10693 ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
10694 ** it sets the output variable to the total number of known foreign key
10695 ** violations in the destination database and returns SQLITE_OK.
@@ -10611,10 +10702,11 @@
10702 );
10703
10704
10705 /*
10706 ** CAPI3REF: Finalize A Changeset Iterator
10707 ** METHOD: sqlite3_changeset_iter
10708 **
10709 ** This function is used to finalize an iterator allocated with
10710 ** [sqlite3changeset_start()].
10711 **
10712 ** This function should only be called on iterators created using the
@@ -10627,18 +10719,20 @@
10719 ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
10720 ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
10721 ** to that error is returned by this function. Otherwise, SQLITE_OK is
10722 ** returned. This is to allow the following pattern (pseudo-code):
10723 **
10724 ** <pre>
10725 ** sqlite3changeset_start();
10726 ** while( SQLITE_ROW==sqlite3changeset_next() ){
10727 ** // Do something with change.
10728 ** }
10729 ** rc = sqlite3changeset_finalize();
10730 ** if( rc!=SQLITE_OK ){
10731 ** // An error has occurred
10732 ** }
10733 ** </pre>
10734 */
10735 SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
10736
10737 /*
10738 ** CAPI3REF: Invert A Changeset
@@ -10682,10 +10776,11 @@
10776 **
10777 ** This function combines the two input changesets using an
10778 ** sqlite3_changegroup object. Calling it produces similar results as the
10779 ** following code fragment:
10780 **
10781 ** <pre>
10782 ** sqlite3_changegroup *pGrp;
10783 ** rc = sqlite3_changegroup_new(&pGrp);
10784 ** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
10785 ** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
10786 ** if( rc==SQLITE_OK ){
@@ -10692,10 +10787,11 @@
10787 ** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
10788 ** }else{
10789 ** *ppOut = 0;
10790 ** *pnOut = 0;
10791 ** }
10792 ** </pre>
10793 **
10794 ** Refer to the sqlite3_changegroup documentation below for details.
10795 */
10796 SQLITE_API int sqlite3changeset_concat(
10797 int nA, /* Number of bytes in buffer pA */
@@ -10707,15 +10803,19 @@
10803 );
10804
10805
10806 /*
10807 ** CAPI3REF: Changegroup Handle
10808 **
10809 ** A changegroup is an object used to combine two or more
10810 ** [changesets] or [patchsets]
10811 */
10812 typedef struct sqlite3_changegroup sqlite3_changegroup;
10813
10814 /*
10815 ** CAPI3REF: Create A New Changegroup Object
10816 ** CONSTRUCTOR: sqlite3_changegroup
10817 **
10818 ** An sqlite3_changegroup object is used to combine two or more changesets
10819 ** (or patchsets) into a single changeset (or patchset). A single changegroup
10820 ** object may combine changesets or patchsets, but not both. The output is
10821 ** always in the same format as the input.
@@ -10749,10 +10849,11 @@
10849 */
10850 SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
10851
10852 /*
10853 ** CAPI3REF: Add A Changeset To A Changegroup
10854 ** METHOD: sqlite3_changegroup
10855 **
10856 ** Add all changes within the changeset (or patchset) in buffer pData (size
10857 ** nData bytes) to the changegroup.
10858 **
10859 ** If the buffer contains a patchset, then all prior calls to this function
@@ -10826,10 +10927,11 @@
10927 */
10928 SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
10929
10930 /*
10931 ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
10932 ** METHOD: sqlite3_changegroup
10933 **
10934 ** Obtain a buffer containing a changeset (or patchset) representing the
10935 ** current contents of the changegroup. If the inputs to the changegroup
10936 ** were themselves changesets, the output is a changeset. Or, if the
10937 ** inputs were patchsets, the output is also a patchset.
@@ -10856,10 +10958,11 @@
10958 void **ppData /* OUT: Pointer to output buffer */
10959 );
10960
10961 /*
10962 ** CAPI3REF: Delete A Changegroup Object
10963 ** DESTRUCTOR: sqlite3_changegroup
10964 */
10965 SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
10966
10967 /*
10968 ** CAPI3REF: Apply A Changeset To A Database
@@ -12671,27 +12774,29 @@
12774 #define TK_THEN 138
12775 #define TK_ELSE 139
12776 #define TK_INDEX 140
12777 #define TK_ALTER 141
12778 #define TK_ADD 142
12779 #define TK_TRUEFALSE 143
12780 #define TK_ISNOT 144
12781 #define TK_FUNCTION 145
12782 #define TK_COLUMN 146
12783 #define TK_AGG_FUNCTION 147
12784 #define TK_AGG_COLUMN 148
12785 #define TK_UMINUS 149
12786 #define TK_UPLUS 150
12787 #define TK_TRUTH 151
12788 #define TK_REGISTER 152
12789 #define TK_VECTOR 153
12790 #define TK_SELECT_COLUMN 154
12791 #define TK_IF_NULL_ROW 155
12792 #define TK_ASTERISK 156
12793 #define TK_SPAN 157
12794 #define TK_END_OF_FILE 158
12795 #define TK_UNCLOSED_STRING 159
12796 #define TK_SPACE 160
12797 #define TK_ILLEGAL 161
12798
12799 /* The token codes above must all fit in 8 bits */
12800 #define TKFLG_MASK 0xff
12801
12802 /* Flags that can be added to a token code when it is not
@@ -13933,84 +14038,85 @@
14038 #define OP_Divide 91 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
14039 #define OP_Remainder 92 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
14040 #define OP_Concat 93 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
14041 #define OP_Compare 94 /* synopsis: r[P1@P3] <-> r[P2@P3] */
14042 #define OP_BitNot 95 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
14043 #define OP_IsTrue 96 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
14044 #define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */
14045 #define OP_Offset 98 /* synopsis: r[P3] = sqlite_offset(P1) */
14046 #define OP_Column 99 /* synopsis: r[P3]=PX */
14047 #define OP_Affinity 100 /* synopsis: affinity(r[P1@P2]) */
14048 #define OP_MakeRecord 101 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
14049 #define OP_Count 102 /* synopsis: r[P2]=count() */
14050 #define OP_ReadCookie 103
14051 #define OP_SetCookie 104
14052 #define OP_ReopenIdx 105 /* synopsis: root=P2 iDb=P3 */
14053 #define OP_OpenRead 106 /* synopsis: root=P2 iDb=P3 */
14054 #define OP_OpenWrite 107 /* synopsis: root=P2 iDb=P3 */
14055 #define OP_OpenDup 108
14056 #define OP_OpenAutoindex 109 /* synopsis: nColumn=P2 */
14057 #define OP_OpenEphemeral 110 /* synopsis: nColumn=P2 */
14058 #define OP_SorterOpen 111
14059 #define OP_SequenceTest 112 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
14060 #define OP_OpenPseudo 113 /* synopsis: P3 columns in r[P2] */
14061 #define OP_Close 114
14062 #define OP_ColumnsUsed 115
14063 #define OP_Sequence 116 /* synopsis: r[P2]=cursor[P1].ctr++ */
14064 #define OP_NewRowid 117 /* synopsis: r[P2]=rowid */
14065 #define OP_Insert 118 /* synopsis: intkey=r[P3] data=r[P2] */
14066 #define OP_InsertInt 119 /* synopsis: intkey=P3 data=r[P2] */
14067 #define OP_Delete 120
14068 #define OP_ResetCount 121
14069 #define OP_SorterCompare 122 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
14070 #define OP_SorterData 123 /* synopsis: r[P2]=data */
14071 #define OP_RowData 124 /* synopsis: r[P2]=data */
14072 #define OP_Rowid 125 /* synopsis: r[P2]=rowid */
14073 #define OP_NullRow 126
14074 #define OP_SeekEnd 127
14075 #define OP_SorterInsert 128 /* synopsis: key=r[P2] */
14076 #define OP_IdxInsert 129 /* synopsis: key=r[P2] */
14077 #define OP_IdxDelete 130 /* synopsis: key=r[P2@P3] */
14078 #define OP_DeferredSeek 131 /* synopsis: Move P3 to P1.rowid if needed */
14079 #define OP_Real 132 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
14080 #define OP_IdxRowid 133 /* synopsis: r[P2]=rowid */
14081 #define OP_Destroy 134
14082 #define OP_Clear 135
14083 #define OP_ResetSorter 136
14084 #define OP_CreateBtree 137 /* synopsis: r[P2]=root iDb=P1 flags=P3 */
14085 #define OP_SqlExec 138
14086 #define OP_ParseSchema 139
14087 #define OP_LoadAnalysis 140
14088 #define OP_DropTable 141
14089 #define OP_DropIndex 142
14090 #define OP_DropTrigger 143
14091 #define OP_IntegrityCk 144
14092 #define OP_RowSetAdd 145 /* synopsis: rowset(P1)=r[P2] */
14093 #define OP_Param 146
14094 #define OP_FkCounter 147 /* synopsis: fkctr[P1]+=P2 */
14095 #define OP_MemMax 148 /* synopsis: r[P1]=max(r[P1],r[P2]) */
14096 #define OP_OffsetLimit 149 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
14097 #define OP_AggStep0 150 /* synopsis: accum=r[P3] step(r[P2@P5]) */
14098 #define OP_AggStep 151 /* synopsis: accum=r[P3] step(r[P2@P5]) */
14099 #define OP_AggFinal 152 /* synopsis: accum=r[P1] N=P2 */
14100 #define OP_Expire 153
14101 #define OP_TableLock 154 /* synopsis: iDb=P1 root=P2 write=P3 */
14102 #define OP_VBegin 155
14103 #define OP_VCreate 156
14104 #define OP_VDestroy 157
14105 #define OP_VOpen 158
14106 #define OP_VColumn 159 /* synopsis: r[P3]=vcolumn(P2) */
14107 #define OP_VRename 160
14108 #define OP_Pagecount 161
14109 #define OP_MaxPgcnt 162
14110 #define OP_PureFunc0 163
14111 #define OP_Function0 164 /* synopsis: r[P3]=func(r[P2@P5]) */
14112 #define OP_PureFunc 165
14113 #define OP_Function 166 /* synopsis: r[P3]=func(r[P2@P5]) */
14114 #define OP_Trace 167
14115 #define OP_CursorHint 168
14116 #define OP_Noop 169
14117 #define OP_Explain 170
14118
14119 /* Properties such as "out2" or "jump" that are specified in
14120 ** comments following the "case" for each opcode in the vdbe.c
14121 ** are encoded into bitvectors as follows:
14122 */
@@ -14031,20 +14137,20 @@
14137 /* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x01, 0x01, 0x01, 0x02,\
14138 /* 64 */ 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00,\
14139 /* 72 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
14140 /* 80 */ 0x02, 0x02, 0x02, 0x00, 0x26, 0x26, 0x26, 0x26,\
14141 /* 88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
14142 /* 96 */ 0x12, 0x10, 0x20, 0x00, 0x00, 0x00, 0x10, 0x10,\
14143 /* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14144 /* 112 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
14145 /* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,\
14146 /* 128 */ 0x04, 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00,\
14147 /* 136 */ 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14148 /* 144 */ 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00,\
14149 /* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14150 /* 160 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
14151 /* 168 */ 0x00, 0x00, 0x00,}
14152
14153 /* The sqlite3P2Values() routine is able to run faster if it knows
14154 ** the value of the largest JUMP opcode. The smaller the maximum
14155 ** JUMP opcode the better, so the mkopcodeh.tcl script that
14156 ** generated this include file strives to group all JUMP opcodes
@@ -15246,12 +15352,13 @@
15352 int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
15353 struct sqlite3InitInfo { /* Information used during initialization */
15354 int newTnum; /* Rootpage of table being initialized */
15355 u8 iDb; /* Which db file is being initialized */
15356 u8 busy; /* TRUE if currently initializing */
15357 unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
15358 unsigned imposterTable : 1; /* Building an imposter table */
15359 unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
15360 } init;
15361 int nVdbeActive; /* Number of VDBEs currently running */
15362 int nVdbeRead; /* Number of active VDBEs that read or write */
15363 int nVdbeWrite; /* Number of active VDBEs that read and write */
15364 int nVdbeExec; /* Number of nested calls to VdbeExec() */
@@ -15635,10 +15742,11 @@
15742 /* Allowed values for Column.colFlags:
15743 */
15744 #define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
15745 #define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */
15746 #define COLFLAG_HASTYPE 0x0004 /* Type name follows column name */
15747 #define COLFLAG_UNIQUE 0x0008 /* Column def contains "UNIQUE" or "PK" */
15748
15749 /*
15750 ** A "Collating Sequence" is defined by an instance of the following
15751 ** structure. Conceptually, a collating sequence consists of a name and
15752 ** a comparison routine that defines the order of that sequence.
@@ -17719,10 +17827,12 @@
17827 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
17828 SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
17829 SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
17830 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
17831 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
17832 SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
17833 SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
17834 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
17835 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
17836 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
17837 SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
17838 SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
@@ -17900,10 +18010,14 @@
18010 SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
18011
18012 #if defined(SQLITE_NEED_ERR_NAME)
18013 SQLITE_PRIVATE const char *sqlite3ErrName(int);
18014 #endif
18015
18016 #ifdef SQLITE_ENABLE_DESERIALIZE
18017 SQLITE_PRIVATE int sqlite3MemdbInit(void);
18018 #endif
18019
18020 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
18021 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
18022 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
18023 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
@@ -17949,10 +18063,13 @@
18063 SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
18064 #ifndef SQLITE_OMIT_WSD
18065 SQLITE_PRIVATE int sqlite3PendingByte;
18066 #endif
18067 #endif
18068 #ifdef VDBE_PROFILE
18069 SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt;
18070 #endif
18071 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
18072 SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
18073 SQLITE_PRIVATE void sqlite3AlterFunctions(void);
18074 SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
18075 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
@@ -18572,10 +18689,17 @@
18689 SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
18690 { "0", 1 },
18691 { "1", 1 }
18692 };
18693
18694 #ifdef VDBE_PROFILE
18695 /*
18696 ** The following performance counter can be used in place of
18697 ** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
18698 */
18699 SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt = 0;
18700 #endif
18701
18702 /*
18703 ** The value of the "pending" byte must be 0x40000000 (1 byte past the
18704 ** 1-gibabyte boundary) in a compatible database. SQLite never uses
18705 ** the database page that contains the pending byte. It never attempts
@@ -18855,12 +18979,10 @@
18979 ** representations of the value stored in the Mem struct.
18980 **
18981 ** If the MEM_Null flag is set, then the value is an SQL NULL value.
18982 ** For a pointer type created using sqlite3_bind_pointer() or
18983 ** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
 
 
18984 **
18985 ** If the MEM_Str flag is set then Mem.z points at a string representation.
18986 ** Usually this is encoded in the same unicode encoding as the main
18987 ** database (see below for exceptions). If the MEM_Term flag is also
18988 ** set, then the string is nul terminated. The MEM_Int and MEM_Real
@@ -18950,11 +19072,10 @@
19072 Mem *pMem; /* Memory cell used to store aggregate context */
19073 Vdbe *pVdbe; /* The VM that owns this context */
19074 int iOp; /* Instruction number of OP_Function */
19075 int isError; /* Error code returned by the function. */
19076 u8 skipFlag; /* Skip accumulator loading if true */
 
19077 u8 argc; /* Number of arguments */
19078 sqlite3_value *argv[1]; /* Argument set */
19079 };
19080
19081 /* A bitfield type for use inside of structures. Always follow with :N where
@@ -19120,10 +19241,11 @@
19241 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
19242 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
19243 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
19244 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
19245 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
19246 SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
19247 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
19248 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
19249 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
19250 SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
19251 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
@@ -19513,10 +19635,13 @@
19635 /*
19636 ** Set *pCurrent to the total cache hits or misses encountered by all
19637 ** pagers the database handle is connected to. *pHighwater is always set
19638 ** to zero.
19639 */
19640 case SQLITE_DBSTATUS_CACHE_SPILL:
19641 op = SQLITE_DBSTATUS_CACHE_WRITE+1;
19642 /* Fall through into the next case */
19643 case SQLITE_DBSTATUS_CACHE_HIT:
19644 case SQLITE_DBSTATUS_CACHE_MISS:
19645 case SQLITE_DBSTATUS_CACHE_WRITE:{
19646 int i;
19647 int nRet = 0;
@@ -24106,15 +24231,16 @@
24231 volatile pthread_t owner; /* Thread that is within this mutex */
24232 int trace; /* True to trace changes */
24233 #endif
24234 };
24235 #if SQLITE_MUTEX_NREF
24236 # define SQLITE3_MUTEX_INITIALIZER(id) \
24237 {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
24238 #elif defined(SQLITE_ENABLE_API_ARMOR)
24239 # define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
24240 #else
24241 #define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
24242 #endif
24243
24244 /*
24245 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
24246 ** intended for use only inside assert() statements. On some platforms,
@@ -24207,22 +24333,22 @@
24333 ** mutex types, the same mutex is returned on every call that has
24334 ** the same type number.
24335 */
24336 static sqlite3_mutex *pthreadMutexAlloc(int iType){
24337 static sqlite3_mutex staticMutexes[] = {
24338 SQLITE3_MUTEX_INITIALIZER(2),
24339 SQLITE3_MUTEX_INITIALIZER(3),
24340 SQLITE3_MUTEX_INITIALIZER(4),
24341 SQLITE3_MUTEX_INITIALIZER(5),
24342 SQLITE3_MUTEX_INITIALIZER(6),
24343 SQLITE3_MUTEX_INITIALIZER(7),
24344 SQLITE3_MUTEX_INITIALIZER(8),
24345 SQLITE3_MUTEX_INITIALIZER(9),
24346 SQLITE3_MUTEX_INITIALIZER(10),
24347 SQLITE3_MUTEX_INITIALIZER(11),
24348 SQLITE3_MUTEX_INITIALIZER(12),
24349 SQLITE3_MUTEX_INITIALIZER(13)
24350 };
24351 sqlite3_mutex *p;
24352 switch( iType ){
24353 case SQLITE_MUTEX_RECURSIVE: {
24354 p = sqlite3MallocZero( sizeof(*p) );
@@ -24237,17 +24363,23 @@
24363 pthread_mutexattr_init(&recursiveAttr);
24364 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
24365 pthread_mutex_init(&p->mutex, &recursiveAttr);
24366 pthread_mutexattr_destroy(&recursiveAttr);
24367 #endif
24368 #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
24369 p->id = SQLITE_MUTEX_RECURSIVE;
24370 #endif
24371 }
24372 break;
24373 }
24374 case SQLITE_MUTEX_FAST: {
24375 p = sqlite3MallocZero( sizeof(*p) );
24376 if( p ){
24377 pthread_mutex_init(&p->mutex, 0);
24378 #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
24379 p->id = SQLITE_MUTEX_FAST;
24380 #endif
24381 }
24382 break;
24383 }
24384 default: {
24385 #ifdef SQLITE_ENABLE_API_ARMOR
@@ -24259,11 +24391,11 @@
24391 p = &staticMutexes[iType-2];
24392 break;
24393 }
24394 }
24395 #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
24396 assert( p==0 || p->id==iType );
24397 #endif
24398 return p;
24399 }
24400
24401
@@ -24776,11 +24908,11 @@
24908 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
24909 int id; /* Mutex type */
24910 #ifdef SQLITE_DEBUG
24911 volatile int nRef; /* Number of enterances */
24912 volatile DWORD owner; /* Thread holding this mutex */
24913 volatile LONG trace; /* True to trace changes */
24914 #endif
24915 };
24916
24917 /*
24918 ** These are the initializer values used when declaring a "static" mutex
@@ -24788,14 +24920,14 @@
24920 ** on the Win32 platform.
24921 */
24922 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
24923
24924 #ifdef SQLITE_DEBUG
24925 #define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \
24926 0L, (DWORD)0, 0 }
24927 #else
24928 #define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id }
24929 #endif
24930
24931 #ifdef SQLITE_DEBUG
24932 /*
24933 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
@@ -24834,22 +24966,22 @@
24966
24967 /*
24968 ** Initialize and deinitialize the mutex subsystem.
24969 */
24970 static sqlite3_mutex winMutex_staticMutexes[] = {
24971 SQLITE3_MUTEX_INITIALIZER(2),
24972 SQLITE3_MUTEX_INITIALIZER(3),
24973 SQLITE3_MUTEX_INITIALIZER(4),
24974 SQLITE3_MUTEX_INITIALIZER(5),
24975 SQLITE3_MUTEX_INITIALIZER(6),
24976 SQLITE3_MUTEX_INITIALIZER(7),
24977 SQLITE3_MUTEX_INITIALIZER(8),
24978 SQLITE3_MUTEX_INITIALIZER(9),
24979 SQLITE3_MUTEX_INITIALIZER(10),
24980 SQLITE3_MUTEX_INITIALIZER(11),
24981 SQLITE3_MUTEX_INITIALIZER(12),
24982 SQLITE3_MUTEX_INITIALIZER(13)
24983 };
24984
24985 static int winMutex_isInit = 0;
24986 static int winMutex_isNt = -1; /* <0 means "need to query" */
24987
@@ -24975,19 +25107,19 @@
25107 (void)SQLITE_MISUSE_BKPT;
25108 return 0;
25109 }
25110 #endif
25111 p = &winMutex_staticMutexes[iType-2];
 
25112 #ifdef SQLITE_DEBUG
25113 #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
25114 InterlockedCompareExchange(&p->trace, 1, 0);
25115 #endif
25116 #endif
25117 break;
25118 }
25119 }
25120 assert( p==0 || p->id==iType );
25121 return p;
25122 }
25123
25124
25125 /*
@@ -26062,10 +26194,15 @@
26194 etByte flag_rtz; /* True if trailing zeros should be removed */
26195 #endif
26196 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
26197 char buf[etBUFSIZE]; /* Conversion buffer */
26198
26199 /* pAccum never starts out with an empty buffer that was obtained from
26200 ** malloc(). This precondition is required by the mprintf("%z...")
26201 ** optimization. */
26202 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
26203
26204 bufpt = 0;
26205 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
26206 pArgList = va_arg(ap, PrintfArguments*);
26207 bArgList = 1;
26208 }else{
@@ -26480,26 +26617,56 @@
26617 length = 1;
26618 break;
26619 case etCHARX:
26620 if( bArgList ){
26621 bufpt = getTextArg(pArgList);
26622 length = 1;
26623 if( bufpt ){
26624 buf[0] = c = *(bufpt++);
26625 if( (c&0xc0)==0xc0 ){
26626 while( length<4 && (bufpt[0]&0xc0)==0x80 ){
26627 buf[length++] = *(bufpt++);
26628 }
26629 }
26630 }else{
26631 buf[0] = 0;
26632 }
26633 }else{
26634 unsigned int ch = va_arg(ap,unsigned int);
26635 if( ch<0x00080 ){
26636 buf[0] = ch & 0xff;
26637 length = 1;
26638 }else if( ch<0x00800 ){
26639 buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
26640 buf[1] = 0x80 + (u8)(ch & 0x3f);
26641 length = 2;
26642 }else if( ch<0x10000 ){
26643 buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
26644 buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
26645 buf[2] = 0x80 + (u8)(ch & 0x3f);
26646 length = 3;
26647 }else{
26648 buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
26649 buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
26650 buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
26651 buf[3] = 0x80 + (u8)(ch & 0x3f);
26652 length = 4;
26653 }
26654 }
26655 if( precision>1 ){
26656 width -= precision-1;
26657 if( width>1 && !flag_leftjustify ){
26658 sqlite3AppendChar(pAccum, width-1, ' ');
26659 width = 0;
26660 }
26661 while( precision-- > 1 ){
26662 sqlite3StrAccumAppend(pAccum, buf, length);
26663 }
26664 }
 
 
26665 bufpt = buf;
26666 flag_altform2 = 1;
26667 goto adjust_width_for_utf8;
26668 case etSTRING:
26669 case etDYNSTRING:
26670 if( bArgList ){
26671 bufpt = getTextArg(pArgList);
26672 xtype = etSTRING;
@@ -26507,21 +26674,49 @@
26674 bufpt = va_arg(ap,char*);
26675 }
26676 if( bufpt==0 ){
26677 bufpt = "";
26678 }else if( xtype==etDYNSTRING ){
26679 if( pAccum->nChar==0 && pAccum->mxAlloc && width==0 && precision<0 ){
26680 /* Special optimization for sqlite3_mprintf("%z..."):
26681 ** Extend an existing memory allocation rather than creating
26682 ** a new one. */
26683 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
26684 pAccum->zText = bufpt;
26685 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
26686 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
26687 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
26688 length = 0;
26689 break;
26690 }
26691 zExtra = bufpt;
26692 }
26693 if( precision>=0 ){
26694 if( flag_altform2 ){
26695 /* Set length to the number of bytes needed in order to display
26696 ** precision characters */
26697 unsigned char *z = (unsigned char*)bufpt;
26698 while( precision-- > 0 && z[0] ){
26699 SQLITE_SKIP_UTF8(z);
26700 }
26701 length = (int)(z - (unsigned char*)bufpt);
26702 }else{
26703 for(length=0; length<precision && bufpt[length]; length++){}
26704 }
26705 }else{
26706 length = 0x7fffffff & (int)strlen(bufpt);
26707 }
26708 adjust_width_for_utf8:
26709 if( flag_altform2 && width>0 ){
26710 /* Adjust width to account for extra bytes in UTF-8 characters */
26711 int ii = length - 1;
26712 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
26713 }
26714 break;
26715 case etSQLESCAPE: /* %q: Escape ' characters */
26716 case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */
26717 case etSQLESCAPE3: { /* %w: Escape " characters */
26718 int i, j, k, n, isnull;
26719 int needQuote;
26720 char ch;
26721 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
26722 char *escarg;
@@ -26531,13 +26726,21 @@
26726 }else{
26727 escarg = va_arg(ap,char*);
26728 }
26729 isnull = escarg==0;
26730 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
26731 /* For %q, %Q, and %w, the precision is the number of byte (or
26732 ** characters if the ! flags is present) to use from the input.
26733 ** Because of the extra quoting characters inserted, the number
26734 ** of output characters may be larger than the precision.
26735 */
26736 k = precision;
26737 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
26738 if( ch==q ) n++;
26739 if( flag_altform2 && (ch&0xc0)==0xc0 ){
26740 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
26741 }
26742 }
26743 needQuote = !isnull && xtype==etSQLESCAPE2;
26744 n += i + 3;
26745 if( n>etBUFSIZE ){
26746 bufpt = zExtra = sqlite3Malloc( n );
@@ -26556,14 +26759,11 @@
26759 if( ch==q ) bufpt[j++] = ch;
26760 }
26761 if( needQuote ) bufpt[j++] = q;
26762 bufpt[j] = 0;
26763 length = j;
26764 goto adjust_width_for_utf8;
 
 
 
26765 }
26766 case etTOKEN: {
26767 Token *pToken;
26768 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
26769 pToken = va_arg(ap, Token*);
@@ -26598,11 +26798,14 @@
26798 }
26799 }/* End switch over the format type */
26800 /*
26801 ** The text of the conversion is pointed to by "bufpt" and is
26802 ** "length" characters long. The field width is "width". Do
26803 ** the output. Both length and width are in bytes, not characters,
26804 ** at this point. If the "!" flag was present on string conversions
26805 ** indicating that width and precision should be expressed in characters,
26806 ** then the values have been translated prior to reaching this point.
26807 */
26808 width -= length;
26809 if( width>0 ){
26810 if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
26811 sqlite3StrAccumAppend(pAccum, bufpt, length);
@@ -27267,10 +27470,15 @@
27470 break;
27471 }
27472 case TK_NULL: {
27473 sqlite3TreeViewLine(pView,"NULL");
27474 break;
27475 }
27476 case TK_TRUEFALSE: {
27477 sqlite3TreeViewLine(pView,
27478 sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
27479 break;
27480 }
27481 #ifndef SQLITE_OMIT_BLOB_LITERAL
27482 case TK_BLOB: {
27483 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
27484 break;
@@ -27323,10 +27531,23 @@
27531 case TK_UPLUS: zUniOp = "UPLUS"; break;
27532 case TK_BITNOT: zUniOp = "BITNOT"; break;
27533 case TK_NOT: zUniOp = "NOT"; break;
27534 case TK_ISNULL: zUniOp = "ISNULL"; break;
27535 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
27536
27537 case TK_TRUTH: {
27538 int x;
27539 const char *azOp[] = {
27540 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
27541 };
27542 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
27543 assert( pExpr->pRight );
27544 assert( pExpr->pRight->op==TK_TRUEFALSE );
27545 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
27546 zUniOp = azOp[x];
27547 break;
27548 }
27549
27550 case TK_SPAN: {
27551 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
27552 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
27553 break;
@@ -29061,11 +29282,11 @@
29282 ** routine does *not* accept hexadecimal notation.
29283 **
29284 ** Returns:
29285 **
29286 ** 0 Successful transformation. Fits in a 64-bit signed integer.
29287 ** 1 Excess non-space text after the integer value
29288 ** 2 Integer too large for a 64-bit signed integer or is malformed
29289 ** 3 Special case of 9223372036854775808
29290 **
29291 ** length is the number of bytes in the string (bytes, not characters).
29292 ** The string is not necessarily zero-terminated. The encoding is
@@ -29104,51 +29325,61 @@
29325 zStart = zNum;
29326 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
29327 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
29328 u = u*10 + c - '0';
29329 }
29330 testcase( i==18*incr );
29331 testcase( i==19*incr );
29332 testcase( i==20*incr );
29333 if( u>LARGEST_INT64 ){
29334 /* This test and assignment is needed only to suppress UB warnings
29335 ** from clang and -fsanitize=undefined. This test and assignment make
29336 ** the code a little larger and slower, and no harm comes from omitting
29337 ** them, but we must appaise the undefined-behavior pharisees. */
29338 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
29339 }else if( neg ){
29340 *pNum = -(i64)u;
29341 }else{
29342 *pNum = (i64)u;
29343 }
29344 rc = 0;
29345 if( (i==0 && zStart==zNum) /* No digits */
 
 
 
29346 || nonNum /* UTF16 with high-order bytes non-zero */
29347 ){
29348 rc = 1;
29349 }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */
29350 int jj = i;
29351 do{
29352 if( !sqlite3Isspace(zNum[jj]) ){
29353 rc = 1; /* Extra non-space text after the integer */
29354 break;
29355 }
29356 jj += incr;
29357 }while( &zNum[jj]<zEnd );
29358 }
29359 if( i<19*incr ){
29360 /* Less than 19 digits, so we know that it fits in 64 bits */
29361 assert( u<=LARGEST_INT64 );
29362 return rc;
29363 }else{
29364 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
29365 c = i>19*incr ? 1 : compare2pow63(zNum, incr);
29366 if( c<0 ){
29367 /* zNum is less than 9223372036854775808 so it fits */
29368 assert( u<=LARGEST_INT64 );
29369 return rc;
 
 
 
29370 }else{
29371 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
29372 if( c>0 ){
29373 /* zNum is greater than 9223372036854775808 so it overflows */
29374 return 2;
29375 }else{
29376 /* zNum is exactly 9223372036854775808. Fits if negative. The
29377 ** special case 2 overflow if positive */
29378 assert( u-1==LARGEST_INT64 );
29379 return neg ? rc : 3;
29380 }
29381 }
29382 }
29383 }
29384
29385 /*
@@ -30461,84 +30692,85 @@
30692 /* 91 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
30693 /* 92 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
30694 /* 93 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
30695 /* 94 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
30696 /* 95 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
30697 /* 96 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
30698 /* 97 */ "String8" OpHelp("r[P2]='P4'"),
30699 /* 98 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"),
30700 /* 99 */ "Column" OpHelp("r[P3]=PX"),
30701 /* 100 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
30702 /* 101 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
30703 /* 102 */ "Count" OpHelp("r[P2]=count()"),
30704 /* 103 */ "ReadCookie" OpHelp(""),
30705 /* 104 */ "SetCookie" OpHelp(""),
30706 /* 105 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
30707 /* 106 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
30708 /* 107 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
30709 /* 108 */ "OpenDup" OpHelp(""),
30710 /* 109 */ "OpenAutoindex" OpHelp("nColumn=P2"),
30711 /* 110 */ "OpenEphemeral" OpHelp("nColumn=P2"),
30712 /* 111 */ "SorterOpen" OpHelp(""),
30713 /* 112 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
30714 /* 113 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
30715 /* 114 */ "Close" OpHelp(""),
30716 /* 115 */ "ColumnsUsed" OpHelp(""),
30717 /* 116 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
30718 /* 117 */ "NewRowid" OpHelp("r[P2]=rowid"),
30719 /* 118 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
30720 /* 119 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
30721 /* 120 */ "Delete" OpHelp(""),
30722 /* 121 */ "ResetCount" OpHelp(""),
30723 /* 122 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
30724 /* 123 */ "SorterData" OpHelp("r[P2]=data"),
30725 /* 124 */ "RowData" OpHelp("r[P2]=data"),
30726 /* 125 */ "Rowid" OpHelp("r[P2]=rowid"),
30727 /* 126 */ "NullRow" OpHelp(""),
30728 /* 127 */ "SeekEnd" OpHelp(""),
30729 /* 128 */ "SorterInsert" OpHelp("key=r[P2]"),
30730 /* 129 */ "IdxInsert" OpHelp("key=r[P2]"),
30731 /* 130 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
30732 /* 131 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"),
30733 /* 132 */ "Real" OpHelp("r[P2]=P4"),
30734 /* 133 */ "IdxRowid" OpHelp("r[P2]=rowid"),
30735 /* 134 */ "Destroy" OpHelp(""),
30736 /* 135 */ "Clear" OpHelp(""),
30737 /* 136 */ "ResetSorter" OpHelp(""),
30738 /* 137 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"),
30739 /* 138 */ "SqlExec" OpHelp(""),
30740 /* 139 */ "ParseSchema" OpHelp(""),
30741 /* 140 */ "LoadAnalysis" OpHelp(""),
30742 /* 141 */ "DropTable" OpHelp(""),
30743 /* 142 */ "DropIndex" OpHelp(""),
30744 /* 143 */ "DropTrigger" OpHelp(""),
30745 /* 144 */ "IntegrityCk" OpHelp(""),
30746 /* 145 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
30747 /* 146 */ "Param" OpHelp(""),
30748 /* 147 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
30749 /* 148 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
30750 /* 149 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
30751 /* 150 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"),
30752 /* 151 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
30753 /* 152 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
30754 /* 153 */ "Expire" OpHelp(""),
30755 /* 154 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
30756 /* 155 */ "VBegin" OpHelp(""),
30757 /* 156 */ "VCreate" OpHelp(""),
30758 /* 157 */ "VDestroy" OpHelp(""),
30759 /* 158 */ "VOpen" OpHelp(""),
30760 /* 159 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
30761 /* 160 */ "VRename" OpHelp(""),
30762 /* 161 */ "Pagecount" OpHelp(""),
30763 /* 162 */ "MaxPgcnt" OpHelp(""),
30764 /* 163 */ "PureFunc0" OpHelp(""),
30765 /* 164 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
30766 /* 165 */ "PureFunc" OpHelp(""),
30767 /* 166 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
30768 /* 167 */ "Trace" OpHelp(""),
30769 /* 168 */ "CursorHint" OpHelp(""),
30770 /* 169 */ "Noop" OpHelp(""),
30771 /* 170 */ "Explain" OpHelp(""),
30772 };
30773 return azName[i];
30774 }
30775 #endif
30776
@@ -31210,11 +31442,15 @@
31442 #else
31443 { "fchown", (sqlite3_syscall_ptr)0, 0 },
31444 #endif
31445 #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
31446
31447 #if defined(HAVE_FCHOWN)
31448 { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 },
31449 #else
31450 { "geteuid", (sqlite3_syscall_ptr)0, 0 },
31451 #endif
31452 #define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent)
31453
31454 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
31455 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
31456 #else
@@ -31438,19 +31674,20 @@
31674 **
31675 ** unixEnterMutex()
31676 ** assert( unixMutexHeld() );
31677 ** unixEnterLeave()
31678 */
31679 static sqlite3_mutex *unixBigLock = 0;
31680 static void unixEnterMutex(void){
31681 sqlite3_mutex_enter(unixBigLock);
31682 }
31683 static void unixLeaveMutex(void){
31684 sqlite3_mutex_leave(unixBigLock);
31685 }
31686 #ifdef SQLITE_DEBUG
31687 static int unixMutexHeld(void) {
31688 return sqlite3_mutex_held(unixBigLock);
31689 }
31690 #endif
31691
31692
31693 #ifdef SQLITE_HAVE_OS_TRACE
@@ -34917,16 +35154,14 @@
35154 /* Locks are within range */
35155 assert( n>=1 && n<=SQLITE_SHM_NLOCK );
35156
35157 if( pShmNode->h>=0 ){
35158 /* Initialize the locking parameters */
 
35159 f.l_type = lockType;
35160 f.l_whence = SEEK_SET;
35161 f.l_start = ofst;
35162 f.l_len = n;
 
35163 rc = osFcntl(pShmNode->h, F_SETLK, &f);
35164 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
35165 }
35166
35167 /* Update the global lock state and do debug tracing */
@@ -36588,11 +36823,10 @@
36823 */
36824 if( randomnessPid!=osGetpid(0) ){
36825 randomnessPid = osGetpid(0);
36826 sqlite3_randomness(0,0);
36827 }
 
36828 memset(p, 0, sizeof(unixFile));
36829
36830 if( eType==SQLITE_OPEN_MAIN_DB ){
36831 UnixUnusedFd *pUnused;
36832 pUnused = findReusableFd(zName, flags);
@@ -38463,10 +38697,11 @@
38697
38698 /* Register all VFSes defined in the aVfs[] array */
38699 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
38700 sqlite3_vfs_register(&aVfs[i], i==0);
38701 }
38702 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
38703 return SQLITE_OK;
38704 }
38705
38706 /*
38707 ** Shutdown the operating system interface.
@@ -38474,10 +38709,11 @@
38709 ** Some operating systems might need to do some cleanup in this routine,
38710 ** to release dynamically allocated objects. But not on unix.
38711 ** This routine is a no-op for unix.
38712 */
38713 SQLITE_API int sqlite3_os_end(void){
38714 unixBigLock = 0;
38715 return SQLITE_OK;
38716 }
38717
38718 #endif /* SQLITE_OS_UNIX */
38719
@@ -42312,19 +42548,20 @@
42548 **
42549 ** winShmEnterMutex()
42550 ** assert( winShmMutexHeld() );
42551 ** winShmLeaveMutex()
42552 */
42553 static sqlite3_mutex *winBigLock = 0;
42554 static void winShmEnterMutex(void){
42555 sqlite3_mutex_enter(winBigLock);
42556 }
42557 static void winShmLeaveMutex(void){
42558 sqlite3_mutex_leave(winBigLock);
42559 }
42560 #ifndef NDEBUG
42561 static int winShmMutexHeld(void) {
42562 return sqlite3_mutex_held(winBigLock);
42563 }
42564 #endif
42565
42566 /*
42567 ** Object used to represent a single file opened and mmapped to provide
@@ -44742,10 +44979,14 @@
44979 sqlite3_vfs_register(&winNolockVfs, 0);
44980
44981 #if defined(SQLITE_WIN32_HAS_WIDE)
44982 sqlite3_vfs_register(&winLongPathNolockVfs, 0);
44983 #endif
44984
44985 #ifndef SQLITE_OMIT_WAL
44986 winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
44987 #endif
44988
44989 return SQLITE_OK;
44990 }
44991
44992 SQLITE_API int sqlite3_os_end(void){
@@ -44753,16 +44994,613 @@
44994 if( sleepObj!=NULL ){
44995 osCloseHandle(sleepObj);
44996 sleepObj = NULL;
44997 }
44998 #endif
44999
45000 #ifndef SQLITE_OMIT_WAL
45001 winBigLock = 0;
45002 #endif
45003
45004 return SQLITE_OK;
45005 }
45006
45007 #endif /* SQLITE_OS_WIN */
45008
45009 /************** End of os_win.c **********************************************/
45010 /************** Begin file memdb.c *******************************************/
45011 /*
45012 ** 2016-09-07
45013 **
45014 ** The author disclaims copyright to this source code. In place of
45015 ** a legal notice, here is a blessing:
45016 **
45017 ** May you do good and not evil.
45018 ** May you find forgiveness for yourself and forgive others.
45019 ** May you share freely, never taking more than you give.
45020 **
45021 ******************************************************************************
45022 **
45023 ** This file implements in-memory VFS. A database is held as a contiguous
45024 ** block of memory.
45025 **
45026 ** This file also implements interface sqlite3_serialize() and
45027 ** sqlite3_deserialize().
45028 */
45029 #ifdef SQLITE_ENABLE_DESERIALIZE
45030 /* #include "sqliteInt.h" */
45031
45032 /*
45033 ** Forward declaration of objects used by this utility
45034 */
45035 typedef struct sqlite3_vfs MemVfs;
45036 typedef struct MemFile MemFile;
45037
45038 /* Access to a lower-level VFS that (might) implement dynamic loading,
45039 ** access to randomness, etc.
45040 */
45041 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
45042
45043 /* An open file */
45044 struct MemFile {
45045 sqlite3_file base; /* IO methods */
45046 sqlite3_int64 sz; /* Size of the file */
45047 sqlite3_int64 szMax; /* Space allocated to aData */
45048 unsigned char *aData; /* content of the file */
45049 int nMmap; /* Number of memory mapped pages */
45050 unsigned mFlags; /* Flags */
45051 int eLock; /* Most recent lock against this file */
45052 };
45053
45054 /*
45055 ** Methods for MemFile
45056 */
45057 static int memdbClose(sqlite3_file*);
45058 static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
45059 static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
45060 static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
45061 static int memdbSync(sqlite3_file*, int flags);
45062 static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
45063 static int memdbLock(sqlite3_file*, int);
45064 /* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
45065 static int memdbFileControl(sqlite3_file*, int op, void *pArg);
45066 /* static int memdbSectorSize(sqlite3_file*); // not used */
45067 static int memdbDeviceCharacteristics(sqlite3_file*);
45068 static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
45069 static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
45070
45071 /*
45072 ** Methods for MemVfs
45073 */
45074 static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
45075 /* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
45076 static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
45077 static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
45078 static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
45079 static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
45080 static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
45081 static void memdbDlClose(sqlite3_vfs*, void*);
45082 static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
45083 static int memdbSleep(sqlite3_vfs*, int microseconds);
45084 /* static int memdbCurrentTime(sqlite3_vfs*, double*); */
45085 static int memdbGetLastError(sqlite3_vfs*, int, char *);
45086 static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
45087
45088 static sqlite3_vfs memdb_vfs = {
45089 2, /* iVersion */
45090 0, /* szOsFile (set when registered) */
45091 1024, /* mxPathname */
45092 0, /* pNext */
45093 "memdb", /* zName */
45094 0, /* pAppData (set when registered) */
45095 memdbOpen, /* xOpen */
45096 0, /* memdbDelete, */ /* xDelete */
45097 memdbAccess, /* xAccess */
45098 memdbFullPathname, /* xFullPathname */
45099 memdbDlOpen, /* xDlOpen */
45100 memdbDlError, /* xDlError */
45101 memdbDlSym, /* xDlSym */
45102 memdbDlClose, /* xDlClose */
45103 memdbRandomness, /* xRandomness */
45104 memdbSleep, /* xSleep */
45105 0, /* memdbCurrentTime, */ /* xCurrentTime */
45106 memdbGetLastError, /* xGetLastError */
45107 memdbCurrentTimeInt64 /* xCurrentTimeInt64 */
45108 };
45109
45110 static const sqlite3_io_methods memdb_io_methods = {
45111 3, /* iVersion */
45112 memdbClose, /* xClose */
45113 memdbRead, /* xRead */
45114 memdbWrite, /* xWrite */
45115 memdbTruncate, /* xTruncate */
45116 memdbSync, /* xSync */
45117 memdbFileSize, /* xFileSize */
45118 memdbLock, /* xLock */
45119 memdbLock, /* xUnlock - same as xLock in this case */
45120 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
45121 memdbFileControl, /* xFileControl */
45122 0, /* memdbSectorSize,*/ /* xSectorSize */
45123 memdbDeviceCharacteristics, /* xDeviceCharacteristics */
45124 0, /* xShmMap */
45125 0, /* xShmLock */
45126 0, /* xShmBarrier */
45127 0, /* xShmUnmap */
45128 memdbFetch, /* xFetch */
45129 memdbUnfetch /* xUnfetch */
45130 };
45131
45132
45133
45134 /*
45135 ** Close an memdb-file.
45136 **
45137 ** The pData pointer is owned by the application, so there is nothing
45138 ** to free.
45139 */
45140 static int memdbClose(sqlite3_file *pFile){
45141 MemFile *p = (MemFile *)pFile;
45142 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ) sqlite3_free(p->aData);
45143 return SQLITE_OK;
45144 }
45145
45146 /*
45147 ** Read data from an memdb-file.
45148 */
45149 static int memdbRead(
45150 sqlite3_file *pFile,
45151 void *zBuf,
45152 int iAmt,
45153 sqlite_int64 iOfst
45154 ){
45155 MemFile *p = (MemFile *)pFile;
45156 if( iOfst+iAmt>p->sz ){
45157 memset(zBuf, 0, iAmt);
45158 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
45159 return SQLITE_IOERR_SHORT_READ;
45160 }
45161 memcpy(zBuf, p->aData+iOfst, iAmt);
45162 return SQLITE_OK;
45163 }
45164
45165 /*
45166 ** Try to enlarge the memory allocation to hold at least sz bytes
45167 */
45168 static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){
45169 unsigned char *pNew;
45170 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){
45171 return SQLITE_FULL;
45172 }
45173 pNew = sqlite3_realloc64(p->aData, newSz);
45174 if( pNew==0 ) return SQLITE_NOMEM;
45175 p->aData = pNew;
45176 p->szMax = newSz;
45177 return SQLITE_OK;
45178 }
45179
45180 /*
45181 ** Write data to an memdb-file.
45182 */
45183 static int memdbWrite(
45184 sqlite3_file *pFile,
45185 const void *z,
45186 int iAmt,
45187 sqlite_int64 iOfst
45188 ){
45189 MemFile *p = (MemFile *)pFile;
45190 if( iOfst+iAmt>p->sz ){
45191 int rc;
45192 if( iOfst+iAmt>p->szMax
45193 && (rc = memdbEnlarge(p, (iOfst+iAmt)*2))!=SQLITE_OK
45194 ){
45195 return rc;
45196 }
45197 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
45198 p->sz = iOfst+iAmt;
45199 }
45200 memcpy(p->aData+iOfst, z, iAmt);
45201 return SQLITE_OK;
45202 }
45203
45204 /*
45205 ** Truncate an memdb-file.
45206 **
45207 ** In rollback mode (which is always the case for memdb, as it does not
45208 ** support WAL mode) the truncate() method is only used to reduce
45209 ** the size of a file, never to increase the size.
45210 */
45211 static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
45212 MemFile *p = (MemFile *)pFile;
45213 if( NEVER(size>p->sz) ) return SQLITE_FULL;
45214 p->sz = size;
45215 return SQLITE_OK;
45216 }
45217
45218 /*
45219 ** Sync an memdb-file.
45220 */
45221 static int memdbSync(sqlite3_file *pFile, int flags){
45222 return SQLITE_OK;
45223 }
45224
45225 /*
45226 ** Return the current file-size of an memdb-file.
45227 */
45228 static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
45229 MemFile *p = (MemFile *)pFile;
45230 *pSize = p->sz;
45231 return SQLITE_OK;
45232 }
45233
45234 /*
45235 ** Lock an memdb-file.
45236 */
45237 static int memdbLock(sqlite3_file *pFile, int eLock){
45238 MemFile *p = (MemFile *)pFile;
45239 p->eLock = eLock;
45240 return SQLITE_OK;
45241 }
45242
45243 #if 0 /* Never used because memdbAccess() always returns false */
45244 /*
45245 ** Check if another file-handle holds a RESERVED lock on an memdb-file.
45246 */
45247 static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
45248 *pResOut = 0;
45249 return SQLITE_OK;
45250 }
45251 #endif
45252
45253 /*
45254 ** File control method. For custom operations on an memdb-file.
45255 */
45256 static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
45257 MemFile *p = (MemFile *)pFile;
45258 int rc = SQLITE_NOTFOUND;
45259 if( op==SQLITE_FCNTL_VFSNAME ){
45260 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
45261 rc = SQLITE_OK;
45262 }
45263 return rc;
45264 }
45265
45266 #if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
45267 /*
45268 ** Return the sector-size in bytes for an memdb-file.
45269 */
45270 static int memdbSectorSize(sqlite3_file *pFile){
45271 return 1024;
45272 }
45273 #endif
45274
45275 /*
45276 ** Return the device characteristic flags supported by an memdb-file.
45277 */
45278 static int memdbDeviceCharacteristics(sqlite3_file *pFile){
45279 return SQLITE_IOCAP_ATOMIC |
45280 SQLITE_IOCAP_POWERSAFE_OVERWRITE |
45281 SQLITE_IOCAP_SAFE_APPEND |
45282 SQLITE_IOCAP_SEQUENTIAL;
45283 }
45284
45285 /* Fetch a page of a memory-mapped file */
45286 static int memdbFetch(
45287 sqlite3_file *pFile,
45288 sqlite3_int64 iOfst,
45289 int iAmt,
45290 void **pp
45291 ){
45292 MemFile *p = (MemFile *)pFile;
45293 p->nMmap++;
45294 *pp = (void*)(p->aData + iOfst);
45295 return SQLITE_OK;
45296 }
45297
45298 /* Release a memory-mapped page */
45299 static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
45300 MemFile *p = (MemFile *)pFile;
45301 p->nMmap--;
45302 return SQLITE_OK;
45303 }
45304
45305 /*
45306 ** Open an mem file handle.
45307 */
45308 static int memdbOpen(
45309 sqlite3_vfs *pVfs,
45310 const char *zName,
45311 sqlite3_file *pFile,
45312 int flags,
45313 int *pOutFlags
45314 ){
45315 MemFile *p = (MemFile*)pFile;
45316 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
45317 return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags);
45318 }
45319 memset(p, 0, sizeof(*p));
45320 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
45321 assert( pOutFlags!=0 ); /* True because flags==SQLITE_OPEN_MAIN_DB */
45322 *pOutFlags = flags | SQLITE_OPEN_MEMORY;
45323 p->base.pMethods = &memdb_io_methods;
45324 return SQLITE_OK;
45325 }
45326
45327 #if 0 /* Only used to delete rollback journals, master journals, and WAL
45328 ** files, none of which exist in memdb. So this routine is never used */
45329 /*
45330 ** Delete the file located at zPath. If the dirSync argument is true,
45331 ** ensure the file-system modifications are synced to disk before
45332 ** returning.
45333 */
45334 static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
45335 return SQLITE_IOERR_DELETE;
45336 }
45337 #endif
45338
45339 /*
45340 ** Test for access permissions. Return true if the requested permission
45341 ** is available, or false otherwise.
45342 **
45343 ** With memdb, no files ever exist on disk. So always return false.
45344 */
45345 static int memdbAccess(
45346 sqlite3_vfs *pVfs,
45347 const char *zPath,
45348 int flags,
45349 int *pResOut
45350 ){
45351 *pResOut = 0;
45352 return SQLITE_OK;
45353 }
45354
45355 /*
45356 ** Populate buffer zOut with the full canonical pathname corresponding
45357 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
45358 ** of at least (INST_MAX_PATHNAME+1) bytes.
45359 */
45360 static int memdbFullPathname(
45361 sqlite3_vfs *pVfs,
45362 const char *zPath,
45363 int nOut,
45364 char *zOut
45365 ){
45366 sqlite3_snprintf(nOut, zOut, "%s", zPath);
45367 return SQLITE_OK;
45368 }
45369
45370 /*
45371 ** Open the dynamic library located at zPath and return a handle.
45372 */
45373 static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
45374 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
45375 }
45376
45377 /*
45378 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
45379 ** utf-8 string describing the most recent error encountered associated
45380 ** with dynamic libraries.
45381 */
45382 static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
45383 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
45384 }
45385
45386 /*
45387 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
45388 */
45389 static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
45390 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
45391 }
45392
45393 /*
45394 ** Close the dynamic library handle pHandle.
45395 */
45396 static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
45397 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
45398 }
45399
45400 /*
45401 ** Populate the buffer pointed to by zBufOut with nByte bytes of
45402 ** random data.
45403 */
45404 static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
45405 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
45406 }
45407
45408 /*
45409 ** Sleep for nMicro microseconds. Return the number of microseconds
45410 ** actually slept.
45411 */
45412 static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
45413 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
45414 }
45415
45416 #if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */
45417 /*
45418 ** Return the current time as a Julian Day number in *pTimeOut.
45419 */
45420 static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
45421 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
45422 }
45423 #endif
45424
45425 static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
45426 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
45427 }
45428 static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
45429 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
45430 }
45431
45432 /*
45433 ** Translate a database connection pointer and schema name into a
45434 ** MemFile pointer.
45435 */
45436 static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
45437 MemFile *p = 0;
45438 int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
45439 if( rc ) return 0;
45440 if( p->base.pMethods!=&memdb_io_methods ) return 0;
45441 return p;
45442 }
45443
45444 /*
45445 ** Return the serialization of a database
45446 */
45447 SQLITE_API unsigned char *sqlite3_serialize(
45448 sqlite3 *db, /* The database connection */
45449 const char *zSchema, /* Which database within the connection */
45450 sqlite3_int64 *piSize, /* Write size here, if not NULL */
45451 unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */
45452 ){
45453 MemFile *p;
45454 int iDb;
45455 Btree *pBt;
45456 sqlite3_int64 sz;
45457 int szPage = 0;
45458 sqlite3_stmt *pStmt = 0;
45459 unsigned char *pOut;
45460 char *zSql;
45461 int rc;
45462
45463 #ifdef SQLITE_ENABLE_API_ARMOR
45464 if( !sqlite3SafetyCheckOk(db) ){
45465 (void)SQLITE_MISUSE_BKPT;
45466 return 0;
45467 }
45468 #endif
45469
45470 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
45471 p = memdbFromDbSchema(db, zSchema);
45472 iDb = sqlite3FindDbName(db, zSchema);
45473 if( piSize ) *piSize = -1;
45474 if( iDb<0 ) return 0;
45475 if( p ){
45476 if( piSize ) *piSize = p->sz;
45477 if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
45478 pOut = p->aData;
45479 }else{
45480 pOut = sqlite3_malloc64( p->sz );
45481 if( pOut ) memcpy(pOut, p->aData, p->sz);
45482 }
45483 return pOut;
45484 }
45485 pBt = db->aDb[iDb].pBt;
45486 if( pBt==0 ) return 0;
45487 szPage = sqlite3BtreeGetPageSize(pBt);
45488 zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
45489 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
45490 sqlite3_free(zSql);
45491 if( rc ) return 0;
45492 rc = sqlite3_step(pStmt);
45493 if( rc!=SQLITE_ROW ){
45494 pOut = 0;
45495 }else{
45496 sz = sqlite3_column_int64(pStmt, 0)*szPage;
45497 if( piSize ) *piSize = sz;
45498 if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
45499 pOut = 0;
45500 }else{
45501 pOut = sqlite3_malloc64( sz );
45502 if( pOut ){
45503 int nPage = sqlite3_column_int(pStmt, 0);
45504 Pager *pPager = sqlite3BtreePager(pBt);
45505 int pgno;
45506 for(pgno=1; pgno<=nPage; pgno++){
45507 DbPage *pPage = 0;
45508 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
45509 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
45510 if( rc==SQLITE_OK ){
45511 memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
45512 }else{
45513 memset(pTo, 0, szPage);
45514 }
45515 sqlite3PagerUnref(pPage);
45516 }
45517 }
45518 }
45519 }
45520 sqlite3_finalize(pStmt);
45521 return pOut;
45522 }
45523
45524 /* Convert zSchema to a MemDB and initialize its content.
45525 */
45526 SQLITE_API int sqlite3_deserialize(
45527 sqlite3 *db, /* The database connection */
45528 const char *zSchema, /* Which DB to reopen with the deserialization */
45529 unsigned char *pData, /* The serialized database content */
45530 sqlite3_int64 szDb, /* Number bytes in the deserialization */
45531 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
45532 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
45533 ){
45534 MemFile *p;
45535 char *zSql;
45536 sqlite3_stmt *pStmt = 0;
45537 int rc;
45538 int iDb;
45539
45540 #ifdef SQLITE_ENABLE_API_ARMOR
45541 if( !sqlite3SafetyCheckOk(db) ){
45542 return SQLITE_MISUSE_BKPT;
45543 }
45544 if( szDb<0 ) return SQLITE_MISUSE_BKPT;
45545 if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
45546 #endif
45547
45548 sqlite3_mutex_enter(db->mutex);
45549 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
45550 iDb = sqlite3FindDbName(db, zSchema);
45551 if( iDb<0 ){
45552 rc = SQLITE_ERROR;
45553 goto end_deserialize;
45554 }
45555 zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
45556 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
45557 sqlite3_free(zSql);
45558 if( rc ) goto end_deserialize;
45559 db->init.iDb = (u8)iDb;
45560 db->init.reopenMemdb = 1;
45561 rc = sqlite3_step(pStmt);
45562 db->init.reopenMemdb = 0;
45563 if( rc!=SQLITE_DONE ){
45564 rc = SQLITE_ERROR;
45565 goto end_deserialize;
45566 }
45567 p = memdbFromDbSchema(db, zSchema);
45568 if( p==0 ){
45569 rc = SQLITE_ERROR;
45570 }else{
45571 p->aData = pData;
45572 p->sz = szDb;
45573 p->szMax = szBuf;
45574 p->mFlags = mFlags;
45575 rc = SQLITE_OK;
45576 }
45577
45578 end_deserialize:
45579 sqlite3_finalize(pStmt);
45580 sqlite3_mutex_leave(db->mutex);
45581 return rc;
45582 }
45583
45584 /*
45585 ** This routine is called when the extension is loaded.
45586 ** Register the new VFS.
45587 */
45588 SQLITE_PRIVATE int sqlite3MemdbInit(void){
45589 sqlite3_vfs *pLower = sqlite3_vfs_find(0);
45590 int sz = pLower->szOsFile;
45591 memdb_vfs.pAppData = pLower;
45592 /* In all known configurations of SQLite, the size of a default
45593 ** sqlite3_file is greater than the size of a memdb sqlite3_file.
45594 ** Should that ever change, remove the following NEVER() */
45595 if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile);
45596 memdb_vfs.szOsFile = sz;
45597 return sqlite3_vfs_register(&memdb_vfs, 0);
45598 }
45599 #endif /* SQLITE_ENABLE_DESERIALIZE */
45600
45601 /************** End of memdb.c ***********************************************/
45602 /************** Begin file bitvec.c ******************************************/
45603 /*
45604 ** 2008 February 16
45605 **
45606 ** The author disclaims copyright to this source code. In place of
@@ -45607,11 +46445,11 @@
46445 int rc;
46446 #ifdef SQLITE_LOG_CACHE_SPILL
46447 sqlite3_log(SQLITE_FULL,
46448 "spill page %d making room for %d - cache used: %d/%d",
46449 pPg->pgno, pgno,
46450 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
46451 numberOfCachePages(pCache));
46452 #endif
46453 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
46454 rc = pCache->xStress(pCache->pStress, pPg);
46455 pcacheDump(pCache);
@@ -48663,11 +49501,11 @@
49501 i64 journalSizeLimit; /* Size limit for persistent journal files */
49502 char *zFilename; /* Name of the database file */
49503 char *zJournal; /* Name of the journal file */
49504 int (*xBusyHandler)(void*); /* Function to call when busy */
49505 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
49506 int aStat[4]; /* Total cache hits, misses, writes, spills */
49507 #ifdef SQLITE_TEST
49508 int nRead; /* Database pages read */
49509 #endif
49510 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
49511 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
@@ -48691,10 +49529,11 @@
49529 ** or CACHE_WRITE to sqlite3_db_status().
49530 */
49531 #define PAGER_STAT_HIT 0
49532 #define PAGER_STAT_MISS 1
49533 #define PAGER_STAT_WRITE 2
49534 #define PAGER_STAT_SPILL 3
49535
49536 /*
49537 ** The following global variables hold counters used for
49538 ** testing purposes only. These variables do not exist in
49539 ** a non-testing build. These variables are not thread-safe.
@@ -49177,11 +50016,11 @@
50016 #else
50017 UNUSED_PARAMETER(pPager);
50018 #endif
50019
50020 #ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
50021 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
50022 return -1;
50023 }
50024 #endif
50025
50026 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
@@ -52066,10 +52905,34 @@
52905 pNext = p->pDirty;
52906 sqlite3_free(p);
52907 }
52908 }
52909
52910 /* Verify that the database file has not be deleted or renamed out from
52911 ** under the pager. Return SQLITE_OK if the database is still where it ought
52912 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
52913 ** code from sqlite3OsAccess()) if the database has gone missing.
52914 */
52915 static int databaseIsUnmoved(Pager *pPager){
52916 int bHasMoved = 0;
52917 int rc;
52918
52919 if( pPager->tempFile ) return SQLITE_OK;
52920 if( pPager->dbSize==0 ) return SQLITE_OK;
52921 assert( pPager->zFilename && pPager->zFilename[0] );
52922 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
52923 if( rc==SQLITE_NOTFOUND ){
52924 /* If the HAS_MOVED file-control is unimplemented, assume that the file
52925 ** has not been moved. That is the historical behavior of SQLite: prior to
52926 ** version 3.8.3, it never checked */
52927 rc = SQLITE_OK;
52928 }else if( rc==SQLITE_OK && bHasMoved ){
52929 rc = SQLITE_READONLY_DBMOVED;
52930 }
52931 return rc;
52932 }
52933
52934
52935 /*
52936 ** Shutdown the page cache. Free all memory and close all files.
52937 **
52938 ** If a transaction was in progress when this routine is called, that
@@ -52082,25 +52945,30 @@
52945 ** is made to roll it back. If an error occurs during the rollback
52946 ** a hot journal may be left in the filesystem but no error is returned
52947 ** to the caller.
52948 */
52949 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
52950 u8 *pTmp = (u8*)pPager->pTmpSpace;
 
52951 assert( db || pagerUseWal(pPager)==0 );
52952 assert( assert_pager_state(pPager) );
52953 disable_simulated_io_errors();
52954 sqlite3BeginBenignMalloc();
52955 pagerFreeMapHdrs(pPager);
52956 /* pPager->errCode = 0; */
52957 pPager->exclusiveMode = 0;
52958 #ifndef SQLITE_OMIT_WAL
52959 {
52960 u8 *a = 0;
52961 assert( db || pPager->pWal==0 );
52962 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
52963 && SQLITE_OK==databaseIsUnmoved(pPager)
52964 ){
52965 a = pTmp;
52966 }
52967 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
52968 pPager->pWal = 0;
52969 }
52970 #endif
52971 pager_reset(pPager);
52972 if( MEMDB ){
52973 pager_unlock(pPager);
52974 }else{
@@ -52553,10 +53421,11 @@
53421 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
53422 ){
53423 return SQLITE_OK;
53424 }
53425
53426 pPager->aStat[PAGER_STAT_SPILL]++;
53427 pPg->pDirty = 0;
53428 if( pagerUseWal(pPager) ){
53429 /* Write a single frame for this page to the log. */
53430 rc = subjournalPageIfRequired(pPg);
53431 if( rc==SQLITE_OK ){
@@ -52658,10 +53527,15 @@
53527 u8 *pPtr;
53528 Pager *pPager = 0; /* Pager object to allocate and return */
53529 int rc = SQLITE_OK; /* Return code */
53530 int tempFile = 0; /* True for temp files (incl. in-memory files) */
53531 int memDb = 0; /* True if this is an in-memory file */
53532 #ifdef SQLITE_ENABLE_DESERIALIZE
53533 int memJM = 0; /* Memory journal mode */
53534 #else
53535 # define memJM 0
53536 #endif
53537 int readOnly = 0; /* True if this is a read-only file */
53538 int journalFileSize; /* Bytes to allocate for each journal fd */
53539 char *zPathname = 0; /* Full path to database file */
53540 int nPathname = 0; /* Number of bytes in zPathname */
53541 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
@@ -52785,11 +53659,14 @@
53659 */
53660 if( zFilename && zFilename[0] ){
53661 int fout = 0; /* VFS flags returned by xOpen() */
53662 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
53663 assert( !memDb );
53664 #ifdef SQLITE_ENABLE_DESERIALIZE
53665 memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
53666 #endif
53667 readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
53668
53669 /* If the file was successfully opened for read/write access,
53670 ** choose a default page size in case we have to create the
53671 ** database file. The default page size is the maximum of:
53672 **
@@ -52916,11 +53793,11 @@
53793 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
53794 assert( isOpen(pPager->fd) || tempFile );
53795 setSectorSize(pPager);
53796 if( !useJournal ){
53797 pPager->journalMode = PAGER_JOURNALMODE_OFF;
53798 }else if( memDb || memJM ){
53799 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
53800 }
53801 /* pPager->xBusyHandler = 0; */
53802 /* pPager->pBusyHandlerArg = 0; */
53803 pPager->xReiniter = xReinit;
@@ -52931,34 +53808,10 @@
53808 *ppPager = pPager;
53809 return SQLITE_OK;
53810 }
53811
53812
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53813
53814 /*
53815 ** This function is called after transitioning from PAGER_UNLOCK to
53816 ** PAGER_SHARED state. It tests if there is a hot journal present in
53817 ** the file-system for the given pager. A hot journal is one that
@@ -54463,12 +55316,13 @@
55316 }
55317 rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
55318 if( bBatch ){
55319 if( rc==SQLITE_OK ){
55320 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
55321 }
55322 if( rc!=SQLITE_OK ){
55323 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
55324 }
55325 }
55326
55327 if( rc!=SQLITE_OK ){
55328 assert( rc!=SQLITE_IOERR_BLOCKED );
@@ -54688,30 +55542,37 @@
55542 return a;
55543 }
55544 #endif
55545
55546 /*
55547 ** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
55548 ** or _WRITE+1. The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
55549 ** of SQLITE_DBSTATUS_CACHE_SPILL. The _SPILL case is not contiguous because
55550 ** it was added later.
55551 **
55552 ** Before returning, *pnVal is incremented by the
55553 ** current cache hit or miss count, according to the value of eStat. If the
55554 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
55555 ** returning.
55556 */
55557 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
55558
55559 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
55560 || eStat==SQLITE_DBSTATUS_CACHE_MISS
55561 || eStat==SQLITE_DBSTATUS_CACHE_WRITE
55562 || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
55563 );
55564
55565 assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
55566 assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
55567 assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
55568 && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
55569
55570 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
55571 *pnVal += pPager->aStat[eStat];
55572 if( reset ){
55573 pPager->aStat[eStat] = 0;
55574 }
55575 }
55576
55577 /*
55578 ** Return true if this is an in-memory or temp-file backed pager.
@@ -56153,11 +57014,15 @@
57014 **
57015 ** If this call is successful, *ppPage is set to point to the wal-index
57016 ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
57017 ** then an SQLite error code is returned and *ppPage is set to 0.
57018 */
57019 static SQLITE_NOINLINE int walIndexPageRealloc(
57020 Wal *pWal, /* The WAL context */
57021 int iPage, /* The page we seek */
57022 volatile u32 **ppPage /* Write the page pointer here */
57023 ){
57024 int rc = SQLITE_OK;
57025
57026 /* Enlarge the pWal->apWiData[] array if required */
57027 if( pWal->nWiData<=iPage ){
57028 int nByte = sizeof(u32*)*(iPage+1);
@@ -56172,32 +57037,41 @@
57037 pWal->apWiData = apNew;
57038 pWal->nWiData = iPage+1;
57039 }
57040
57041 /* Request a pointer to the required page from the VFS */
57042 assert( pWal->apWiData[iPage]==0 );
57043 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
57044 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
57045 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
57046 }else{
57047 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
57048 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
57049 );
57050 assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
57051 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
57052 if( (rc&0xff)==SQLITE_READONLY ){
57053 pWal->readOnly |= WAL_SHM_RDONLY;
57054 if( rc==SQLITE_READONLY ){
57055 rc = SQLITE_OK;
 
57056 }
57057 }
57058 }
57059
57060 *ppPage = pWal->apWiData[iPage];
57061 assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
57062 return rc;
57063 }
57064 static int walIndexPage(
57065 Wal *pWal, /* The WAL context */
57066 int iPage, /* The page we seek */
57067 volatile u32 **ppPage /* Write the page pointer here */
57068 ){
57069 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
57070 return walIndexPageRealloc(pWal, iPage, ppPage);
57071 }
57072 return SQLITE_OK;
57073 }
57074
57075 /*
57076 ** Return a pointer to the WalCkptInfo structure in the wal-index.
57077 */
@@ -57171,21 +58045,22 @@
58045 sqlite3_free(p);
58046 }
58047
58048 /*
58049 ** Construct a WalInterator object that can be used to loop over all
58050 ** pages in the WAL following frame nBackfill in ascending order. Frames
58051 ** nBackfill or earlier may be included - excluding them is an optimization
58052 ** only. The caller must hold the checkpoint lock.
58053 **
58054 ** On success, make *pp point to the newly allocated WalInterator object
58055 ** return SQLITE_OK. Otherwise, return an error code. If this routine
58056 ** returns an error, the value of *pp is undefined.
58057 **
58058 ** The calling routine should invoke walIteratorFree() to destroy the
58059 ** WalIterator object when it has finished with it.
58060 */
58061 static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
58062 WalIterator *p; /* Return value */
58063 int nSegment; /* Number of segments to merge */
58064 u32 iLast; /* Last frame in log */
58065 int nByte; /* Number of bytes to allocate */
58066 int i; /* Iterator variable */
@@ -57218,11 +58093,11 @@
58093 );
58094 if( !aTmp ){
58095 rc = SQLITE_NOMEM_BKPT;
58096 }
58097
58098 for(i=walFramePage(nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
58099 volatile ht_slot *aHash;
58100 u32 iZero;
58101 volatile u32 *aPgno;
58102
58103 rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
@@ -57252,10 +58127,11 @@
58127 }
58128 sqlite3_free(aTmp);
58129
58130 if( rc!=SQLITE_OK ){
58131 walIteratorFree(p);
58132 p = 0;
58133 }
58134 *pp = p;
58135 return rc;
58136 }
58137
@@ -57374,17 +58250,10 @@
58250 testcase( szPage<=32768 );
58251 testcase( szPage>=65536 );
58252 pInfo = walCkptInfo(pWal);
58253 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
58254
 
 
 
 
 
 
 
58255 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
58256 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
58257 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
58258
58259 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
@@ -57417,11 +58286,17 @@
58286 goto walcheckpoint_out;
58287 }
58288 }
58289 }
58290
58291 /* Allocate the iterator */
58292 if( pInfo->nBackfill<mxSafeFrame ){
58293 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
58294 assert( rc==SQLITE_OK || pIter==0 );
58295 }
58296
58297 if( pIter
58298 && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK
58299 ){
58300 i64 nSize; /* Current size of database file */
58301 u32 nBackfill = pInfo->nBackfill;
58302
@@ -58467,11 +59342,11 @@
59342 ** (iFrame<=iLast):
59343 ** This condition filters out entries that were added to the hash
59344 ** table after the current read-transaction had started.
59345 */
59346 iMinHash = walFramePage(pWal->minFrame);
59347 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
59348 volatile ht_slot *aHash; /* Pointer to hash table */
59349 volatile u32 *aPgno; /* Pointer to array of page numbers */
59350 u32 iZero; /* Frame number corresponding to aPgno[0] */
59351 int iKey; /* Hash slot index */
59352 int nCollide; /* Number of hash collisions remaining */
@@ -58490,10 +59365,11 @@
59365 }
59366 if( (nCollide--)==0 ){
59367 return SQLITE_CORRUPT_BKPT;
59368 }
59369 }
59370 if( iRead ) break;
59371 }
59372
59373 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
59374 /* If expensive assert() statements are available, do a linear search
59375 ** of the wal-index file content. Make sure the results agree with the
@@ -59904,24 +60780,24 @@
60780 struct BtCursor {
60781 u8 eState; /* One of the CURSOR_XXX constants (see below) */
60782 u8 curFlags; /* zero or more BTCF_* flags defined below */
60783 u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */
60784 u8 hints; /* As configured by CursorSetHints() */
 
 
 
 
 
 
 
 
 
60785 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
60786 ** Error code if eState==CURSOR_FAULT */
60787 Btree *pBtree; /* The Btree to which this cursor belongs */
60788 Pgno *aOverflow; /* Cache of overflow page locations */
60789 void *pKey; /* Saved key that was cursor last known position */
60790 /* All fields above are zeroed when the cursor is allocated. See
60791 ** sqlite3BtreeCursorZero(). Fields that follow must be manually
60792 ** initialized. */
60793 #define BTCURSOR_FIRST_UNINIT pBt /* Name of first uninitialized field */
60794 BtShared *pBt; /* The BtShared this cursor points to */
60795 BtCursor *pNext; /* Forms a linked list of all cursors */
60796 CellInfo info; /* A parse of the cell we are pointing at */
60797 i64 nKey; /* Size of pKey, or last integer key */
60798 Pgno pgnoRoot; /* The root page of this tree */
60799 i8 iPage; /* Index of current page in apPage */
60800 u8 curIntKey; /* Value of apPage[0]->intKey */
60801 u16 ix; /* Current index for apPage[iPage] */
60802 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
60803 struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */
@@ -59967,12 +60843,12 @@
60843 ** on a different connection that shares the BtShared cache with this
60844 ** cursor. The error has left the cache in an inconsistent state.
60845 ** Do nothing else with this cursor. Any attempt to use the cursor
60846 ** should return the error code stored in BtCursor.skipNext
60847 */
60848 #define CURSOR_VALID 0
60849 #define CURSOR_INVALID 1
60850 #define CURSOR_SKIPNEXT 2
60851 #define CURSOR_REQUIRESEEK 3
60852 #define CURSOR_FAULT 4
60853
60854 /*
@@ -64746,11 +65622,11 @@
65622 ** to zero. But it turns out that the apPage[] and aiIdx[] arrays
65623 ** do not need to be zeroed and they are large, so we can save a lot
65624 ** of run-time by skipping the initialization of those elements.
65625 */
65626 SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
65627 memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
65628 }
65629
65630 /*
65631 ** Close a cursor. The read lock on the database file is released
65632 ** when the last cursor is closed.
@@ -64789,15 +65665,23 @@
65665 **
65666 ** BtCursor.info is a cache of the information in the current cell.
65667 ** Using this cache reduces the number of calls to btreeParseCell().
65668 */
65669 #ifndef NDEBUG
65670 static int cellInfoEqual(CellInfo *a, CellInfo *b){
65671 if( a->nKey!=b->nKey ) return 0;
65672 if( a->pPayload!=b->pPayload ) return 0;
65673 if( a->nPayload!=b->nPayload ) return 0;
65674 if( a->nLocal!=b->nLocal ) return 0;
65675 if( a->nSize!=b->nSize ) return 0;
65676 return 1;
65677 }
65678 static void assertCellInfo(BtCursor *pCur){
65679 CellInfo info;
65680 memset(&info, 0, sizeof(info));
65681 btreeParseCell(pCur->pPage, pCur->ix, &info);
65682 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
65683 }
65684 #else
65685 #define assertCellInfo(x)
65686 #endif
65687 static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
@@ -65069,18 +65953,19 @@
65953 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
65954 ** means "not yet known" (the cache is lazily populated).
65955 */
65956 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
65957 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
65958 if( pCur->aOverflow==0
65959 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
65960 ){
65961 Pgno *aNew = (Pgno*)sqlite3Realloc(
65962 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
65963 );
65964 if( aNew==0 ){
65965 return SQLITE_NOMEM_BKPT;
65966 }else{
 
65967 pCur->aOverflow = aNew;
65968 }
65969 }
65970 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
65971 pCur->curFlags |= BTCF_ValidOvfl;
@@ -66590,13 +67475,12 @@
67475 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
67476 }
67477 }
67478
67479 /*
67480 ** Free any overflow pages associated with the given Cell. Store
67481 ** size information about the cell in pInfo.
 
67482 */
67483 static int clearCell(
67484 MemPage *pPage, /* The page that contains the Cell */
67485 unsigned char *pCell, /* First byte of the Cell */
67486 CellInfo *pInfo /* Size information about the cell */
@@ -67796,11 +68680,11 @@
68680 rc = SQLITE_CORRUPT_BKPT;
68681 goto balance_cleanup;
68682 }
68683
68684 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
68685 ** contains overflow cells, include them in the b.apCell[] array
68686 ** in the correct spot.
68687 **
68688 ** Note that when there are multiple overflow cells, it is always the
68689 ** case that they are sequential and adjacent. This invariant arises
68690 ** because multiple overflows can only occurs when inserting divider
@@ -71281,10 +72165,55 @@
72165 }
72166 return 1;
72167 }
72168 #endif
72169
72170 #ifdef SQLITE_DEBUG
72171 /*
72172 ** Check that string value of pMem agrees with its integer or real value.
72173 **
72174 ** A single int or real value always converts to the same strings. But
72175 ** many different strings can be converted into the same int or real.
72176 ** If a table contains a numeric value and an index is based on the
72177 ** corresponding string value, then it is important that the string be
72178 ** derived from the numeric value, not the other way around, to ensure
72179 ** that the index and table are consistent. See ticket
72180 ** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
72181 ** an example.
72182 **
72183 ** This routine looks at pMem to verify that if it has both a numeric
72184 ** representation and a string representation then the string rep has
72185 ** been derived from the numeric and not the other way around. It returns
72186 ** true if everything is ok and false if there is a problem.
72187 **
72188 ** This routine is for use inside of assert() statements only.
72189 */
72190 SQLITE_PRIVATE int sqlite3VdbeMemConsistentDualRep(Mem *p){
72191 char zBuf[100];
72192 char *z;
72193 int i, j, incr;
72194 if( (p->flags & MEM_Str)==0 ) return 1;
72195 if( (p->flags & (MEM_Int|MEM_Real))==0 ) return 1;
72196 if( p->flags & MEM_Int ){
72197 sqlite3_snprintf(sizeof(zBuf),zBuf,"%lld",p->u.i);
72198 }else{
72199 sqlite3_snprintf(sizeof(zBuf),zBuf,"%!.15g",p->u.r);
72200 }
72201 z = p->z;
72202 i = j = 0;
72203 incr = 1;
72204 if( p->enc!=SQLITE_UTF8 ){
72205 incr = 2;
72206 if( p->enc==SQLITE_UTF16BE ) z++;
72207 }
72208 while( zBuf[j] ){
72209 if( zBuf[j++]!=z[i] ) return 0;
72210 i += incr;
72211 }
72212 return 1;
72213 }
72214 #endif /* SQLITE_DEBUG */
72215
72216 /*
72217 ** If pMem is an object with a valid string representation, this routine
72218 ** ensures the internal encoding for the string representation is
72219 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
@@ -71714,10 +72643,20 @@
72643 }else{
72644 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
72645 return (double)0;
72646 }
72647 }
72648
72649 /*
72650 ** Return 1 if pMem represents true, and return 0 if pMem represents false.
72651 ** Return the value ifNull if pMem is NULL.
72652 */
72653 SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
72654 if( pMem->flags & MEM_Int ) return pMem->u.i!=0;
72655 if( pMem->flags & MEM_Null ) return ifNull;
72656 return sqlite3VdbeRealValue(pMem)!=0.0;
72657 }
72658
72659 /*
72660 ** The MEM structure is already a MEM_Real. Try to also make it a
72661 ** MEM_Int if we can.
72662 */
@@ -71769,10 +72708,22 @@
72708
72709 pMem->u.r = sqlite3VdbeRealValue(pMem);
72710 MemSetTypeFlag(pMem, MEM_Real);
72711 return SQLITE_OK;
72712 }
72713
72714 /* Compare a floating point value to an integer. Return true if the two
72715 ** values are the same within the precision of the floating point value.
72716 **
72717 ** For some versions of GCC on 32-bit machines, if you do the more obvious
72718 ** comparison of "r1==(double)i" you sometimes get an answer of false even
72719 ** though the r1 and (double)i values are bit-for-bit the same.
72720 */
72721 static int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
72722 double r2 = (double)i;
72723 return memcmp(&r1, &r2, sizeof(r1))==0;
72724 }
72725
72726 /*
72727 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
72728 ** Invalidate any prior representations.
72729 **
@@ -71789,11 +72740,11 @@
72740 if( rc==0 ){
72741 MemSetTypeFlag(pMem, MEM_Int);
72742 }else{
72743 i64 i = pMem->u.i;
72744 sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
72745 if( rc==1 && sqlite3RealSameAsInt(pMem->u.r, i) ){
72746 pMem->u.i = i;
72747 MemSetTypeFlag(pMem, MEM_Int);
72748 }else{
72749 MemSetTypeFlag(pMem, MEM_Real);
72750 }
@@ -72272,10 +73223,11 @@
73223 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
73224 }
73225 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
73226 || pVal->db->mallocFailed );
73227 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
73228 assert( sqlite3VdbeMemConsistentDualRep(pVal) );
73229 return pVal->z;
73230 }else{
73231 return 0;
73232 }
73233 }
@@ -72294,10 +73246,11 @@
73246 if( !pVal ) return 0;
73247 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
73248 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
73249 assert( (pVal->flags & MEM_RowSet)==0 );
73250 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
73251 assert( sqlite3VdbeMemConsistentDualRep(pVal) );
73252 return pVal->z;
73253 }
73254 if( pVal->flags&MEM_Null ){
73255 return 0;
73256 }
@@ -78075,18 +79028,16 @@
79028 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
79029 }
79030 SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
79031 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
79032 pCtx->isError = SQLITE_ERROR;
 
79033 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
79034 }
79035 #ifndef SQLITE_OMIT_UTF16
79036 SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
79037 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
79038 pCtx->isError = SQLITE_ERROR;
 
79039 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
79040 }
79041 #endif
79042 SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
79043 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
@@ -78188,12 +79139,11 @@
79139 }
79140 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
79141 return SQLITE_OK;
79142 }
79143 SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
79144 pCtx->isError = errCode ? errCode : -1;
 
79145 #ifdef SQLITE_DEBUG
79146 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
79147 #endif
79148 if( pCtx->pOut->flags & MEM_Null ){
79149 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
@@ -78203,21 +79153,19 @@
79153
79154 /* Force an SQLITE_TOOBIG error. */
79155 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
79156 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
79157 pCtx->isError = SQLITE_TOOBIG;
 
79158 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
79159 SQLITE_UTF8, SQLITE_STATIC);
79160 }
79161
79162 /* An SQLITE_NOMEM error. */
79163 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
79164 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
79165 sqlite3VdbeMemSetNull(pCtx->pOut);
79166 pCtx->isError = SQLITE_NOMEM_BKPT;
 
79167 sqlite3OomFault(pCtx->pOut->db);
79168 }
79169
79170 /*
79171 ** This function is called after a transaction has been committed. It
@@ -78620,14 +79568,11 @@
79568 if( !pAuxData ) goto failed;
79569 pAuxData->iAuxOp = pCtx->iOp;
79570 pAuxData->iAuxArg = iArg;
79571 pAuxData->pNextAux = pVdbe->pAuxData;
79572 pVdbe->pAuxData = pAuxData;
79573 if( pCtx->isError==0 ) pCtx->isError = -1;
 
 
 
79574 }else if( pAuxData->xDeleteAux ){
79575 pAuxData->xDeleteAux(pAuxData->pAux);
79576 }
79577
79578 pAuxData->pAux = pAux;
@@ -79379,11 +80324,13 @@
80324 */
80325 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
80326 Vdbe *pVdbe = (Vdbe*)pStmt;
80327 u32 v;
80328 #ifdef SQLITE_ENABLE_API_ARMOR
80329 if( !pStmt
80330 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
80331 ){
80332 (void)SQLITE_MISUSE_BKPT;
80333 return 0;
80334 }
80335 #endif
80336 if( op==SQLITE_STMTSTATUS_MEMUSED ){
@@ -80153,10 +81100,15 @@
81100 }else{
81101 pRec->u.r = rValue;
81102 pRec->flags |= MEM_Real;
81103 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
81104 }
81105 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
81106 ** string representation after computing a numeric equivalent, because the
81107 ** string representation might not be the canonical representation for the
81108 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
81109 pRec->flags &= ~MEM_Str;
81110 }
81111
81112 /*
81113 ** Processing is determine by the affinity parameter:
81114 **
@@ -80621,11 +81573,11 @@
81573 ** jumps to abort_due_to_error. */
81574 assert( rc==SQLITE_OK );
81575
81576 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
81577 #ifdef VDBE_PROFILE
81578 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
81579 #endif
81580 nVmStep++;
81581 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
81582 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
81583 #endif
@@ -82145,22 +83097,12 @@
83097 case OP_And: /* same as TK_AND, in1, in2, out3 */
83098 case OP_Or: { /* same as TK_OR, in1, in2, out3 */
83099 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
83100 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
83101
83102 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
83103 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
 
 
 
 
 
 
 
 
 
 
83104 if( pOp->opcode==OP_And ){
83105 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
83106 v1 = and_logic[v1*3+v2];
83107 }else{
83108 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
@@ -82173,10 +83115,39 @@
83115 pOut->u.i = v1;
83116 MemSetTypeFlag(pOut, MEM_Int);
83117 }
83118 break;
83119 }
83120
83121 /* Opcode: IsTrue P1 P2 P3 P4 *
83122 ** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
83123 **
83124 ** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
83125 ** IS NOT FALSE operators.
83126 **
83127 ** Interpret the value in register P1 as a boolean value. Store that
83128 ** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
83129 ** NULL, then the P3 is stored in register P2. Invert the answer if P4
83130 ** is 1.
83131 **
83132 ** The logic is summarized like this:
83133 **
83134 ** <ul>
83135 ** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
83136 ** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
83137 ** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
83138 ** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
83139 ** </ul>
83140 */
83141 case OP_IsTrue: { /* in1, out2 */
83142 assert( pOp->p4type==P4_INT32 );
83143 assert( pOp->p4.i==0 || pOp->p4.i==1 );
83144 assert( pOp->p3==0 || pOp->p3==1 );
83145 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
83146 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
83147 break;
83148 }
83149
83150 /* Opcode: Not P1 P2 * * *
83151 ** Synopsis: r[P2]= !r[P1]
83152 **
83153 ** Interpret the value in register P1 as a boolean value. Store the
@@ -82184,14 +83155,14 @@
83155 ** NULL, then a NULL is stored in P2.
83156 */
83157 case OP_Not: { /* same as TK_NOT, in1, out2 */
83158 pIn1 = &aMem[pOp->p1];
83159 pOut = &aMem[pOp->p2];
 
83160 if( (pIn1->flags & MEM_Null)==0 ){
83161 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
83162 }else{
83163 sqlite3VdbeMemSetNull(pOut);
83164 }
83165 break;
83166 }
83167
83168 /* Opcode: BitNot P1 P2 * * *
@@ -82254,34 +83225,29 @@
83225 **
83226 ** Jump to P2 if the value in register P1 is true. The value
83227 ** is considered true if it is numeric and non-zero. If the value
83228 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
83229 */
83230 case OP_If: { /* jump, in1 */
83231 int c;
83232 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
83233 VdbeBranchTaken(c!=0, 2);
83234 if( c ) goto jump_to_p2;
83235 break;
83236 }
83237
83238 /* Opcode: IfNot P1 P2 P3 * *
83239 **
83240 ** Jump to P2 if the value in register P1 is False. The value
83241 ** is considered false if it has a numeric value of zero. If the value
83242 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
83243 */
 
83244 case OP_IfNot: { /* jump, in1 */
83245 int c;
83246 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
 
 
 
 
 
 
 
 
 
 
83247 VdbeBranchTaken(c!=0, 2);
83248 if( c ) goto jump_to_p2;
 
 
83249 break;
83250 }
83251
83252 /* Opcode: IsNull P1 P2 * * *
83253 ** Synopsis: if r[P1]==NULL goto P2
@@ -82338,11 +83304,11 @@
83304 ** pointing.
83305 **
83306 ** P2 is the column number for the argument to the sqlite_offset() function.
83307 ** This opcode does not use P2 itself, but the P2 value is used by the
83308 ** code generator. The P1, P2, and P3 operands to this opcode are the
83309 ** same as for OP_Column.
83310 **
83311 ** This opcode is only available if SQLite is compiled with the
83312 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
83313 */
83314 case OP_Offset: { /* out3 */
@@ -84246,10 +85212,14 @@
85212 v = 0;
85213 res = 0;
85214 pOut = out2Prerelease(p, pOp);
85215 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
85216 pC = p->apCsr[pOp->p1];
85217 if( !pC->isTable ){
85218 rc = SQLITE_CORRUPT_BKPT;
85219 goto abort_due_to_error;
85220 }
85221 assert( pC!=0 );
85222 assert( pC->eCurType==CURTYPE_BTREE );
85223 assert( pC->uc.pCursor!=0 );
85224 {
85225 /* The next rowid or record number (different terms for the same
@@ -86182,16 +87152,21 @@
87152 assert( pOp->p4type==P4_FUNCDEF );
87153 n = pOp->p5;
87154 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
87155 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
87156 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
87157 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
87158 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
87159 if( pCtx==0 ) goto no_mem;
87160 pCtx->pMem = 0;
87161 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
87162 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
87163 pCtx->pFunc = pOp->p4.pFunc;
87164 pCtx->iOp = (int)(pOp - aOp);
87165 pCtx->pVdbe = p;
87166 pCtx->skipFlag = 0;
87167 pCtx->isError = 0;
87168 pCtx->argc = n;
87169 pOp->p4type = P4_FUNCCTX;
87170 pOp->p4.pCtx = pCtx;
87171 pOp->opcode = OP_AggStep;
87172 /* Fall through into OP_AggStep */
@@ -86198,11 +87173,10 @@
87173 }
87174 case OP_AggStep: {
87175 int i;
87176 sqlite3_context *pCtx;
87177 Mem *pMem;
 
87178
87179 assert( pOp->p4type==P4_FUNCCTX );
87180 pCtx = pOp->p4.pCtx;
87181 pMem = &aMem[pOp->p3];
87182
@@ -86221,30 +87195,32 @@
87195 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
87196 }
87197 #endif
87198
87199 pMem->n++;
87200 assert( pCtx->pOut->flags==MEM_Null );
87201 assert( pCtx->isError==0 );
87202 assert( pCtx->skipFlag==0 );
 
87203 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
87204 if( pCtx->isError ){
87205 if( pCtx->isError>0 ){
87206 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
87207 rc = pCtx->isError;
87208 }
87209 if( pCtx->skipFlag ){
87210 assert( pOp[-1].opcode==OP_CollSeq );
87211 i = pOp[-1].p1;
87212 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
87213 pCtx->skipFlag = 0;
87214 }
87215 sqlite3VdbeMemRelease(pCtx->pOut);
87216 pCtx->pOut->flags = MEM_Null;
87217 pCtx->isError = 0;
87218 if( rc ) goto abort_due_to_error;
87219 }
87220 assert( pCtx->pOut->flags==MEM_Null );
87221 assert( pCtx->skipFlag==0 );
 
 
 
 
 
87222 break;
87223 }
87224
87225 /* Opcode: AggFinal P1 P2 * P4 *
87226 ** Synopsis: accum=r[P1] N=P2
@@ -86727,11 +87703,12 @@
87703 }else{
87704 MemSetTypeFlag(pDest, MEM_Null);
87705 }
87706 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
87707 sqlite3VtabImportErrmsg(p, pVtab);
87708 if( sContext.isError>0 ){
87709 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
87710 rc = sContext.isError;
87711 }
87712 sqlite3VdbeChangeEncoding(pDest, encoding);
87713 REGISTER_TRACE(pOp->p3, pDest);
87714 UPDATE_MAX_BLOBSIZE(pDest);
@@ -86992,10 +87969,11 @@
87969 if( pCtx==0 ) goto no_mem;
87970 pCtx->pOut = 0;
87971 pCtx->pFunc = pOp->p4.pFunc;
87972 pCtx->iOp = (int)(pOp - aOp);
87973 pCtx->pVdbe = p;
87974 pCtx->isError = 0;
87975 pCtx->argc = n;
87976 pOp->p4type = P4_FUNCCTX;
87977 pOp->p4.pCtx = pCtx;
87978 assert( OP_PureFunc == OP_PureFunc0+2 );
87979 assert( OP_Function == OP_Function0+2 );
@@ -87026,20 +88004,21 @@
88004 assert( memIsValid(pCtx->argv[i]) );
88005 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
88006 }
88007 #endif
88008 MemSetTypeFlag(pOut, MEM_Null);
88009 assert( pCtx->isError==0 );
88010 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
88011
88012 /* If the function returned an error, throw an exception */
88013 if( pCtx->isError ){
88014 if( pCtx->isError>0 ){
88015 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
88016 rc = pCtx->isError;
88017 }
88018 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
88019 pCtx->isError = 0;
88020 if( rc ) goto abort_due_to_error;
88021 }
88022
88023 /* Copy the result of the function into register P3 */
88024 if( pOut->flags & (MEM_Str|MEM_Blob) ){
@@ -87077,12 +88056,14 @@
88056 ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
88057 ** error is encountered.
88058 */
88059 case OP_Trace:
88060 case OP_Init: { /* jump */
88061 int i;
88062 #ifndef SQLITE_OMIT_TRACE
88063 char *zTrace;
88064 #endif
88065
88066 /* If the P4 argument is not NULL, then it must be an SQL comment string.
88067 ** The "--" string is broken up to prevent false-positives with srcck1.c.
88068 **
88069 ** This assert() provides evidence for:
@@ -87195,11 +88176,11 @@
88176 *****************************************************************************/
88177 }
88178
88179 #ifdef VDBE_PROFILE
88180 {
88181 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
88182 if( endTime>start ) pOrigOp->cycles += endTime - start;
88183 pOrigOp->cnt++;
88184 }
88185 #endif
88186
@@ -91582,14 +92563,20 @@
92563 ** pExpr.
92564 **
92565 ** Because no reference was made to outer contexts, the pNC->nRef
92566 ** fields are not changed in any context.
92567 */
92568 if( cnt==0 && zTab==0 ){
92569 assert( pExpr->op==TK_ID );
92570 if( ExprHasProperty(pExpr,EP_DblQuoted) ){
92571 pExpr->op = TK_STRING;
92572 pExpr->pTab = 0;
92573 return WRC_Prune;
92574 }
92575 if( sqlite3ExprIdToTrueFalse(pExpr) ){
92576 return WRC_Prune;
92577 }
92578 }
92579
92580 /*
92581 ** cnt==0 means there was not match. cnt>1 means there were two or
92582 ** more matches. Either way, we have an error.
@@ -91934,19 +92921,34 @@
92921 }
92922 case TK_VARIABLE: {
92923 notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
92924 break;
92925 }
92926 case TK_IS:
92927 case TK_ISNOT: {
92928 Expr *pRight;
92929 assert( !ExprHasProperty(pExpr, EP_Reduced) );
92930 /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
92931 ** and "x IS NOT FALSE". */
92932 if( (pRight = pExpr->pRight)->op==TK_ID ){
92933 int rc = resolveExprStep(pWalker, pRight);
92934 if( rc==WRC_Abort ) return WRC_Abort;
92935 if( pRight->op==TK_TRUEFALSE ){
92936 pExpr->op2 = pExpr->op;
92937 pExpr->op = TK_TRUTH;
92938 return WRC_Continue;
92939 }
92940 }
92941 /* Fall thru */
92942 }
92943 case TK_BETWEEN:
92944 case TK_EQ:
92945 case TK_NE:
92946 case TK_LT:
92947 case TK_LE:
92948 case TK_GT:
92949 case TK_GE: {
 
 
92950 int nLeft, nRight;
92951 if( pParse->db->mallocFailed ) break;
92952 assert( pExpr->pLeft!=0 );
92953 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
92954 if( pExpr->op==TK_BETWEEN ){
@@ -94416,10 +95418,38 @@
95418 SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
95419 UNUSED_PARAMETER(NotUsed);
95420 pWalker->eCode = 0;
95421 return WRC_Abort;
95422 }
95423
95424 /*
95425 ** If the input expression is an ID with the name "true" or "false"
95426 ** then convert it into an TK_TRUEFALSE term. Return non-zero if
95427 ** the conversion happened, and zero if the expression is unaltered.
95428 */
95429 SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr *pExpr){
95430 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
95431 if( sqlite3StrICmp(pExpr->u.zToken, "true")==0
95432 || sqlite3StrICmp(pExpr->u.zToken, "false")==0
95433 ){
95434 pExpr->op = TK_TRUEFALSE;
95435 return 1;
95436 }
95437 return 0;
95438 }
95439
95440 /*
95441 ** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE
95442 ** and 0 if it is FALSE.
95443 */
95444 SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr *pExpr){
95445 assert( pExpr->op==TK_TRUEFALSE );
95446 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
95447 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
95448 return pExpr->u.zToken[4]==0;
95449 }
95450
95451
95452 /*
95453 ** These routines are Walker callbacks used to check expressions to
95454 ** see if they are "constant" for some definition of constant. The
95455 ** Walker.eCode value determines the type of "constant" we are looking
@@ -94464,10 +95494,16 @@
95494 }else{
95495 pWalker->eCode = 0;
95496 return WRC_Abort;
95497 }
95498 case TK_ID:
95499 /* Convert "true" or "false" in a DEFAULT clause into the
95500 ** appropriate TK_TRUEFALSE operator */
95501 if( sqlite3ExprIdToTrueFalse(pExpr) ){
95502 return WRC_Prune;
95503 }
95504 /* Fall thru */
95505 case TK_COLUMN:
95506 case TK_AGG_FUNCTION:
95507 case TK_AGG_COLUMN:
95508 testcase( pExpr->op==TK_ID );
95509 testcase( pExpr->op==TK_COLUMN );
@@ -96227,10 +97263,14 @@
97263 pExpr->op2);
97264 }
97265 case TK_INTEGER: {
97266 codeInteger(pParse, pExpr, 0, target);
97267 return target;
97268 }
97269 case TK_TRUEFALSE: {
97270 sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target);
97271 return target;
97272 }
97273 #ifndef SQLITE_OMIT_FLOATING_POINT
97274 case TK_FLOAT: {
97275 assert( !ExprHasProperty(pExpr, EP_IntValue) );
97276 codeReal(v, pExpr->u.zToken, 0, target);
@@ -96382,10 +97422,22 @@
97422 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT );
97423 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
97424 testcase( regFree1==0 );
97425 sqlite3VdbeAddOp2(v, op, r1, inReg);
97426 break;
97427 }
97428 case TK_TRUTH: {
97429 int isTrue; /* IS TRUE or IS NOT TRUE */
97430 int bNormal; /* IS TRUE or IS FALSE */
97431 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
97432 testcase( regFree1==0 );
97433 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
97434 bNormal = pExpr->op2==TK_IS;
97435 testcase( isTrue && bNormal);
97436 testcase( !isTrue && bNormal);
97437 sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal);
97438 break;
97439 }
97440 case TK_ISNULL:
97441 case TK_NOTNULL: {
97442 int addr;
97443 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
@@ -97157,10 +98209,27 @@
98209 }
98210 case TK_NOT: {
98211 testcase( jumpIfNull==0 );
98212 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
98213 break;
98214 }
98215 case TK_TRUTH: {
98216 int isNot; /* IS NOT TRUE or IS NOT FALSE */
98217 int isTrue; /* IS TRUE or IS NOT TRUE */
98218 testcase( jumpIfNull==0 );
98219 isNot = pExpr->op2==TK_ISNOT;
98220 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
98221 testcase( isTrue && isNot );
98222 testcase( !isTrue && isNot );
98223 if( isTrue ^ isNot ){
98224 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
98225 isNot ? SQLITE_JUMPIFNULL : 0);
98226 }else{
98227 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
98228 isNot ? SQLITE_JUMPIFNULL : 0);
98229 }
98230 break;
98231 }
98232 case TK_IS:
98233 case TK_ISNOT:
98234 testcase( op==TK_IS );
98235 testcase( op==TK_ISNOT );
@@ -97311,10 +98380,30 @@
98380 }
98381 case TK_NOT: {
98382 testcase( jumpIfNull==0 );
98383 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
98384 break;
98385 }
98386 case TK_TRUTH: {
98387 int isNot; /* IS NOT TRUE or IS NOT FALSE */
98388 int isTrue; /* IS TRUE or IS NOT TRUE */
98389 testcase( jumpIfNull==0 );
98390 isNot = pExpr->op2==TK_ISNOT;
98391 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
98392 testcase( isTrue && isNot );
98393 testcase( !isTrue && isNot );
98394 if( isTrue ^ isNot ){
98395 /* IS TRUE and IS NOT FALSE */
98396 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
98397 isNot ? 0 : SQLITE_JUMPIFNULL);
98398
98399 }else{
98400 /* IS FALSE and IS NOT TRUE */
98401 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
98402 isNot ? 0 : SQLITE_JUMPIFNULL);
98403 }
98404 break;
98405 }
98406 case TK_IS:
98407 case TK_ISNOT:
98408 testcase( pExpr->op==TK_IS );
98409 testcase( pExpr->op==TK_ISNOT );
@@ -100822,10 +101911,14 @@
101911 **
101912 ** SELECT sqlite_attach(x, y, z)
101913 **
101914 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
101915 ** third argument.
101916 **
101917 ** If the db->init.reopenMemdb flags is set, then instead of attaching a
101918 ** new database, close the database on db->init.iDb and reopen it as an
101919 ** empty MemDB.
101920 */
101921 static void attachFunc(
101922 sqlite3_context *context,
101923 int NotUsed,
101924 sqlite3_value **argv
@@ -100842,69 +101935,89 @@
101935 Db *pNew; /* Db object for the newly attached database */
101936 char *zErrDyn = 0;
101937 sqlite3_vfs *pVfs;
101938
101939 UNUSED_PARAMETER(NotUsed);
 
101940 zFile = (const char *)sqlite3_value_text(argv[0]);
101941 zName = (const char *)sqlite3_value_text(argv[1]);
101942 if( zFile==0 ) zFile = "";
101943 if( zName==0 ) zName = "";
101944
101945 #ifdef SQLITE_ENABLE_DESERIALIZE
101946 # define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
101947 #else
101948 # define REOPEN_AS_MEMDB(db) (0)
101949 #endif
101950
101951 if( REOPEN_AS_MEMDB(db) ){
101952 /* This is not a real ATTACH. Instead, this routine is being called
101953 ** from sqlite3_deserialize() to close database db->init.iDb and
101954 ** reopen it as a MemDB */
101955 pVfs = sqlite3_vfs_find("memdb");
101956 if( pVfs==0 ) return;
101957 pNew = &db->aDb[db->init.iDb];
101958 if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
101959 pNew->pBt = 0;
101960 pNew->pSchema = 0;
101961 rc = sqlite3BtreeOpen(pVfs, "x", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
101962 }else{
101963 /* This is a real ATTACH
101964 **
101965 ** Check for the following errors:
101966 **
101967 ** * Too many attached databases,
101968 ** * Transaction currently open
101969 ** * Specified database name already being used.
101970 */
101971 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
101972 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
101973 db->aLimit[SQLITE_LIMIT_ATTACHED]
101974 );
101975 goto attach_error;
101976 }
101977 for(i=0; i<db->nDb; i++){
101978 char *z = db->aDb[i].zDbSName;
101979 assert( z && zName );
101980 if( sqlite3StrICmp(z, zName)==0 ){
101981 zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
101982 goto attach_error;
101983 }
101984 }
101985
101986 /* Allocate the new entry in the db->aDb[] array and initialize the schema
101987 ** hash tables.
101988 */
101989 if( db->aDb==db->aDbStatic ){
101990 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
101991 if( aNew==0 ) return;
101992 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
101993 }else{
101994 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
101995 if( aNew==0 ) return;
101996 }
101997 db->aDb = aNew;
101998 pNew = &db->aDb[db->nDb];
101999 memset(pNew, 0, sizeof(*pNew));
102000
102001 /* Open the database file. If the btree is successfully opened, use
102002 ** it to obtain the database schema. At this point the schema may
102003 ** or may not be initialized.
102004 */
102005 flags = db->openFlags;
102006 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
102007 if( rc!=SQLITE_OK ){
102008 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
102009 sqlite3_result_error(context, zErr, -1);
102010 sqlite3_free(zErr);
102011 return;
102012 }
102013 assert( pVfs );
102014 flags |= SQLITE_OPEN_MAIN_DB;
102015 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
102016 sqlite3_free( zPath );
102017 db->nDb++;
102018 }
102019 db->skipBtreeMutex = 0;
102020 if( rc==SQLITE_CONSTRAINT ){
102021 rc = SQLITE_ERROR;
102022 zErrDyn = sqlite3MPrintf(db, "database is already attached");
102023 }else if( rc==SQLITE_OK ){
@@ -100927,11 +102040,11 @@
102040 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
102041 #endif
102042 sqlite3BtreeLeave(pNew->pBt);
102043 }
102044 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
102045 if( !REOPEN_AS_MEMDB(db) ) pNew->zDbSName = sqlite3DbStrDup(db, zName);
102046 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
102047 rc = SQLITE_NOMEM_BKPT;
102048 }
102049
102050
@@ -100967,17 +102080,19 @@
102080 }
102081 #endif
102082
102083 /* If the file was opened successfully, read the schema for the new database.
102084 ** If this fails, or if opening the file failed, then close the file and
102085 ** remove the entry from the db->aDb[] array. i.e. put everything back the
102086 ** way we found it.
102087 */
102088 if( rc==SQLITE_OK ){
102089 sqlite3BtreeEnterAll(db);
102090 db->init.iDb = 0;
102091 rc = sqlite3Init(db, &zErrDyn);
102092 sqlite3BtreeLeaveAll(db);
102093 assert( zErrDyn==0 || rc!=SQLITE_OK );
102094 }
102095 #ifdef SQLITE_USER_AUTHENTICATION
102096 if( rc==SQLITE_OK ){
102097 u8 newAuth = 0;
102098 rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
@@ -100985,25 +102100,27 @@
102100 rc = SQLITE_AUTH_USER;
102101 }
102102 }
102103 #endif
102104 if( rc ){
102105 if( !REOPEN_AS_MEMDB(db) ){
102106 int iDb = db->nDb - 1;
102107 assert( iDb>=2 );
102108 if( db->aDb[iDb].pBt ){
102109 sqlite3BtreeClose(db->aDb[iDb].pBt);
102110 db->aDb[iDb].pBt = 0;
102111 db->aDb[iDb].pSchema = 0;
102112 }
102113 sqlite3ResetAllSchemasOfConnection(db);
102114 db->nDb = iDb;
102115 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
102116 sqlite3OomFault(db);
102117 sqlite3DbFree(db, zErrDyn);
102118 zErrDyn = sqlite3MPrintf(db, "out of memory");
102119 }else if( zErrDyn==0 ){
102120 zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
102121 }
102122 }
102123 goto attach_error;
102124 }
102125
102126 return;
@@ -101270,10 +102387,18 @@
102387 if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
102388 return 1;
102389 }
102390 if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
102391 return 1;
102392 }
102393 if( pSelect->pWith ){
102394 int i;
102395 for(i=0; i<pSelect->pWith->nCte; i++){
102396 if( sqlite3FixSelect(pFix, pSelect->pWith->a[i].pSelect) ){
102397 return 1;
102398 }
102399 }
102400 }
102401 pSelect = pSelect->pPrior;
102402 }
102403 return 0;
102404 }
@@ -102734,14 +103859,28 @@
103859 ** been seen on a column. This routine sets the notNull flag on
103860 ** the column currently under construction.
103861 */
103862 SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
103863 Table *p;
103864 Column *pCol;
103865 p = pParse->pNewTable;
103866 if( p==0 || NEVER(p->nCol<1) ) return;
103867 pCol = &p->aCol[p->nCol-1];
103868 pCol->notNull = (u8)onError;
103869 p->tabFlags |= TF_HasNotNull;
103870
103871 /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
103872 ** on this column. */
103873 if( pCol->colFlags & COLFLAG_UNIQUE ){
103874 Index *pIdx;
103875 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
103876 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
103877 if( pIdx->aiColumn[0]==p->nCol-1 ){
103878 pIdx->uniqNotNull = 1;
103879 }
103880 }
103881 }
103882 }
103883
103884 /*
103885 ** Scan the column type name zType (length nType) and return the
103886 ** associated affinity type.
@@ -103472,12 +104611,10 @@
104611 }
104612 assert( !db->mallocFailed );
104613 p = pParse->pNewTable;
104614 if( p==0 ) return;
104615
 
 
104616 /* If the db->init.busy is 1 it means we are reading the SQL off the
104617 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
104618 ** So do not write to the disk again. Extract the root page number
104619 ** for the table from the db->init.newTnum field. (The page number
104620 ** should have been put there by the sqliteOpenCb routine.)
@@ -103484,10 +104621,14 @@
104621 **
104622 ** If the root page number is 1, that means this is the sqlite_master
104623 ** table itself. So mark it read-only.
104624 */
104625 if( db->init.busy ){
104626 if( pSelect ){
104627 sqlite3ErrorMsg(pParse, "");
104628 return;
104629 }
104630 p->tnum = db->init.newTnum;
104631 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
104632 }
104633
104634 /* Special processing for WITHOUT ROWID Tables */
@@ -104701,11 +105842,13 @@
105842 ** key out of the last column added to the table under construction.
105843 ** So create a fake list to simulate this.
105844 */
105845 if( pList==0 ){
105846 Token prevCol;
105847 Column *pCol = &pTab->aCol[pTab->nCol-1];
105848 pCol->colFlags |= COLFLAG_UNIQUE;
105849 sqlite3TokenInit(&prevCol, pCol->zName);
105850 pList = sqlite3ExprListAppend(pParse, 0,
105851 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
105852 if( pList==0 ) goto exit_create_index;
105853 assert( pList->nExpr==1 );
105854 sqlite3ExprListSetSortOrder(pList, sortOrder);
@@ -107546,10 +108689,12 @@
108689 /*
108690 ** Indicate that the accumulator load should be skipped on this
108691 ** iteration of the aggregate loop.
108692 */
108693 static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
108694 assert( context->isError<=0 );
108695 context->isError = -1;
108696 context->skipFlag = 1;
108697 }
108698
108699 /*
108700 ** Implementation of the non-aggregate min() and max() functions
@@ -107612,12 +108757,10 @@
108757 static void lengthFunc(
108758 sqlite3_context *context,
108759 int argc,
108760 sqlite3_value **argv
108761 ){
 
 
108762 assert( argc==1 );
108763 UNUSED_PARAMETER(argc);
108764 switch( sqlite3_value_type(argv[0]) ){
108765 case SQLITE_BLOB:
108766 case SQLITE_INTEGER:
@@ -107625,17 +108768,21 @@
108768 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
108769 break;
108770 }
108771 case SQLITE_TEXT: {
108772 const unsigned char *z = sqlite3_value_text(argv[0]);
108773 const unsigned char *z0;
108774 unsigned char c;
108775 if( z==0 ) return;
108776 z0 = z;
108777 while( (c = *z)!=0 ){
108778 z++;
108779 if( c>=0xc0 ){
108780 while( (*z & 0xc0)==0x80 ){ z++; z0++; }
108781 }
108782 }
108783 sqlite3_result_int(context, (int)(z-z0));
108784 break;
108785 }
108786 default: {
108787 sqlite3_result_null(context);
108788 break;
@@ -108706,10 +109853,12 @@
109853 int nPattern; /* Size of zPattern */
109854 int nRep; /* Size of zRep */
109855 i64 nOut; /* Maximum size of zOut */
109856 int loopLimit; /* Last zStr[] that might match zPattern[] */
109857 int i, j; /* Loop counters */
109858 unsigned cntExpand; /* Number zOut expansions */
109859 sqlite3 *db = sqlite3_context_db_handle(context);
109860
109861 assert( argc==3 );
109862 UNUSED_PARAMETER(argc);
109863 zStr = sqlite3_value_text(argv[0]);
109864 if( zStr==0 ) return;
@@ -108737,37 +109886,44 @@
109886 zOut = contextMalloc(context, (i64)nOut);
109887 if( zOut==0 ){
109888 return;
109889 }
109890 loopLimit = nStr - nPattern;
109891 cntExpand = 0;
109892 for(i=j=0; i<=loopLimit; i++){
109893 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
109894 zOut[j++] = zStr[i];
109895 }else{
109896 if( nRep>nPattern ){
109897 nOut += nRep - nPattern;
109898 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
109899 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
109900 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
109901 sqlite3_result_error_toobig(context);
109902 sqlite3_free(zOut);
109903 return;
109904 }
109905 cntExpand++;
109906 if( (cntExpand&(cntExpand-1))==0 ){
109907 /* Grow the size of the output buffer only on substitutions
109908 ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
109909 u8 *zOld;
109910 zOld = zOut;
109911 zOut = sqlite3_realloc64(zOut, (int)nOut + (nOut - nStr - 1));
109912 if( zOut==0 ){
109913 sqlite3_result_error_nomem(context);
109914 sqlite3_free(zOld);
109915 return;
109916 }
109917 }
109918 }
109919 memcpy(&zOut[j], zRep, nRep);
109920 j += nRep;
109921 i += nPattern-1;
109922 }
109923 }
109924 assert( j+nStr-i+1<=nOut );
109925 memcpy(&zOut[j], &zStr[i], nStr-i);
109926 j += nStr - i;
109927 assert( j<=nOut );
109928 zOut[j] = 0;
109929 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
@@ -113826,12 +114982,12 @@
114982 #define sqlite3_bind_pointer sqlite3_api->bind_pointer
114983 #define sqlite3_result_pointer sqlite3_api->result_pointer
114984 #define sqlite3_value_pointer sqlite3_api->value_pointer
114985 /* Version 3.22.0 and later */
114986 #define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
114987 #define sqlite3_value_nochange sqlite3_api->value_nochange
114988 #define sqlite3_vtab_collation sqlite3_api->vtab_collation
114989 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
114990
114991 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
114992 /* This case when the file really is being compiled as a loadable
114993 ** extension */
@@ -117811,11 +118967,11 @@
118967 sqlite3 *db = pData->db;
118968 if( !db->mallocFailed && (db->flags & SQLITE_WriteSchema)==0 ){
118969 char *z;
118970 if( zObj==0 ) zObj = "?";
118971 z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
118972 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
118973 sqlite3DbFree(db, *pData->pzErrMsg);
118974 *pData->pzErrMsg = z;
118975 }
118976 pData->rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_CORRUPT_BKPT;
118977 }
@@ -119950,16 +121106,19 @@
121106 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
121107 codeOffset(v, p->iOffset, addrContinue);
121108 iSortTab = iTab;
121109 bSeq = 1;
121110 }
121111 for(i=0, iCol=nKey+bSeq-1; i<nSortData; i++){
121112 if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
121113 }
121114 for(i=nSortData-1; i>=0; i--){
121115 int iRead;
121116 if( aOutEx[i].u.x.iOrderByCol ){
121117 iRead = aOutEx[i].u.x.iOrderByCol-1;
121118 }else{
121119 iRead = iCol--;
121120 }
121121 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
121122 VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
121123 }
121124 switch( eDest ){
@@ -127022,12 +128181,12 @@
128181 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
128182 if( rc!=SQLITE_OK ) return rc;
128183 while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
128184 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
128185 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
128186 assert( sqlite3_strnicmp(zSubSql,"SELECT",6)!=0 || CORRUPT_DB );
128187 if( zSubSql && zSubSql[0]!='S' ){
128188 rc = execSql(db, pzErrMsg, zSubSql);
128189 if( rc!=SQLITE_OK ) break;
128190 }
128191 }
128192 assert( rc!=SQLITE_ROW );
@@ -130561,11 +131720,19 @@
131720 assert( pX!=0 );
131721 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
131722 if( sqlite3ExprIsVector(pX->pRight) ){
131723 r1 = rTemp = sqlite3GetTempReg(pParse);
131724 codeExprOrVector(pParse, pX->pRight, r1, 1);
131725 testcase( pX->op==TK_GT );
131726 testcase( pX->op==TK_GE );
131727 testcase( pX->op==TK_LT );
131728 testcase( pX->op==TK_LE );
131729 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
131730 assert( pX->op!=TK_GT || op==OP_SeekGE );
131731 assert( pX->op!=TK_GE || op==OP_SeekGE );
131732 assert( pX->op!=TK_LT || op==OP_SeekLE );
131733 assert( pX->op!=TK_LE || op==OP_SeekLE );
131734 }else{
131735 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
131736 disableTerm(pLevel, pStart);
131737 op = aMoveOp[(pX->op - TK_GT)];
131738 }
@@ -131336,10 +132503,16 @@
132503 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
132504 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
132505 WO_EQ|WO_IN|WO_IS, 0);
132506 if( pAlt==0 ) continue;
132507 if( pAlt->wtFlags & (TERM_CODED) ) continue;
132508 if( (pAlt->eOperator & WO_IN)
132509 && (pAlt->pExpr->flags & EP_xIsSelect)
132510 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
132511 ){
132512 continue;
132513 }
132514 testcase( pAlt->eOperator & WO_EQ );
132515 testcase( pAlt->eOperator & WO_IS );
132516 testcase( pAlt->eOperator & WO_IN );
132517 VdbeModuleComment((v, "begin transitive constraint"));
132518 sEAlt = *pAlt->pExpr;
@@ -132250,10 +133423,13 @@
133423 if( ALWAYS(pSrc!=0) ){
133424 int i;
133425 for(i=0; i<pSrc->nSrc; i++){
133426 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
133427 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
133428 if( pSrc->a[i].fg.isTabFunc ){
133429 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
133430 }
133431 }
133432 }
133433 pS = pS->pPrior;
133434 }
133435 return mask;
@@ -132662,11 +133838,11 @@
133838 transferJoinMarkings(pNew, pExpr);
133839 idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
133840 exprAnalyze(pSrc, pWC, idxNew);
133841 }
133842 pTerm = &pWC->a[idxTerm];
133843 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
133844 pTerm->eOperator = 0;
133845 }
133846
133847 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
133848 ** a virtual term for each vector component. The expression object
@@ -135387,14 +136563,16 @@
136563 pNew->wsFlags |= WHERE_COLUMN_EQ;
136564 assert( saved_nEq==pNew->u.btree.nEq );
136565 if( iCol==XN_ROWID
136566 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
136567 ){
136568 if( iCol==XN_ROWID || pProbe->uniqNotNull
136569 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
136570 ){
136571 pNew->wsFlags |= WHERE_ONEROW;
136572 }else{
136573 pNew->wsFlags |= WHERE_UNQ_WANTED;
136574 }
136575 }
136576 }else if( eOp & WO_ISNULL ){
136577 pNew->wsFlags |= WHERE_COLUMN_NULL;
136578 }else if( eOp & (WO_GT|WO_GE) ){
@@ -137537,10 +138715,11 @@
138715 ** FROM ... WHERE random()>0; -- eval random() once per row
138716 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
138717 */
138718 for(ii=0; ii<sWLB.pWC->nTerm; ii++){
138719 WhereTerm *pT = &sWLB.pWC->a[ii];
138720 if( pT->wtFlags & TERM_VIRTUAL ) continue;
138721 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
138722 sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
138723 pT->wtFlags |= TERM_CODED;
138724 }
138725 }
@@ -139995,11 +141174,12 @@
141174 #if defined(YYCOVERAGE)
141175 yycoverage[stateno][iLookAhead] = 1;
141176 #endif
141177 do{
141178 i = yy_shift_ofst[stateno];
141179 assert( i>=0 );
141180 assert( i+YYNTOKEN<=(int)sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) );
141181 assert( iLookAhead!=YYNOCODE );
141182 assert( iLookAhead < YYNTOKEN );
141183 i += iLookAhead;
141184 if( yy_lookahead[i]!=iLookAhead ){
141185 #ifdef YYFALLBACK
@@ -140709,10 +141889,14 @@
141889 }
141890 break;
141891 case 34: /* ccons ::= DEFAULT scanpt ID|INDEXED */
141892 {
141893 Expr *p = tokenExpr(pParse, TK_STRING, yymsp[0].minor.yy0);
141894 if( p ){
141895 sqlite3ExprIdToTrueFalse(p);
141896 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
141897 }
141898 sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
141899 }
141900 break;
141901 case 35: /* ccons ::= NOT NULL onconf */
141902 {sqlite3AddNotNull(pParse, yymsp[0].minor.yy4);}
@@ -143602,10 +144786,15 @@
144786 }
144787 if( rc==SQLITE_OK ){
144788 sqlite3GlobalConfig.isPCacheInit = 1;
144789 rc = sqlite3OsInit();
144790 }
144791 #ifdef SQLITE_ENABLE_DESERIALIZE
144792 if( rc==SQLITE_OK ){
144793 rc = sqlite3MemdbInit();
144794 }
144795 #endif
144796 if( rc==SQLITE_OK ){
144797 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
144798 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
144799 sqlite3GlobalConfig.isInit = 1;
144800 #ifdef SQLITE_EXTRA_INIT
@@ -143634,11 +144823,11 @@
144823 ** reason. So we run it once during initialization.
144824 */
144825 #ifndef NDEBUG
144826 #ifndef SQLITE_OMIT_FLOATING_POINT
144827 /* This section of code's only "output" is via assert() statements. */
144828 if( rc==SQLITE_OK ){
144829 u64 x = (((u64)1)<<63)-1;
144830 double y;
144831 assert(sizeof(x)==8);
144832 assert(sizeof(x)==sizeof(y));
144833 memcpy(&y, &x, 8);
@@ -144801,16 +145990,26 @@
145990 #endif
145991 /* SQLITE_AUTH */ "authorization denied",
145992 /* SQLITE_FORMAT */ 0,
145993 /* SQLITE_RANGE */ "column index out of range",
145994 /* SQLITE_NOTADB */ "file is not a database",
145995 /* SQLITE_NOTICE */ "notification message",
145996 /* SQLITE_WARNING */ "warning message",
145997 };
145998 const char *zErr = "unknown error";
145999 switch( rc ){
146000 case SQLITE_ABORT_ROLLBACK: {
146001 zErr = "abort due to ROLLBACK";
146002 break;
146003 }
146004 case SQLITE_ROW: {
146005 zErr = "another row available";
146006 break;
146007 }
146008 case SQLITE_DONE: {
146009 zErr = "no more rows available";
146010 break;
146011 }
146012 default: {
146013 rc &= 0xff;
146014 if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
146015 zErr = aMsg[rc];
@@ -160794,10 +161993,11 @@
161993 if( rc==SQLITE_OK ){
161994 sqlite3_bind_int64(pStmt, 1, iBlock);
161995 sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
161996 sqlite3_step(pStmt);
161997 rc = sqlite3_reset(pStmt);
161998 sqlite3_bind_null(pStmt, 2);
161999 }
162000 return rc;
162001 }
162002
162003 /*
@@ -160850,10 +162050,11 @@
162050 sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
162051 }
162052 sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
162053 sqlite3_step(pStmt);
162054 rc = sqlite3_reset(pStmt);
162055 sqlite3_bind_null(pStmt, 6);
162056 }
162057 return rc;
162058 }
162059
162060 /*
@@ -162329,10 +163530,11 @@
163530 }
163531 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
163532 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
163533 sqlite3_step(pStmt);
163534 *pRC = sqlite3_reset(pStmt);
163535 sqlite3_bind_null(pStmt, 2);
163536 sqlite3_free(a);
163537 }
163538
163539 /*
163540 ** Merge the entire database so that there is one segment for each
@@ -163517,10 +164719,11 @@
164719 sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
164720 sqlite3_bind_int64(pChomp, 3, iAbsLevel);
164721 sqlite3_bind_int(pChomp, 4, iIdx);
164722 sqlite3_step(pChomp);
164723 rc = sqlite3_reset(pChomp);
164724 sqlite3_bind_null(pChomp, 2);
164725 }
164726 }
164727
164728 sqlite3_free(root.a);
164729 sqlite3_free(block.a);
@@ -163596,10 +164799,11 @@
164799 if( rc==SQLITE_OK ){
164800 sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
164801 sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
164802 sqlite3_step(pReplace);
164803 rc = sqlite3_reset(pReplace);
164804 sqlite3_bind_null(pReplace, 2);
164805 }
164806
164807 return rc;
164808 }
164809
@@ -164410,11 +165614,10 @@
165614 sqlite3_value **apVal, /* Array of arguments */
165615 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
165616 ){
165617 Fts3Table *p = (Fts3Table *)pVtab;
165618 int rc = SQLITE_OK; /* Return Code */
 
165619 u32 *aSzIns = 0; /* Sizes of inserted documents */
165620 u32 *aSzDel = 0; /* Sizes of deleted documents */
165621 int nChng = 0; /* Net change in number of documents */
165622 int bInsertDone = 0;
165623
@@ -164508,11 +165711,10 @@
165711
165712 /* If this is a DELETE or UPDATE operation, remove the old record. */
165713 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
165714 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
165715 rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
 
165716 }
165717
165718 /* If this is an INSERT or UPDATE operation, insert the new record. */
165719 if( nArg>1 && rc==SQLITE_OK ){
165720 int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
@@ -164520,11 +165722,11 @@
165722 rc = fts3InsertData(p, apVal, pRowid);
165723 if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
165724 rc = FTS_CORRUPT_VTAB;
165725 }
165726 }
165727 if( rc==SQLITE_OK ){
165728 rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
165729 }
165730 if( rc==SQLITE_OK ){
165731 assert( p->iPrevDocid==*pRowid );
165732 rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
@@ -167828,10 +169030,11 @@
169030 }
169031 sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
169032 sqlite3_step(p);
169033 pNode->isDirty = 0;
169034 rc = sqlite3_reset(p);
169035 sqlite3_bind_null(p, 2);
169036 if( pNode->iNode==0 && rc==SQLITE_OK ){
169037 pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
169038 nodeHashInsert(pRtree, pNode);
169039 }
169040 }
@@ -179823,11 +181026,11 @@
181026 SessionTable *pTab /* Table that change applies to */
181027 ){
181028 int iHash;
181029 int bNull = 0;
181030 int rc = SQLITE_OK;
181031 SessionStat1Ctx stat1 = {0};
181032
181033 if( pSession->rc ) return;
181034
181035 /* Load table details if required */
181036 if( sessionInitTable(pSession, pTab) ) return;
@@ -181428,30 +182631,37 @@
182631 for(i=0; i<nCol && rc==SQLITE_OK; i++){
182632 int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
182633 if( abPK && abPK[i]==0 ) continue;
182634 rc = sessionInputBuffer(pIn, 9);
182635 if( rc==SQLITE_OK ){
182636 if( pIn->iNext>=pIn->nData ){
182637 rc = SQLITE_CORRUPT_BKPT;
182638 }else{
182639 eType = pIn->aData[pIn->iNext++];
182640 assert( apOut[i]==0 );
182641 if( eType ){
182642 apOut[i] = sqlite3ValueNew(0);
182643 if( !apOut[i] ) rc = SQLITE_NOMEM;
182644 }
182645 }
182646 }
182647
182648 if( rc==SQLITE_OK ){
182649 u8 *aVal = &pIn->aData[pIn->iNext];
182650 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
182651 int nByte;
182652 pIn->iNext += sessionVarintGet(aVal, &nByte);
182653 rc = sessionInputBuffer(pIn, nByte);
182654 if( rc==SQLITE_OK ){
182655 if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
182656 rc = SQLITE_CORRUPT_BKPT;
182657 }else{
182658 u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
182659 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
182660 pIn->iNext += nByte;
182661 }
182662 }
 
182663 }
182664 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
182665 sqlite3_int64 v = sessionGetI64(aVal);
182666 if( eType==SQLITE_INTEGER ){
182667 sqlite3VdbeMemSetInt64(apOut[i], v);
@@ -181487,12 +182697,23 @@
182697 int nRead = 0;
182698
182699 rc = sessionInputBuffer(pIn, 9);
182700 if( rc==SQLITE_OK ){
182701 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
182702 /* The hard upper limit for the number of columns in an SQLite
182703 ** database table is, according to sqliteLimit.h, 32676. So
182704 ** consider any table-header that purports to have more than 65536
182705 ** columns to be corrupt. This is convenient because otherwise,
182706 ** if the (nCol>65536) condition below were omitted, a sufficiently
182707 ** large value for nCol may cause nRead to wrap around and become
182708 ** negative. Leading to a crash. */
182709 if( nCol<0 || nCol>65536 ){
182710 rc = SQLITE_CORRUPT_BKPT;
182711 }else{
182712 rc = sessionInputBuffer(pIn, nRead+nCol+100);
182713 nRead += nCol;
182714 }
182715 }
182716
182717 while( rc==SQLITE_OK ){
182718 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
182719 nRead++;
@@ -181565,15 +182786,19 @@
182786 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
182787 if( rc==SQLITE_OK ){
182788 int nByte;
182789 int nVarint;
182790 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
182791 if( p->nCol>0 ){
182792 nCopy -= nVarint;
182793 p->in.iNext += nVarint;
182794 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
182795 p->tblhdr.nBuf = 0;
182796 sessionBufferGrow(&p->tblhdr, nByte, &rc);
182797 }else{
182798 rc = SQLITE_CORRUPT_BKPT;
182799 }
182800 }
182801
182802 if( rc==SQLITE_OK ){
182803 int iPK = sizeof(sqlite3_value*)*p->nCol*2;
182804 memset(p->tblhdr.aBuf, 0, iPK);
@@ -181646,10 +182871,17 @@
182871 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
182872 p->in.iCurrent = p->in.iNext;
182873 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
182874 op = p->in.aData[p->in.iNext++];
182875 }
182876
182877 if( p->zTab==0 ){
182878 /* The first record in the changeset is not a table header. Must be a
182879 ** corrupt changeset. */
182880 assert( p->in.iNext==1 );
182881 return (p->rc = SQLITE_CORRUPT_BKPT);
182882 }
182883
182884 p->op = op;
182885 p->bIndirect = p->in.aData[p->in.iNext++];
182886 if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
182887 return (p->rc = SQLITE_CORRUPT_BKPT);
@@ -181689,13 +182921,13 @@
182921 ** modified fields are present in the new.* record. The old.* record
182922 ** is currently completely empty. This block shifts the PK fields from
182923 ** new.* to old.*, to accommodate the code that reads these arrays. */
182924 for(i=0; i<p->nCol; i++){
182925 assert( p->apValue[i]==0 );
 
182926 if( p->abPK[i] ){
182927 p->apValue[i] = p->apValue[i+p->nCol];
182928 if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
182929 p->apValue[i+p->nCol] = 0;
182930 }
182931 }
182932 }
182933 }
@@ -182416,11 +183648,17 @@
183648
183649 for(i=0; rc==SQLITE_OK && i<nCol; i++){
183650 if( !abPK || abPK[i] ){
183651 sqlite3_value *pVal;
183652 (void)xValue(pIter, i, &pVal);
183653 if( pVal==0 ){
183654 /* The value in the changeset was "undefined". This indicates a
183655 ** corrupt changeset blob. */
183656 rc = SQLITE_CORRUPT_BKPT;
183657 }else{
183658 rc = sessionBindValue(pStmt, i+1, pVal);
183659 }
183660 }
183661 }
183662 return rc;
183663 }
183664
@@ -188053,11 +189291,12 @@
189291 #if defined(fts5YYCOVERAGE)
189292 fts5yycoverage[stateno][iLookAhead] = 1;
189293 #endif
189294 do{
189295 i = fts5yy_shift_ofst[stateno];
189296 assert( i>=0 );
189297 assert( i+fts5YYNFTS5TOKEN<=(int)sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0]) );
189298 assert( iLookAhead!=fts5YYNOCODE );
189299 assert( iLookAhead < fts5YYNFTS5TOKEN );
189300 i += iLookAhead;
189301 if( fts5yy_lookahead[i]!=iLookAhead ){
189302 #ifdef fts5YYFALLBACK
@@ -192495,11 +193734,11 @@
193734 if( sCtx.pPhrase==0 ){
193735 /* This happens when parsing a token or quoted phrase that contains
193736 ** no token characters at all. (e.g ... MATCH '""'). */
193737 sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
193738 }else if( sCtx.pPhrase->nTerm ){
193739 sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
193740 }
193741 pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
193742 }
193743
193744 return sCtx.pPhrase;
@@ -194958,10 +196197,11 @@
196197
196198 sqlite3_bind_int64(p->pWriter, 1, iRowid);
196199 sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC);
196200 sqlite3_step(p->pWriter);
196201 p->rc = sqlite3_reset(p->pWriter);
196202 sqlite3_bind_null(p->pWriter, 2);
196203 }
196204
196205 /*
196206 ** Execute the following SQL:
196207 **
@@ -196586,10 +197826,11 @@
197826 i64 val = sqlite3_column_int(pIdxSelect, 0);
197827 iPg = (int)(val>>1);
197828 bDlidx = (val & 0x0001);
197829 }
197830 p->rc = sqlite3_reset(pIdxSelect);
197831 sqlite3_bind_null(pIdxSelect, 2);
197832
197833 if( iPg<pSeg->pgnoFirst ){
197834 iPg = pSeg->pgnoFirst;
197835 bDlidx = 0;
197836 }
@@ -197798,10 +199039,11 @@
199039 u8 aBlob[2] = {0xff, 0xff};
199040 sqlite3_bind_int(pIdxSelect, 1, iSegid);
199041 sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
199042 assert( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
199043 p->rc = sqlite3_reset(pIdxSelect);
199044 sqlite3_bind_null(pIdxSelect, 2);
199045 }
199046 }
199047 #endif
199048 }
199049 }
@@ -197924,10 +199166,11 @@
199166 /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
199167 sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC);
199168 sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
199169 sqlite3_step(p->pIdxWriter);
199170 p->rc = sqlite3_reset(p->pIdxWriter);
199171 sqlite3_bind_null(p->pIdxWriter, 2);
199172 }
199173 pWriter->iBtPage = 0;
199174 }
199175
199176 /*
@@ -203331,11 +204574,11 @@
204574 int nArg, /* Number of args */
204575 sqlite3_value **apUnused /* Function arguments */
204576 ){
204577 assert( nArg==0 );
204578 UNUSED_PARAM2(nArg, apUnused);
204579 sqlite3_result_text(pCtx, "fts5: 2018-03-16 20:23:01 d75e67654aa9620b9617786553a002f54e8c6dcbbcc58948a06bd98a0916d75a", -1, SQLITE_TRANSIENT);
204580 }
204581
204582 static int fts5Init(sqlite3 *db){
204583 static const sqlite3_module fts5Mod = {
204584 /* iVersion */ 2,
@@ -203907,10 +205150,11 @@
205150 if( rc==SQLITE_OK ){
205151 sqlite3_bind_int64(pReplace, 1, iRowid);
205152 sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC);
205153 sqlite3_step(pReplace);
205154 rc = sqlite3_reset(pReplace);
205155 sqlite3_bind_null(pReplace, 2);
205156 }
205157 }
205158 return rc;
205159 }
205160
@@ -204567,10 +205811,11 @@
205811 }else{
205812 sqlite3_bind_int(pReplace, 2, iVal);
205813 }
205814 sqlite3_step(pReplace);
205815 rc = sqlite3_reset(pReplace);
205816 sqlite3_bind_null(pReplace, 1);
205817 }
205818 if( rc==SQLITE_OK && pVal ){
205819 int iNew = p->pConfig->iCookie + 1;
205820 rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
205821 if( rc==SQLITE_OK ){
@@ -207599,12 +208844,12 @@
208844 }
208845 #endif /* SQLITE_CORE */
208846 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
208847
208848 /************** End of stmt.c ************************************************/
208849 #if __LINE__!=208849
208850 #undef SQLITE_SOURCE_ID
208851 #define SQLITE_SOURCE_ID "2018-03-17 16:26:36 442e816b5fed80ebeb58c7c0ab9c2ef999bf488519bf5da670e9cec47703alt2"
208852 #endif
208853 /* Return the source-id for this library */
208854 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
208855 /************************** End of sqlite3.c ******************************/
208856
+180 -77
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -121,13 +121,13 @@
121121
**
122122
** See also: [sqlite3_libversion()],
123123
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124124
** [sqlite_version()] and [sqlite_source_id()].
125125
*/
126
-#define SQLITE_VERSION "3.22.0"
127
-#define SQLITE_VERSION_NUMBER 3022000
128
-#define SQLITE_SOURCE_ID "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d"
126
+#define SQLITE_VERSION "3.23.0"
127
+#define SQLITE_VERSION_NUMBER 3023000
128
+#define SQLITE_SOURCE_ID "2018-03-17 16:26:36 442e816b5fed80ebeb58c7c0ab9c2ef999bf488519bf5da670e9cec477034540"
129129
130130
/*
131131
** CAPI3REF: Run-Time Library Version Numbers
132132
** KEYWORDS: sqlite3_version sqlite3_sourceid
133133
**
@@ -2494,20 +2494,20 @@
24942494
/*
24952495
** CAPI3REF: Formatted String Printing Functions
24962496
**
24972497
** These routines are work-alikes of the "printf()" family of functions
24982498
** from the standard C library.
2499
-** These routines understand most of the common K&R formatting options,
2500
-** plus some additional non-standard formats, detailed below.
2501
-** Note that some of the more obscure formatting options from recent
2502
-** C-library standards are omitted from this implementation.
2499
+** These routines understand most of the common formatting options from
2500
+** the standard library printf()
2501
+** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
2502
+** See the [built-in printf()] documentation for details.
25032503
**
25042504
** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
2505
-** results into memory obtained from [sqlite3_malloc()].
2505
+** results into memory obtained from [sqlite3_malloc64()].
25062506
** The strings returned by these two routines should be
25072507
** released by [sqlite3_free()]. ^Both routines return a
2508
-** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
2508
+** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
25092509
** memory to hold the resulting string.
25102510
**
25112511
** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
25122512
** the standard C library. The result is written into the
25132513
** buffer supplied as the second parameter whose size is given by
@@ -2527,75 +2527,11 @@
25272527
** the zero terminator. So the longest string that can be completely
25282528
** written will be n-1 characters.
25292529
**
25302530
** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
25312531
**
2532
-** These routines all implement some additional formatting
2533
-** options that are useful for constructing SQL statements.
2534
-** All of the usual printf() formatting options apply. In addition, there
2535
-** is are "%q", "%Q", "%w" and "%z" options.
2536
-**
2537
-** ^(The %q option works like %s in that it substitutes a nul-terminated
2538
-** string from the argument list. But %q also doubles every '\'' character.
2539
-** %q is designed for use inside a string literal.)^ By doubling each '\''
2540
-** character it escapes that character and allows it to be inserted into
2541
-** the string.
2542
-**
2543
-** For example, assume the string variable zText contains text as follows:
2544
-**
2545
-** <blockquote><pre>
2546
-** char *zText = "It's a happy day!";
2547
-** </pre></blockquote>
2548
-**
2549
-** One can use this text in an SQL statement as follows:
2550
-**
2551
-** <blockquote><pre>
2552
-** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
2553
-** sqlite3_exec(db, zSQL, 0, 0, 0);
2554
-** sqlite3_free(zSQL);
2555
-** </pre></blockquote>
2556
-**
2557
-** Because the %q format string is used, the '\'' character in zText
2558
-** is escaped and the SQL generated is as follows:
2559
-**
2560
-** <blockquote><pre>
2561
-** INSERT INTO table1 VALUES('It''s a happy day!')
2562
-** </pre></blockquote>
2563
-**
2564
-** This is correct. Had we used %s instead of %q, the generated SQL
2565
-** would have looked like this:
2566
-**
2567
-** <blockquote><pre>
2568
-** INSERT INTO table1 VALUES('It's a happy day!');
2569
-** </pre></blockquote>
2570
-**
2571
-** This second example is an SQL syntax error. As a general rule you should
2572
-** always use %q instead of %s when inserting text into a string literal.
2573
-**
2574
-** ^(The %Q option works like %q except it also adds single quotes around
2575
-** the outside of the total string. Additionally, if the parameter in the
2576
-** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
2577
-** single quotes).)^ So, for example, one could say:
2578
-**
2579
-** <blockquote><pre>
2580
-** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
2581
-** sqlite3_exec(db, zSQL, 0, 0, 0);
2582
-** sqlite3_free(zSQL);
2583
-** </pre></blockquote>
2584
-**
2585
-** The code above will render a correct SQL statement in the zSQL
2586
-** variable even if the zText variable is a NULL pointer.
2587
-**
2588
-** ^(The "%w" formatting option is like "%q" except that it expects to
2589
-** be contained within double-quotes instead of single quotes, and it
2590
-** escapes the double-quote character instead of the single-quote
2591
-** character.)^ The "%w" formatting option is intended for safely inserting
2592
-** table and column names into a constructed SQL statement.
2593
-**
2594
-** ^(The "%z" formatting option works like "%s" but with the
2595
-** addition that after the string has been read and copied into
2596
-** the result, [sqlite3_free()] is called on the input string.)^
2532
+** See also: [built-in printf()], [printf() SQL function]
25972533
*/
25982534
SQLITE_API char *sqlite3_mprintf(const char*,...);
25992535
SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
26002536
SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
26012537
SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
@@ -3657,17 +3593,17 @@
36573593
** ^The specific value of WHERE-clause [parameter] might influence the
36583594
** choice of query plan if the parameter is the left-hand side of a [LIKE]
36593595
** or [GLOB] operator or if the parameter is compared to an indexed column
36603596
** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
36613597
** </li>
3598
+** </ol>
36623599
**
36633600
** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
36643601
** the extra prepFlags parameter, which is a bit array consisting of zero or
36653602
** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
36663603
** sqlite3_prepare_v2() interface works exactly the same as
36673604
** sqlite3_prepare_v3() with a zero prepFlags parameter.
3668
-** </ol>
36693605
*/
36703606
SQLITE_API int sqlite3_prepare(
36713607
sqlite3 *db, /* Database handle */
36723608
const char *zSql, /* SQL statement, UTF-8 encoded */
36733609
int nByte, /* Maximum length of zSql in bytes. */
@@ -7291,10 +7227,19 @@
72917227
** transaction rollback or database recovery operations are not included.
72927228
** If an IO or other error occurs while writing a page to disk, the effect
72937229
** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
72947230
** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
72957231
** </dd>
7232
+**
7233
+** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
7234
+** <dd>This parameter returns the number of dirty cache entries that have
7235
+** been written to disk in the middle of a transaction due to the page
7236
+** cache overflowing. Transactions are more efficient if they are written
7237
+** to disk all at once. When pages spill mid-transaction, that introduces
7238
+** additional overhead. This parameter can be used help identify
7239
+** inefficiencies that can be resolve by increasing the cache size.
7240
+** </dd>
72967241
**
72977242
** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
72987243
** <dd>This parameter returns zero for the current value if and only if
72997244
** all foreign key constraints (deferred or immediate) have been
73007245
** resolved.)^ ^The highwater mark is always 0.
@@ -7311,11 +7256,12 @@
73117256
#define SQLITE_DBSTATUS_CACHE_HIT 7
73127257
#define SQLITE_DBSTATUS_CACHE_MISS 8
73137258
#define SQLITE_DBSTATUS_CACHE_WRITE 9
73147259
#define SQLITE_DBSTATUS_DEFERRED_FKS 10
73157260
#define SQLITE_DBSTATUS_CACHE_USED_SHARED 11
7316
-#define SQLITE_DBSTATUS_MAX 11 /* Largest defined DBSTATUS */
7261
+#define SQLITE_DBSTATUS_CACHE_SPILL 12
7262
+#define SQLITE_DBSTATUS_MAX 12 /* Largest defined DBSTATUS */
73177263
73187264
73197265
/*
73207266
** CAPI3REF: Prepared Statement Status
73217267
** METHOD: sqlite3_stmt
@@ -8791,10 +8737,132 @@
87918737
**
87928738
** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
87938739
*/
87948740
SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
87958741
8742
+/*
8743
+** CAPI3REF: Serialize a database
8744
+**
8745
+** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
8746
+** that is a serialization of the S database on [database connection] D.
8747
+** If P is not a NULL pointer, then the size of the database in bytes
8748
+** is written into *P.
8749
+**
8750
+** For an ordinary on-disk database file, the serialization is just a
8751
+** copy of the disk file. For an in-memory database or a "TEMP" database,
8752
+** the serialization is the same sequence of bytes which would be written
8753
+** to disk if that database where backed up to disk.
8754
+**
8755
+** The usual case is that sqlite3_serialize() copies the serialization of
8756
+** the database into memory obtained from [sqlite3_malloc64()] and returns
8757
+** a pointer to that memory. The caller is responsible for freeing the
8758
+** returned value to avoid a memory leak. However, if the F argument
8759
+** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
8760
+** are made, and the sqlite3_serialize() function will return a pointer
8761
+** to the contiguous memory representation of the database that SQLite
8762
+** is currently using for that database, or NULL if the no such contiguous
8763
+** memory representation of the database exists. A contiguous memory
8764
+** representation of the database will usually only exist if there has
8765
+** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
8766
+** values of D and S.
8767
+** The size of the database is written into *P even if the
8768
+** SQLITE_SERIALIZE_NOCOPY bit is set but no contigious copy
8769
+** of the database exists.
8770
+**
8771
+** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
8772
+** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
8773
+** allocation error occurs.
8774
+**
8775
+** This interface is only available if SQLite is compiled with the
8776
+** [SQLITE_ENABLE_DESERIALIZE] option.
8777
+*/
8778
+SQLITE_API unsigned char *sqlite3_serialize(
8779
+ sqlite3 *db, /* The database connection */
8780
+ const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */
8781
+ sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
8782
+ unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */
8783
+);
8784
+
8785
+/*
8786
+** CAPI3REF: Flags for sqlite3_serialize
8787
+**
8788
+** Zero or more of the following constants can be OR-ed together for
8789
+** the F argument to [sqlite3_serialize(D,S,P,F)].
8790
+**
8791
+** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
8792
+** a pointer to contiguous in-memory database that it is currently using,
8793
+** without making a copy of the database. If SQLite is not currently using
8794
+** a contiguous in-memory database, then this option causes
8795
+** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be
8796
+** using a contiguous in-memory database if it has been initialized by a
8797
+** prior call to [sqlite3_deserialize()].
8798
+*/
8799
+#define SQLITE_SERIALIZE_NOCOPY 0x001 /* Do no memory allocations */
8800
+
8801
+/*
8802
+** CAPI3REF: Deserialize a database
8803
+**
8804
+** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
8805
+** [database connection] D to disconnection from database S and then
8806
+** reopen S as an in-memory database based on the serialization contained
8807
+** in P. The serialized database P is N bytes in size. M is the size of
8808
+** the buffer P, which might be larger than N. If M is larger than N, and
8809
+** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
8810
+** permitted to add content to the in-memory database as long as the total
8811
+** size does not exceed M bytes.
8812
+**
8813
+** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
8814
+** invoke sqlite3_free() on the serialization buffer when the database
8815
+** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
8816
+** SQLite will try to increase the buffer size using sqlite3_realloc64()
8817
+** if writes on the database cause it to grow larger than M bytes.
8818
+**
8819
+** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
8820
+** database is currently in a read transaction or is involved in a backup
8821
+** operation.
8822
+**
8823
+** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
8824
+** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
8825
+** [sqlite3_free()] is invoked on argument P prior to returning.
8826
+**
8827
+** This interface is only available if SQLite is compiled with the
8828
+** [SQLITE_ENABLE_DESERIALIZE] option.
8829
+*/
8830
+SQLITE_API int sqlite3_deserialize(
8831
+ sqlite3 *db, /* The database connection */
8832
+ const char *zSchema, /* Which DB to reopen with the deserialization */
8833
+ unsigned char *pData, /* The serialized database content */
8834
+ sqlite3_int64 szDb, /* Number bytes in the deserialization */
8835
+ sqlite3_int64 szBuf, /* Total size of buffer pData[] */
8836
+ unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
8837
+);
8838
+
8839
+/*
8840
+** CAPI3REF: Flags for sqlite3_deserialize()
8841
+**
8842
+** The following are allowed values for 6th argument (the F argument) to
8843
+** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
8844
+**
8845
+** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
8846
+** in the P argument is held in memory obtained from [sqlite3_malloc64()]
8847
+** and that SQLite should take ownership of this memory and automatically
8848
+** free it when it has finished using it. Without this flag, the caller
8849
+** is resposible for freeing any dynamically allocated memory.
8850
+**
8851
+** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
8852
+** grow the size of the database using calls to [sqlite3_realloc64()]. This
8853
+** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
8854
+** Without this flag, the deserialized database cannot increase in size beyond
8855
+** the number of bytes specified by the M parameter.
8856
+**
8857
+** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
8858
+** should be treated as read-only.
8859
+*/
8860
+#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
8861
+#define SQLITE_DESERIALIZE_RESIZEABLE 2 /* Resize using sqlite3_realloc64() */
8862
+#define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
8863
+
87968864
/*
87978865
** Undo the hack that converts floating point types to integer for
87988866
** builds on processors without floating point support.
87998867
*/
88008868
#ifdef SQLITE_OMIT_FLOATING_POINT
@@ -8938,20 +9006,27 @@
89389006
#endif
89399007
89409008
89419009
/*
89429010
** CAPI3REF: Session Object Handle
9011
+**
9012
+** An instance of this object is a [session] that can be used to
9013
+** record changes to a database.
89439014
*/
89449015
typedef struct sqlite3_session sqlite3_session;
89459016
89469017
/*
89479018
** CAPI3REF: Changeset Iterator Handle
9019
+**
9020
+** An instance of this object acts as a cursor for iterating
9021
+** over the elements of a [changeset] or [patchset].
89489022
*/
89499023
typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
89509024
89519025
/*
89529026
** CAPI3REF: Create A New Session Object
9027
+** CONSTRUCTOR: sqlite3_session
89539028
**
89549029
** Create a new session object attached to database handle db. If successful,
89559030
** a pointer to the new object is written to *ppSession and SQLITE_OK is
89569031
** returned. If an error occurs, *ppSession is set to NULL and an SQLite
89579032
** error code (e.g. SQLITE_NOMEM) is returned.
@@ -8984,10 +9059,11 @@
89849059
sqlite3_session **ppSession /* OUT: New session object */
89859060
);
89869061
89879062
/*
89889063
** CAPI3REF: Delete A Session Object
9064
+** DESTRUCTOR: sqlite3_session
89899065
**
89909066
** Delete a session object previously allocated using
89919067
** [sqlite3session_create()]. Once a session object has been deleted, the
89929068
** results of attempting to use pSession with any other session module
89939069
** function are undefined.
@@ -8999,10 +9075,11 @@
89999075
SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
90009076
90019077
90029078
/*
90039079
** CAPI3REF: Enable Or Disable A Session Object
9080
+** METHOD: sqlite3_session
90049081
**
90059082
** Enable or disable the recording of changes by a session object. When
90069083
** enabled, a session object records changes made to the database. When
90079084
** disabled - it does not. A newly created session object is enabled.
90089085
** Refer to the documentation for [sqlite3session_changeset()] for further
@@ -9018,10 +9095,11 @@
90189095
*/
90199096
SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
90209097
90219098
/*
90229099
** CAPI3REF: Set Or Clear the Indirect Change Flag
9100
+** METHOD: sqlite3_session
90239101
**
90249102
** Each change recorded by a session object is marked as either direct or
90259103
** indirect. A change is marked as indirect if either:
90269104
**
90279105
** <ul>
@@ -9047,10 +9125,11 @@
90479125
*/
90489126
SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
90499127
90509128
/*
90519129
** CAPI3REF: Attach A Table To A Session Object
9130
+** METHOD: sqlite3_session
90529131
**
90539132
** If argument zTab is not NULL, then it is the name of a table to attach
90549133
** to the session object passed as the first argument. All subsequent changes
90559134
** made to the table while the session object is enabled will be recorded. See
90569135
** documentation for [sqlite3session_changeset()] for further details.
@@ -9109,10 +9188,11 @@
91099188
const char *zTab /* Table name */
91109189
);
91119190
91129191
/*
91139192
** CAPI3REF: Set a table filter on a Session Object.
9193
+** METHOD: sqlite3_session
91149194
**
91159195
** The second argument (xFilter) is the "filter callback". For changes to rows
91169196
** in tables that are not attached to the Session object, the filter is called
91179197
** to determine whether changes to the table's rows should be tracked or not.
91189198
** If xFilter returns 0, changes is not tracked. Note that once a table is
@@ -9127,10 +9207,11 @@
91279207
void *pCtx /* First argument passed to xFilter */
91289208
);
91299209
91309210
/*
91319211
** CAPI3REF: Generate A Changeset From A Session Object
9212
+** METHOD: sqlite3_session
91329213
**
91339214
** Obtain a changeset containing changes to the tables attached to the
91349215
** session object passed as the first argument. If successful,
91359216
** set *ppChangeset to point to a buffer containing the changeset
91369217
** and *pnChangeset to the size of the changeset in bytes before returning
@@ -9236,11 +9317,12 @@
92369317
int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
92379318
void **ppChangeset /* OUT: Buffer containing changeset */
92389319
);
92399320
92409321
/*
9241
-** CAPI3REF: Load The Difference Between Tables Into A Session
9322
+** CAPI3REF: Load The Difference Between Tables Into A Session
9323
+** METHOD: sqlite3_session
92429324
**
92439325
** If it is not already attached to the session object passed as the first
92449326
** argument, this function attaches table zTbl in the same manner as the
92459327
** [sqlite3session_attach()] function. If zTbl does not exist, or if it
92469328
** does not have a primary key, this function is a no-op (but does not return
@@ -9301,10 +9383,11 @@
93019383
);
93029384
93039385
93049386
/*
93059387
** CAPI3REF: Generate A Patchset From A Session Object
9388
+** METHOD: sqlite3_session
93069389
**
93079390
** The differences between a patchset and a changeset are that:
93089391
**
93099392
** <ul>
93109393
** <li> DELETE records consist of the primary key fields only. The
@@ -9352,10 +9435,11 @@
93529435
*/
93539436
SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
93549437
93559438
/*
93569439
** CAPI3REF: Create An Iterator To Traverse A Changeset
9440
+** CONSTRUCTOR: sqlite3_changeset_iter
93579441
**
93589442
** Create an iterator used to iterate through the contents of a changeset.
93599443
** If successful, *pp is set to point to the iterator handle and SQLITE_OK
93609444
** is returned. Otherwise, if an error occurs, *pp is set to zero and an
93619445
** SQLite error code is returned.
@@ -9392,10 +9476,11 @@
93929476
);
93939477
93949478
93959479
/*
93969480
** CAPI3REF: Advance A Changeset Iterator
9481
+** METHOD: sqlite3_changeset_iter
93979482
**
93989483
** This function may only be used with iterators created by function
93999484
** [sqlite3changeset_start()]. If it is called on an iterator passed to
94009485
** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
94019486
** is returned and the call has no effect.
@@ -9416,10 +9501,11 @@
94169501
*/
94179502
SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
94189503
94199504
/*
94209505
** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
9506
+** METHOD: sqlite3_changeset_iter
94219507
**
94229508
** The pIter argument passed to this function may either be an iterator
94239509
** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
94249510
** created by [sqlite3changeset_start()]. In the latter case, the most recent
94259511
** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
@@ -9450,10 +9536,11 @@
94509536
int *pbIndirect /* OUT: True for an 'indirect' change */
94519537
);
94529538
94539539
/*
94549540
** CAPI3REF: Obtain The Primary Key Definition Of A Table
9541
+** METHOD: sqlite3_changeset_iter
94559542
**
94569543
** For each modified table, a changeset includes the following:
94579544
**
94589545
** <ul>
94599546
** <li> The number of columns in the table, and
@@ -9481,10 +9568,11 @@
94819568
int *pnCol /* OUT: Number of entries in output array */
94829569
);
94839570
94849571
/*
94859572
** CAPI3REF: Obtain old.* Values From A Changeset Iterator
9573
+** METHOD: sqlite3_changeset_iter
94869574
**
94879575
** The pIter argument passed to this function may either be an iterator
94889576
** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
94899577
** created by [sqlite3changeset_start()]. In the latter case, the most recent
94909578
** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
@@ -9511,10 +9599,11 @@
95119599
sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
95129600
);
95139601
95149602
/*
95159603
** CAPI3REF: Obtain new.* Values From A Changeset Iterator
9604
+** METHOD: sqlite3_changeset_iter
95169605
**
95179606
** The pIter argument passed to this function may either be an iterator
95189607
** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
95199608
** created by [sqlite3changeset_start()]. In the latter case, the most recent
95209609
** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
@@ -9544,10 +9633,11 @@
95449633
sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
95459634
);
95469635
95479636
/*
95489637
** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
9638
+** METHOD: sqlite3_changeset_iter
95499639
**
95509640
** This function should only be used with iterator objects passed to a
95519641
** conflict-handler callback by [sqlite3changeset_apply()] with either
95529642
** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
95539643
** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
@@ -9571,10 +9661,11 @@
95719661
sqlite3_value **ppValue /* OUT: Value from conflicting row */
95729662
);
95739663
95749664
/*
95759665
** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
9666
+** METHOD: sqlite3_changeset_iter
95769667
**
95779668
** This function may only be called with an iterator passed to an
95789669
** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
95799670
** it sets the output variable to the total number of known foreign key
95809671
** violations in the destination database and returns SQLITE_OK.
@@ -9587,10 +9678,11 @@
95879678
);
95889679
95899680
95909681
/*
95919682
** CAPI3REF: Finalize A Changeset Iterator
9683
+** METHOD: sqlite3_changeset_iter
95929684
**
95939685
** This function is used to finalize an iterator allocated with
95949686
** [sqlite3changeset_start()].
95959687
**
95969688
** This function should only be called on iterators created using the
@@ -9603,18 +9695,20 @@
96039695
** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
96049696
** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
96059697
** to that error is returned by this function. Otherwise, SQLITE_OK is
96069698
** returned. This is to allow the following pattern (pseudo-code):
96079699
**
9700
+** <pre>
96089701
** sqlite3changeset_start();
96099702
** while( SQLITE_ROW==sqlite3changeset_next() ){
96109703
** // Do something with change.
96119704
** }
96129705
** rc = sqlite3changeset_finalize();
96139706
** if( rc!=SQLITE_OK ){
96149707
** // An error has occurred
96159708
** }
9709
+** </pre>
96169710
*/
96179711
SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
96189712
96199713
/*
96209714
** CAPI3REF: Invert A Changeset
@@ -9658,10 +9752,11 @@
96589752
**
96599753
** This function combines the two input changesets using an
96609754
** sqlite3_changegroup object. Calling it produces similar results as the
96619755
** following code fragment:
96629756
**
9757
+** <pre>
96639758
** sqlite3_changegroup *pGrp;
96649759
** rc = sqlite3_changegroup_new(&pGrp);
96659760
** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
96669761
** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
96679762
** if( rc==SQLITE_OK ){
@@ -9668,10 +9763,11 @@
96689763
** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
96699764
** }else{
96709765
** *ppOut = 0;
96719766
** *pnOut = 0;
96729767
** }
9768
+** </pre>
96739769
**
96749770
** Refer to the sqlite3_changegroup documentation below for details.
96759771
*/
96769772
SQLITE_API int sqlite3changeset_concat(
96779773
int nA, /* Number of bytes in buffer pA */
@@ -9683,15 +9779,19 @@
96839779
);
96849780
96859781
96869782
/*
96879783
** CAPI3REF: Changegroup Handle
9784
+**
9785
+** A changegroup is an object used to combine two or more
9786
+** [changesets] or [patchsets]
96889787
*/
96899788
typedef struct sqlite3_changegroup sqlite3_changegroup;
96909789
96919790
/*
96929791
** CAPI3REF: Create A New Changegroup Object
9792
+** CONSTRUCTOR: sqlite3_changegroup
96939793
**
96949794
** An sqlite3_changegroup object is used to combine two or more changesets
96959795
** (or patchsets) into a single changeset (or patchset). A single changegroup
96969796
** object may combine changesets or patchsets, but not both. The output is
96979797
** always in the same format as the input.
@@ -9725,10 +9825,11 @@
97259825
*/
97269826
SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
97279827
97289828
/*
97299829
** CAPI3REF: Add A Changeset To A Changegroup
9830
+** METHOD: sqlite3_changegroup
97309831
**
97319832
** Add all changes within the changeset (or patchset) in buffer pData (size
97329833
** nData bytes) to the changegroup.
97339834
**
97349835
** If the buffer contains a patchset, then all prior calls to this function
@@ -9802,10 +9903,11 @@
98029903
*/
98039904
SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
98049905
98059906
/*
98069907
** CAPI3REF: Obtain A Composite Changeset From A Changegroup
9908
+** METHOD: sqlite3_changegroup
98079909
**
98089910
** Obtain a buffer containing a changeset (or patchset) representing the
98099911
** current contents of the changegroup. If the inputs to the changegroup
98109912
** were themselves changesets, the output is a changeset. Or, if the
98119913
** inputs were patchsets, the output is also a patchset.
@@ -9832,10 +9934,11 @@
98329934
void **ppData /* OUT: Pointer to output buffer */
98339935
);
98349936
98359937
/*
98369938
** CAPI3REF: Delete A Changegroup Object
9939
+** DESTRUCTOR: sqlite3_changegroup
98379940
*/
98389941
SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
98399942
98409943
/*
98419944
** CAPI3REF: Apply A Changeset To A Database
98429945
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -121,13 +121,13 @@
121 **
122 ** See also: [sqlite3_libversion()],
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-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
@@ -2494,20 +2494,20 @@
2494 /*
2495 ** CAPI3REF: Formatted String Printing Functions
2496 **
2497 ** These routines are work-alikes of the "printf()" family of functions
2498 ** from the standard C library.
2499 ** These routines understand most of the common K&R formatting options,
2500 ** plus some additional non-standard formats, detailed below.
2501 ** Note that some of the more obscure formatting options from recent
2502 ** C-library standards are omitted from this implementation.
2503 **
2504 ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
2505 ** results into memory obtained from [sqlite3_malloc()].
2506 ** The strings returned by these two routines should be
2507 ** released by [sqlite3_free()]. ^Both routines return a
2508 ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
2509 ** memory to hold the resulting string.
2510 **
2511 ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
2512 ** the standard C library. The result is written into the
2513 ** buffer supplied as the second parameter whose size is given by
@@ -2527,75 +2527,11 @@
2527 ** the zero terminator. So the longest string that can be completely
2528 ** written will be n-1 characters.
2529 **
2530 ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
2531 **
2532 ** These routines all implement some additional formatting
2533 ** options that are useful for constructing SQL statements.
2534 ** All of the usual printf() formatting options apply. In addition, there
2535 ** is are "%q", "%Q", "%w" and "%z" options.
2536 **
2537 ** ^(The %q option works like %s in that it substitutes a nul-terminated
2538 ** string from the argument list. But %q also doubles every '\'' character.
2539 ** %q is designed for use inside a string literal.)^ By doubling each '\''
2540 ** character it escapes that character and allows it to be inserted into
2541 ** the string.
2542 **
2543 ** For example, assume the string variable zText contains text as follows:
2544 **
2545 ** <blockquote><pre>
2546 ** char *zText = "It's a happy day!";
2547 ** </pre></blockquote>
2548 **
2549 ** One can use this text in an SQL statement as follows:
2550 **
2551 ** <blockquote><pre>
2552 ** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
2553 ** sqlite3_exec(db, zSQL, 0, 0, 0);
2554 ** sqlite3_free(zSQL);
2555 ** </pre></blockquote>
2556 **
2557 ** Because the %q format string is used, the '\'' character in zText
2558 ** is escaped and the SQL generated is as follows:
2559 **
2560 ** <blockquote><pre>
2561 ** INSERT INTO table1 VALUES('It''s a happy day!')
2562 ** </pre></blockquote>
2563 **
2564 ** This is correct. Had we used %s instead of %q, the generated SQL
2565 ** would have looked like this:
2566 **
2567 ** <blockquote><pre>
2568 ** INSERT INTO table1 VALUES('It's a happy day!');
2569 ** </pre></blockquote>
2570 **
2571 ** This second example is an SQL syntax error. As a general rule you should
2572 ** always use %q instead of %s when inserting text into a string literal.
2573 **
2574 ** ^(The %Q option works like %q except it also adds single quotes around
2575 ** the outside of the total string. Additionally, if the parameter in the
2576 ** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
2577 ** single quotes).)^ So, for example, one could say:
2578 **
2579 ** <blockquote><pre>
2580 ** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
2581 ** sqlite3_exec(db, zSQL, 0, 0, 0);
2582 ** sqlite3_free(zSQL);
2583 ** </pre></blockquote>
2584 **
2585 ** The code above will render a correct SQL statement in the zSQL
2586 ** variable even if the zText variable is a NULL pointer.
2587 **
2588 ** ^(The "%w" formatting option is like "%q" except that it expects to
2589 ** be contained within double-quotes instead of single quotes, and it
2590 ** escapes the double-quote character instead of the single-quote
2591 ** character.)^ The "%w" formatting option is intended for safely inserting
2592 ** table and column names into a constructed SQL statement.
2593 **
2594 ** ^(The "%z" formatting option works like "%s" but with the
2595 ** addition that after the string has been read and copied into
2596 ** the result, [sqlite3_free()] is called on the input string.)^
2597 */
2598 SQLITE_API char *sqlite3_mprintf(const char*,...);
2599 SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
2600 SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
2601 SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
@@ -3657,17 +3593,17 @@
3657 ** ^The specific value of WHERE-clause [parameter] might influence the
3658 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3659 ** or [GLOB] operator or if the parameter is compared to an indexed column
3660 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
3661 ** </li>
 
3662 **
3663 ** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
3664 ** the extra prepFlags parameter, which is a bit array consisting of zero or
3665 ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
3666 ** sqlite3_prepare_v2() interface works exactly the same as
3667 ** sqlite3_prepare_v3() with a zero prepFlags parameter.
3668 ** </ol>
3669 */
3670 SQLITE_API int sqlite3_prepare(
3671 sqlite3 *db, /* Database handle */
3672 const char *zSql, /* SQL statement, UTF-8 encoded */
3673 int nByte, /* Maximum length of zSql in bytes. */
@@ -7291,10 +7227,19 @@
7291 ** transaction rollback or database recovery operations are not included.
7292 ** If an IO or other error occurs while writing a page to disk, the effect
7293 ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
7294 ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
7295 ** </dd>
 
 
 
 
 
 
 
 
 
7296 **
7297 ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
7298 ** <dd>This parameter returns zero for the current value if and only if
7299 ** all foreign key constraints (deferred or immediate) have been
7300 ** resolved.)^ ^The highwater mark is always 0.
@@ -7311,11 +7256,12 @@
7311 #define SQLITE_DBSTATUS_CACHE_HIT 7
7312 #define SQLITE_DBSTATUS_CACHE_MISS 8
7313 #define SQLITE_DBSTATUS_CACHE_WRITE 9
7314 #define SQLITE_DBSTATUS_DEFERRED_FKS 10
7315 #define SQLITE_DBSTATUS_CACHE_USED_SHARED 11
7316 #define SQLITE_DBSTATUS_MAX 11 /* Largest defined DBSTATUS */
 
7317
7318
7319 /*
7320 ** CAPI3REF: Prepared Statement Status
7321 ** METHOD: sqlite3_stmt
@@ -8791,10 +8737,132 @@
8791 **
8792 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
8793 */
8794 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
8795
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8796 /*
8797 ** Undo the hack that converts floating point types to integer for
8798 ** builds on processors without floating point support.
8799 */
8800 #ifdef SQLITE_OMIT_FLOATING_POINT
@@ -8938,20 +9006,27 @@
8938 #endif
8939
8940
8941 /*
8942 ** CAPI3REF: Session Object Handle
 
 
 
8943 */
8944 typedef struct sqlite3_session sqlite3_session;
8945
8946 /*
8947 ** CAPI3REF: Changeset Iterator Handle
 
 
 
8948 */
8949 typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
8950
8951 /*
8952 ** CAPI3REF: Create A New Session Object
 
8953 **
8954 ** Create a new session object attached to database handle db. If successful,
8955 ** a pointer to the new object is written to *ppSession and SQLITE_OK is
8956 ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
8957 ** error code (e.g. SQLITE_NOMEM) is returned.
@@ -8984,10 +9059,11 @@
8984 sqlite3_session **ppSession /* OUT: New session object */
8985 );
8986
8987 /*
8988 ** CAPI3REF: Delete A Session Object
 
8989 **
8990 ** Delete a session object previously allocated using
8991 ** [sqlite3session_create()]. Once a session object has been deleted, the
8992 ** results of attempting to use pSession with any other session module
8993 ** function are undefined.
@@ -8999,10 +9075,11 @@
8999 SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
9000
9001
9002 /*
9003 ** CAPI3REF: Enable Or Disable A Session Object
 
9004 **
9005 ** Enable or disable the recording of changes by a session object. When
9006 ** enabled, a session object records changes made to the database. When
9007 ** disabled - it does not. A newly created session object is enabled.
9008 ** Refer to the documentation for [sqlite3session_changeset()] for further
@@ -9018,10 +9095,11 @@
9018 */
9019 SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
9020
9021 /*
9022 ** CAPI3REF: Set Or Clear the Indirect Change Flag
 
9023 **
9024 ** Each change recorded by a session object is marked as either direct or
9025 ** indirect. A change is marked as indirect if either:
9026 **
9027 ** <ul>
@@ -9047,10 +9125,11 @@
9047 */
9048 SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
9049
9050 /*
9051 ** CAPI3REF: Attach A Table To A Session Object
 
9052 **
9053 ** If argument zTab is not NULL, then it is the name of a table to attach
9054 ** to the session object passed as the first argument. All subsequent changes
9055 ** made to the table while the session object is enabled will be recorded. See
9056 ** documentation for [sqlite3session_changeset()] for further details.
@@ -9109,10 +9188,11 @@
9109 const char *zTab /* Table name */
9110 );
9111
9112 /*
9113 ** CAPI3REF: Set a table filter on a Session Object.
 
9114 **
9115 ** The second argument (xFilter) is the "filter callback". For changes to rows
9116 ** in tables that are not attached to the Session object, the filter is called
9117 ** to determine whether changes to the table's rows should be tracked or not.
9118 ** If xFilter returns 0, changes is not tracked. Note that once a table is
@@ -9127,10 +9207,11 @@
9127 void *pCtx /* First argument passed to xFilter */
9128 );
9129
9130 /*
9131 ** CAPI3REF: Generate A Changeset From A Session Object
 
9132 **
9133 ** Obtain a changeset containing changes to the tables attached to the
9134 ** session object passed as the first argument. If successful,
9135 ** set *ppChangeset to point to a buffer containing the changeset
9136 ** and *pnChangeset to the size of the changeset in bytes before returning
@@ -9236,11 +9317,12 @@
9236 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
9237 void **ppChangeset /* OUT: Buffer containing changeset */
9238 );
9239
9240 /*
9241 ** CAPI3REF: Load The Difference Between Tables Into A Session
 
9242 **
9243 ** If it is not already attached to the session object passed as the first
9244 ** argument, this function attaches table zTbl in the same manner as the
9245 ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
9246 ** does not have a primary key, this function is a no-op (but does not return
@@ -9301,10 +9383,11 @@
9301 );
9302
9303
9304 /*
9305 ** CAPI3REF: Generate A Patchset From A Session Object
 
9306 **
9307 ** The differences between a patchset and a changeset are that:
9308 **
9309 ** <ul>
9310 ** <li> DELETE records consist of the primary key fields only. The
@@ -9352,10 +9435,11 @@
9352 */
9353 SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
9354
9355 /*
9356 ** CAPI3REF: Create An Iterator To Traverse A Changeset
 
9357 **
9358 ** Create an iterator used to iterate through the contents of a changeset.
9359 ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
9360 ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
9361 ** SQLite error code is returned.
@@ -9392,10 +9476,11 @@
9392 );
9393
9394
9395 /*
9396 ** CAPI3REF: Advance A Changeset Iterator
 
9397 **
9398 ** This function may only be used with iterators created by function
9399 ** [sqlite3changeset_start()]. If it is called on an iterator passed to
9400 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
9401 ** is returned and the call has no effect.
@@ -9416,10 +9501,11 @@
9416 */
9417 SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
9418
9419 /*
9420 ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
 
9421 **
9422 ** The pIter argument passed to this function may either be an iterator
9423 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
9424 ** created by [sqlite3changeset_start()]. In the latter case, the most recent
9425 ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
@@ -9450,10 +9536,11 @@
9450 int *pbIndirect /* OUT: True for an 'indirect' change */
9451 );
9452
9453 /*
9454 ** CAPI3REF: Obtain The Primary Key Definition Of A Table
 
9455 **
9456 ** For each modified table, a changeset includes the following:
9457 **
9458 ** <ul>
9459 ** <li> The number of columns in the table, and
@@ -9481,10 +9568,11 @@
9481 int *pnCol /* OUT: Number of entries in output array */
9482 );
9483
9484 /*
9485 ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
 
9486 **
9487 ** The pIter argument passed to this function may either be an iterator
9488 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
9489 ** created by [sqlite3changeset_start()]. In the latter case, the most recent
9490 ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
@@ -9511,10 +9599,11 @@
9511 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
9512 );
9513
9514 /*
9515 ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
 
9516 **
9517 ** The pIter argument passed to this function may either be an iterator
9518 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
9519 ** created by [sqlite3changeset_start()]. In the latter case, the most recent
9520 ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
@@ -9544,10 +9633,11 @@
9544 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
9545 );
9546
9547 /*
9548 ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
 
9549 **
9550 ** This function should only be used with iterator objects passed to a
9551 ** conflict-handler callback by [sqlite3changeset_apply()] with either
9552 ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
9553 ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
@@ -9571,10 +9661,11 @@
9571 sqlite3_value **ppValue /* OUT: Value from conflicting row */
9572 );
9573
9574 /*
9575 ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
 
9576 **
9577 ** This function may only be called with an iterator passed to an
9578 ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
9579 ** it sets the output variable to the total number of known foreign key
9580 ** violations in the destination database and returns SQLITE_OK.
@@ -9587,10 +9678,11 @@
9587 );
9588
9589
9590 /*
9591 ** CAPI3REF: Finalize A Changeset Iterator
 
9592 **
9593 ** This function is used to finalize an iterator allocated with
9594 ** [sqlite3changeset_start()].
9595 **
9596 ** This function should only be called on iterators created using the
@@ -9603,18 +9695,20 @@
9603 ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
9604 ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
9605 ** to that error is returned by this function. Otherwise, SQLITE_OK is
9606 ** returned. This is to allow the following pattern (pseudo-code):
9607 **
 
9608 ** sqlite3changeset_start();
9609 ** while( SQLITE_ROW==sqlite3changeset_next() ){
9610 ** // Do something with change.
9611 ** }
9612 ** rc = sqlite3changeset_finalize();
9613 ** if( rc!=SQLITE_OK ){
9614 ** // An error has occurred
9615 ** }
 
9616 */
9617 SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
9618
9619 /*
9620 ** CAPI3REF: Invert A Changeset
@@ -9658,10 +9752,11 @@
9658 **
9659 ** This function combines the two input changesets using an
9660 ** sqlite3_changegroup object. Calling it produces similar results as the
9661 ** following code fragment:
9662 **
 
9663 ** sqlite3_changegroup *pGrp;
9664 ** rc = sqlite3_changegroup_new(&pGrp);
9665 ** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
9666 ** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
9667 ** if( rc==SQLITE_OK ){
@@ -9668,10 +9763,11 @@
9668 ** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
9669 ** }else{
9670 ** *ppOut = 0;
9671 ** *pnOut = 0;
9672 ** }
 
9673 **
9674 ** Refer to the sqlite3_changegroup documentation below for details.
9675 */
9676 SQLITE_API int sqlite3changeset_concat(
9677 int nA, /* Number of bytes in buffer pA */
@@ -9683,15 +9779,19 @@
9683 );
9684
9685
9686 /*
9687 ** CAPI3REF: Changegroup Handle
 
 
 
9688 */
9689 typedef struct sqlite3_changegroup sqlite3_changegroup;
9690
9691 /*
9692 ** CAPI3REF: Create A New Changegroup Object
 
9693 **
9694 ** An sqlite3_changegroup object is used to combine two or more changesets
9695 ** (or patchsets) into a single changeset (or patchset). A single changegroup
9696 ** object may combine changesets or patchsets, but not both. The output is
9697 ** always in the same format as the input.
@@ -9725,10 +9825,11 @@
9725 */
9726 SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
9727
9728 /*
9729 ** CAPI3REF: Add A Changeset To A Changegroup
 
9730 **
9731 ** Add all changes within the changeset (or patchset) in buffer pData (size
9732 ** nData bytes) to the changegroup.
9733 **
9734 ** If the buffer contains a patchset, then all prior calls to this function
@@ -9802,10 +9903,11 @@
9802 */
9803 SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
9804
9805 /*
9806 ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
 
9807 **
9808 ** Obtain a buffer containing a changeset (or patchset) representing the
9809 ** current contents of the changegroup. If the inputs to the changegroup
9810 ** were themselves changesets, the output is a changeset. Or, if the
9811 ** inputs were patchsets, the output is also a patchset.
@@ -9832,10 +9934,11 @@
9832 void **ppData /* OUT: Pointer to output buffer */
9833 );
9834
9835 /*
9836 ** CAPI3REF: Delete A Changegroup Object
 
9837 */
9838 SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
9839
9840 /*
9841 ** CAPI3REF: Apply A Changeset To A Database
9842
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -121,13 +121,13 @@
121 **
122 ** See also: [sqlite3_libversion()],
123 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124 ** [sqlite_version()] and [sqlite_source_id()].
125 */
126 #define SQLITE_VERSION "3.23.0"
127 #define SQLITE_VERSION_NUMBER 3023000
128 #define SQLITE_SOURCE_ID "2018-03-17 16:26:36 442e816b5fed80ebeb58c7c0ab9c2ef999bf488519bf5da670e9cec477034540"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
@@ -2494,20 +2494,20 @@
2494 /*
2495 ** CAPI3REF: Formatted String Printing Functions
2496 **
2497 ** These routines are work-alikes of the "printf()" family of functions
2498 ** from the standard C library.
2499 ** These routines understand most of the common formatting options from
2500 ** the standard library printf()
2501 ** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
2502 ** See the [built-in printf()] documentation for details.
2503 **
2504 ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
2505 ** results into memory obtained from [sqlite3_malloc64()].
2506 ** The strings returned by these two routines should be
2507 ** released by [sqlite3_free()]. ^Both routines return a
2508 ** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
2509 ** memory to hold the resulting string.
2510 **
2511 ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
2512 ** the standard C library. The result is written into the
2513 ** buffer supplied as the second parameter whose size is given by
@@ -2527,75 +2527,11 @@
2527 ** the zero terminator. So the longest string that can be completely
2528 ** written will be n-1 characters.
2529 **
2530 ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
2531 **
2532 ** See also: [built-in printf()], [printf() SQL function]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2533 */
2534 SQLITE_API char *sqlite3_mprintf(const char*,...);
2535 SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
2536 SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
2537 SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
@@ -3657,17 +3593,17 @@
3593 ** ^The specific value of WHERE-clause [parameter] might influence the
3594 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3595 ** or [GLOB] operator or if the parameter is compared to an indexed column
3596 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
3597 ** </li>
3598 ** </ol>
3599 **
3600 ** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
3601 ** the extra prepFlags parameter, which is a bit array consisting of zero or
3602 ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
3603 ** sqlite3_prepare_v2() interface works exactly the same as
3604 ** sqlite3_prepare_v3() with a zero prepFlags parameter.
 
3605 */
3606 SQLITE_API int sqlite3_prepare(
3607 sqlite3 *db, /* Database handle */
3608 const char *zSql, /* SQL statement, UTF-8 encoded */
3609 int nByte, /* Maximum length of zSql in bytes. */
@@ -7291,10 +7227,19 @@
7227 ** transaction rollback or database recovery operations are not included.
7228 ** If an IO or other error occurs while writing a page to disk, the effect
7229 ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
7230 ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
7231 ** </dd>
7232 **
7233 ** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
7234 ** <dd>This parameter returns the number of dirty cache entries that have
7235 ** been written to disk in the middle of a transaction due to the page
7236 ** cache overflowing. Transactions are more efficient if they are written
7237 ** to disk all at once. When pages spill mid-transaction, that introduces
7238 ** additional overhead. This parameter can be used help identify
7239 ** inefficiencies that can be resolve by increasing the cache size.
7240 ** </dd>
7241 **
7242 ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
7243 ** <dd>This parameter returns zero for the current value if and only if
7244 ** all foreign key constraints (deferred or immediate) have been
7245 ** resolved.)^ ^The highwater mark is always 0.
@@ -7311,11 +7256,12 @@
7256 #define SQLITE_DBSTATUS_CACHE_HIT 7
7257 #define SQLITE_DBSTATUS_CACHE_MISS 8
7258 #define SQLITE_DBSTATUS_CACHE_WRITE 9
7259 #define SQLITE_DBSTATUS_DEFERRED_FKS 10
7260 #define SQLITE_DBSTATUS_CACHE_USED_SHARED 11
7261 #define SQLITE_DBSTATUS_CACHE_SPILL 12
7262 #define SQLITE_DBSTATUS_MAX 12 /* Largest defined DBSTATUS */
7263
7264
7265 /*
7266 ** CAPI3REF: Prepared Statement Status
7267 ** METHOD: sqlite3_stmt
@@ -8791,10 +8737,132 @@
8737 **
8738 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
8739 */
8740 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
8741
8742 /*
8743 ** CAPI3REF: Serialize a database
8744 **
8745 ** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
8746 ** that is a serialization of the S database on [database connection] D.
8747 ** If P is not a NULL pointer, then the size of the database in bytes
8748 ** is written into *P.
8749 **
8750 ** For an ordinary on-disk database file, the serialization is just a
8751 ** copy of the disk file. For an in-memory database or a "TEMP" database,
8752 ** the serialization is the same sequence of bytes which would be written
8753 ** to disk if that database where backed up to disk.
8754 **
8755 ** The usual case is that sqlite3_serialize() copies the serialization of
8756 ** the database into memory obtained from [sqlite3_malloc64()] and returns
8757 ** a pointer to that memory. The caller is responsible for freeing the
8758 ** returned value to avoid a memory leak. However, if the F argument
8759 ** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
8760 ** are made, and the sqlite3_serialize() function will return a pointer
8761 ** to the contiguous memory representation of the database that SQLite
8762 ** is currently using for that database, or NULL if the no such contiguous
8763 ** memory representation of the database exists. A contiguous memory
8764 ** representation of the database will usually only exist if there has
8765 ** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
8766 ** values of D and S.
8767 ** The size of the database is written into *P even if the
8768 ** SQLITE_SERIALIZE_NOCOPY bit is set but no contigious copy
8769 ** of the database exists.
8770 **
8771 ** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
8772 ** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
8773 ** allocation error occurs.
8774 **
8775 ** This interface is only available if SQLite is compiled with the
8776 ** [SQLITE_ENABLE_DESERIALIZE] option.
8777 */
8778 SQLITE_API unsigned char *sqlite3_serialize(
8779 sqlite3 *db, /* The database connection */
8780 const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */
8781 sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
8782 unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */
8783 );
8784
8785 /*
8786 ** CAPI3REF: Flags for sqlite3_serialize
8787 **
8788 ** Zero or more of the following constants can be OR-ed together for
8789 ** the F argument to [sqlite3_serialize(D,S,P,F)].
8790 **
8791 ** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
8792 ** a pointer to contiguous in-memory database that it is currently using,
8793 ** without making a copy of the database. If SQLite is not currently using
8794 ** a contiguous in-memory database, then this option causes
8795 ** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be
8796 ** using a contiguous in-memory database if it has been initialized by a
8797 ** prior call to [sqlite3_deserialize()].
8798 */
8799 #define SQLITE_SERIALIZE_NOCOPY 0x001 /* Do no memory allocations */
8800
8801 /*
8802 ** CAPI3REF: Deserialize a database
8803 **
8804 ** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
8805 ** [database connection] D to disconnection from database S and then
8806 ** reopen S as an in-memory database based on the serialization contained
8807 ** in P. The serialized database P is N bytes in size. M is the size of
8808 ** the buffer P, which might be larger than N. If M is larger than N, and
8809 ** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
8810 ** permitted to add content to the in-memory database as long as the total
8811 ** size does not exceed M bytes.
8812 **
8813 ** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
8814 ** invoke sqlite3_free() on the serialization buffer when the database
8815 ** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
8816 ** SQLite will try to increase the buffer size using sqlite3_realloc64()
8817 ** if writes on the database cause it to grow larger than M bytes.
8818 **
8819 ** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
8820 ** database is currently in a read transaction or is involved in a backup
8821 ** operation.
8822 **
8823 ** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
8824 ** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
8825 ** [sqlite3_free()] is invoked on argument P prior to returning.
8826 **
8827 ** This interface is only available if SQLite is compiled with the
8828 ** [SQLITE_ENABLE_DESERIALIZE] option.
8829 */
8830 SQLITE_API int sqlite3_deserialize(
8831 sqlite3 *db, /* The database connection */
8832 const char *zSchema, /* Which DB to reopen with the deserialization */
8833 unsigned char *pData, /* The serialized database content */
8834 sqlite3_int64 szDb, /* Number bytes in the deserialization */
8835 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
8836 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
8837 );
8838
8839 /*
8840 ** CAPI3REF: Flags for sqlite3_deserialize()
8841 **
8842 ** The following are allowed values for 6th argument (the F argument) to
8843 ** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
8844 **
8845 ** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
8846 ** in the P argument is held in memory obtained from [sqlite3_malloc64()]
8847 ** and that SQLite should take ownership of this memory and automatically
8848 ** free it when it has finished using it. Without this flag, the caller
8849 ** is resposible for freeing any dynamically allocated memory.
8850 **
8851 ** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
8852 ** grow the size of the database using calls to [sqlite3_realloc64()]. This
8853 ** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
8854 ** Without this flag, the deserialized database cannot increase in size beyond
8855 ** the number of bytes specified by the M parameter.
8856 **
8857 ** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
8858 ** should be treated as read-only.
8859 */
8860 #define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
8861 #define SQLITE_DESERIALIZE_RESIZEABLE 2 /* Resize using sqlite3_realloc64() */
8862 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
8863
8864 /*
8865 ** Undo the hack that converts floating point types to integer for
8866 ** builds on processors without floating point support.
8867 */
8868 #ifdef SQLITE_OMIT_FLOATING_POINT
@@ -8938,20 +9006,27 @@
9006 #endif
9007
9008
9009 /*
9010 ** CAPI3REF: Session Object Handle
9011 **
9012 ** An instance of this object is a [session] that can be used to
9013 ** record changes to a database.
9014 */
9015 typedef struct sqlite3_session sqlite3_session;
9016
9017 /*
9018 ** CAPI3REF: Changeset Iterator Handle
9019 **
9020 ** An instance of this object acts as a cursor for iterating
9021 ** over the elements of a [changeset] or [patchset].
9022 */
9023 typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
9024
9025 /*
9026 ** CAPI3REF: Create A New Session Object
9027 ** CONSTRUCTOR: sqlite3_session
9028 **
9029 ** Create a new session object attached to database handle db. If successful,
9030 ** a pointer to the new object is written to *ppSession and SQLITE_OK is
9031 ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
9032 ** error code (e.g. SQLITE_NOMEM) is returned.
@@ -8984,10 +9059,11 @@
9059 sqlite3_session **ppSession /* OUT: New session object */
9060 );
9061
9062 /*
9063 ** CAPI3REF: Delete A Session Object
9064 ** DESTRUCTOR: sqlite3_session
9065 **
9066 ** Delete a session object previously allocated using
9067 ** [sqlite3session_create()]. Once a session object has been deleted, the
9068 ** results of attempting to use pSession with any other session module
9069 ** function are undefined.
@@ -8999,10 +9075,11 @@
9075 SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
9076
9077
9078 /*
9079 ** CAPI3REF: Enable Or Disable A Session Object
9080 ** METHOD: sqlite3_session
9081 **
9082 ** Enable or disable the recording of changes by a session object. When
9083 ** enabled, a session object records changes made to the database. When
9084 ** disabled - it does not. A newly created session object is enabled.
9085 ** Refer to the documentation for [sqlite3session_changeset()] for further
@@ -9018,10 +9095,11 @@
9095 */
9096 SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
9097
9098 /*
9099 ** CAPI3REF: Set Or Clear the Indirect Change Flag
9100 ** METHOD: sqlite3_session
9101 **
9102 ** Each change recorded by a session object is marked as either direct or
9103 ** indirect. A change is marked as indirect if either:
9104 **
9105 ** <ul>
@@ -9047,10 +9125,11 @@
9125 */
9126 SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
9127
9128 /*
9129 ** CAPI3REF: Attach A Table To A Session Object
9130 ** METHOD: sqlite3_session
9131 **
9132 ** If argument zTab is not NULL, then it is the name of a table to attach
9133 ** to the session object passed as the first argument. All subsequent changes
9134 ** made to the table while the session object is enabled will be recorded. See
9135 ** documentation for [sqlite3session_changeset()] for further details.
@@ -9109,10 +9188,11 @@
9188 const char *zTab /* Table name */
9189 );
9190
9191 /*
9192 ** CAPI3REF: Set a table filter on a Session Object.
9193 ** METHOD: sqlite3_session
9194 **
9195 ** The second argument (xFilter) is the "filter callback". For changes to rows
9196 ** in tables that are not attached to the Session object, the filter is called
9197 ** to determine whether changes to the table's rows should be tracked or not.
9198 ** If xFilter returns 0, changes is not tracked. Note that once a table is
@@ -9127,10 +9207,11 @@
9207 void *pCtx /* First argument passed to xFilter */
9208 );
9209
9210 /*
9211 ** CAPI3REF: Generate A Changeset From A Session Object
9212 ** METHOD: sqlite3_session
9213 **
9214 ** Obtain a changeset containing changes to the tables attached to the
9215 ** session object passed as the first argument. If successful,
9216 ** set *ppChangeset to point to a buffer containing the changeset
9217 ** and *pnChangeset to the size of the changeset in bytes before returning
@@ -9236,11 +9317,12 @@
9317 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
9318 void **ppChangeset /* OUT: Buffer containing changeset */
9319 );
9320
9321 /*
9322 ** CAPI3REF: Load The Difference Between Tables Into A Session
9323 ** METHOD: sqlite3_session
9324 **
9325 ** If it is not already attached to the session object passed as the first
9326 ** argument, this function attaches table zTbl in the same manner as the
9327 ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
9328 ** does not have a primary key, this function is a no-op (but does not return
@@ -9301,10 +9383,11 @@
9383 );
9384
9385
9386 /*
9387 ** CAPI3REF: Generate A Patchset From A Session Object
9388 ** METHOD: sqlite3_session
9389 **
9390 ** The differences between a patchset and a changeset are that:
9391 **
9392 ** <ul>
9393 ** <li> DELETE records consist of the primary key fields only. The
@@ -9352,10 +9435,11 @@
9435 */
9436 SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
9437
9438 /*
9439 ** CAPI3REF: Create An Iterator To Traverse A Changeset
9440 ** CONSTRUCTOR: sqlite3_changeset_iter
9441 **
9442 ** Create an iterator used to iterate through the contents of a changeset.
9443 ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
9444 ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
9445 ** SQLite error code is returned.
@@ -9392,10 +9476,11 @@
9476 );
9477
9478
9479 /*
9480 ** CAPI3REF: Advance A Changeset Iterator
9481 ** METHOD: sqlite3_changeset_iter
9482 **
9483 ** This function may only be used with iterators created by function
9484 ** [sqlite3changeset_start()]. If it is called on an iterator passed to
9485 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
9486 ** is returned and the call has no effect.
@@ -9416,10 +9501,11 @@
9501 */
9502 SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
9503
9504 /*
9505 ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
9506 ** METHOD: sqlite3_changeset_iter
9507 **
9508 ** The pIter argument passed to this function may either be an iterator
9509 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
9510 ** created by [sqlite3changeset_start()]. In the latter case, the most recent
9511 ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
@@ -9450,10 +9536,11 @@
9536 int *pbIndirect /* OUT: True for an 'indirect' change */
9537 );
9538
9539 /*
9540 ** CAPI3REF: Obtain The Primary Key Definition Of A Table
9541 ** METHOD: sqlite3_changeset_iter
9542 **
9543 ** For each modified table, a changeset includes the following:
9544 **
9545 ** <ul>
9546 ** <li> The number of columns in the table, and
@@ -9481,10 +9568,11 @@
9568 int *pnCol /* OUT: Number of entries in output array */
9569 );
9570
9571 /*
9572 ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
9573 ** METHOD: sqlite3_changeset_iter
9574 **
9575 ** The pIter argument passed to this function may either be an iterator
9576 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
9577 ** created by [sqlite3changeset_start()]. In the latter case, the most recent
9578 ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
@@ -9511,10 +9599,11 @@
9599 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
9600 );
9601
9602 /*
9603 ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
9604 ** METHOD: sqlite3_changeset_iter
9605 **
9606 ** The pIter argument passed to this function may either be an iterator
9607 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
9608 ** created by [sqlite3changeset_start()]. In the latter case, the most recent
9609 ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
@@ -9544,10 +9633,11 @@
9633 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
9634 );
9635
9636 /*
9637 ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
9638 ** METHOD: sqlite3_changeset_iter
9639 **
9640 ** This function should only be used with iterator objects passed to a
9641 ** conflict-handler callback by [sqlite3changeset_apply()] with either
9642 ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
9643 ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
@@ -9571,10 +9661,11 @@
9661 sqlite3_value **ppValue /* OUT: Value from conflicting row */
9662 );
9663
9664 /*
9665 ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
9666 ** METHOD: sqlite3_changeset_iter
9667 **
9668 ** This function may only be called with an iterator passed to an
9669 ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
9670 ** it sets the output variable to the total number of known foreign key
9671 ** violations in the destination database and returns SQLITE_OK.
@@ -9587,10 +9678,11 @@
9678 );
9679
9680
9681 /*
9682 ** CAPI3REF: Finalize A Changeset Iterator
9683 ** METHOD: sqlite3_changeset_iter
9684 **
9685 ** This function is used to finalize an iterator allocated with
9686 ** [sqlite3changeset_start()].
9687 **
9688 ** This function should only be called on iterators created using the
@@ -9603,18 +9695,20 @@
9695 ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
9696 ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
9697 ** to that error is returned by this function. Otherwise, SQLITE_OK is
9698 ** returned. This is to allow the following pattern (pseudo-code):
9699 **
9700 ** <pre>
9701 ** sqlite3changeset_start();
9702 ** while( SQLITE_ROW==sqlite3changeset_next() ){
9703 ** // Do something with change.
9704 ** }
9705 ** rc = sqlite3changeset_finalize();
9706 ** if( rc!=SQLITE_OK ){
9707 ** // An error has occurred
9708 ** }
9709 ** </pre>
9710 */
9711 SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
9712
9713 /*
9714 ** CAPI3REF: Invert A Changeset
@@ -9658,10 +9752,11 @@
9752 **
9753 ** This function combines the two input changesets using an
9754 ** sqlite3_changegroup object. Calling it produces similar results as the
9755 ** following code fragment:
9756 **
9757 ** <pre>
9758 ** sqlite3_changegroup *pGrp;
9759 ** rc = sqlite3_changegroup_new(&pGrp);
9760 ** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
9761 ** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
9762 ** if( rc==SQLITE_OK ){
@@ -9668,10 +9763,11 @@
9763 ** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
9764 ** }else{
9765 ** *ppOut = 0;
9766 ** *pnOut = 0;
9767 ** }
9768 ** </pre>
9769 **
9770 ** Refer to the sqlite3_changegroup documentation below for details.
9771 */
9772 SQLITE_API int sqlite3changeset_concat(
9773 int nA, /* Number of bytes in buffer pA */
@@ -9683,15 +9779,19 @@
9779 );
9780
9781
9782 /*
9783 ** CAPI3REF: Changegroup Handle
9784 **
9785 ** A changegroup is an object used to combine two or more
9786 ** [changesets] or [patchsets]
9787 */
9788 typedef struct sqlite3_changegroup sqlite3_changegroup;
9789
9790 /*
9791 ** CAPI3REF: Create A New Changegroup Object
9792 ** CONSTRUCTOR: sqlite3_changegroup
9793 **
9794 ** An sqlite3_changegroup object is used to combine two or more changesets
9795 ** (or patchsets) into a single changeset (or patchset). A single changegroup
9796 ** object may combine changesets or patchsets, but not both. The output is
9797 ** always in the same format as the input.
@@ -9725,10 +9825,11 @@
9825 */
9826 SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
9827
9828 /*
9829 ** CAPI3REF: Add A Changeset To A Changegroup
9830 ** METHOD: sqlite3_changegroup
9831 **
9832 ** Add all changes within the changeset (or patchset) in buffer pData (size
9833 ** nData bytes) to the changegroup.
9834 **
9835 ** If the buffer contains a patchset, then all prior calls to this function
@@ -9802,10 +9903,11 @@
9903 */
9904 SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
9905
9906 /*
9907 ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
9908 ** METHOD: sqlite3_changegroup
9909 **
9910 ** Obtain a buffer containing a changeset (or patchset) representing the
9911 ** current contents of the changegroup. If the inputs to the changegroup
9912 ** were themselves changesets, the output is a changeset. Or, if the
9913 ** inputs were patchsets, the output is also a patchset.
@@ -9832,10 +9934,11 @@
9934 void **ppData /* OUT: Pointer to output buffer */
9935 );
9936
9937 /*
9938 ** CAPI3REF: Delete A Changegroup Object
9939 ** DESTRUCTOR: sqlite3_changegroup
9940 */
9941 SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
9942
9943 /*
9944 ** CAPI3REF: Apply A Changeset To A Database
9945
--- win/Makefile.PellesCGMake
+++ win/Makefile.PellesCGMake
@@ -89,11 +89,11 @@
8989
9090
# define the SQLite shell files, which need special flags on compile
9191
SQLITESHELLSRC=shell.c
9292
ORIGSQLITESHELLSRC=$(foreach sf,$(SQLITESHELLSRC),$(SRCDIR)$(sf))
9393
SQLITESHELLOBJ=$(foreach sf,$(SQLITESHELLSRC),$(sf:.c=.obj))
94
-SQLITESHELLDEFINES=-Dmain=sqlite3_shell -DSQLITE_SHELL_IS_UTF8=1 -DSQLITE_OMIT_LOAD_EXTENSION=1 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) -DSQLITE_SHELL_DBNAME_PROC=fossil_open -Daccess=file_access -Dsystem=fossil_system -Dgetenv=fossil_getenv -Dfopen=fossil_fopen
94
+SQLITESHELLDEFINES=-DNDEBUG=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_OMIT_DECLTYPE -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_GET_TABLE -DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_USE_ALLOCA -DSQLITE_ENABLE_LOCKING_STYLE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_STMTVTAB -DSQLITE_USE_ZLIB -DSQLITE_INTROSPECTION_PRAGMAS -DSQLITE_ENABLE_DBPAGE_VTAB -Dmain=sqlite3_shell -DSQLITE_SHELL_IS_UTF8=1 -DSQLITE_OMIT_LOAD_EXTENSION=1 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) -DSQLITE_SHELL_DBNAME_PROC=fossil_open -Daccess=file_access -Dsystem=fossil_system -Dgetenv=fossil_getenv -Dfopen=fossil_fopen
9595
9696
# define the th scripting files, which need special flags on compile
9797
THSRC=th.c th_lang.c
9898
ORIGTHSRC=$(foreach sf,$(THSRC),$(SRCDIR)$(sf))
9999
THOBJ=$(foreach sf,$(THSRC),$(sf:.c=.obj))
100100
--- win/Makefile.PellesCGMake
+++ win/Makefile.PellesCGMake
@@ -89,11 +89,11 @@
89
90 # define the SQLite shell files, which need special flags on compile
91 SQLITESHELLSRC=shell.c
92 ORIGSQLITESHELLSRC=$(foreach sf,$(SQLITESHELLSRC),$(SRCDIR)$(sf))
93 SQLITESHELLOBJ=$(foreach sf,$(SQLITESHELLSRC),$(sf:.c=.obj))
94 SQLITESHELLDEFINES=-Dmain=sqlite3_shell -DSQLITE_SHELL_IS_UTF8=1 -DSQLITE_OMIT_LOAD_EXTENSION=1 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) -DSQLITE_SHELL_DBNAME_PROC=fossil_open -Daccess=file_access -Dsystem=fossil_system -Dgetenv=fossil_getenv -Dfopen=fossil_fopen
95
96 # define the th scripting files, which need special flags on compile
97 THSRC=th.c th_lang.c
98 ORIGTHSRC=$(foreach sf,$(THSRC),$(SRCDIR)$(sf))
99 THOBJ=$(foreach sf,$(THSRC),$(sf:.c=.obj))
100
--- win/Makefile.PellesCGMake
+++ win/Makefile.PellesCGMake
@@ -89,11 +89,11 @@
89
90 # define the SQLite shell files, which need special flags on compile
91 SQLITESHELLSRC=shell.c
92 ORIGSQLITESHELLSRC=$(foreach sf,$(SQLITESHELLSRC),$(SRCDIR)$(sf))
93 SQLITESHELLOBJ=$(foreach sf,$(SQLITESHELLSRC),$(sf:.c=.obj))
94 SQLITESHELLDEFINES=-DNDEBUG=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_OMIT_DECLTYPE -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_GET_TABLE -DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_USE_ALLOCA -DSQLITE_ENABLE_LOCKING_STYLE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_STMTVTAB -DSQLITE_USE_ZLIB -DSQLITE_INTROSPECTION_PRAGMAS -DSQLITE_ENABLE_DBPAGE_VTAB -Dmain=sqlite3_shell -DSQLITE_SHELL_IS_UTF8=1 -DSQLITE_OMIT_LOAD_EXTENSION=1 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) -DSQLITE_SHELL_DBNAME_PROC=fossil_open -Daccess=file_access -Dsystem=fossil_system -Dgetenv=fossil_getenv -Dfopen=fossil_fopen
95
96 # define the th scripting files, which need special flags on compile
97 THSRC=th.c th_lang.c
98 ORIGTHSRC=$(foreach sf,$(THSRC),$(SRCDIR)$(sf))
99 THOBJ=$(foreach sf,$(THSRC),$(sf:.c=.obj))
100
--- win/Makefile.dmc
+++ win/Makefile.dmc
@@ -26,11 +26,11 @@
2626
TCC = $(DMDIR)\bin\dmc $(CFLAGS) $(DMCDEF) $(SSL) $(INCL)
2727
LIBS = $(DMDIR)\extra\lib\ zlib wsock32 advapi32
2828
2929
SQLITE_OPTIONS = -DNDEBUG=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_OMIT_DECLTYPE -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_GET_TABLE -DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_USE_ALLOCA -DSQLITE_ENABLE_LOCKING_STYLE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_STMTVTAB -DSQLITE_USE_ZLIB -DSQLITE_INTROSPECTION_PRAGMAS -DSQLITE_ENABLE_DBPAGE_VTAB
3030
31
-SHELL_OPTIONS = -Dmain=sqlite3_shell -DSQLITE_SHELL_IS_UTF8=1 -DSQLITE_OMIT_LOAD_EXTENSION=1 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) -DSQLITE_SHELL_DBNAME_PROC=fossil_open -Daccess=file_access -Dsystem=fossil_system -Dgetenv=fossil_getenv -Dfopen=fossil_fopen
31
+SHELL_OPTIONS = -DNDEBUG=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_OMIT_DECLTYPE -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_GET_TABLE -DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_USE_ALLOCA -DSQLITE_ENABLE_LOCKING_STYLE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_STMTVTAB -DSQLITE_USE_ZLIB -DSQLITE_INTROSPECTION_PRAGMAS -DSQLITE_ENABLE_DBPAGE_VTAB -Dmain=sqlite3_shell -DSQLITE_SHELL_IS_UTF8=1 -DSQLITE_OMIT_LOAD_EXTENSION=1 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) -DSQLITE_SHELL_DBNAME_PROC=fossil_open -Daccess=file_access -Dsystem=fossil_system -Dgetenv=fossil_getenv -Dfopen=fossil_fopen
3232
3333
SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c builtin_.c bundle_.c cache_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c cookies_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c dispatch_.c doc_.c encode_.c etag_.c event_.c export_.c file_.c finfo_.c foci_.c fshell_.c fusefs_.c glob_.c graph_.c gzip_.c hname_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c json_.c json_artifact_.c json_branch_.c json_config_.c json_diff_.c json_dir_.c json_finfo_.c json_login_.c json_query_.c json_report_.c json_status_.c json_tag_.c json_timeline_.c json_user_.c json_wiki_.c leaf_.c loadctrl_.c login_.c lookslike_.c main_.c manifest_.c markdown_.c markdown_html_.c md5_.c merge_.c merge3_.c moderate_.c name_.c path_.c piechart_.c pivot_.c popen_.c pqueue_.c printf_.c publish_.c purge_.c rebuild_.c regexp_.c report_.c rss_.c schema_.c search_.c security_audit_.c setup_.c sha1_.c sha1hard_.c sha3_.c shun_.c sitemap_.c skins_.c sqlcmd_.c stash_.c stat_.c statrep_.c style_.c sync_.c tag_.c tar_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c unicode_.c unversioned_.c update_.c url_.c user_.c utf8_.c util_.c verify_.c vfile_.c wiki_.c wikiformat_.c winfile_.c winhttp_.c wysiwyg_.c xfer_.c xfersetup_.c zip_.c
3434
3535
OBJ = $(OBJDIR)\add$O $(OBJDIR)\allrepo$O $(OBJDIR)\attach$O $(OBJDIR)\bag$O $(OBJDIR)\bisect$O $(OBJDIR)\blob$O $(OBJDIR)\branch$O $(OBJDIR)\browse$O $(OBJDIR)\builtin$O $(OBJDIR)\bundle$O $(OBJDIR)\cache$O $(OBJDIR)\captcha$O $(OBJDIR)\cgi$O $(OBJDIR)\checkin$O $(OBJDIR)\checkout$O $(OBJDIR)\clearsign$O $(OBJDIR)\clone$O $(OBJDIR)\comformat$O $(OBJDIR)\configure$O $(OBJDIR)\content$O $(OBJDIR)\cookies$O $(OBJDIR)\db$O $(OBJDIR)\delta$O $(OBJDIR)\deltacmd$O $(OBJDIR)\descendants$O $(OBJDIR)\diff$O $(OBJDIR)\diffcmd$O $(OBJDIR)\dispatch$O $(OBJDIR)\doc$O $(OBJDIR)\encode$O $(OBJDIR)\etag$O $(OBJDIR)\event$O $(OBJDIR)\export$O $(OBJDIR)\file$O $(OBJDIR)\finfo$O $(OBJDIR)\foci$O $(OBJDIR)\fshell$O $(OBJDIR)\fusefs$O $(OBJDIR)\glob$O $(OBJDIR)\graph$O $(OBJDIR)\gzip$O $(OBJDIR)\hname$O $(OBJDIR)\http$O $(OBJDIR)\http_socket$O $(OBJDIR)\http_ssl$O $(OBJDIR)\http_transport$O $(OBJDIR)\import$O $(OBJDIR)\info$O $(OBJDIR)\json$O $(OBJDIR)\json_artifact$O $(OBJDIR)\json_branch$O $(OBJDIR)\json_config$O $(OBJDIR)\json_diff$O $(OBJDIR)\json_dir$O $(OBJDIR)\json_finfo$O $(OBJDIR)\json_login$O $(OBJDIR)\json_query$O $(OBJDIR)\json_report$O $(OBJDIR)\json_status$O $(OBJDIR)\json_tag$O $(OBJDIR)\json_timeline$O $(OBJDIR)\json_user$O $(OBJDIR)\json_wiki$O $(OBJDIR)\leaf$O $(OBJDIR)\loadctrl$O $(OBJDIR)\login$O $(OBJDIR)\lookslike$O $(OBJDIR)\main$O $(OBJDIR)\manifest$O $(OBJDIR)\markdown$O $(OBJDIR)\markdown_html$O $(OBJDIR)\md5$O $(OBJDIR)\merge$O $(OBJDIR)\merge3$O $(OBJDIR)\moderate$O $(OBJDIR)\name$O $(OBJDIR)\path$O $(OBJDIR)\piechart$O $(OBJDIR)\pivot$O $(OBJDIR)\popen$O $(OBJDIR)\pqueue$O $(OBJDIR)\printf$O $(OBJDIR)\publish$O $(OBJDIR)\purge$O $(OBJDIR)\rebuild$O $(OBJDIR)\regexp$O $(OBJDIR)\report$O $(OBJDIR)\rss$O $(OBJDIR)\schema$O $(OBJDIR)\search$O $(OBJDIR)\security_audit$O $(OBJDIR)\setup$O $(OBJDIR)\sha1$O $(OBJDIR)\sha1hard$O $(OBJDIR)\sha3$O $(OBJDIR)\shun$O $(OBJDIR)\sitemap$O $(OBJDIR)\skins$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\stash$O $(OBJDIR)\stat$O $(OBJDIR)\statrep$O $(OBJDIR)\style$O $(OBJDIR)\sync$O $(OBJDIR)\tag$O $(OBJDIR)\tar$O $(OBJDIR)\th_main$O $(OBJDIR)\timeline$O $(OBJDIR)\tkt$O $(OBJDIR)\tktsetup$O $(OBJDIR)\undo$O $(OBJDIR)\unicode$O $(OBJDIR)\unversioned$O $(OBJDIR)\update$O $(OBJDIR)\url$O $(OBJDIR)\user$O $(OBJDIR)\utf8$O $(OBJDIR)\util$O $(OBJDIR)\verify$O $(OBJDIR)\vfile$O $(OBJDIR)\wiki$O $(OBJDIR)\wikiformat$O $(OBJDIR)\winfile$O $(OBJDIR)\winhttp$O $(OBJDIR)\wysiwyg$O $(OBJDIR)\xfer$O $(OBJDIR)\xfersetup$O $(OBJDIR)\zip$O $(OBJDIR)\shell$O $(OBJDIR)\sqlite3$O $(OBJDIR)\th$O $(OBJDIR)\th_lang$O
3636
3737
--- win/Makefile.dmc
+++ win/Makefile.dmc
@@ -26,11 +26,11 @@
26 TCC = $(DMDIR)\bin\dmc $(CFLAGS) $(DMCDEF) $(SSL) $(INCL)
27 LIBS = $(DMDIR)\extra\lib\ zlib wsock32 advapi32
28
29 SQLITE_OPTIONS = -DNDEBUG=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_OMIT_DECLTYPE -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_GET_TABLE -DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_USE_ALLOCA -DSQLITE_ENABLE_LOCKING_STYLE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_STMTVTAB -DSQLITE_USE_ZLIB -DSQLITE_INTROSPECTION_PRAGMAS -DSQLITE_ENABLE_DBPAGE_VTAB
30
31 SHELL_OPTIONS = -Dmain=sqlite3_shell -DSQLITE_SHELL_IS_UTF8=1 -DSQLITE_OMIT_LOAD_EXTENSION=1 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) -DSQLITE_SHELL_DBNAME_PROC=fossil_open -Daccess=file_access -Dsystem=fossil_system -Dgetenv=fossil_getenv -Dfopen=fossil_fopen
32
33 SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c builtin_.c bundle_.c cache_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c cookies_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c dispatch_.c doc_.c encode_.c etag_.c event_.c export_.c file_.c finfo_.c foci_.c fshell_.c fusefs_.c glob_.c graph_.c gzip_.c hname_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c json_.c json_artifact_.c json_branch_.c json_config_.c json_diff_.c json_dir_.c json_finfo_.c json_login_.c json_query_.c json_report_.c json_status_.c json_tag_.c json_timeline_.c json_user_.c json_wiki_.c leaf_.c loadctrl_.c login_.c lookslike_.c main_.c manifest_.c markdown_.c markdown_html_.c md5_.c merge_.c merge3_.c moderate_.c name_.c path_.c piechart_.c pivot_.c popen_.c pqueue_.c printf_.c publish_.c purge_.c rebuild_.c regexp_.c report_.c rss_.c schema_.c search_.c security_audit_.c setup_.c sha1_.c sha1hard_.c sha3_.c shun_.c sitemap_.c skins_.c sqlcmd_.c stash_.c stat_.c statrep_.c style_.c sync_.c tag_.c tar_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c unicode_.c unversioned_.c update_.c url_.c user_.c utf8_.c util_.c verify_.c vfile_.c wiki_.c wikiformat_.c winfile_.c winhttp_.c wysiwyg_.c xfer_.c xfersetup_.c zip_.c
34
35 OBJ = $(OBJDIR)\add$O $(OBJDIR)\allrepo$O $(OBJDIR)\attach$O $(OBJDIR)\bag$O $(OBJDIR)\bisect$O $(OBJDIR)\blob$O $(OBJDIR)\branch$O $(OBJDIR)\browse$O $(OBJDIR)\builtin$O $(OBJDIR)\bundle$O $(OBJDIR)\cache$O $(OBJDIR)\captcha$O $(OBJDIR)\cgi$O $(OBJDIR)\checkin$O $(OBJDIR)\checkout$O $(OBJDIR)\clearsign$O $(OBJDIR)\clone$O $(OBJDIR)\comformat$O $(OBJDIR)\configure$O $(OBJDIR)\content$O $(OBJDIR)\cookies$O $(OBJDIR)\db$O $(OBJDIR)\delta$O $(OBJDIR)\deltacmd$O $(OBJDIR)\descendants$O $(OBJDIR)\diff$O $(OBJDIR)\diffcmd$O $(OBJDIR)\dispatch$O $(OBJDIR)\doc$O $(OBJDIR)\encode$O $(OBJDIR)\etag$O $(OBJDIR)\event$O $(OBJDIR)\export$O $(OBJDIR)\file$O $(OBJDIR)\finfo$O $(OBJDIR)\foci$O $(OBJDIR)\fshell$O $(OBJDIR)\fusefs$O $(OBJDIR)\glob$O $(OBJDIR)\graph$O $(OBJDIR)\gzip$O $(OBJDIR)\hname$O $(OBJDIR)\http$O $(OBJDIR)\http_socket$O $(OBJDIR)\http_ssl$O $(OBJDIR)\http_transport$O $(OBJDIR)\import$O $(OBJDIR)\info$O $(OBJDIR)\json$O $(OBJDIR)\json_artifact$O $(OBJDIR)\json_branch$O $(OBJDIR)\json_config$O $(OBJDIR)\json_diff$O $(OBJDIR)\json_dir$O $(OBJDIR)\json_finfo$O $(OBJDIR)\json_login$O $(OBJDIR)\json_query$O $(OBJDIR)\json_report$O $(OBJDIR)\json_status$O $(OBJDIR)\json_tag$O $(OBJDIR)\json_timeline$O $(OBJDIR)\json_user$O $(OBJDIR)\json_wiki$O $(OBJDIR)\leaf$O $(OBJDIR)\loadctrl$O $(OBJDIR)\login$O $(OBJDIR)\lookslike$O $(OBJDIR)\main$O $(OBJDIR)\manifest$O $(OBJDIR)\markdown$O $(OBJDIR)\markdown_html$O $(OBJDIR)\md5$O $(OBJDIR)\merge$O $(OBJDIR)\merge3$O $(OBJDIR)\moderate$O $(OBJDIR)\name$O $(OBJDIR)\path$O $(OBJDIR)\piechart$O $(OBJDIR)\pivot$O $(OBJDIR)\popen$O $(OBJDIR)\pqueue$O $(OBJDIR)\printf$O $(OBJDIR)\publish$O $(OBJDIR)\purge$O $(OBJDIR)\rebuild$O $(OBJDIR)\regexp$O $(OBJDIR)\report$O $(OBJDIR)\rss$O $(OBJDIR)\schema$O $(OBJDIR)\search$O $(OBJDIR)\security_audit$O $(OBJDIR)\setup$O $(OBJDIR)\sha1$O $(OBJDIR)\sha1hard$O $(OBJDIR)\sha3$O $(OBJDIR)\shun$O $(OBJDIR)\sitemap$O $(OBJDIR)\skins$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\stash$O $(OBJDIR)\stat$O $(OBJDIR)\statrep$O $(OBJDIR)\style$O $(OBJDIR)\sync$O $(OBJDIR)\tag$O $(OBJDIR)\tar$O $(OBJDIR)\th_main$O $(OBJDIR)\timeline$O $(OBJDIR)\tkt$O $(OBJDIR)\tktsetup$O $(OBJDIR)\undo$O $(OBJDIR)\unicode$O $(OBJDIR)\unversioned$O $(OBJDIR)\update$O $(OBJDIR)\url$O $(OBJDIR)\user$O $(OBJDIR)\utf8$O $(OBJDIR)\util$O $(OBJDIR)\verify$O $(OBJDIR)\vfile$O $(OBJDIR)\wiki$O $(OBJDIR)\wikiformat$O $(OBJDIR)\winfile$O $(OBJDIR)\winhttp$O $(OBJDIR)\wysiwyg$O $(OBJDIR)\xfer$O $(OBJDIR)\xfersetup$O $(OBJDIR)\zip$O $(OBJDIR)\shell$O $(OBJDIR)\sqlite3$O $(OBJDIR)\th$O $(OBJDIR)\th_lang$O
36
37
--- win/Makefile.dmc
+++ win/Makefile.dmc
@@ -26,11 +26,11 @@
26 TCC = $(DMDIR)\bin\dmc $(CFLAGS) $(DMCDEF) $(SSL) $(INCL)
27 LIBS = $(DMDIR)\extra\lib\ zlib wsock32 advapi32
28
29 SQLITE_OPTIONS = -DNDEBUG=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_OMIT_DECLTYPE -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_GET_TABLE -DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_USE_ALLOCA -DSQLITE_ENABLE_LOCKING_STYLE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_STMTVTAB -DSQLITE_USE_ZLIB -DSQLITE_INTROSPECTION_PRAGMAS -DSQLITE_ENABLE_DBPAGE_VTAB
30
31 SHELL_OPTIONS = -DNDEBUG=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_OMIT_DECLTYPE -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_GET_TABLE -DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_MAX_EXPR_DEPTH=0 -DSQLITE_USE_ALLOCA -DSQLITE_ENABLE_LOCKING_STYLE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_STMTVTAB -DSQLITE_USE_ZLIB -DSQLITE_INTROSPECTION_PRAGMAS -DSQLITE_ENABLE_DBPAGE_VTAB -Dmain=sqlite3_shell -DSQLITE_SHELL_IS_UTF8=1 -DSQLITE_OMIT_LOAD_EXTENSION=1 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) -DSQLITE_SHELL_DBNAME_PROC=fossil_open -Daccess=file_access -Dsystem=fossil_system -Dgetenv=fossil_getenv -Dfopen=fossil_fopen
32
33 SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c builtin_.c bundle_.c cache_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c cookies_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c dispatch_.c doc_.c encode_.c etag_.c event_.c export_.c file_.c finfo_.c foci_.c fshell_.c fusefs_.c glob_.c graph_.c gzip_.c hname_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c json_.c json_artifact_.c json_branch_.c json_config_.c json_diff_.c json_dir_.c json_finfo_.c json_login_.c json_query_.c json_report_.c json_status_.c json_tag_.c json_timeline_.c json_user_.c json_wiki_.c leaf_.c loadctrl_.c login_.c lookslike_.c main_.c manifest_.c markdown_.c markdown_html_.c md5_.c merge_.c merge3_.c moderate_.c name_.c path_.c piechart_.c pivot_.c popen_.c pqueue_.c printf_.c publish_.c purge_.c rebuild_.c regexp_.c report_.c rss_.c schema_.c search_.c security_audit_.c setup_.c sha1_.c sha1hard_.c sha3_.c shun_.c sitemap_.c skins_.c sqlcmd_.c stash_.c stat_.c statrep_.c style_.c sync_.c tag_.c tar_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c unicode_.c unversioned_.c update_.c url_.c user_.c utf8_.c util_.c verify_.c vfile_.c wiki_.c wikiformat_.c winfile_.c winhttp_.c wysiwyg_.c xfer_.c xfersetup_.c zip_.c
34
35 OBJ = $(OBJDIR)\add$O $(OBJDIR)\allrepo$O $(OBJDIR)\attach$O $(OBJDIR)\bag$O $(OBJDIR)\bisect$O $(OBJDIR)\blob$O $(OBJDIR)\branch$O $(OBJDIR)\browse$O $(OBJDIR)\builtin$O $(OBJDIR)\bundle$O $(OBJDIR)\cache$O $(OBJDIR)\captcha$O $(OBJDIR)\cgi$O $(OBJDIR)\checkin$O $(OBJDIR)\checkout$O $(OBJDIR)\clearsign$O $(OBJDIR)\clone$O $(OBJDIR)\comformat$O $(OBJDIR)\configure$O $(OBJDIR)\content$O $(OBJDIR)\cookies$O $(OBJDIR)\db$O $(OBJDIR)\delta$O $(OBJDIR)\deltacmd$O $(OBJDIR)\descendants$O $(OBJDIR)\diff$O $(OBJDIR)\diffcmd$O $(OBJDIR)\dispatch$O $(OBJDIR)\doc$O $(OBJDIR)\encode$O $(OBJDIR)\etag$O $(OBJDIR)\event$O $(OBJDIR)\export$O $(OBJDIR)\file$O $(OBJDIR)\finfo$O $(OBJDIR)\foci$O $(OBJDIR)\fshell$O $(OBJDIR)\fusefs$O $(OBJDIR)\glob$O $(OBJDIR)\graph$O $(OBJDIR)\gzip$O $(OBJDIR)\hname$O $(OBJDIR)\http$O $(OBJDIR)\http_socket$O $(OBJDIR)\http_ssl$O $(OBJDIR)\http_transport$O $(OBJDIR)\import$O $(OBJDIR)\info$O $(OBJDIR)\json$O $(OBJDIR)\json_artifact$O $(OBJDIR)\json_branch$O $(OBJDIR)\json_config$O $(OBJDIR)\json_diff$O $(OBJDIR)\json_dir$O $(OBJDIR)\json_finfo$O $(OBJDIR)\json_login$O $(OBJDIR)\json_query$O $(OBJDIR)\json_report$O $(OBJDIR)\json_status$O $(OBJDIR)\json_tag$O $(OBJDIR)\json_timeline$O $(OBJDIR)\json_user$O $(OBJDIR)\json_wiki$O $(OBJDIR)\leaf$O $(OBJDIR)\loadctrl$O $(OBJDIR)\login$O $(OBJDIR)\lookslike$O $(OBJDIR)\main$O $(OBJDIR)\manifest$O $(OBJDIR)\markdown$O $(OBJDIR)\markdown_html$O $(OBJDIR)\md5$O $(OBJDIR)\merge$O $(OBJDIR)\merge3$O $(OBJDIR)\moderate$O $(OBJDIR)\name$O $(OBJDIR)\path$O $(OBJDIR)\piechart$O $(OBJDIR)\pivot$O $(OBJDIR)\popen$O $(OBJDIR)\pqueue$O $(OBJDIR)\printf$O $(OBJDIR)\publish$O $(OBJDIR)\purge$O $(OBJDIR)\rebuild$O $(OBJDIR)\regexp$O $(OBJDIR)\report$O $(OBJDIR)\rss$O $(OBJDIR)\schema$O $(OBJDIR)\search$O $(OBJDIR)\security_audit$O $(OBJDIR)\setup$O $(OBJDIR)\sha1$O $(OBJDIR)\sha1hard$O $(OBJDIR)\sha3$O $(OBJDIR)\shun$O $(OBJDIR)\sitemap$O $(OBJDIR)\skins$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\stash$O $(OBJDIR)\stat$O $(OBJDIR)\statrep$O $(OBJDIR)\style$O $(OBJDIR)\sync$O $(OBJDIR)\tag$O $(OBJDIR)\tar$O $(OBJDIR)\th_main$O $(OBJDIR)\timeline$O $(OBJDIR)\tkt$O $(OBJDIR)\tktsetup$O $(OBJDIR)\undo$O $(OBJDIR)\unicode$O $(OBJDIR)\unversioned$O $(OBJDIR)\update$O $(OBJDIR)\url$O $(OBJDIR)\user$O $(OBJDIR)\utf8$O $(OBJDIR)\util$O $(OBJDIR)\verify$O $(OBJDIR)\vfile$O $(OBJDIR)\wiki$O $(OBJDIR)\wikiformat$O $(OBJDIR)\winfile$O $(OBJDIR)\winhttp$O $(OBJDIR)\wysiwyg$O $(OBJDIR)\xfer$O $(OBJDIR)\xfersetup$O $(OBJDIR)\zip$O $(OBJDIR)\shell$O $(OBJDIR)\sqlite3$O $(OBJDIR)\th$O $(OBJDIR)\th_lang$O
36
37
--- win/Makefile.mingw
+++ win/Makefile.mingw
@@ -2295,11 +2295,36 @@
22952295
-DSQLITE_WIN32_NO_ANSI \
22962296
$(MINGW_OPTIONS) \
22972297
-DSQLITE_USE_MALLOC_H \
22982298
-DSQLITE_USE_MSIZE
22992299
2300
-SHELL_OPTIONS = -Dmain=sqlite3_shell \
2300
+SHELL_OPTIONS = -DNDEBUG=1 \
2301
+ -DSQLITE_THREADSAFE=0 \
2302
+ -DSQLITE_DEFAULT_MEMSTATUS=0 \
2303
+ -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
2304
+ -DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
2305
+ -DSQLITE_OMIT_DECLTYPE \
2306
+ -DSQLITE_OMIT_DEPRECATED \
2307
+ -DSQLITE_OMIT_GET_TABLE \
2308
+ -DSQLITE_OMIT_PROGRESS_CALLBACK \
2309
+ -DSQLITE_OMIT_SHARED_CACHE \
2310
+ -DSQLITE_OMIT_LOAD_EXTENSION \
2311
+ -DSQLITE_MAX_EXPR_DEPTH=0 \
2312
+ -DSQLITE_USE_ALLOCA \
2313
+ -DSQLITE_ENABLE_LOCKING_STYLE=0 \
2314
+ -DSQLITE_DEFAULT_FILE_FORMAT=4 \
2315
+ -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
2316
+ -DSQLITE_ENABLE_FTS4 \
2317
+ -DSQLITE_ENABLE_FTS3_PARENTHESIS \
2318
+ -DSQLITE_ENABLE_DBSTAT_VTAB \
2319
+ -DSQLITE_ENABLE_JSON1 \
2320
+ -DSQLITE_ENABLE_FTS5 \
2321
+ -DSQLITE_ENABLE_STMTVTAB \
2322
+ -DSQLITE_USE_ZLIB \
2323
+ -DSQLITE_INTROSPECTION_PRAGMAS \
2324
+ -DSQLITE_ENABLE_DBPAGE_VTAB \
2325
+ -Dmain=sqlite3_shell \
23012326
-DSQLITE_SHELL_IS_UTF8=1 \
23022327
-DSQLITE_OMIT_LOAD_EXTENSION=1 \
23032328
-DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) \
23042329
-DSQLITE_SHELL_DBNAME_PROC=fossil_open \
23052330
-Daccess=file_access \
23062331
--- win/Makefile.mingw
+++ win/Makefile.mingw
@@ -2295,11 +2295,36 @@
2295 -DSQLITE_WIN32_NO_ANSI \
2296 $(MINGW_OPTIONS) \
2297 -DSQLITE_USE_MALLOC_H \
2298 -DSQLITE_USE_MSIZE
2299
2300 SHELL_OPTIONS = -Dmain=sqlite3_shell \
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2301 -DSQLITE_SHELL_IS_UTF8=1 \
2302 -DSQLITE_OMIT_LOAD_EXTENSION=1 \
2303 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) \
2304 -DSQLITE_SHELL_DBNAME_PROC=fossil_open \
2305 -Daccess=file_access \
2306
--- win/Makefile.mingw
+++ win/Makefile.mingw
@@ -2295,11 +2295,36 @@
2295 -DSQLITE_WIN32_NO_ANSI \
2296 $(MINGW_OPTIONS) \
2297 -DSQLITE_USE_MALLOC_H \
2298 -DSQLITE_USE_MSIZE
2299
2300 SHELL_OPTIONS = -DNDEBUG=1 \
2301 -DSQLITE_THREADSAFE=0 \
2302 -DSQLITE_DEFAULT_MEMSTATUS=0 \
2303 -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
2304 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
2305 -DSQLITE_OMIT_DECLTYPE \
2306 -DSQLITE_OMIT_DEPRECATED \
2307 -DSQLITE_OMIT_GET_TABLE \
2308 -DSQLITE_OMIT_PROGRESS_CALLBACK \
2309 -DSQLITE_OMIT_SHARED_CACHE \
2310 -DSQLITE_OMIT_LOAD_EXTENSION \
2311 -DSQLITE_MAX_EXPR_DEPTH=0 \
2312 -DSQLITE_USE_ALLOCA \
2313 -DSQLITE_ENABLE_LOCKING_STYLE=0 \
2314 -DSQLITE_DEFAULT_FILE_FORMAT=4 \
2315 -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
2316 -DSQLITE_ENABLE_FTS4 \
2317 -DSQLITE_ENABLE_FTS3_PARENTHESIS \
2318 -DSQLITE_ENABLE_DBSTAT_VTAB \
2319 -DSQLITE_ENABLE_JSON1 \
2320 -DSQLITE_ENABLE_FTS5 \
2321 -DSQLITE_ENABLE_STMTVTAB \
2322 -DSQLITE_USE_ZLIB \
2323 -DSQLITE_INTROSPECTION_PRAGMAS \
2324 -DSQLITE_ENABLE_DBPAGE_VTAB \
2325 -Dmain=sqlite3_shell \
2326 -DSQLITE_SHELL_IS_UTF8=1 \
2327 -DSQLITE_OMIT_LOAD_EXTENSION=1 \
2328 -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) \
2329 -DSQLITE_SHELL_DBNAME_PROC=fossil_open \
2330 -Daccess=file_access \
2331
+26 -1
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -338,11 +338,36 @@
338338
/DSQLITE_USE_ZLIB \
339339
/DSQLITE_INTROSPECTION_PRAGMAS \
340340
/DSQLITE_ENABLE_DBPAGE_VTAB \
341341
/DSQLITE_WIN32_NO_ANSI
342342
343
-SHELL_OPTIONS = /Dmain=sqlite3_shell \
343
+SHELL_OPTIONS = /DNDEBUG=1 \
344
+ /DSQLITE_THREADSAFE=0 \
345
+ /DSQLITE_DEFAULT_MEMSTATUS=0 \
346
+ /DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
347
+ /DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
348
+ /DSQLITE_OMIT_DECLTYPE \
349
+ /DSQLITE_OMIT_DEPRECATED \
350
+ /DSQLITE_OMIT_GET_TABLE \
351
+ /DSQLITE_OMIT_PROGRESS_CALLBACK \
352
+ /DSQLITE_OMIT_SHARED_CACHE \
353
+ /DSQLITE_OMIT_LOAD_EXTENSION \
354
+ /DSQLITE_MAX_EXPR_DEPTH=0 \
355
+ /DSQLITE_USE_ALLOCA \
356
+ /DSQLITE_ENABLE_LOCKING_STYLE=0 \
357
+ /DSQLITE_DEFAULT_FILE_FORMAT=4 \
358
+ /DSQLITE_ENABLE_EXPLAIN_COMMENTS \
359
+ /DSQLITE_ENABLE_FTS4 \
360
+ /DSQLITE_ENABLE_FTS3_PARENTHESIS \
361
+ /DSQLITE_ENABLE_DBSTAT_VTAB \
362
+ /DSQLITE_ENABLE_JSON1 \
363
+ /DSQLITE_ENABLE_FTS5 \
364
+ /DSQLITE_ENABLE_STMTVTAB \
365
+ /DSQLITE_USE_ZLIB \
366
+ /DSQLITE_INTROSPECTION_PRAGMAS \
367
+ /DSQLITE_ENABLE_DBPAGE_VTAB \
368
+ /Dmain=sqlite3_shell \
344369
/DSQLITE_SHELL_IS_UTF8=1 \
345370
/DSQLITE_OMIT_LOAD_EXTENSION=1 \
346371
/DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) \
347372
/DSQLITE_SHELL_DBNAME_PROC=fossil_open \
348373
/Daccess=file_access \
349374
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -338,11 +338,36 @@
338 /DSQLITE_USE_ZLIB \
339 /DSQLITE_INTROSPECTION_PRAGMAS \
340 /DSQLITE_ENABLE_DBPAGE_VTAB \
341 /DSQLITE_WIN32_NO_ANSI
342
343 SHELL_OPTIONS = /Dmain=sqlite3_shell \
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
344 /DSQLITE_SHELL_IS_UTF8=1 \
345 /DSQLITE_OMIT_LOAD_EXTENSION=1 \
346 /DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) \
347 /DSQLITE_SHELL_DBNAME_PROC=fossil_open \
348 /Daccess=file_access \
349
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -338,11 +338,36 @@
338 /DSQLITE_USE_ZLIB \
339 /DSQLITE_INTROSPECTION_PRAGMAS \
340 /DSQLITE_ENABLE_DBPAGE_VTAB \
341 /DSQLITE_WIN32_NO_ANSI
342
343 SHELL_OPTIONS = /DNDEBUG=1 \
344 /DSQLITE_THREADSAFE=0 \
345 /DSQLITE_DEFAULT_MEMSTATUS=0 \
346 /DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
347 /DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
348 /DSQLITE_OMIT_DECLTYPE \
349 /DSQLITE_OMIT_DEPRECATED \
350 /DSQLITE_OMIT_GET_TABLE \
351 /DSQLITE_OMIT_PROGRESS_CALLBACK \
352 /DSQLITE_OMIT_SHARED_CACHE \
353 /DSQLITE_OMIT_LOAD_EXTENSION \
354 /DSQLITE_MAX_EXPR_DEPTH=0 \
355 /DSQLITE_USE_ALLOCA \
356 /DSQLITE_ENABLE_LOCKING_STYLE=0 \
357 /DSQLITE_DEFAULT_FILE_FORMAT=4 \
358 /DSQLITE_ENABLE_EXPLAIN_COMMENTS \
359 /DSQLITE_ENABLE_FTS4 \
360 /DSQLITE_ENABLE_FTS3_PARENTHESIS \
361 /DSQLITE_ENABLE_DBSTAT_VTAB \
362 /DSQLITE_ENABLE_JSON1 \
363 /DSQLITE_ENABLE_FTS5 \
364 /DSQLITE_ENABLE_STMTVTAB \
365 /DSQLITE_USE_ZLIB \
366 /DSQLITE_INTROSPECTION_PRAGMAS \
367 /DSQLITE_ENABLE_DBPAGE_VTAB \
368 /Dmain=sqlite3_shell \
369 /DSQLITE_SHELL_IS_UTF8=1 \
370 /DSQLITE_OMIT_LOAD_EXTENSION=1 \
371 /DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) \
372 /DSQLITE_SHELL_DBNAME_PROC=fossil_open \
373 /Daccess=file_access \
374

Keyboard Shortcuts

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