Fossil SCM

Update the built-in SQLite to 3.8.9 alpha.

drh 2015-03-24 18:22 trunk
Commit a00cdb182c1f7bebcbc4a20d1a2c53029c00e2f9
3 files changed +8 -8 +834 -253 +37 -17
+8 -8
--- src/shell.c
+++ src/shell.c
@@ -25,11 +25,11 @@
2525
#endif
2626
2727
/*
2828
** No support for loadable extensions in VxWorks.
2929
*/
30
-#if defined(_WRS_KERNEL) && !SQLITE_OMIT_LOAD_EXTENSION
30
+#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
3131
# define SQLITE_OMIT_LOAD_EXTENSION 1
3232
#endif
3333
3434
/*
3535
** Enable large-file support for fopen() and friends on unix.
@@ -2141,11 +2141,11 @@
21412141
** + Keep track of the line number in p->nLine.
21422142
** + Store the character that terminates the field in p->cTerm. Store
21432143
** EOF on end-of-file.
21442144
** + Report syntax errors on stderr
21452145
*/
2146
-static char *csv_read_one_field(ImportCtx *p){
2146
+static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
21472147
int c;
21482148
int cSep = p->cColSep;
21492149
int rSep = p->cRowSep;
21502150
p->n = 0;
21512151
c = fgetc(p->in);
@@ -2215,11 +2215,11 @@
22152215
** + Keep track of the row number in p->nLine.
22162216
** + Store the character that terminates the field in p->cTerm. Store
22172217
** EOF on end-of-file.
22182218
** + Report syntax errors on stderr
22192219
*/
2220
-static char *ascii_read_one_field(ImportCtx *p){
2220
+static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
22212221
int c;
22222222
int cSep = p->cColSep;
22232223
int rSep = p->cRowSep;
22242224
p->n = 0;
22252225
c = fgetc(p->in);
@@ -2909,12 +2909,12 @@
29092909
int i, j; /* Loop counters */
29102910
int needCommit; /* True to COMMIT or ROLLBACK at end */
29112911
int nSep; /* Number of bytes in p->colSeparator[] */
29122912
char *zSql; /* An SQL statement */
29132913
ImportCtx sCtx; /* Reader context */
2914
- char *(*xRead)(ImportCtx*); /* Procedure to read one value */
2915
- int (*xCloser)(FILE*); /* Procedure to close th3 connection */
2914
+ char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
2915
+ int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
29162916
29172917
if( nArg!=3 ){
29182918
fprintf(stderr, "Usage: .import FILE TABLE\n");
29192919
goto meta_command_exit;
29202920
}
@@ -2953,11 +2953,11 @@
29532953
}
29542954
sCtx.zFile = zFile;
29552955
sCtx.nLine = 1;
29562956
if( sCtx.zFile[0]=='|' ){
29572957
#ifdef SQLITE_OMIT_POPEN
2958
- fprintf(stderr, "Error: pipes are not supporte in this OS\n");
2958
+ fprintf(stderr, "Error: pipes are not supported in this OS\n");
29592959
return 1;
29602960
#else
29612961
sCtx.in = popen(sCtx.zFile+1, "r");
29622962
sCtx.zFile = "<pipe>";
29632963
xCloser = pclose;
@@ -3839,16 +3839,16 @@
38393839
}
38403840
}else
38413841
38423842
if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
38433843
open_db(p, 0);
3844
- output_file_close(p->traceOut);
38453844
if( nArg!=2 ){
38463845
fprintf(stderr, "Usage: .trace FILE|off\n");
38473846
rc = 1;
38483847
goto meta_command_exit;
38493848
}
3849
+ output_file_close(p->traceOut);
38503850
p->traceOut = output_file_open(azArg[1]);
38513851
#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
38523852
if( p->traceOut==0 ){
38533853
sqlite3_trace(p->db, 0, 0);
38543854
}else{
@@ -4352,11 +4352,11 @@
43524352
exit(1);
43534353
}
43544354
return argv[i];
43554355
}
43564356
4357
-int main(int argc, char **argv){
4357
+int SQLITE_CDECL main(int argc, char **argv){
43584358
char *zErrMsg = 0;
43594359
ShellState data;
43604360
const char *zInitFile = 0;
43614361
int i;
43624362
int rc = 0;
43634363
--- src/shell.c
+++ src/shell.c
@@ -25,11 +25,11 @@
25 #endif
26
27 /*
28 ** No support for loadable extensions in VxWorks.
29 */
30 #if defined(_WRS_KERNEL) && !SQLITE_OMIT_LOAD_EXTENSION
31 # define SQLITE_OMIT_LOAD_EXTENSION 1
32 #endif
33
34 /*
35 ** Enable large-file support for fopen() and friends on unix.
@@ -2141,11 +2141,11 @@
2141 ** + Keep track of the line number in p->nLine.
2142 ** + Store the character that terminates the field in p->cTerm. Store
2143 ** EOF on end-of-file.
2144 ** + Report syntax errors on stderr
2145 */
2146 static char *csv_read_one_field(ImportCtx *p){
2147 int c;
2148 int cSep = p->cColSep;
2149 int rSep = p->cRowSep;
2150 p->n = 0;
2151 c = fgetc(p->in);
@@ -2215,11 +2215,11 @@
2215 ** + Keep track of the row number in p->nLine.
2216 ** + Store the character that terminates the field in p->cTerm. Store
2217 ** EOF on end-of-file.
2218 ** + Report syntax errors on stderr
2219 */
2220 static char *ascii_read_one_field(ImportCtx *p){
2221 int c;
2222 int cSep = p->cColSep;
2223 int rSep = p->cRowSep;
2224 p->n = 0;
2225 c = fgetc(p->in);
@@ -2909,12 +2909,12 @@
2909 int i, j; /* Loop counters */
2910 int needCommit; /* True to COMMIT or ROLLBACK at end */
2911 int nSep; /* Number of bytes in p->colSeparator[] */
2912 char *zSql; /* An SQL statement */
2913 ImportCtx sCtx; /* Reader context */
2914 char *(*xRead)(ImportCtx*); /* Procedure to read one value */
2915 int (*xCloser)(FILE*); /* Procedure to close th3 connection */
2916
2917 if( nArg!=3 ){
2918 fprintf(stderr, "Usage: .import FILE TABLE\n");
2919 goto meta_command_exit;
2920 }
@@ -2953,11 +2953,11 @@
2953 }
2954 sCtx.zFile = zFile;
2955 sCtx.nLine = 1;
2956 if( sCtx.zFile[0]=='|' ){
2957 #ifdef SQLITE_OMIT_POPEN
2958 fprintf(stderr, "Error: pipes are not supporte in this OS\n");
2959 return 1;
2960 #else
2961 sCtx.in = popen(sCtx.zFile+1, "r");
2962 sCtx.zFile = "<pipe>";
2963 xCloser = pclose;
@@ -3839,16 +3839,16 @@
3839 }
3840 }else
3841
3842 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
3843 open_db(p, 0);
3844 output_file_close(p->traceOut);
3845 if( nArg!=2 ){
3846 fprintf(stderr, "Usage: .trace FILE|off\n");
3847 rc = 1;
3848 goto meta_command_exit;
3849 }
 
3850 p->traceOut = output_file_open(azArg[1]);
3851 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
3852 if( p->traceOut==0 ){
3853 sqlite3_trace(p->db, 0, 0);
3854 }else{
@@ -4352,11 +4352,11 @@
4352 exit(1);
4353 }
4354 return argv[i];
4355 }
4356
4357 int main(int argc, char **argv){
4358 char *zErrMsg = 0;
4359 ShellState data;
4360 const char *zInitFile = 0;
4361 int i;
4362 int rc = 0;
4363
--- src/shell.c
+++ src/shell.c
@@ -25,11 +25,11 @@
25 #endif
26
27 /*
28 ** No support for loadable extensions in VxWorks.
29 */
30 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
31 # define SQLITE_OMIT_LOAD_EXTENSION 1
32 #endif
33
34 /*
35 ** Enable large-file support for fopen() and friends on unix.
@@ -2141,11 +2141,11 @@
2141 ** + Keep track of the line number in p->nLine.
2142 ** + Store the character that terminates the field in p->cTerm. Store
2143 ** EOF on end-of-file.
2144 ** + Report syntax errors on stderr
2145 */
2146 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
2147 int c;
2148 int cSep = p->cColSep;
2149 int rSep = p->cRowSep;
2150 p->n = 0;
2151 c = fgetc(p->in);
@@ -2215,11 +2215,11 @@
2215 ** + Keep track of the row number in p->nLine.
2216 ** + Store the character that terminates the field in p->cTerm. Store
2217 ** EOF on end-of-file.
2218 ** + Report syntax errors on stderr
2219 */
2220 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
2221 int c;
2222 int cSep = p->cColSep;
2223 int rSep = p->cRowSep;
2224 p->n = 0;
2225 c = fgetc(p->in);
@@ -2909,12 +2909,12 @@
2909 int i, j; /* Loop counters */
2910 int needCommit; /* True to COMMIT or ROLLBACK at end */
2911 int nSep; /* Number of bytes in p->colSeparator[] */
2912 char *zSql; /* An SQL statement */
2913 ImportCtx sCtx; /* Reader context */
2914 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
2915 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
2916
2917 if( nArg!=3 ){
2918 fprintf(stderr, "Usage: .import FILE TABLE\n");
2919 goto meta_command_exit;
2920 }
@@ -2953,11 +2953,11 @@
2953 }
2954 sCtx.zFile = zFile;
2955 sCtx.nLine = 1;
2956 if( sCtx.zFile[0]=='|' ){
2957 #ifdef SQLITE_OMIT_POPEN
2958 fprintf(stderr, "Error: pipes are not supported in this OS\n");
2959 return 1;
2960 #else
2961 sCtx.in = popen(sCtx.zFile+1, "r");
2962 sCtx.zFile = "<pipe>";
2963 xCloser = pclose;
@@ -3839,16 +3839,16 @@
3839 }
3840 }else
3841
3842 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
3843 open_db(p, 0);
 
3844 if( nArg!=2 ){
3845 fprintf(stderr, "Usage: .trace FILE|off\n");
3846 rc = 1;
3847 goto meta_command_exit;
3848 }
3849 output_file_close(p->traceOut);
3850 p->traceOut = output_file_open(azArg[1]);
3851 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
3852 if( p->traceOut==0 ){
3853 sqlite3_trace(p->db, 0, 0);
3854 }else{
@@ -4352,11 +4352,11 @@
4352 exit(1);
4353 }
4354 return argv[i];
4355 }
4356
4357 int SQLITE_CDECL main(int argc, char **argv){
4358 char *zErrMsg = 0;
4359 ShellState data;
4360 const char *zInitFile = 0;
4361 int i;
4362 int rc = 0;
4363
+834 -253
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -261,10 +261,17 @@
261261
#ifndef SQLITE_API
262262
# define SQLITE_API
263263
#endif
264264
265265
266
+/*
267
+** Add the ability to override 'cdecl'
268
+*/
269
+#ifndef SQLITE_CDECL
270
+# define SQLITE_CDECL
271
+#endif
272
+
266273
/*
267274
** These no-op macros are used in front of interfaces to mark those
268275
** interfaces as either deprecated or experimental. New applications
269276
** should not use deprecated interfaces - they are supported for backwards
270277
** compatibility only. Application writers should be aware that
@@ -316,11 +323,11 @@
316323
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
317324
** [sqlite_version()] and [sqlite_source_id()].
318325
*/
319326
#define SQLITE_VERSION "3.8.9"
320327
#define SQLITE_VERSION_NUMBER 3008009
321
-#define SQLITE_SOURCE_ID "2015-03-09 10:40:48 e5da5e7d5dc5a3438ced23f1ee83e695abc29c45"
328
+#define SQLITE_SOURCE_ID "2015-03-24 18:19:39 436314b5728c9413f9ac2d837e1c19364f31be72"
322329
323330
/*
324331
** CAPI3REF: Run-Time Library Version Numbers
325332
** KEYWORDS: sqlite3_version, sqlite3_sourceid
326333
**
@@ -1157,10 +1164,17 @@
11571164
** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
11581165
** opcode causes the xFileControl method to swap the file handle with the one
11591166
** pointed to by the pArg argument. This capability is used during testing
11601167
** and only needs to be supported when SQLITE_TEST is defined.
11611168
**
1169
+** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1170
+** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
1171
+** be advantageous to block on the next WAL lock if the lock is not immediately
1172
+** available. The WAL subsystem issues this signal during rare
1173
+** circumstances in order to fix a problem with priority inversion.
1174
+** Applications should <em>not</em> use this file-control.
1175
+**
11621176
** </ul>
11631177
*/
11641178
#define SQLITE_FCNTL_LOCKSTATE 1
11651179
#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
11661180
#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1181,10 +1195,11 @@
11811195
#define SQLITE_FCNTL_TRACE 19
11821196
#define SQLITE_FCNTL_HAS_MOVED 20
11831197
#define SQLITE_FCNTL_SYNC 21
11841198
#define SQLITE_FCNTL_COMMIT_PHASETWO 22
11851199
#define SQLITE_FCNTL_WIN32_SET_HANDLE 23
1200
+#define SQLITE_FCNTL_WAL_BLOCK 24
11861201
11871202
/* deprecated names */
11881203
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
11891204
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
11901205
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -1747,11 +1762,11 @@
17471762
** disabled, the following SQLite interfaces become non-operational:
17481763
** <ul>
17491764
** <li> [sqlite3_memory_used()]
17501765
** <li> [sqlite3_memory_highwater()]
17511766
** <li> [sqlite3_soft_heap_limit64()]
1752
-** <li> [sqlite3_status()]
1767
+** <li> [sqlite3_status64()]
17531768
** </ul>)^
17541769
** ^Memory allocation statistics are enabled by default unless SQLite is
17551770
** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
17561771
** allocation statistics are disabled by default.
17571772
** </dd>
@@ -3204,15 +3219,17 @@
32043219
32053220
32063221
/*
32073222
** CAPI3REF: Error Codes And Messages
32083223
**
3209
-** ^The sqlite3_errcode() interface returns the numeric [result code] or
3210
-** [extended result code] for the most recent failed sqlite3_* API call
3211
-** associated with a [database connection]. If a prior API call failed
3212
-** but the most recent API call succeeded, the return value from
3213
-** sqlite3_errcode() is undefined. ^The sqlite3_extended_errcode()
3224
+** ^If the most recent sqlite3_* API call associated with
3225
+** [database connection] D failed, then the sqlite3_errcode(D) interface
3226
+** returns the numeric [result code] or [extended result code] for that
3227
+** API call.
3228
+** If the most recent API call was successful,
3229
+** then the return value from sqlite3_errcode() is undefined.
3230
+** ^The sqlite3_extended_errcode()
32143231
** interface is the same except that it always returns the
32153232
** [extended result code] even when extended result codes are
32163233
** disabled.
32173234
**
32183235
** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
@@ -5822,11 +5839,11 @@
58225839
** is delivered up to the client application, the string will be automatically
58235840
** freed by sqlite3_free() and the zErrMsg field will be zeroed.
58245841
*/
58255842
struct sqlite3_vtab {
58265843
const sqlite3_module *pModule; /* The module for this virtual table */
5827
- int nRef; /* NO LONGER USED */
5844
+ int nRef; /* Number of open cursors */
58285845
char *zErrMsg; /* Error message from sqlite3_mprintf() */
58295846
/* Virtual table implementations will typically add additional fields */
58305847
};
58315848
58325849
/*
@@ -6500,11 +6517,11 @@
65006517
#define SQLITE_TESTCTRL_LAST 25
65016518
65026519
/*
65036520
** CAPI3REF: SQLite Runtime Status
65046521
**
6505
-** ^This interface is used to retrieve runtime status information
6522
+** ^These interfaces are used to retrieve runtime status information
65066523
** about the performance of SQLite, and optionally to reset various
65076524
** highwater marks. ^The first argument is an integer code for
65086525
** the specific parameter to measure. ^(Recognized integer codes
65096526
** are of the form [status parameters | SQLITE_STATUS_...].)^
65106527
** ^The current value of the parameter is returned into *pCurrent.
@@ -6514,23 +6531,26 @@
65146531
** value. For those parameters
65156532
** nothing is written into *pHighwater and the resetFlag is ignored.)^
65166533
** ^(Other parameters record only the highwater mark and not the current
65176534
** value. For these latter parameters nothing is written into *pCurrent.)^
65186535
**
6519
-** ^The sqlite3_status() routine returns SQLITE_OK on success and a
6520
-** non-zero [error code] on failure.
6536
+** ^The sqlite3_status() and sqlite3_status64() routines return
6537
+** SQLITE_OK on success and a non-zero [error code] on failure.
65216538
**
6522
-** This routine is threadsafe but is not atomic. This routine can be
6523
-** called while other threads are running the same or different SQLite
6524
-** interfaces. However the values returned in *pCurrent and
6525
-** *pHighwater reflect the status of SQLite at different points in time
6526
-** and it is possible that another thread might change the parameter
6527
-** in between the times when *pCurrent and *pHighwater are written.
6539
+** If either the current value or the highwater mark is too large to
6540
+** be represented by a 32-bit integer, then the values returned by
6541
+** sqlite3_status() are undefined.
65286542
**
65296543
** See also: [sqlite3_db_status()]
65306544
*/
65316545
SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
6546
+SQLITE_API int sqlite3_status64(
6547
+ int op,
6548
+ sqlite3_int64 *pCurrent,
6549
+ sqlite3_int64 *pHighwater,
6550
+ int resetFlag
6551
+);
65326552
65336553
65346554
/*
65356555
** CAPI3REF: Status Parameters
65366556
** KEYWORDS: {status parameters}
@@ -8911,10 +8931,24 @@
89118931
**
89128932
** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
89138933
*/
89148934
typedef INT16_TYPE LogEst;
89158935
8936
+/*
8937
+** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
8938
+*/
8939
+#ifndef SQLITE_PTRSIZE
8940
+# if defined(__SIZEOF_POINTER__)
8941
+# define SQLITE_PTRSIZE __SIZEOF_POINTER__
8942
+# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
8943
+ defined(_M_ARM) || defined(__arm__) || defined(__x86)
8944
+# define SQLITE_PTRSIZE 4
8945
+# else
8946
+# define SQLITE_PTRSIZE 8
8947
+# endif
8948
+#endif
8949
+
89168950
/*
89178951
** Macros to determine whether the machine is big or little endian,
89188952
** and whether or not that determination is run-time or compile-time.
89198953
**
89208954
** For best performance, an attempt is made to guess at the byte-order
@@ -9361,12 +9395,22 @@
93619395
#define BTREE_DATA_VERSION 15 /* A virtual meta-value */
93629396
93639397
/*
93649398
** Values that may be OR'd together to form the second argument of an
93659399
** sqlite3BtreeCursorHints() call.
9400
+**
9401
+** The BTREE_BULKLOAD flag is set on index cursors when the index is going
9402
+** to be filled with content that is already in sorted order.
9403
+**
9404
+** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
9405
+** OP_SeekLE opcodes for a range search, but where the range of entries
9406
+** selected will all have the same key. In other words, the cursor will
9407
+** be used only for equality key searches.
9408
+**
93669409
*/
9367
-#define BTREE_BULKLOAD 0x00000001
9410
+#define BTREE_BULKLOAD 0x00000001 /* Used to full index in sorted order */
9411
+#define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
93689412
93699413
SQLITE_PRIVATE int sqlite3BtreeCursor(
93709414
Btree*, /* BTree containing table to open */
93719415
int iTable, /* Index of root page */
93729416
int wrFlag, /* 1 for writing. 0 for read-only */
@@ -9408,10 +9452,13 @@
94089452
SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
94099453
SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
94109454
SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
94119455
SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
94129456
SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
9457
+#ifdef SQLITE_DEBUG
9458
+SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
9459
+#endif
94139460
SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
94149461
SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
94159462
94169463
#ifndef NDEBUG
94179464
SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
@@ -10908,10 +10955,11 @@
1090810955
} init;
1090910956
int nVdbeActive; /* Number of VDBEs currently running */
1091010957
int nVdbeRead; /* Number of active VDBEs that read or write */
1091110958
int nVdbeWrite; /* Number of active VDBEs that read and write */
1091210959
int nVdbeExec; /* Number of nested calls to VdbeExec() */
10960
+ int nVDestroy; /* Number of active OP_VDestroy operations */
1091310961
int nExtension; /* Number of loaded extensions */
1091410962
void **aExtension; /* Array of shared library handles */
1091510963
void (*xTrace)(void*,const char*); /* Trace function */
1091610964
void *pTraceArg; /* Argument to the trace function */
1091710965
void (*xProfile)(void*,const char*,u64); /* Profiling function */
@@ -12502,11 +12550,12 @@
1250212550
#define OPFLAG_APPEND 0x08 /* This is likely to be an append */
1250312551
#define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
1250412552
#define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
1250512553
#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
1250612554
#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
12507
-#define OPFLAG_P2ISREG 0x02 /* P2 to OP_Open** is a register number */
12555
+#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
12556
+#define OPFLAG_P2ISREG 0x04 /* P2 to OP_Open** is a register number */
1250812557
#define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
1250912558
1251012559
/*
1251112560
* Each trigger present in the database schema is stored as an instance of
1251212561
* struct Trigger.
@@ -12906,14 +12955,19 @@
1290612955
SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
1290712956
SQLITE_PRIVATE int sqlite3MutexInit(void);
1290812957
SQLITE_PRIVATE int sqlite3MutexEnd(void);
1290912958
#endif
1291012959
12911
-SQLITE_PRIVATE int sqlite3StatusValue(int);
12912
-SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
12960
+SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int);
12961
+SQLITE_PRIVATE void sqlite3StatusUp(int, int);
12962
+SQLITE_PRIVATE void sqlite3StatusDown(int, int);
1291312963
SQLITE_PRIVATE void sqlite3StatusSet(int, int);
1291412964
12965
+/* Access to mutexes used by sqlite3_status() */
12966
+SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void);
12967
+SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void);
12968
+
1291512969
#ifndef SQLITE_OMIT_FLOATING_POINT
1291612970
SQLITE_PRIVATE int sqlite3IsNaN(double);
1291712971
#else
1291812972
# define sqlite3IsNaN(X) 0
1291912973
#endif
@@ -13289,11 +13343,11 @@
1328913343
SQLITE_PRIVATE const char *sqlite3ErrStr(int);
1329013344
SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
1329113345
SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
1329213346
SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
1329313347
SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
13294
-SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*);
13348
+SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
1329513349
SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
1329613350
SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
1329713351
SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
1329813352
SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
1329913353
SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
@@ -14676,18 +14730,10 @@
1467614730
** An instance of the virtual machine. This structure contains the complete
1467714731
** state of the virtual machine.
1467814732
**
1467914733
** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
1468014734
** is really a pointer to an instance of this structure.
14681
-**
14682
-** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
14683
-** any virtual table method invocations made by the vdbe program. It is
14684
-** set to 2 for xDestroy method calls and 1 for all other methods. This
14685
-** variable is used for two purposes: to allow xDestroy methods to execute
14686
-** "DROP TABLE" statements and to prevent some nasty side effects of
14687
-** malloc failure when SQLite is invoked recursively by a virtual table
14688
-** method function.
1468914735
*/
1469014736
struct Vdbe {
1469114737
sqlite3 *db; /* The database connection that owns this statement */
1469214738
Op *aOp; /* Space to hold the virtual machine's program */
1469314739
Mem *aMem; /* The memory locations */
@@ -14714,11 +14760,10 @@
1471414760
#endif
1471514761
u16 nResColumn; /* Number of columns in one row of the result set */
1471614762
u8 errorAction; /* Recovery action to do in case of an error */
1471714763
u8 minWriteFileFormat; /* Minimum file format for writable database files */
1471814764
bft explain:2; /* True if EXPLAIN present on SQL command */
14719
- bft inVtabMethod:2; /* See comments above */
1472014765
bft changeCntOn:1; /* True to update the change-counter */
1472114766
bft expired:1; /* True if the VM needs to be recompiled */
1472214767
bft runOnlyOnce:1; /* Automatically expire on reset */
1472314768
bft usesStmtJournal:1; /* True if uses a statement journal */
1472414769
bft readOnly:1; /* True for statements that do not write */
@@ -14874,13 +14919,35 @@
1487414919
/*
1487514920
** Variables in which to record status information.
1487614921
*/
1487714922
typedef struct sqlite3StatType sqlite3StatType;
1487814923
static SQLITE_WSD struct sqlite3StatType {
14879
- int nowValue[10]; /* Current value */
14880
- int mxValue[10]; /* Maximum value */
14924
+#if SQLITE_PTRSIZE>4
14925
+ sqlite3_int64 nowValue[10]; /* Current value */
14926
+ sqlite3_int64 mxValue[10]; /* Maximum value */
14927
+#else
14928
+ u32 nowValue[10]; /* Current value */
14929
+ u32 mxValue[10]; /* Maximum value */
14930
+#endif
1488114931
} sqlite3Stat = { {0,}, {0,} };
14932
+
14933
+/*
14934
+** Elements of sqlite3Stat[] are protected by either the memory allocator
14935
+** mutex, or by the pcache1 mutex. The following array determines which.
14936
+*/
14937
+static const char statMutex[] = {
14938
+ 0, /* SQLITE_STATUS_MEMORY_USED */
14939
+ 1, /* SQLITE_STATUS_PAGECACHE_USED */
14940
+ 1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
14941
+ 0, /* SQLITE_STATUS_SCRATCH_USED */
14942
+ 0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */
14943
+ 0, /* SQLITE_STATUS_MALLOC_SIZE */
14944
+ 0, /* SQLITE_STATUS_PARSER_STACK */
14945
+ 1, /* SQLITE_STATUS_PAGECACHE_SIZE */
14946
+ 0, /* SQLITE_STATUS_SCRATCH_SIZE */
14947
+ 0, /* SQLITE_STATUS_MALLOC_COUNT */
14948
+};
1488214949
1488314950
1488414951
/* The "wsdStat" macro will resolve to the status information
1488514952
** state vector. If writable static data is unsupported on the target,
1488614953
** we have to locate the state vector at run-time. In the more common
@@ -14894,64 +14961,110 @@
1489414961
# define wsdStatInit
1489514962
# define wsdStat sqlite3Stat
1489614963
#endif
1489714964
1489814965
/*
14899
-** Return the current value of a status parameter.
14966
+** Return the current value of a status parameter. The caller must
14967
+** be holding the appropriate mutex.
1490014968
*/
14901
-SQLITE_PRIVATE int sqlite3StatusValue(int op){
14969
+SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int op){
1490214970
wsdStatInit;
1490314971
assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
14972
+ assert( op>=0 && op<ArraySize(statMutex) );
14973
+ assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
14974
+ : sqlite3MallocMutex()) );
1490414975
return wsdStat.nowValue[op];
1490514976
}
1490614977
1490714978
/*
14908
-** Add N to the value of a status record. It is assumed that the
14909
-** caller holds appropriate locks.
14979
+** Add N to the value of a status record. The caller must hold the
14980
+** appropriate mutex. (Locking is checked by assert()).
14981
+**
14982
+** The StatusUp() routine can accept positive or negative values for N.
14983
+** The value of N is added to the current status value and the high-water
14984
+** mark is adjusted if necessary.
14985
+**
14986
+** The StatusDown() routine lowers the current value by N. The highwater
14987
+** mark is unchanged. N must be non-negative for StatusDown().
1491014988
*/
14911
-SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
14989
+SQLITE_PRIVATE void sqlite3StatusUp(int op, int N){
1491214990
wsdStatInit;
1491314991
assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
14992
+ assert( op>=0 && op<ArraySize(statMutex) );
14993
+ assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
14994
+ : sqlite3MallocMutex()) );
1491414995
wsdStat.nowValue[op] += N;
1491514996
if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
1491614997
wsdStat.mxValue[op] = wsdStat.nowValue[op];
1491714998
}
1491814999
}
15000
+SQLITE_PRIVATE void sqlite3StatusDown(int op, int N){
15001
+ wsdStatInit;
15002
+ assert( N>=0 );
15003
+ assert( op>=0 && op<ArraySize(statMutex) );
15004
+ assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
15005
+ : sqlite3MallocMutex()) );
15006
+ assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
15007
+ wsdStat.nowValue[op] -= N;
15008
+}
1491915009
1492015010
/*
14921
-** Set the value of a status to X.
15011
+** Set the value of a status to X. The highwater mark is adjusted if
15012
+** necessary. The caller must hold the appropriate mutex.
1492215013
*/
1492315014
SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
1492415015
wsdStatInit;
1492515016
assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
15017
+ assert( op>=0 && op<ArraySize(statMutex) );
15018
+ assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
15019
+ : sqlite3MallocMutex()) );
1492615020
wsdStat.nowValue[op] = X;
1492715021
if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
1492815022
wsdStat.mxValue[op] = wsdStat.nowValue[op];
1492915023
}
1493015024
}
1493115025
1493215026
/*
1493315027
** Query status information.
14934
-**
14935
-** This implementation assumes that reading or writing an aligned
14936
-** 32-bit integer is an atomic operation. If that assumption is not true,
14937
-** then this routine is not threadsafe.
1493815028
*/
14939
-SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
15029
+SQLITE_API int sqlite3_status64(
15030
+ int op,
15031
+ sqlite3_int64 *pCurrent,
15032
+ sqlite3_int64 *pHighwater,
15033
+ int resetFlag
15034
+){
15035
+ sqlite3_mutex *pMutex;
1494015036
wsdStatInit;
1494115037
if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
1494215038
return SQLITE_MISUSE_BKPT;
1494315039
}
1494415040
#ifdef SQLITE_ENABLE_API_ARMOR
1494515041
if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
1494615042
#endif
15043
+ pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
15044
+ sqlite3_mutex_enter(pMutex);
1494715045
*pCurrent = wsdStat.nowValue[op];
1494815046
*pHighwater = wsdStat.mxValue[op];
1494915047
if( resetFlag ){
1495015048
wsdStat.mxValue[op] = wsdStat.nowValue[op];
1495115049
}
15050
+ sqlite3_mutex_leave(pMutex);
15051
+ (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */
1495215052
return SQLITE_OK;
15053
+}
15054
+SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
15055
+ sqlite3_int64 iCur, iHwtr;
15056
+ int rc;
15057
+#ifdef SQLITE_ENABLE_API_ARMOR
15058
+ if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
15059
+#endif
15060
+ rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
15061
+ if( rc==0 ){
15062
+ *pCurrent = (int)iCur;
15063
+ *pHighwater = (int)iHwtr;
15064
+ }
15065
+ return rc;
1495315066
}
1495415067
1495515068
/*
1495615069
** Query status information for a single database connection
1495715070
*/
@@ -20401,10 +20514,17 @@
2040120514
*/
2040220515
int nearlyFull;
2040320516
} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
2040420517
2040520518
#define mem0 GLOBAL(struct Mem0Global, mem0)
20519
+
20520
+/*
20521
+** Return the memory allocator mutex. sqlite3_status() needs it.
20522
+*/
20523
+SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){
20524
+ return mem0.mutex;
20525
+}
2040620526
2040720527
/*
2040820528
** This routine runs when the memory allocator sees that the
2040920529
** total memory allocation is about to exceed the soft heap
2041020530
** limit.
@@ -20424,11 +20544,11 @@
2042420544
static int sqlite3MemoryAlarm(
2042520545
void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
2042620546
void *pArg,
2042720547
sqlite3_int64 iThreshold
2042820548
){
20429
- int nUsed;
20549
+ sqlite3_int64 nUsed;
2043020550
sqlite3_mutex_enter(mem0.mutex);
2043120551
mem0.alarmCallback = xCallback;
2043220552
mem0.alarmArg = pArg;
2043320553
mem0.alarmThreshold = iThreshold;
2043420554
nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
@@ -20593,11 +20713,11 @@
2059320713
void *p;
2059420714
assert( sqlite3_mutex_held(mem0.mutex) );
2059520715
nFull = sqlite3GlobalConfig.m.xRoundup(n);
2059620716
sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
2059720717
if( mem0.alarmCallback!=0 ){
20598
- int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
20718
+ sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
2059920719
if( nUsed >= mem0.alarmThreshold - nFull ){
2060020720
mem0.nearlyFull = 1;
2060120721
sqlite3MallocAlarm(nFull);
2060220722
}else{
2060320723
mem0.nearlyFull = 0;
@@ -20610,12 +20730,12 @@
2061020730
p = sqlite3GlobalConfig.m.xMalloc(nFull);
2061120731
}
2061220732
#endif
2061320733
if( p ){
2061420734
nFull = sqlite3MallocSize(p);
20615
- sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
20616
- sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
20735
+ sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
20736
+ sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
2061720737
}
2061820738
*pp = p;
2061920739
return nFull;
2062020740
}
2062120741
@@ -20688,18 +20808,18 @@
2068820808
sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
2068920809
if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
2069020810
p = mem0.pScratchFree;
2069120811
mem0.pScratchFree = mem0.pScratchFree->pNext;
2069220812
mem0.nScratchFree--;
20693
- sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
20813
+ sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
2069420814
sqlite3_mutex_leave(mem0.mutex);
2069520815
}else{
2069620816
sqlite3_mutex_leave(mem0.mutex);
2069720817
p = sqlite3Malloc(n);
2069820818
if( sqlite3GlobalConfig.bMemstat && p ){
2069920819
sqlite3_mutex_enter(mem0.mutex);
20700
- sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
20820
+ sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
2070120821
sqlite3_mutex_leave(mem0.mutex);
2070220822
}
2070320823
sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
2070420824
}
2070520825
assert( sqlite3_mutex_notheld(mem0.mutex) );
@@ -20736,23 +20856,23 @@
2073620856
sqlite3_mutex_enter(mem0.mutex);
2073720857
pSlot->pNext = mem0.pScratchFree;
2073820858
mem0.pScratchFree = pSlot;
2073920859
mem0.nScratchFree++;
2074020860
assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
20741
- sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
20861
+ sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
2074220862
sqlite3_mutex_leave(mem0.mutex);
2074320863
}else{
2074420864
/* Release memory back to the heap */
2074520865
assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
20746
- assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
20866
+ assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
2074720867
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
2074820868
if( sqlite3GlobalConfig.bMemstat ){
2074920869
int iSize = sqlite3MallocSize(p);
2075020870
sqlite3_mutex_enter(mem0.mutex);
20751
- sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
20752
- sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
20753
- sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
20871
+ sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
20872
+ sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
20873
+ sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
2075420874
sqlite3GlobalConfig.m.xFree(p);
2075520875
sqlite3_mutex_leave(mem0.mutex);
2075620876
}else{
2075720877
sqlite3GlobalConfig.m.xFree(p);
2075820878
}
@@ -20779,26 +20899,26 @@
2077920899
assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
2078020900
return sqlite3GlobalConfig.m.xSize(p);
2078120901
}
2078220902
SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
2078320903
if( db==0 ){
20784
- assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
20904
+ assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
2078520905
assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
2078620906
return sqlite3MallocSize(p);
2078720907
}else{
2078820908
assert( sqlite3_mutex_held(db->mutex) );
2078920909
if( isLookaside(db, p) ){
2079020910
return db->lookaside.sz;
2079120911
}else{
2079220912
assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
20793
- assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
20913
+ assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
2079420914
return sqlite3GlobalConfig.m.xSize(p);
2079520915
}
2079620916
}
2079720917
}
2079820918
SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
20799
- assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
20919
+ assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
2080020920
assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
2080120921
return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
2080220922
}
2080320923
2080420924
/*
@@ -20805,15 +20925,15 @@
2080520925
** Free memory previously obtained from sqlite3Malloc().
2080620926
*/
2080720927
SQLITE_API void sqlite3_free(void *p){
2080820928
if( p==0 ) return; /* IMP: R-49053-54554 */
2080920929
assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
20810
- assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
20930
+ assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
2081120931
if( sqlite3GlobalConfig.bMemstat ){
2081220932
sqlite3_mutex_enter(mem0.mutex);
20813
- sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
20814
- sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
20933
+ sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
20934
+ sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
2081520935
sqlite3GlobalConfig.m.xFree(p);
2081620936
sqlite3_mutex_leave(mem0.mutex);
2081720937
}else{
2081820938
sqlite3GlobalConfig.m.xFree(p);
2081920939
}
@@ -20850,11 +20970,11 @@
2085020970
db->lookaside.nOut--;
2085120971
return;
2085220972
}
2085320973
}
2085420974
assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
20855
- assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
20975
+ assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
2085620976
assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
2085720977
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
2085820978
sqlite3_free(p);
2085920979
}
2086020980
@@ -20863,11 +20983,11 @@
2086320983
*/
2086420984
SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
2086520985
int nOld, nNew, nDiff;
2086620986
void *pNew;
2086720987
assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
20868
- assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
20988
+ assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
2086920989
if( pOld==0 ){
2087020990
return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
2087120991
}
2087220992
if( nBytes==0 ){
2087320993
sqlite3_free(pOld); /* IMP: R-26507-47431 */
@@ -20897,11 +21017,11 @@
2089721017
sqlite3MallocAlarm((int)nBytes);
2089821018
pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
2089921019
}
2090021020
if( pNew ){
2090121021
nNew = sqlite3MallocSize(pNew);
20902
- sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
21022
+ sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
2090321023
}
2090421024
sqlite3_mutex_leave(mem0.mutex);
2090521025
}else{
2090621026
pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
2090721027
}
@@ -21030,11 +21150,11 @@
2103021150
memcpy(pNew, p, db->lookaside.sz);
2103121151
sqlite3DbFree(db, p);
2103221152
}
2103321153
}else{
2103421154
assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
21035
- assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
21155
+ assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
2103621156
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
2103721157
pNew = sqlite3_realloc64(p, n);
2103821158
if( !pNew ){
2103921159
db->mallocFailed = 1;
2104021160
}
@@ -25287,10 +25407,11 @@
2528725407
#define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
2528825408
#define UNIXFILE_DELETE 0x20 /* Delete on close */
2528925409
#define UNIXFILE_URI 0x40 /* Filename might have query parameters */
2529025410
#define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
2529125411
#define UNIXFILE_WARNED 0x0100 /* verifyDbFile() warnings issued */
25412
+#define UNIXFILE_BLOCK 0x0200 /* Next SHM lock might block */
2529225413
2529325414
/*
2529425415
** Include code that is common to all os_*.c files
2529525416
*/
2529625417
/************** Include os_common.h in the middle of os_unix.c ***************/
@@ -26782,11 +26903,11 @@
2678226903
2678326904
assert( pFile );
2678426905
OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
2678526906
azFileLock(eFileLock), azFileLock(pFile->eFileLock),
2678626907
azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
26787
- osGetpid()));
26908
+ osGetpid(0)));
2678826909
2678926910
/* If there is already a lock of this type or more restrictive on the
2679026911
** unixFile, do nothing. Don't use the end_lock: exit path, as
2679126912
** unixEnterMutex() hasn't been called yet.
2679226913
*/
@@ -26990,11 +27111,11 @@
2699027111
int rc = SQLITE_OK;
2699127112
2699227113
assert( pFile );
2699327114
OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
2699427115
pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
26995
- osGetpid()));
27116
+ osGetpid(0)));
2699627117
2699727118
assert( eFileLock<=SHARED_LOCK );
2699827119
if( pFile->eFileLock<=eFileLock ){
2699927120
return SQLITE_OK;
2700027121
}
@@ -27417,11 +27538,11 @@
2741727538
char *zLockFile = (char *)pFile->lockingContext;
2741827539
int rc;
2741927540
2742027541
assert( pFile );
2742127542
OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
27422
- pFile->eFileLock, osGetpid()));
27543
+ pFile->eFileLock, osGetpid(0)));
2742327544
assert( eFileLock<=SHARED_LOCK );
2742427545
2742527546
/* no-op if possible */
2742627547
if( pFile->eFileLock==eFileLock ){
2742727548
return SQLITE_OK;
@@ -27635,11 +27756,11 @@
2763527756
static int flockUnlock(sqlite3_file *id, int eFileLock) {
2763627757
unixFile *pFile = (unixFile*)id;
2763727758
2763827759
assert( pFile );
2763927760
OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
27640
- pFile->eFileLock, osGetpid()));
27761
+ pFile->eFileLock, osGetpid(0)));
2764127762
assert( eFileLock<=SHARED_LOCK );
2764227763
2764327764
/* no-op if possible */
2764427765
if( pFile->eFileLock==eFileLock ){
2764527766
return SQLITE_OK;
@@ -27803,11 +27924,11 @@
2780327924
sem_t *pSem = pFile->pInode->pSem;
2780427925
2780527926
assert( pFile );
2780627927
assert( pSem );
2780727928
OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
27808
- pFile->eFileLock, osGetpid()));
27929
+ pFile->eFileLock, osGetpid(0)));
2780927930
assert( eFileLock<=SHARED_LOCK );
2781027931
2781127932
/* no-op if possible */
2781227933
if( pFile->eFileLock==eFileLock ){
2781327934
return SQLITE_OK;
@@ -28017,11 +28138,11 @@
2801728138
afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2801828139
2801928140
assert( pFile );
2802028141
OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
2802128142
azFileLock(eFileLock), azFileLock(pFile->eFileLock),
28022
- azFileLock(pInode->eFileLock), pInode->nShared , osGetpid()));
28143
+ azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
2802328144
2802428145
/* If there is already a lock of this type or more restrictive on the
2802528146
** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
2802628147
** unixEnterMutex() hasn't been called yet.
2802728148
*/
@@ -28203,11 +28324,11 @@
2820328324
#endif
2820428325
2820528326
assert( pFile );
2820628327
OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
2820728328
pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
28208
- osGetpid()));
28329
+ osGetpid(0)));
2820928330
2821028331
assert( eFileLock<=SHARED_LOCK );
2821128332
if( pFile->eFileLock<=eFileLock ){
2821228333
return SQLITE_OK;
2821328334
}
@@ -29028,10 +29149,14 @@
2902829149
** Information and control of an open file handle.
2902929150
*/
2903029151
static int unixFileControl(sqlite3_file *id, int op, void *pArg){
2903129152
unixFile *pFile = (unixFile*)id;
2903229153
switch( op ){
29154
+ case SQLITE_FCNTL_WAL_BLOCK: {
29155
+ pFile->ctrlFlags |= UNIXFILE_BLOCK;
29156
+ return SQLITE_OK;
29157
+ }
2903329158
case SQLITE_FCNTL_LOCKSTATE: {
2903429159
*(int*)pArg = pFile->eFileLock;
2903529160
return SQLITE_OK;
2903629161
}
2903729162
case SQLITE_FCNTL_LAST_ERRNO: {
@@ -29337,37 +29462,42 @@
2933729462
**
2933829463
** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
2933929464
** otherwise.
2934029465
*/
2934129466
static int unixShmSystemLock(
29342
- unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
29467
+ unixFile *pFile, /* Open connection to the WAL file */
2934329468
int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
2934429469
int ofst, /* First byte of the locking range */
2934529470
int n /* Number of bytes to lock */
2934629471
){
29347
- struct flock f; /* The posix advisory locking structure */
29348
- int rc = SQLITE_OK; /* Result code form fcntl() */
29472
+ unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
29473
+ struct flock f; /* The posix advisory locking structure */
29474
+ int rc = SQLITE_OK; /* Result code form fcntl() */
2934929475
2935029476
/* Access to the unixShmNode object is serialized by the caller */
29477
+ pShmNode = pFile->pInode->pShmNode;
2935129478
assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
2935229479
2935329480
/* Shared locks never span more than one byte */
2935429481
assert( n==1 || lockType!=F_RDLCK );
2935529482
2935629483
/* Locks are within range */
2935729484
assert( n>=1 && n<SQLITE_SHM_NLOCK );
2935829485
2935929486
if( pShmNode->h>=0 ){
29487
+ int lkType;
2936029488
/* Initialize the locking parameters */
2936129489
memset(&f, 0, sizeof(f));
2936229490
f.l_type = lockType;
2936329491
f.l_whence = SEEK_SET;
2936429492
f.l_start = ofst;
2936529493
f.l_len = n;
2936629494
29367
- rc = osFcntl(pShmNode->h, F_SETLK, &f);
29495
+ lkType = (pFile->ctrlFlags & UNIXFILE_BLOCK)!=0 ? F_SETLKW : F_SETLK;
29496
+ rc = osFcntl(pShmNode->h, lkType, &f);
2936829497
rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
29498
+ pFile->ctrlFlags &= ~UNIXFILE_BLOCK;
2936929499
}
2937029500
2937129501
/* Update the global lock state and do debug tracing */
2937229502
#ifdef SQLITE_DEBUG
2937329503
{ u16 mask;
@@ -29573,17 +29703,17 @@
2957329703
2957429704
/* Check to see if another process is holding the dead-man switch.
2957529705
** If not, truncate the file to zero length.
2957629706
*/
2957729707
rc = SQLITE_OK;
29578
- if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
29708
+ if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
2957929709
if( robust_ftruncate(pShmNode->h, 0) ){
2958029710
rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
2958129711
}
2958229712
}
2958329713
if( rc==SQLITE_OK ){
29584
- rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
29714
+ rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
2958529715
}
2958629716
if( rc ) goto shm_open_err;
2958729717
}
2958829718
}
2958929719
@@ -29811,11 +29941,11 @@
2981129941
allMask |= pX->sharedMask;
2981229942
}
2981329943
2981429944
/* Unlock the system-level locks */
2981529945
if( (mask & allMask)==0 ){
29816
- rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
29946
+ rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n);
2981729947
}else{
2981829948
rc = SQLITE_OK;
2981929949
}
2982029950
2982129951
/* Undo the local locks */
@@ -29839,11 +29969,11 @@
2983929969
}
2984029970
2984129971
/* Get shared locks at the system level, if necessary */
2984229972
if( rc==SQLITE_OK ){
2984329973
if( (allShared & mask)==0 ){
29844
- rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
29974
+ rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n);
2984529975
}else{
2984629976
rc = SQLITE_OK;
2984729977
}
2984829978
}
2984929979
@@ -29864,20 +29994,20 @@
2986429994
2986529995
/* Get the exclusive locks at the system level. Then if successful
2986629996
** also mark the local connection as being locked.
2986729997
*/
2986829998
if( rc==SQLITE_OK ){
29869
- rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
29999
+ rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
2987030000
if( rc==SQLITE_OK ){
2987130001
assert( (p->sharedMask & mask)==0 );
2987230002
p->exclMask |= mask;
2987330003
}
2987430004
}
2987530005
}
2987630006
sqlite3_mutex_leave(pShmNode->mutex);
2987730007
OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
29878
- p->id, osGetpid(), p->sharedMask, p->exclMask));
30008
+ p->id, osGetpid(0), p->sharedMask, p->exclMask));
2987930009
return rc;
2988030010
}
2988130011
2988230012
/*
2988330013
** Implement a memory barrier or memory fence on shared memory.
@@ -30968,12 +31098,12 @@
3096831098
/* Detect a pid change and reset the PRNG. There is a race condition
3096931099
** here such that two or more threads all trying to open databases at
3097031100
** the same instant might all reset the PRNG. But multiple resets
3097131101
** are harmless.
3097231102
*/
30973
- if( randomnessPid!=osGetpid() ){
30974
- randomnessPid = osGetpid();
31103
+ if( randomnessPid!=osGetpid(0) ){
31104
+ randomnessPid = osGetpid(0);
3097531105
sqlite3_randomness(0,0);
3097631106
}
3097731107
3097831108
memset(p, 0, sizeof(unixFile));
3097931109
@@ -31360,11 +31490,11 @@
3136031490
** When testing, initializing zBuf[] to zero is all we do. That means
3136131491
** that we always use the same random number sequence. This makes the
3136231492
** tests repeatable.
3136331493
*/
3136431494
memset(zBuf, 0, nBuf);
31365
- randomnessPid = osGetpid();
31495
+ randomnessPid = osGetpid(0);
3136631496
#if !defined(SQLITE_TEST)
3136731497
{
3136831498
int fd, got;
3136931499
fd = robust_open("/dev/urandom", O_RDONLY, 0);
3137031500
if( fd<0 ){
@@ -31681,11 +31811,11 @@
3168131811
#else
3168231812
# ifdef _CS_DARWIN_USER_TEMP_DIR
3168331813
{
3168431814
if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
3168531815
OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
31686
- lPath, errno, osGetpid()));
31816
+ lPath, errno, osGetpid(0)));
3168731817
return SQLITE_IOERR_LOCK;
3168831818
}
3168931819
len = strlcat(lPath, "sqliteplocks", maxLen);
3169031820
}
3169131821
# else
@@ -31703,11 +31833,11 @@
3170331833
char c = dbPath[i];
3170431834
lPath[i+len] = (c=='/')?'_':c;
3170531835
}
3170631836
lPath[i+len]='\0';
3170731837
strlcat(lPath, ":auto:", maxLen);
31708
- OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid()));
31838
+ OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
3170931839
return SQLITE_OK;
3171031840
}
3171131841
3171231842
/*
3171331843
** Creates the lock file and any missing directories in lockPath
@@ -31730,20 +31860,20 @@
3173031860
if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
3173131861
int err=errno;
3173231862
if( err!=EEXIST ) {
3173331863
OSTRACE(("CREATELOCKPATH FAILED creating %s, "
3173431864
"'%s' proxy lock path=%s pid=%d\n",
31735
- buf, strerror(err), lockPath, osGetpid()));
31865
+ buf, strerror(err), lockPath, osGetpid(0)));
3173631866
return err;
3173731867
}
3173831868
}
3173931869
}
3174031870
start=i+1;
3174131871
}
3174231872
buf[i] = lockPath[i];
3174331873
}
31744
- OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, osGetpid()));
31874
+ OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, osGetpid(0)));
3174531875
return 0;
3174631876
}
3174731877
3174831878
/*
3174931879
** Create a new VFS file descriptor (stored in memory obtained from
@@ -32045,11 +32175,11 @@
3204532175
int tryOldLockPath = 0;
3204632176
int forceNewLockPath = 0;
3204732177
3204832178
OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
3204932179
(pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
32050
- osGetpid()));
32180
+ osGetpid(0)));
3205132181
3205232182
rc = proxyGetHostID(myHostID, &pError);
3205332183
if( (rc&0xff)==SQLITE_IOERR ){
3205432184
storeLastErrno(pFile, pError);
3205532185
goto end_takeconch;
@@ -32255,11 +32385,11 @@
3225532385
3225632386
pCtx = (proxyLockingContext *)pFile->lockingContext;
3225732387
conchFile = pCtx->conchFile;
3225832388
OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
3225932389
(pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
32260
- osGetpid()));
32390
+ osGetpid(0)));
3226132391
if( pCtx->conchHeld>0 ){
3226232392
rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
3226332393
}
3226432394
pCtx->conchHeld = 0;
3226532395
OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
@@ -32397,11 +32527,11 @@
3239732527
}else{
3239832528
lockPath=(char *)path;
3239932529
}
3240032530
3240132531
OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
32402
- (lockPath ? lockPath : ":auto:"), osGetpid()));
32532
+ (lockPath ? lockPath : ":auto:"), osGetpid(0)));
3240332533
3240432534
pCtx = sqlite3_malloc( sizeof(*pCtx) );
3240532535
if( pCtx==0 ){
3240632536
return SQLITE_NOMEM;
3240732537
}
@@ -39866,20 +39996,20 @@
3986639996
** in pcache1 need to be protected via mutex.
3986739997
*/
3986839998
static void *pcache1Alloc(int nByte){
3986939999
void *p = 0;
3987040000
assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
39871
- sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
3987240001
if( nByte<=pcache1.szSlot ){
3987340002
sqlite3_mutex_enter(pcache1.mutex);
3987440003
p = (PgHdr1 *)pcache1.pFree;
3987540004
if( p ){
3987640005
pcache1.pFree = pcache1.pFree->pNext;
3987740006
pcache1.nFreeSlot--;
3987840007
pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
3987940008
assert( pcache1.nFreeSlot>=0 );
39880
- sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
40009
+ sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
40010
+ sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
3988140011
}
3988240012
sqlite3_mutex_leave(pcache1.mutex);
3988340013
}
3988440014
if( p==0 ){
3988540015
/* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
@@ -39888,11 +40018,12 @@
3988840018
p = sqlite3Malloc(nByte);
3988940019
#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
3989040020
if( p ){
3989140021
int sz = sqlite3MallocSize(p);
3989240022
sqlite3_mutex_enter(pcache1.mutex);
39893
- sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
40023
+ sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
40024
+ sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
3989440025
sqlite3_mutex_leave(pcache1.mutex);
3989540026
}
3989640027
#endif
3989740028
sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
3989840029
}
@@ -39906,11 +40037,11 @@
3990640037
int nFreed = 0;
3990740038
if( p==0 ) return 0;
3990840039
if( p>=pcache1.pStart && p<pcache1.pEnd ){
3990940040
PgFreeslot *pSlot;
3991040041
sqlite3_mutex_enter(pcache1.mutex);
39911
- sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
40042
+ sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
3991240043
pSlot = (PgFreeslot*)p;
3991340044
pSlot->pNext = pcache1.pFree;
3991440045
pcache1.pFree = pSlot;
3991540046
pcache1.nFreeSlot++;
3991640047
pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
@@ -39920,11 +40051,11 @@
3992040051
assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
3992140052
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
3992240053
nFreed = sqlite3MallocSize(p);
3992340054
#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
3992440055
sqlite3_mutex_enter(pcache1.mutex);
39925
- sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed);
40056
+ sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
3992640057
sqlite3_mutex_leave(pcache1.mutex);
3992740058
#endif
3992840059
sqlite3_free(p);
3992940060
}
3993040061
return nFreed;
@@ -40656,10 +40787,18 @@
4065640787
4065740788
/*
4065840789
** Return the size of the header on each page of this PCACHE implementation.
4065940790
*/
4066040791
SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
40792
+
40793
+/*
40794
+** Return the global mutex used by this PCACHE implementation. The
40795
+** sqlite3_status() routine needs access to this mutex.
40796
+*/
40797
+SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void){
40798
+ return pcache1.mutex;
40799
+}
4066140800
4066240801
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
4066340802
/*
4066440803
** This function is called to free superfluous dynamically allocated memory
4066540804
** held by the pager system. Memory in use by any SQLite pager allocated
@@ -49429,13 +49568,14 @@
4942949568
if( pWal->exclusiveMode ) return;
4943049569
(void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
4943149570
SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
4943249571
WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
4943349572
}
49434
-static int walLockExclusive(Wal *pWal, int lockIdx, int n){
49573
+static int walLockExclusive(Wal *pWal, int lockIdx, int n, int fBlock){
4943549574
int rc;
4943649575
if( pWal->exclusiveMode ) return SQLITE_OK;
49576
+ if( fBlock ) sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_WAL_BLOCK, 0);
4943749577
rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
4943849578
SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
4943949579
WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
4944049580
walLockName(lockIdx), n, rc ? "failed" : "ok"));
4944149581
VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
@@ -49717,11 +49857,11 @@
4971749857
assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
4971849858
assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
4971949859
assert( pWal->writeLock );
4972049860
iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
4972149861
nLock = SQLITE_SHM_NLOCK - iLock;
49722
- rc = walLockExclusive(pWal, iLock, nLock);
49862
+ rc = walLockExclusive(pWal, iLock, nLock, 0);
4972349863
if( rc ){
4972449864
return rc;
4972549865
}
4972649866
WALTRACE(("WAL%p: recovery begin...\n", pWal));
4972749867
@@ -50251,11 +50391,11 @@
5025150391
int lockIdx, /* Offset of first byte to lock */
5025250392
int n /* Number of bytes to lock */
5025350393
){
5025450394
int rc;
5025550395
do {
50256
- rc = walLockExclusive(pWal, lockIdx, n);
50396
+ rc = walLockExclusive(pWal, lockIdx, n, 0);
5025750397
}while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
5025850398
return rc;
5025950399
}
5026050400
5026150401
/*
@@ -50684,11 +50824,11 @@
5068450824
if( pWal->readOnly & WAL_SHM_RDONLY ){
5068550825
if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
5068650826
walUnlockShared(pWal, WAL_WRITE_LOCK);
5068750827
rc = SQLITE_READONLY_RECOVERY;
5068850828
}
50689
- }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
50829
+ }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1, 1)) ){
5069050830
pWal->writeLock = 1;
5069150831
if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
5069250832
badHdr = walIndexTryHdr(pWal, pChanged);
5069350833
if( badHdr ){
5069450834
/* If the wal-index header is still malformed even while holding
@@ -50890,11 +51030,11 @@
5089051030
{
5089151031
if( (pWal->readOnly & WAL_SHM_RDONLY)==0
5089251032
&& (mxReadMark<pWal->hdr.mxFrame || mxI==0)
5089351033
){
5089451034
for(i=1; i<WAL_NREADER; i++){
50895
- rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
51035
+ rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1, 0);
5089651036
if( rc==SQLITE_OK ){
5089751037
mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
5089851038
mxI = i;
5089951039
walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
5090051040
break;
@@ -51146,11 +51286,11 @@
5114651286
}
5114751287
5114851288
/* Only one writer allowed at a time. Get the write lock. Return
5114951289
** SQLITE_BUSY if unable.
5115051290
*/
51151
- rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
51291
+ rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1, 0);
5115251292
if( rc ){
5115351293
return rc;
5115451294
}
5115551295
pWal->writeLock = 1;
5115651296
@@ -51291,11 +51431,11 @@
5129151431
volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
5129251432
assert( pInfo->nBackfill==pWal->hdr.mxFrame );
5129351433
if( pInfo->nBackfill>0 ){
5129451434
u32 salt1;
5129551435
sqlite3_randomness(4, &salt1);
51296
- rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
51436
+ rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1, 0);
5129751437
if( rc==SQLITE_OK ){
5129851438
/* If all readers are using WAL_READ_LOCK(0) (in other words if no
5129951439
** readers are currently using the WAL), then the transactions
5130051440
** frames will overwrite the start of the existing log. Update the
5130151441
** wal-index header to reflect this.
@@ -51616,11 +51756,11 @@
5161651756
if( pWal->readOnly ) return SQLITE_READONLY;
5161751757
WALTRACE(("WAL%p: checkpoint begins\n", pWal));
5161851758
5161951759
/* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
5162051760
** "checkpoint" lock on the database file. */
51621
- rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
51761
+ rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1, 0);
5162251762
if( rc ){
5162351763
/* EVIDENCE-OF: R-10421-19736 If any other process is running a
5162451764
** checkpoint operation at the same time, the lock cannot be obtained and
5162551765
** SQLITE_BUSY is returned.
5162651766
** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
@@ -52618,10 +52758,11 @@
5261852758
5261952759
/*
5262052760
** Exit the recursive mutex on a Btree.
5262152761
*/
5262252762
SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
52763
+ assert( sqlite3_mutex_held(p->db->mutex) );
5262352764
if( p->sharable ){
5262452765
assert( p->wantToLock>0 );
5262552766
p->wantToLock--;
5262652767
if( p->wantToLock==0 ){
5262752768
unlockBtreeMutex(p);
@@ -54796,12 +54937,12 @@
5479654937
/*
5479754938
** The following asserts make sure that structures used by the btree are
5479854939
** the right size. This is to guard against size changes that result
5479954940
** when compiling on a different architecture.
5480054941
*/
54801
- assert( sizeof(i64)==8 || sizeof(i64)==4 );
54802
- assert( sizeof(u64)==8 || sizeof(u64)==4 );
54942
+ assert( sizeof(i64)==8 );
54943
+ assert( sizeof(u64)==8 );
5480354944
assert( sizeof(u32)==4 );
5480454945
assert( sizeof(u16)==2 );
5480554946
assert( sizeof(Pgno)==4 );
5480654947
5480754948
pBt = sqlite3MallocZero( sizeof(*pBt) );
@@ -60259,11 +60400,12 @@
6025960400
** the previous call, as the overflow cell data will have been
6026060401
** copied either into the body of a database page or into the new
6026160402
** pSpace buffer passed to the latter call to balance_nonroot().
6026260403
*/
6026360404
u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
60264
- rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints);
60405
+ rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
60406
+ pCur->hints&BTREE_BULKLOAD);
6026560407
if( pFree ){
6026660408
/* If pFree is not NULL, it points to the pSpace buffer used
6026760409
** by a previous call to balance_nonroot(). Its contents are
6026860410
** now stored either on real database pages or within the
6026960411
** new pSpace buffer, so it may be safely freed here. */
@@ -61922,17 +62064,26 @@
6192262064
pBt->btsFlags &= ~BTS_NO_WAL;
6192362065
return rc;
6192462066
}
6192562067
6192662068
/*
61927
-** set the mask of hint flags for cursor pCsr. Currently the only valid
61928
-** values are 0 and BTREE_BULKLOAD.
62069
+** set the mask of hint flags for cursor pCsr.
6192962070
*/
6193062071
SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
61931
- assert( mask==BTREE_BULKLOAD || mask==0 );
62072
+ assert( mask==BTREE_BULKLOAD || mask==BTREE_SEEK_EQ || mask==0 );
6193262073
pCsr->hints = mask;
6193362074
}
62075
+
62076
+#ifdef SQLITE_DEBUG
62077
+/*
62078
+** Return true if the cursor has a hint specified. This routine is
62079
+** only used from within assert() statements
62080
+*/
62081
+SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
62082
+ return (pCsr->hints & mask)!=0;
62083
+}
62084
+#endif
6193462085
6193562086
/*
6193662087
** Return true if the given Btree is read-only.
6193762088
*/
6193862089
SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
@@ -63822,11 +63973,11 @@
6382263973
** by calling sqlite3ValueNew().
6382363974
**
6382463975
** Otherwise, if the second argument is non-zero, then this function is
6382563976
** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
6382663977
** already been allocated, allocate the UnpackedRecord structure that
63827
-** that function will return to its caller here. Then return a pointer
63978
+** that function will return to its caller here. Then return a pointer to
6382863979
** an sqlite3_value within the UnpackedRecord.a[] array.
6382963980
*/
6383063981
static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
6383163982
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
6383263983
if( p ){
@@ -63866,10 +64017,117 @@
6386664017
UNUSED_PARAMETER(p);
6386764018
#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
6386864019
return sqlite3ValueNew(db);
6386964020
}
6387064021
64022
+/*
64023
+** The expression object indicated by the second argument is guaranteed
64024
+** to be a scalar SQL function. If
64025
+**
64026
+** * all function arguments are SQL literals,
64027
+** * the SQLITE_FUNC_CONSTANT function flag is set, and
64028
+** * the SQLITE_FUNC_NEEDCOLL function flag is not set,
64029
+**
64030
+** then this routine attempts to invoke the SQL function. Assuming no
64031
+** error occurs, output parameter (*ppVal) is set to point to a value
64032
+** object containing the result before returning SQLITE_OK.
64033
+**
64034
+** Affinity aff is applied to the result of the function before returning.
64035
+** If the result is a text value, the sqlite3_value object uses encoding
64036
+** enc.
64037
+**
64038
+** If the conditions above are not met, this function returns SQLITE_OK
64039
+** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
64040
+** NULL and an SQLite error code returned.
64041
+*/
64042
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
64043
+static int valueFromFunction(
64044
+ sqlite3 *db, /* The database connection */
64045
+ Expr *p, /* The expression to evaluate */
64046
+ u8 enc, /* Encoding to use */
64047
+ u8 aff, /* Affinity to use */
64048
+ sqlite3_value **ppVal, /* Write the new value here */
64049
+ struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
64050
+){
64051
+ sqlite3_context ctx; /* Context object for function invocation */
64052
+ sqlite3_value **apVal = 0; /* Function arguments */
64053
+ int nVal = 0; /* Size of apVal[] array */
64054
+ FuncDef *pFunc = 0; /* Function definition */
64055
+ sqlite3_value *pVal = 0; /* New value */
64056
+ int rc = SQLITE_OK; /* Return code */
64057
+ int nName; /* Size of function name in bytes */
64058
+ ExprList *pList = 0; /* Function arguments */
64059
+ int i; /* Iterator variable */
64060
+
64061
+ assert( pCtx!=0 );
64062
+ assert( (p->flags & EP_TokenOnly)==0 );
64063
+ pList = p->x.pList;
64064
+ if( pList ) nVal = pList->nExpr;
64065
+ nName = sqlite3Strlen30(p->u.zToken);
64066
+ pFunc = sqlite3FindFunction(db, p->u.zToken, nName, nVal, enc, 0);
64067
+ assert( pFunc );
64068
+ if( (pFunc->funcFlags & SQLITE_FUNC_CONSTANT)==0
64069
+ || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
64070
+ ){
64071
+ return SQLITE_OK;
64072
+ }
64073
+
64074
+ if( pList ){
64075
+ apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
64076
+ if( apVal==0 ){
64077
+ rc = SQLITE_NOMEM;
64078
+ goto value_from_function_out;
64079
+ }
64080
+ for(i=0; i<nVal; i++){
64081
+ rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
64082
+ if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out;
64083
+ }
64084
+ }
64085
+
64086
+ pVal = valueNew(db, pCtx);
64087
+ if( pVal==0 ){
64088
+ rc = SQLITE_NOMEM;
64089
+ goto value_from_function_out;
64090
+ }
64091
+
64092
+ assert( pCtx->pParse->rc==SQLITE_OK );
64093
+ memset(&ctx, 0, sizeof(ctx));
64094
+ ctx.pOut = pVal;
64095
+ ctx.pFunc = pFunc;
64096
+ pFunc->xFunc(&ctx, nVal, apVal);
64097
+ if( ctx.isError ){
64098
+ rc = ctx.isError;
64099
+ sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
64100
+ }else{
64101
+ sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8);
64102
+ assert( rc==SQLITE_OK );
64103
+ rc = sqlite3VdbeChangeEncoding(pVal, enc);
64104
+ if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){
64105
+ rc = SQLITE_TOOBIG;
64106
+ pCtx->pParse->nErr++;
64107
+ }
64108
+ }
64109
+ pCtx->pParse->rc = rc;
64110
+
64111
+ value_from_function_out:
64112
+ if( rc!=SQLITE_OK ){
64113
+ pVal = 0;
64114
+ }
64115
+ if( apVal ){
64116
+ for(i=0; i<nVal; i++){
64117
+ sqlite3ValueFree(apVal[i]);
64118
+ }
64119
+ sqlite3DbFree(db, apVal);
64120
+ }
64121
+
64122
+ *ppVal = pVal;
64123
+ return rc;
64124
+}
64125
+#else
64126
+# define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
64127
+#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
64128
+
6387164129
/*
6387264130
** Extract a value from the supplied expression in the manner described
6387364131
** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
6387464132
** using valueNew().
6387564133
**
@@ -63897,10 +64155,16 @@
6389764155
*ppVal = 0;
6389864156
return SQLITE_OK;
6389964157
}
6390064158
while( (op = pExpr->op)==TK_UPLUS ) pExpr = pExpr->pLeft;
6390164159
if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
64160
+
64161
+ /* Compressed expressions only appear when parsing the DEFAULT clause
64162
+ ** on a table column definition, and hence only when pCtx==0. This
64163
+ ** check ensures that an EP_TokenOnly expression is never passed down
64164
+ ** into valueFromFunction(). */
64165
+ assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
6390264166
6390364167
if( op==TK_CAST ){
6390464168
u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
6390564169
rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
6390664170
testcase( rc!=SQLITE_OK );
@@ -63973,10 +64237,16 @@
6397364237
assert( zVal[nVal]=='\'' );
6397464238
sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
6397564239
0, SQLITE_DYNAMIC);
6397664240
}
6397764241
#endif
64242
+
64243
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
64244
+ else if( op==TK_FUNCTION && pCtx!=0 ){
64245
+ rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
64246
+ }
64247
+#endif
6397864248
6397964249
*ppVal = pVal;
6398064250
return rc;
6398164251
6398264252
no_mem:
@@ -65426,11 +65696,11 @@
6542665696
break;
6542765697
}
6542865698
#ifndef SQLITE_OMIT_VIRTUALTABLE
6542965699
case P4_VTAB: {
6543065700
sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
65431
- sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
65701
+ sqlite3_snprintf(nTemp, zTemp, "vtab:%p", pVtab);
6543265702
break;
6543365703
}
6543465704
#endif
6543565705
case P4_INTARRAY: {
6543665706
sqlite3_snprintf(nTemp, zTemp, "intarray");
@@ -66090,13 +66360,13 @@
6609066360
}
6609166361
#ifndef SQLITE_OMIT_VIRTUALTABLE
6609266362
else if( pCx->pVtabCursor ){
6609366363
sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
6609466364
const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
66095
- p->inVtabMethod = 1;
66365
+ assert( pVtabCursor->pVtab->nRef>0 );
66366
+ pVtabCursor->pVtab->nRef--;
6609666367
pModule->xClose(pVtabCursor);
66097
- p->inVtabMethod = 0;
6609866368
}
6609966369
#endif
6610066370
}
6610166371
6610266372
/*
@@ -66451,11 +66721,11 @@
6645166721
6645266722
/* Delete the master journal file. This commits the transaction. After
6645366723
** doing this the directory is synced again before any individual
6645466724
** transaction files are deleted.
6645566725
*/
66456
- rc = sqlite3OsDelete(pVfs, zMaster, 1);
66726
+ rc = sqlite3OsDelete(pVfs, zMaster, needSync);
6645766727
sqlite3DbFree(db, zMaster);
6645866728
zMaster = 0;
6645966729
if( rc ){
6646066730
return rc;
6646166731
}
@@ -68864,11 +69134,11 @@
6886469134
}
6886569135
SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
6886669136
pCtx->isError = errCode;
6886769137
pCtx->fErrorOrAux = 1;
6886869138
#ifdef SQLITE_DEBUG
68869
- pCtx->pVdbe->rcApp = errCode;
69139
+ if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
6887069140
#endif
6887169141
if( pCtx->pOut->flags & MEM_Null ){
6887269142
sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
6887369143
SQLITE_UTF8, SQLITE_STATIC);
6887469144
}
@@ -69127,20 +69397,30 @@
6912769397
assert( p && p->pFunc );
6912869398
return p->pOut->db;
6912969399
}
6913069400
6913169401
/*
69132
-** Return the current time for a statement
69402
+** Return the current time for a statement. If the current time
69403
+** is requested more than once within the same run of a single prepared
69404
+** statement, the exact same time is returned for each invocation regardless
69405
+** of the amount of time that elapses between invocations. In other words,
69406
+** the time returned is always the time of the first call.
6913369407
*/
6913469408
SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
69135
- Vdbe *v = p->pVdbe;
6913669409
int rc;
69137
- if( v->iCurrentTime==0 ){
69138
- rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, &v->iCurrentTime);
69139
- if( rc ) v->iCurrentTime = 0;
69410
+#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
69411
+ sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
69412
+ assert( p->pVdbe!=0 );
69413
+#else
69414
+ sqlite3_int64 iTime = 0;
69415
+ sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
69416
+#endif
69417
+ if( *piTime==0 ){
69418
+ rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
69419
+ if( rc ) *piTime = 0;
6914069420
}
69141
- return v->iCurrentTime;
69421
+ return *piTime;
6914269422
}
6914369423
6914469424
/*
6914569425
** The following is the implementation of an SQL function that always
6914669426
** fails with an error message stating that the function is used in the
@@ -69206,10 +69486,15 @@
6920669486
*/
6920769487
SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
6920869488
AuxData *pAuxData;
6920969489
6921069490
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
69491
+#if SQLITE_ENABLE_STAT3_OR_STAT4
69492
+ if( pCtx->pVdbe==0 ) return 0;
69493
+#else
69494
+ assert( pCtx->pVdbe!=0 );
69495
+#endif
6921169496
for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
6921269497
if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
6921369498
}
6921469499
6921569500
return (pAuxData ? pAuxData->pAux : 0);
@@ -69229,10 +69514,15 @@
6922969514
AuxData *pAuxData;
6923069515
Vdbe *pVdbe = pCtx->pVdbe;
6923169516
6923269517
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
6923369518
if( iArg<0 ) goto failed;
69519
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
69520
+ if( pVdbe==0 ) goto failed;
69521
+#else
69522
+ assert( pVdbe!=0 );
69523
+#endif
6923469524
6923569525
for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
6923669526
if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
6923769527
}
6923869528
if( pAuxData==0 ){
@@ -71845,11 +72135,11 @@
7184572135
** max() aggregate will set to 1 if the current row is not the minimum or
7184672136
** maximum. The P1 register is initialized to 0 by this instruction.
7184772137
**
7184872138
** The interface used by the implementation of the aforementioned functions
7184972139
** to retrieve the collation sequence set by this opcode is not available
71850
-** publicly, only to user functions defined in func.c.
72140
+** publicly. Only built-in functions have access to this feature.
7185172141
*/
7185272142
case OP_CollSeq: {
7185372143
assert( pOp->p4type==P4_COLLSEQ );
7185472144
if( pOp->p1 ){
7185572145
sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
@@ -73563,35 +73853,33 @@
7356373853
** cursors or a single read/write cursor but not both.
7356473854
**
7356573855
** See also OpenRead.
7356673856
*/
7356773857
case OP_ReopenIdx: {
73568
- VdbeCursor *pCur;
73569
-
73570
- assert( pOp->p5==0 );
73571
- assert( pOp->p4type==P4_KEYINFO );
73572
- pCur = p->apCsr[pOp->p1];
73573
- if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
73574
- assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
73575
- break;
73576
- }
73577
- /* If the cursor is not currently open or is open on a different
73578
- ** index, then fall through into OP_OpenRead to force a reopen */
73579
-}
73580
-case OP_OpenRead:
73581
-case OP_OpenWrite: {
7358273858
int nField;
7358373859
KeyInfo *pKeyInfo;
7358473860
int p2;
7358573861
int iDb;
7358673862
int wrFlag;
7358773863
Btree *pX;
7358873864
VdbeCursor *pCur;
7358973865
Db *pDb;
7359073866
73591
- assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
73592
- assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
73867
+ assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
73868
+ assert( pOp->p4type==P4_KEYINFO );
73869
+ pCur = p->apCsr[pOp->p1];
73870
+ if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
73871
+ assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
73872
+ goto open_cursor_set_hints;
73873
+ }
73874
+ /* If the cursor is not currently open or is open on a different
73875
+ ** index, then fall through into OP_OpenRead to force a reopen */
73876
+case OP_OpenRead:
73877
+case OP_OpenWrite:
73878
+
73879
+ assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR|OPFLAG_SEEKEQ))==pOp->p5 );
73880
+ assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
7359373881
assert( p->bIsReader );
7359473882
assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
7359573883
|| p->readOnly==0 );
7359673884
7359773885
if( p->expired ){
@@ -73650,18 +73938,21 @@
7365073938
pCur->nullRow = 1;
7365173939
pCur->isOrdered = 1;
7365273940
pCur->pgnoRoot = p2;
7365373941
rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
7365473942
pCur->pKeyInfo = pKeyInfo;
73655
- assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
73656
- sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
73657
-
7365873943
/* Set the VdbeCursor.isTable variable. Previous versions of
7365973944
** SQLite used to check if the root-page flags were sane at this point
7366073945
** and report database corruption if they were not, but this check has
7366173946
** since moved into the btree layer. */
7366273947
pCur->isTable = pOp->p4type!=P4_KEYINFO;
73948
+
73949
+open_cursor_set_hints:
73950
+ assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
73951
+ assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
73952
+ sqlite3BtreeCursorHints(pCur->pCursor,
73953
+ (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
7366373954
break;
7366473955
}
7366573956
7366673957
/* Opcode: OpenEphemeral P1 P2 * P4 P5
7366773958
** Synopsis: nColumn=P2
@@ -73918,10 +74209,26 @@
7391874209
oc = pOp->opcode;
7391974210
pC->nullRow = 0;
7392074211
#ifdef SQLITE_DEBUG
7392174212
pC->seekOp = pOp->opcode;
7392274213
#endif
74214
+
74215
+ /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
74216
+ ** OP_SeekLE opcodes are allowed, and these must be immediately followed
74217
+ ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
74218
+ */
74219
+#ifdef SQLITE_DEBUG
74220
+ if( sqlite3BtreeCursorHasHint(pC->pCursor, BTREE_SEEK_EQ) ){
74221
+ assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
74222
+ assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
74223
+ assert( pOp[1].p1==pOp[0].p1 );
74224
+ assert( pOp[1].p2==pOp[0].p2 );
74225
+ assert( pOp[1].p3==pOp[0].p3 );
74226
+ assert( pOp[1].p4.i==pOp[0].p4.i );
74227
+ }
74228
+#endif
74229
+
7392374230
if( pC->isTable ){
7392474231
/* The input value in P3 might be of any type: integer, real, string,
7392574232
** blob, or NULL. But it needs to be an integer before we can do
7392674233
** the seek, so convert it. */
7392774234
pIn3 = &aMem[pOp->p3];
@@ -75257,34 +75564,19 @@
7525775564
**
7525875565
** See also: Clear
7525975566
*/
7526075567
case OP_Destroy: { /* out2-prerelease */
7526175568
int iMoved;
75262
- int iCnt;
75263
- Vdbe *pVdbe;
7526475569
int iDb;
7526575570
7526675571
assert( p->readOnly==0 );
75267
-#ifndef SQLITE_OMIT_VIRTUALTABLE
75268
- iCnt = 0;
75269
- for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
75270
- if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader
75271
- && pVdbe->inVtabMethod<2 && pVdbe->pc>=0
75272
- ){
75273
- iCnt++;
75274
- }
75275
- }
75276
-#else
75277
- iCnt = db->nVdbeRead;
75278
-#endif
7527975572
pOut->flags = MEM_Null;
75280
- if( iCnt>1 ){
75573
+ if( db->nVdbeRead > db->nVDestroy+1 ){
7528175574
rc = SQLITE_LOCKED;
7528275575
p->errorAction = OE_Abort;
7528375576
}else{
7528475577
iDb = pOp->p3;
75285
- assert( iCnt==1 );
7528675578
assert( DbMaskTest(p->btreeMask, iDb) );
7528775579
iMoved = 0; /* Not needed. Only to silence a warning. */
7528875580
rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
7528975581
pOut->flags = MEM_Int;
7529075582
pOut->u.i = iMoved;
@@ -76337,17 +76629,33 @@
7633776629
break;
7633876630
}
7633976631
#endif /* SQLITE_OMIT_VIRTUALTABLE */
7634076632
7634176633
#ifndef SQLITE_OMIT_VIRTUALTABLE
76342
-/* Opcode: VCreate P1 * * P4 *
76634
+/* Opcode: VCreate P1 P2 * * *
7634376635
**
76344
-** P4 is the name of a virtual table in database P1. Call the xCreate method
76345
-** for that table.
76636
+** P2 is a register that holds the name of a virtual table in database
76637
+** P1. Call the xCreate method for that table.
7634676638
*/
7634776639
case OP_VCreate: {
76348
- rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
76640
+ Mem sMem; /* For storing the record being decoded */
76641
+ const char *zTab; /* Name of the virtual table */
76642
+
76643
+ memset(&sMem, 0, sizeof(sMem));
76644
+ sMem.db = db;
76645
+ /* Because P2 is always a static string, it is impossible for the
76646
+ ** sqlite3VdbeMemCopy() to fail */
76647
+ assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
76648
+ assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
76649
+ rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
76650
+ assert( rc==SQLITE_OK );
76651
+ zTab = (const char*)sqlite3_value_text(&sMem);
76652
+ assert( zTab || db->mallocFailed );
76653
+ if( zTab ){
76654
+ rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
76655
+ }
76656
+ sqlite3VdbeMemRelease(&sMem);
7634976657
break;
7635076658
}
7635176659
#endif /* SQLITE_OMIT_VIRTUALTABLE */
7635276660
7635376661
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -76355,13 +76663,13 @@
7635576663
**
7635676664
** P4 is the name of a virtual table in database P1. Call the xDestroy method
7635776665
** of that table.
7635876666
*/
7635976667
case OP_VDestroy: {
76360
- p->inVtabMethod = 2;
76668
+ db->nVDestroy++;
7636176669
rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
76362
- p->inVtabMethod = 0;
76670
+ db->nVDestroy--;
7636376671
break;
7636476672
}
7636576673
#endif /* SQLITE_OMIT_VIRTUALTABLE */
7636676674
7636776675
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -76373,18 +76681,21 @@
7637376681
*/
7637476682
case OP_VOpen: {
7637576683
VdbeCursor *pCur;
7637676684
sqlite3_vtab_cursor *pVtabCursor;
7637776685
sqlite3_vtab *pVtab;
76378
- sqlite3_module *pModule;
76686
+ const sqlite3_module *pModule;
7637976687
7638076688
assert( p->bIsReader );
7638176689
pCur = 0;
7638276690
pVtabCursor = 0;
7638376691
pVtab = pOp->p4.pVtab->pVtab;
76384
- pModule = (sqlite3_module *)pVtab->pModule;
76385
- assert(pVtab && pModule);
76692
+ if( pVtab==0 || NEVER(pVtab->pModule==0) ){
76693
+ rc = SQLITE_LOCKED;
76694
+ break;
76695
+ }
76696
+ pModule = pVtab->pModule;
7638676697
rc = pModule->xOpen(pVtab, &pVtabCursor);
7638776698
sqlite3VtabImportErrmsg(p, pVtab);
7638876699
if( SQLITE_OK==rc ){
7638976700
/* Initialize sqlite3_vtab_cursor base class */
7639076701
pVtabCursor->pVtab = pVtab;
@@ -76391,10 +76702,11 @@
7639176702
7639276703
/* Initialize vdbe cursor object */
7639376704
pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
7639476705
if( pCur ){
7639576706
pCur->pVtabCursor = pVtabCursor;
76707
+ pVtab->nRef++;
7639676708
}else{
7639776709
db->mallocFailed = 1;
7639876710
pModule->xClose(pVtabCursor);
7639976711
}
7640076712
}
@@ -76456,13 +76768,11 @@
7645676768
apArg = p->apArg;
7645776769
for(i = 0; i<nArg; i++){
7645876770
apArg[i] = &pArgc[i+1];
7645976771
}
7646076772
76461
- p->inVtabMethod = 1;
7646276773
rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
76463
- p->inVtabMethod = 0;
7646476774
sqlite3VtabImportErrmsg(p, pVtab);
7646576775
if( rc==SQLITE_OK ){
7646676776
res = pModule->xEof(pVtabCursor);
7646776777
}
7646876778
VdbeBranchTaken(res!=0,2);
@@ -76548,13 +76858,11 @@
7654876858
** underlying implementation to return an error if one occurs during
7654976859
** xNext(). Instead, if an error occurs, true is returned (indicating that
7655076860
** data is available) and the error code returned when xColumn or
7655176861
** some other method is next invoked on the save virtual table cursor.
7655276862
*/
76553
- p->inVtabMethod = 1;
7655476863
rc = pModule->xNext(pCur->pVtabCursor);
76555
- p->inVtabMethod = 0;
7655676864
sqlite3VtabImportErrmsg(p, pVtab);
7655776865
if( rc==SQLITE_OK ){
7655876866
res = pModule->xEof(pCur->pVtabCursor);
7655976867
}
7656076868
VdbeBranchTaken(!res,2);
@@ -76625,11 +76933,11 @@
7662576933
** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
7662676934
** apply in the case of a constraint failure on an insert or update.
7662776935
*/
7662876936
case OP_VUpdate: {
7662976937
sqlite3_vtab *pVtab;
76630
- sqlite3_module *pModule;
76938
+ const sqlite3_module *pModule;
7663176939
int nArg;
7663276940
int i;
7663376941
sqlite_int64 rowid;
7663476942
Mem **apArg;
7663576943
Mem *pX;
@@ -76637,11 +76945,15 @@
7663776945
assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
7663876946
|| pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
7663976947
);
7664076948
assert( p->readOnly==0 );
7664176949
pVtab = pOp->p4.pVtab->pVtab;
76642
- pModule = (sqlite3_module *)pVtab->pModule;
76950
+ if( pVtab==0 || NEVER(pVtab->pModule==0) ){
76951
+ rc = SQLITE_LOCKED;
76952
+ break;
76953
+ }
76954
+ pModule = pVtab->pModule;
7664376955
nArg = pOp->p2;
7664476956
assert( pOp->p4type==P4_VTAB );
7664576957
if( ALWAYS(pModule->xUpdate) ){
7664676958
u8 vtabOnConflict = db->vtabOnConflict;
7664776959
apArg = p->apArg;
@@ -78507,10 +78819,11 @@
7850778819
sqlite3 *db, /* Database handle doing sort */
7850878820
i64 nExtend, /* Attempt to extend file to this size */
7850978821
sqlite3_file **ppFd
7851078822
){
7851178823
int rc;
78824
+ if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS;
7851278825
rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
7851378826
SQLITE_OPEN_TEMP_JOURNAL |
7851478827
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
7851578828
SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &rc
7851678829
);
@@ -82098,14 +82411,15 @@
8209882411
** and the pExpr parameter is returned unchanged.
8209982412
*/
8210082413
SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(
8210182414
Parse *pParse, /* Parsing context */
8210282415
Expr *pExpr, /* Add the "COLLATE" clause to this expression */
82103
- const Token *pCollName /* Name of collating sequence */
82416
+ const Token *pCollName, /* Name of collating sequence */
82417
+ int dequote /* True to dequote pCollName */
8210482418
){
8210582419
if( pCollName->n>0 ){
82106
- Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
82420
+ Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
8210782421
if( pNew ){
8210882422
pNew->pLeft = pExpr;
8210982423
pNew->flags |= EP_Collate|EP_Skip;
8211082424
pExpr = pNew;
8211182425
}
@@ -82115,11 +82429,11 @@
8211582429
SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
8211682430
Token s;
8211782431
assert( zC!=0 );
8211882432
s.z = zC;
8211982433
s.n = sqlite3Strlen30(s.z);
82120
- return sqlite3ExprAddCollateToken(pParse, pExpr, &s);
82434
+ return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
8212182435
}
8212282436
8212382437
/*
8212482438
** Skip over any TK_COLLATE or TK_AS operators and any unlikely()
8212582439
** or likelihood() function at the root of an expression.
@@ -82425,10 +82739,11 @@
8242582739
**
8242682740
** Also propagate all EP_Propagate flags from the Expr.x.pList into
8242782741
** Expr.flags.
8242882742
*/
8242982743
SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
82744
+ if( pParse->nErr ) return;
8243082745
exprSetHeight(p);
8243182746
sqlite3ExprCheckHeight(pParse, p->nHeight);
8243282747
}
8243382748
8243482749
/*
@@ -87139,11 +87454,14 @@
8713987454
/* Ensure the default expression is something that sqlite3ValueFromExpr()
8714087455
** can handle (i.e. not CURRENT_TIME etc.)
8714187456
*/
8714287457
if( pDflt ){
8714387458
sqlite3_value *pVal = 0;
87144
- if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
87459
+ int rc;
87460
+ rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal);
87461
+ assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
87462
+ if( rc!=SQLITE_OK ){
8714587463
db->mallocFailed = 1;
8714687464
return;
8714787465
}
8714887466
if( !pVal ){
8714987467
sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
@@ -93202,10 +93520,11 @@
9320293520
}
9320393521
if( pIdx->onError==OE_Default ){
9320493522
pIdx->onError = pIndex->onError;
9320593523
}
9320693524
}
93525
+ pRet = pIdx;
9320793526
goto exit_create_index;
9320893527
}
9320993528
}
9321093529
}
9321193530
@@ -95661,11 +95980,13 @@
9566195980
9566295981
/*
9566395982
** Return the collating function associated with a function.
9566495983
*/
9566595984
static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
95666
- VdbeOp *pOp = &context->pVdbe->aOp[context->iOp-1];
95985
+ VdbeOp *pOp;
95986
+ assert( context->pVdbe!=0 );
95987
+ pOp = &context->pVdbe->aOp[context->iOp-1];
9566795988
assert( pOp->opcode==OP_CollSeq );
9566895989
assert( pOp->p4type==P4_COLLSEQ );
9566995990
return pOp->p4.pColl;
9567095991
}
9567195992
@@ -109678,11 +109999,11 @@
109678109999
sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
109679110000
109680110001
for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
109681110002
pEList = pLeft->pEList;
109682110003
if( pCte->pCols ){
109683
- if( pEList->nExpr!=pCte->pCols->nExpr ){
110004
+ if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
109684110005
sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
109685110006
pCte->zName, pEList->nExpr, pCte->pCols->nExpr
109686110007
);
109687110008
pParse->pWith = pSavedWith;
109688110009
return SQLITE_ERROR;
@@ -114104,10 +114425,11 @@
114104114425
*/
114105114426
if( !db->init.busy ){
114106114427
char *zStmt;
114107114428
char *zWhere;
114108114429
int iDb;
114430
+ int iReg;
114109114431
Vdbe *v;
114110114432
114111114433
/* Compute the complete text of the CREATE VIRTUAL TABLE statement */
114112114434
if( pEnd ){
114113114435
pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
@@ -114138,12 +114460,14 @@
114138114460
sqlite3ChangeCookie(pParse, iDb);
114139114461
114140114462
sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
114141114463
zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
114142114464
sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
114143
- sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
114144
- pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
114465
+
114466
+ iReg = ++pParse->nMem;
114467
+ sqlite3VdbeAddOp4(v, OP_String8, 0, iReg, 0, pTab->zName, 0);
114468
+ sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg);
114145114469
}
114146114470
114147114471
/* If we are rereading the sqlite_master table create the in-memory
114148114472
** record of the table. The xConnect() method is not called until
114149114473
** the first time the virtual table is used in an SQL statement. This
@@ -114492,15 +114816,19 @@
114492114816
int rc = SQLITE_OK;
114493114817
Table *pTab;
114494114818
114495114819
pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
114496114820
if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
114497
- VTable *p = vtabDisconnectAll(db, pTab);
114498
-
114499
- assert( rc==SQLITE_OK );
114821
+ VTable *p;
114822
+ for(p=pTab->pVTable; p; p=p->pNext){
114823
+ assert( p->pVtab );
114824
+ if( p->pVtab->nRef>0 ){
114825
+ return SQLITE_LOCKED;
114826
+ }
114827
+ }
114828
+ p = vtabDisconnectAll(db, pTab);
114500114829
rc = p->pMod->pModule->xDestroy(p->pVtab);
114501
-
114502114830
/* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
114503114831
if( rc==SQLITE_OK ){
114504114832
assert( pTab->pVTable==p && p->pNext==0 );
114505114833
p->pVtab = 0;
114506114834
pTab->pVTable = 0;
@@ -116078,10 +116406,83 @@
116078116406
static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
116079116407
pWC->a[iChild].iParent = iParent;
116080116408
pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
116081116409
pWC->a[iParent].nChild++;
116082116410
}
116411
+
116412
+/*
116413
+** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
116414
+** a conjunction, then return just pTerm when N==0. If N is exceeds
116415
+** the number of available subterms, return NULL.
116416
+*/
116417
+static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
116418
+ if( pTerm->eOperator!=WO_AND ){
116419
+ return N==0 ? pTerm : 0;
116420
+ }
116421
+ if( N<pTerm->u.pAndInfo->wc.nTerm ){
116422
+ return &pTerm->u.pAndInfo->wc.a[N];
116423
+ }
116424
+ return 0;
116425
+}
116426
+
116427
+/*
116428
+** Subterms pOne and pTwo are contained within WHERE clause pWC. The
116429
+** two subterms are in disjunction - they are OR-ed together.
116430
+**
116431
+** If these two terms are both of the form: "A op B" with the same
116432
+** A and B values but different operators and if the operators are
116433
+** compatible (if one is = and the other is <, for example) then
116434
+** add a new virtual AND term to pWC that is the combination of the
116435
+** two.
116436
+**
116437
+** Some examples:
116438
+**
116439
+** x<y OR x=y --> x<=y
116440
+** x=y OR x=y --> x=y
116441
+** x<=y OR x<y --> x<=y
116442
+**
116443
+** The following is NOT generated:
116444
+**
116445
+** x<y OR x>y --> x!=y
116446
+*/
116447
+static void whereCombineDisjuncts(
116448
+ SrcList *pSrc, /* the FROM clause */
116449
+ WhereClause *pWC, /* The complete WHERE clause */
116450
+ WhereTerm *pOne, /* First disjunct */
116451
+ WhereTerm *pTwo /* Second disjunct */
116452
+){
116453
+ u16 eOp = pOne->eOperator | pTwo->eOperator;
116454
+ sqlite3 *db; /* Database connection (for malloc) */
116455
+ Expr *pNew; /* New virtual expression */
116456
+ int op; /* Operator for the combined expression */
116457
+ int idxNew; /* Index in pWC of the next virtual term */
116458
+
116459
+ if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
116460
+ if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
116461
+ if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
116462
+ && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
116463
+ assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
116464
+ assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
116465
+ if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
116466
+ if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return;
116467
+ /* If we reach this point, it means the two subterms can be combined */
116468
+ if( (eOp & (eOp-1))!=0 ){
116469
+ if( eOp & (WO_LT|WO_LE) ){
116470
+ eOp = WO_LE;
116471
+ }else{
116472
+ assert( eOp & (WO_GT|WO_GE) );
116473
+ eOp = WO_GE;
116474
+ }
116475
+ }
116476
+ db = pWC->pWInfo->pParse->db;
116477
+ pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
116478
+ if( pNew==0 ) return;
116479
+ for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
116480
+ pNew->op = op;
116481
+ idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
116482
+ exprAnalyze(pSrc, pWC, idxNew);
116483
+}
116083116484
116084116485
#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
116085116486
/*
116086116487
** Analyze a term that consists of two or more OR-connected
116087116488
** subterms. So in:
@@ -116103,10 +116504,11 @@
116103116504
** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
116104116505
** (B) x=expr1 OR expr2=x OR x=expr3
116105116506
** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
116106116507
** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
116107116508
** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
116509
+** (F) x>A OR (x=A AND y>=B)
116108116510
**
116109116511
** CASE 1:
116110116512
**
116111116513
** If all subterms are of the form T.C=expr for some single column of C and
116112116514
** a single table T (as shown in example B above) then create a new virtual
@@ -116118,10 +116520,20 @@
116118116520
** then create a new virtual term like this:
116119116521
**
116120116522
** x IN (expr1,expr2,expr3)
116121116523
**
116122116524
** CASE 2:
116525
+**
116526
+** If there are exactly two disjuncts one side has x>A and the other side
116527
+** has x=A (for the same x and A) then add a new virtual conjunct term to the
116528
+** WHERE clause of the form "x>=A". Example:
116529
+**
116530
+** x>A OR (x=A AND y>B) adds: x>=A
116531
+**
116532
+** The added conjunct can sometimes be helpful in query planning.
116533
+**
116534
+** CASE 3:
116123116535
**
116124116536
** If all subterms are indexable by a single table T, then set
116125116537
**
116126116538
** WhereTerm.eOperator = WO_OR
116127116539
** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
@@ -116245,15 +116657,29 @@
116245116657
}
116246116658
}
116247116659
}
116248116660
116249116661
/*
116250
- ** Record the set of tables that satisfy case 2. The set might be
116662
+ ** Record the set of tables that satisfy case 3. The set might be
116251116663
** empty.
116252116664
*/
116253116665
pOrInfo->indexable = indexable;
116254116666
pTerm->eOperator = indexable==0 ? 0 : WO_OR;
116667
+
116668
+ /* For a two-way OR, attempt to implementation case 2.
116669
+ */
116670
+ if( indexable && pOrWc->nTerm==2 ){
116671
+ int iOne = 0;
116672
+ WhereTerm *pOne;
116673
+ while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
116674
+ int iTwo = 0;
116675
+ WhereTerm *pTwo;
116676
+ while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
116677
+ whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
116678
+ }
116679
+ }
116680
+ }
116255116681
116256116682
/*
116257116683
** chngToIN holds a set of tables that *might* satisfy case 1. But
116258116684
** we have to do some additional checking to see if case 1 really
116259116685
** is satisfied.
@@ -116380,11 +116806,11 @@
116380116806
pTerm = &pWC->a[idxTerm];
116381116807
markTermAsChild(pWC, idxNew, idxTerm);
116382116808
}else{
116383116809
sqlite3ExprListDelete(db, pList);
116384116810
}
116385
- pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */
116811
+ pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */
116386116812
}
116387116813
}
116388116814
}
116389116815
#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
116390116816
@@ -116575,11 +117001,11 @@
116575117001
Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
116576117002
Expr *pNewExpr1;
116577117003
Expr *pNewExpr2;
116578117004
int idxNew1;
116579117005
int idxNew2;
116580
- Token sCollSeqName; /* Name of collating sequence */
117006
+ const char *zCollSeqName; /* Name of collating sequence */
116581117007
const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
116582117008
116583117009
pLeft = pExpr->x.pList->a[1].pExpr;
116584117010
pStr2 = sqlite3ExprDup(db, pStr1, 0);
116585117011
@@ -116611,23 +117037,22 @@
116611117037
if( c=='A'-1 ) isComplete = 0;
116612117038
c = sqlite3UpperToLower[c];
116613117039
}
116614117040
*pC = c + 1;
116615117041
}
116616
- sCollSeqName.z = noCase ? "NOCASE" : "BINARY";
116617
- sCollSeqName.n = 6;
117042
+ zCollSeqName = noCase ? "NOCASE" : "BINARY";
116618117043
pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
116619117044
pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
116620
- sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
117045
+ sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
116621117046
pStr1, 0);
116622117047
transferJoinMarkings(pNewExpr1, pExpr);
116623117048
idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
116624117049
testcase( idxNew1==0 );
116625117050
exprAnalyze(pSrc, pWC, idxNew1);
116626117051
pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
116627117052
pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
116628
- sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
117053
+ sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
116629117054
pStr2, 0);
116630117055
transferJoinMarkings(pNewExpr2, pExpr);
116631117056
idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
116632117057
testcase( idxNew2==0 );
116633117058
exprAnalyze(pSrc, pWC, idxNew2);
@@ -117240,15 +117665,18 @@
117240117665
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
117241117666
/*
117242117667
** Estimate the location of a particular key among all keys in an
117243117668
** index. Store the results in aStat as follows:
117244117669
**
117245
-** aStat[0] Est. number of rows less than pVal
117246
-** aStat[1] Est. number of rows equal to pVal
117670
+** aStat[0] Est. number of rows less than pRec
117671
+** aStat[1] Est. number of rows equal to pRec
117247117672
**
117248117673
** Return the index of the sample that is the smallest sample that
117249
-** is greater than or equal to pRec.
117674
+** is greater than or equal to pRec. Note that this index is not an index
117675
+** into the aSample[] array - it is an index into a virtual set of samples
117676
+** based on the contents of aSample[] and the number of fields in record
117677
+** pRec.
117250117678
*/
117251117679
static int whereKeyStats(
117252117680
Parse *pParse, /* Database connection */
117253117681
Index *pIdx, /* Index to consider domain of */
117254117682
UnpackedRecord *pRec, /* Vector of values to consider */
@@ -117255,71 +117683,162 @@
117255117683
int roundUp, /* Round up if true. Round down if false */
117256117684
tRowcnt *aStat /* OUT: stats written here */
117257117685
){
117258117686
IndexSample *aSample = pIdx->aSample;
117259117687
int iCol; /* Index of required stats in anEq[] etc. */
117688
+ int i; /* Index of first sample >= pRec */
117689
+ int iSample; /* Smallest sample larger than or equal to pRec */
117260117690
int iMin = 0; /* Smallest sample not yet tested */
117261
- int i = pIdx->nSample; /* Smallest sample larger than or equal to pRec */
117262117691
int iTest; /* Next sample to test */
117263117692
int res; /* Result of comparison operation */
117693
+ int nField; /* Number of fields in pRec */
117694
+ tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */
117264117695
117265117696
#ifndef SQLITE_DEBUG
117266117697
UNUSED_PARAMETER( pParse );
117267117698
#endif
117268117699
assert( pRec!=0 );
117269
- iCol = pRec->nField - 1;
117270117700
assert( pIdx->nSample>0 );
117271
- assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
117701
+ assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol );
117702
+
117703
+ /* Do a binary search to find the first sample greater than or equal
117704
+ ** to pRec. If pRec contains a single field, the set of samples to search
117705
+ ** is simply the aSample[] array. If the samples in aSample[] contain more
117706
+ ** than one fields, all fields following the first are ignored.
117707
+ **
117708
+ ** If pRec contains N fields, where N is more than one, then as well as the
117709
+ ** samples in aSample[] (truncated to N fields), the search also has to
117710
+ ** consider prefixes of those samples. For example, if the set of samples
117711
+ ** in aSample is:
117712
+ **
117713
+ ** aSample[0] = (a, 5)
117714
+ ** aSample[1] = (a, 10)
117715
+ ** aSample[2] = (b, 5)
117716
+ ** aSample[3] = (c, 100)
117717
+ ** aSample[4] = (c, 105)
117718
+ **
117719
+ ** Then the search space should ideally be the samples above and the
117720
+ ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
117721
+ ** the code actually searches this set:
117722
+ **
117723
+ ** 0: (a)
117724
+ ** 1: (a, 5)
117725
+ ** 2: (a, 10)
117726
+ ** 3: (a, 10)
117727
+ ** 4: (b)
117728
+ ** 5: (b, 5)
117729
+ ** 6: (c)
117730
+ ** 7: (c, 100)
117731
+ ** 8: (c, 105)
117732
+ ** 9: (c, 105)
117733
+ **
117734
+ ** For each sample in the aSample[] array, N samples are present in the
117735
+ ** effective sample array. In the above, samples 0 and 1 are based on
117736
+ ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
117737
+ **
117738
+ ** Often, sample i of each block of N effective samples has (i+1) fields.
117739
+ ** Except, each sample may be extended to ensure that it is greater than or
117740
+ ** equal to the previous sample in the array. For example, in the above,
117741
+ ** sample 2 is the first sample of a block of N samples, so at first it
117742
+ ** appears that it should be 1 field in size. However, that would make it
117743
+ ** smaller than sample 1, so the binary search would not work. As a result,
117744
+ ** it is extended to two fields. The duplicates that this creates do not
117745
+ ** cause any problems.
117746
+ */
117747
+ nField = pRec->nField;
117748
+ iCol = 0;
117749
+ iSample = pIdx->nSample * nField;
117272117750
do{
117273
- iTest = (iMin+i)/2;
117274
- res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
117751
+ int iSamp; /* Index in aSample[] of test sample */
117752
+ int n; /* Number of fields in test sample */
117753
+
117754
+ iTest = (iMin+iSample)/2;
117755
+ iSamp = iTest / nField;
117756
+ if( iSamp>0 ){
117757
+ /* The proposed effective sample is a prefix of sample aSample[iSamp].
117758
+ ** Specifically, the shortest prefix of at least (1 + iTest%nField)
117759
+ ** fields that is greater than the previous effective sample. */
117760
+ for(n=(iTest % nField) + 1; n<nField; n++){
117761
+ if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
117762
+ }
117763
+ }else{
117764
+ n = iTest + 1;
117765
+ }
117766
+
117767
+ pRec->nField = n;
117768
+ res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec);
117275117769
if( res<0 ){
117770
+ iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
117771
+ iMin = iTest+1;
117772
+ }else if( res==0 && n<nField ){
117773
+ iLower = aSample[iSamp].anLt[n-1];
117276117774
iMin = iTest+1;
117775
+ res = -1;
117277117776
}else{
117278
- i = iTest;
117777
+ iSample = iTest;
117778
+ iCol = n-1;
117279117779
}
117280
- }while( res && iMin<i );
117780
+ }while( res && iMin<iSample );
117781
+ i = iSample / nField;
117281117782
117282117783
#ifdef SQLITE_DEBUG
117283117784
/* The following assert statements check that the binary search code
117284117785
** above found the right answer. This block serves no purpose other
117285117786
** than to invoke the asserts. */
117286
- if( res==0 ){
117287
- /* If (res==0) is true, then sample $i must be equal to pRec */
117288
- assert( i<pIdx->nSample );
117289
- assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
117290
- || pParse->db->mallocFailed );
117291
- }else{
117292
- /* Otherwise, pRec must be smaller than sample $i and larger than
117293
- ** sample ($i-1). */
117294
- assert( i==pIdx->nSample
117295
- || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
117296
- || pParse->db->mallocFailed );
117297
- assert( i==0
117298
- || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
117299
- || pParse->db->mallocFailed );
117787
+ if( pParse->db->mallocFailed==0 ){
117788
+ if( res==0 ){
117789
+ /* If (res==0) is true, then pRec must be equal to sample i. */
117790
+ assert( i<pIdx->nSample );
117791
+ assert( iCol==nField-1 );
117792
+ pRec->nField = nField;
117793
+ assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
117794
+ || pParse->db->mallocFailed
117795
+ );
117796
+ }else{
117797
+ /* Unless i==pIdx->nSample, indicating that pRec is larger than
117798
+ ** all samples in the aSample[] array, pRec must be smaller than the
117799
+ ** (iCol+1) field prefix of sample i. */
117800
+ assert( i<=pIdx->nSample && i>=0 );
117801
+ pRec->nField = iCol+1;
117802
+ assert( i==pIdx->nSample
117803
+ || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
117804
+ || pParse->db->mallocFailed );
117805
+
117806
+ /* if i==0 and iCol==0, then record pRec is smaller than all samples
117807
+ ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must
117808
+ ** be greater than or equal to the (iCol) field prefix of sample i.
117809
+ ** If (i>0), then pRec must also be greater than sample (i-1). */
117810
+ if( iCol>0 ){
117811
+ pRec->nField = iCol;
117812
+ assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0
117813
+ || pParse->db->mallocFailed );
117814
+ }
117815
+ if( i>0 ){
117816
+ pRec->nField = nField;
117817
+ assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
117818
+ || pParse->db->mallocFailed );
117819
+ }
117820
+ }
117300117821
}
117301117822
#endif /* ifdef SQLITE_DEBUG */
117302117823
117303
- /* At this point, aSample[i] is the first sample that is greater than
117304
- ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
117305
- ** than pVal. If aSample[i]==pVal, then res==0.
117306
- */
117307117824
if( res==0 ){
117825
+ /* Record pRec is equal to sample i */
117826
+ assert( iCol==nField-1 );
117308117827
aStat[0] = aSample[i].anLt[iCol];
117309117828
aStat[1] = aSample[i].anEq[iCol];
117310117829
}else{
117311
- tRowcnt iLower, iUpper, iGap;
117312
- if( i==0 ){
117313
- iLower = 0;
117314
- iUpper = aSample[0].anLt[iCol];
117830
+ /* At this point, the (iCol+1) field prefix of aSample[i] is the first
117831
+ ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
117832
+ ** is larger than all samples in the array. */
117833
+ tRowcnt iUpper, iGap;
117834
+ if( i>=pIdx->nSample ){
117835
+ iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
117315117836
}else{
117316
- i64 nRow0 = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
117317
- iUpper = i>=pIdx->nSample ? nRow0 : aSample[i].anLt[iCol];
117318
- iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
117837
+ iUpper = aSample[i].anLt[iCol];
117319117838
}
117320
- aStat[1] = pIdx->aAvgEq[iCol];
117839
+
117321117840
if( iLower>=iUpper ){
117322117841
iGap = 0;
117323117842
}else{
117324117843
iGap = iUpper - iLower;
117325117844
}
@@ -117327,11 +117846,15 @@
117327117846
iGap = (iGap*2)/3;
117328117847
}else{
117329117848
iGap = iGap/3;
117330117849
}
117331117850
aStat[0] = iLower + iGap;
117851
+ aStat[1] = pIdx->aAvgEq[iCol];
117332117852
}
117853
+
117854
+ /* Restore the pRec->nField value before returning. */
117855
+ pRec->nField = nField;
117333117856
return i;
117334117857
}
117335117858
#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
117336117859
117337117860
/*
@@ -118322,25 +118845,33 @@
118322118845
#else
118323118846
# define addScanStatus(a, b, c, d) ((void)d)
118324118847
#endif
118325118848
118326118849
/*
118327
-** Look at the last instruction coded. If that instruction is OP_String8
118328
-** and if pLoop->iLikeRepCntr is non-zero, then change the P3 to be
118850
+** If the most recently coded instruction is a constant range contraint
118851
+** that originated from the LIKE optimization, then change the P3 to be
118329118852
** pLoop->iLikeRepCntr and set P5.
118330118853
**
118331118854
** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
118332118855
** expression: "x>='ABC' AND x<'abd'". But this requires that the range
118333118856
** scan loop run twice, once for strings and a second time for BLOBs.
118334118857
** The OP_String opcodes on the second pass convert the upper and lower
118335118858
** bound string contants to blobs. This routine makes the necessary changes
118336118859
** to the OP_String opcodes for that to happen.
118337118860
*/
118338
-static void whereLikeOptimizationStringFixup(Vdbe *v, WhereLevel *pLevel){
118339
- VdbeOp *pOp;
118340
- pOp = sqlite3VdbeGetOp(v, -1);
118341
- if( pLevel->iLikeRepCntr && pOp->opcode==OP_String8 ){
118861
+static void whereLikeOptimizationStringFixup(
118862
+ Vdbe *v, /* prepared statement under construction */
118863
+ WhereLevel *pLevel, /* The loop that contains the LIKE operator */
118864
+ WhereTerm *pTerm /* The upper or lower bound just coded */
118865
+){
118866
+ if( pTerm->wtFlags & TERM_LIKEOPT ){
118867
+ VdbeOp *pOp;
118868
+ assert( pLevel->iLikeRepCntr>0 );
118869
+ pOp = sqlite3VdbeGetOp(v, -1);
118870
+ assert( pOp!=0 );
118871
+ assert( pOp->opcode==OP_String8
118872
+ || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
118342118873
pOp->p3 = pLevel->iLikeRepCntr;
118343118874
pOp->p5 = 1;
118344118875
}
118345118876
}
118346118877
@@ -118670,18 +119201,20 @@
118670119201
*/
118671119202
j = nEq;
118672119203
if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
118673119204
pRangeStart = pLoop->aLTerm[j++];
118674119205
nExtraReg = 1;
119206
+ /* Like optimization range constraints always occur in pairs */
119207
+ assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
119208
+ (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
118675119209
}
118676119210
if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
118677119211
pRangeEnd = pLoop->aLTerm[j++];
118678119212
nExtraReg = 1;
118679
- if( pRangeStart
118680
- && (pRangeStart->wtFlags & TERM_LIKEOPT)!=0
118681
- && (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0
118682
- ){
119213
+ if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
119214
+ assert( pRangeStart!=0 ); /* LIKE opt constraints */
119215
+ assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
118683119216
pLevel->iLikeRepCntr = ++pParse->nMem;
118684119217
testcase( bRev );
118685119218
testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
118686119219
sqlite3VdbeAddOp2(v, OP_Integer,
118687119220
bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
@@ -118729,11 +119262,11 @@
118729119262
/* Seek the index cursor to the start of the range. */
118730119263
nConstraint = nEq;
118731119264
if( pRangeStart ){
118732119265
Expr *pRight = pRangeStart->pExpr->pRight;
118733119266
sqlite3ExprCode(pParse, pRight, regBase+nEq);
118734
- whereLikeOptimizationStringFixup(v, pLevel);
119267
+ whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
118735119268
if( (pRangeStart->wtFlags & TERM_VNULL)==0
118736119269
&& sqlite3ExprCanBeNull(pRight)
118737119270
){
118738119271
sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
118739119272
VdbeCoverage(v);
@@ -118775,11 +119308,11 @@
118775119308
nConstraint = nEq;
118776119309
if( pRangeEnd ){
118777119310
Expr *pRight = pRangeEnd->pExpr->pRight;
118778119311
sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
118779119312
sqlite3ExprCode(pParse, pRight, regBase+nEq);
118780
- whereLikeOptimizationStringFixup(v, pLevel);
119313
+ whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
118781119314
if( (pRangeEnd->wtFlags & TERM_VNULL)==0
118782119315
&& sqlite3ExprCanBeNull(pRight)
118783119316
){
118784119317
sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
118785119318
VdbeCoverage(v);
@@ -119852,10 +120385,14 @@
119852120385
){
119853120386
continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
119854120387
}
119855120388
if( pTerm->prereqRight & pNew->maskSelf ) continue;
119856120389
120390
+ /* Do not allow the upper bound of a LIKE optimization range constraint
120391
+ ** to mix with a lower range bound from some other source */
120392
+ if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
120393
+
119857120394
pNew->wsFlags = saved_wsFlags;
119858120395
pNew->u.btree.nEq = saved_nEq;
119859120396
pNew->nLTerm = saved_nLTerm;
119860120397
if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
119861120398
pNew->aLTerm[pNew->nLTerm++] = pTerm;
@@ -119895,10 +120432,21 @@
119895120432
testcase( eOp & WO_GT );
119896120433
testcase( eOp & WO_GE );
119897120434
pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
119898120435
pBtm = pTerm;
119899120436
pTop = 0;
120437
+ if( pTerm->wtFlags & TERM_LIKEOPT ){
120438
+ /* Range contraints that come from the LIKE optimization are
120439
+ ** always used in pairs. */
120440
+ pTop = &pTerm[1];
120441
+ assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
120442
+ assert( pTop->wtFlags & TERM_LIKEOPT );
120443
+ assert( pTop->eOperator==WO_LT );
120444
+ if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
120445
+ pNew->aLTerm[pNew->nLTerm++] = pTop;
120446
+ pNew->wsFlags |= WHERE_TOP_LIMIT;
120447
+ }
119900120448
}else{
119901120449
assert( eOp & (WO_LT|WO_LE) );
119902120450
testcase( eOp & WO_LT );
119903120451
testcase( eOp & WO_LE );
119904120452
pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
@@ -121089,14 +121637,14 @@
121089121637
assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] );
121090121638
assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX );
121091121639
121092121640
/* Seed the search with a single WherePath containing zero WhereLoops.
121093121641
**
121094
- ** TUNING: Do not let the number of iterations go above 25. If the cost
121095
- ** of computing an automatic index is not paid back within the first 25
121642
+ ** TUNING: Do not let the number of iterations go above 28. If the cost
121643
+ ** of computing an automatic index is not paid back within the first 28
121096121644
** rows, then do not use the automatic index. */
121097
- aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==sqlite3LogEst(25) );
121645
+ aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
121098121646
nFrom = 1;
121099121647
assert( aFrom[0].isOrdered==0 );
121100121648
if( nOrderBy ){
121101121649
/* If nLoop is zero, then there are no FROM terms in the query. Since
121102121650
** in this case the query may return a maximum of one row, the results
@@ -121890,10 +122438,16 @@
121890122438
assert( pIx->pSchema==pTab->pSchema );
121891122439
assert( iIndexCur>=0 );
121892122440
if( op ){
121893122441
sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
121894122442
sqlite3VdbeSetP4KeyInfo(pParse, pIx);
122443
+ if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
122444
+ && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
122445
+ && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
122446
+ ){
122447
+ sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */
122448
+ }
121895122449
VdbeComment((v, "%s", pIx->zName));
121896122450
}
121897122451
}
121898122452
if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
121899122453
notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
@@ -124891,11 +125445,11 @@
124891125445
spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
124892125446
}
124893125447
break;
124894125448
case 193: /* expr ::= expr COLLATE ID|STRING */
124895125449
{
124896
- yygotominor.yy346.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy346.pExpr, &yymsp[0].minor.yy0);
125450
+ yygotominor.yy346.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy346.pExpr, &yymsp[0].minor.yy0, 1);
124897125451
yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart;
124898125452
yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
124899125453
}
124900125454
break;
124901125455
case 194: /* expr ::= CAST LP expr AS typetoken RP */
@@ -125171,20 +125725,20 @@
125171125725
case 241: /* uniqueflag ::= */
125172125726
{yygotominor.yy328 = OE_None;}
125173125727
break;
125174125728
case 244: /* idxlist ::= idxlist COMMA nm collate sortorder */
125175125729
{
125176
- Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
125730
+ Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0, 1);
125177125731
yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, p);
125178125732
sqlite3ExprListSetName(pParse,yygotominor.yy14,&yymsp[-2].minor.yy0,1);
125179125733
sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
125180125734
if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
125181125735
}
125182125736
break;
125183125737
case 245: /* idxlist ::= nm collate sortorder */
125184125738
{
125185
- Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
125739
+ Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0, 1);
125186125740
yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, p);
125187125741
sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
125188125742
sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
125189125743
if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
125190125744
}
@@ -126439,13 +126993,15 @@
126439126993
pParse->zTail = &zSql[i];
126440126994
}
126441126995
sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
126442126996
}
126443126997
#ifdef YYTRACKMAXSTACKDEPTH
126998
+ sqlite3_mutex_enter(sqlite3MallocMutex());
126444126999
sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
126445127000
sqlite3ParserStackPeak(pEngine)
126446127001
);
127002
+ sqlite3_mutex_leave(sqlite3MallocMutex());
126447127003
#endif /* YYDEBUG */
126448127004
sqlite3ParserFree(pEngine, sqlite3_free);
126449127005
db->lookaside.bEnabled = enableLookaside;
126450127006
if( db->mallocFailed ){
126451127007
pParse->rc = SQLITE_NOMEM;
@@ -127011,10 +127567,15 @@
127011127567
rc = sqlite3_wsd_init(4096, 24);
127012127568
if( rc!=SQLITE_OK ){
127013127569
return rc;
127014127570
}
127015127571
#endif
127572
+
127573
+ /* If the following assert() fails on some obscure processor/compiler
127574
+ ** combination, the work-around is to set the correct pointer
127575
+ ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
127576
+ assert( SQLITE_PTRSIZE==sizeof(char*) );
127016127577
127017127578
/* If SQLite is already completely initialized, then this call
127018127579
** to sqlite3_initialize() should be a no-op. But the initialization
127019127580
** must be complete. So isInit must not be set until the very end
127020127581
** of this routine.
@@ -132766,15 +133327,20 @@
132766133327
** the output value undefined. Otherwise SQLITE_OK is returned.
132767133328
**
132768133329
** This function is used when parsing the "prefix=" FTS4 parameter.
132769133330
*/
132770133331
static int fts3GobbleInt(const char **pp, int *pnOut){
133332
+ const int MAX_NPREFIX = 10000000;
132771133333
const char *p; /* Iterator pointer */
132772133334
int nInt = 0; /* Output value */
132773133335
132774133336
for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
132775133337
nInt = nInt * 10 + (p[0] - '0');
133338
+ if( nInt>MAX_NPREFIX ){
133339
+ nInt = 0;
133340
+ break;
133341
+ }
132776133342
}
132777133343
if( p==*pp ) return SQLITE_ERROR;
132778133344
*pnOut = nInt;
132779133345
*pp = p;
132780133346
return SQLITE_OK;
@@ -132813,27 +133379,33 @@
132813133379
}
132814133380
}
132815133381
132816133382
aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
132817133383
*apIndex = aIndex;
132818
- *pnIndex = nIndex;
132819133384
if( !aIndex ){
132820133385
return SQLITE_NOMEM;
132821133386
}
132822133387
132823133388
memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
132824133389
if( zParam ){
132825133390
const char *p = zParam;
132826133391
int i;
132827133392
for(i=1; i<nIndex; i++){
132828
- int nPrefix;
133393
+ int nPrefix = 0;
132829133394
if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
132830
- aIndex[i].nPrefix = nPrefix;
133395
+ assert( nPrefix>=0 );
133396
+ if( nPrefix==0 ){
133397
+ nIndex--;
133398
+ i--;
133399
+ }else{
133400
+ aIndex[i].nPrefix = nPrefix;
133401
+ }
132831133402
p++;
132832133403
}
132833133404
}
132834133405
133406
+ *pnIndex = nIndex;
132835133407
return SQLITE_OK;
132836133408
}
132837133409
132838133410
/*
132839133411
** This function is called when initializing an FTS4 table that uses the
@@ -140609,11 +141181,11 @@
140609141181
nName = sqlite3_value_bytes(argv[0])+1;
140610141182
140611141183
if( argc==2 ){
140612141184
void *pOld;
140613141185
int n = sqlite3_value_bytes(argv[1]);
140614
- if( n!=sizeof(pPtr) ){
141186
+ if( zName==0 || n!=sizeof(pPtr) ){
140615141187
sqlite3_result_error(context, "argument type mismatch", -1);
140616141188
return;
140617141189
}
140618141190
pPtr = *(void **)sqlite3_value_blob(argv[1]);
140619141191
pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
@@ -140620,11 +141192,13 @@
140620141192
if( pOld==pPtr ){
140621141193
sqlite3_result_error(context, "out of memory", -1);
140622141194
return;
140623141195
}
140624141196
}else{
140625
- pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
141197
+ if( zName ){
141198
+ pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
141199
+ }
140626141200
if( !pPtr ){
140627141201
char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
140628141202
sqlite3_result_error(context, zErr, -1);
140629141203
sqlite3_free(zErr);
140630141204
return;
@@ -140701,10 +141275,14 @@
140701141275
zCopy = sqlite3_mprintf("%s", zArg);
140702141276
if( !zCopy ) return SQLITE_NOMEM;
140703141277
zEnd = &zCopy[strlen(zCopy)];
140704141278
140705141279
z = (char *)sqlite3Fts3NextToken(zCopy, &n);
141280
+ if( z==0 ){
141281
+ assert( n==0 );
141282
+ z = zCopy;
141283
+ }
140706141284
z[n] = '\0';
140707141285
sqlite3Fts3Dequote(z);
140708141286
140709141287
m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
140710141288
if( !m ){
@@ -143346,11 +143924,14 @@
143346143924
/*
143347143925
** This is a comparison function used as a qsort() callback when sorting
143348143926
** an array of pending terms by term. This occurs as part of flushing
143349143927
** the contents of the pending-terms hash table to the database.
143350143928
*/
143351
-static int fts3CompareElemByTerm(const void *lhs, const void *rhs){
143929
+static int SQLITE_CDECL fts3CompareElemByTerm(
143930
+ const void *lhs,
143931
+ const void *rhs
143932
+){
143352143933
char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
143353143934
char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
143354143935
int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
143355143936
int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
143356143937
143357143938
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -261,10 +261,17 @@
261 #ifndef SQLITE_API
262 # define SQLITE_API
263 #endif
264
265
 
 
 
 
 
 
 
266 /*
267 ** These no-op macros are used in front of interfaces to mark those
268 ** interfaces as either deprecated or experimental. New applications
269 ** should not use deprecated interfaces - they are supported for backwards
270 ** compatibility only. Application writers should be aware that
@@ -316,11 +323,11 @@
316 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
317 ** [sqlite_version()] and [sqlite_source_id()].
318 */
319 #define SQLITE_VERSION "3.8.9"
320 #define SQLITE_VERSION_NUMBER 3008009
321 #define SQLITE_SOURCE_ID "2015-03-09 10:40:48 e5da5e7d5dc5a3438ced23f1ee83e695abc29c45"
322
323 /*
324 ** CAPI3REF: Run-Time Library Version Numbers
325 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
326 **
@@ -1157,10 +1164,17 @@
1157 ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
1158 ** opcode causes the xFileControl method to swap the file handle with the one
1159 ** pointed to by the pArg argument. This capability is used during testing
1160 ** and only needs to be supported when SQLITE_TEST is defined.
1161 **
 
 
 
 
 
 
 
1162 ** </ul>
1163 */
1164 #define SQLITE_FCNTL_LOCKSTATE 1
1165 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
1166 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1181,10 +1195,11 @@
1181 #define SQLITE_FCNTL_TRACE 19
1182 #define SQLITE_FCNTL_HAS_MOVED 20
1183 #define SQLITE_FCNTL_SYNC 21
1184 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
1185 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
 
1186
1187 /* deprecated names */
1188 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1189 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1190 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -1747,11 +1762,11 @@
1747 ** disabled, the following SQLite interfaces become non-operational:
1748 ** <ul>
1749 ** <li> [sqlite3_memory_used()]
1750 ** <li> [sqlite3_memory_highwater()]
1751 ** <li> [sqlite3_soft_heap_limit64()]
1752 ** <li> [sqlite3_status()]
1753 ** </ul>)^
1754 ** ^Memory allocation statistics are enabled by default unless SQLite is
1755 ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1756 ** allocation statistics are disabled by default.
1757 ** </dd>
@@ -3204,15 +3219,17 @@
3204
3205
3206 /*
3207 ** CAPI3REF: Error Codes And Messages
3208 **
3209 ** ^The sqlite3_errcode() interface returns the numeric [result code] or
3210 ** [extended result code] for the most recent failed sqlite3_* API call
3211 ** associated with a [database connection]. If a prior API call failed
3212 ** but the most recent API call succeeded, the return value from
3213 ** sqlite3_errcode() is undefined. ^The sqlite3_extended_errcode()
 
 
3214 ** interface is the same except that it always returns the
3215 ** [extended result code] even when extended result codes are
3216 ** disabled.
3217 **
3218 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
@@ -5822,11 +5839,11 @@
5822 ** is delivered up to the client application, the string will be automatically
5823 ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
5824 */
5825 struct sqlite3_vtab {
5826 const sqlite3_module *pModule; /* The module for this virtual table */
5827 int nRef; /* NO LONGER USED */
5828 char *zErrMsg; /* Error message from sqlite3_mprintf() */
5829 /* Virtual table implementations will typically add additional fields */
5830 };
5831
5832 /*
@@ -6500,11 +6517,11 @@
6500 #define SQLITE_TESTCTRL_LAST 25
6501
6502 /*
6503 ** CAPI3REF: SQLite Runtime Status
6504 **
6505 ** ^This interface is used to retrieve runtime status information
6506 ** about the performance of SQLite, and optionally to reset various
6507 ** highwater marks. ^The first argument is an integer code for
6508 ** the specific parameter to measure. ^(Recognized integer codes
6509 ** are of the form [status parameters | SQLITE_STATUS_...].)^
6510 ** ^The current value of the parameter is returned into *pCurrent.
@@ -6514,23 +6531,26 @@
6514 ** value. For those parameters
6515 ** nothing is written into *pHighwater and the resetFlag is ignored.)^
6516 ** ^(Other parameters record only the highwater mark and not the current
6517 ** value. For these latter parameters nothing is written into *pCurrent.)^
6518 **
6519 ** ^The sqlite3_status() routine returns SQLITE_OK on success and a
6520 ** non-zero [error code] on failure.
6521 **
6522 ** This routine is threadsafe but is not atomic. This routine can be
6523 ** called while other threads are running the same or different SQLite
6524 ** interfaces. However the values returned in *pCurrent and
6525 ** *pHighwater reflect the status of SQLite at different points in time
6526 ** and it is possible that another thread might change the parameter
6527 ** in between the times when *pCurrent and *pHighwater are written.
6528 **
6529 ** See also: [sqlite3_db_status()]
6530 */
6531 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
 
 
 
 
 
 
6532
6533
6534 /*
6535 ** CAPI3REF: Status Parameters
6536 ** KEYWORDS: {status parameters}
@@ -8911,10 +8931,24 @@
8911 **
8912 ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
8913 */
8914 typedef INT16_TYPE LogEst;
8915
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8916 /*
8917 ** Macros to determine whether the machine is big or little endian,
8918 ** and whether or not that determination is run-time or compile-time.
8919 **
8920 ** For best performance, an attempt is made to guess at the byte-order
@@ -9361,12 +9395,22 @@
9361 #define BTREE_DATA_VERSION 15 /* A virtual meta-value */
9362
9363 /*
9364 ** Values that may be OR'd together to form the second argument of an
9365 ** sqlite3BtreeCursorHints() call.
 
 
 
 
 
 
 
 
 
9366 */
9367 #define BTREE_BULKLOAD 0x00000001
 
9368
9369 SQLITE_PRIVATE int sqlite3BtreeCursor(
9370 Btree*, /* BTree containing table to open */
9371 int iTable, /* Index of root page */
9372 int wrFlag, /* 1 for writing. 0 for read-only */
@@ -9408,10 +9452,13 @@
9408 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
9409 SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
9410 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
9411 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
9412 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
 
 
 
9413 SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
9414 SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
9415
9416 #ifndef NDEBUG
9417 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
@@ -10908,10 +10955,11 @@
10908 } init;
10909 int nVdbeActive; /* Number of VDBEs currently running */
10910 int nVdbeRead; /* Number of active VDBEs that read or write */
10911 int nVdbeWrite; /* Number of active VDBEs that read and write */
10912 int nVdbeExec; /* Number of nested calls to VdbeExec() */
 
10913 int nExtension; /* Number of loaded extensions */
10914 void **aExtension; /* Array of shared library handles */
10915 void (*xTrace)(void*,const char*); /* Trace function */
10916 void *pTraceArg; /* Argument to the trace function */
10917 void (*xProfile)(void*,const char*,u64); /* Profiling function */
@@ -12502,11 +12550,12 @@
12502 #define OPFLAG_APPEND 0x08 /* This is likely to be an append */
12503 #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
12504 #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
12505 #define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
12506 #define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
12507 #define OPFLAG_P2ISREG 0x02 /* P2 to OP_Open** is a register number */
 
12508 #define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
12509
12510 /*
12511 * Each trigger present in the database schema is stored as an instance of
12512 * struct Trigger.
@@ -12906,14 +12955,19 @@
12906 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
12907 SQLITE_PRIVATE int sqlite3MutexInit(void);
12908 SQLITE_PRIVATE int sqlite3MutexEnd(void);
12909 #endif
12910
12911 SQLITE_PRIVATE int sqlite3StatusValue(int);
12912 SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
 
12913 SQLITE_PRIVATE void sqlite3StatusSet(int, int);
12914
 
 
 
 
12915 #ifndef SQLITE_OMIT_FLOATING_POINT
12916 SQLITE_PRIVATE int sqlite3IsNaN(double);
12917 #else
12918 # define sqlite3IsNaN(X) 0
12919 #endif
@@ -13289,11 +13343,11 @@
13289 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
13290 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
13291 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
13292 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
13293 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
13294 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*);
13295 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
13296 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
13297 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
13298 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
13299 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
@@ -14676,18 +14730,10 @@
14676 ** An instance of the virtual machine. This structure contains the complete
14677 ** state of the virtual machine.
14678 **
14679 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
14680 ** is really a pointer to an instance of this structure.
14681 **
14682 ** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
14683 ** any virtual table method invocations made by the vdbe program. It is
14684 ** set to 2 for xDestroy method calls and 1 for all other methods. This
14685 ** variable is used for two purposes: to allow xDestroy methods to execute
14686 ** "DROP TABLE" statements and to prevent some nasty side effects of
14687 ** malloc failure when SQLite is invoked recursively by a virtual table
14688 ** method function.
14689 */
14690 struct Vdbe {
14691 sqlite3 *db; /* The database connection that owns this statement */
14692 Op *aOp; /* Space to hold the virtual machine's program */
14693 Mem *aMem; /* The memory locations */
@@ -14714,11 +14760,10 @@
14714 #endif
14715 u16 nResColumn; /* Number of columns in one row of the result set */
14716 u8 errorAction; /* Recovery action to do in case of an error */
14717 u8 minWriteFileFormat; /* Minimum file format for writable database files */
14718 bft explain:2; /* True if EXPLAIN present on SQL command */
14719 bft inVtabMethod:2; /* See comments above */
14720 bft changeCntOn:1; /* True to update the change-counter */
14721 bft expired:1; /* True if the VM needs to be recompiled */
14722 bft runOnlyOnce:1; /* Automatically expire on reset */
14723 bft usesStmtJournal:1; /* True if uses a statement journal */
14724 bft readOnly:1; /* True for statements that do not write */
@@ -14874,13 +14919,35 @@
14874 /*
14875 ** Variables in which to record status information.
14876 */
14877 typedef struct sqlite3StatType sqlite3StatType;
14878 static SQLITE_WSD struct sqlite3StatType {
14879 int nowValue[10]; /* Current value */
14880 int mxValue[10]; /* Maximum value */
 
 
 
 
 
14881 } sqlite3Stat = { {0,}, {0,} };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14882
14883
14884 /* The "wsdStat" macro will resolve to the status information
14885 ** state vector. If writable static data is unsupported on the target,
14886 ** we have to locate the state vector at run-time. In the more common
@@ -14894,64 +14961,110 @@
14894 # define wsdStatInit
14895 # define wsdStat sqlite3Stat
14896 #endif
14897
14898 /*
14899 ** Return the current value of a status parameter.
 
14900 */
14901 SQLITE_PRIVATE int sqlite3StatusValue(int op){
14902 wsdStatInit;
14903 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
 
 
 
14904 return wsdStat.nowValue[op];
14905 }
14906
14907 /*
14908 ** Add N to the value of a status record. It is assumed that the
14909 ** caller holds appropriate locks.
 
 
 
 
 
 
 
14910 */
14911 SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
14912 wsdStatInit;
14913 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
 
 
 
14914 wsdStat.nowValue[op] += N;
14915 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
14916 wsdStat.mxValue[op] = wsdStat.nowValue[op];
14917 }
14918 }
 
 
 
 
 
 
 
 
 
14919
14920 /*
14921 ** Set the value of a status to X.
 
14922 */
14923 SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
14924 wsdStatInit;
14925 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
 
 
 
14926 wsdStat.nowValue[op] = X;
14927 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
14928 wsdStat.mxValue[op] = wsdStat.nowValue[op];
14929 }
14930 }
14931
14932 /*
14933 ** Query status information.
14934 **
14935 ** This implementation assumes that reading or writing an aligned
14936 ** 32-bit integer is an atomic operation. If that assumption is not true,
14937 ** then this routine is not threadsafe.
14938 */
14939 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
 
 
 
 
 
 
14940 wsdStatInit;
14941 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
14942 return SQLITE_MISUSE_BKPT;
14943 }
14944 #ifdef SQLITE_ENABLE_API_ARMOR
14945 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
14946 #endif
 
 
14947 *pCurrent = wsdStat.nowValue[op];
14948 *pHighwater = wsdStat.mxValue[op];
14949 if( resetFlag ){
14950 wsdStat.mxValue[op] = wsdStat.nowValue[op];
14951 }
 
 
14952 return SQLITE_OK;
 
 
 
 
 
 
 
 
 
 
 
 
 
14953 }
14954
14955 /*
14956 ** Query status information for a single database connection
14957 */
@@ -20401,10 +20514,17 @@
20401 */
20402 int nearlyFull;
20403 } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
20404
20405 #define mem0 GLOBAL(struct Mem0Global, mem0)
 
 
 
 
 
 
 
20406
20407 /*
20408 ** This routine runs when the memory allocator sees that the
20409 ** total memory allocation is about to exceed the soft heap
20410 ** limit.
@@ -20424,11 +20544,11 @@
20424 static int sqlite3MemoryAlarm(
20425 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
20426 void *pArg,
20427 sqlite3_int64 iThreshold
20428 ){
20429 int nUsed;
20430 sqlite3_mutex_enter(mem0.mutex);
20431 mem0.alarmCallback = xCallback;
20432 mem0.alarmArg = pArg;
20433 mem0.alarmThreshold = iThreshold;
20434 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
@@ -20593,11 +20713,11 @@
20593 void *p;
20594 assert( sqlite3_mutex_held(mem0.mutex) );
20595 nFull = sqlite3GlobalConfig.m.xRoundup(n);
20596 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
20597 if( mem0.alarmCallback!=0 ){
20598 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
20599 if( nUsed >= mem0.alarmThreshold - nFull ){
20600 mem0.nearlyFull = 1;
20601 sqlite3MallocAlarm(nFull);
20602 }else{
20603 mem0.nearlyFull = 0;
@@ -20610,12 +20730,12 @@
20610 p = sqlite3GlobalConfig.m.xMalloc(nFull);
20611 }
20612 #endif
20613 if( p ){
20614 nFull = sqlite3MallocSize(p);
20615 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
20616 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
20617 }
20618 *pp = p;
20619 return nFull;
20620 }
20621
@@ -20688,18 +20808,18 @@
20688 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
20689 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
20690 p = mem0.pScratchFree;
20691 mem0.pScratchFree = mem0.pScratchFree->pNext;
20692 mem0.nScratchFree--;
20693 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
20694 sqlite3_mutex_leave(mem0.mutex);
20695 }else{
20696 sqlite3_mutex_leave(mem0.mutex);
20697 p = sqlite3Malloc(n);
20698 if( sqlite3GlobalConfig.bMemstat && p ){
20699 sqlite3_mutex_enter(mem0.mutex);
20700 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
20701 sqlite3_mutex_leave(mem0.mutex);
20702 }
20703 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
20704 }
20705 assert( sqlite3_mutex_notheld(mem0.mutex) );
@@ -20736,23 +20856,23 @@
20736 sqlite3_mutex_enter(mem0.mutex);
20737 pSlot->pNext = mem0.pScratchFree;
20738 mem0.pScratchFree = pSlot;
20739 mem0.nScratchFree++;
20740 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
20741 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
20742 sqlite3_mutex_leave(mem0.mutex);
20743 }else{
20744 /* Release memory back to the heap */
20745 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
20746 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
20747 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
20748 if( sqlite3GlobalConfig.bMemstat ){
20749 int iSize = sqlite3MallocSize(p);
20750 sqlite3_mutex_enter(mem0.mutex);
20751 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
20752 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
20753 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
20754 sqlite3GlobalConfig.m.xFree(p);
20755 sqlite3_mutex_leave(mem0.mutex);
20756 }else{
20757 sqlite3GlobalConfig.m.xFree(p);
20758 }
@@ -20779,26 +20899,26 @@
20779 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
20780 return sqlite3GlobalConfig.m.xSize(p);
20781 }
20782 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
20783 if( db==0 ){
20784 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
20785 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
20786 return sqlite3MallocSize(p);
20787 }else{
20788 assert( sqlite3_mutex_held(db->mutex) );
20789 if( isLookaside(db, p) ){
20790 return db->lookaside.sz;
20791 }else{
20792 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
20793 assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
20794 return sqlite3GlobalConfig.m.xSize(p);
20795 }
20796 }
20797 }
20798 SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
20799 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
20800 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
20801 return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
20802 }
20803
20804 /*
@@ -20805,15 +20925,15 @@
20805 ** Free memory previously obtained from sqlite3Malloc().
20806 */
20807 SQLITE_API void sqlite3_free(void *p){
20808 if( p==0 ) return; /* IMP: R-49053-54554 */
20809 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
20810 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
20811 if( sqlite3GlobalConfig.bMemstat ){
20812 sqlite3_mutex_enter(mem0.mutex);
20813 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
20814 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
20815 sqlite3GlobalConfig.m.xFree(p);
20816 sqlite3_mutex_leave(mem0.mutex);
20817 }else{
20818 sqlite3GlobalConfig.m.xFree(p);
20819 }
@@ -20850,11 +20970,11 @@
20850 db->lookaside.nOut--;
20851 return;
20852 }
20853 }
20854 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
20855 assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
20856 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
20857 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
20858 sqlite3_free(p);
20859 }
20860
@@ -20863,11 +20983,11 @@
20863 */
20864 SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
20865 int nOld, nNew, nDiff;
20866 void *pNew;
20867 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
20868 assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
20869 if( pOld==0 ){
20870 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
20871 }
20872 if( nBytes==0 ){
20873 sqlite3_free(pOld); /* IMP: R-26507-47431 */
@@ -20897,11 +21017,11 @@
20897 sqlite3MallocAlarm((int)nBytes);
20898 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
20899 }
20900 if( pNew ){
20901 nNew = sqlite3MallocSize(pNew);
20902 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
20903 }
20904 sqlite3_mutex_leave(mem0.mutex);
20905 }else{
20906 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
20907 }
@@ -21030,11 +21150,11 @@
21030 memcpy(pNew, p, db->lookaside.sz);
21031 sqlite3DbFree(db, p);
21032 }
21033 }else{
21034 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
21035 assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
21036 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
21037 pNew = sqlite3_realloc64(p, n);
21038 if( !pNew ){
21039 db->mallocFailed = 1;
21040 }
@@ -25287,10 +25407,11 @@
25287 #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
25288 #define UNIXFILE_DELETE 0x20 /* Delete on close */
25289 #define UNIXFILE_URI 0x40 /* Filename might have query parameters */
25290 #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
25291 #define UNIXFILE_WARNED 0x0100 /* verifyDbFile() warnings issued */
 
25292
25293 /*
25294 ** Include code that is common to all os_*.c files
25295 */
25296 /************** Include os_common.h in the middle of os_unix.c ***************/
@@ -26782,11 +26903,11 @@
26782
26783 assert( pFile );
26784 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
26785 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
26786 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
26787 osGetpid()));
26788
26789 /* If there is already a lock of this type or more restrictive on the
26790 ** unixFile, do nothing. Don't use the end_lock: exit path, as
26791 ** unixEnterMutex() hasn't been called yet.
26792 */
@@ -26990,11 +27111,11 @@
26990 int rc = SQLITE_OK;
26991
26992 assert( pFile );
26993 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
26994 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
26995 osGetpid()));
26996
26997 assert( eFileLock<=SHARED_LOCK );
26998 if( pFile->eFileLock<=eFileLock ){
26999 return SQLITE_OK;
27000 }
@@ -27417,11 +27538,11 @@
27417 char *zLockFile = (char *)pFile->lockingContext;
27418 int rc;
27419
27420 assert( pFile );
27421 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
27422 pFile->eFileLock, osGetpid()));
27423 assert( eFileLock<=SHARED_LOCK );
27424
27425 /* no-op if possible */
27426 if( pFile->eFileLock==eFileLock ){
27427 return SQLITE_OK;
@@ -27635,11 +27756,11 @@
27635 static int flockUnlock(sqlite3_file *id, int eFileLock) {
27636 unixFile *pFile = (unixFile*)id;
27637
27638 assert( pFile );
27639 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
27640 pFile->eFileLock, osGetpid()));
27641 assert( eFileLock<=SHARED_LOCK );
27642
27643 /* no-op if possible */
27644 if( pFile->eFileLock==eFileLock ){
27645 return SQLITE_OK;
@@ -27803,11 +27924,11 @@
27803 sem_t *pSem = pFile->pInode->pSem;
27804
27805 assert( pFile );
27806 assert( pSem );
27807 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
27808 pFile->eFileLock, osGetpid()));
27809 assert( eFileLock<=SHARED_LOCK );
27810
27811 /* no-op if possible */
27812 if( pFile->eFileLock==eFileLock ){
27813 return SQLITE_OK;
@@ -28017,11 +28138,11 @@
28017 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
28018
28019 assert( pFile );
28020 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
28021 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
28022 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid()));
28023
28024 /* If there is already a lock of this type or more restrictive on the
28025 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
28026 ** unixEnterMutex() hasn't been called yet.
28027 */
@@ -28203,11 +28324,11 @@
28203 #endif
28204
28205 assert( pFile );
28206 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
28207 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
28208 osGetpid()));
28209
28210 assert( eFileLock<=SHARED_LOCK );
28211 if( pFile->eFileLock<=eFileLock ){
28212 return SQLITE_OK;
28213 }
@@ -29028,10 +29149,14 @@
29028 ** Information and control of an open file handle.
29029 */
29030 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
29031 unixFile *pFile = (unixFile*)id;
29032 switch( op ){
 
 
 
 
29033 case SQLITE_FCNTL_LOCKSTATE: {
29034 *(int*)pArg = pFile->eFileLock;
29035 return SQLITE_OK;
29036 }
29037 case SQLITE_FCNTL_LAST_ERRNO: {
@@ -29337,37 +29462,42 @@
29337 **
29338 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
29339 ** otherwise.
29340 */
29341 static int unixShmSystemLock(
29342 unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
29343 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
29344 int ofst, /* First byte of the locking range */
29345 int n /* Number of bytes to lock */
29346 ){
29347 struct flock f; /* The posix advisory locking structure */
29348 int rc = SQLITE_OK; /* Result code form fcntl() */
 
29349
29350 /* Access to the unixShmNode object is serialized by the caller */
 
29351 assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
29352
29353 /* Shared locks never span more than one byte */
29354 assert( n==1 || lockType!=F_RDLCK );
29355
29356 /* Locks are within range */
29357 assert( n>=1 && n<SQLITE_SHM_NLOCK );
29358
29359 if( pShmNode->h>=0 ){
 
29360 /* Initialize the locking parameters */
29361 memset(&f, 0, sizeof(f));
29362 f.l_type = lockType;
29363 f.l_whence = SEEK_SET;
29364 f.l_start = ofst;
29365 f.l_len = n;
29366
29367 rc = osFcntl(pShmNode->h, F_SETLK, &f);
 
29368 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
 
29369 }
29370
29371 /* Update the global lock state and do debug tracing */
29372 #ifdef SQLITE_DEBUG
29373 { u16 mask;
@@ -29573,17 +29703,17 @@
29573
29574 /* Check to see if another process is holding the dead-man switch.
29575 ** If not, truncate the file to zero length.
29576 */
29577 rc = SQLITE_OK;
29578 if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
29579 if( robust_ftruncate(pShmNode->h, 0) ){
29580 rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
29581 }
29582 }
29583 if( rc==SQLITE_OK ){
29584 rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
29585 }
29586 if( rc ) goto shm_open_err;
29587 }
29588 }
29589
@@ -29811,11 +29941,11 @@
29811 allMask |= pX->sharedMask;
29812 }
29813
29814 /* Unlock the system-level locks */
29815 if( (mask & allMask)==0 ){
29816 rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
29817 }else{
29818 rc = SQLITE_OK;
29819 }
29820
29821 /* Undo the local locks */
@@ -29839,11 +29969,11 @@
29839 }
29840
29841 /* Get shared locks at the system level, if necessary */
29842 if( rc==SQLITE_OK ){
29843 if( (allShared & mask)==0 ){
29844 rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
29845 }else{
29846 rc = SQLITE_OK;
29847 }
29848 }
29849
@@ -29864,20 +29994,20 @@
29864
29865 /* Get the exclusive locks at the system level. Then if successful
29866 ** also mark the local connection as being locked.
29867 */
29868 if( rc==SQLITE_OK ){
29869 rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
29870 if( rc==SQLITE_OK ){
29871 assert( (p->sharedMask & mask)==0 );
29872 p->exclMask |= mask;
29873 }
29874 }
29875 }
29876 sqlite3_mutex_leave(pShmNode->mutex);
29877 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
29878 p->id, osGetpid(), p->sharedMask, p->exclMask));
29879 return rc;
29880 }
29881
29882 /*
29883 ** Implement a memory barrier or memory fence on shared memory.
@@ -30968,12 +31098,12 @@
30968 /* Detect a pid change and reset the PRNG. There is a race condition
30969 ** here such that two or more threads all trying to open databases at
30970 ** the same instant might all reset the PRNG. But multiple resets
30971 ** are harmless.
30972 */
30973 if( randomnessPid!=osGetpid() ){
30974 randomnessPid = osGetpid();
30975 sqlite3_randomness(0,0);
30976 }
30977
30978 memset(p, 0, sizeof(unixFile));
30979
@@ -31360,11 +31490,11 @@
31360 ** When testing, initializing zBuf[] to zero is all we do. That means
31361 ** that we always use the same random number sequence. This makes the
31362 ** tests repeatable.
31363 */
31364 memset(zBuf, 0, nBuf);
31365 randomnessPid = osGetpid();
31366 #if !defined(SQLITE_TEST)
31367 {
31368 int fd, got;
31369 fd = robust_open("/dev/urandom", O_RDONLY, 0);
31370 if( fd<0 ){
@@ -31681,11 +31811,11 @@
31681 #else
31682 # ifdef _CS_DARWIN_USER_TEMP_DIR
31683 {
31684 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
31685 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
31686 lPath, errno, osGetpid()));
31687 return SQLITE_IOERR_LOCK;
31688 }
31689 len = strlcat(lPath, "sqliteplocks", maxLen);
31690 }
31691 # else
@@ -31703,11 +31833,11 @@
31703 char c = dbPath[i];
31704 lPath[i+len] = (c=='/')?'_':c;
31705 }
31706 lPath[i+len]='\0';
31707 strlcat(lPath, ":auto:", maxLen);
31708 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid()));
31709 return SQLITE_OK;
31710 }
31711
31712 /*
31713 ** Creates the lock file and any missing directories in lockPath
@@ -31730,20 +31860,20 @@
31730 if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
31731 int err=errno;
31732 if( err!=EEXIST ) {
31733 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
31734 "'%s' proxy lock path=%s pid=%d\n",
31735 buf, strerror(err), lockPath, osGetpid()));
31736 return err;
31737 }
31738 }
31739 }
31740 start=i+1;
31741 }
31742 buf[i] = lockPath[i];
31743 }
31744 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, osGetpid()));
31745 return 0;
31746 }
31747
31748 /*
31749 ** Create a new VFS file descriptor (stored in memory obtained from
@@ -32045,11 +32175,11 @@
32045 int tryOldLockPath = 0;
32046 int forceNewLockPath = 0;
32047
32048 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
32049 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
32050 osGetpid()));
32051
32052 rc = proxyGetHostID(myHostID, &pError);
32053 if( (rc&0xff)==SQLITE_IOERR ){
32054 storeLastErrno(pFile, pError);
32055 goto end_takeconch;
@@ -32255,11 +32385,11 @@
32255
32256 pCtx = (proxyLockingContext *)pFile->lockingContext;
32257 conchFile = pCtx->conchFile;
32258 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
32259 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
32260 osGetpid()));
32261 if( pCtx->conchHeld>0 ){
32262 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
32263 }
32264 pCtx->conchHeld = 0;
32265 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
@@ -32397,11 +32527,11 @@
32397 }else{
32398 lockPath=(char *)path;
32399 }
32400
32401 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
32402 (lockPath ? lockPath : ":auto:"), osGetpid()));
32403
32404 pCtx = sqlite3_malloc( sizeof(*pCtx) );
32405 if( pCtx==0 ){
32406 return SQLITE_NOMEM;
32407 }
@@ -39866,20 +39996,20 @@
39866 ** in pcache1 need to be protected via mutex.
39867 */
39868 static void *pcache1Alloc(int nByte){
39869 void *p = 0;
39870 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
39871 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
39872 if( nByte<=pcache1.szSlot ){
39873 sqlite3_mutex_enter(pcache1.mutex);
39874 p = (PgHdr1 *)pcache1.pFree;
39875 if( p ){
39876 pcache1.pFree = pcache1.pFree->pNext;
39877 pcache1.nFreeSlot--;
39878 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
39879 assert( pcache1.nFreeSlot>=0 );
39880 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
 
39881 }
39882 sqlite3_mutex_leave(pcache1.mutex);
39883 }
39884 if( p==0 ){
39885 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
@@ -39888,11 +40018,12 @@
39888 p = sqlite3Malloc(nByte);
39889 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
39890 if( p ){
39891 int sz = sqlite3MallocSize(p);
39892 sqlite3_mutex_enter(pcache1.mutex);
39893 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
 
39894 sqlite3_mutex_leave(pcache1.mutex);
39895 }
39896 #endif
39897 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
39898 }
@@ -39906,11 +40037,11 @@
39906 int nFreed = 0;
39907 if( p==0 ) return 0;
39908 if( p>=pcache1.pStart && p<pcache1.pEnd ){
39909 PgFreeslot *pSlot;
39910 sqlite3_mutex_enter(pcache1.mutex);
39911 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
39912 pSlot = (PgFreeslot*)p;
39913 pSlot->pNext = pcache1.pFree;
39914 pcache1.pFree = pSlot;
39915 pcache1.nFreeSlot++;
39916 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
@@ -39920,11 +40051,11 @@
39920 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
39921 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
39922 nFreed = sqlite3MallocSize(p);
39923 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
39924 sqlite3_mutex_enter(pcache1.mutex);
39925 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed);
39926 sqlite3_mutex_leave(pcache1.mutex);
39927 #endif
39928 sqlite3_free(p);
39929 }
39930 return nFreed;
@@ -40656,10 +40787,18 @@
40656
40657 /*
40658 ** Return the size of the header on each page of this PCACHE implementation.
40659 */
40660 SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
 
 
 
 
 
 
 
 
40661
40662 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
40663 /*
40664 ** This function is called to free superfluous dynamically allocated memory
40665 ** held by the pager system. Memory in use by any SQLite pager allocated
@@ -49429,13 +49568,14 @@
49429 if( pWal->exclusiveMode ) return;
49430 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
49431 SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
49432 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
49433 }
49434 static int walLockExclusive(Wal *pWal, int lockIdx, int n){
49435 int rc;
49436 if( pWal->exclusiveMode ) return SQLITE_OK;
 
49437 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
49438 SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
49439 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
49440 walLockName(lockIdx), n, rc ? "failed" : "ok"));
49441 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
@@ -49717,11 +49857,11 @@
49717 assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
49718 assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
49719 assert( pWal->writeLock );
49720 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
49721 nLock = SQLITE_SHM_NLOCK - iLock;
49722 rc = walLockExclusive(pWal, iLock, nLock);
49723 if( rc ){
49724 return rc;
49725 }
49726 WALTRACE(("WAL%p: recovery begin...\n", pWal));
49727
@@ -50251,11 +50391,11 @@
50251 int lockIdx, /* Offset of first byte to lock */
50252 int n /* Number of bytes to lock */
50253 ){
50254 int rc;
50255 do {
50256 rc = walLockExclusive(pWal, lockIdx, n);
50257 }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
50258 return rc;
50259 }
50260
50261 /*
@@ -50684,11 +50824,11 @@
50684 if( pWal->readOnly & WAL_SHM_RDONLY ){
50685 if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
50686 walUnlockShared(pWal, WAL_WRITE_LOCK);
50687 rc = SQLITE_READONLY_RECOVERY;
50688 }
50689 }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
50690 pWal->writeLock = 1;
50691 if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
50692 badHdr = walIndexTryHdr(pWal, pChanged);
50693 if( badHdr ){
50694 /* If the wal-index header is still malformed even while holding
@@ -50890,11 +51030,11 @@
50890 {
50891 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
50892 && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
50893 ){
50894 for(i=1; i<WAL_NREADER; i++){
50895 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
50896 if( rc==SQLITE_OK ){
50897 mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
50898 mxI = i;
50899 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
50900 break;
@@ -51146,11 +51286,11 @@
51146 }
51147
51148 /* Only one writer allowed at a time. Get the write lock. Return
51149 ** SQLITE_BUSY if unable.
51150 */
51151 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
51152 if( rc ){
51153 return rc;
51154 }
51155 pWal->writeLock = 1;
51156
@@ -51291,11 +51431,11 @@
51291 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
51292 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
51293 if( pInfo->nBackfill>0 ){
51294 u32 salt1;
51295 sqlite3_randomness(4, &salt1);
51296 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
51297 if( rc==SQLITE_OK ){
51298 /* If all readers are using WAL_READ_LOCK(0) (in other words if no
51299 ** readers are currently using the WAL), then the transactions
51300 ** frames will overwrite the start of the existing log. Update the
51301 ** wal-index header to reflect this.
@@ -51616,11 +51756,11 @@
51616 if( pWal->readOnly ) return SQLITE_READONLY;
51617 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
51618
51619 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
51620 ** "checkpoint" lock on the database file. */
51621 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
51622 if( rc ){
51623 /* EVIDENCE-OF: R-10421-19736 If any other process is running a
51624 ** checkpoint operation at the same time, the lock cannot be obtained and
51625 ** SQLITE_BUSY is returned.
51626 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
@@ -52618,10 +52758,11 @@
52618
52619 /*
52620 ** Exit the recursive mutex on a Btree.
52621 */
52622 SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
 
52623 if( p->sharable ){
52624 assert( p->wantToLock>0 );
52625 p->wantToLock--;
52626 if( p->wantToLock==0 ){
52627 unlockBtreeMutex(p);
@@ -54796,12 +54937,12 @@
54796 /*
54797 ** The following asserts make sure that structures used by the btree are
54798 ** the right size. This is to guard against size changes that result
54799 ** when compiling on a different architecture.
54800 */
54801 assert( sizeof(i64)==8 || sizeof(i64)==4 );
54802 assert( sizeof(u64)==8 || sizeof(u64)==4 );
54803 assert( sizeof(u32)==4 );
54804 assert( sizeof(u16)==2 );
54805 assert( sizeof(Pgno)==4 );
54806
54807 pBt = sqlite3MallocZero( sizeof(*pBt) );
@@ -60259,11 +60400,12 @@
60259 ** the previous call, as the overflow cell data will have been
60260 ** copied either into the body of a database page or into the new
60261 ** pSpace buffer passed to the latter call to balance_nonroot().
60262 */
60263 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
60264 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints);
 
60265 if( pFree ){
60266 /* If pFree is not NULL, it points to the pSpace buffer used
60267 ** by a previous call to balance_nonroot(). Its contents are
60268 ** now stored either on real database pages or within the
60269 ** new pSpace buffer, so it may be safely freed here. */
@@ -61922,17 +62064,26 @@
61922 pBt->btsFlags &= ~BTS_NO_WAL;
61923 return rc;
61924 }
61925
61926 /*
61927 ** set the mask of hint flags for cursor pCsr. Currently the only valid
61928 ** values are 0 and BTREE_BULKLOAD.
61929 */
61930 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
61931 assert( mask==BTREE_BULKLOAD || mask==0 );
61932 pCsr->hints = mask;
61933 }
 
 
 
 
 
 
 
 
 
 
61934
61935 /*
61936 ** Return true if the given Btree is read-only.
61937 */
61938 SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
@@ -63822,11 +63973,11 @@
63822 ** by calling sqlite3ValueNew().
63823 **
63824 ** Otherwise, if the second argument is non-zero, then this function is
63825 ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
63826 ** already been allocated, allocate the UnpackedRecord structure that
63827 ** that function will return to its caller here. Then return a pointer
63828 ** an sqlite3_value within the UnpackedRecord.a[] array.
63829 */
63830 static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
63831 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
63832 if( p ){
@@ -63866,10 +64017,117 @@
63866 UNUSED_PARAMETER(p);
63867 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
63868 return sqlite3ValueNew(db);
63869 }
63870
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63871 /*
63872 ** Extract a value from the supplied expression in the manner described
63873 ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
63874 ** using valueNew().
63875 **
@@ -63897,10 +64155,16 @@
63897 *ppVal = 0;
63898 return SQLITE_OK;
63899 }
63900 while( (op = pExpr->op)==TK_UPLUS ) pExpr = pExpr->pLeft;
63901 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
 
 
 
 
 
 
63902
63903 if( op==TK_CAST ){
63904 u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
63905 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
63906 testcase( rc!=SQLITE_OK );
@@ -63973,10 +64237,16 @@
63973 assert( zVal[nVal]=='\'' );
63974 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
63975 0, SQLITE_DYNAMIC);
63976 }
63977 #endif
 
 
 
 
 
 
63978
63979 *ppVal = pVal;
63980 return rc;
63981
63982 no_mem:
@@ -65426,11 +65696,11 @@
65426 break;
65427 }
65428 #ifndef SQLITE_OMIT_VIRTUALTABLE
65429 case P4_VTAB: {
65430 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
65431 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
65432 break;
65433 }
65434 #endif
65435 case P4_INTARRAY: {
65436 sqlite3_snprintf(nTemp, zTemp, "intarray");
@@ -66090,13 +66360,13 @@
66090 }
66091 #ifndef SQLITE_OMIT_VIRTUALTABLE
66092 else if( pCx->pVtabCursor ){
66093 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
66094 const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
66095 p->inVtabMethod = 1;
 
66096 pModule->xClose(pVtabCursor);
66097 p->inVtabMethod = 0;
66098 }
66099 #endif
66100 }
66101
66102 /*
@@ -66451,11 +66721,11 @@
66451
66452 /* Delete the master journal file. This commits the transaction. After
66453 ** doing this the directory is synced again before any individual
66454 ** transaction files are deleted.
66455 */
66456 rc = sqlite3OsDelete(pVfs, zMaster, 1);
66457 sqlite3DbFree(db, zMaster);
66458 zMaster = 0;
66459 if( rc ){
66460 return rc;
66461 }
@@ -68864,11 +69134,11 @@
68864 }
68865 SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
68866 pCtx->isError = errCode;
68867 pCtx->fErrorOrAux = 1;
68868 #ifdef SQLITE_DEBUG
68869 pCtx->pVdbe->rcApp = errCode;
68870 #endif
68871 if( pCtx->pOut->flags & MEM_Null ){
68872 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
68873 SQLITE_UTF8, SQLITE_STATIC);
68874 }
@@ -69127,20 +69397,30 @@
69127 assert( p && p->pFunc );
69128 return p->pOut->db;
69129 }
69130
69131 /*
69132 ** Return the current time for a statement
 
 
 
 
69133 */
69134 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
69135 Vdbe *v = p->pVdbe;
69136 int rc;
69137 if( v->iCurrentTime==0 ){
69138 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, &v->iCurrentTime);
69139 if( rc ) v->iCurrentTime = 0;
 
 
 
 
 
 
 
69140 }
69141 return v->iCurrentTime;
69142 }
69143
69144 /*
69145 ** The following is the implementation of an SQL function that always
69146 ** fails with an error message stating that the function is used in the
@@ -69206,10 +69486,15 @@
69206 */
69207 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
69208 AuxData *pAuxData;
69209
69210 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
 
 
 
 
 
69211 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
69212 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
69213 }
69214
69215 return (pAuxData ? pAuxData->pAux : 0);
@@ -69229,10 +69514,15 @@
69229 AuxData *pAuxData;
69230 Vdbe *pVdbe = pCtx->pVdbe;
69231
69232 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
69233 if( iArg<0 ) goto failed;
 
 
 
 
 
69234
69235 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
69236 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
69237 }
69238 if( pAuxData==0 ){
@@ -71845,11 +72135,11 @@
71845 ** max() aggregate will set to 1 if the current row is not the minimum or
71846 ** maximum. The P1 register is initialized to 0 by this instruction.
71847 **
71848 ** The interface used by the implementation of the aforementioned functions
71849 ** to retrieve the collation sequence set by this opcode is not available
71850 ** publicly, only to user functions defined in func.c.
71851 */
71852 case OP_CollSeq: {
71853 assert( pOp->p4type==P4_COLLSEQ );
71854 if( pOp->p1 ){
71855 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
@@ -73563,35 +73853,33 @@
73563 ** cursors or a single read/write cursor but not both.
73564 **
73565 ** See also OpenRead.
73566 */
73567 case OP_ReopenIdx: {
73568 VdbeCursor *pCur;
73569
73570 assert( pOp->p5==0 );
73571 assert( pOp->p4type==P4_KEYINFO );
73572 pCur = p->apCsr[pOp->p1];
73573 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
73574 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
73575 break;
73576 }
73577 /* If the cursor is not currently open or is open on a different
73578 ** index, then fall through into OP_OpenRead to force a reopen */
73579 }
73580 case OP_OpenRead:
73581 case OP_OpenWrite: {
73582 int nField;
73583 KeyInfo *pKeyInfo;
73584 int p2;
73585 int iDb;
73586 int wrFlag;
73587 Btree *pX;
73588 VdbeCursor *pCur;
73589 Db *pDb;
73590
73591 assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
73592 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
 
 
 
 
 
 
 
 
 
 
 
 
73593 assert( p->bIsReader );
73594 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
73595 || p->readOnly==0 );
73596
73597 if( p->expired ){
@@ -73650,18 +73938,21 @@
73650 pCur->nullRow = 1;
73651 pCur->isOrdered = 1;
73652 pCur->pgnoRoot = p2;
73653 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
73654 pCur->pKeyInfo = pKeyInfo;
73655 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
73656 sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
73657
73658 /* Set the VdbeCursor.isTable variable. Previous versions of
73659 ** SQLite used to check if the root-page flags were sane at this point
73660 ** and report database corruption if they were not, but this check has
73661 ** since moved into the btree layer. */
73662 pCur->isTable = pOp->p4type!=P4_KEYINFO;
 
 
 
 
 
 
73663 break;
73664 }
73665
73666 /* Opcode: OpenEphemeral P1 P2 * P4 P5
73667 ** Synopsis: nColumn=P2
@@ -73918,10 +74209,26 @@
73918 oc = pOp->opcode;
73919 pC->nullRow = 0;
73920 #ifdef SQLITE_DEBUG
73921 pC->seekOp = pOp->opcode;
73922 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73923 if( pC->isTable ){
73924 /* The input value in P3 might be of any type: integer, real, string,
73925 ** blob, or NULL. But it needs to be an integer before we can do
73926 ** the seek, so convert it. */
73927 pIn3 = &aMem[pOp->p3];
@@ -75257,34 +75564,19 @@
75257 **
75258 ** See also: Clear
75259 */
75260 case OP_Destroy: { /* out2-prerelease */
75261 int iMoved;
75262 int iCnt;
75263 Vdbe *pVdbe;
75264 int iDb;
75265
75266 assert( p->readOnly==0 );
75267 #ifndef SQLITE_OMIT_VIRTUALTABLE
75268 iCnt = 0;
75269 for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
75270 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader
75271 && pVdbe->inVtabMethod<2 && pVdbe->pc>=0
75272 ){
75273 iCnt++;
75274 }
75275 }
75276 #else
75277 iCnt = db->nVdbeRead;
75278 #endif
75279 pOut->flags = MEM_Null;
75280 if( iCnt>1 ){
75281 rc = SQLITE_LOCKED;
75282 p->errorAction = OE_Abort;
75283 }else{
75284 iDb = pOp->p3;
75285 assert( iCnt==1 );
75286 assert( DbMaskTest(p->btreeMask, iDb) );
75287 iMoved = 0; /* Not needed. Only to silence a warning. */
75288 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
75289 pOut->flags = MEM_Int;
75290 pOut->u.i = iMoved;
@@ -76337,17 +76629,33 @@
76337 break;
76338 }
76339 #endif /* SQLITE_OMIT_VIRTUALTABLE */
76340
76341 #ifndef SQLITE_OMIT_VIRTUALTABLE
76342 /* Opcode: VCreate P1 * * P4 *
76343 **
76344 ** P4 is the name of a virtual table in database P1. Call the xCreate method
76345 ** for that table.
76346 */
76347 case OP_VCreate: {
76348 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76349 break;
76350 }
76351 #endif /* SQLITE_OMIT_VIRTUALTABLE */
76352
76353 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -76355,13 +76663,13 @@
76355 **
76356 ** P4 is the name of a virtual table in database P1. Call the xDestroy method
76357 ** of that table.
76358 */
76359 case OP_VDestroy: {
76360 p->inVtabMethod = 2;
76361 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
76362 p->inVtabMethod = 0;
76363 break;
76364 }
76365 #endif /* SQLITE_OMIT_VIRTUALTABLE */
76366
76367 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -76373,18 +76681,21 @@
76373 */
76374 case OP_VOpen: {
76375 VdbeCursor *pCur;
76376 sqlite3_vtab_cursor *pVtabCursor;
76377 sqlite3_vtab *pVtab;
76378 sqlite3_module *pModule;
76379
76380 assert( p->bIsReader );
76381 pCur = 0;
76382 pVtabCursor = 0;
76383 pVtab = pOp->p4.pVtab->pVtab;
76384 pModule = (sqlite3_module *)pVtab->pModule;
76385 assert(pVtab && pModule);
 
 
 
76386 rc = pModule->xOpen(pVtab, &pVtabCursor);
76387 sqlite3VtabImportErrmsg(p, pVtab);
76388 if( SQLITE_OK==rc ){
76389 /* Initialize sqlite3_vtab_cursor base class */
76390 pVtabCursor->pVtab = pVtab;
@@ -76391,10 +76702,11 @@
76391
76392 /* Initialize vdbe cursor object */
76393 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
76394 if( pCur ){
76395 pCur->pVtabCursor = pVtabCursor;
 
76396 }else{
76397 db->mallocFailed = 1;
76398 pModule->xClose(pVtabCursor);
76399 }
76400 }
@@ -76456,13 +76768,11 @@
76456 apArg = p->apArg;
76457 for(i = 0; i<nArg; i++){
76458 apArg[i] = &pArgc[i+1];
76459 }
76460
76461 p->inVtabMethod = 1;
76462 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
76463 p->inVtabMethod = 0;
76464 sqlite3VtabImportErrmsg(p, pVtab);
76465 if( rc==SQLITE_OK ){
76466 res = pModule->xEof(pVtabCursor);
76467 }
76468 VdbeBranchTaken(res!=0,2);
@@ -76548,13 +76858,11 @@
76548 ** underlying implementation to return an error if one occurs during
76549 ** xNext(). Instead, if an error occurs, true is returned (indicating that
76550 ** data is available) and the error code returned when xColumn or
76551 ** some other method is next invoked on the save virtual table cursor.
76552 */
76553 p->inVtabMethod = 1;
76554 rc = pModule->xNext(pCur->pVtabCursor);
76555 p->inVtabMethod = 0;
76556 sqlite3VtabImportErrmsg(p, pVtab);
76557 if( rc==SQLITE_OK ){
76558 res = pModule->xEof(pCur->pVtabCursor);
76559 }
76560 VdbeBranchTaken(!res,2);
@@ -76625,11 +76933,11 @@
76625 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
76626 ** apply in the case of a constraint failure on an insert or update.
76627 */
76628 case OP_VUpdate: {
76629 sqlite3_vtab *pVtab;
76630 sqlite3_module *pModule;
76631 int nArg;
76632 int i;
76633 sqlite_int64 rowid;
76634 Mem **apArg;
76635 Mem *pX;
@@ -76637,11 +76945,15 @@
76637 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
76638 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
76639 );
76640 assert( p->readOnly==0 );
76641 pVtab = pOp->p4.pVtab->pVtab;
76642 pModule = (sqlite3_module *)pVtab->pModule;
 
 
 
 
76643 nArg = pOp->p2;
76644 assert( pOp->p4type==P4_VTAB );
76645 if( ALWAYS(pModule->xUpdate) ){
76646 u8 vtabOnConflict = db->vtabOnConflict;
76647 apArg = p->apArg;
@@ -78507,10 +78819,11 @@
78507 sqlite3 *db, /* Database handle doing sort */
78508 i64 nExtend, /* Attempt to extend file to this size */
78509 sqlite3_file **ppFd
78510 ){
78511 int rc;
 
78512 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
78513 SQLITE_OPEN_TEMP_JOURNAL |
78514 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
78515 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &rc
78516 );
@@ -82098,14 +82411,15 @@
82098 ** and the pExpr parameter is returned unchanged.
82099 */
82100 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(
82101 Parse *pParse, /* Parsing context */
82102 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
82103 const Token *pCollName /* Name of collating sequence */
 
82104 ){
82105 if( pCollName->n>0 ){
82106 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
82107 if( pNew ){
82108 pNew->pLeft = pExpr;
82109 pNew->flags |= EP_Collate|EP_Skip;
82110 pExpr = pNew;
82111 }
@@ -82115,11 +82429,11 @@
82115 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
82116 Token s;
82117 assert( zC!=0 );
82118 s.z = zC;
82119 s.n = sqlite3Strlen30(s.z);
82120 return sqlite3ExprAddCollateToken(pParse, pExpr, &s);
82121 }
82122
82123 /*
82124 ** Skip over any TK_COLLATE or TK_AS operators and any unlikely()
82125 ** or likelihood() function at the root of an expression.
@@ -82425,10 +82739,11 @@
82425 **
82426 ** Also propagate all EP_Propagate flags from the Expr.x.pList into
82427 ** Expr.flags.
82428 */
82429 SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
 
82430 exprSetHeight(p);
82431 sqlite3ExprCheckHeight(pParse, p->nHeight);
82432 }
82433
82434 /*
@@ -87139,11 +87454,14 @@
87139 /* Ensure the default expression is something that sqlite3ValueFromExpr()
87140 ** can handle (i.e. not CURRENT_TIME etc.)
87141 */
87142 if( pDflt ){
87143 sqlite3_value *pVal = 0;
87144 if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
 
 
 
87145 db->mallocFailed = 1;
87146 return;
87147 }
87148 if( !pVal ){
87149 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
@@ -93202,10 +93520,11 @@
93202 }
93203 if( pIdx->onError==OE_Default ){
93204 pIdx->onError = pIndex->onError;
93205 }
93206 }
 
93207 goto exit_create_index;
93208 }
93209 }
93210 }
93211
@@ -95661,11 +95980,13 @@
95661
95662 /*
95663 ** Return the collating function associated with a function.
95664 */
95665 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
95666 VdbeOp *pOp = &context->pVdbe->aOp[context->iOp-1];
 
 
95667 assert( pOp->opcode==OP_CollSeq );
95668 assert( pOp->p4type==P4_COLLSEQ );
95669 return pOp->p4.pColl;
95670 }
95671
@@ -109678,11 +109999,11 @@
109678 sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
109679
109680 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
109681 pEList = pLeft->pEList;
109682 if( pCte->pCols ){
109683 if( pEList->nExpr!=pCte->pCols->nExpr ){
109684 sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
109685 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
109686 );
109687 pParse->pWith = pSavedWith;
109688 return SQLITE_ERROR;
@@ -114104,10 +114425,11 @@
114104 */
114105 if( !db->init.busy ){
114106 char *zStmt;
114107 char *zWhere;
114108 int iDb;
 
114109 Vdbe *v;
114110
114111 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
114112 if( pEnd ){
114113 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
@@ -114138,12 +114460,14 @@
114138 sqlite3ChangeCookie(pParse, iDb);
114139
114140 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
114141 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
114142 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
114143 sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
114144 pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
 
 
114145 }
114146
114147 /* If we are rereading the sqlite_master table create the in-memory
114148 ** record of the table. The xConnect() method is not called until
114149 ** the first time the virtual table is used in an SQL statement. This
@@ -114492,15 +114816,19 @@
114492 int rc = SQLITE_OK;
114493 Table *pTab;
114494
114495 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
114496 if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
114497 VTable *p = vtabDisconnectAll(db, pTab);
114498
114499 assert( rc==SQLITE_OK );
 
 
 
 
 
114500 rc = p->pMod->pModule->xDestroy(p->pVtab);
114501
114502 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
114503 if( rc==SQLITE_OK ){
114504 assert( pTab->pVTable==p && p->pNext==0 );
114505 p->pVtab = 0;
114506 pTab->pVTable = 0;
@@ -116078,10 +116406,83 @@
116078 static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
116079 pWC->a[iChild].iParent = iParent;
116080 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
116081 pWC->a[iParent].nChild++;
116082 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116083
116084 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
116085 /*
116086 ** Analyze a term that consists of two or more OR-connected
116087 ** subterms. So in:
@@ -116103,10 +116504,11 @@
116103 ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
116104 ** (B) x=expr1 OR expr2=x OR x=expr3
116105 ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
116106 ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
116107 ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
 
116108 **
116109 ** CASE 1:
116110 **
116111 ** If all subterms are of the form T.C=expr for some single column of C and
116112 ** a single table T (as shown in example B above) then create a new virtual
@@ -116118,10 +116520,20 @@
116118 ** then create a new virtual term like this:
116119 **
116120 ** x IN (expr1,expr2,expr3)
116121 **
116122 ** CASE 2:
 
 
 
 
 
 
 
 
 
 
116123 **
116124 ** If all subterms are indexable by a single table T, then set
116125 **
116126 ** WhereTerm.eOperator = WO_OR
116127 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
@@ -116245,15 +116657,29 @@
116245 }
116246 }
116247 }
116248
116249 /*
116250 ** Record the set of tables that satisfy case 2. The set might be
116251 ** empty.
116252 */
116253 pOrInfo->indexable = indexable;
116254 pTerm->eOperator = indexable==0 ? 0 : WO_OR;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116255
116256 /*
116257 ** chngToIN holds a set of tables that *might* satisfy case 1. But
116258 ** we have to do some additional checking to see if case 1 really
116259 ** is satisfied.
@@ -116380,11 +116806,11 @@
116380 pTerm = &pWC->a[idxTerm];
116381 markTermAsChild(pWC, idxNew, idxTerm);
116382 }else{
116383 sqlite3ExprListDelete(db, pList);
116384 }
116385 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */
116386 }
116387 }
116388 }
116389 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
116390
@@ -116575,11 +117001,11 @@
116575 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
116576 Expr *pNewExpr1;
116577 Expr *pNewExpr2;
116578 int idxNew1;
116579 int idxNew2;
116580 Token sCollSeqName; /* Name of collating sequence */
116581 const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
116582
116583 pLeft = pExpr->x.pList->a[1].pExpr;
116584 pStr2 = sqlite3ExprDup(db, pStr1, 0);
116585
@@ -116611,23 +117037,22 @@
116611 if( c=='A'-1 ) isComplete = 0;
116612 c = sqlite3UpperToLower[c];
116613 }
116614 *pC = c + 1;
116615 }
116616 sCollSeqName.z = noCase ? "NOCASE" : "BINARY";
116617 sCollSeqName.n = 6;
116618 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
116619 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
116620 sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
116621 pStr1, 0);
116622 transferJoinMarkings(pNewExpr1, pExpr);
116623 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
116624 testcase( idxNew1==0 );
116625 exprAnalyze(pSrc, pWC, idxNew1);
116626 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
116627 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
116628 sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
116629 pStr2, 0);
116630 transferJoinMarkings(pNewExpr2, pExpr);
116631 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
116632 testcase( idxNew2==0 );
116633 exprAnalyze(pSrc, pWC, idxNew2);
@@ -117240,15 +117665,18 @@
117240 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
117241 /*
117242 ** Estimate the location of a particular key among all keys in an
117243 ** index. Store the results in aStat as follows:
117244 **
117245 ** aStat[0] Est. number of rows less than pVal
117246 ** aStat[1] Est. number of rows equal to pVal
117247 **
117248 ** Return the index of the sample that is the smallest sample that
117249 ** is greater than or equal to pRec.
 
 
 
117250 */
117251 static int whereKeyStats(
117252 Parse *pParse, /* Database connection */
117253 Index *pIdx, /* Index to consider domain of */
117254 UnpackedRecord *pRec, /* Vector of values to consider */
@@ -117255,71 +117683,162 @@
117255 int roundUp, /* Round up if true. Round down if false */
117256 tRowcnt *aStat /* OUT: stats written here */
117257 ){
117258 IndexSample *aSample = pIdx->aSample;
117259 int iCol; /* Index of required stats in anEq[] etc. */
 
 
117260 int iMin = 0; /* Smallest sample not yet tested */
117261 int i = pIdx->nSample; /* Smallest sample larger than or equal to pRec */
117262 int iTest; /* Next sample to test */
117263 int res; /* Result of comparison operation */
 
 
117264
117265 #ifndef SQLITE_DEBUG
117266 UNUSED_PARAMETER( pParse );
117267 #endif
117268 assert( pRec!=0 );
117269 iCol = pRec->nField - 1;
117270 assert( pIdx->nSample>0 );
117271 assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117272 do{
117273 iTest = (iMin+i)/2;
117274 res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117275 if( res<0 ){
 
 
 
 
117276 iMin = iTest+1;
 
117277 }else{
117278 i = iTest;
 
117279 }
117280 }while( res && iMin<i );
 
117281
117282 #ifdef SQLITE_DEBUG
117283 /* The following assert statements check that the binary search code
117284 ** above found the right answer. This block serves no purpose other
117285 ** than to invoke the asserts. */
117286 if( res==0 ){
117287 /* If (res==0) is true, then sample $i must be equal to pRec */
117288 assert( i<pIdx->nSample );
117289 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
117290 || pParse->db->mallocFailed );
117291 }else{
117292 /* Otherwise, pRec must be smaller than sample $i and larger than
117293 ** sample ($i-1). */
117294 assert( i==pIdx->nSample
117295 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
117296 || pParse->db->mallocFailed );
117297 assert( i==0
117298 || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
117299 || pParse->db->mallocFailed );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117300 }
117301 #endif /* ifdef SQLITE_DEBUG */
117302
117303 /* At this point, aSample[i] is the first sample that is greater than
117304 ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
117305 ** than pVal. If aSample[i]==pVal, then res==0.
117306 */
117307 if( res==0 ){
 
 
117308 aStat[0] = aSample[i].anLt[iCol];
117309 aStat[1] = aSample[i].anEq[iCol];
117310 }else{
117311 tRowcnt iLower, iUpper, iGap;
117312 if( i==0 ){
117313 iLower = 0;
117314 iUpper = aSample[0].anLt[iCol];
 
 
117315 }else{
117316 i64 nRow0 = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
117317 iUpper = i>=pIdx->nSample ? nRow0 : aSample[i].anLt[iCol];
117318 iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
117319 }
117320 aStat[1] = pIdx->aAvgEq[iCol];
117321 if( iLower>=iUpper ){
117322 iGap = 0;
117323 }else{
117324 iGap = iUpper - iLower;
117325 }
@@ -117327,11 +117846,15 @@
117327 iGap = (iGap*2)/3;
117328 }else{
117329 iGap = iGap/3;
117330 }
117331 aStat[0] = iLower + iGap;
 
117332 }
 
 
 
117333 return i;
117334 }
117335 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
117336
117337 /*
@@ -118322,25 +118845,33 @@
118322 #else
118323 # define addScanStatus(a, b, c, d) ((void)d)
118324 #endif
118325
118326 /*
118327 ** Look at the last instruction coded. If that instruction is OP_String8
118328 ** and if pLoop->iLikeRepCntr is non-zero, then change the P3 to be
118329 ** pLoop->iLikeRepCntr and set P5.
118330 **
118331 ** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
118332 ** expression: "x>='ABC' AND x<'abd'". But this requires that the range
118333 ** scan loop run twice, once for strings and a second time for BLOBs.
118334 ** The OP_String opcodes on the second pass convert the upper and lower
118335 ** bound string contants to blobs. This routine makes the necessary changes
118336 ** to the OP_String opcodes for that to happen.
118337 */
118338 static void whereLikeOptimizationStringFixup(Vdbe *v, WhereLevel *pLevel){
118339 VdbeOp *pOp;
118340 pOp = sqlite3VdbeGetOp(v, -1);
118341 if( pLevel->iLikeRepCntr && pOp->opcode==OP_String8 ){
 
 
 
 
 
 
 
 
118342 pOp->p3 = pLevel->iLikeRepCntr;
118343 pOp->p5 = 1;
118344 }
118345 }
118346
@@ -118670,18 +119201,20 @@
118670 */
118671 j = nEq;
118672 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
118673 pRangeStart = pLoop->aLTerm[j++];
118674 nExtraReg = 1;
 
 
 
118675 }
118676 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
118677 pRangeEnd = pLoop->aLTerm[j++];
118678 nExtraReg = 1;
118679 if( pRangeStart
118680 && (pRangeStart->wtFlags & TERM_LIKEOPT)!=0
118681 && (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0
118682 ){
118683 pLevel->iLikeRepCntr = ++pParse->nMem;
118684 testcase( bRev );
118685 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
118686 sqlite3VdbeAddOp2(v, OP_Integer,
118687 bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
@@ -118729,11 +119262,11 @@
118729 /* Seek the index cursor to the start of the range. */
118730 nConstraint = nEq;
118731 if( pRangeStart ){
118732 Expr *pRight = pRangeStart->pExpr->pRight;
118733 sqlite3ExprCode(pParse, pRight, regBase+nEq);
118734 whereLikeOptimizationStringFixup(v, pLevel);
118735 if( (pRangeStart->wtFlags & TERM_VNULL)==0
118736 && sqlite3ExprCanBeNull(pRight)
118737 ){
118738 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
118739 VdbeCoverage(v);
@@ -118775,11 +119308,11 @@
118775 nConstraint = nEq;
118776 if( pRangeEnd ){
118777 Expr *pRight = pRangeEnd->pExpr->pRight;
118778 sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
118779 sqlite3ExprCode(pParse, pRight, regBase+nEq);
118780 whereLikeOptimizationStringFixup(v, pLevel);
118781 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
118782 && sqlite3ExprCanBeNull(pRight)
118783 ){
118784 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
118785 VdbeCoverage(v);
@@ -119852,10 +120385,14 @@
119852 ){
119853 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
119854 }
119855 if( pTerm->prereqRight & pNew->maskSelf ) continue;
119856
 
 
 
 
119857 pNew->wsFlags = saved_wsFlags;
119858 pNew->u.btree.nEq = saved_nEq;
119859 pNew->nLTerm = saved_nLTerm;
119860 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
119861 pNew->aLTerm[pNew->nLTerm++] = pTerm;
@@ -119895,10 +120432,21 @@
119895 testcase( eOp & WO_GT );
119896 testcase( eOp & WO_GE );
119897 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
119898 pBtm = pTerm;
119899 pTop = 0;
 
 
 
 
 
 
 
 
 
 
 
119900 }else{
119901 assert( eOp & (WO_LT|WO_LE) );
119902 testcase( eOp & WO_LT );
119903 testcase( eOp & WO_LE );
119904 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
@@ -121089,14 +121637,14 @@
121089 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] );
121090 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX );
121091
121092 /* Seed the search with a single WherePath containing zero WhereLoops.
121093 **
121094 ** TUNING: Do not let the number of iterations go above 25. If the cost
121095 ** of computing an automatic index is not paid back within the first 25
121096 ** rows, then do not use the automatic index. */
121097 aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==sqlite3LogEst(25) );
121098 nFrom = 1;
121099 assert( aFrom[0].isOrdered==0 );
121100 if( nOrderBy ){
121101 /* If nLoop is zero, then there are no FROM terms in the query. Since
121102 ** in this case the query may return a maximum of one row, the results
@@ -121890,10 +122438,16 @@
121890 assert( pIx->pSchema==pTab->pSchema );
121891 assert( iIndexCur>=0 );
121892 if( op ){
121893 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
121894 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
 
 
 
 
 
 
121895 VdbeComment((v, "%s", pIx->zName));
121896 }
121897 }
121898 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
121899 notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
@@ -124891,11 +125445,11 @@
124891 spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
124892 }
124893 break;
124894 case 193: /* expr ::= expr COLLATE ID|STRING */
124895 {
124896 yygotominor.yy346.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy346.pExpr, &yymsp[0].minor.yy0);
124897 yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart;
124898 yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
124899 }
124900 break;
124901 case 194: /* expr ::= CAST LP expr AS typetoken RP */
@@ -125171,20 +125725,20 @@
125171 case 241: /* uniqueflag ::= */
125172 {yygotominor.yy328 = OE_None;}
125173 break;
125174 case 244: /* idxlist ::= idxlist COMMA nm collate sortorder */
125175 {
125176 Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
125177 yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, p);
125178 sqlite3ExprListSetName(pParse,yygotominor.yy14,&yymsp[-2].minor.yy0,1);
125179 sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
125180 if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
125181 }
125182 break;
125183 case 245: /* idxlist ::= nm collate sortorder */
125184 {
125185 Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
125186 yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, p);
125187 sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
125188 sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
125189 if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
125190 }
@@ -126439,13 +126993,15 @@
126439 pParse->zTail = &zSql[i];
126440 }
126441 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
126442 }
126443 #ifdef YYTRACKMAXSTACKDEPTH
 
126444 sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
126445 sqlite3ParserStackPeak(pEngine)
126446 );
 
126447 #endif /* YYDEBUG */
126448 sqlite3ParserFree(pEngine, sqlite3_free);
126449 db->lookaside.bEnabled = enableLookaside;
126450 if( db->mallocFailed ){
126451 pParse->rc = SQLITE_NOMEM;
@@ -127011,10 +127567,15 @@
127011 rc = sqlite3_wsd_init(4096, 24);
127012 if( rc!=SQLITE_OK ){
127013 return rc;
127014 }
127015 #endif
 
 
 
 
 
127016
127017 /* If SQLite is already completely initialized, then this call
127018 ** to sqlite3_initialize() should be a no-op. But the initialization
127019 ** must be complete. So isInit must not be set until the very end
127020 ** of this routine.
@@ -132766,15 +133327,20 @@
132766 ** the output value undefined. Otherwise SQLITE_OK is returned.
132767 **
132768 ** This function is used when parsing the "prefix=" FTS4 parameter.
132769 */
132770 static int fts3GobbleInt(const char **pp, int *pnOut){
 
132771 const char *p; /* Iterator pointer */
132772 int nInt = 0; /* Output value */
132773
132774 for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
132775 nInt = nInt * 10 + (p[0] - '0');
 
 
 
 
132776 }
132777 if( p==*pp ) return SQLITE_ERROR;
132778 *pnOut = nInt;
132779 *pp = p;
132780 return SQLITE_OK;
@@ -132813,27 +133379,33 @@
132813 }
132814 }
132815
132816 aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
132817 *apIndex = aIndex;
132818 *pnIndex = nIndex;
132819 if( !aIndex ){
132820 return SQLITE_NOMEM;
132821 }
132822
132823 memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
132824 if( zParam ){
132825 const char *p = zParam;
132826 int i;
132827 for(i=1; i<nIndex; i++){
132828 int nPrefix;
132829 if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
132830 aIndex[i].nPrefix = nPrefix;
 
 
 
 
 
 
132831 p++;
132832 }
132833 }
132834
 
132835 return SQLITE_OK;
132836 }
132837
132838 /*
132839 ** This function is called when initializing an FTS4 table that uses the
@@ -140609,11 +141181,11 @@
140609 nName = sqlite3_value_bytes(argv[0])+1;
140610
140611 if( argc==2 ){
140612 void *pOld;
140613 int n = sqlite3_value_bytes(argv[1]);
140614 if( n!=sizeof(pPtr) ){
140615 sqlite3_result_error(context, "argument type mismatch", -1);
140616 return;
140617 }
140618 pPtr = *(void **)sqlite3_value_blob(argv[1]);
140619 pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
@@ -140620,11 +141192,13 @@
140620 if( pOld==pPtr ){
140621 sqlite3_result_error(context, "out of memory", -1);
140622 return;
140623 }
140624 }else{
140625 pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
 
 
140626 if( !pPtr ){
140627 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
140628 sqlite3_result_error(context, zErr, -1);
140629 sqlite3_free(zErr);
140630 return;
@@ -140701,10 +141275,14 @@
140701 zCopy = sqlite3_mprintf("%s", zArg);
140702 if( !zCopy ) return SQLITE_NOMEM;
140703 zEnd = &zCopy[strlen(zCopy)];
140704
140705 z = (char *)sqlite3Fts3NextToken(zCopy, &n);
 
 
 
 
140706 z[n] = '\0';
140707 sqlite3Fts3Dequote(z);
140708
140709 m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
140710 if( !m ){
@@ -143346,11 +143924,14 @@
143346 /*
143347 ** This is a comparison function used as a qsort() callback when sorting
143348 ** an array of pending terms by term. This occurs as part of flushing
143349 ** the contents of the pending-terms hash table to the database.
143350 */
143351 static int fts3CompareElemByTerm(const void *lhs, const void *rhs){
 
 
 
143352 char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
143353 char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
143354 int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
143355 int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
143356
143357
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -261,10 +261,17 @@
261 #ifndef SQLITE_API
262 # define SQLITE_API
263 #endif
264
265
266 /*
267 ** Add the ability to override 'cdecl'
268 */
269 #ifndef SQLITE_CDECL
270 # define SQLITE_CDECL
271 #endif
272
273 /*
274 ** These no-op macros are used in front of interfaces to mark those
275 ** interfaces as either deprecated or experimental. New applications
276 ** should not use deprecated interfaces - they are supported for backwards
277 ** compatibility only. Application writers should be aware that
@@ -316,11 +323,11 @@
323 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
324 ** [sqlite_version()] and [sqlite_source_id()].
325 */
326 #define SQLITE_VERSION "3.8.9"
327 #define SQLITE_VERSION_NUMBER 3008009
328 #define SQLITE_SOURCE_ID "2015-03-24 18:19:39 436314b5728c9413f9ac2d837e1c19364f31be72"
329
330 /*
331 ** CAPI3REF: Run-Time Library Version Numbers
332 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
333 **
@@ -1157,10 +1164,17 @@
1164 ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
1165 ** opcode causes the xFileControl method to swap the file handle with the one
1166 ** pointed to by the pArg argument. This capability is used during testing
1167 ** and only needs to be supported when SQLITE_TEST is defined.
1168 **
1169 ** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1170 ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
1171 ** be advantageous to block on the next WAL lock if the lock is not immediately
1172 ** available. The WAL subsystem issues this signal during rare
1173 ** circumstances in order to fix a problem with priority inversion.
1174 ** Applications should <em>not</em> use this file-control.
1175 **
1176 ** </ul>
1177 */
1178 #define SQLITE_FCNTL_LOCKSTATE 1
1179 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
1180 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1181,10 +1195,11 @@
1195 #define SQLITE_FCNTL_TRACE 19
1196 #define SQLITE_FCNTL_HAS_MOVED 20
1197 #define SQLITE_FCNTL_SYNC 21
1198 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
1199 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
1200 #define SQLITE_FCNTL_WAL_BLOCK 24
1201
1202 /* deprecated names */
1203 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1204 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1205 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -1747,11 +1762,11 @@
1762 ** disabled, the following SQLite interfaces become non-operational:
1763 ** <ul>
1764 ** <li> [sqlite3_memory_used()]
1765 ** <li> [sqlite3_memory_highwater()]
1766 ** <li> [sqlite3_soft_heap_limit64()]
1767 ** <li> [sqlite3_status64()]
1768 ** </ul>)^
1769 ** ^Memory allocation statistics are enabled by default unless SQLite is
1770 ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1771 ** allocation statistics are disabled by default.
1772 ** </dd>
@@ -3204,15 +3219,17 @@
3219
3220
3221 /*
3222 ** CAPI3REF: Error Codes And Messages
3223 **
3224 ** ^If the most recent sqlite3_* API call associated with
3225 ** [database connection] D failed, then the sqlite3_errcode(D) interface
3226 ** returns the numeric [result code] or [extended result code] for that
3227 ** API call.
3228 ** If the most recent API call was successful,
3229 ** then the return value from sqlite3_errcode() is undefined.
3230 ** ^The sqlite3_extended_errcode()
3231 ** interface is the same except that it always returns the
3232 ** [extended result code] even when extended result codes are
3233 ** disabled.
3234 **
3235 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
@@ -5822,11 +5839,11 @@
5839 ** is delivered up to the client application, the string will be automatically
5840 ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
5841 */
5842 struct sqlite3_vtab {
5843 const sqlite3_module *pModule; /* The module for this virtual table */
5844 int nRef; /* Number of open cursors */
5845 char *zErrMsg; /* Error message from sqlite3_mprintf() */
5846 /* Virtual table implementations will typically add additional fields */
5847 };
5848
5849 /*
@@ -6500,11 +6517,11 @@
6517 #define SQLITE_TESTCTRL_LAST 25
6518
6519 /*
6520 ** CAPI3REF: SQLite Runtime Status
6521 **
6522 ** ^These interfaces are used to retrieve runtime status information
6523 ** about the performance of SQLite, and optionally to reset various
6524 ** highwater marks. ^The first argument is an integer code for
6525 ** the specific parameter to measure. ^(Recognized integer codes
6526 ** are of the form [status parameters | SQLITE_STATUS_...].)^
6527 ** ^The current value of the parameter is returned into *pCurrent.
@@ -6514,23 +6531,26 @@
6531 ** value. For those parameters
6532 ** nothing is written into *pHighwater and the resetFlag is ignored.)^
6533 ** ^(Other parameters record only the highwater mark and not the current
6534 ** value. For these latter parameters nothing is written into *pCurrent.)^
6535 **
6536 ** ^The sqlite3_status() and sqlite3_status64() routines return
6537 ** SQLITE_OK on success and a non-zero [error code] on failure.
6538 **
6539 ** If either the current value or the highwater mark is too large to
6540 ** be represented by a 32-bit integer, then the values returned by
6541 ** sqlite3_status() are undefined.
 
 
 
6542 **
6543 ** See also: [sqlite3_db_status()]
6544 */
6545 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
6546 SQLITE_API int sqlite3_status64(
6547 int op,
6548 sqlite3_int64 *pCurrent,
6549 sqlite3_int64 *pHighwater,
6550 int resetFlag
6551 );
6552
6553
6554 /*
6555 ** CAPI3REF: Status Parameters
6556 ** KEYWORDS: {status parameters}
@@ -8911,10 +8931,24 @@
8931 **
8932 ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
8933 */
8934 typedef INT16_TYPE LogEst;
8935
8936 /*
8937 ** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
8938 */
8939 #ifndef SQLITE_PTRSIZE
8940 # if defined(__SIZEOF_POINTER__)
8941 # define SQLITE_PTRSIZE __SIZEOF_POINTER__
8942 # elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
8943 defined(_M_ARM) || defined(__arm__) || defined(__x86)
8944 # define SQLITE_PTRSIZE 4
8945 # else
8946 # define SQLITE_PTRSIZE 8
8947 # endif
8948 #endif
8949
8950 /*
8951 ** Macros to determine whether the machine is big or little endian,
8952 ** and whether or not that determination is run-time or compile-time.
8953 **
8954 ** For best performance, an attempt is made to guess at the byte-order
@@ -9361,12 +9395,22 @@
9395 #define BTREE_DATA_VERSION 15 /* A virtual meta-value */
9396
9397 /*
9398 ** Values that may be OR'd together to form the second argument of an
9399 ** sqlite3BtreeCursorHints() call.
9400 **
9401 ** The BTREE_BULKLOAD flag is set on index cursors when the index is going
9402 ** to be filled with content that is already in sorted order.
9403 **
9404 ** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
9405 ** OP_SeekLE opcodes for a range search, but where the range of entries
9406 ** selected will all have the same key. In other words, the cursor will
9407 ** be used only for equality key searches.
9408 **
9409 */
9410 #define BTREE_BULKLOAD 0x00000001 /* Used to full index in sorted order */
9411 #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
9412
9413 SQLITE_PRIVATE int sqlite3BtreeCursor(
9414 Btree*, /* BTree containing table to open */
9415 int iTable, /* Index of root page */
9416 int wrFlag, /* 1 for writing. 0 for read-only */
@@ -9408,10 +9452,13 @@
9452 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
9453 SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
9454 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
9455 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
9456 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
9457 #ifdef SQLITE_DEBUG
9458 SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
9459 #endif
9460 SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
9461 SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
9462
9463 #ifndef NDEBUG
9464 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
@@ -10908,10 +10955,11 @@
10955 } init;
10956 int nVdbeActive; /* Number of VDBEs currently running */
10957 int nVdbeRead; /* Number of active VDBEs that read or write */
10958 int nVdbeWrite; /* Number of active VDBEs that read and write */
10959 int nVdbeExec; /* Number of nested calls to VdbeExec() */
10960 int nVDestroy; /* Number of active OP_VDestroy operations */
10961 int nExtension; /* Number of loaded extensions */
10962 void **aExtension; /* Array of shared library handles */
10963 void (*xTrace)(void*,const char*); /* Trace function */
10964 void *pTraceArg; /* Argument to the trace function */
10965 void (*xProfile)(void*,const char*,u64); /* Profiling function */
@@ -12502,11 +12550,12 @@
12550 #define OPFLAG_APPEND 0x08 /* This is likely to be an append */
12551 #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
12552 #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
12553 #define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
12554 #define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
12555 #define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
12556 #define OPFLAG_P2ISREG 0x04 /* P2 to OP_Open** is a register number */
12557 #define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
12558
12559 /*
12560 * Each trigger present in the database schema is stored as an instance of
12561 * struct Trigger.
@@ -12906,14 +12955,19 @@
12955 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
12956 SQLITE_PRIVATE int sqlite3MutexInit(void);
12957 SQLITE_PRIVATE int sqlite3MutexEnd(void);
12958 #endif
12959
12960 SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int);
12961 SQLITE_PRIVATE void sqlite3StatusUp(int, int);
12962 SQLITE_PRIVATE void sqlite3StatusDown(int, int);
12963 SQLITE_PRIVATE void sqlite3StatusSet(int, int);
12964
12965 /* Access to mutexes used by sqlite3_status() */
12966 SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void);
12967 SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void);
12968
12969 #ifndef SQLITE_OMIT_FLOATING_POINT
12970 SQLITE_PRIVATE int sqlite3IsNaN(double);
12971 #else
12972 # define sqlite3IsNaN(X) 0
12973 #endif
@@ -13289,11 +13343,11 @@
13343 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
13344 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
13345 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
13346 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
13347 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
13348 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
13349 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
13350 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
13351 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
13352 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
13353 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
@@ -14676,18 +14730,10 @@
14730 ** An instance of the virtual machine. This structure contains the complete
14731 ** state of the virtual machine.
14732 **
14733 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
14734 ** is really a pointer to an instance of this structure.
 
 
 
 
 
 
 
 
14735 */
14736 struct Vdbe {
14737 sqlite3 *db; /* The database connection that owns this statement */
14738 Op *aOp; /* Space to hold the virtual machine's program */
14739 Mem *aMem; /* The memory locations */
@@ -14714,11 +14760,10 @@
14760 #endif
14761 u16 nResColumn; /* Number of columns in one row of the result set */
14762 u8 errorAction; /* Recovery action to do in case of an error */
14763 u8 minWriteFileFormat; /* Minimum file format for writable database files */
14764 bft explain:2; /* True if EXPLAIN present on SQL command */
 
14765 bft changeCntOn:1; /* True to update the change-counter */
14766 bft expired:1; /* True if the VM needs to be recompiled */
14767 bft runOnlyOnce:1; /* Automatically expire on reset */
14768 bft usesStmtJournal:1; /* True if uses a statement journal */
14769 bft readOnly:1; /* True for statements that do not write */
@@ -14874,13 +14919,35 @@
14919 /*
14920 ** Variables in which to record status information.
14921 */
14922 typedef struct sqlite3StatType sqlite3StatType;
14923 static SQLITE_WSD struct sqlite3StatType {
14924 #if SQLITE_PTRSIZE>4
14925 sqlite3_int64 nowValue[10]; /* Current value */
14926 sqlite3_int64 mxValue[10]; /* Maximum value */
14927 #else
14928 u32 nowValue[10]; /* Current value */
14929 u32 mxValue[10]; /* Maximum value */
14930 #endif
14931 } sqlite3Stat = { {0,}, {0,} };
14932
14933 /*
14934 ** Elements of sqlite3Stat[] are protected by either the memory allocator
14935 ** mutex, or by the pcache1 mutex. The following array determines which.
14936 */
14937 static const char statMutex[] = {
14938 0, /* SQLITE_STATUS_MEMORY_USED */
14939 1, /* SQLITE_STATUS_PAGECACHE_USED */
14940 1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
14941 0, /* SQLITE_STATUS_SCRATCH_USED */
14942 0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */
14943 0, /* SQLITE_STATUS_MALLOC_SIZE */
14944 0, /* SQLITE_STATUS_PARSER_STACK */
14945 1, /* SQLITE_STATUS_PAGECACHE_SIZE */
14946 0, /* SQLITE_STATUS_SCRATCH_SIZE */
14947 0, /* SQLITE_STATUS_MALLOC_COUNT */
14948 };
14949
14950
14951 /* The "wsdStat" macro will resolve to the status information
14952 ** state vector. If writable static data is unsupported on the target,
14953 ** we have to locate the state vector at run-time. In the more common
@@ -14894,64 +14961,110 @@
14961 # define wsdStatInit
14962 # define wsdStat sqlite3Stat
14963 #endif
14964
14965 /*
14966 ** Return the current value of a status parameter. The caller must
14967 ** be holding the appropriate mutex.
14968 */
14969 SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int op){
14970 wsdStatInit;
14971 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
14972 assert( op>=0 && op<ArraySize(statMutex) );
14973 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
14974 : sqlite3MallocMutex()) );
14975 return wsdStat.nowValue[op];
14976 }
14977
14978 /*
14979 ** Add N to the value of a status record. The caller must hold the
14980 ** appropriate mutex. (Locking is checked by assert()).
14981 **
14982 ** The StatusUp() routine can accept positive or negative values for N.
14983 ** The value of N is added to the current status value and the high-water
14984 ** mark is adjusted if necessary.
14985 **
14986 ** The StatusDown() routine lowers the current value by N. The highwater
14987 ** mark is unchanged. N must be non-negative for StatusDown().
14988 */
14989 SQLITE_PRIVATE void sqlite3StatusUp(int op, int N){
14990 wsdStatInit;
14991 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
14992 assert( op>=0 && op<ArraySize(statMutex) );
14993 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
14994 : sqlite3MallocMutex()) );
14995 wsdStat.nowValue[op] += N;
14996 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
14997 wsdStat.mxValue[op] = wsdStat.nowValue[op];
14998 }
14999 }
15000 SQLITE_PRIVATE void sqlite3StatusDown(int op, int N){
15001 wsdStatInit;
15002 assert( N>=0 );
15003 assert( op>=0 && op<ArraySize(statMutex) );
15004 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
15005 : sqlite3MallocMutex()) );
15006 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
15007 wsdStat.nowValue[op] -= N;
15008 }
15009
15010 /*
15011 ** Set the value of a status to X. The highwater mark is adjusted if
15012 ** necessary. The caller must hold the appropriate mutex.
15013 */
15014 SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
15015 wsdStatInit;
15016 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
15017 assert( op>=0 && op<ArraySize(statMutex) );
15018 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
15019 : sqlite3MallocMutex()) );
15020 wsdStat.nowValue[op] = X;
15021 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
15022 wsdStat.mxValue[op] = wsdStat.nowValue[op];
15023 }
15024 }
15025
15026 /*
15027 ** Query status information.
 
 
 
 
15028 */
15029 SQLITE_API int sqlite3_status64(
15030 int op,
15031 sqlite3_int64 *pCurrent,
15032 sqlite3_int64 *pHighwater,
15033 int resetFlag
15034 ){
15035 sqlite3_mutex *pMutex;
15036 wsdStatInit;
15037 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
15038 return SQLITE_MISUSE_BKPT;
15039 }
15040 #ifdef SQLITE_ENABLE_API_ARMOR
15041 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
15042 #endif
15043 pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
15044 sqlite3_mutex_enter(pMutex);
15045 *pCurrent = wsdStat.nowValue[op];
15046 *pHighwater = wsdStat.mxValue[op];
15047 if( resetFlag ){
15048 wsdStat.mxValue[op] = wsdStat.nowValue[op];
15049 }
15050 sqlite3_mutex_leave(pMutex);
15051 (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */
15052 return SQLITE_OK;
15053 }
15054 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
15055 sqlite3_int64 iCur, iHwtr;
15056 int rc;
15057 #ifdef SQLITE_ENABLE_API_ARMOR
15058 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
15059 #endif
15060 rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
15061 if( rc==0 ){
15062 *pCurrent = (int)iCur;
15063 *pHighwater = (int)iHwtr;
15064 }
15065 return rc;
15066 }
15067
15068 /*
15069 ** Query status information for a single database connection
15070 */
@@ -20401,10 +20514,17 @@
20514 */
20515 int nearlyFull;
20516 } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
20517
20518 #define mem0 GLOBAL(struct Mem0Global, mem0)
20519
20520 /*
20521 ** Return the memory allocator mutex. sqlite3_status() needs it.
20522 */
20523 SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){
20524 return mem0.mutex;
20525 }
20526
20527 /*
20528 ** This routine runs when the memory allocator sees that the
20529 ** total memory allocation is about to exceed the soft heap
20530 ** limit.
@@ -20424,11 +20544,11 @@
20544 static int sqlite3MemoryAlarm(
20545 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
20546 void *pArg,
20547 sqlite3_int64 iThreshold
20548 ){
20549 sqlite3_int64 nUsed;
20550 sqlite3_mutex_enter(mem0.mutex);
20551 mem0.alarmCallback = xCallback;
20552 mem0.alarmArg = pArg;
20553 mem0.alarmThreshold = iThreshold;
20554 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
@@ -20593,11 +20713,11 @@
20713 void *p;
20714 assert( sqlite3_mutex_held(mem0.mutex) );
20715 nFull = sqlite3GlobalConfig.m.xRoundup(n);
20716 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
20717 if( mem0.alarmCallback!=0 ){
20718 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
20719 if( nUsed >= mem0.alarmThreshold - nFull ){
20720 mem0.nearlyFull = 1;
20721 sqlite3MallocAlarm(nFull);
20722 }else{
20723 mem0.nearlyFull = 0;
@@ -20610,12 +20730,12 @@
20730 p = sqlite3GlobalConfig.m.xMalloc(nFull);
20731 }
20732 #endif
20733 if( p ){
20734 nFull = sqlite3MallocSize(p);
20735 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
20736 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
20737 }
20738 *pp = p;
20739 return nFull;
20740 }
20741
@@ -20688,18 +20808,18 @@
20808 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
20809 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
20810 p = mem0.pScratchFree;
20811 mem0.pScratchFree = mem0.pScratchFree->pNext;
20812 mem0.nScratchFree--;
20813 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
20814 sqlite3_mutex_leave(mem0.mutex);
20815 }else{
20816 sqlite3_mutex_leave(mem0.mutex);
20817 p = sqlite3Malloc(n);
20818 if( sqlite3GlobalConfig.bMemstat && p ){
20819 sqlite3_mutex_enter(mem0.mutex);
20820 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
20821 sqlite3_mutex_leave(mem0.mutex);
20822 }
20823 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
20824 }
20825 assert( sqlite3_mutex_notheld(mem0.mutex) );
@@ -20736,23 +20856,23 @@
20856 sqlite3_mutex_enter(mem0.mutex);
20857 pSlot->pNext = mem0.pScratchFree;
20858 mem0.pScratchFree = pSlot;
20859 mem0.nScratchFree++;
20860 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
20861 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
20862 sqlite3_mutex_leave(mem0.mutex);
20863 }else{
20864 /* Release memory back to the heap */
20865 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
20866 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
20867 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
20868 if( sqlite3GlobalConfig.bMemstat ){
20869 int iSize = sqlite3MallocSize(p);
20870 sqlite3_mutex_enter(mem0.mutex);
20871 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
20872 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
20873 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
20874 sqlite3GlobalConfig.m.xFree(p);
20875 sqlite3_mutex_leave(mem0.mutex);
20876 }else{
20877 sqlite3GlobalConfig.m.xFree(p);
20878 }
@@ -20779,26 +20899,26 @@
20899 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
20900 return sqlite3GlobalConfig.m.xSize(p);
20901 }
20902 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
20903 if( db==0 ){
20904 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
20905 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
20906 return sqlite3MallocSize(p);
20907 }else{
20908 assert( sqlite3_mutex_held(db->mutex) );
20909 if( isLookaside(db, p) ){
20910 return db->lookaside.sz;
20911 }else{
20912 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
20913 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
20914 return sqlite3GlobalConfig.m.xSize(p);
20915 }
20916 }
20917 }
20918 SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
20919 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
20920 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
20921 return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
20922 }
20923
20924 /*
@@ -20805,15 +20925,15 @@
20925 ** Free memory previously obtained from sqlite3Malloc().
20926 */
20927 SQLITE_API void sqlite3_free(void *p){
20928 if( p==0 ) return; /* IMP: R-49053-54554 */
20929 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
20930 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
20931 if( sqlite3GlobalConfig.bMemstat ){
20932 sqlite3_mutex_enter(mem0.mutex);
20933 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
20934 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
20935 sqlite3GlobalConfig.m.xFree(p);
20936 sqlite3_mutex_leave(mem0.mutex);
20937 }else{
20938 sqlite3GlobalConfig.m.xFree(p);
20939 }
@@ -20850,11 +20970,11 @@
20970 db->lookaside.nOut--;
20971 return;
20972 }
20973 }
20974 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
20975 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
20976 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
20977 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
20978 sqlite3_free(p);
20979 }
20980
@@ -20863,11 +20983,11 @@
20983 */
20984 SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
20985 int nOld, nNew, nDiff;
20986 void *pNew;
20987 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
20988 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
20989 if( pOld==0 ){
20990 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
20991 }
20992 if( nBytes==0 ){
20993 sqlite3_free(pOld); /* IMP: R-26507-47431 */
@@ -20897,11 +21017,11 @@
21017 sqlite3MallocAlarm((int)nBytes);
21018 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
21019 }
21020 if( pNew ){
21021 nNew = sqlite3MallocSize(pNew);
21022 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
21023 }
21024 sqlite3_mutex_leave(mem0.mutex);
21025 }else{
21026 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
21027 }
@@ -21030,11 +21150,11 @@
21150 memcpy(pNew, p, db->lookaside.sz);
21151 sqlite3DbFree(db, p);
21152 }
21153 }else{
21154 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
21155 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
21156 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
21157 pNew = sqlite3_realloc64(p, n);
21158 if( !pNew ){
21159 db->mallocFailed = 1;
21160 }
@@ -25287,10 +25407,11 @@
25407 #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
25408 #define UNIXFILE_DELETE 0x20 /* Delete on close */
25409 #define UNIXFILE_URI 0x40 /* Filename might have query parameters */
25410 #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
25411 #define UNIXFILE_WARNED 0x0100 /* verifyDbFile() warnings issued */
25412 #define UNIXFILE_BLOCK 0x0200 /* Next SHM lock might block */
25413
25414 /*
25415 ** Include code that is common to all os_*.c files
25416 */
25417 /************** Include os_common.h in the middle of os_unix.c ***************/
@@ -26782,11 +26903,11 @@
26903
26904 assert( pFile );
26905 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
26906 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
26907 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
26908 osGetpid(0)));
26909
26910 /* If there is already a lock of this type or more restrictive on the
26911 ** unixFile, do nothing. Don't use the end_lock: exit path, as
26912 ** unixEnterMutex() hasn't been called yet.
26913 */
@@ -26990,11 +27111,11 @@
27111 int rc = SQLITE_OK;
27112
27113 assert( pFile );
27114 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
27115 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
27116 osGetpid(0)));
27117
27118 assert( eFileLock<=SHARED_LOCK );
27119 if( pFile->eFileLock<=eFileLock ){
27120 return SQLITE_OK;
27121 }
@@ -27417,11 +27538,11 @@
27538 char *zLockFile = (char *)pFile->lockingContext;
27539 int rc;
27540
27541 assert( pFile );
27542 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
27543 pFile->eFileLock, osGetpid(0)));
27544 assert( eFileLock<=SHARED_LOCK );
27545
27546 /* no-op if possible */
27547 if( pFile->eFileLock==eFileLock ){
27548 return SQLITE_OK;
@@ -27635,11 +27756,11 @@
27756 static int flockUnlock(sqlite3_file *id, int eFileLock) {
27757 unixFile *pFile = (unixFile*)id;
27758
27759 assert( pFile );
27760 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
27761 pFile->eFileLock, osGetpid(0)));
27762 assert( eFileLock<=SHARED_LOCK );
27763
27764 /* no-op if possible */
27765 if( pFile->eFileLock==eFileLock ){
27766 return SQLITE_OK;
@@ -27803,11 +27924,11 @@
27924 sem_t *pSem = pFile->pInode->pSem;
27925
27926 assert( pFile );
27927 assert( pSem );
27928 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
27929 pFile->eFileLock, osGetpid(0)));
27930 assert( eFileLock<=SHARED_LOCK );
27931
27932 /* no-op if possible */
27933 if( pFile->eFileLock==eFileLock ){
27934 return SQLITE_OK;
@@ -28017,11 +28138,11 @@
28138 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
28139
28140 assert( pFile );
28141 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
28142 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
28143 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
28144
28145 /* If there is already a lock of this type or more restrictive on the
28146 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
28147 ** unixEnterMutex() hasn't been called yet.
28148 */
@@ -28203,11 +28324,11 @@
28324 #endif
28325
28326 assert( pFile );
28327 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
28328 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
28329 osGetpid(0)));
28330
28331 assert( eFileLock<=SHARED_LOCK );
28332 if( pFile->eFileLock<=eFileLock ){
28333 return SQLITE_OK;
28334 }
@@ -29028,10 +29149,14 @@
29149 ** Information and control of an open file handle.
29150 */
29151 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
29152 unixFile *pFile = (unixFile*)id;
29153 switch( op ){
29154 case SQLITE_FCNTL_WAL_BLOCK: {
29155 pFile->ctrlFlags |= UNIXFILE_BLOCK;
29156 return SQLITE_OK;
29157 }
29158 case SQLITE_FCNTL_LOCKSTATE: {
29159 *(int*)pArg = pFile->eFileLock;
29160 return SQLITE_OK;
29161 }
29162 case SQLITE_FCNTL_LAST_ERRNO: {
@@ -29337,37 +29462,42 @@
29462 **
29463 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
29464 ** otherwise.
29465 */
29466 static int unixShmSystemLock(
29467 unixFile *pFile, /* Open connection to the WAL file */
29468 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
29469 int ofst, /* First byte of the locking range */
29470 int n /* Number of bytes to lock */
29471 ){
29472 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
29473 struct flock f; /* The posix advisory locking structure */
29474 int rc = SQLITE_OK; /* Result code form fcntl() */
29475
29476 /* Access to the unixShmNode object is serialized by the caller */
29477 pShmNode = pFile->pInode->pShmNode;
29478 assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
29479
29480 /* Shared locks never span more than one byte */
29481 assert( n==1 || lockType!=F_RDLCK );
29482
29483 /* Locks are within range */
29484 assert( n>=1 && n<SQLITE_SHM_NLOCK );
29485
29486 if( pShmNode->h>=0 ){
29487 int lkType;
29488 /* Initialize the locking parameters */
29489 memset(&f, 0, sizeof(f));
29490 f.l_type = lockType;
29491 f.l_whence = SEEK_SET;
29492 f.l_start = ofst;
29493 f.l_len = n;
29494
29495 lkType = (pFile->ctrlFlags & UNIXFILE_BLOCK)!=0 ? F_SETLKW : F_SETLK;
29496 rc = osFcntl(pShmNode->h, lkType, &f);
29497 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
29498 pFile->ctrlFlags &= ~UNIXFILE_BLOCK;
29499 }
29500
29501 /* Update the global lock state and do debug tracing */
29502 #ifdef SQLITE_DEBUG
29503 { u16 mask;
@@ -29573,17 +29703,17 @@
29703
29704 /* Check to see if another process is holding the dead-man switch.
29705 ** If not, truncate the file to zero length.
29706 */
29707 rc = SQLITE_OK;
29708 if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
29709 if( robust_ftruncate(pShmNode->h, 0) ){
29710 rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
29711 }
29712 }
29713 if( rc==SQLITE_OK ){
29714 rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
29715 }
29716 if( rc ) goto shm_open_err;
29717 }
29718 }
29719
@@ -29811,11 +29941,11 @@
29941 allMask |= pX->sharedMask;
29942 }
29943
29944 /* Unlock the system-level locks */
29945 if( (mask & allMask)==0 ){
29946 rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n);
29947 }else{
29948 rc = SQLITE_OK;
29949 }
29950
29951 /* Undo the local locks */
@@ -29839,11 +29969,11 @@
29969 }
29970
29971 /* Get shared locks at the system level, if necessary */
29972 if( rc==SQLITE_OK ){
29973 if( (allShared & mask)==0 ){
29974 rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n);
29975 }else{
29976 rc = SQLITE_OK;
29977 }
29978 }
29979
@@ -29864,20 +29994,20 @@
29994
29995 /* Get the exclusive locks at the system level. Then if successful
29996 ** also mark the local connection as being locked.
29997 */
29998 if( rc==SQLITE_OK ){
29999 rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
30000 if( rc==SQLITE_OK ){
30001 assert( (p->sharedMask & mask)==0 );
30002 p->exclMask |= mask;
30003 }
30004 }
30005 }
30006 sqlite3_mutex_leave(pShmNode->mutex);
30007 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
30008 p->id, osGetpid(0), p->sharedMask, p->exclMask));
30009 return rc;
30010 }
30011
30012 /*
30013 ** Implement a memory barrier or memory fence on shared memory.
@@ -30968,12 +31098,12 @@
31098 /* Detect a pid change and reset the PRNG. There is a race condition
31099 ** here such that two or more threads all trying to open databases at
31100 ** the same instant might all reset the PRNG. But multiple resets
31101 ** are harmless.
31102 */
31103 if( randomnessPid!=osGetpid(0) ){
31104 randomnessPid = osGetpid(0);
31105 sqlite3_randomness(0,0);
31106 }
31107
31108 memset(p, 0, sizeof(unixFile));
31109
@@ -31360,11 +31490,11 @@
31490 ** When testing, initializing zBuf[] to zero is all we do. That means
31491 ** that we always use the same random number sequence. This makes the
31492 ** tests repeatable.
31493 */
31494 memset(zBuf, 0, nBuf);
31495 randomnessPid = osGetpid(0);
31496 #if !defined(SQLITE_TEST)
31497 {
31498 int fd, got;
31499 fd = robust_open("/dev/urandom", O_RDONLY, 0);
31500 if( fd<0 ){
@@ -31681,11 +31811,11 @@
31811 #else
31812 # ifdef _CS_DARWIN_USER_TEMP_DIR
31813 {
31814 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
31815 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
31816 lPath, errno, osGetpid(0)));
31817 return SQLITE_IOERR_LOCK;
31818 }
31819 len = strlcat(lPath, "sqliteplocks", maxLen);
31820 }
31821 # else
@@ -31703,11 +31833,11 @@
31833 char c = dbPath[i];
31834 lPath[i+len] = (c=='/')?'_':c;
31835 }
31836 lPath[i+len]='\0';
31837 strlcat(lPath, ":auto:", maxLen);
31838 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
31839 return SQLITE_OK;
31840 }
31841
31842 /*
31843 ** Creates the lock file and any missing directories in lockPath
@@ -31730,20 +31860,20 @@
31860 if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
31861 int err=errno;
31862 if( err!=EEXIST ) {
31863 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
31864 "'%s' proxy lock path=%s pid=%d\n",
31865 buf, strerror(err), lockPath, osGetpid(0)));
31866 return err;
31867 }
31868 }
31869 }
31870 start=i+1;
31871 }
31872 buf[i] = lockPath[i];
31873 }
31874 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, osGetpid(0)));
31875 return 0;
31876 }
31877
31878 /*
31879 ** Create a new VFS file descriptor (stored in memory obtained from
@@ -32045,11 +32175,11 @@
32175 int tryOldLockPath = 0;
32176 int forceNewLockPath = 0;
32177
32178 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
32179 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
32180 osGetpid(0)));
32181
32182 rc = proxyGetHostID(myHostID, &pError);
32183 if( (rc&0xff)==SQLITE_IOERR ){
32184 storeLastErrno(pFile, pError);
32185 goto end_takeconch;
@@ -32255,11 +32385,11 @@
32385
32386 pCtx = (proxyLockingContext *)pFile->lockingContext;
32387 conchFile = pCtx->conchFile;
32388 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
32389 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
32390 osGetpid(0)));
32391 if( pCtx->conchHeld>0 ){
32392 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
32393 }
32394 pCtx->conchHeld = 0;
32395 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
@@ -32397,11 +32527,11 @@
32527 }else{
32528 lockPath=(char *)path;
32529 }
32530
32531 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
32532 (lockPath ? lockPath : ":auto:"), osGetpid(0)));
32533
32534 pCtx = sqlite3_malloc( sizeof(*pCtx) );
32535 if( pCtx==0 ){
32536 return SQLITE_NOMEM;
32537 }
@@ -39866,20 +39996,20 @@
39996 ** in pcache1 need to be protected via mutex.
39997 */
39998 static void *pcache1Alloc(int nByte){
39999 void *p = 0;
40000 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
 
40001 if( nByte<=pcache1.szSlot ){
40002 sqlite3_mutex_enter(pcache1.mutex);
40003 p = (PgHdr1 *)pcache1.pFree;
40004 if( p ){
40005 pcache1.pFree = pcache1.pFree->pNext;
40006 pcache1.nFreeSlot--;
40007 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
40008 assert( pcache1.nFreeSlot>=0 );
40009 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
40010 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
40011 }
40012 sqlite3_mutex_leave(pcache1.mutex);
40013 }
40014 if( p==0 ){
40015 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
@@ -39888,11 +40018,12 @@
40018 p = sqlite3Malloc(nByte);
40019 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
40020 if( p ){
40021 int sz = sqlite3MallocSize(p);
40022 sqlite3_mutex_enter(pcache1.mutex);
40023 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
40024 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
40025 sqlite3_mutex_leave(pcache1.mutex);
40026 }
40027 #endif
40028 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
40029 }
@@ -39906,11 +40037,11 @@
40037 int nFreed = 0;
40038 if( p==0 ) return 0;
40039 if( p>=pcache1.pStart && p<pcache1.pEnd ){
40040 PgFreeslot *pSlot;
40041 sqlite3_mutex_enter(pcache1.mutex);
40042 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
40043 pSlot = (PgFreeslot*)p;
40044 pSlot->pNext = pcache1.pFree;
40045 pcache1.pFree = pSlot;
40046 pcache1.nFreeSlot++;
40047 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
@@ -39920,11 +40051,11 @@
40051 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
40052 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
40053 nFreed = sqlite3MallocSize(p);
40054 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
40055 sqlite3_mutex_enter(pcache1.mutex);
40056 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
40057 sqlite3_mutex_leave(pcache1.mutex);
40058 #endif
40059 sqlite3_free(p);
40060 }
40061 return nFreed;
@@ -40656,10 +40787,18 @@
40787
40788 /*
40789 ** Return the size of the header on each page of this PCACHE implementation.
40790 */
40791 SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
40792
40793 /*
40794 ** Return the global mutex used by this PCACHE implementation. The
40795 ** sqlite3_status() routine needs access to this mutex.
40796 */
40797 SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void){
40798 return pcache1.mutex;
40799 }
40800
40801 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
40802 /*
40803 ** This function is called to free superfluous dynamically allocated memory
40804 ** held by the pager system. Memory in use by any SQLite pager allocated
@@ -49429,13 +49568,14 @@
49568 if( pWal->exclusiveMode ) return;
49569 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
49570 SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
49571 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
49572 }
49573 static int walLockExclusive(Wal *pWal, int lockIdx, int n, int fBlock){
49574 int rc;
49575 if( pWal->exclusiveMode ) return SQLITE_OK;
49576 if( fBlock ) sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_WAL_BLOCK, 0);
49577 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
49578 SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
49579 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
49580 walLockName(lockIdx), n, rc ? "failed" : "ok"));
49581 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
@@ -49717,11 +49857,11 @@
49857 assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
49858 assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
49859 assert( pWal->writeLock );
49860 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
49861 nLock = SQLITE_SHM_NLOCK - iLock;
49862 rc = walLockExclusive(pWal, iLock, nLock, 0);
49863 if( rc ){
49864 return rc;
49865 }
49866 WALTRACE(("WAL%p: recovery begin...\n", pWal));
49867
@@ -50251,11 +50391,11 @@
50391 int lockIdx, /* Offset of first byte to lock */
50392 int n /* Number of bytes to lock */
50393 ){
50394 int rc;
50395 do {
50396 rc = walLockExclusive(pWal, lockIdx, n, 0);
50397 }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
50398 return rc;
50399 }
50400
50401 /*
@@ -50684,11 +50824,11 @@
50824 if( pWal->readOnly & WAL_SHM_RDONLY ){
50825 if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
50826 walUnlockShared(pWal, WAL_WRITE_LOCK);
50827 rc = SQLITE_READONLY_RECOVERY;
50828 }
50829 }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1, 1)) ){
50830 pWal->writeLock = 1;
50831 if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
50832 badHdr = walIndexTryHdr(pWal, pChanged);
50833 if( badHdr ){
50834 /* If the wal-index header is still malformed even while holding
@@ -50890,11 +51030,11 @@
51030 {
51031 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
51032 && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
51033 ){
51034 for(i=1; i<WAL_NREADER; i++){
51035 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1, 0);
51036 if( rc==SQLITE_OK ){
51037 mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
51038 mxI = i;
51039 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
51040 break;
@@ -51146,11 +51286,11 @@
51286 }
51287
51288 /* Only one writer allowed at a time. Get the write lock. Return
51289 ** SQLITE_BUSY if unable.
51290 */
51291 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1, 0);
51292 if( rc ){
51293 return rc;
51294 }
51295 pWal->writeLock = 1;
51296
@@ -51291,11 +51431,11 @@
51431 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
51432 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
51433 if( pInfo->nBackfill>0 ){
51434 u32 salt1;
51435 sqlite3_randomness(4, &salt1);
51436 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1, 0);
51437 if( rc==SQLITE_OK ){
51438 /* If all readers are using WAL_READ_LOCK(0) (in other words if no
51439 ** readers are currently using the WAL), then the transactions
51440 ** frames will overwrite the start of the existing log. Update the
51441 ** wal-index header to reflect this.
@@ -51616,11 +51756,11 @@
51756 if( pWal->readOnly ) return SQLITE_READONLY;
51757 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
51758
51759 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
51760 ** "checkpoint" lock on the database file. */
51761 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1, 0);
51762 if( rc ){
51763 /* EVIDENCE-OF: R-10421-19736 If any other process is running a
51764 ** checkpoint operation at the same time, the lock cannot be obtained and
51765 ** SQLITE_BUSY is returned.
51766 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
@@ -52618,10 +52758,11 @@
52758
52759 /*
52760 ** Exit the recursive mutex on a Btree.
52761 */
52762 SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
52763 assert( sqlite3_mutex_held(p->db->mutex) );
52764 if( p->sharable ){
52765 assert( p->wantToLock>0 );
52766 p->wantToLock--;
52767 if( p->wantToLock==0 ){
52768 unlockBtreeMutex(p);
@@ -54796,12 +54937,12 @@
54937 /*
54938 ** The following asserts make sure that structures used by the btree are
54939 ** the right size. This is to guard against size changes that result
54940 ** when compiling on a different architecture.
54941 */
54942 assert( sizeof(i64)==8 );
54943 assert( sizeof(u64)==8 );
54944 assert( sizeof(u32)==4 );
54945 assert( sizeof(u16)==2 );
54946 assert( sizeof(Pgno)==4 );
54947
54948 pBt = sqlite3MallocZero( sizeof(*pBt) );
@@ -60259,11 +60400,12 @@
60400 ** the previous call, as the overflow cell data will have been
60401 ** copied either into the body of a database page or into the new
60402 ** pSpace buffer passed to the latter call to balance_nonroot().
60403 */
60404 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
60405 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
60406 pCur->hints&BTREE_BULKLOAD);
60407 if( pFree ){
60408 /* If pFree is not NULL, it points to the pSpace buffer used
60409 ** by a previous call to balance_nonroot(). Its contents are
60410 ** now stored either on real database pages or within the
60411 ** new pSpace buffer, so it may be safely freed here. */
@@ -61922,17 +62064,26 @@
62064 pBt->btsFlags &= ~BTS_NO_WAL;
62065 return rc;
62066 }
62067
62068 /*
62069 ** set the mask of hint flags for cursor pCsr.
 
62070 */
62071 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
62072 assert( mask==BTREE_BULKLOAD || mask==BTREE_SEEK_EQ || mask==0 );
62073 pCsr->hints = mask;
62074 }
62075
62076 #ifdef SQLITE_DEBUG
62077 /*
62078 ** Return true if the cursor has a hint specified. This routine is
62079 ** only used from within assert() statements
62080 */
62081 SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
62082 return (pCsr->hints & mask)!=0;
62083 }
62084 #endif
62085
62086 /*
62087 ** Return true if the given Btree is read-only.
62088 */
62089 SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
@@ -63822,11 +63973,11 @@
63973 ** by calling sqlite3ValueNew().
63974 **
63975 ** Otherwise, if the second argument is non-zero, then this function is
63976 ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
63977 ** already been allocated, allocate the UnpackedRecord structure that
63978 ** that function will return to its caller here. Then return a pointer to
63979 ** an sqlite3_value within the UnpackedRecord.a[] array.
63980 */
63981 static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
63982 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
63983 if( p ){
@@ -63866,10 +64017,117 @@
64017 UNUSED_PARAMETER(p);
64018 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
64019 return sqlite3ValueNew(db);
64020 }
64021
64022 /*
64023 ** The expression object indicated by the second argument is guaranteed
64024 ** to be a scalar SQL function. If
64025 **
64026 ** * all function arguments are SQL literals,
64027 ** * the SQLITE_FUNC_CONSTANT function flag is set, and
64028 ** * the SQLITE_FUNC_NEEDCOLL function flag is not set,
64029 **
64030 ** then this routine attempts to invoke the SQL function. Assuming no
64031 ** error occurs, output parameter (*ppVal) is set to point to a value
64032 ** object containing the result before returning SQLITE_OK.
64033 **
64034 ** Affinity aff is applied to the result of the function before returning.
64035 ** If the result is a text value, the sqlite3_value object uses encoding
64036 ** enc.
64037 **
64038 ** If the conditions above are not met, this function returns SQLITE_OK
64039 ** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
64040 ** NULL and an SQLite error code returned.
64041 */
64042 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
64043 static int valueFromFunction(
64044 sqlite3 *db, /* The database connection */
64045 Expr *p, /* The expression to evaluate */
64046 u8 enc, /* Encoding to use */
64047 u8 aff, /* Affinity to use */
64048 sqlite3_value **ppVal, /* Write the new value here */
64049 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
64050 ){
64051 sqlite3_context ctx; /* Context object for function invocation */
64052 sqlite3_value **apVal = 0; /* Function arguments */
64053 int nVal = 0; /* Size of apVal[] array */
64054 FuncDef *pFunc = 0; /* Function definition */
64055 sqlite3_value *pVal = 0; /* New value */
64056 int rc = SQLITE_OK; /* Return code */
64057 int nName; /* Size of function name in bytes */
64058 ExprList *pList = 0; /* Function arguments */
64059 int i; /* Iterator variable */
64060
64061 assert( pCtx!=0 );
64062 assert( (p->flags & EP_TokenOnly)==0 );
64063 pList = p->x.pList;
64064 if( pList ) nVal = pList->nExpr;
64065 nName = sqlite3Strlen30(p->u.zToken);
64066 pFunc = sqlite3FindFunction(db, p->u.zToken, nName, nVal, enc, 0);
64067 assert( pFunc );
64068 if( (pFunc->funcFlags & SQLITE_FUNC_CONSTANT)==0
64069 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
64070 ){
64071 return SQLITE_OK;
64072 }
64073
64074 if( pList ){
64075 apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
64076 if( apVal==0 ){
64077 rc = SQLITE_NOMEM;
64078 goto value_from_function_out;
64079 }
64080 for(i=0; i<nVal; i++){
64081 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
64082 if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out;
64083 }
64084 }
64085
64086 pVal = valueNew(db, pCtx);
64087 if( pVal==0 ){
64088 rc = SQLITE_NOMEM;
64089 goto value_from_function_out;
64090 }
64091
64092 assert( pCtx->pParse->rc==SQLITE_OK );
64093 memset(&ctx, 0, sizeof(ctx));
64094 ctx.pOut = pVal;
64095 ctx.pFunc = pFunc;
64096 pFunc->xFunc(&ctx, nVal, apVal);
64097 if( ctx.isError ){
64098 rc = ctx.isError;
64099 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
64100 }else{
64101 sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8);
64102 assert( rc==SQLITE_OK );
64103 rc = sqlite3VdbeChangeEncoding(pVal, enc);
64104 if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){
64105 rc = SQLITE_TOOBIG;
64106 pCtx->pParse->nErr++;
64107 }
64108 }
64109 pCtx->pParse->rc = rc;
64110
64111 value_from_function_out:
64112 if( rc!=SQLITE_OK ){
64113 pVal = 0;
64114 }
64115 if( apVal ){
64116 for(i=0; i<nVal; i++){
64117 sqlite3ValueFree(apVal[i]);
64118 }
64119 sqlite3DbFree(db, apVal);
64120 }
64121
64122 *ppVal = pVal;
64123 return rc;
64124 }
64125 #else
64126 # define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
64127 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
64128
64129 /*
64130 ** Extract a value from the supplied expression in the manner described
64131 ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
64132 ** using valueNew().
64133 **
@@ -63897,10 +64155,16 @@
64155 *ppVal = 0;
64156 return SQLITE_OK;
64157 }
64158 while( (op = pExpr->op)==TK_UPLUS ) pExpr = pExpr->pLeft;
64159 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
64160
64161 /* Compressed expressions only appear when parsing the DEFAULT clause
64162 ** on a table column definition, and hence only when pCtx==0. This
64163 ** check ensures that an EP_TokenOnly expression is never passed down
64164 ** into valueFromFunction(). */
64165 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
64166
64167 if( op==TK_CAST ){
64168 u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
64169 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
64170 testcase( rc!=SQLITE_OK );
@@ -63973,10 +64237,16 @@
64237 assert( zVal[nVal]=='\'' );
64238 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
64239 0, SQLITE_DYNAMIC);
64240 }
64241 #endif
64242
64243 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
64244 else if( op==TK_FUNCTION && pCtx!=0 ){
64245 rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
64246 }
64247 #endif
64248
64249 *ppVal = pVal;
64250 return rc;
64251
64252 no_mem:
@@ -65426,11 +65696,11 @@
65696 break;
65697 }
65698 #ifndef SQLITE_OMIT_VIRTUALTABLE
65699 case P4_VTAB: {
65700 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
65701 sqlite3_snprintf(nTemp, zTemp, "vtab:%p", pVtab);
65702 break;
65703 }
65704 #endif
65705 case P4_INTARRAY: {
65706 sqlite3_snprintf(nTemp, zTemp, "intarray");
@@ -66090,13 +66360,13 @@
66360 }
66361 #ifndef SQLITE_OMIT_VIRTUALTABLE
66362 else if( pCx->pVtabCursor ){
66363 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
66364 const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
66365 assert( pVtabCursor->pVtab->nRef>0 );
66366 pVtabCursor->pVtab->nRef--;
66367 pModule->xClose(pVtabCursor);
 
66368 }
66369 #endif
66370 }
66371
66372 /*
@@ -66451,11 +66721,11 @@
66721
66722 /* Delete the master journal file. This commits the transaction. After
66723 ** doing this the directory is synced again before any individual
66724 ** transaction files are deleted.
66725 */
66726 rc = sqlite3OsDelete(pVfs, zMaster, needSync);
66727 sqlite3DbFree(db, zMaster);
66728 zMaster = 0;
66729 if( rc ){
66730 return rc;
66731 }
@@ -68864,11 +69134,11 @@
69134 }
69135 SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
69136 pCtx->isError = errCode;
69137 pCtx->fErrorOrAux = 1;
69138 #ifdef SQLITE_DEBUG
69139 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
69140 #endif
69141 if( pCtx->pOut->flags & MEM_Null ){
69142 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
69143 SQLITE_UTF8, SQLITE_STATIC);
69144 }
@@ -69127,20 +69397,30 @@
69397 assert( p && p->pFunc );
69398 return p->pOut->db;
69399 }
69400
69401 /*
69402 ** Return the current time for a statement. If the current time
69403 ** is requested more than once within the same run of a single prepared
69404 ** statement, the exact same time is returned for each invocation regardless
69405 ** of the amount of time that elapses between invocations. In other words,
69406 ** the time returned is always the time of the first call.
69407 */
69408 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
 
69409 int rc;
69410 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
69411 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
69412 assert( p->pVdbe!=0 );
69413 #else
69414 sqlite3_int64 iTime = 0;
69415 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
69416 #endif
69417 if( *piTime==0 ){
69418 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
69419 if( rc ) *piTime = 0;
69420 }
69421 return *piTime;
69422 }
69423
69424 /*
69425 ** The following is the implementation of an SQL function that always
69426 ** fails with an error message stating that the function is used in the
@@ -69206,10 +69486,15 @@
69486 */
69487 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
69488 AuxData *pAuxData;
69489
69490 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
69491 #if SQLITE_ENABLE_STAT3_OR_STAT4
69492 if( pCtx->pVdbe==0 ) return 0;
69493 #else
69494 assert( pCtx->pVdbe!=0 );
69495 #endif
69496 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
69497 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
69498 }
69499
69500 return (pAuxData ? pAuxData->pAux : 0);
@@ -69229,10 +69514,15 @@
69514 AuxData *pAuxData;
69515 Vdbe *pVdbe = pCtx->pVdbe;
69516
69517 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
69518 if( iArg<0 ) goto failed;
69519 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
69520 if( pVdbe==0 ) goto failed;
69521 #else
69522 assert( pVdbe!=0 );
69523 #endif
69524
69525 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
69526 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
69527 }
69528 if( pAuxData==0 ){
@@ -71845,11 +72135,11 @@
72135 ** max() aggregate will set to 1 if the current row is not the minimum or
72136 ** maximum. The P1 register is initialized to 0 by this instruction.
72137 **
72138 ** The interface used by the implementation of the aforementioned functions
72139 ** to retrieve the collation sequence set by this opcode is not available
72140 ** publicly. Only built-in functions have access to this feature.
72141 */
72142 case OP_CollSeq: {
72143 assert( pOp->p4type==P4_COLLSEQ );
72144 if( pOp->p1 ){
72145 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
@@ -73563,35 +73853,33 @@
73853 ** cursors or a single read/write cursor but not both.
73854 **
73855 ** See also OpenRead.
73856 */
73857 case OP_ReopenIdx: {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73858 int nField;
73859 KeyInfo *pKeyInfo;
73860 int p2;
73861 int iDb;
73862 int wrFlag;
73863 Btree *pX;
73864 VdbeCursor *pCur;
73865 Db *pDb;
73866
73867 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
73868 assert( pOp->p4type==P4_KEYINFO );
73869 pCur = p->apCsr[pOp->p1];
73870 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
73871 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
73872 goto open_cursor_set_hints;
73873 }
73874 /* If the cursor is not currently open or is open on a different
73875 ** index, then fall through into OP_OpenRead to force a reopen */
73876 case OP_OpenRead:
73877 case OP_OpenWrite:
73878
73879 assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR|OPFLAG_SEEKEQ))==pOp->p5 );
73880 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
73881 assert( p->bIsReader );
73882 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
73883 || p->readOnly==0 );
73884
73885 if( p->expired ){
@@ -73650,18 +73938,21 @@
73938 pCur->nullRow = 1;
73939 pCur->isOrdered = 1;
73940 pCur->pgnoRoot = p2;
73941 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
73942 pCur->pKeyInfo = pKeyInfo;
 
 
 
73943 /* Set the VdbeCursor.isTable variable. Previous versions of
73944 ** SQLite used to check if the root-page flags were sane at this point
73945 ** and report database corruption if they were not, but this check has
73946 ** since moved into the btree layer. */
73947 pCur->isTable = pOp->p4type!=P4_KEYINFO;
73948
73949 open_cursor_set_hints:
73950 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
73951 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
73952 sqlite3BtreeCursorHints(pCur->pCursor,
73953 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
73954 break;
73955 }
73956
73957 /* Opcode: OpenEphemeral P1 P2 * P4 P5
73958 ** Synopsis: nColumn=P2
@@ -73918,10 +74209,26 @@
74209 oc = pOp->opcode;
74210 pC->nullRow = 0;
74211 #ifdef SQLITE_DEBUG
74212 pC->seekOp = pOp->opcode;
74213 #endif
74214
74215 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
74216 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
74217 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
74218 */
74219 #ifdef SQLITE_DEBUG
74220 if( sqlite3BtreeCursorHasHint(pC->pCursor, BTREE_SEEK_EQ) ){
74221 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
74222 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
74223 assert( pOp[1].p1==pOp[0].p1 );
74224 assert( pOp[1].p2==pOp[0].p2 );
74225 assert( pOp[1].p3==pOp[0].p3 );
74226 assert( pOp[1].p4.i==pOp[0].p4.i );
74227 }
74228 #endif
74229
74230 if( pC->isTable ){
74231 /* The input value in P3 might be of any type: integer, real, string,
74232 ** blob, or NULL. But it needs to be an integer before we can do
74233 ** the seek, so convert it. */
74234 pIn3 = &aMem[pOp->p3];
@@ -75257,34 +75564,19 @@
75564 **
75565 ** See also: Clear
75566 */
75567 case OP_Destroy: { /* out2-prerelease */
75568 int iMoved;
 
 
75569 int iDb;
75570
75571 assert( p->readOnly==0 );
 
 
 
 
 
 
 
 
 
 
 
 
75572 pOut->flags = MEM_Null;
75573 if( db->nVdbeRead > db->nVDestroy+1 ){
75574 rc = SQLITE_LOCKED;
75575 p->errorAction = OE_Abort;
75576 }else{
75577 iDb = pOp->p3;
 
75578 assert( DbMaskTest(p->btreeMask, iDb) );
75579 iMoved = 0; /* Not needed. Only to silence a warning. */
75580 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
75581 pOut->flags = MEM_Int;
75582 pOut->u.i = iMoved;
@@ -76337,17 +76629,33 @@
76629 break;
76630 }
76631 #endif /* SQLITE_OMIT_VIRTUALTABLE */
76632
76633 #ifndef SQLITE_OMIT_VIRTUALTABLE
76634 /* Opcode: VCreate P1 P2 * * *
76635 **
76636 ** P2 is a register that holds the name of a virtual table in database
76637 ** P1. Call the xCreate method for that table.
76638 */
76639 case OP_VCreate: {
76640 Mem sMem; /* For storing the record being decoded */
76641 const char *zTab; /* Name of the virtual table */
76642
76643 memset(&sMem, 0, sizeof(sMem));
76644 sMem.db = db;
76645 /* Because P2 is always a static string, it is impossible for the
76646 ** sqlite3VdbeMemCopy() to fail */
76647 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
76648 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
76649 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
76650 assert( rc==SQLITE_OK );
76651 zTab = (const char*)sqlite3_value_text(&sMem);
76652 assert( zTab || db->mallocFailed );
76653 if( zTab ){
76654 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
76655 }
76656 sqlite3VdbeMemRelease(&sMem);
76657 break;
76658 }
76659 #endif /* SQLITE_OMIT_VIRTUALTABLE */
76660
76661 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -76355,13 +76663,13 @@
76663 **
76664 ** P4 is the name of a virtual table in database P1. Call the xDestroy method
76665 ** of that table.
76666 */
76667 case OP_VDestroy: {
76668 db->nVDestroy++;
76669 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
76670 db->nVDestroy--;
76671 break;
76672 }
76673 #endif /* SQLITE_OMIT_VIRTUALTABLE */
76674
76675 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -76373,18 +76681,21 @@
76681 */
76682 case OP_VOpen: {
76683 VdbeCursor *pCur;
76684 sqlite3_vtab_cursor *pVtabCursor;
76685 sqlite3_vtab *pVtab;
76686 const sqlite3_module *pModule;
76687
76688 assert( p->bIsReader );
76689 pCur = 0;
76690 pVtabCursor = 0;
76691 pVtab = pOp->p4.pVtab->pVtab;
76692 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
76693 rc = SQLITE_LOCKED;
76694 break;
76695 }
76696 pModule = pVtab->pModule;
76697 rc = pModule->xOpen(pVtab, &pVtabCursor);
76698 sqlite3VtabImportErrmsg(p, pVtab);
76699 if( SQLITE_OK==rc ){
76700 /* Initialize sqlite3_vtab_cursor base class */
76701 pVtabCursor->pVtab = pVtab;
@@ -76391,10 +76702,11 @@
76702
76703 /* Initialize vdbe cursor object */
76704 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
76705 if( pCur ){
76706 pCur->pVtabCursor = pVtabCursor;
76707 pVtab->nRef++;
76708 }else{
76709 db->mallocFailed = 1;
76710 pModule->xClose(pVtabCursor);
76711 }
76712 }
@@ -76456,13 +76768,11 @@
76768 apArg = p->apArg;
76769 for(i = 0; i<nArg; i++){
76770 apArg[i] = &pArgc[i+1];
76771 }
76772
 
76773 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
 
76774 sqlite3VtabImportErrmsg(p, pVtab);
76775 if( rc==SQLITE_OK ){
76776 res = pModule->xEof(pVtabCursor);
76777 }
76778 VdbeBranchTaken(res!=0,2);
@@ -76548,13 +76858,11 @@
76858 ** underlying implementation to return an error if one occurs during
76859 ** xNext(). Instead, if an error occurs, true is returned (indicating that
76860 ** data is available) and the error code returned when xColumn or
76861 ** some other method is next invoked on the save virtual table cursor.
76862 */
 
76863 rc = pModule->xNext(pCur->pVtabCursor);
 
76864 sqlite3VtabImportErrmsg(p, pVtab);
76865 if( rc==SQLITE_OK ){
76866 res = pModule->xEof(pCur->pVtabCursor);
76867 }
76868 VdbeBranchTaken(!res,2);
@@ -76625,11 +76933,11 @@
76933 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
76934 ** apply in the case of a constraint failure on an insert or update.
76935 */
76936 case OP_VUpdate: {
76937 sqlite3_vtab *pVtab;
76938 const sqlite3_module *pModule;
76939 int nArg;
76940 int i;
76941 sqlite_int64 rowid;
76942 Mem **apArg;
76943 Mem *pX;
@@ -76637,11 +76945,15 @@
76945 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
76946 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
76947 );
76948 assert( p->readOnly==0 );
76949 pVtab = pOp->p4.pVtab->pVtab;
76950 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
76951 rc = SQLITE_LOCKED;
76952 break;
76953 }
76954 pModule = pVtab->pModule;
76955 nArg = pOp->p2;
76956 assert( pOp->p4type==P4_VTAB );
76957 if( ALWAYS(pModule->xUpdate) ){
76958 u8 vtabOnConflict = db->vtabOnConflict;
76959 apArg = p->apArg;
@@ -78507,10 +78819,11 @@
78819 sqlite3 *db, /* Database handle doing sort */
78820 i64 nExtend, /* Attempt to extend file to this size */
78821 sqlite3_file **ppFd
78822 ){
78823 int rc;
78824 if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS;
78825 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
78826 SQLITE_OPEN_TEMP_JOURNAL |
78827 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
78828 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &rc
78829 );
@@ -82098,14 +82411,15 @@
82411 ** and the pExpr parameter is returned unchanged.
82412 */
82413 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(
82414 Parse *pParse, /* Parsing context */
82415 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
82416 const Token *pCollName, /* Name of collating sequence */
82417 int dequote /* True to dequote pCollName */
82418 ){
82419 if( pCollName->n>0 ){
82420 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
82421 if( pNew ){
82422 pNew->pLeft = pExpr;
82423 pNew->flags |= EP_Collate|EP_Skip;
82424 pExpr = pNew;
82425 }
@@ -82115,11 +82429,11 @@
82429 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
82430 Token s;
82431 assert( zC!=0 );
82432 s.z = zC;
82433 s.n = sqlite3Strlen30(s.z);
82434 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
82435 }
82436
82437 /*
82438 ** Skip over any TK_COLLATE or TK_AS operators and any unlikely()
82439 ** or likelihood() function at the root of an expression.
@@ -82425,10 +82739,11 @@
82739 **
82740 ** Also propagate all EP_Propagate flags from the Expr.x.pList into
82741 ** Expr.flags.
82742 */
82743 SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
82744 if( pParse->nErr ) return;
82745 exprSetHeight(p);
82746 sqlite3ExprCheckHeight(pParse, p->nHeight);
82747 }
82748
82749 /*
@@ -87139,11 +87454,14 @@
87454 /* Ensure the default expression is something that sqlite3ValueFromExpr()
87455 ** can handle (i.e. not CURRENT_TIME etc.)
87456 */
87457 if( pDflt ){
87458 sqlite3_value *pVal = 0;
87459 int rc;
87460 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal);
87461 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
87462 if( rc!=SQLITE_OK ){
87463 db->mallocFailed = 1;
87464 return;
87465 }
87466 if( !pVal ){
87467 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
@@ -93202,10 +93520,11 @@
93520 }
93521 if( pIdx->onError==OE_Default ){
93522 pIdx->onError = pIndex->onError;
93523 }
93524 }
93525 pRet = pIdx;
93526 goto exit_create_index;
93527 }
93528 }
93529 }
93530
@@ -95661,11 +95980,13 @@
95980
95981 /*
95982 ** Return the collating function associated with a function.
95983 */
95984 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
95985 VdbeOp *pOp;
95986 assert( context->pVdbe!=0 );
95987 pOp = &context->pVdbe->aOp[context->iOp-1];
95988 assert( pOp->opcode==OP_CollSeq );
95989 assert( pOp->p4type==P4_COLLSEQ );
95990 return pOp->p4.pColl;
95991 }
95992
@@ -109678,11 +109999,11 @@
109999 sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
110000
110001 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
110002 pEList = pLeft->pEList;
110003 if( pCte->pCols ){
110004 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
110005 sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
110006 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
110007 );
110008 pParse->pWith = pSavedWith;
110009 return SQLITE_ERROR;
@@ -114104,10 +114425,11 @@
114425 */
114426 if( !db->init.busy ){
114427 char *zStmt;
114428 char *zWhere;
114429 int iDb;
114430 int iReg;
114431 Vdbe *v;
114432
114433 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
114434 if( pEnd ){
114435 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
@@ -114138,12 +114460,14 @@
114460 sqlite3ChangeCookie(pParse, iDb);
114461
114462 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
114463 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
114464 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
114465
114466 iReg = ++pParse->nMem;
114467 sqlite3VdbeAddOp4(v, OP_String8, 0, iReg, 0, pTab->zName, 0);
114468 sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg);
114469 }
114470
114471 /* If we are rereading the sqlite_master table create the in-memory
114472 ** record of the table. The xConnect() method is not called until
114473 ** the first time the virtual table is used in an SQL statement. This
@@ -114492,15 +114816,19 @@
114816 int rc = SQLITE_OK;
114817 Table *pTab;
114818
114819 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
114820 if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
114821 VTable *p;
114822 for(p=pTab->pVTable; p; p=p->pNext){
114823 assert( p->pVtab );
114824 if( p->pVtab->nRef>0 ){
114825 return SQLITE_LOCKED;
114826 }
114827 }
114828 p = vtabDisconnectAll(db, pTab);
114829 rc = p->pMod->pModule->xDestroy(p->pVtab);
 
114830 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
114831 if( rc==SQLITE_OK ){
114832 assert( pTab->pVTable==p && p->pNext==0 );
114833 p->pVtab = 0;
114834 pTab->pVTable = 0;
@@ -116078,10 +116406,83 @@
116406 static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
116407 pWC->a[iChild].iParent = iParent;
116408 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
116409 pWC->a[iParent].nChild++;
116410 }
116411
116412 /*
116413 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
116414 ** a conjunction, then return just pTerm when N==0. If N is exceeds
116415 ** the number of available subterms, return NULL.
116416 */
116417 static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
116418 if( pTerm->eOperator!=WO_AND ){
116419 return N==0 ? pTerm : 0;
116420 }
116421 if( N<pTerm->u.pAndInfo->wc.nTerm ){
116422 return &pTerm->u.pAndInfo->wc.a[N];
116423 }
116424 return 0;
116425 }
116426
116427 /*
116428 ** Subterms pOne and pTwo are contained within WHERE clause pWC. The
116429 ** two subterms are in disjunction - they are OR-ed together.
116430 **
116431 ** If these two terms are both of the form: "A op B" with the same
116432 ** A and B values but different operators and if the operators are
116433 ** compatible (if one is = and the other is <, for example) then
116434 ** add a new virtual AND term to pWC that is the combination of the
116435 ** two.
116436 **
116437 ** Some examples:
116438 **
116439 ** x<y OR x=y --> x<=y
116440 ** x=y OR x=y --> x=y
116441 ** x<=y OR x<y --> x<=y
116442 **
116443 ** The following is NOT generated:
116444 **
116445 ** x<y OR x>y --> x!=y
116446 */
116447 static void whereCombineDisjuncts(
116448 SrcList *pSrc, /* the FROM clause */
116449 WhereClause *pWC, /* The complete WHERE clause */
116450 WhereTerm *pOne, /* First disjunct */
116451 WhereTerm *pTwo /* Second disjunct */
116452 ){
116453 u16 eOp = pOne->eOperator | pTwo->eOperator;
116454 sqlite3 *db; /* Database connection (for malloc) */
116455 Expr *pNew; /* New virtual expression */
116456 int op; /* Operator for the combined expression */
116457 int idxNew; /* Index in pWC of the next virtual term */
116458
116459 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
116460 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
116461 if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
116462 && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
116463 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
116464 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
116465 if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
116466 if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return;
116467 /* If we reach this point, it means the two subterms can be combined */
116468 if( (eOp & (eOp-1))!=0 ){
116469 if( eOp & (WO_LT|WO_LE) ){
116470 eOp = WO_LE;
116471 }else{
116472 assert( eOp & (WO_GT|WO_GE) );
116473 eOp = WO_GE;
116474 }
116475 }
116476 db = pWC->pWInfo->pParse->db;
116477 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
116478 if( pNew==0 ) return;
116479 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
116480 pNew->op = op;
116481 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
116482 exprAnalyze(pSrc, pWC, idxNew);
116483 }
116484
116485 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
116486 /*
116487 ** Analyze a term that consists of two or more OR-connected
116488 ** subterms. So in:
@@ -116103,10 +116504,11 @@
116504 ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
116505 ** (B) x=expr1 OR expr2=x OR x=expr3
116506 ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
116507 ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
116508 ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
116509 ** (F) x>A OR (x=A AND y>=B)
116510 **
116511 ** CASE 1:
116512 **
116513 ** If all subterms are of the form T.C=expr for some single column of C and
116514 ** a single table T (as shown in example B above) then create a new virtual
@@ -116118,10 +116520,20 @@
116520 ** then create a new virtual term like this:
116521 **
116522 ** x IN (expr1,expr2,expr3)
116523 **
116524 ** CASE 2:
116525 **
116526 ** If there are exactly two disjuncts one side has x>A and the other side
116527 ** has x=A (for the same x and A) then add a new virtual conjunct term to the
116528 ** WHERE clause of the form "x>=A". Example:
116529 **
116530 ** x>A OR (x=A AND y>B) adds: x>=A
116531 **
116532 ** The added conjunct can sometimes be helpful in query planning.
116533 **
116534 ** CASE 3:
116535 **
116536 ** If all subterms are indexable by a single table T, then set
116537 **
116538 ** WhereTerm.eOperator = WO_OR
116539 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
@@ -116245,15 +116657,29 @@
116657 }
116658 }
116659 }
116660
116661 /*
116662 ** Record the set of tables that satisfy case 3. The set might be
116663 ** empty.
116664 */
116665 pOrInfo->indexable = indexable;
116666 pTerm->eOperator = indexable==0 ? 0 : WO_OR;
116667
116668 /* For a two-way OR, attempt to implementation case 2.
116669 */
116670 if( indexable && pOrWc->nTerm==2 ){
116671 int iOne = 0;
116672 WhereTerm *pOne;
116673 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
116674 int iTwo = 0;
116675 WhereTerm *pTwo;
116676 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
116677 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
116678 }
116679 }
116680 }
116681
116682 /*
116683 ** chngToIN holds a set of tables that *might* satisfy case 1. But
116684 ** we have to do some additional checking to see if case 1 really
116685 ** is satisfied.
@@ -116380,11 +116806,11 @@
116806 pTerm = &pWC->a[idxTerm];
116807 markTermAsChild(pWC, idxNew, idxTerm);
116808 }else{
116809 sqlite3ExprListDelete(db, pList);
116810 }
116811 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */
116812 }
116813 }
116814 }
116815 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
116816
@@ -116575,11 +117001,11 @@
117001 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
117002 Expr *pNewExpr1;
117003 Expr *pNewExpr2;
117004 int idxNew1;
117005 int idxNew2;
117006 const char *zCollSeqName; /* Name of collating sequence */
117007 const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
117008
117009 pLeft = pExpr->x.pList->a[1].pExpr;
117010 pStr2 = sqlite3ExprDup(db, pStr1, 0);
117011
@@ -116611,23 +117037,22 @@
117037 if( c=='A'-1 ) isComplete = 0;
117038 c = sqlite3UpperToLower[c];
117039 }
117040 *pC = c + 1;
117041 }
117042 zCollSeqName = noCase ? "NOCASE" : "BINARY";
 
117043 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
117044 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
117045 sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
117046 pStr1, 0);
117047 transferJoinMarkings(pNewExpr1, pExpr);
117048 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
117049 testcase( idxNew1==0 );
117050 exprAnalyze(pSrc, pWC, idxNew1);
117051 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
117052 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
117053 sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
117054 pStr2, 0);
117055 transferJoinMarkings(pNewExpr2, pExpr);
117056 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
117057 testcase( idxNew2==0 );
117058 exprAnalyze(pSrc, pWC, idxNew2);
@@ -117240,15 +117665,18 @@
117665 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
117666 /*
117667 ** Estimate the location of a particular key among all keys in an
117668 ** index. Store the results in aStat as follows:
117669 **
117670 ** aStat[0] Est. number of rows less than pRec
117671 ** aStat[1] Est. number of rows equal to pRec
117672 **
117673 ** Return the index of the sample that is the smallest sample that
117674 ** is greater than or equal to pRec. Note that this index is not an index
117675 ** into the aSample[] array - it is an index into a virtual set of samples
117676 ** based on the contents of aSample[] and the number of fields in record
117677 ** pRec.
117678 */
117679 static int whereKeyStats(
117680 Parse *pParse, /* Database connection */
117681 Index *pIdx, /* Index to consider domain of */
117682 UnpackedRecord *pRec, /* Vector of values to consider */
@@ -117255,71 +117683,162 @@
117683 int roundUp, /* Round up if true. Round down if false */
117684 tRowcnt *aStat /* OUT: stats written here */
117685 ){
117686 IndexSample *aSample = pIdx->aSample;
117687 int iCol; /* Index of required stats in anEq[] etc. */
117688 int i; /* Index of first sample >= pRec */
117689 int iSample; /* Smallest sample larger than or equal to pRec */
117690 int iMin = 0; /* Smallest sample not yet tested */
 
117691 int iTest; /* Next sample to test */
117692 int res; /* Result of comparison operation */
117693 int nField; /* Number of fields in pRec */
117694 tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */
117695
117696 #ifndef SQLITE_DEBUG
117697 UNUSED_PARAMETER( pParse );
117698 #endif
117699 assert( pRec!=0 );
 
117700 assert( pIdx->nSample>0 );
117701 assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol );
117702
117703 /* Do a binary search to find the first sample greater than or equal
117704 ** to pRec. If pRec contains a single field, the set of samples to search
117705 ** is simply the aSample[] array. If the samples in aSample[] contain more
117706 ** than one fields, all fields following the first are ignored.
117707 **
117708 ** If pRec contains N fields, where N is more than one, then as well as the
117709 ** samples in aSample[] (truncated to N fields), the search also has to
117710 ** consider prefixes of those samples. For example, if the set of samples
117711 ** in aSample is:
117712 **
117713 ** aSample[0] = (a, 5)
117714 ** aSample[1] = (a, 10)
117715 ** aSample[2] = (b, 5)
117716 ** aSample[3] = (c, 100)
117717 ** aSample[4] = (c, 105)
117718 **
117719 ** Then the search space should ideally be the samples above and the
117720 ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
117721 ** the code actually searches this set:
117722 **
117723 ** 0: (a)
117724 ** 1: (a, 5)
117725 ** 2: (a, 10)
117726 ** 3: (a, 10)
117727 ** 4: (b)
117728 ** 5: (b, 5)
117729 ** 6: (c)
117730 ** 7: (c, 100)
117731 ** 8: (c, 105)
117732 ** 9: (c, 105)
117733 **
117734 ** For each sample in the aSample[] array, N samples are present in the
117735 ** effective sample array. In the above, samples 0 and 1 are based on
117736 ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
117737 **
117738 ** Often, sample i of each block of N effective samples has (i+1) fields.
117739 ** Except, each sample may be extended to ensure that it is greater than or
117740 ** equal to the previous sample in the array. For example, in the above,
117741 ** sample 2 is the first sample of a block of N samples, so at first it
117742 ** appears that it should be 1 field in size. However, that would make it
117743 ** smaller than sample 1, so the binary search would not work. As a result,
117744 ** it is extended to two fields. The duplicates that this creates do not
117745 ** cause any problems.
117746 */
117747 nField = pRec->nField;
117748 iCol = 0;
117749 iSample = pIdx->nSample * nField;
117750 do{
117751 int iSamp; /* Index in aSample[] of test sample */
117752 int n; /* Number of fields in test sample */
117753
117754 iTest = (iMin+iSample)/2;
117755 iSamp = iTest / nField;
117756 if( iSamp>0 ){
117757 /* The proposed effective sample is a prefix of sample aSample[iSamp].
117758 ** Specifically, the shortest prefix of at least (1 + iTest%nField)
117759 ** fields that is greater than the previous effective sample. */
117760 for(n=(iTest % nField) + 1; n<nField; n++){
117761 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
117762 }
117763 }else{
117764 n = iTest + 1;
117765 }
117766
117767 pRec->nField = n;
117768 res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec);
117769 if( res<0 ){
117770 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
117771 iMin = iTest+1;
117772 }else if( res==0 && n<nField ){
117773 iLower = aSample[iSamp].anLt[n-1];
117774 iMin = iTest+1;
117775 res = -1;
117776 }else{
117777 iSample = iTest;
117778 iCol = n-1;
117779 }
117780 }while( res && iMin<iSample );
117781 i = iSample / nField;
117782
117783 #ifdef SQLITE_DEBUG
117784 /* The following assert statements check that the binary search code
117785 ** above found the right answer. This block serves no purpose other
117786 ** than to invoke the asserts. */
117787 if( pParse->db->mallocFailed==0 ){
117788 if( res==0 ){
117789 /* If (res==0) is true, then pRec must be equal to sample i. */
117790 assert( i<pIdx->nSample );
117791 assert( iCol==nField-1 );
117792 pRec->nField = nField;
117793 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
117794 || pParse->db->mallocFailed
117795 );
117796 }else{
117797 /* Unless i==pIdx->nSample, indicating that pRec is larger than
117798 ** all samples in the aSample[] array, pRec must be smaller than the
117799 ** (iCol+1) field prefix of sample i. */
117800 assert( i<=pIdx->nSample && i>=0 );
117801 pRec->nField = iCol+1;
117802 assert( i==pIdx->nSample
117803 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
117804 || pParse->db->mallocFailed );
117805
117806 /* if i==0 and iCol==0, then record pRec is smaller than all samples
117807 ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must
117808 ** be greater than or equal to the (iCol) field prefix of sample i.
117809 ** If (i>0), then pRec must also be greater than sample (i-1). */
117810 if( iCol>0 ){
117811 pRec->nField = iCol;
117812 assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0
117813 || pParse->db->mallocFailed );
117814 }
117815 if( i>0 ){
117816 pRec->nField = nField;
117817 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
117818 || pParse->db->mallocFailed );
117819 }
117820 }
117821 }
117822 #endif /* ifdef SQLITE_DEBUG */
117823
 
 
 
 
117824 if( res==0 ){
117825 /* Record pRec is equal to sample i */
117826 assert( iCol==nField-1 );
117827 aStat[0] = aSample[i].anLt[iCol];
117828 aStat[1] = aSample[i].anEq[iCol];
117829 }else{
117830 /* At this point, the (iCol+1) field prefix of aSample[i] is the first
117831 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
117832 ** is larger than all samples in the array. */
117833 tRowcnt iUpper, iGap;
117834 if( i>=pIdx->nSample ){
117835 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
117836 }else{
117837 iUpper = aSample[i].anLt[iCol];
 
 
117838 }
117839
117840 if( iLower>=iUpper ){
117841 iGap = 0;
117842 }else{
117843 iGap = iUpper - iLower;
117844 }
@@ -117327,11 +117846,15 @@
117846 iGap = (iGap*2)/3;
117847 }else{
117848 iGap = iGap/3;
117849 }
117850 aStat[0] = iLower + iGap;
117851 aStat[1] = pIdx->aAvgEq[iCol];
117852 }
117853
117854 /* Restore the pRec->nField value before returning. */
117855 pRec->nField = nField;
117856 return i;
117857 }
117858 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
117859
117860 /*
@@ -118322,25 +118845,33 @@
118845 #else
118846 # define addScanStatus(a, b, c, d) ((void)d)
118847 #endif
118848
118849 /*
118850 ** If the most recently coded instruction is a constant range contraint
118851 ** that originated from the LIKE optimization, then change the P3 to be
118852 ** pLoop->iLikeRepCntr and set P5.
118853 **
118854 ** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
118855 ** expression: "x>='ABC' AND x<'abd'". But this requires that the range
118856 ** scan loop run twice, once for strings and a second time for BLOBs.
118857 ** The OP_String opcodes on the second pass convert the upper and lower
118858 ** bound string contants to blobs. This routine makes the necessary changes
118859 ** to the OP_String opcodes for that to happen.
118860 */
118861 static void whereLikeOptimizationStringFixup(
118862 Vdbe *v, /* prepared statement under construction */
118863 WhereLevel *pLevel, /* The loop that contains the LIKE operator */
118864 WhereTerm *pTerm /* The upper or lower bound just coded */
118865 ){
118866 if( pTerm->wtFlags & TERM_LIKEOPT ){
118867 VdbeOp *pOp;
118868 assert( pLevel->iLikeRepCntr>0 );
118869 pOp = sqlite3VdbeGetOp(v, -1);
118870 assert( pOp!=0 );
118871 assert( pOp->opcode==OP_String8
118872 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
118873 pOp->p3 = pLevel->iLikeRepCntr;
118874 pOp->p5 = 1;
118875 }
118876 }
118877
@@ -118670,18 +119201,20 @@
119201 */
119202 j = nEq;
119203 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
119204 pRangeStart = pLoop->aLTerm[j++];
119205 nExtraReg = 1;
119206 /* Like optimization range constraints always occur in pairs */
119207 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
119208 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
119209 }
119210 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
119211 pRangeEnd = pLoop->aLTerm[j++];
119212 nExtraReg = 1;
119213 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
119214 assert( pRangeStart!=0 ); /* LIKE opt constraints */
119215 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
 
119216 pLevel->iLikeRepCntr = ++pParse->nMem;
119217 testcase( bRev );
119218 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
119219 sqlite3VdbeAddOp2(v, OP_Integer,
119220 bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
@@ -118729,11 +119262,11 @@
119262 /* Seek the index cursor to the start of the range. */
119263 nConstraint = nEq;
119264 if( pRangeStart ){
119265 Expr *pRight = pRangeStart->pExpr->pRight;
119266 sqlite3ExprCode(pParse, pRight, regBase+nEq);
119267 whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
119268 if( (pRangeStart->wtFlags & TERM_VNULL)==0
119269 && sqlite3ExprCanBeNull(pRight)
119270 ){
119271 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
119272 VdbeCoverage(v);
@@ -118775,11 +119308,11 @@
119308 nConstraint = nEq;
119309 if( pRangeEnd ){
119310 Expr *pRight = pRangeEnd->pExpr->pRight;
119311 sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
119312 sqlite3ExprCode(pParse, pRight, regBase+nEq);
119313 whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
119314 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
119315 && sqlite3ExprCanBeNull(pRight)
119316 ){
119317 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
119318 VdbeCoverage(v);
@@ -119852,10 +120385,14 @@
120385 ){
120386 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
120387 }
120388 if( pTerm->prereqRight & pNew->maskSelf ) continue;
120389
120390 /* Do not allow the upper bound of a LIKE optimization range constraint
120391 ** to mix with a lower range bound from some other source */
120392 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
120393
120394 pNew->wsFlags = saved_wsFlags;
120395 pNew->u.btree.nEq = saved_nEq;
120396 pNew->nLTerm = saved_nLTerm;
120397 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
120398 pNew->aLTerm[pNew->nLTerm++] = pTerm;
@@ -119895,10 +120432,21 @@
120432 testcase( eOp & WO_GT );
120433 testcase( eOp & WO_GE );
120434 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
120435 pBtm = pTerm;
120436 pTop = 0;
120437 if( pTerm->wtFlags & TERM_LIKEOPT ){
120438 /* Range contraints that come from the LIKE optimization are
120439 ** always used in pairs. */
120440 pTop = &pTerm[1];
120441 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
120442 assert( pTop->wtFlags & TERM_LIKEOPT );
120443 assert( pTop->eOperator==WO_LT );
120444 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
120445 pNew->aLTerm[pNew->nLTerm++] = pTop;
120446 pNew->wsFlags |= WHERE_TOP_LIMIT;
120447 }
120448 }else{
120449 assert( eOp & (WO_LT|WO_LE) );
120450 testcase( eOp & WO_LT );
120451 testcase( eOp & WO_LE );
120452 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
@@ -121089,14 +121637,14 @@
121637 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] );
121638 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX );
121639
121640 /* Seed the search with a single WherePath containing zero WhereLoops.
121641 **
121642 ** TUNING: Do not let the number of iterations go above 28. If the cost
121643 ** of computing an automatic index is not paid back within the first 28
121644 ** rows, then do not use the automatic index. */
121645 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
121646 nFrom = 1;
121647 assert( aFrom[0].isOrdered==0 );
121648 if( nOrderBy ){
121649 /* If nLoop is zero, then there are no FROM terms in the query. Since
121650 ** in this case the query may return a maximum of one row, the results
@@ -121890,10 +122438,16 @@
122438 assert( pIx->pSchema==pTab->pSchema );
122439 assert( iIndexCur>=0 );
122440 if( op ){
122441 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
122442 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
122443 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
122444 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
122445 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
122446 ){
122447 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */
122448 }
122449 VdbeComment((v, "%s", pIx->zName));
122450 }
122451 }
122452 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
122453 notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
@@ -124891,11 +125445,11 @@
125445 spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
125446 }
125447 break;
125448 case 193: /* expr ::= expr COLLATE ID|STRING */
125449 {
125450 yygotominor.yy346.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy346.pExpr, &yymsp[0].minor.yy0, 1);
125451 yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart;
125452 yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
125453 }
125454 break;
125455 case 194: /* expr ::= CAST LP expr AS typetoken RP */
@@ -125171,20 +125725,20 @@
125725 case 241: /* uniqueflag ::= */
125726 {yygotominor.yy328 = OE_None;}
125727 break;
125728 case 244: /* idxlist ::= idxlist COMMA nm collate sortorder */
125729 {
125730 Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0, 1);
125731 yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, p);
125732 sqlite3ExprListSetName(pParse,yygotominor.yy14,&yymsp[-2].minor.yy0,1);
125733 sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
125734 if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
125735 }
125736 break;
125737 case 245: /* idxlist ::= nm collate sortorder */
125738 {
125739 Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0, 1);
125740 yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, p);
125741 sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
125742 sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
125743 if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
125744 }
@@ -126439,13 +126993,15 @@
126993 pParse->zTail = &zSql[i];
126994 }
126995 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
126996 }
126997 #ifdef YYTRACKMAXSTACKDEPTH
126998 sqlite3_mutex_enter(sqlite3MallocMutex());
126999 sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
127000 sqlite3ParserStackPeak(pEngine)
127001 );
127002 sqlite3_mutex_leave(sqlite3MallocMutex());
127003 #endif /* YYDEBUG */
127004 sqlite3ParserFree(pEngine, sqlite3_free);
127005 db->lookaside.bEnabled = enableLookaside;
127006 if( db->mallocFailed ){
127007 pParse->rc = SQLITE_NOMEM;
@@ -127011,10 +127567,15 @@
127567 rc = sqlite3_wsd_init(4096, 24);
127568 if( rc!=SQLITE_OK ){
127569 return rc;
127570 }
127571 #endif
127572
127573 /* If the following assert() fails on some obscure processor/compiler
127574 ** combination, the work-around is to set the correct pointer
127575 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
127576 assert( SQLITE_PTRSIZE==sizeof(char*) );
127577
127578 /* If SQLite is already completely initialized, then this call
127579 ** to sqlite3_initialize() should be a no-op. But the initialization
127580 ** must be complete. So isInit must not be set until the very end
127581 ** of this routine.
@@ -132766,15 +133327,20 @@
133327 ** the output value undefined. Otherwise SQLITE_OK is returned.
133328 **
133329 ** This function is used when parsing the "prefix=" FTS4 parameter.
133330 */
133331 static int fts3GobbleInt(const char **pp, int *pnOut){
133332 const int MAX_NPREFIX = 10000000;
133333 const char *p; /* Iterator pointer */
133334 int nInt = 0; /* Output value */
133335
133336 for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
133337 nInt = nInt * 10 + (p[0] - '0');
133338 if( nInt>MAX_NPREFIX ){
133339 nInt = 0;
133340 break;
133341 }
133342 }
133343 if( p==*pp ) return SQLITE_ERROR;
133344 *pnOut = nInt;
133345 *pp = p;
133346 return SQLITE_OK;
@@ -132813,27 +133379,33 @@
133379 }
133380 }
133381
133382 aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
133383 *apIndex = aIndex;
 
133384 if( !aIndex ){
133385 return SQLITE_NOMEM;
133386 }
133387
133388 memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
133389 if( zParam ){
133390 const char *p = zParam;
133391 int i;
133392 for(i=1; i<nIndex; i++){
133393 int nPrefix = 0;
133394 if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
133395 assert( nPrefix>=0 );
133396 if( nPrefix==0 ){
133397 nIndex--;
133398 i--;
133399 }else{
133400 aIndex[i].nPrefix = nPrefix;
133401 }
133402 p++;
133403 }
133404 }
133405
133406 *pnIndex = nIndex;
133407 return SQLITE_OK;
133408 }
133409
133410 /*
133411 ** This function is called when initializing an FTS4 table that uses the
@@ -140609,11 +141181,11 @@
141181 nName = sqlite3_value_bytes(argv[0])+1;
141182
141183 if( argc==2 ){
141184 void *pOld;
141185 int n = sqlite3_value_bytes(argv[1]);
141186 if( zName==0 || n!=sizeof(pPtr) ){
141187 sqlite3_result_error(context, "argument type mismatch", -1);
141188 return;
141189 }
141190 pPtr = *(void **)sqlite3_value_blob(argv[1]);
141191 pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
@@ -140620,11 +141192,13 @@
141192 if( pOld==pPtr ){
141193 sqlite3_result_error(context, "out of memory", -1);
141194 return;
141195 }
141196 }else{
141197 if( zName ){
141198 pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
141199 }
141200 if( !pPtr ){
141201 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
141202 sqlite3_result_error(context, zErr, -1);
141203 sqlite3_free(zErr);
141204 return;
@@ -140701,10 +141275,14 @@
141275 zCopy = sqlite3_mprintf("%s", zArg);
141276 if( !zCopy ) return SQLITE_NOMEM;
141277 zEnd = &zCopy[strlen(zCopy)];
141278
141279 z = (char *)sqlite3Fts3NextToken(zCopy, &n);
141280 if( z==0 ){
141281 assert( n==0 );
141282 z = zCopy;
141283 }
141284 z[n] = '\0';
141285 sqlite3Fts3Dequote(z);
141286
141287 m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
141288 if( !m ){
@@ -143346,11 +143924,14 @@
143924 /*
143925 ** This is a comparison function used as a qsort() callback when sorting
143926 ** an array of pending terms by term. This occurs as part of flushing
143927 ** the contents of the pending-terms hash table to the database.
143928 */
143929 static int SQLITE_CDECL fts3CompareElemByTerm(
143930 const void *lhs,
143931 const void *rhs
143932 ){
143933 char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
143934 char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
143935 int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
143936 int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
143937
143938
+37 -17
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -52,10 +52,17 @@
5252
#ifndef SQLITE_API
5353
# define SQLITE_API
5454
#endif
5555
5656
57
+/*
58
+** Add the ability to override 'cdecl'
59
+*/
60
+#ifndef SQLITE_CDECL
61
+# define SQLITE_CDECL
62
+#endif
63
+
5764
/*
5865
** These no-op macros are used in front of interfaces to mark those
5966
** interfaces as either deprecated or experimental. New applications
6067
** should not use deprecated interfaces - they are supported for backwards
6168
** compatibility only. Application writers should be aware that
@@ -107,11 +114,11 @@
107114
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108115
** [sqlite_version()] and [sqlite_source_id()].
109116
*/
110117
#define SQLITE_VERSION "3.8.9"
111118
#define SQLITE_VERSION_NUMBER 3008009
112
-#define SQLITE_SOURCE_ID "2015-03-09 10:40:48 e5da5e7d5dc5a3438ced23f1ee83e695abc29c45"
119
+#define SQLITE_SOURCE_ID "2015-03-24 18:19:39 436314b5728c9413f9ac2d837e1c19364f31be72"
113120
114121
/*
115122
** CAPI3REF: Run-Time Library Version Numbers
116123
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117124
**
@@ -948,10 +955,17 @@
948955
** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
949956
** opcode causes the xFileControl method to swap the file handle with the one
950957
** pointed to by the pArg argument. This capability is used during testing
951958
** and only needs to be supported when SQLITE_TEST is defined.
952959
**
960
+** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
961
+** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
962
+** be advantageous to block on the next WAL lock if the lock is not immediately
963
+** available. The WAL subsystem issues this signal during rare
964
+** circumstances in order to fix a problem with priority inversion.
965
+** Applications should <em>not</em> use this file-control.
966
+**
953967
** </ul>
954968
*/
955969
#define SQLITE_FCNTL_LOCKSTATE 1
956970
#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
957971
#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -972,10 +986,11 @@
972986
#define SQLITE_FCNTL_TRACE 19
973987
#define SQLITE_FCNTL_HAS_MOVED 20
974988
#define SQLITE_FCNTL_SYNC 21
975989
#define SQLITE_FCNTL_COMMIT_PHASETWO 22
976990
#define SQLITE_FCNTL_WIN32_SET_HANDLE 23
991
+#define SQLITE_FCNTL_WAL_BLOCK 24
977992
978993
/* deprecated names */
979994
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
980995
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
981996
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -1538,11 +1553,11 @@
15381553
** disabled, the following SQLite interfaces become non-operational:
15391554
** <ul>
15401555
** <li> [sqlite3_memory_used()]
15411556
** <li> [sqlite3_memory_highwater()]
15421557
** <li> [sqlite3_soft_heap_limit64()]
1543
-** <li> [sqlite3_status()]
1558
+** <li> [sqlite3_status64()]
15441559
** </ul>)^
15451560
** ^Memory allocation statistics are enabled by default unless SQLite is
15461561
** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
15471562
** allocation statistics are disabled by default.
15481563
** </dd>
@@ -2995,15 +3010,17 @@
29953010
29963011
29973012
/*
29983013
** CAPI3REF: Error Codes And Messages
29993014
**
3000
-** ^The sqlite3_errcode() interface returns the numeric [result code] or
3001
-** [extended result code] for the most recent failed sqlite3_* API call
3002
-** associated with a [database connection]. If a prior API call failed
3003
-** but the most recent API call succeeded, the return value from
3004
-** sqlite3_errcode() is undefined. ^The sqlite3_extended_errcode()
3015
+** ^If the most recent sqlite3_* API call associated with
3016
+** [database connection] D failed, then the sqlite3_errcode(D) interface
3017
+** returns the numeric [result code] or [extended result code] for that
3018
+** API call.
3019
+** If the most recent API call was successful,
3020
+** then the return value from sqlite3_errcode() is undefined.
3021
+** ^The sqlite3_extended_errcode()
30053022
** interface is the same except that it always returns the
30063023
** [extended result code] even when extended result codes are
30073024
** disabled.
30083025
**
30093026
** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
@@ -5613,11 +5630,11 @@
56135630
** is delivered up to the client application, the string will be automatically
56145631
** freed by sqlite3_free() and the zErrMsg field will be zeroed.
56155632
*/
56165633
struct sqlite3_vtab {
56175634
const sqlite3_module *pModule; /* The module for this virtual table */
5618
- int nRef; /* NO LONGER USED */
5635
+ int nRef; /* Number of open cursors */
56195636
char *zErrMsg; /* Error message from sqlite3_mprintf() */
56205637
/* Virtual table implementations will typically add additional fields */
56215638
};
56225639
56235640
/*
@@ -6291,11 +6308,11 @@
62916308
#define SQLITE_TESTCTRL_LAST 25
62926309
62936310
/*
62946311
** CAPI3REF: SQLite Runtime Status
62956312
**
6296
-** ^This interface is used to retrieve runtime status information
6313
+** ^These interfaces are used to retrieve runtime status information
62976314
** about the performance of SQLite, and optionally to reset various
62986315
** highwater marks. ^The first argument is an integer code for
62996316
** the specific parameter to measure. ^(Recognized integer codes
63006317
** are of the form [status parameters | SQLITE_STATUS_...].)^
63016318
** ^The current value of the parameter is returned into *pCurrent.
@@ -6305,23 +6322,26 @@
63056322
** value. For those parameters
63066323
** nothing is written into *pHighwater and the resetFlag is ignored.)^
63076324
** ^(Other parameters record only the highwater mark and not the current
63086325
** value. For these latter parameters nothing is written into *pCurrent.)^
63096326
**
6310
-** ^The sqlite3_status() routine returns SQLITE_OK on success and a
6311
-** non-zero [error code] on failure.
6327
+** ^The sqlite3_status() and sqlite3_status64() routines return
6328
+** SQLITE_OK on success and a non-zero [error code] on failure.
63126329
**
6313
-** This routine is threadsafe but is not atomic. This routine can be
6314
-** called while other threads are running the same or different SQLite
6315
-** interfaces. However the values returned in *pCurrent and
6316
-** *pHighwater reflect the status of SQLite at different points in time
6317
-** and it is possible that another thread might change the parameter
6318
-** in between the times when *pCurrent and *pHighwater are written.
6330
+** If either the current value or the highwater mark is too large to
6331
+** be represented by a 32-bit integer, then the values returned by
6332
+** sqlite3_status() are undefined.
63196333
**
63206334
** See also: [sqlite3_db_status()]
63216335
*/
63226336
SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
6337
+SQLITE_API int sqlite3_status64(
6338
+ int op,
6339
+ sqlite3_int64 *pCurrent,
6340
+ sqlite3_int64 *pHighwater,
6341
+ int resetFlag
6342
+);
63236343
63246344
63256345
/*
63266346
** CAPI3REF: Status Parameters
63276347
** KEYWORDS: {status parameters}
63286348
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -52,10 +52,17 @@
52 #ifndef SQLITE_API
53 # define SQLITE_API
54 #endif
55
56
 
 
 
 
 
 
 
57 /*
58 ** These no-op macros are used in front of interfaces to mark those
59 ** interfaces as either deprecated or experimental. New applications
60 ** should not use deprecated interfaces - they are supported for backwards
61 ** compatibility only. Application writers should be aware that
@@ -107,11 +114,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.9"
111 #define SQLITE_VERSION_NUMBER 3008009
112 #define SQLITE_SOURCE_ID "2015-03-09 10:40:48 e5da5e7d5dc5a3438ced23f1ee83e695abc29c45"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -948,10 +955,17 @@
948 ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
949 ** opcode causes the xFileControl method to swap the file handle with the one
950 ** pointed to by the pArg argument. This capability is used during testing
951 ** and only needs to be supported when SQLITE_TEST is defined.
952 **
 
 
 
 
 
 
 
953 ** </ul>
954 */
955 #define SQLITE_FCNTL_LOCKSTATE 1
956 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
957 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -972,10 +986,11 @@
972 #define SQLITE_FCNTL_TRACE 19
973 #define SQLITE_FCNTL_HAS_MOVED 20
974 #define SQLITE_FCNTL_SYNC 21
975 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
976 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
 
977
978 /* deprecated names */
979 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
980 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
981 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -1538,11 +1553,11 @@
1538 ** disabled, the following SQLite interfaces become non-operational:
1539 ** <ul>
1540 ** <li> [sqlite3_memory_used()]
1541 ** <li> [sqlite3_memory_highwater()]
1542 ** <li> [sqlite3_soft_heap_limit64()]
1543 ** <li> [sqlite3_status()]
1544 ** </ul>)^
1545 ** ^Memory allocation statistics are enabled by default unless SQLite is
1546 ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1547 ** allocation statistics are disabled by default.
1548 ** </dd>
@@ -2995,15 +3010,17 @@
2995
2996
2997 /*
2998 ** CAPI3REF: Error Codes And Messages
2999 **
3000 ** ^The sqlite3_errcode() interface returns the numeric [result code] or
3001 ** [extended result code] for the most recent failed sqlite3_* API call
3002 ** associated with a [database connection]. If a prior API call failed
3003 ** but the most recent API call succeeded, the return value from
3004 ** sqlite3_errcode() is undefined. ^The sqlite3_extended_errcode()
 
 
3005 ** interface is the same except that it always returns the
3006 ** [extended result code] even when extended result codes are
3007 ** disabled.
3008 **
3009 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
@@ -5613,11 +5630,11 @@
5613 ** is delivered up to the client application, the string will be automatically
5614 ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
5615 */
5616 struct sqlite3_vtab {
5617 const sqlite3_module *pModule; /* The module for this virtual table */
5618 int nRef; /* NO LONGER USED */
5619 char *zErrMsg; /* Error message from sqlite3_mprintf() */
5620 /* Virtual table implementations will typically add additional fields */
5621 };
5622
5623 /*
@@ -6291,11 +6308,11 @@
6291 #define SQLITE_TESTCTRL_LAST 25
6292
6293 /*
6294 ** CAPI3REF: SQLite Runtime Status
6295 **
6296 ** ^This interface is used to retrieve runtime status information
6297 ** about the performance of SQLite, and optionally to reset various
6298 ** highwater marks. ^The first argument is an integer code for
6299 ** the specific parameter to measure. ^(Recognized integer codes
6300 ** are of the form [status parameters | SQLITE_STATUS_...].)^
6301 ** ^The current value of the parameter is returned into *pCurrent.
@@ -6305,23 +6322,26 @@
6305 ** value. For those parameters
6306 ** nothing is written into *pHighwater and the resetFlag is ignored.)^
6307 ** ^(Other parameters record only the highwater mark and not the current
6308 ** value. For these latter parameters nothing is written into *pCurrent.)^
6309 **
6310 ** ^The sqlite3_status() routine returns SQLITE_OK on success and a
6311 ** non-zero [error code] on failure.
6312 **
6313 ** This routine is threadsafe but is not atomic. This routine can be
6314 ** called while other threads are running the same or different SQLite
6315 ** interfaces. However the values returned in *pCurrent and
6316 ** *pHighwater reflect the status of SQLite at different points in time
6317 ** and it is possible that another thread might change the parameter
6318 ** in between the times when *pCurrent and *pHighwater are written.
6319 **
6320 ** See also: [sqlite3_db_status()]
6321 */
6322 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
 
 
 
 
 
 
6323
6324
6325 /*
6326 ** CAPI3REF: Status Parameters
6327 ** KEYWORDS: {status parameters}
6328
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -52,10 +52,17 @@
52 #ifndef SQLITE_API
53 # define SQLITE_API
54 #endif
55
56
57 /*
58 ** Add the ability to override 'cdecl'
59 */
60 #ifndef SQLITE_CDECL
61 # define SQLITE_CDECL
62 #endif
63
64 /*
65 ** These no-op macros are used in front of interfaces to mark those
66 ** interfaces as either deprecated or experimental. New applications
67 ** should not use deprecated interfaces - they are supported for backwards
68 ** compatibility only. Application writers should be aware that
@@ -107,11 +114,11 @@
114 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
115 ** [sqlite_version()] and [sqlite_source_id()].
116 */
117 #define SQLITE_VERSION "3.8.9"
118 #define SQLITE_VERSION_NUMBER 3008009
119 #define SQLITE_SOURCE_ID "2015-03-24 18:19:39 436314b5728c9413f9ac2d837e1c19364f31be72"
120
121 /*
122 ** CAPI3REF: Run-Time Library Version Numbers
123 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
124 **
@@ -948,10 +955,17 @@
955 ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
956 ** opcode causes the xFileControl method to swap the file handle with the one
957 ** pointed to by the pArg argument. This capability is used during testing
958 ** and only needs to be supported when SQLITE_TEST is defined.
959 **
960 ** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
961 ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
962 ** be advantageous to block on the next WAL lock if the lock is not immediately
963 ** available. The WAL subsystem issues this signal during rare
964 ** circumstances in order to fix a problem with priority inversion.
965 ** Applications should <em>not</em> use this file-control.
966 **
967 ** </ul>
968 */
969 #define SQLITE_FCNTL_LOCKSTATE 1
970 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
971 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -972,10 +986,11 @@
986 #define SQLITE_FCNTL_TRACE 19
987 #define SQLITE_FCNTL_HAS_MOVED 20
988 #define SQLITE_FCNTL_SYNC 21
989 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
990 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
991 #define SQLITE_FCNTL_WAL_BLOCK 24
992
993 /* deprecated names */
994 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
995 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
996 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -1538,11 +1553,11 @@
1553 ** disabled, the following SQLite interfaces become non-operational:
1554 ** <ul>
1555 ** <li> [sqlite3_memory_used()]
1556 ** <li> [sqlite3_memory_highwater()]
1557 ** <li> [sqlite3_soft_heap_limit64()]
1558 ** <li> [sqlite3_status64()]
1559 ** </ul>)^
1560 ** ^Memory allocation statistics are enabled by default unless SQLite is
1561 ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1562 ** allocation statistics are disabled by default.
1563 ** </dd>
@@ -2995,15 +3010,17 @@
3010
3011
3012 /*
3013 ** CAPI3REF: Error Codes And Messages
3014 **
3015 ** ^If the most recent sqlite3_* API call associated with
3016 ** [database connection] D failed, then the sqlite3_errcode(D) interface
3017 ** returns the numeric [result code] or [extended result code] for that
3018 ** API call.
3019 ** If the most recent API call was successful,
3020 ** then the return value from sqlite3_errcode() is undefined.
3021 ** ^The sqlite3_extended_errcode()
3022 ** interface is the same except that it always returns the
3023 ** [extended result code] even when extended result codes are
3024 ** disabled.
3025 **
3026 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
@@ -5613,11 +5630,11 @@
5630 ** is delivered up to the client application, the string will be automatically
5631 ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
5632 */
5633 struct sqlite3_vtab {
5634 const sqlite3_module *pModule; /* The module for this virtual table */
5635 int nRef; /* Number of open cursors */
5636 char *zErrMsg; /* Error message from sqlite3_mprintf() */
5637 /* Virtual table implementations will typically add additional fields */
5638 };
5639
5640 /*
@@ -6291,11 +6308,11 @@
6308 #define SQLITE_TESTCTRL_LAST 25
6309
6310 /*
6311 ** CAPI3REF: SQLite Runtime Status
6312 **
6313 ** ^These interfaces are used to retrieve runtime status information
6314 ** about the performance of SQLite, and optionally to reset various
6315 ** highwater marks. ^The first argument is an integer code for
6316 ** the specific parameter to measure. ^(Recognized integer codes
6317 ** are of the form [status parameters | SQLITE_STATUS_...].)^
6318 ** ^The current value of the parameter is returned into *pCurrent.
@@ -6305,23 +6322,26 @@
6322 ** value. For those parameters
6323 ** nothing is written into *pHighwater and the resetFlag is ignored.)^
6324 ** ^(Other parameters record only the highwater mark and not the current
6325 ** value. For these latter parameters nothing is written into *pCurrent.)^
6326 **
6327 ** ^The sqlite3_status() and sqlite3_status64() routines return
6328 ** SQLITE_OK on success and a non-zero [error code] on failure.
6329 **
6330 ** If either the current value or the highwater mark is too large to
6331 ** be represented by a 32-bit integer, then the values returned by
6332 ** sqlite3_status() are undefined.
 
 
 
6333 **
6334 ** See also: [sqlite3_db_status()]
6335 */
6336 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
6337 SQLITE_API int sqlite3_status64(
6338 int op,
6339 sqlite3_int64 *pCurrent,
6340 sqlite3_int64 *pHighwater,
6341 int resetFlag
6342 );
6343
6344
6345 /*
6346 ** CAPI3REF: Status Parameters
6347 ** KEYWORDS: {status parameters}
6348

Keyboard Shortcuts

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