Fossil SCM

Update the built-in SQLite to 3.8.11 alpha.

drh 2015-05-29 17:52 trunk
Commit 2b1261a59e054c93ae7af8f9e8a54fa89a5db6a3
2 files changed +4912 -449 +49 -25
+4912 -449
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.8.10.2. By combining all the individual C code files into this
3
+** version 3.8.11. By combining all the individual C code files into this
44
** single large file, the entire code can be compiled as a single translation
55
** unit. This allows many compilers to do optimizations that would not be
66
** possible if the files were compiled separately. Performance improvements
77
** of 5% or more are commonly seen when SQLite is compiled as a single
88
** translation unit.
@@ -316,13 +316,13 @@
316316
**
317317
** See also: [sqlite3_libversion()],
318318
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
319319
** [sqlite_version()] and [sqlite_source_id()].
320320
*/
321
-#define SQLITE_VERSION "3.8.10.2"
322
-#define SQLITE_VERSION_NUMBER 3008010
323
-#define SQLITE_SOURCE_ID "2015-05-20 18:17:19 2ef4f3a5b1d1d0c4338f8243d40a2452cc1f7fe4"
321
+#define SQLITE_VERSION "3.8.11"
322
+#define SQLITE_VERSION_NUMBER 3008011
323
+#define SQLITE_SOURCE_ID "2015-05-29 17:51:16 db4e9728fae5f7b0fad6aa0a5be317a7c9e7c417"
324324
325325
/*
326326
** CAPI3REF: Run-Time Library Version Numbers
327327
** KEYWORDS: sqlite3_version, sqlite3_sourceid
328328
**
@@ -1161,17 +1161,25 @@
11611161
** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
11621162
** opcode causes the xFileControl method to swap the file handle with the one
11631163
** pointed to by the pArg argument. This capability is used during testing
11641164
** and only needs to be supported when SQLITE_TEST is defined.
11651165
**
1166
-** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1166
+* <li>[[SQLITE_FCNTL_WAL_BLOCK]]
11671167
** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
11681168
** be advantageous to block on the next WAL lock if the lock is not immediately
11691169
** available. The WAL subsystem issues this signal during rare
11701170
** circumstances in order to fix a problem with priority inversion.
11711171
** Applications should <em>not</em> use this file-control.
11721172
**
1173
+** <li>[[SQLITE_FCNTL_ZIPVFS]]
1174
+** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
1175
+** VFS should return SQLITE_NOTFOUND for this opcode.
1176
+**
1177
+** <li>[[SQLITE_FCNTL_OTA]]
1178
+** The [SQLITE_FCNTL_OTA] opcode is implemented by the special VFS used by
1179
+** the OTA extension only. All other VFS should return SQLITE_NOTFOUND for
1180
+** this opcode.
11731181
** </ul>
11741182
*/
11751183
#define SQLITE_FCNTL_LOCKSTATE 1
11761184
#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
11771185
#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1193,10 +1201,12 @@
11931201
#define SQLITE_FCNTL_HAS_MOVED 20
11941202
#define SQLITE_FCNTL_SYNC 21
11951203
#define SQLITE_FCNTL_COMMIT_PHASETWO 22
11961204
#define SQLITE_FCNTL_WIN32_SET_HANDLE 23
11971205
#define SQLITE_FCNTL_WAL_BLOCK 24
1206
+#define SQLITE_FCNTL_ZIPVFS 25
1207
+#define SQLITE_FCNTL_OTA 26
11981208
11991209
/* deprecated names */
12001210
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
12011211
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
12021212
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -3595,11 +3605,13 @@
35953605
**
35963606
** An sqlite3_value object may be either "protected" or "unprotected".
35973607
** Some interfaces require a protected sqlite3_value. Other interfaces
35983608
** will accept either a protected or an unprotected sqlite3_value.
35993609
** Every interface that accepts sqlite3_value arguments specifies
3600
-** whether or not it requires a protected sqlite3_value.
3610
+** whether or not it requires a protected sqlite3_value. The
3611
+** [sqlite3_value_dup()] interface can be used to construct a new
3612
+** protected sqlite3_value from an unprotected sqlite3_value.
36013613
**
36023614
** The terms "protected" and "unprotected" refer to whether or not
36033615
** a mutex is held. An internal mutex is held for a protected
36043616
** sqlite3_value object but no mutex is held for an unprotected
36053617
** sqlite3_value object. If SQLite is compiled to be single-threaded
@@ -4098,12 +4110,10 @@
40984110
/*
40994111
** CAPI3REF: Result Values From A Query
41004112
** KEYWORDS: {column access functions}
41014113
** METHOD: sqlite3_stmt
41024114
**
4103
-** These routines form the "result set" interface.
4104
-**
41054115
** ^These routines return information about a single column of the current
41064116
** result row of a query. ^In every case the first argument is a pointer
41074117
** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
41084118
** that was returned from [sqlite3_prepare_v2()] or one of its variants)
41094119
** and the second argument is the index of the column for which information
@@ -4159,17 +4169,18 @@
41594169
**
41604170
** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
41614171
** even empty strings, are always zero-terminated. ^The return
41624172
** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
41634173
**
4164
-** ^The object returned by [sqlite3_column_value()] is an
4165
-** [unprotected sqlite3_value] object. An unprotected sqlite3_value object
4166
-** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
4174
+** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
4175
+** [unprotected sqlite3_value] object. In a multithreaded environment,
4176
+** an unprotected sqlite3_value object may only be used safely with
4177
+** [sqlite3_bind_value()] and [sqlite3_result_value()].
41674178
** If the [unprotected sqlite3_value] object returned by
41684179
** [sqlite3_column_value()] is used in any other way, including calls
41694180
** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
4170
-** or [sqlite3_value_bytes()], then the behavior is undefined.
4181
+** or [sqlite3_value_bytes()], the behavior is not threadsafe.
41714182
**
41724183
** These routines attempt to convert the value where appropriate. ^For
41734184
** example, if the internal representation is FLOAT and a text result
41744185
** is requested, [sqlite3_snprintf()] is used internally to perform the
41754186
** conversion automatically. ^(The following table details the conversions
@@ -4196,16 +4207,10 @@
41964207
** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
41974208
** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
41984209
** </table>
41994210
** </blockquote>)^
42004211
**
4201
-** The table above makes reference to standard C library functions atoi()
4202
-** and atof(). SQLite does not really use these functions. It has its
4203
-** own equivalent internal routines. The atoi() and atof() names are
4204
-** used in the table for brevity and because they are familiar to most
4205
-** C programmers.
4206
-**
42074212
** Note that when type conversions occur, pointers returned by prior
42084213
** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
42094214
** sqlite3_column_text16() may be invalidated.
42104215
** Type conversions and pointer invalidations might occur
42114216
** in the following cases:
@@ -4226,11 +4231,11 @@
42264231
** not invalidate a prior pointer, though of course the content of the buffer
42274232
** that the prior pointer references will have been modified. Other kinds
42284233
** of conversion are done in place when it is possible, but sometimes they
42294234
** are not possible and in those cases prior pointers are invalidated.
42304235
**
4231
-** The safest and easiest to remember policy is to invoke these routines
4236
+** The safest policy is to invoke these routines
42324237
** in one of the following ways:
42334238
**
42344239
** <ul>
42354240
** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
42364241
** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
@@ -4246,11 +4251,11 @@
42464251
** with calls to sqlite3_column_bytes().
42474252
**
42484253
** ^The pointers returned are valid until a type conversion occurs as
42494254
** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
42504255
** [sqlite3_finalize()] is called. ^The memory space used to hold strings
4251
-** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned
4256
+** and BLOBs is freed automatically. Do <em>not</em> pass the pointers returned
42524257
** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
42534258
** [sqlite3_free()].
42544259
**
42554260
** ^(If a memory allocation error occurs during the evaluation of any
42564261
** of these routines, a default value is returned. The default value
@@ -4496,16 +4501,16 @@
44964501
SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
44974502
void*,sqlite3_int64);
44984503
#endif
44994504
45004505
/*
4501
-** CAPI3REF: Obtaining SQL Function Parameter Values
4506
+** CAPI3REF: Obtaining SQL Values
45024507
** METHOD: sqlite3_value
45034508
**
45044509
** The C-language implementation of SQL functions and aggregates uses
45054510
** this set of interface routines to access the parameter values on
4506
-** the function or aggregate.
4511
+** the function or aggregate.
45074512
**
45084513
** The xFunc (for scalar functions) or xStep (for aggregates) parameters
45094514
** to [sqlite3_create_function()] and [sqlite3_create_function16()]
45104515
** define callbacks that implement the SQL functions and aggregates.
45114516
** The 3rd parameter to these callbacks is an array of pointers to
@@ -4554,10 +4559,27 @@
45544559
SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value*);
45554560
SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value*);
45564561
SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value*);
45574562
SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*);
45584563
4564
+/*
4565
+** CAPI3REF: Copy And Free SQL Values
4566
+** METHOD: sqlite3_value
4567
+**
4568
+** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
4569
+** object D and returns a pointer to that copy. ^The [sqlite3_value] returned
4570
+** is a [protected sqlite3_value] object even if the input is not.
4571
+** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
4572
+** memory allocation fails.
4573
+**
4574
+** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
4575
+** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer
4576
+** then sqlite3_value_free(V) is a harmless no-op.
4577
+*/
4578
+SQLITE_API SQLITE_EXPERIMENTAL sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value*);
4579
+SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_value_free(sqlite3_value*);
4580
+
45594581
/*
45604582
** CAPI3REF: Obtain Aggregate Function Context
45614583
** METHOD: sqlite3_context
45624584
**
45634585
** Implementations of aggregate SQL functions use this
@@ -4801,11 +4823,11 @@
48014823
** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
48024824
** then SQLite makes a copy of the result into space obtained from
48034825
** from [sqlite3_malloc()] before it returns.
48044826
**
48054827
** ^The sqlite3_result_value() interface sets the result of
4806
-** the application-defined function to be a copy the
4828
+** the application-defined function to be a copy of the
48074829
** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
48084830
** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
48094831
** so that the [sqlite3_value] specified in the parameter may change or
48104832
** be deallocated after sqlite3_result_value() returns without harm.
48114833
** ^A [protected sqlite3_value] object may always be used where an
@@ -6077,11 +6099,11 @@
60776099
** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
60786100
** always returns zero.
60796101
**
60806102
** ^This function sets the database handle error code and message.
60816103
*/
6082
-SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
6104
+SQLITE_API int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
60836105
60846106
/*
60856107
** CAPI3REF: Close A BLOB Handle
60866108
** DESTRUCTOR: sqlite3_blob
60876109
**
@@ -7887,11 +7909,11 @@
78877909
** as if the loop did not exist - it returns non-zero and leave the variable
78887910
** that pOut points to unchanged.
78897911
**
78907912
** See also: [sqlite3_stmt_scanstatus_reset()]
78917913
*/
7892
-SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_stmt_scanstatus(
7914
+SQLITE_API int SQLITE_STDCALL sqlite3_stmt_scanstatus(
78937915
sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
78947916
int idx, /* Index of loop to report on */
78957917
int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
78967918
void *pOut /* Result written here */
78977919
);
@@ -7903,11 +7925,11 @@
79037925
** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
79047926
**
79057927
** This API is only available if the library is built with pre-processor
79067928
** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
79077929
*/
7908
-SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
7930
+SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
79097931
79107932
79117933
/*
79127934
** Undo the hack that converts floating point types to integer for
79137935
** builds on processors without floating point support.
@@ -8018,10 +8040,12 @@
80188040
sqlite3_int64 iRowid; /* Rowid for current entry */
80198041
sqlite3_rtree_dbl rParentScore; /* Score of parent node */
80208042
int eParentWithin; /* Visibility of parent node */
80218043
int eWithin; /* OUT: Visiblity */
80228044
sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
8045
+ /* The following fields are only available in 3.8.11 and later */
8046
+ sqlite3_value **apSqlParam; /* Original SQL values of parameters */
80238047
};
80248048
80258049
/*
80268050
** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
80278051
*/
@@ -11167,10 +11191,11 @@
1116711191
#define SQLITE_EnableTrigger 0x00800000 /* True to enable triggers */
1116811192
#define SQLITE_DeferFKs 0x01000000 /* Defer all FK constraints */
1116911193
#define SQLITE_QueryOnly 0x02000000 /* Disable database changes */
1117011194
#define SQLITE_VdbeEQP 0x04000000 /* Debug EXPLAIN QUERY PLAN */
1117111195
#define SQLITE_Vacuum 0x08000000 /* Currently in a VACUUM */
11196
+#define SQLITE_CellSizeCk 0x10000000 /* Check btree cell sizes on load */
1117211197
1117311198
1117411199
/*
1117511200
** Bits of the sqlite3.dbOptFlags field that are used by the
1117611201
** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
@@ -11548,12 +11573,13 @@
1154811573
#define TF_Readonly 0x01 /* Read-only system table */
1154911574
#define TF_Ephemeral 0x02 /* An ephemeral table */
1155011575
#define TF_HasPrimaryKey 0x04 /* Table has a primary key */
1155111576
#define TF_Autoincrement 0x08 /* Integer primary key is autoincrement */
1155211577
#define TF_Virtual 0x10 /* Is a virtual table */
11553
-#define TF_WithoutRowid 0x20 /* No rowid used. PRIMARY KEY is the key */
11554
-#define TF_OOOHidden 0x40 /* Out-of-Order hidden columns */
11578
+#define TF_WithoutRowid 0x20 /* No rowid. PRIMARY KEY is the key */
11579
+#define TF_NoVisibleRowid 0x40 /* No user-visible "rowid" column */
11580
+#define TF_OOOHidden 0x80 /* Out-of-Order hidden columns */
1155511581
1155611582
1155711583
/*
1155811584
** Test to see whether or not a table is a virtual table. This is
1155911585
** done as a macro so that it will be optimized out when virtual
@@ -11567,10 +11593,11 @@
1156711593
# define IsHiddenColumn(X) 0
1156811594
#endif
1156911595
1157011596
/* Does the table have a rowid */
1157111597
#define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
11598
+#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
1157211599
1157311600
/*
1157411601
** Each foreign key constraint is an instance of the following structure.
1157511602
**
1157611603
** A foreign key is associated with two tables. The "from" table is
@@ -11725,10 +11752,18 @@
1172511752
** must be unique and what to do if they are not. When Index.onError=OE_None,
1172611753
** it means this is not a unique index. Otherwise it is a unique index
1172711754
** and the value of Index.onError indicate the which conflict resolution
1172811755
** algorithm to employ whenever an attempt is made to insert a non-unique
1172911756
** element.
11757
+**
11758
+** While parsing a CREATE TABLE or CREATE INDEX statement in order to
11759
+** generate VDBE code (as opposed to parsing one read from an sqlite_master
11760
+** table as part of parsing an existing database schema), transient instances
11761
+** of this structure may be created. In this case the Index.tnum variable is
11762
+** used to store the address of a VDBE instruction, not a database page
11763
+** number (it cannot - the database page is not allocated until the VDBE
11764
+** program is executed). See convertToWithoutRowidTable() for details.
1173011765
*/
1173111766
struct Index {
1173211767
char *zName; /* Name of this index */
1173311768
i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
1173411769
LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
@@ -12299,23 +12334,24 @@
1229912334
/*
1230012335
** Allowed values for Select.selFlags. The "SF" prefix stands for
1230112336
** "Select Flag".
1230212337
*/
1230312338
#define SF_Distinct 0x0001 /* Output should be DISTINCT */
12304
-#define SF_Resolved 0x0002 /* Identifiers have been resolved */
12305
-#define SF_Aggregate 0x0004 /* Contains aggregate functions */
12306
-#define SF_UsesEphemeral 0x0008 /* Uses the OpenEphemeral opcode */
12307
-#define SF_Expanded 0x0010 /* sqlite3SelectExpand() called on this */
12308
-#define SF_HasTypeInfo 0x0020 /* FROM subqueries have Table metadata */
12309
-#define SF_Compound 0x0040 /* Part of a compound query */
12310
-#define SF_Values 0x0080 /* Synthesized from VALUES clause */
12311
-#define SF_MultiValue 0x0100 /* Single VALUES term with multiple rows */
12312
-#define SF_NestedFrom 0x0200 /* Part of a parenthesized FROM clause */
12313
-#define SF_MaybeConvert 0x0400 /* Need convertCompoundSelectToSubquery() */
12314
-#define SF_Recursive 0x0800 /* The recursive part of a recursive CTE */
12339
+#define SF_All 0x0002 /* Includes the ALL keyword */
12340
+#define SF_Resolved 0x0004 /* Identifiers have been resolved */
12341
+#define SF_Aggregate 0x0008 /* Contains aggregate functions */
12342
+#define SF_UsesEphemeral 0x0010 /* Uses the OpenEphemeral opcode */
12343
+#define SF_Expanded 0x0020 /* sqlite3SelectExpand() called on this */
12344
+#define SF_HasTypeInfo 0x0040 /* FROM subqueries have Table metadata */
12345
+#define SF_Compound 0x0080 /* Part of a compound query */
12346
+#define SF_Values 0x0100 /* Synthesized from VALUES clause */
12347
+#define SF_MultiValue 0x0200 /* Single VALUES term with multiple rows */
12348
+#define SF_NestedFrom 0x0400 /* Part of a parenthesized FROM clause */
12349
+#define SF_MaybeConvert 0x0800 /* Need convertCompoundSelectToSubquery() */
1231512350
#define SF_MinMaxAgg 0x1000 /* Aggregate containing min() or max() */
12316
-#define SF_Converted 0x2000 /* By convertCompoundSelectToSubquery() */
12351
+#define SF_Recursive 0x2000 /* The recursive part of a recursive CTE */
12352
+#define SF_Converted 0x4000 /* By convertCompoundSelectToSubquery() */
1231712353
1231812354
1231912355
/*
1232012356
** The results of a SELECT can be distributed in several ways, as defined
1232112357
** by one of the following macros. The "SRT" prefix means "SELECT Result
@@ -12553,11 +12589,10 @@
1255312589
1255412590
/* Information used while coding trigger programs. */
1255512591
Parse *pToplevel; /* Parse structure for main program (or NULL) */
1255612592
Table *pTriggerTab; /* Table triggers are being coded for */
1255712593
int addrCrTab; /* Address of OP_CreateTable opcode on CREATE TABLE */
12558
- int addrSkipPK; /* Address of instruction to skip PRIMARY KEY index */
1255912594
u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
1256012595
u32 oldmask; /* Mask of old.* columns referenced */
1256112596
u32 newmask; /* Mask of new.* columns referenced */
1256212597
u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
1256312598
u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
@@ -13065,11 +13100,10 @@
1306513100
#define SQLITE_PRINTF_SQLFUNC 0x02
1306613101
SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
1306713102
SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
1306813103
SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
1306913104
SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
13070
-SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
1307113105
#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
1307213106
SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
1307313107
#endif
1307413108
#if defined(SQLITE_TEST)
1307513109
SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
@@ -13084,11 +13118,11 @@
1308413118
SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
1308513119
SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
1308613120
#endif
1308713121
1308813122
13089
-SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
13123
+SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
1309013124
SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
1309113125
SQLITE_PRIVATE int sqlite3Dequote(char*);
1309213126
SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
1309313127
SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
1309413128
SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
@@ -13780,10 +13814,14 @@
1378013814
*/
1378113815
#if SQLITE_MAX_WORKER_THREADS>0
1378213816
SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
1378313817
SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
1378413818
#endif
13819
+
13820
+#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
13821
+SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*);
13822
+#endif
1378513823
1378613824
#endif /* _SQLITEINT_H_ */
1378713825
1378813826
/************** End of sqliteInt.h *******************************************/
1378913827
/************** Begin file global.c ******************************************/
@@ -14685,10 +14723,16 @@
1468514723
Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */
1468614724
void *pFiller; /* So that sizeof(Mem) is a multiple of 8 */
1468714725
#endif
1468814726
};
1468914727
14728
+/*
14729
+** Size of struct Mem not including the Mem.zMalloc member or anything that
14730
+** follows.
14731
+*/
14732
+#define MEMCELLSIZE offsetof(Mem,zMalloc)
14733
+
1469014734
/* One or more of the following flags are set to indicate the validOK
1469114735
** representations of the value stored in the Mem struct.
1469214736
**
1469314737
** If the MEM_Null flag is set, then the value is an SQL NULL value.
1469414738
** No other flags may be set in this case.
@@ -14890,10 +14934,11 @@
1489014934
#define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */
1489114935
1489214936
/*
1489314937
** Function prototypes
1489414938
*/
14939
+SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
1489514940
SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
1489614941
void sqliteVdbePopStack(Vdbe*,int);
1489714942
SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
1489814943
SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
1489914944
#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
@@ -20738,28 +20783,24 @@
2073820783
2073920784
/*
2074020785
** Return the amount of memory currently checked out.
2074120786
*/
2074220787
SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void){
20743
- int n, mx;
20744
- sqlite3_int64 res;
20745
- sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
20746
- res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
20788
+ sqlite3_int64 res, mx;
20789
+ sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
2074720790
return res;
2074820791
}
2074920792
2075020793
/*
2075120794
** Return the maximum amount of memory that has ever been
2075220795
** checked out since either the beginning of this process
2075320796
** or since the most recent reset.
2075420797
*/
2075520798
SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag){
20756
- int n, mx;
20757
- sqlite3_int64 res;
20758
- sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
20759
- res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
20760
- return res;
20799
+ sqlite3_int64 res, mx;
20800
+ sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
20801
+ return mx;
2076120802
}
2076220803
2076320804
/*
2076420805
** Trigger the alarm
2076520806
*/
@@ -21287,23 +21328,15 @@
2128721328
}
2128821329
return zNew;
2128921330
}
2129021331
2129121332
/*
21292
-** Create a string from the zFromat argument and the va_list that follows.
21293
-** Store the string in memory obtained from sqliteMalloc() and make *pz
21294
-** point to that string.
21333
+** Free any prior content in *pz and replace it with a copy of zNew.
2129521334
*/
21296
-SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
21297
- va_list ap;
21298
- char *z;
21299
-
21300
- va_start(ap, zFormat);
21301
- z = sqlite3VMPrintf(db, zFormat, ap);
21302
- va_end(ap);
21335
+SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
2130321336
sqlite3DbFree(db, *pz);
21304
- *pz = z;
21337
+ *pz = sqlite3DbStrDup(db, zNew);
2130521338
}
2130621339
2130721340
/*
2130821341
** Take actions at the end of an API call to indicate an OOM error
2130921342
*/
@@ -22271,28 +22304,10 @@
2227122304
z = sqlite3VMPrintf(db, zFormat, ap);
2227222305
va_end(ap);
2227322306
return z;
2227422307
}
2227522308
22276
-/*
22277
-** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
22278
-** the string and before returning. This routine is intended to be used
22279
-** to modify an existing string. For example:
22280
-**
22281
-** x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
22282
-**
22283
-*/
22284
-SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
22285
- va_list ap;
22286
- char *z;
22287
- va_start(ap, zFormat);
22288
- z = sqlite3VMPrintf(db, zFormat, ap);
22289
- va_end(ap);
22290
- sqlite3DbFree(db, zStr);
22291
- return z;
22292
-}
22293
-
2229422309
/*
2229522310
** Print into memory obtained from sqlite3_malloc(). Omit the internal
2229622311
** %-conversion extensions.
2229722312
*/
2229822313
SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char *zFormat, va_list ap){
@@ -36220,10 +36235,16 @@
3622036235
*/
3622136236
if( pFile->locktype>=locktype ){
3622236237
OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
3622336238
return SQLITE_OK;
3622436239
}
36240
+
36241
+ /* Do not allow any kind of write-lock on a read-only database
36242
+ */
36243
+ if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
36244
+ return SQLITE_IOERR_LOCK;
36245
+ }
3622536246
3622636247
/* Make sure the locking sequence is correct
3622736248
*/
3622836249
assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
3622936250
assert( locktype!=PENDING_LOCK );
@@ -38617,18 +38638,18 @@
3861738638
#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
3861838639
if( sizeof(UUID)<=nBuf-n ){
3861938640
UUID id;
3862038641
memset(&id, 0, sizeof(UUID));
3862138642
osUuidCreate(&id);
38622
- memcpy(zBuf, &id, sizeof(UUID));
38643
+ memcpy(&zBuf[n], &id, sizeof(UUID));
3862338644
n += sizeof(UUID);
3862438645
}
3862538646
if( sizeof(UUID)<=nBuf-n ){
3862638647
UUID id;
3862738648
memset(&id, 0, sizeof(UUID));
3862838649
osUuidCreateSequential(&id);
38629
- memcpy(zBuf, &id, sizeof(UUID));
38650
+ memcpy(&zBuf[n], &id, sizeof(UUID));
3863038651
n += sizeof(UUID);
3863138652
}
3863238653
#endif
3863338654
#endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */
3863438655
return n;
@@ -44777,15 +44798,14 @@
4477744798
*/
4477844799
assert( pPager->eState==PAGER_OPEN );
4477944800
assert( pPager->eLock>=SHARED_LOCK );
4478044801
nPage = sqlite3WalDbsize(pPager->pWal);
4478144802
44782
- /* If the database size was not available from the WAL sub-system,
44783
- ** determine it based on the size of the database file. If the size
44784
- ** of the database file is not an integer multiple of the page-size,
44785
- ** round down to the nearest page. Except, any file larger than 0
44786
- ** bytes in size is considered to contain at least one page.
44803
+ /* If the number of pages in the database is not available from the
44804
+ ** WAL sub-system, determine the page counte based on the size of
44805
+ ** the database file. If the size of the database file is not an
44806
+ ** integer multiple of the page-size, round up the result.
4478744807
*/
4478844808
if( nPage==0 ){
4478944809
i64 n = 0; /* Size of db file in bytes */
4479044810
assert( isOpen(pPager->fd) || pPager->tempFile );
4479144811
if( isOpen(pPager->fd) ){
@@ -54245,30 +54265,22 @@
5424554265
u8 *pAddr; /* The i-th cell pointer */
5424654266
pAddr = &data[cellOffset + i*2];
5424754267
pc = get2byte(pAddr);
5424854268
testcase( pc==iCellFirst );
5424954269
testcase( pc==iCellLast );
54250
-#if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
5425154270
/* These conditions have already been verified in btreeInitPage()
54252
- ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined
54271
+ ** if PRAGMA cell_size_check=ON.
5425354272
*/
5425454273
if( pc<iCellFirst || pc>iCellLast ){
5425554274
return SQLITE_CORRUPT_BKPT;
5425654275
}
54257
-#endif
5425854276
assert( pc>=iCellFirst && pc<=iCellLast );
5425954277
size = cellSizePtr(pPage, &src[pc]);
5426054278
cbrk -= size;
54261
-#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
54262
- if( cbrk<iCellFirst ){
54263
- return SQLITE_CORRUPT_BKPT;
54264
- }
54265
-#else
5426654279
if( cbrk<iCellFirst || pc+size>usableSize ){
5426754280
return SQLITE_CORRUPT_BKPT;
5426854281
}
54269
-#endif
5427054282
assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
5427154283
testcase( cbrk+size==usableSize );
5427254284
testcase( pc+size==usableSize );
5427354285
put2byte(pAddr, cbrk);
5427454286
if( temp==0 ){
@@ -54340,11 +54352,11 @@
5434054352
}
5434154353
/* Remove the slot from the free-list. Update the number of
5434254354
** fragmented bytes within the page. */
5434354355
memcpy(&aData[iAddr], &aData[pc], 2);
5434454356
aData[hdr+7] += (u8)x;
54345
- }else if( size+pc > usableSize ){
54357
+ }else if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){
5434654358
*pRc = SQLITE_CORRUPT_BKPT;
5434754359
return 0;
5434854360
}else{
5434954361
/* The slot remains on the free-list. Reduce its size to account
5435054362
** for the portion used by the new allocation. */
@@ -54392,11 +54404,15 @@
5439254404
** and the reserved space is zero (the usual value for reserved space)
5439354405
** then the cell content offset of an empty page wants to be 65536.
5439454406
** However, that integer is too large to be stored in a 2-byte unsigned
5439554407
** integer, so a value of 0 is used in its place. */
5439654408
top = get2byteNotZero(&data[hdr+5]);
54397
- if( gap>top ) return SQLITE_CORRUPT_BKPT;
54409
+ if( gap>top || NEVER((u32)top>pPage->pBt->usableSize) ){
54410
+ /* The NEVER() is because a oversize "top" value will be blocked from
54411
+ ** reaching this point by btreeInitPage() or btreeGetUnusedPage() */
54412
+ return SQLITE_CORRUPT_BKPT;
54413
+ }
5439854414
5439954415
/* If there is enough space between gap and top for one more cell pointer
5440054416
** array entry offset, and if the freelist is not empty, then search the
5440154417
** freelist looking for a free slot big enough to satisfy the request.
5440254418
*/
@@ -54465,11 +54481,11 @@
5446554481
u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
5446654482
unsigned char *data = pPage->aData; /* Page content */
5446754483
5446854484
assert( pPage->pBt!=0 );
5446954485
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
54470
- assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
54486
+ assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
5447154487
assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
5447254488
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5447354489
assert( iSize>=4 ); /* Minimum cell size is 4 */
5447454490
assert( iStart<=iLast );
5447554491
@@ -54605,10 +54621,11 @@
5460554621
** we failed to detect any corruption.
5460654622
*/
5460754623
static int btreeInitPage(MemPage *pPage){
5460854624
5460954625
assert( pPage->pBt!=0 );
54626
+ assert( pPage->pBt->db!=0 );
5461054627
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5461154628
assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
5461254629
assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
5461354630
assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
5461454631
@@ -54663,12 +54680,11 @@
5466354680
** past the end of a page boundary and causes SQLITE_CORRUPT to be
5466454681
** returned if it does.
5466554682
*/
5466654683
iCellFirst = cellOffset + 2*pPage->nCell;
5466754684
iCellLast = usableSize - 4;
54668
-#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
54669
- {
54685
+ if( pBt->db->flags & SQLITE_CellSizeCk ){
5467054686
int i; /* Index into the cell pointer array */
5467154687
int sz; /* Size of a cell */
5467254688
5467354689
if( !pPage->leaf ) iCellLast--;
5467454690
for(i=0; i<pPage->nCell; i++){
@@ -54684,11 +54700,10 @@
5468454700
return SQLITE_CORRUPT_BKPT;
5468554701
}
5468654702
}
5468754703
if( !pPage->leaf ) iCellLast++;
5468854704
}
54689
-#endif
5469054705
5469154706
/* Compute the total free space on the page
5469254707
** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
5469354708
** start of the first freeblock on the page, or is zero if there are no
5469454709
** freeblocks. */
@@ -54781,14 +54796,14 @@
5478154796
return pPage;
5478254797
}
5478354798
5478454799
/*
5478554800
** Get a page from the pager. Initialize the MemPage.pBt and
54786
-** MemPage.aData elements if needed.
54801
+** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
5478754802
**
54788
-** If the noContent flag is set, it means that we do not care about
54789
-** the content of the page at this time. So do not go to the disk
54803
+** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
54804
+** about the content of the page at this time. So do not go to the disk
5479054805
** to fetch the content. Just fill in the content with zeros for now.
5479154806
** If in the future we call sqlite3PagerWrite() on this page, that
5479254807
** means we have started to be concerned about content and the disk
5479354808
** read should occur at that point.
5479454809
*/
@@ -54885,10 +54900,40 @@
5488554900
assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
5488654901
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5488754902
sqlite3PagerUnrefNotNull(pPage->pDbPage);
5488854903
}
5488954904
}
54905
+
54906
+/*
54907
+** Get an unused page.
54908
+**
54909
+** This works just like btreeGetPage() with the addition:
54910
+**
54911
+** * If the page is already in use for some other purpose, immediately
54912
+** release it and return an SQLITE_CURRUPT error.
54913
+** * Make sure the isInit flag is clear
54914
+*/
54915
+static int btreeGetUnusedPage(
54916
+ BtShared *pBt, /* The btree */
54917
+ Pgno pgno, /* Number of the page to fetch */
54918
+ MemPage **ppPage, /* Return the page in this parameter */
54919
+ int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
54920
+){
54921
+ int rc = btreeGetPage(pBt, pgno, ppPage, flags);
54922
+ if( rc==SQLITE_OK ){
54923
+ if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
54924
+ releasePage(*ppPage);
54925
+ *ppPage = 0;
54926
+ return SQLITE_CORRUPT_BKPT;
54927
+ }
54928
+ (*ppPage)->isInit = 0;
54929
+ }else{
54930
+ *ppPage = 0;
54931
+ }
54932
+ return rc;
54933
+}
54934
+
5489054935
5489154936
/*
5489254937
** During a rollback, when the pager reloads information into the cache
5489354938
** so that the cache is restored to its original state at the start of
5489454939
** the transaction, for each page restored this routine is called.
@@ -56133,12 +56178,14 @@
5613356178
put4byte(pPage->aData, iTo);
5613456179
}else{
5613556180
u8 isInitOrig = pPage->isInit;
5613656181
int i;
5613756182
int nCell;
56183
+ int rc;
5613856184
56139
- btreeInitPage(pPage);
56185
+ rc = btreeInitPage(pPage);
56186
+ if( rc ) return rc;
5614056187
nCell = pPage->nCell;
5614156188
5614256189
for(i=0; i<nCell; i++){
5614356190
u8 *pCell = findCell(pPage, i);
5614456191
if( eType==PTRMAP_OVERFLOW1 ){
@@ -56935,13 +56982,17 @@
5693556982
int wrFlag, /* 1 to write. 0 read-only */
5693656983
struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
5693756984
BtCursor *pCur /* Write new cursor here */
5693856985
){
5693956986
int rc;
56940
- sqlite3BtreeEnter(p);
56941
- rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
56942
- sqlite3BtreeLeave(p);
56987
+ if( iTable<1 ){
56988
+ rc = SQLITE_CORRUPT_BKPT;
56989
+ }else{
56990
+ sqlite3BtreeEnter(p);
56991
+ rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
56992
+ sqlite3BtreeLeave(p);
56993
+ }
5694356994
return rc;
5694456995
}
5694556996
5694656997
/*
5694756998
** Return the size of a BtCursor object in bytes.
@@ -57965,11 +58016,11 @@
5796558016
assert( lwr+upr>=0 );
5796658017
idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
5796758018
}
5796858019
}else{
5796958020
for(;;){
57970
- int nCell;
58021
+ int nCell; /* Size of the pCell cell in bytes */
5797158022
pCell = findCell(pPage, idx) + pPage->childPtrSize;
5797258023
5797358024
/* The maximum supported page-size is 65536 bytes. This means that
5797458025
** the maximum number of record bytes stored on an index B-Tree
5797558026
** page is less than 16384 bytes and may be stored as a 2-byte
@@ -57994,16 +58045,29 @@
5799458045
c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
5799558046
}else{
5799658047
/* The record flows over onto one or more overflow pages. In
5799758048
** this case the whole cell needs to be parsed, a buffer allocated
5799858049
** and accessPayload() used to retrieve the record into the
57999
- ** buffer before VdbeRecordCompare() can be called. */
58050
+ ** buffer before VdbeRecordCompare() can be called.
58051
+ **
58052
+ ** If the record is corrupt, the xRecordCompare routine may read
58053
+ ** up to two varints past the end of the buffer. An extra 18
58054
+ ** bytes of padding is allocated at the end of the buffer in
58055
+ ** case this happens. */
5800058056
void *pCellKey;
5800158057
u8 * const pCellBody = pCell - pPage->childPtrSize;
5800258058
btreeParseCellPtr(pPage, pCellBody, &pCur->info);
5800358059
nCell = (int)pCur->info.nKey;
58004
- pCellKey = sqlite3Malloc( nCell );
58060
+ testcase( nCell<0 ); /* True if key size is 2^32 or more */
58061
+ testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
58062
+ testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
58063
+ testcase( nCell==2 ); /* Minimum legal index key size */
58064
+ if( nCell<2 ){
58065
+ rc = SQLITE_CORRUPT_BKPT;
58066
+ goto moveto_finish;
58067
+ }
58068
+ pCellKey = sqlite3Malloc( nCell+18 );
5800558069
if( pCellKey==0 ){
5800658070
rc = SQLITE_NOMEM;
5800758071
goto moveto_finish;
5800858072
}
5800958073
pCur->aiIdx[pCur->iPage] = (u16)idx;
@@ -58387,11 +58451,11 @@
5838758451
}
5838858452
testcase( iTrunk==mxPage );
5838958453
if( iTrunk>mxPage ){
5839058454
rc = SQLITE_CORRUPT_BKPT;
5839158455
}else{
58392
- rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
58456
+ rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
5839358457
}
5839458458
if( rc ){
5839558459
pTrunk = 0;
5839658460
goto end_allocate_page;
5839758461
}
@@ -58452,11 +58516,11 @@
5845258516
if( iNewTrunk>mxPage ){
5845358517
rc = SQLITE_CORRUPT_BKPT;
5845458518
goto end_allocate_page;
5845558519
}
5845658520
testcase( iNewTrunk==mxPage );
58457
- rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
58521
+ rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
5845858522
if( rc!=SQLITE_OK ){
5845958523
goto end_allocate_page;
5846058524
}
5846158525
rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
5846258526
if( rc!=SQLITE_OK ){
@@ -58532,11 +58596,11 @@
5853258596
if( closest<k-1 ){
5853358597
memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
5853458598
}
5853558599
put4byte(&aData[4], k-1);
5853658600
noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
58537
- rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
58601
+ rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
5853858602
if( rc==SQLITE_OK ){
5853958603
rc = sqlite3PagerWrite((*ppPage)->pDbPage);
5854058604
if( rc!=SQLITE_OK ){
5854158605
releasePage(*ppPage);
5854258606
}
@@ -58580,11 +58644,11 @@
5858058644
** becomes a new pointer-map page, the second is used by the caller.
5858158645
*/
5858258646
MemPage *pPg = 0;
5858358647
TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
5858458648
assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
58585
- rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent);
58649
+ rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
5858658650
if( rc==SQLITE_OK ){
5858758651
rc = sqlite3PagerWrite(pPg->pDbPage);
5858858652
releasePage(pPg);
5858958653
}
5859058654
if( rc ) return rc;
@@ -58594,35 +58658,27 @@
5859458658
#endif
5859558659
put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
5859658660
*pPgno = pBt->nPage;
5859758661
5859858662
assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
58599
- rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent);
58663
+ rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
5860058664
if( rc ) return rc;
5860158665
rc = sqlite3PagerWrite((*ppPage)->pDbPage);
5860258666
if( rc!=SQLITE_OK ){
5860358667
releasePage(*ppPage);
58668
+ *ppPage = 0;
5860458669
}
5860558670
TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
5860658671
}
5860758672
5860858673
assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
5860958674
5861058675
end_allocate_page:
5861158676
releasePage(pTrunk);
5861258677
releasePage(pPrevTrunk);
58613
- if( rc==SQLITE_OK ){
58614
- if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
58615
- releasePage(*ppPage);
58616
- *ppPage = 0;
58617
- return SQLITE_CORRUPT_BKPT;
58618
- }
58619
- (*ppPage)->isInit = 0;
58620
- }else{
58621
- *ppPage = 0;
58622
- }
58623
- assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
58678
+ assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
58679
+ assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
5862458680
return rc;
5862558681
}
5862658682
5862758683
/*
5862858684
** This function is used to add page iPage to the database file free-list.
@@ -58643,13 +58699,14 @@
5864358699
MemPage *pPage; /* Page being freed. May be NULL. */
5864458700
int rc; /* Return Code */
5864558701
int nFree; /* Initial number of pages on free-list */
5864658702
5864758703
assert( sqlite3_mutex_held(pBt->mutex) );
58648
- assert( iPage>1 );
58704
+ assert( CORRUPT_DB || iPage>1 );
5864958705
assert( !pMemPage || pMemPage->pgno==iPage );
5865058706
58707
+ if( iPage<2 ) return SQLITE_CORRUPT_BKPT;
5865158708
if( pMemPage ){
5865258709
pPage = pMemPage;
5865358710
sqlite3PagerRef(pPage->pDbPage);
5865458711
}else{
5865558712
pPage = btreePageLookup(pBt, iPage);
@@ -58797,11 +58854,13 @@
5879758854
}
5879858855
ovflPgno = get4byte(&pCell[info.iOverflow]);
5879958856
assert( pBt->usableSize > 4 );
5880058857
ovflPageSize = pBt->usableSize - 4;
5880158858
nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
58802
- assert( ovflPgno==0 || nOvfl>0 );
58859
+ assert( nOvfl>0 ||
58860
+ (CORRUPT_DB && (info.nPayload + ovflPageSize)<ovflPageSize)
58861
+ );
5880358862
while( nOvfl-- ){
5880458863
Pgno iNext = 0;
5880558864
MemPage *pOvfl = 0;
5880658865
if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
5880758866
/* 0 is not a legal page number and page 1 cannot be an
@@ -59052,11 +59111,11 @@
5905259111
int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
5905359112
5905459113
if( *pRC ) return;
5905559114
5905659115
assert( idx>=0 && idx<pPage->nCell );
59057
- assert( sz==cellSize(pPage, idx) );
59116
+ assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
5905859117
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
5905959118
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5906059119
data = pPage->aData;
5906159120
ptr = &pPage->aCellIdx[2*idx];
5906259121
pc = get2byte(ptr);
@@ -59216,11 +59275,12 @@
5921659275
}
5921759276
pData -= szCell[i];
5921859277
memcpy(pData, pCell, szCell[i]);
5921959278
put2byte(pCellptr, (pData - aData));
5922059279
pCellptr += 2;
59221
- assert( szCell[i]==cellSizePtr(pPg, pCell) );
59280
+ assert( szCell[i]==cellSizePtr(pPg, pCell) || CORRUPT_DB );
59281
+ testcase( szCell[i]==cellSizePtr(pPg,pCell) );
5922259282
}
5922359283
5922459284
/* The pPg->nFree field is now set incorrectly. The caller will fix it. */
5922559285
pPg->nCell = nCell;
5922659286
pPg->nOverflow = 0;
@@ -59893,10 +59953,18 @@
5989359953
leafCorrection = apOld[0]->leaf*4;
5989459954
leafData = apOld[0]->intKeyLeaf;
5989559955
for(i=0; i<nOld; i++){
5989659956
int limit;
5989759957
MemPage *pOld = apOld[i];
59958
+
59959
+ /* Verify that all sibling pages are of the same "type" (table-leaf,
59960
+ ** table-interior, index-leaf, or index-interior).
59961
+ */
59962
+ if( pOld->aData[0]!=apOld[0]->aData[0] ){
59963
+ rc = SQLITE_CORRUPT_BKPT;
59964
+ goto balance_cleanup;
59965
+ }
5989859966
5989959967
limit = pOld->nCell+pOld->nOverflow;
5990059968
if( pOld->nOverflow>0 ){
5990159969
for(j=0; j<limit; j++){
5990259970
assert( nCell<nMaxCells );
@@ -59935,17 +60003,17 @@
5993560003
/* The right pointer of the child page pOld becomes the left
5993660004
** pointer of the divider cell */
5993760005
memcpy(apCell[nCell], &pOld->aData[8], 4);
5993860006
}else{
5993960007
assert( leafCorrection==4 );
59940
- if( szCell[nCell]<4 ){
60008
+ while( szCell[nCell]<4 ){
5994160009
/* Do not allow any cells smaller than 4 bytes. If a smaller cell
5994260010
** does exist, pad it with 0x00 bytes. */
59943
- assert( szCell[nCell]==3 );
59944
- assert( apCell[nCell]==&aSpace1[iSpace1-3] );
60011
+ assert( szCell[nCell]==3 || CORRUPT_DB );
60012
+ assert( apCell[nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
5994560013
aSpace1[iSpace1++] = 0x00;
59946
- szCell[nCell] = 4;
60014
+ szCell[nCell]++;
5994760015
}
5994860016
}
5994960017
nCell++;
5995060018
}
5995160019
}
@@ -60032,14 +60100,10 @@
6003260100
));
6003360101
6003460102
/*
6003560103
** Allocate k new pages. Reuse old pages where possible.
6003660104
*/
60037
- if( apOld[0]->pgno<=1 ){
60038
- rc = SQLITE_CORRUPT_BKPT;
60039
- goto balance_cleanup;
60040
- }
6004160105
pageFlags = apOld[0]->aData[0];
6004260106
for(i=0; i<k; i++){
6004360107
MemPage *pNew;
6004460108
if( i<nOld ){
6004560109
pNew = apNew[i] = apOld[i];
@@ -60817,10 +60881,11 @@
6081760881
int nCell;
6081860882
Pgno n = pCur->apPage[iCellDepth+1]->pgno;
6081960883
unsigned char *pTmp;
6082060884
6082160885
pCell = findCell(pLeaf, pLeaf->nCell-1);
60886
+ if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
6082260887
nCell = cellSizePtr(pLeaf, pCell);
6082360888
assert( MX_CELL_SIZE(pBt) >= nCell );
6082460889
pTmp = pBt->pTmpSpace;
6082560890
assert( pTmp!=0 );
6082660891
rc = sqlite3PagerWrite(pLeaf->pDbPage);
@@ -60909,11 +60974,12 @@
6090960974
*/
6091060975
while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
6091160976
pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
6091260977
pgnoRoot++;
6091360978
}
60914
- assert( pgnoRoot>=3 );
60979
+ assert( pgnoRoot>=3 || CORRUPT_DB );
60980
+ testcase( pgnoRoot<3 );
6091560981
6091660982
/* Allocate a page. The page that currently resides at pgnoRoot will
6091760983
** be moved to the allocated page (unless the allocated page happens
6091860984
** to reside at pgnoRoot).
6091960985
*/
@@ -61059,11 +61125,12 @@
6105961125
}
6106061126
if( !pPage->leaf ){
6106161127
rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
6106261128
if( rc ) goto cleardatabasepage_out;
6106361129
}else if( pnChange ){
61064
- assert( pPage->intKey );
61130
+ assert( pPage->intKey || CORRUPT_DB );
61131
+ testcase( !pPage->intKey );
6106561132
*pnChange += pPage->nCell;
6106661133
}
6106761134
if( freePageFlag ){
6106861135
freePage(pPage, &rc);
6106961136
}else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
@@ -63854,14 +63921,10 @@
6385463921
}
6385563922
pMem->pScopyFrom = 0;
6385663923
}
6385763924
#endif /* SQLITE_DEBUG */
6385863925
63859
-/*
63860
-** Size of struct Mem not including the Mem.zMalloc member.
63861
-*/
63862
-#define MEMCELLSIZE offsetof(Mem,zMalloc)
6386363926
6386463927
/*
6386563928
** Make an shallow copy of pFrom into pTo. Prior contents of
6386663929
** pTo are freed. The pFrom->z field is not duplicated. If
6386763930
** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
@@ -63884,11 +63947,14 @@
6388463947
** freed before the copy is made.
6388563948
*/
6388663949
SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
6388763950
int rc = SQLITE_OK;
6388863951
63889
- assert( pTo->db==pFrom->db );
63952
+ /* The pFrom==0 case in the following assert() is when an sqlite3_value
63953
+ ** from sqlite3_value_dup() is used as the argument
63954
+ ** to sqlite3_result_value(). */
63955
+ assert( pTo->db==pFrom->db || pFrom->db==0 );
6389063956
assert( (pFrom->flags & MEM_RowSet)==0 );
6389163957
if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
6389263958
memcpy(pTo, pFrom, MEMCELLSIZE);
6389363959
pTo->flags &= ~MEM_Dyn;
6389463960
if( pTo->flags&(MEM_Str|MEM_Blob) ){
@@ -64817,10 +64883,21 @@
6481764883
assert( pParse->aLabel==0 );
6481864884
assert( pParse->nLabel==0 );
6481964885
assert( pParse->nOpAlloc==0 );
6482064886
return p;
6482164887
}
64888
+
64889
+/*
64890
+** Change the error string stored in Vdbe.zErrMsg
64891
+*/
64892
+SQLITE_PRIVATE void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
64893
+ va_list ap;
64894
+ sqlite3DbFree(p->db, p->zErrMsg);
64895
+ va_start(ap, zFormat);
64896
+ p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
64897
+ va_end(ap);
64898
+}
6482264899
6482364900
/*
6482464901
** Remember the SQL string for a prepared statement.
6482564902
*/
6482664903
SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
@@ -66174,11 +66251,11 @@
6617466251
p->rc = SQLITE_OK;
6617566252
rc = SQLITE_DONE;
6617666253
}else if( db->u1.isInterrupted ){
6617766254
p->rc = SQLITE_INTERRUPT;
6617866255
rc = SQLITE_ERROR;
66179
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
66256
+ sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
6618066257
}else{
6618166258
char *zP4;
6618266259
Op *pOp;
6618366260
if( i<p->nOp ){
6618466261
/* The output line number is small enough that we are still in the
@@ -67077,11 +67154,11 @@
6707767154
if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
6707867155
|| (!deferred && p->nFkConstraint>0)
6707967156
){
6708067157
p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
6708167158
p->errorAction = OE_Abort;
67082
- sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed");
67159
+ sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
6708367160
return SQLITE_ERROR;
6708467161
}
6708567162
return SQLITE_OK;
6708667163
}
6708767164
#endif
@@ -68420,11 +68497,11 @@
6842068497
6842168498
/* RHS is an integer */
6842268499
if( pRhs->flags & MEM_Int ){
6842368500
serial_type = aKey1[idx1];
6842468501
testcase( serial_type==12 );
68425
- if( serial_type>=12 ){
68502
+ if( serial_type>=10 ){
6842668503
rc = +1;
6842768504
}else if( serial_type==0 ){
6842868505
rc = -1;
6842968506
}else if( serial_type==7 ){
6843068507
double rhs = (double)pRhs->u.i;
@@ -68446,11 +68523,15 @@
6844668523
}
6844768524
6844868525
/* RHS is real */
6844968526
else if( pRhs->flags & MEM_Real ){
6845068527
serial_type = aKey1[idx1];
68451
- if( serial_type>=12 ){
68528
+ if( serial_type>=10 ){
68529
+ /* Serial types 12 or greater are strings and blobs (greater than
68530
+ ** numbers). Types 10 and 11 are currently "reserved for future
68531
+ ** use", so it doesn't really matter what the results of comparing
68532
+ ** them to numberic values are. */
6845268533
rc = +1;
6845368534
}else if( serial_type==0 ){
6845468535
rc = -1;
6845568536
}else{
6845668537
double rhs = pRhs->u.r;
@@ -69184,10 +69265,40 @@
6918469265
SQLITE_INTEGER, /* 0x1e */
6918569266
SQLITE_NULL, /* 0x1f */
6918669267
};
6918769268
return aType[pVal->flags&MEM_AffMask];
6918869269
}
69270
+
69271
+/* Make a copy of an sqlite3_value object
69272
+*/
69273
+SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value *pOrig){
69274
+ sqlite3_value *pNew;
69275
+ if( pOrig==0 ) return 0;
69276
+ pNew = sqlite3_malloc( sizeof(*pNew) );
69277
+ if( pNew==0 ) return 0;
69278
+ memset(pNew, 0, sizeof(*pNew));
69279
+ memcpy(pNew, pOrig, MEMCELLSIZE);
69280
+ pNew->flags &= ~MEM_Dyn;
69281
+ pNew->db = 0;
69282
+ if( pNew->flags&(MEM_Str|MEM_Blob) ){
69283
+ pNew->flags &= ~(MEM_Static|MEM_Dyn);
69284
+ pNew->flags |= MEM_Ephem;
69285
+ if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
69286
+ sqlite3ValueFree(pNew);
69287
+ pNew = 0;
69288
+ }
69289
+ }
69290
+ return pNew;
69291
+}
69292
+
69293
+/* Destroy an sqlite3_value object previously obtained from
69294
+** sqlite3_value_dup().
69295
+*/
69296
+SQLITE_API void SQLITE_STDCALL sqlite3_value_free(sqlite3_value *pOld){
69297
+ sqlite3ValueFree(pOld);
69298
+}
69299
+
6918969300
6919069301
/**************************** sqlite3_result_ *******************************
6919169302
** The following routines are used by user-defined functions to specify
6919269303
** the function result.
6919369304
**
@@ -71798,16 +71909,15 @@
7179871909
zType = 0;
7179971910
}
7180071911
assert( zType!=0 || pOp->p4.z!=0 );
7180171912
zLogFmt = "abort at %d in [%s]: %s";
7180271913
if( zType && pOp->p4.z ){
71803
- sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s",
71804
- zType, pOp->p4.z);
71914
+ sqlite3VdbeError(p, "%s constraint failed: %s", zType, pOp->p4.z);
7180571915
}else if( pOp->p4.z ){
71806
- sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
71916
+ sqlite3VdbeError(p, "%s", pOp->p4.z);
7180771917
}else{
71808
- sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType);
71918
+ sqlite3VdbeError(p, "%s constraint failed", zType);
7180971919
}
7181071920
sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg);
7181171921
}
7181271922
rc = sqlite3VdbeHalt(p);
7181371923
assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
@@ -72435,11 +72545,11 @@
7243572545
lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */
7243672546
7243772547
/* If the function returned an error, throw an exception */
7243872548
if( ctx.fErrorOrAux ){
7243972549
if( ctx.isError ){
72440
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut));
72550
+ sqlite3VdbeError(p, "%s", sqlite3_value_text(ctx.pOut));
7244172551
rc = ctx.isError;
7244272552
}
7244372553
sqlite3VdbeDeleteAuxData(p, (int)(pOp - aOp), pOp->p1);
7244472554
}
7244572555
@@ -73622,12 +73732,11 @@
7362273732
if( p1==SAVEPOINT_BEGIN ){
7362373733
if( db->nVdbeWrite>0 ){
7362473734
/* A new savepoint cannot be created if there are active write
7362573735
** statements (i.e. open read/write incremental blob handles).
7362673736
*/
73627
- sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
73628
- "SQL statements in progress");
73737
+ sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
7362973738
rc = SQLITE_BUSY;
7363073739
}else{
7363173740
nName = sqlite3Strlen30(zName);
7363273741
7363373742
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -73674,19 +73783,18 @@
7367473783
pSavepoint = pSavepoint->pNext
7367573784
){
7367673785
iSavepoint++;
7367773786
}
7367873787
if( !pSavepoint ){
73679
- sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
73788
+ sqlite3VdbeError(p, "no such savepoint: %s", zName);
7368073789
rc = SQLITE_ERROR;
7368173790
}else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
7368273791
/* It is not possible to release (commit) a savepoint if there are
7368373792
** active write statements.
7368473793
*/
73685
- sqlite3SetString(&p->zErrMsg, db,
73686
- "cannot release savepoint - SQL statements in progress"
73687
- );
73794
+ sqlite3VdbeError(p, "cannot release savepoint - "
73795
+ "SQL statements in progress");
7368873796
rc = SQLITE_BUSY;
7368973797
}else{
7369073798
7369173799
/* Determine whether or not this is a transaction savepoint. If so,
7369273800
** and this is a RELEASE command, then the current transaction
@@ -73788,27 +73896,16 @@
7378873896
assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
7378973897
assert( desiredAutoCommit==1 || iRollback==0 );
7379073898
assert( db->nVdbeActive>0 ); /* At least this one VM is active */
7379173899
assert( p->bIsReader );
7379273900
73793
-#if 0
73794
- if( turnOnAC && iRollback && db->nVdbeActive>1 ){
73795
- /* If this instruction implements a ROLLBACK and other VMs are
73796
- ** still running, and a transaction is active, return an error indicating
73797
- ** that the other VMs must complete first.
73798
- */
73799
- sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
73800
- "SQL statements in progress");
73801
- rc = SQLITE_BUSY;
73802
- }else
73803
-#endif
7380473901
if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
7380573902
/* If this instruction implements a COMMIT and other VMs are writing
7380673903
** return an error indicating that the other VMs must complete first.
7380773904
*/
73808
- sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
73809
- "SQL statements in progress");
73905
+ sqlite3VdbeError(p, "cannot commit transaction - "
73906
+ "SQL statements in progress");
7381073907
rc = SQLITE_BUSY;
7381173908
}else if( desiredAutoCommit!=db->autoCommit ){
7381273909
if( iRollback ){
7381373910
assert( desiredAutoCommit==1 );
7381473911
sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
@@ -73831,11 +73928,11 @@
7383173928
}else{
7383273929
rc = SQLITE_ERROR;
7383373930
}
7383473931
goto vdbe_return;
7383573932
}else{
73836
- sqlite3SetString(&p->zErrMsg, db,
73933
+ sqlite3VdbeError(p,
7383773934
(!desiredAutoCommit)?"cannot start a transaction within a transaction":(
7383873935
(iRollback)?"cannot rollback - no transaction is active":
7383973936
"cannot commit - no transaction is active"));
7384073937
7384173938
rc = SQLITE_ERROR;
@@ -76264,11 +76361,11 @@
7626476361
if( pFrame ) break;
7626576362
}
7626676363
7626776364
if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
7626876365
rc = SQLITE_ERROR;
76269
- sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
76366
+ sqlite3VdbeError(p, "too many levels of trigger recursion");
7627076367
break;
7627176368
}
7627276369
7627376370
/* Register pRt is used to store the memory required to save the state
7627476371
** of the current program, and the memory required at runtime to execute
@@ -76567,11 +76664,11 @@
7656776664
ctx.pVdbe = p;
7656876665
ctx.iOp = (int)(pOp - aOp);
7656976666
ctx.skipFlag = 0;
7657076667
(ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
7657176668
if( ctx.isError ){
76572
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t));
76669
+ sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
7657376670
rc = ctx.isError;
7657476671
}
7657576672
if( ctx.skipFlag ){
7657676673
assert( pOp[-1].opcode==OP_CollSeq );
7657776674
i = pOp[-1].p1;
@@ -76599,11 +76696,11 @@
7659976696
assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
7660076697
pMem = &aMem[pOp->p1];
7660176698
assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
7660276699
rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
7660376700
if( rc ){
76604
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
76701
+ sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
7660576702
}
7660676703
sqlite3VdbeChangeEncoding(pMem, encoding);
7660776704
UPDATE_MAX_BLOBSIZE(pMem);
7660876705
if( sqlite3VdbeMemTooBig(pMem) ){
7660976706
goto too_big;
@@ -76704,11 +76801,11 @@
7670476801
if( (eNew!=eOld)
7670576802
&& (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
7670676803
){
7670776804
if( !db->autoCommit || db->nVdbeRead>1 ){
7670876805
rc = SQLITE_ERROR;
76709
- sqlite3SetString(&p->zErrMsg, db,
76806
+ sqlite3VdbeError(p,
7671076807
"cannot change %s wal mode from within a transaction",
7671176808
(eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
7671276809
);
7671376810
break;
7671476811
}else{
@@ -76835,11 +76932,11 @@
7683576932
assert( DbMaskTest(p->btreeMask, p1) );
7683676933
assert( isWriteLock==0 || isWriteLock==1 );
7683776934
rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
7683876935
if( (rc&0xFF)==SQLITE_LOCKED ){
7683976936
const char *z = pOp->p4.z;
76840
- sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
76937
+ sqlite3VdbeError(p, "database table is locked: %s", z);
7684176938
}
7684276939
}
7684376940
break;
7684476941
}
7684576942
#endif /* SQLITE_OMIT_SHARED_CACHE */
@@ -77383,19 +77480,19 @@
7738377480
7738477481
/* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
7738577482
** is encountered.
7738677483
*/
7738777484
too_big:
77388
- sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
77485
+ sqlite3VdbeError(p, "string or blob too big");
7738977486
rc = SQLITE_TOOBIG;
7739077487
goto vdbe_error_halt;
7739177488
7739277489
/* Jump to here if a malloc() fails.
7739377490
*/
7739477491
no_mem:
7739577492
db->mallocFailed = 1;
77396
- sqlite3SetString(&p->zErrMsg, db, "out of memory");
77493
+ sqlite3VdbeError(p, "out of memory");
7739777494
rc = SQLITE_NOMEM;
7739877495
goto vdbe_error_halt;
7739977496
7740077497
/* Jump to here for any other kind of fatal error. The "rc" variable
7740177498
** should hold the error number.
@@ -77402,11 +77499,11 @@
7740277499
*/
7740377500
abort_due_to_error:
7740477501
assert( p->zErrMsg==0 );
7740577502
if( db->mallocFailed ) rc = SQLITE_NOMEM;
7740677503
if( rc!=SQLITE_IOERR_NOMEM ){
77407
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
77504
+ sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
7740877505
}
7740977506
goto vdbe_error_halt;
7741077507
7741177508
/* Jump to here if the sqlite3_interrupt() API sets the interrupt
7741277509
** flag.
@@ -77413,11 +77510,11 @@
7741377510
*/
7741477511
abort_due_to_interrupt:
7741577512
assert( db->u1.isInterrupted );
7741677513
rc = SQLITE_INTERRUPT;
7741777514
p->rc = rc;
77418
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
77515
+ sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
7741977516
goto vdbe_error_halt;
7742077517
}
7742177518
7742277519
7742377520
/************** End of vdbe.c ************************************************/
@@ -81652,11 +81749,11 @@
8165281749
iCol = -1;
8165381750
}
8165481751
break;
8165581752
}
8165681753
}
81657
- if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
81754
+ if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
8165881755
/* IMP: R-51414-32910 */
8165981756
/* IMP: R-44911-55124 */
8166081757
iCol = -1;
8166181758
}
8166281759
if( iCol<pTab->nCol ){
@@ -81682,11 +81779,11 @@
8168281779
8168381780
/*
8168481781
** Perhaps the name is a reference to the ROWID
8168581782
*/
8168681783
if( cnt==0 && cntTab==1 && pMatch && sqlite3IsRowid(zCol)
81687
- && HasRowid(pMatch->pTab) ){
81784
+ && VisibleRowid(pMatch->pTab) ){
8168881785
cnt = 1;
8168981786
pExpr->iColumn = -1; /* IMP: R-44911-55124 */
8169081787
pExpr->affinity = SQLITE_AFF_INTEGER;
8169181788
}
8169281789
@@ -92126,18 +92223,15 @@
9212692223
#ifndef SQLITE_OMIT_AUTOINCREMENT
9212792224
sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
9212892225
"INTEGER PRIMARY KEY");
9212992226
#endif
9213092227
}else{
92131
- Vdbe *v = pParse->pVdbe;
9213292228
Index *p;
92133
- if( v ) pParse->addrSkipPK = sqlite3VdbeAddOp0(v, OP_Noop);
9213492229
p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
9213592230
0, sortOrder, 0);
9213692231
if( p ){
9213792232
p->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
92138
- if( v ) sqlite3VdbeJumpHere(v, pParse->addrSkipPK);
9213992233
}
9214092234
pList = 0;
9214192235
}
9214292236
9214392237
primary_key_exit:
@@ -92486,18 +92580,10 @@
9248692580
if( pParse->addrCrTab ){
9248792581
assert( v );
9248892582
sqlite3VdbeGetOp(v, pParse->addrCrTab)->opcode = OP_CreateIndex;
9248992583
}
9249092584
92491
- /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
92492
- ** table entry.
92493
- */
92494
- if( pParse->addrSkipPK ){
92495
- assert( v );
92496
- sqlite3VdbeGetOp(v, pParse->addrSkipPK)->opcode = OP_Goto;
92497
- }
92498
-
9249992585
/* Locate the PRIMARY KEY index. Or, if this table was originally
9250092586
** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
9250192587
*/
9250292588
if( pTab->iPKey>=0 ){
9250392589
ExprList *pList;
@@ -92511,10 +92597,20 @@
9251192597
if( pPk==0 ) return;
9251292598
pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
9251392599
pTab->iPKey = -1;
9251492600
}else{
9251592601
pPk = sqlite3PrimaryKeyIndex(pTab);
92602
+
92603
+ /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
92604
+ ** table entry. This is only required if currently generating VDBE
92605
+ ** code for a CREATE TABLE (not when parsing one as part of reading
92606
+ ** a database schema). */
92607
+ if( v ){
92608
+ assert( db->init.busy==0 );
92609
+ sqlite3VdbeGetOp(v, pPk->tnum)->opcode = OP_Goto;
92610
+ }
92611
+
9251692612
/*
9251792613
** Remove all redundant columns from the PRIMARY KEY. For example, change
9251892614
** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
9251992615
** code assumes the PRIMARY KEY contains no repeated columns.
9252092616
*/
@@ -92646,11 +92742,11 @@
9264692742
return;
9264792743
}
9264892744
if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
9264992745
sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
9265092746
}else{
92651
- p->tabFlags |= TF_WithoutRowid;
92747
+ p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
9265292748
convertToWithoutRowidTable(pParse, p);
9265392749
}
9265492750
}
9265592751
9265692752
iDb = sqlite3SchemaToIndex(db, p->pSchema);
@@ -92714,30 +92810,49 @@
9271492810
** as a schema-lock must have already been obtained to create it. Since
9271592811
** a schema-lock excludes all other database users, the write-lock would
9271692812
** be redundant.
9271792813
*/
9271892814
if( pSelect ){
92719
- SelectDest dest;
92720
- Table *pSelTab;
92815
+ SelectDest dest; /* Where the SELECT should store results */
92816
+ int regYield; /* Register holding co-routine entry-point */
92817
+ int addrTop; /* Top of the co-routine */
92818
+ int regRec; /* A record to be insert into the new table */
92819
+ int regRowid; /* Rowid of the next row to insert */
92820
+ int addrInsLoop; /* Top of the loop for inserting rows */
92821
+ Table *pSelTab; /* A table that describes the SELECT results */
9272192822
92823
+ regYield = ++pParse->nMem;
92824
+ regRec = ++pParse->nMem;
92825
+ regRowid = ++pParse->nMem;
9272292826
assert(pParse->nTab==1);
9272392827
sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
9272492828
sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
9272592829
pParse->nTab = 2;
92726
- sqlite3SelectDestInit(&dest, SRT_Table, 1);
92830
+ addrTop = sqlite3VdbeCurrentAddr(v) + 1;
92831
+ sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
92832
+ sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
9272792833
sqlite3Select(pParse, pSelect, &dest);
92834
+ sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
92835
+ sqlite3VdbeJumpHere(v, addrTop - 1);
92836
+ if( pParse->nErr ) return;
92837
+ pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
92838
+ if( pSelTab==0 ) return;
92839
+ assert( p->aCol==0 );
92840
+ p->nCol = pSelTab->nCol;
92841
+ p->aCol = pSelTab->aCol;
92842
+ pSelTab->nCol = 0;
92843
+ pSelTab->aCol = 0;
92844
+ sqlite3DeleteTable(db, pSelTab);
92845
+ addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
92846
+ VdbeCoverage(v);
92847
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
92848
+ sqlite3TableAffinity(v, p, 0);
92849
+ sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
92850
+ sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
92851
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, addrInsLoop);
92852
+ sqlite3VdbeJumpHere(v, addrInsLoop);
9272892853
sqlite3VdbeAddOp1(v, OP_Close, 1);
92729
- if( pParse->nErr==0 ){
92730
- pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
92731
- if( pSelTab==0 ) return;
92732
- assert( p->aCol==0 );
92733
- p->nCol = pSelTab->nCol;
92734
- p->aCol = pSelTab->aCol;
92735
- pSelTab->nCol = 0;
92736
- pSelTab->aCol = 0;
92737
- sqlite3DeleteTable(db, pSelTab);
92738
- }
9273992854
}
9274092855
9274192856
/* Compute the complete text of the CREATE statement */
9274292857
if( pSelect ){
9274392858
zStmt = createTableStmt(db, p);
@@ -94032,14 +94147,19 @@
9403294147
int iMem = ++pParse->nMem;
9403394148
9403494149
v = sqlite3GetVdbe(pParse);
9403594150
if( v==0 ) goto exit_create_index;
9403694151
94037
-
94038
- /* Create the rootpage for the index
94039
- */
9404094152
sqlite3BeginWriteOperation(pParse, 1, iDb);
94153
+
94154
+ /* Create the rootpage for the index using CreateIndex. But before
94155
+ ** doing so, code a Noop instruction and store its address in
94156
+ ** Index.tnum. This is required in case this index is actually a
94157
+ ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
94158
+ ** that case the convertToWithoutRowidTable() routine will replace
94159
+ ** the Noop with a Goto to jump over the VDBE code generated below. */
94160
+ pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
9404194161
sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
9404294162
9404394163
/* Gather the complete text of the CREATE INDEX statement into
9404494164
** the zStmt variable
9404594165
*/
@@ -94075,10 +94195,12 @@
9407594195
sqlite3ChangeCookie(pParse, iDb);
9407694196
sqlite3VdbeAddParseSchemaOp(v, iDb,
9407794197
sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
9407894198
sqlite3VdbeAddOp1(v, OP_Expire, 0);
9407994199
}
94200
+
94201
+ sqlite3VdbeJumpHere(v, pIndex->tnum);
9408094202
}
9408194203
9408294204
/* When adding an index to the list of indices for a table, make
9408394205
** sure all indices labeled OE_Replace come after all those labeled
9408494206
** OE_Ignore. This is necessary for the correct constraint check
@@ -99670,11 +99792,11 @@
9967099792
sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
9967199793
VdbeComment((v, "%s", pTab->zName));
9967299794
}else{
9967399795
Index *pPk = sqlite3PrimaryKeyIndex(pTab);
9967499796
assert( pPk!=0 );
99675
- assert( pPk->tnum=pTab->tnum );
99797
+ assert( pPk->tnum==pTab->tnum );
9967699798
sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
9967799799
sqlite3VdbeSetP4KeyInfo(pParse, pPk);
9967899800
VdbeComment((v, "%s", pTab->zName));
9967999801
}
9968099802
}
@@ -102120,10 +102242,12 @@
102120102242
void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
102121102243
void(*)(void*));
102122102244
void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
102123102245
void(*)(void*), unsigned char);
102124102246
int (*strglob)(const char*,const char*);
102247
+ sqlite3_value (*value_dup)(const sqlite3_value*);
102248
+ void (*value_free)(sqlite3_value*);
102125102249
};
102126102250
102127102251
/*
102128102252
** The following macros redefine the API routines so that they are
102129102253
** redirected through the global sqlite3_api structure.
@@ -102350,10 +102474,13 @@
102350102474
#define sqlite3_realloc64 sqlite3_api->realloc64
102351102475
#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
102352102476
#define sqlite3_result_blob64 sqlite3_api->result_blob64
102353102477
#define sqlite3_result_text64 sqlite3_api->result_text64
102354102478
#define sqlite3_strglob sqlite3_api->strglob
102479
+/* Version 3.8.11 and later */
102480
+#define sqlite3_value_dup sqlite3_api->value_dup
102481
+#define sqlite3_value_free sqlite3_api->value_free
102355102482
#endif /* SQLITE_CORE */
102356102483
102357102484
#ifndef SQLITE_CORE
102358102485
/* This case when the file really is being compiled as a loadable
102359102486
** extension */
@@ -103256,10 +103383,14 @@
103256103383
#endif
103257103384
{ /* zName: */ "case_sensitive_like",
103258103385
/* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE,
103259103386
/* ePragFlag: */ 0,
103260103387
/* iArg: */ 0 },
103388
+ { /* zName: */ "cell_size_check",
103389
+ /* ePragTyp: */ PragTyp_FLAG,
103390
+ /* ePragFlag: */ 0,
103391
+ /* iArg: */ SQLITE_CellSizeCk },
103261103392
#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
103262103393
{ /* zName: */ "checkpoint_fullfsync",
103263103394
/* ePragTyp: */ PragTyp_FLAG,
103264103395
/* ePragFlag: */ 0,
103265103396
/* iArg: */ SQLITE_CkptFullFSync },
@@ -103613,11 +103744,11 @@
103613103744
/* ePragTyp: */ PragTyp_FLAG,
103614103745
/* ePragFlag: */ 0,
103615103746
/* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
103616103747
#endif
103617103748
};
103618
-/* Number of pragmas: 59 on by default, 72 total. */
103749
+/* Number of pragmas: 60 on by default, 73 total. */
103619103750
103620103751
/************** End of pragma.h **********************************************/
103621103752
/************** Continuing where we left off in pragma.c *********************/
103622103753
103623103754
/*
@@ -105596,17 +105727,17 @@
105596105727
const char *zObj, /* Object being parsed at the point of error */
105597105728
const char *zExtra /* Error information */
105598105729
){
105599105730
sqlite3 *db = pData->db;
105600105731
if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
105732
+ char *z;
105601105733
if( zObj==0 ) zObj = "?";
105602
- sqlite3SetString(pData->pzErrMsg, db,
105603
- "malformed database schema (%s)", zObj);
105604
- if( zExtra ){
105605
- *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg,
105606
- "%s - %s", *pData->pzErrMsg, zExtra);
105607
- }
105734
+ z = sqlite3_mprintf("malformed database schema (%s)", zObj);
105735
+ if( z && zExtra ) z = sqlite3_mprintf("%z - %s", z, zExtra);
105736
+ sqlite3DbFree(db, *pData->pzErrMsg);
105737
+ *pData->pzErrMsg = z;
105738
+ if( z==0 ) db->mallocFailed = 1;
105608105739
}
105609105740
pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
105610105741
}
105611105742
105612105743
/*
@@ -105794,11 +105925,11 @@
105794105925
** will be closed before this function returns. */
105795105926
sqlite3BtreeEnter(pDb->pBt);
105796105927
if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
105797105928
rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
105798105929
if( rc!=SQLITE_OK ){
105799
- sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
105930
+ sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
105800105931
goto initone_error_out;
105801105932
}
105802105933
openedTransaction = 1;
105803105934
}
105804105935
@@ -107178,12 +107309,17 @@
107178107309
}
107179107310
}else if( eDest!=SRT_Exists ){
107180107311
/* If the destination is an EXISTS(...) expression, the actual
107181107312
** values returned by the SELECT are not required.
107182107313
*/
107183
- sqlite3ExprCodeExprList(pParse, pEList, regResult,
107184
- (eDest==SRT_Output||eDest==SRT_Coroutine)?SQLITE_ECEL_DUP:0);
107314
+ u8 ecelFlags;
107315
+ if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
107316
+ ecelFlags = SQLITE_ECEL_DUP;
107317
+ }else{
107318
+ ecelFlags = 0;
107319
+ }
107320
+ sqlite3ExprCodeExprList(pParse, pEList, regResult, ecelFlags);
107185107321
}
107186107322
107187107323
/* If the DISTINCT keyword was present on the SELECT statement
107188107324
** and this row has been seen before, then do not make this row
107189107325
** part of the result.
@@ -107276,10 +107412,12 @@
107276107412
case SRT_Table:
107277107413
case SRT_EphemTab: {
107278107414
int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
107279107415
testcase( eDest==SRT_Table );
107280107416
testcase( eDest==SRT_EphemTab );
107417
+ testcase( eDest==SRT_Fifo );
107418
+ testcase( eDest==SRT_DistFifo );
107281107419
sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
107282107420
#ifndef SQLITE_OMIT_CTE
107283107421
if( eDest==SRT_DistFifo ){
107284107422
/* If the destination is DistFifo, then cursor (iParm+1) is open
107285107423
** on an ephemeral index. If the current row is already present
@@ -107691,14 +107829,11 @@
107691107829
for(i=0; i<nSortData; i++){
107692107830
sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq+i, regRow+i);
107693107831
VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
107694107832
}
107695107833
switch( eDest ){
107696
- case SRT_Table:
107697107834
case SRT_EphemTab: {
107698
- testcase( eDest==SRT_Table );
107699
- testcase( eDest==SRT_EphemTab );
107700107835
sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
107701107836
sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
107702107837
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
107703107838
break;
107704107839
}
@@ -109043,19 +109178,18 @@
109043109178
109044109179
/* Suppress the first OFFSET entries if there is an OFFSET clause
109045109180
*/
109046109181
codeOffset(v, p->iOffset, iContinue);
109047109182
109183
+ assert( pDest->eDest!=SRT_Exists );
109184
+ assert( pDest->eDest!=SRT_Table );
109048109185
switch( pDest->eDest ){
109049109186
/* Store the result as data using a unique key.
109050109187
*/
109051
- case SRT_Table:
109052109188
case SRT_EphemTab: {
109053109189
int r1 = sqlite3GetTempReg(pParse);
109054109190
int r2 = sqlite3GetTempReg(pParse);
109055
- testcase( pDest->eDest==SRT_Table );
109056
- testcase( pDest->eDest==SRT_EphemTab );
109057109191
sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
109058109192
sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
109059109193
sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
109060109194
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
109061109195
sqlite3ReleaseTempReg(pParse, r2);
@@ -109079,20 +109213,10 @@
109079109213
sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
109080109214
sqlite3ReleaseTempReg(pParse, r1);
109081109215
break;
109082109216
}
109083109217
109084
-#if 0 /* Never occurs on an ORDER BY query */
109085
- /* If any row exist in the result set, record that fact and abort.
109086
- */
109087
- case SRT_Exists: {
109088
- sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm);
109089
- /* The LIMIT clause will terminate the loop for us */
109090
- break;
109091
- }
109092
-#endif
109093
-
109094109218
/* If this is a scalar select that is part of an expression, then
109095109219
** store the results in the appropriate memory cell and break out
109096109220
** of the scan loop.
109097109221
*/
109098109222
case SRT_Mem: {
@@ -110463,11 +110587,11 @@
110463110587
if( pTab==0 ) return WRC_Abort;
110464110588
pTab->nRef = 1;
110465110589
pTab->zName = sqlite3DbStrDup(db, pCte->zName);
110466110590
pTab->iPKey = -1;
110467110591
pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
110468
- pTab->tabFlags |= TF_Ephemeral;
110592
+ pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
110469110593
pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
110470110594
if( db->mallocFailed ) return SQLITE_NOMEM;
110471110595
assert( pFrom->pSelect );
110472110596
110473110597
/* Check if this is a recursive CTE. */
@@ -110708,17 +110832,10 @@
110708110832
ExprList *pNew = 0;
110709110833
int flags = pParse->db->flags;
110710110834
int longNames = (flags & SQLITE_FullColNames)!=0
110711110835
&& (flags & SQLITE_ShortColNames)==0;
110712110836
110713
- /* When processing FROM-clause subqueries, it is always the case
110714
- ** that full_column_names=OFF and short_column_names=ON. The
110715
- ** sqlite3ResultSetOfSelect() routine makes it so. */
110716
- assert( (p->selFlags & SF_NestedFrom)==0
110717
- || ((flags & SQLITE_FullColNames)==0 &&
110718
- (flags & SQLITE_ShortColNames)!=0) );
110719
-
110720110837
for(k=0; k<pEList->nExpr; k++){
110721110838
pE = a[k].pExpr;
110722110839
pRight = pE->pRight;
110723110840
assert( pE->op!=TK_DOT || pRight!=0 );
110724110841
if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pRight->op!=TK_ALL) ){
@@ -111296,10 +111413,11 @@
111296111413
isAgg = 1;
111297111414
p->selFlags |= SF_Aggregate;
111298111415
}
111299111416
i = -1;
111300111417
}else if( pTabList->nSrc==1
111418
+ && (p->selFlags & SF_All)==0
111301111419
&& OptimizationEnabled(db, SQLITE_SubqCoroutine)
111302111420
){
111303111421
/* Implement a co-routine that will return a single row of the result
111304111422
** set on each invocation.
111305111423
*/
@@ -111983,13 +112101,13 @@
111983112101
** Generate a human-readable description of a the Select object.
111984112102
*/
111985112103
SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
111986112104
int n = 0;
111987112105
pView = sqlite3TreeViewPush(pView, moreToFollow);
111988
- sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p)",
112106
+ sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
111989112107
((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
111990
- ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p
112108
+ ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
111991112109
);
111992112110
if( p->pSrc && p->pSrc->nSrc ) n++;
111993112111
if( p->pWhere ) n++;
111994112112
if( p->pGroupBy ) n++;
111995112113
if( p->pHaving ) n++;
@@ -114139,16 +114257,14 @@
114139114257
/* Create the ephemeral table into which the update results will
114140114258
** be stored.
114141114259
*/
114142114260
assert( v );
114143114261
ephemTab = pParse->nTab++;
114144
- sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
114145
- sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
114146114262
114147114263
/* fill the ephemeral table
114148114264
*/
114149
- sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
114265
+ sqlite3SelectDestInit(&dest, SRT_EphemTab, ephemTab);
114150114266
sqlite3Select(pParse, pSelect, &dest);
114151114267
114152114268
/* Generate code to scan the ephemeral table and call VUpdate. */
114153114269
iReg = ++pParse->nMem;
114154114270
pParse->nMem += pTab->nCol+1;
@@ -115999,10 +116115,11 @@
115999116115
# define TERM_VNULL 0x00 /* Disabled if not using stat3 */
116000116116
#endif
116001116117
#define TERM_LIKEOPT 0x100 /* Virtual terms from the LIKE optimization */
116002116118
#define TERM_LIKECOND 0x200 /* Conditionally this LIKE operator term */
116003116119
#define TERM_LIKE 0x400 /* The original LIKE operator */
116120
+#define TERM_IS 0x800 /* Term.pExpr is an IS operator */
116004116121
116005116122
/*
116006116123
** An instance of the WhereScan object is used as an iterator for locating
116007116124
** terms in the WHERE clause that are useful to the query planner.
116008116125
*/
@@ -116147,25 +116264,26 @@
116147116264
** Bitmasks for the operators on WhereTerm objects. These are all
116148116265
** operators that are of interest to the query planner. An
116149116266
** OR-ed combination of these values can be used when searching for
116150116267
** particular WhereTerms within a WhereClause.
116151116268
*/
116152
-#define WO_IN 0x001
116153
-#define WO_EQ 0x002
116269
+#define WO_IN 0x0001
116270
+#define WO_EQ 0x0002
116154116271
#define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
116155116272
#define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
116156116273
#define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
116157116274
#define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
116158
-#define WO_MATCH 0x040
116159
-#define WO_ISNULL 0x080
116160
-#define WO_OR 0x100 /* Two or more OR-connected terms */
116161
-#define WO_AND 0x200 /* Two or more AND-connected terms */
116162
-#define WO_EQUIV 0x400 /* Of the form A==B, both columns */
116163
-#define WO_NOOP 0x800 /* This term does not restrict search space */
116164
-
116165
-#define WO_ALL 0xfff /* Mask of all possible WO_* values */
116166
-#define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */
116275
+#define WO_MATCH 0x0040
116276
+#define WO_IS 0x0080
116277
+#define WO_ISNULL 0x0100
116278
+#define WO_OR 0x0200 /* Two or more OR-connected terms */
116279
+#define WO_AND 0x0400 /* Two or more AND-connected terms */
116280
+#define WO_EQUIV 0x0800 /* Of the form A==B, both columns */
116281
+#define WO_NOOP 0x1000 /* This term does not restrict search space */
116282
+
116283
+#define WO_ALL 0x1fff /* Mask of all possible WO_* values */
116284
+#define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
116167116285
116168116286
/*
116169116287
** These are definitions of bits in the WhereLoop.wsFlags field.
116170116288
** The particular combination of bits in each WhereLoop help to
116171116289
** determine the algorithm that WhereLoop represents.
@@ -116535,11 +116653,11 @@
116535116653
static int allowedOp(int op){
116536116654
assert( TK_GT>TK_EQ && TK_GT<TK_GE );
116537116655
assert( TK_LT>TK_EQ && TK_LT<TK_GE );
116538116656
assert( TK_LE>TK_EQ && TK_LE<TK_GE );
116539116657
assert( TK_GE==TK_EQ+4 );
116540
- return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
116658
+ return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
116541116659
}
116542116660
116543116661
/*
116544116662
** Commute a comparison operator. Expressions of the form "X op Y"
116545116663
** are converted into "Y op X".
@@ -116588,10 +116706,12 @@
116588116706
assert( allowedOp(op) );
116589116707
if( op==TK_IN ){
116590116708
c = WO_IN;
116591116709
}else if( op==TK_ISNULL ){
116592116710
c = WO_ISNULL;
116711
+ }else if( op==TK_IS ){
116712
+ c = WO_IS;
116593116713
}else{
116594116714
assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
116595116715
c = (u16)(WO_EQ<<(op-TK_EQ));
116596116716
}
116597116717
assert( op!=TK_ISNULL || c==WO_ISNULL );
@@ -116599,10 +116719,11 @@
116599116719
assert( op!=TK_EQ || c==WO_EQ );
116600116720
assert( op!=TK_LT || c==WO_LT );
116601116721
assert( op!=TK_LE || c==WO_LE );
116602116722
assert( op!=TK_GT || c==WO_GT );
116603116723
assert( op!=TK_GE || c==WO_GE );
116724
+ assert( op!=TK_IS || c==WO_IS );
116604116725
return c;
116605116726
}
116606116727
116607116728
/*
116608116729
** Advance to the next WhereTerm that matches according to the criteria
@@ -116659,15 +116780,16 @@
116659116780
if( pColl==0 ) pColl = pParse->db->pDfltColl;
116660116781
if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
116661116782
continue;
116662116783
}
116663116784
}
116664
- if( (pTerm->eOperator & WO_EQ)!=0
116785
+ if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
116665116786
&& (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
116666116787
&& pX->iTable==pScan->aEquiv[0]
116667116788
&& pX->iColumn==pScan->aEquiv[1]
116668116789
){
116790
+ testcase( pTerm->eOperator & WO_IS );
116669116791
continue;
116670116792
}
116671116793
pScan->k = k+1;
116672116794
return pTerm;
116673116795
}
@@ -116765,13 +116887,15 @@
116765116887
WhereTerm *pResult = 0;
116766116888
WhereTerm *p;
116767116889
WhereScan scan;
116768116890
116769116891
p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
116892
+ op &= WO_EQ|WO_IS;
116770116893
while( p ){
116771116894
if( (p->prereqRight & notReady)==0 ){
116772
- if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){
116895
+ if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
116896
+ testcase( p->eOperator & WO_IS );
116773116897
return p;
116774116898
}
116775116899
if( pResult==0 ) pResult = p;
116776116900
}
116777116901
p = whereScanNext(&scan);
@@ -116802,11 +116926,11 @@
116802116926
** so and false if not.
116803116927
**
116804116928
** In order for the operator to be optimizible, the RHS must be a string
116805116929
** literal that does not begin with a wildcard. The LHS must be a column
116806116930
** that may only be NULL, a string, or a BLOB, never a number. (This means
116807
-** that virtual tables cannot participate in the LIKE optimization.) If the
116931
+** that virtual tables cannot participate in the LIKE optimization.) The
116808116932
** collating sequence for the column on the LHS must be appropriate for
116809116933
** the operator.
116810116934
*/
116811116935
static int isLikeOrGlob(
116812116936
Parse *pParse, /* Parsing and code generating context */
@@ -117347,10 +117471,50 @@
117347117471
pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */
117348117472
}
117349117473
}
117350117474
}
117351117475
#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
117476
+
117477
+/*
117478
+** We already know that pExpr is a binary operator where both operands are
117479
+** column references. This routine checks to see if pExpr is an equivalence
117480
+** relation:
117481
+** 1. The SQLITE_Transitive optimization must be enabled
117482
+** 2. Must be either an == or an IS operator
117483
+** 3. Not originating the ON clause of an OUTER JOIN
117484
+** 4. The affinities of A and B must be compatible
117485
+** 5a. Both operands use the same collating sequence OR
117486
+** 5b. The overall collating sequence is BINARY
117487
+** If this routine returns TRUE, that means that the RHS can be substituted
117488
+** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
117489
+** This is an optimization. No harm comes from returning 0. But if 1 is
117490
+** returned when it should not be, then incorrect answers might result.
117491
+*/
117492
+static int termIsEquivalence(Parse *pParse, Expr *pExpr){
117493
+ char aff1, aff2;
117494
+ CollSeq *pColl;
117495
+ const char *zColl1, *zColl2;
117496
+ if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
117497
+ if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
117498
+ if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
117499
+ aff1 = sqlite3ExprAffinity(pExpr->pLeft);
117500
+ aff2 = sqlite3ExprAffinity(pExpr->pRight);
117501
+ if( aff1!=aff2
117502
+ && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
117503
+ ){
117504
+ return 0;
117505
+ }
117506
+ pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
117507
+ if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1;
117508
+ pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
117509
+ /* Since pLeft and pRight are both a column references, their collating
117510
+ ** sequence should always be defined. */
117511
+ zColl1 = ALWAYS(pColl) ? pColl->zName : 0;
117512
+ pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
117513
+ zColl2 = ALWAYS(pColl) ? pColl->zName : 0;
117514
+ return sqlite3StrICmp(zColl1, zColl2)==0;
117515
+}
117352117516
117353117517
/*
117354117518
** The input to this routine is an WhereTerm structure with only the
117355117519
** "pExpr" field filled in. The job of this routine is to analyze the
117356117520
** subexpression and populate all the other fields of the WhereTerm
@@ -117426,10 +117590,11 @@
117426117590
if( pLeft->op==TK_COLUMN ){
117427117591
pTerm->leftCursor = pLeft->iTable;
117428117592
pTerm->u.leftColumn = pLeft->iColumn;
117429117593
pTerm->eOperator = operatorMask(op) & opMask;
117430117594
}
117595
+ if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
117431117596
if( pRight && pRight->op==TK_COLUMN ){
117432117597
WhereTerm *pNew;
117433117598
Expr *pDup;
117434117599
u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
117435117600
if( pTerm->leftCursor>=0 ){
@@ -117441,16 +117606,15 @@
117441117606
}
117442117607
idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
117443117608
if( idxNew==0 ) return;
117444117609
pNew = &pWC->a[idxNew];
117445117610
markTermAsChild(pWC, idxNew, idxTerm);
117611
+ if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
117446117612
pTerm = &pWC->a[idxTerm];
117447117613
pTerm->wtFlags |= TERM_COPIED;
117448
- if( pExpr->op==TK_EQ
117449
- && !ExprHasProperty(pExpr, EP_FromJoin)
117450
- && OptimizationEnabled(db, SQLITE_Transitive)
117451
- ){
117614
+
117615
+ if( termIsEquivalence(pParse, pDup) ){
117452117616
pTerm->eOperator |= WO_EQUIV;
117453117617
eExtraOp = WO_EQUIV;
117454117618
}
117455117619
}else{
117456117620
pDup = pExpr;
@@ -117640,14 +117804,11 @@
117640117804
/* When sqlite_stat3 histogram data is available an operator of the
117641117805
** form "x IS NOT NULL" can sometimes be evaluated more efficiently
117642117806
** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
117643117807
** virtual term of that form.
117644117808
**
117645
- ** Note that the virtual term must be tagged with TERM_VNULL. This
117646
- ** TERM_VNULL tag will suppress the not-null check at the beginning
117647
- ** of the loop. Without the TERM_VNULL flag, the not-null check at
117648
- ** the start of the loop will prevent any results from being returned.
117809
+ ** Note that the virtual term must be tagged with TERM_VNULL.
117649117810
*/
117650117811
if( pExpr->op==TK_NOTNULL
117651117812
&& pExpr->pLeft->op==TK_COLUMN
117652117813
&& pExpr->pLeft->iColumn>=0
117653117814
&& OptimizationEnabled(db, SQLITE_Stat34)
@@ -117788,10 +117949,40 @@
117788117949
** Estimate the logarithm of the input value to base 2.
117789117950
*/
117790117951
static LogEst estLog(LogEst N){
117791117952
return N<=10 ? 0 : sqlite3LogEst(N) - 33;
117792117953
}
117954
+
117955
+/*
117956
+** Convert OP_Column opcodes to OP_Copy in previously generated code.
117957
+**
117958
+** This routine runs over generated VDBE code and translates OP_Column
117959
+** opcodes into OP_Copy, and OP_Rowid into OP_Null, when the table is being
117960
+** accessed via co-routine instead of via table lookup.
117961
+*/
117962
+static void translateColumnToCopy(
117963
+ Vdbe *v, /* The VDBE containing code to translate */
117964
+ int iStart, /* Translate from this opcode to the end */
117965
+ int iTabCur, /* OP_Column/OP_Rowid references to this table */
117966
+ int iRegister /* The first column is in this register */
117967
+){
117968
+ VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
117969
+ int iEnd = sqlite3VdbeCurrentAddr(v);
117970
+ for(; iStart<iEnd; iStart++, pOp++){
117971
+ if( pOp->p1!=iTabCur ) continue;
117972
+ if( pOp->opcode==OP_Column ){
117973
+ pOp->opcode = OP_Copy;
117974
+ pOp->p1 = pOp->p2 + iRegister;
117975
+ pOp->p2 = pOp->p3;
117976
+ pOp->p3 = 0;
117977
+ }else if( pOp->opcode==OP_Rowid ){
117978
+ pOp->opcode = OP_Null;
117979
+ pOp->p1 = 0;
117980
+ pOp->p3 = 0;
117981
+ }
117982
+ }
117983
+}
117793117984
117794117985
/*
117795117986
** Two routines for printing the content of an sqlite3_index_info
117796117987
** structure. Used for testing and debugging only. If neither
117797117988
** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
@@ -117847,15 +118038,16 @@
117847118038
struct SrcList_item *pSrc, /* Table we are trying to access */
117848118039
Bitmask notReady /* Tables in outer loops of the join */
117849118040
){
117850118041
char aff;
117851118042
if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
117852
- if( (pTerm->eOperator & WO_EQ)==0 ) return 0;
118043
+ if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
117853118044
if( (pTerm->prereqRight & notReady)!=0 ) return 0;
117854118045
if( pTerm->u.leftColumn<0 ) return 0;
117855118046
aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
117856118047
if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
118048
+ testcase( pTerm->pExpr->op==TK_IS );
117857118049
return 1;
117858118050
}
117859118051
#endif
117860118052
117861118053
@@ -117890,10 +118082,11 @@
117890118082
Bitmask idxCols; /* Bitmap of columns used for indexing */
117891118083
Bitmask extraCols; /* Bitmap of additional columns */
117892118084
u8 sentWarning = 0; /* True if a warnning has been issued */
117893118085
Expr *pPartial = 0; /* Partial Index Expression */
117894118086
int iContinue = 0; /* Jump here to skip excluded rows */
118087
+ struct SrcList_item *pTabItem; /* FROM clause term being indexed */
117895118088
117896118089
/* Generate code to skip over the creation and initialization of the
117897118090
** transient index on 2nd and subsequent iterations of the loop. */
117898118091
v = pParse->pVdbe;
117899118092
assert( v!=0 );
@@ -118015,11 +118208,20 @@
118015118208
sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
118016118209
VdbeComment((v, "for %s", pTable->zName));
118017118210
118018118211
/* Fill the automatic index with content */
118019118212
sqlite3ExprCachePush(pParse);
118020
- addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
118213
+ pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
118214
+ if( pTabItem->viaCoroutine ){
118215
+ int regYield = pTabItem->regReturn;
118216
+ sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
118217
+ addrTop = sqlite3VdbeAddOp1(v, OP_Yield, regYield);
118218
+ VdbeCoverage(v);
118219
+ VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
118220
+ }else{
118221
+ addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
118222
+ }
118021118223
if( pPartial ){
118022118224
iContinue = sqlite3VdbeMakeLabel(v);
118023118225
sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL);
118024118226
pLoop->wsFlags |= WHERE_PARTIALIDX;
118025118227
}
@@ -118026,11 +118228,17 @@
118026118228
regRecord = sqlite3GetTempReg(pParse);
118027118229
sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0);
118028118230
sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
118029118231
sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
118030118232
if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
118031
- sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
118233
+ if( pTabItem->viaCoroutine ){
118234
+ translateColumnToCopy(v, addrTop, pLevel->iTabCur, pTabItem->regResult);
118235
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
118236
+ pTabItem->viaCoroutine = 0;
118237
+ }else{
118238
+ sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
118239
+ }
118032118240
sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
118033118241
sqlite3VdbeJumpHere(v, addrTop);
118034118242
sqlite3ReleaseTempReg(pParse, regRecord);
118035118243
sqlite3ExprCachePop(pParse);
118036118244
@@ -118068,12 +118276,13 @@
118068118276
for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
118069118277
if( pTerm->leftCursor != pSrc->iCursor ) continue;
118070118278
assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
118071118279
testcase( pTerm->eOperator & WO_IN );
118072118280
testcase( pTerm->eOperator & WO_ISNULL );
118281
+ testcase( pTerm->eOperator & WO_IS );
118073118282
testcase( pTerm->eOperator & WO_ALL );
118074
- if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
118283
+ if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
118075118284
if( pTerm->wtFlags & TERM_VNULL ) continue;
118076118285
nTerm++;
118077118286
}
118078118287
118079118288
/* If the ORDER BY clause contains only columns in the current
@@ -118120,13 +118329,14 @@
118120118329
for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
118121118330
u8 op;
118122118331
if( pTerm->leftCursor != pSrc->iCursor ) continue;
118123118332
assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
118124118333
testcase( pTerm->eOperator & WO_IN );
118334
+ testcase( pTerm->eOperator & WO_IS );
118125118335
testcase( pTerm->eOperator & WO_ISNULL );
118126118336
testcase( pTerm->eOperator & WO_ALL );
118127
- if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
118337
+ if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
118128118338
if( pTerm->wtFlags & TERM_VNULL ) continue;
118129118339
pIdxCons[j].iColumn = pTerm->u.leftColumn;
118130118340
pIdxCons[j].iTermOffset = i;
118131118341
op = (u8)pTerm->eOperator & WO_ALL;
118132118342
if( op==WO_IN ) op = WO_EQ;
@@ -118964,11 +119174,11 @@
118964119174
Expr *pX = pTerm->pExpr;
118965119175
Vdbe *v = pParse->pVdbe;
118966119176
int iReg; /* Register holding results */
118967119177
118968119178
assert( iTarget>0 );
118969
- if( pX->op==TK_EQ ){
119179
+ if( pX->op==TK_EQ || pX->op==TK_IS ){
118970119180
iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
118971119181
}else if( pX->op==TK_ISNULL ){
118972119182
iReg = iTarget;
118973119183
sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
118974119184
#ifndef SQLITE_OMIT_SUBQUERY
@@ -119149,11 +119359,11 @@
119149119359
}
119150119360
testcase( pTerm->eOperator & WO_ISNULL );
119151119361
testcase( pTerm->eOperator & WO_IN );
119152119362
if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
119153119363
Expr *pRight = pTerm->pExpr->pRight;
119154
- if( sqlite3ExprCanBeNull(pRight) ){
119364
+ if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
119155119365
sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
119156119366
VdbeCoverage(v);
119157119367
}
119158119368
if( zAff ){
119159119369
if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
@@ -120271,20 +120481,23 @@
120271120481
*/
120272120482
for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
120273120483
Expr *pE, *pEAlt;
120274120484
WhereTerm *pAlt;
120275120485
if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
120276
- if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue;
120486
+ if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
120487
+ if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
120277120488
if( pTerm->leftCursor!=iCur ) continue;
120278120489
if( pLevel->iLeftJoin ) continue;
120279120490
pE = pTerm->pExpr;
120280120491
assert( !ExprHasProperty(pE, EP_FromJoin) );
120281120492
assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
120282
- pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
120493
+ pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
120494
+ WO_EQ|WO_IN|WO_IS, 0);
120283120495
if( pAlt==0 ) continue;
120284120496
if( pAlt->wtFlags & (TERM_CODED) ) continue;
120285120497
testcase( pAlt->eOperator & WO_EQ );
120498
+ testcase( pAlt->eOperator & WO_IS );
120286120499
testcase( pAlt->eOperator & WO_IN );
120287120500
VdbeModuleComment((v, "begin transitive constraint"));
120288120501
pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
120289120502
if( pEAlt ){
120290120503
*pEAlt = *pAlt->pExpr;
@@ -120330,13 +120543,14 @@
120330120543
char zType[4];
120331120544
memcpy(zType, "...", 4);
120332120545
if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
120333120546
if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
120334120547
if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
120335
- sqlite3DebugPrintf("TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x\n",
120336
- iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb,
120337
- pTerm->eOperator);
120548
+ sqlite3DebugPrintf(
120549
+ "TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x wtFlags=0x%04x\n",
120550
+ iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb,
120551
+ pTerm->eOperator, pTerm->wtFlags);
120338120552
sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
120339120553
}
120340120554
}
120341120555
#endif
120342120556
@@ -120822,12 +121036,13 @@
120822121036
pLoop->nOut += pTerm->truthProb;
120823121037
}else{
120824121038
/* In the absence of explicit truth probabilities, use heuristics to
120825121039
** guess a reasonable truth probability. */
120826121040
pLoop->nOut--;
120827
- if( pTerm->eOperator&WO_EQ ){
121041
+ if( pTerm->eOperator&(WO_EQ|WO_IS) ){
120828121042
Expr *pRight = pTerm->pExpr->pRight;
121043
+ testcase( pTerm->pExpr->op==TK_IS );
120829121044
if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
120830121045
k = 10;
120831121046
}else{
120832121047
k = 20;
120833121048
}
@@ -120891,14 +121106,14 @@
120891121106
120892121107
assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
120893121108
assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
120894121109
if( pNew->wsFlags & WHERE_BTM_LIMIT ){
120895121110
opMask = WO_LT|WO_LE;
120896
- }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){
121111
+ }else if( /*pProbe->tnum<=0 ||*/ (pSrc->jointype & JT_LEFT)!=0 ){
120897121112
opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE;
120898121113
}else{
120899
- opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE;
121114
+ opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS;
120900121115
}
120901121116
if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
120902121117
120903121118
assert( pNew->u.btree.nEq<pProbe->nColumn );
120904121119
iCol = pProbe->aiColumn[pNew->u.btree.nEq];
@@ -120957,11 +121172,11 @@
120957121172
nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
120958121173
}
120959121174
assert( nIn>0 ); /* RHS always has 2 or more terms... The parser
120960121175
** changes "x IN (?)" into "x=?". */
120961121176
120962
- }else if( eOp & (WO_EQ) ){
121177
+ }else if( eOp & (WO_EQ|WO_IS) ){
120963121178
pNew->wsFlags |= WHERE_COLUMN_EQ;
120964121179
if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
120965121180
if( iCol>=0 && pProbe->uniqNotNull==0 ){
120966121181
pNew->wsFlags |= WHERE_UNQ_WANTED;
120967121182
}else{
@@ -121007,11 +121222,11 @@
121007121222
/* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4
121008121223
** data, using some other estimate. */
121009121224
whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
121010121225
}else{
121011121226
int nEq = ++pNew->u.btree.nEq;
121012
- assert( eOp & (WO_ISNULL|WO_EQ|WO_IN) );
121227
+ assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) );
121013121228
121014121229
assert( pNew->nOut==saved_nOut );
121015121230
if( pTerm->truthProb<=0 && iCol>=0 ){
121016121231
assert( (eOp & WO_IN) || nIn==0 );
121017121232
testcase( eOp & WO_IN );
@@ -121024,12 +121239,13 @@
121024121239
&& pProbe->nSample
121025121240
&& pNew->u.btree.nEq<=pProbe->nSampleCol
121026121241
&& ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
121027121242
){
121028121243
Expr *pExpr = pTerm->pExpr;
121029
- if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){
121244
+ if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){
121030121245
testcase( eOp & WO_EQ );
121246
+ testcase( eOp & WO_IS );
121031121247
testcase( eOp & WO_ISNULL );
121032121248
rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
121033121249
}else{
121034121250
rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
121035121251
}
@@ -121294,19 +121510,18 @@
121294121510
rSize = pTab->nRowLogEst;
121295121511
rLogSize = estLog(rSize);
121296121512
121297121513
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
121298121514
/* Automatic indexes */
121299
- if( !pBuilder->pOrSet
121515
+ if( !pBuilder->pOrSet /* Not part of an OR optimization */
121300121516
&& (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0
121301121517
&& (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
121302
- && pSrc->pIndex==0
121303
- && !pSrc->viaCoroutine
121304
- && !pSrc->notIndexed
121305
- && HasRowid(pTab)
121306
- && !pSrc->isCorrelated
121307
- && !pSrc->isRecursive
121518
+ && pSrc->pIndex==0 /* Has no INDEXED BY clause */
121519
+ && !pSrc->notIndexed /* Has no NOT INDEXED clause */
121520
+ && HasRowid(pTab) /* Is not a WITHOUT ROWID table. (FIXME: Why not?) */
121521
+ && !pSrc->isCorrelated /* Not a correlated subquery */
121522
+ && !pSrc->isRecursive /* Not a recursive common table expression. */
121308121523
){
121309121524
/* Generate auto-index WhereLoops */
121310121525
WhereTerm *pTerm;
121311121526
WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
121312121527
for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
@@ -121862,21 +122077,22 @@
121862122077
if( MASKBIT(i) & obSat ) continue;
121863122078
pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
121864122079
if( pOBExpr->op!=TK_COLUMN ) continue;
121865122080
if( pOBExpr->iTable!=iCur ) continue;
121866122081
pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
121867
- ~ready, WO_EQ|WO_ISNULL, 0);
122082
+ ~ready, WO_EQ|WO_ISNULL|WO_IS, 0);
121868122083
if( pTerm==0 ) continue;
121869
- if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){
122084
+ if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
121870122085
const char *z1, *z2;
121871122086
pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
121872122087
if( !pColl ) pColl = db->pDfltColl;
121873122088
z1 = pColl->zName;
121874122089
pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr);
121875122090
if( !pColl ) pColl = db->pDfltColl;
121876122091
z2 = pColl->zName;
121877122092
if( sqlite3StrICmp(z1, z2)!=0 ) continue;
122093
+ testcase( pTerm->pExpr->op==TK_IS );
121878122094
}
121879122095
obSat |= MASKBIT(i);
121880122096
}
121881122097
121882122098
if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
@@ -121903,11 +122119,11 @@
121903122119
u8 bOnce; /* True to run the ORDER BY search loop */
121904122120
121905122121
/* Skip over == and IS NULL terms */
121906122122
if( j<pLoop->u.btree.nEq
121907122123
&& pLoop->nSkip==0
121908
- && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0
122124
+ && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL|WO_IS))!=0
121909122125
){
121910122126
if( i & WO_ISNULL ){
121911122127
testcase( isOrderDistinct );
121912122128
isOrderDistinct = 0;
121913122129
}
@@ -122476,28 +122692,32 @@
122476122692
iCur = pItem->iCursor;
122477122693
pWC = &pWInfo->sWC;
122478122694
pLoop = pBuilder->pNew;
122479122695
pLoop->wsFlags = 0;
122480122696
pLoop->nSkip = 0;
122481
- pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0);
122697
+ pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
122482122698
if( pTerm ){
122699
+ testcase( pTerm->eOperator & WO_IS );
122483122700
pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
122484122701
pLoop->aLTerm[0] = pTerm;
122485122702
pLoop->nLTerm = 1;
122486122703
pLoop->u.btree.nEq = 1;
122487122704
/* TUNING: Cost of a rowid lookup is 10 */
122488122705
pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
122489122706
}else{
122490122707
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
122708
+ int opMask;
122491122709
assert( pLoop->aLTermSpace==pLoop->aLTerm );
122492122710
if( !IsUniqueIndex(pIdx)
122493122711
|| pIdx->pPartIdxWhere!=0
122494122712
|| pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
122495122713
) continue;
122714
+ opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
122496122715
for(j=0; j<pIdx->nKeyCol; j++){
122497
- pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
122716
+ pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx);
122498122717
if( pTerm==0 ) break;
122718
+ testcase( pTerm->eOperator & WO_IS );
122499122719
pLoop->aLTerm[j] = pTerm;
122500122720
}
122501122721
if( j!=pIdx->nKeyCol ) continue;
122502122722
pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
122503122723
if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
@@ -123132,30 +123352,16 @@
123132123352
Table *pTab = pTabItem->pTab;
123133123353
assert( pTab!=0 );
123134123354
pLoop = pLevel->pWLoop;
123135123355
123136123356
/* For a co-routine, change all OP_Column references to the table of
123137
- ** the co-routine into OP_SCopy of result contained in a register.
123357
+ ** the co-routine into OP_Copy of result contained in a register.
123138123358
** OP_Rowid becomes OP_Null.
123139123359
*/
123140123360
if( pTabItem->viaCoroutine && !db->mallocFailed ){
123141
- last = sqlite3VdbeCurrentAddr(v);
123142
- k = pLevel->addrBody;
123143
- pOp = sqlite3VdbeGetOp(v, k);
123144
- for(; k<last; k++, pOp++){
123145
- if( pOp->p1!=pLevel->iTabCur ) continue;
123146
- if( pOp->opcode==OP_Column ){
123147
- pOp->opcode = OP_Copy;
123148
- pOp->p1 = pOp->p2 + pTabItem->regResult;
123149
- pOp->p2 = pOp->p3;
123150
- pOp->p3 = 0;
123151
- }else if( pOp->opcode==OP_Rowid ){
123152
- pOp->opcode = OP_Null;
123153
- pOp->p1 = 0;
123154
- pOp->p3 = 0;
123155
- }
123156
- }
123361
+ translateColumnToCopy(v, pLevel->addrBody, pLevel->iTabCur,
123362
+ pTabItem->regResult);
123157123363
continue;
123158123364
}
123159123365
123160123366
/* Close all of the cursors that were opened by sqlite3WhereBegin.
123161123367
** Except, do not close cursors that will be reused by the OR optimization
@@ -125419,11 +125625,11 @@
125419125625
{yygotominor.yy186 = 0;}
125420125626
break;
125421125627
case 35: /* table_options ::= WITHOUT nm */
125422125628
{
125423125629
if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
125424
- yygotominor.yy186 = TF_WithoutRowid;
125630
+ yygotominor.yy186 = TF_WithoutRowid | TF_NoVisibleRowid;
125425125631
}else{
125426125632
yygotominor.yy186 = 0;
125427125633
sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
125428125634
}
125429125635
}
@@ -125643,10 +125849,11 @@
125643125849
{yygotominor.yy3 = yymsp[0].minor.yy3;}
125644125850
break;
125645125851
case 114: /* selectnowith ::= selectnowith multiselect_op oneselect */
125646125852
{
125647125853
Select *pRhs = yymsp[0].minor.yy3;
125854
+ Select *pLhs = yymsp[-2].minor.yy3;
125648125855
if( pRhs && pRhs->pPrior ){
125649125856
SrcList *pFrom;
125650125857
Token x;
125651125858
x.n = 0;
125652125859
parserDoubleLinkSelect(pParse, pRhs);
@@ -125653,15 +125860,16 @@
125653125860
pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
125654125861
pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0);
125655125862
}
125656125863
if( pRhs ){
125657125864
pRhs->op = (u8)yymsp[-1].minor.yy328;
125658
- pRhs->pPrior = yymsp[-2].minor.yy3;
125865
+ pRhs->pPrior = pLhs;
125866
+ if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
125659125867
pRhs->selFlags &= ~SF_MultiValue;
125660125868
if( yymsp[-1].minor.yy328!=TK_ALL ) pParse->hasCompound = 1;
125661125869
}else{
125662
- sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy3);
125870
+ sqlite3SelectDelete(pParse->db, pLhs);
125663125871
}
125664125872
yygotominor.yy3 = pRhs;
125665125873
}
125666125874
break;
125667125875
case 116: /* multiselect_op ::= UNION ALL */
@@ -125718,11 +125926,13 @@
125718125926
break;
125719125927
case 122: /* distinct ::= DISTINCT */
125720125928
{yygotominor.yy381 = SF_Distinct;}
125721125929
break;
125722125930
case 123: /* distinct ::= ALL */
125723
- case 124: /* distinct ::= */ yytestcase(yyruleno==124);
125931
+{yygotominor.yy381 = SF_All;}
125932
+ break;
125933
+ case 124: /* distinct ::= */
125724125934
{yygotominor.yy381 = 0;}
125725125935
break;
125726125936
case 125: /* sclp ::= selcollist COMMA */
125727125937
case 243: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==243);
125728125938
{yygotominor.yy14 = yymsp[-1].minor.yy14;}
@@ -126013,11 +126223,11 @@
126013126223
if( yymsp[-1].minor.yy14 && yymsp[-1].minor.yy14->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
126014126224
sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
126015126225
}
126016126226
yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy14, &yymsp[-4].minor.yy0);
126017126227
spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
126018
- if( yymsp[-2].minor.yy381 && yygotominor.yy346.pExpr ){
126228
+ if( yymsp[-2].minor.yy381==SF_Distinct && yygotominor.yy346.pExpr ){
126019126229
yygotominor.yy346.pExpr->flags |= EP_Distinct;
126020126230
}
126021126231
}
126022126232
break;
126023126233
case 196: /* expr ::= ID|INDEXED LP STAR RP */
@@ -127534,11 +127744,12 @@
127534127744
}
127535127745
}
127536127746
}
127537127747
abort_parse:
127538127748
assert( nErr==0 );
127539
- if( zSql[i]==0 && pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
127749
+ if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
127750
+ assert( zSql[i]==0 );
127540127751
if( lastTokenParsed!=TK_SEMI ){
127541127752
sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
127542127753
pParse->zTail = &zSql[i];
127543127754
}
127544127755
if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
@@ -127556,11 +127767,11 @@
127556127767
db->lookaside.bEnabled = enableLookaside;
127557127768
if( db->mallocFailed ){
127558127769
pParse->rc = SQLITE_NOMEM;
127559127770
}
127560127771
if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
127561
- sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
127772
+ pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
127562127773
}
127563127774
assert( pzErrMsg!=0 );
127564127775
if( pParse->zErrMsg ){
127565127776
*pzErrMsg = pParse->zErrMsg;
127566127777
sqlite3_log(pParse->rc, "%s", *pzErrMsg);
@@ -130748,10 +130959,13 @@
130748130959
| SQLITE_ForeignKeys
130749130960
#endif
130750130961
#if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
130751130962
| SQLITE_ReverseOrder
130752130963
#endif
130964
+#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
130965
+ | SQLITE_CellSizeCk
130966
+#endif
130753130967
;
130754130968
sqlite3HashInit(&db->aCollSeq);
130755130969
#ifndef SQLITE_OMIT_VIRTUALTABLE
130756130970
sqlite3HashInit(&db->aModule);
130757130971
#endif
@@ -130867,12 +131081,11 @@
130867131081
}
130868131082
#endif
130869131083
130870131084
#ifdef SQLITE_ENABLE_DBSTAT_VTAB
130871131085
if( !db->mallocFailed && rc==SQLITE_OK){
130872
- int sqlite3_dbstat_register(sqlite3*);
130873
- rc = sqlite3_dbstat_register(db);
131086
+ rc = sqlite3DbstatRegister(db);
130874131087
}
130875131088
#endif
130876131089
130877131090
/* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
130878131091
** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
@@ -132872,10 +133085,12 @@
132872133085
typedef struct Fts3SegFilter Fts3SegFilter;
132873133086
typedef struct Fts3DeferredToken Fts3DeferredToken;
132874133087
typedef struct Fts3SegReader Fts3SegReader;
132875133088
typedef struct Fts3MultiSegReader Fts3MultiSegReader;
132876133089
133090
+typedef struct MatchinfoBuffer MatchinfoBuffer;
133091
+
132877133092
/*
132878133093
** A connection to a fulltext index is an instance of the following
132879133094
** structure. The xCreate and xConnect methods create an instance
132880133095
** of this structure and xDestroy and xDisconnect free that instance.
132881133096
** All other methods receive a pointer to the structure as one of their
@@ -132981,13 +133196,11 @@
132981133196
int nRowAvg; /* Average size of database rows, in pages */
132982133197
sqlite3_int64 nDoc; /* Documents in table */
132983133198
i64 iMinDocid; /* Minimum docid to return */
132984133199
i64 iMaxDocid; /* Maximum docid to return */
132985133200
int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */
132986
- u32 *aMatchinfo; /* Information about most recent match */
132987
- int nMatchinfo; /* Number of elements in aMatchinfo[] */
132988
- char *zMatchinfo; /* Matchinfo specification */
133201
+ MatchinfoBuffer *pMIBuffer; /* Buffer for matchinfo data */
132989133202
};
132990133203
132991133204
#define FTS3_EVAL_FILTER 0
132992133205
#define FTS3_EVAL_NEXT 1
132993133206
#define FTS3_EVAL_MATCHINFO 2
@@ -133103,11 +133316,13 @@
133103133316
sqlite3_int64 iDocid; /* Current docid */
133104133317
u8 bEof; /* True this expression is at EOF already */
133105133318
u8 bStart; /* True if iDocid is valid */
133106133319
u8 bDeferred; /* True if this expression is entirely deferred */
133107133320
133108
- u32 *aMI;
133321
+ /* The following are used by the fts3_snippet.c module. */
133322
+ int iPhrase; /* Index of this phrase in matchinfo() results */
133323
+ u32 *aMI; /* See above */
133109133324
};
133110133325
133111133326
/*
133112133327
** Candidate values for Fts3Query.eType. Note that the order of the first
133113133328
** four values is in order of precedence when parsing expressions. For
@@ -133224,10 +133439,11 @@
133224133439
SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
133225133440
SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
133226133441
SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
133227133442
SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
133228133443
SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
133444
+SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
133229133445
133230133446
/* fts3_tokenizer.c */
133231133447
SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
133232133448
SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
133233133449
SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
@@ -133239,10 +133455,11 @@
133239133455
SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
133240133456
SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
133241133457
const char *, const char *, int, int
133242133458
);
133243133459
SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
133460
+SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
133244133461
133245133462
/* fts3_expr.c */
133246133463
SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
133247133464
char **, int, int, int, const char *, int, Fts3Expr **, char **
133248133465
);
@@ -134666,11 +134883,11 @@
134666134883
assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
134667134884
sqlite3_finalize(pCsr->pStmt);
134668134885
sqlite3Fts3ExprFree(pCsr->pExpr);
134669134886
sqlite3Fts3FreeDeferredTokens(pCsr);
134670134887
sqlite3_free(pCsr->aDoclist);
134671
- sqlite3_free(pCsr->aMatchinfo);
134888
+ sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
134672134889
assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
134673134890
sqlite3_free(pCsr);
134674134891
return SQLITE_OK;
134675134892
}
134676134893
@@ -136167,11 +136384,11 @@
136167136384
assert( iIdx==nVal );
136168136385
136169136386
/* In case the cursor has been used before, clear it now. */
136170136387
sqlite3_finalize(pCsr->pStmt);
136171136388
sqlite3_free(pCsr->aDoclist);
136172
- sqlite3_free(pCsr->aMatchinfo);
136389
+ sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
136173136390
sqlite3Fts3ExprFree(pCsr->pExpr);
136174136391
memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
136175136392
136176136393
/* Set the lower and upper bounds on docids to return */
136177136394
pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
@@ -138065,11 +138282,11 @@
138065138282
** is populated as for "A * C" before returning.
138066138283
**
138067138284
** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is
138068138285
** advanced to point to the next row that matches "x AND y".
138069138286
**
138070
-** See fts3EvalTestDeferredAndNear() for details on testing if a row is
138287
+** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
138071138288
** really a match, taking into account deferred tokens and NEAR operators.
138072138289
*/
138073138290
static void fts3EvalNextRow(
138074138291
Fts3Cursor *pCsr, /* FTS Cursor handle */
138075138292
Fts3Expr *pExpr, /* Expr. to advance to next matching row */
@@ -138285,11 +138502,11 @@
138285138502
138286138503
return res;
138287138504
}
138288138505
138289138506
/*
138290
-** This function is a helper function for fts3EvalTestDeferredAndNear().
138507
+** This function is a helper function for sqlite3Fts3EvalTestDeferred().
138291138508
** Assuming no error occurs or has occurred, It returns non-zero if the
138292138509
** expression passed as the second argument matches the row that pCsr
138293138510
** currently points to, or zero if it does not.
138294138511
**
138295138512
** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
@@ -138406,11 +138623,11 @@
138406138623
** it is determined that the row does *not* match the query.
138407138624
**
138408138625
** Or, if no error occurs and it seems the current row does match the FTS
138409138626
** query, return 0.
138410138627
*/
138411
-static int fts3EvalTestDeferredAndNear(Fts3Cursor *pCsr, int *pRc){
138628
+SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
138412138629
int rc = *pRc;
138413138630
int bMiss = 0;
138414138631
if( rc==SQLITE_OK ){
138415138632
138416138633
/* If there are one or more deferred tokens, load the current row into
@@ -138453,11 +138670,11 @@
138453138670
fts3EvalNextRow(pCsr, pExpr, &rc);
138454138671
pCsr->isEof = pExpr->bEof;
138455138672
pCsr->isRequireSeek = 1;
138456138673
pCsr->isMatchinfoNeeded = 1;
138457138674
pCsr->iPrevId = pExpr->iDocid;
138458
- }while( pCsr->isEof==0 && fts3EvalTestDeferredAndNear(pCsr, &rc) );
138675
+ }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
138459138676
}
138460138677
138461138678
/* Check if the cursor is past the end of the docid range specified
138462138679
** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */
138463138680
if( rc==SQLITE_OK && (
@@ -138614,11 +138831,11 @@
138614138831
pCsr->isRequireSeek = 1;
138615138832
pCsr->isMatchinfoNeeded = 1;
138616138833
pCsr->iPrevId = pRoot->iDocid;
138617138834
}while( pCsr->isEof==0
138618138835
&& pRoot->eType==FTSQUERY_NEAR
138619
- && fts3EvalTestDeferredAndNear(pCsr, &rc)
138836
+ && sqlite3Fts3EvalTestDeferred(pCsr, &rc)
138620138837
);
138621138838
138622138839
if( rc==SQLITE_OK && pCsr->isEof==0 ){
138623138840
fts3EvalUpdateCounts(pRoot);
138624138841
}
@@ -138639,11 +138856,10 @@
138639138856
fts3EvalRestart(pCsr, pRoot, &rc);
138640138857
do {
138641138858
fts3EvalNextRow(pCsr, pRoot, &rc);
138642138859
assert( pRoot->bEof==0 );
138643138860
}while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
138644
- fts3EvalTestDeferredAndNear(pCsr, &rc);
138645138861
}
138646138862
}
138647138863
return rc;
138648138864
}
138649138865
@@ -148651,10 +148867,11 @@
148651148867
#define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */
148652148868
#define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */
148653148869
#define FTS3_MATCHINFO_LCS 's' /* nCol values */
148654148870
#define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */
148655148871
#define FTS3_MATCHINFO_LHITS 'y' /* nCol*nPhrase values */
148872
+#define FTS3_MATCHINFO_LHITS_BM 'b' /* nCol*nPhrase values */
148656148873
148657148874
/*
148658148875
** The default value for the second argument to matchinfo().
148659148876
*/
148660148877
#define FTS3_MATCHINFO_DEFAULT "pcx"
@@ -148712,13 +148929,26 @@
148712148929
struct MatchInfo {
148713148930
Fts3Cursor *pCursor; /* FTS3 Cursor */
148714148931
int nCol; /* Number of columns in table */
148715148932
int nPhrase; /* Number of matchable phrases in query */
148716148933
sqlite3_int64 nDoc; /* Number of docs in database */
148934
+ char flag;
148717148935
u32 *aMatchinfo; /* Pre-allocated buffer */
148718148936
};
148719148937
148938
+/*
148939
+** An instance of this structure is used to manage a pair of buffers, each
148940
+** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
148941
+** for details.
148942
+*/
148943
+struct MatchinfoBuffer {
148944
+ u8 aRef[3];
148945
+ int nElem;
148946
+ int bGlobal; /* Set if global data is loaded */
148947
+ char *zMatchinfo;
148948
+ u32 aMatchinfo[1];
148949
+};
148720148950
148721148951
148722148952
/*
148723148953
** The snippet() and offsets() functions both return text values. An instance
148724148954
** of the following structure is used to accumulate those values while the
@@ -148729,10 +148959,101 @@
148729148959
char *z; /* Pointer to buffer containing string */
148730148960
int n; /* Length of z in bytes (excl. nul-term) */
148731148961
int nAlloc; /* Allocated size of buffer z in bytes */
148732148962
};
148733148963
148964
+
148965
+/*************************************************************************
148966
+** Start of MatchinfoBuffer code.
148967
+*/
148968
+
148969
+/*
148970
+** Allocate a two-slot MatchinfoBuffer object.
148971
+*/
148972
+static MatchinfoBuffer *fts3MIBufferNew(int nElem, const char *zMatchinfo){
148973
+ MatchinfoBuffer *pRet;
148974
+ int nByte = sizeof(u32) * (2*nElem + 1) + sizeof(MatchinfoBuffer);
148975
+ int nStr = (int)strlen(zMatchinfo);
148976
+
148977
+ pRet = sqlite3_malloc(nByte + nStr+1);
148978
+ if( pRet ){
148979
+ memset(pRet, 0, nByte);
148980
+ pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
148981
+ pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0] + sizeof(u32)*(nElem+1);
148982
+ pRet->nElem = nElem;
148983
+ pRet->zMatchinfo = ((char*)pRet) + nByte;
148984
+ memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
148985
+ pRet->aRef[0] = 1;
148986
+ }
148987
+
148988
+ return pRet;
148989
+}
148990
+
148991
+static void fts3MIBufferFree(void *p){
148992
+ MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
148993
+
148994
+ assert( (u32*)p==&pBuf->aMatchinfo[1]
148995
+ || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2]
148996
+ );
148997
+ if( (u32*)p==&pBuf->aMatchinfo[1] ){
148998
+ pBuf->aRef[1] = 0;
148999
+ }else{
149000
+ pBuf->aRef[2] = 0;
149001
+ }
149002
+
149003
+ if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
149004
+ sqlite3_free(pBuf);
149005
+ }
149006
+}
149007
+
149008
+static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
149009
+ void (*xRet)(void*) = 0;
149010
+ u32 *aOut = 0;
149011
+
149012
+ if( p->aRef[1]==0 ){
149013
+ p->aRef[1] = 1;
149014
+ aOut = &p->aMatchinfo[1];
149015
+ xRet = fts3MIBufferFree;
149016
+ }
149017
+ else if( p->aRef[2]==0 ){
149018
+ p->aRef[2] = 1;
149019
+ aOut = &p->aMatchinfo[p->nElem+2];
149020
+ xRet = fts3MIBufferFree;
149021
+ }else{
149022
+ aOut = (u32*)sqlite3_malloc(p->nElem * sizeof(u32));
149023
+ if( aOut ){
149024
+ xRet = sqlite3_free;
149025
+ if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32));
149026
+ }
149027
+ }
149028
+
149029
+ *paOut = aOut;
149030
+ return xRet;
149031
+}
149032
+
149033
+static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
149034
+ p->bGlobal = 1;
149035
+ memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32));
149036
+}
149037
+
149038
+/*
149039
+** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
149040
+*/
149041
+SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
149042
+ if( p ){
149043
+ assert( p->aRef[0]==1 );
149044
+ p->aRef[0] = 0;
149045
+ if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
149046
+ sqlite3_free(p);
149047
+ }
149048
+ }
149049
+}
149050
+
149051
+/*
149052
+** End of MatchinfoBuffer code.
149053
+*************************************************************************/
149054
+
148734149055
148735149056
/*
148736149057
** This function is used to help iterate through a position-list. A position
148737149058
** list is a list of unique integers, sorted from smallest to largest. Each
148738149059
** element of the list is represented by an FTS3 varint that takes the value
@@ -148766,11 +149087,11 @@
148766149087
int *piPhrase, /* Pointer to phrase counter */
148767149088
int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
148768149089
void *pCtx /* Second argument to pass to callback */
148769149090
){
148770149091
int rc; /* Return code */
148771
- int eType = pExpr->eType; /* Type of expression node pExpr */
149092
+ int eType = pExpr->eType; /* Type of expression node pExpr */
148772149093
148773149094
if( eType!=FTSQUERY_PHRASE ){
148774149095
assert( pExpr->pLeft && pExpr->pRight );
148775149096
rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
148776149097
if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
@@ -148799,10 +149120,11 @@
148799149120
void *pCtx /* Second argument to pass to callback */
148800149121
){
148801149122
int iPhrase = 0; /* Variable used as the phrase counter */
148802149123
return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
148803149124
}
149125
+
148804149126
148805149127
/*
148806149128
** This is an fts3ExprIterate() callback used while loading the doclists
148807149129
** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
148808149130
** fts3ExprLoadDoclists().
@@ -148844,12 +149166,11 @@
148844149166
return rc;
148845149167
}
148846149168
148847149169
static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
148848149170
(*(int *)ctx)++;
148849
- UNUSED_PARAMETER(pExpr);
148850
- UNUSED_PARAMETER(iPhrase);
149171
+ pExpr->iPhrase = iPhrase;
148851149172
return SQLITE_OK;
148852149173
}
148853149174
static int fts3ExprPhraseCount(Fts3Expr *pExpr){
148854149175
int nPhrase = 0;
148855149176
(void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
@@ -149066,11 +149387,11 @@
149066149387
sIter.pCsr = pCsr;
149067149388
sIter.iCol = iCol;
149068149389
sIter.nSnippet = nSnippet;
149069149390
sIter.nPhrase = nList;
149070149391
sIter.iCurrent = -1;
149071
- rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter);
149392
+ rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter);
149072149393
if( rc==SQLITE_OK ){
149073149394
149074149395
/* Set the *pmSeen output variable. */
149075149396
for(i=0; i<nList; i++){
149076149397
if( sIter.aPhrase[i].pHead ){
@@ -149366,10 +149687,64 @@
149366149687
}
149367149688
149368149689
*ppCollist = pEnd;
149369149690
return nEntry;
149370149691
}
149692
+
149693
+/*
149694
+** This function gathers 'y' or 'b' data for a single phrase.
149695
+*/
149696
+static void fts3ExprLHits(
149697
+ Fts3Expr *pExpr, /* Phrase expression node */
149698
+ MatchInfo *p /* Matchinfo context */
149699
+){
149700
+ Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
149701
+ int iStart;
149702
+ Fts3Phrase *pPhrase = pExpr->pPhrase;
149703
+ char *pIter = pPhrase->doclist.pList;
149704
+ int iCol = 0;
149705
+
149706
+ assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS );
149707
+ if( p->flag==FTS3_MATCHINFO_LHITS ){
149708
+ iStart = pExpr->iPhrase * p->nCol;
149709
+ }else{
149710
+ iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
149711
+ }
149712
+
149713
+ while( 1 ){
149714
+ int nHit = fts3ColumnlistCount(&pIter);
149715
+ if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
149716
+ if( p->flag==FTS3_MATCHINFO_LHITS ){
149717
+ p->aMatchinfo[iStart + iCol] = (u32)nHit;
149718
+ }else if( nHit ){
149719
+ p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
149720
+ }
149721
+ }
149722
+ assert( *pIter==0x00 || *pIter==0x01 );
149723
+ if( *pIter!=0x01 ) break;
149724
+ pIter++;
149725
+ pIter += fts3GetVarint32(pIter, &iCol);
149726
+ }
149727
+}
149728
+
149729
+/*
149730
+** Gather the results for matchinfo directives 'y' and 'b'.
149731
+*/
149732
+static void fts3ExprLHitGather(
149733
+ Fts3Expr *pExpr,
149734
+ MatchInfo *p
149735
+){
149736
+ assert( (pExpr->pLeft==0)==(pExpr->pRight==0) );
149737
+ if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
149738
+ if( pExpr->pLeft ){
149739
+ fts3ExprLHitGather(pExpr->pLeft, p);
149740
+ fts3ExprLHitGather(pExpr->pRight, p);
149741
+ }else{
149742
+ fts3ExprLHits(pExpr, p);
149743
+ }
149744
+ }
149745
+}
149371149746
149372149747
/*
149373149748
** fts3ExprIterate() callback used to collect the "global" matchinfo stats
149374149749
** for a single query.
149375149750
**
@@ -149433,55 +149808,10 @@
149433149808
}
149434149809
149435149810
return rc;
149436149811
}
149437149812
149438
-/*
149439
-** fts3ExprIterate() callback used to gather information for the matchinfo
149440
-** directive 'y'.
149441
-*/
149442
-static int fts3ExprLHitsCb(
149443
- Fts3Expr *pExpr, /* Phrase expression node */
149444
- int iPhrase, /* Phrase number */
149445
- void *pCtx /* Pointer to MatchInfo structure */
149446
-){
149447
- MatchInfo *p = (MatchInfo *)pCtx;
149448
- Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
149449
- int rc = SQLITE_OK;
149450
- int iStart = iPhrase * p->nCol;
149451
- Fts3Expr *pEof; /* Ancestor node already at EOF */
149452
-
149453
- /* This must be a phrase */
149454
- assert( pExpr->pPhrase );
149455
-
149456
- /* Initialize all output integers to zero. */
149457
- memset(&p->aMatchinfo[iStart], 0, sizeof(u32) * p->nCol);
149458
-
149459
- /* Check if this or any parent node is at EOF. If so, then all output
149460
- ** values are zero. */
149461
- for(pEof=pExpr; pEof && pEof->bEof==0; pEof=pEof->pParent);
149462
-
149463
- if( pEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
149464
- Fts3Phrase *pPhrase = pExpr->pPhrase;
149465
- char *pIter = pPhrase->doclist.pList;
149466
- int iCol = 0;
149467
-
149468
- while( 1 ){
149469
- int nHit = fts3ColumnlistCount(&pIter);
149470
- if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
149471
- p->aMatchinfo[iStart + iCol] = (u32)nHit;
149472
- }
149473
- assert( *pIter==0x00 || *pIter==0x01 );
149474
- if( *pIter!=0x01 ) break;
149475
- pIter++;
149476
- pIter += fts3GetVarint32(pIter, &iCol);
149477
- }
149478
- }
149479
-
149480
- return rc;
149481
-}
149482
-
149483149813
static int fts3MatchinfoCheck(
149484149814
Fts3Table *pTab,
149485149815
char cArg,
149486149816
char **pzErr
149487149817
){
@@ -149491,10 +149821,11 @@
149491149821
|| (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
149492149822
|| (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
149493149823
|| (cArg==FTS3_MATCHINFO_LCS)
149494149824
|| (cArg==FTS3_MATCHINFO_HITS)
149495149825
|| (cArg==FTS3_MATCHINFO_LHITS)
149826
+ || (cArg==FTS3_MATCHINFO_LHITS_BM)
149496149827
){
149497149828
return SQLITE_OK;
149498149829
}
149499149830
sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
149500149831
return SQLITE_ERROR;
@@ -149517,10 +149848,14 @@
149517149848
break;
149518149849
149519149850
case FTS3_MATCHINFO_LHITS:
149520149851
nVal = pInfo->nCol * pInfo->nPhrase;
149521149852
break;
149853
+
149854
+ case FTS3_MATCHINFO_LHITS_BM:
149855
+ nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
149856
+ break;
149522149857
149523149858
default:
149524149859
assert( cArg==FTS3_MATCHINFO_HITS );
149525149860
nVal = pInfo->nCol * pInfo->nPhrase * 3;
149526149861
break;
@@ -149712,11 +150047,11 @@
149712150047
int i;
149713150048
Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
149714150049
sqlite3_stmt *pSelect = 0;
149715150050
149716150051
for(i=0; rc==SQLITE_OK && zArg[i]; i++){
149717
-
150052
+ pInfo->flag = zArg[i];
149718150053
switch( zArg[i] ){
149719150054
case FTS3_MATCHINFO_NPHRASE:
149720150055
if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
149721150056
break;
149722150057
@@ -149772,13 +150107,17 @@
149772150107
if( rc==SQLITE_OK ){
149773150108
rc = fts3MatchinfoLcs(pCsr, pInfo);
149774150109
}
149775150110
break;
149776150111
149777
- case FTS3_MATCHINFO_LHITS:
149778
- (void)fts3ExprIterate(pCsr->pExpr, fts3ExprLHitsCb, (void*)pInfo);
150112
+ case FTS3_MATCHINFO_LHITS_BM:
150113
+ case FTS3_MATCHINFO_LHITS: {
150114
+ int nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32);
150115
+ memset(pInfo->aMatchinfo, 0, nZero);
150116
+ fts3ExprLHitGather(pCsr->pExpr, pInfo);
149779150117
break;
150118
+ }
149780150119
149781150120
default: {
149782150121
Fts3Expr *pExpr;
149783150122
assert( zArg[i]==FTS3_MATCHINFO_HITS );
149784150123
pExpr = pCsr->pExpr;
@@ -149788,10 +150127,11 @@
149788150127
if( pCsr->pDeferred ){
149789150128
rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
149790150129
if( rc!=SQLITE_OK ) break;
149791150130
}
149792150131
rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
150132
+ sqlite3Fts3EvalTestDeferred(pCsr, &rc);
149793150133
if( rc!=SQLITE_OK ) break;
149794150134
}
149795150135
(void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
149796150136
break;
149797150137
}
@@ -149807,73 +150147,90 @@
149807150147
149808150148
/*
149809150149
** Populate pCsr->aMatchinfo[] with data for the current row. The
149810150150
** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
149811150151
*/
149812
-static int fts3GetMatchinfo(
150152
+static void fts3GetMatchinfo(
150153
+ sqlite3_context *pCtx, /* Return results here */
149813150154
Fts3Cursor *pCsr, /* FTS3 Cursor object */
149814150155
const char *zArg /* Second argument to matchinfo() function */
149815150156
){
149816150157
MatchInfo sInfo;
149817150158
Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
149818150159
int rc = SQLITE_OK;
149819150160
int bGlobal = 0; /* Collect 'global' stats as well as local */
149820150161
150162
+ u32 *aOut = 0;
150163
+ void (*xDestroyOut)(void*) = 0;
150164
+
149821150165
memset(&sInfo, 0, sizeof(MatchInfo));
149822150166
sInfo.pCursor = pCsr;
149823150167
sInfo.nCol = pTab->nColumn;
149824150168
149825150169
/* If there is cached matchinfo() data, but the format string for the
149826150170
** cache does not match the format string for this request, discard
149827150171
** the cached data. */
149828
- if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){
149829
- assert( pCsr->aMatchinfo );
149830
- sqlite3_free(pCsr->aMatchinfo);
149831
- pCsr->zMatchinfo = 0;
149832
- pCsr->aMatchinfo = 0;
150172
+ if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
150173
+ sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
150174
+ pCsr->pMIBuffer = 0;
149833150175
}
149834150176
149835
- /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the
150177
+ /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
149836150178
** matchinfo function has been called for this query. In this case
149837150179
** allocate the array used to accumulate the matchinfo data and
149838150180
** initialize those elements that are constant for every row.
149839150181
*/
149840
- if( pCsr->aMatchinfo==0 ){
150182
+ if( pCsr->pMIBuffer==0 ){
149841150183
int nMatchinfo = 0; /* Number of u32 elements in match-info */
149842
- int nArg; /* Bytes in zArg */
149843150184
int i; /* Used to iterate through zArg */
149844150185
149845150186
/* Determine the number of phrases in the query */
149846150187
pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
149847150188
sInfo.nPhrase = pCsr->nPhrase;
149848150189
149849150190
/* Determine the number of integers in the buffer returned by this call. */
149850150191
for(i=0; zArg[i]; i++){
150192
+ char *zErr = 0;
150193
+ if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
150194
+ sqlite3_result_error(pCtx, zErr, -1);
150195
+ sqlite3_free(zErr);
150196
+ return;
150197
+ }
149851150198
nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
149852150199
}
149853150200
149854150201
/* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
149855
- nArg = (int)strlen(zArg);
149856
- pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1);
149857
- if( !pCsr->aMatchinfo ) return SQLITE_NOMEM;
149858
-
149859
- pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo];
149860
- pCsr->nMatchinfo = nMatchinfo;
149861
- memcpy(pCsr->zMatchinfo, zArg, nArg+1);
149862
- memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo);
150202
+ pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
150203
+ if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM;
150204
+
149863150205
pCsr->isMatchinfoNeeded = 1;
149864150206
bGlobal = 1;
149865150207
}
149866150208
149867
- sInfo.aMatchinfo = pCsr->aMatchinfo;
149868
- sInfo.nPhrase = pCsr->nPhrase;
149869
- if( pCsr->isMatchinfoNeeded ){
150209
+ if( rc==SQLITE_OK ){
150210
+ xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
150211
+ if( xDestroyOut==0 ){
150212
+ rc = SQLITE_NOMEM;
150213
+ }
150214
+ }
150215
+
150216
+ if( rc==SQLITE_OK ){
150217
+ sInfo.aMatchinfo = aOut;
150218
+ sInfo.nPhrase = pCsr->nPhrase;
149870150219
rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
149871
- pCsr->isMatchinfoNeeded = 0;
150220
+ if( bGlobal ){
150221
+ fts3MIBufferSetGlobal(pCsr->pMIBuffer);
150222
+ }
149872150223
}
149873150224
149874
- return rc;
150225
+ if( rc!=SQLITE_OK ){
150226
+ sqlite3_result_error_code(pCtx, rc);
150227
+ if( xDestroyOut ) xDestroyOut(aOut);
150228
+ }else{
150229
+ int n = pCsr->pMIBuffer->nElem * sizeof(u32);
150230
+ sqlite3_result_blob(pCtx, aOut, n, xDestroyOut);
150231
+ }
149875150232
}
149876150233
149877150234
/*
149878150235
** Implementation of snippet() function.
149879150236
*/
@@ -150075,11 +150432,11 @@
150075150432
** no way that this operation can fail, so the return code from
150076150433
** fts3ExprIterate() can be discarded.
150077150434
*/
150078150435
sCtx.iCol = iCol;
150079150436
sCtx.iTerm = 0;
150080
- (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx);
150437
+ (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx);
150081150438
150082150439
/* Retreive the text stored in column iCol. If an SQL NULL is stored
150083150440
** in column iCol, jump immediately to the next iteration of the loop.
150084150441
** If an OOM occurs while retrieving the data (this can happen if SQLite
150085150442
** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
@@ -150167,42 +150524,25 @@
150167150524
sqlite3_context *pContext, /* Function call context */
150168150525
Fts3Cursor *pCsr, /* FTS3 table cursor */
150169150526
const char *zArg /* Second arg to matchinfo() function */
150170150527
){
150171150528
Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
150172
- int rc;
150173
- int i;
150174150529
const char *zFormat;
150175150530
150176150531
if( zArg ){
150177
- for(i=0; zArg[i]; i++){
150178
- char *zErr = 0;
150179
- if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
150180
- sqlite3_result_error(pContext, zErr, -1);
150181
- sqlite3_free(zErr);
150182
- return;
150183
- }
150184
- }
150185150532
zFormat = zArg;
150186150533
}else{
150187150534
zFormat = FTS3_MATCHINFO_DEFAULT;
150188150535
}
150189150536
150190150537
if( !pCsr->pExpr ){
150191150538
sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
150192150539
return;
150193
- }
150194
-
150195
- /* Retrieve matchinfo() data. */
150196
- rc = fts3GetMatchinfo(pCsr, zFormat);
150197
- sqlite3Fts3SegmentsClose(pTab);
150198
-
150199
- if( rc!=SQLITE_OK ){
150200
- sqlite3_result_error_code(pContext, rc);
150201150540
}else{
150202
- int n = pCsr->nMatchinfo * sizeof(u32);
150203
- sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT);
150541
+ /* Retrieve matchinfo() data. */
150542
+ fts3GetMatchinfo(pContext, pCsr, zFormat);
150543
+ sqlite3Fts3SegmentsClose(pTab);
150204150544
}
150205150545
}
150206150546
150207150547
#endif
150208150548
@@ -151319,10 +151659,11 @@
151319151659
*/
151320151660
struct RtreeMatchArg {
151321151661
u32 magic; /* Always RTREE_GEOMETRY_MAGIC */
151322151662
RtreeGeomCallback cb; /* Info about the callback functions */
151323151663
int nParam; /* Number of parameters to the SQL function */
151664
+ sqlite3_value **apSqlParam; /* Original SQL parameter values */
151324151665
RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
151325151666
};
151326151667
151327151668
#ifndef MAX
151328151669
# define MAX(x,y) ((x) < (y) ? (y) : (x))
@@ -152450,13 +152791,11 @@
152450152791
/* Check that value is actually a blob. */
152451152792
if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
152452152793
152453152794
/* Check that the blob is roughly the right size. */
152454152795
nBlob = sqlite3_value_bytes(pValue);
152455
- if( nBlob<(int)sizeof(RtreeMatchArg)
152456
- || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
152457
- ){
152796
+ if( nBlob<(int)sizeof(RtreeMatchArg) ){
152458152797
return SQLITE_ERROR;
152459152798
}
152460152799
152461152800
pInfo = (sqlite3_rtree_query_info*)sqlite3_malloc( sizeof(*pInfo)+nBlob );
152462152801
if( !pInfo ) return SQLITE_NOMEM;
@@ -152463,18 +152802,20 @@
152463152802
memset(pInfo, 0, sizeof(*pInfo));
152464152803
pBlob = (RtreeMatchArg*)&pInfo[1];
152465152804
152466152805
memcpy(pBlob, sqlite3_value_blob(pValue), nBlob);
152467152806
nExpected = (int)(sizeof(RtreeMatchArg) +
152807
+ pBlob->nParam*sizeof(sqlite3_value*) +
152468152808
(pBlob->nParam-1)*sizeof(RtreeDValue));
152469152809
if( pBlob->magic!=RTREE_GEOMETRY_MAGIC || nBlob!=nExpected ){
152470152810
sqlite3_free(pInfo);
152471152811
return SQLITE_ERROR;
152472152812
}
152473152813
pInfo->pContext = pBlob->cb.pContext;
152474152814
pInfo->nParam = pBlob->nParam;
152475152815
pInfo->aParam = pBlob->aParam;
152816
+ pInfo->apSqlParam = pBlob->apSqlParam;
152476152817
152477152818
if( pBlob->cb.xGeom ){
152478152819
pCons->u.xGeom = pBlob->cb.xGeom;
152479152820
}else{
152480152821
pCons->op = RTREE_QUERY;
@@ -152637,21 +152978,34 @@
152637152978
*/
152638152979
static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
152639152980
Rtree *pRtree = (Rtree*)tab;
152640152981
int rc = SQLITE_OK;
152641152982
int ii;
152983
+ int bMatch = 0; /* True if there exists a MATCH constraint */
152642152984
i64 nRow; /* Estimated rows returned by this scan */
152643152985
152644152986
int iIdx = 0;
152645152987
char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
152646152988
memset(zIdxStr, 0, sizeof(zIdxStr));
152989
+
152990
+ /* Check if there exists a MATCH constraint - even an unusable one. If there
152991
+ ** is, do not consider the lookup-by-rowid plan as using such a plan would
152992
+ ** require the VDBE to evaluate the MATCH constraint, which is not currently
152993
+ ** possible. */
152994
+ for(ii=0; ii<pIdxInfo->nConstraint; ii++){
152995
+ if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){
152996
+ bMatch = 1;
152997
+ }
152998
+ }
152647152999
152648153000
assert( pIdxInfo->idxStr==0 );
152649153001
for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
152650153002
struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
152651153003
152652
- if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
153004
+ if( bMatch==0 && p->usable
153005
+ && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ
153006
+ ){
152653153007
/* We have an equality constraint on the rowid. Use strategy 1. */
152654153008
int jj;
152655153009
for(jj=0; jj<ii; jj++){
152656153010
pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
152657153011
pIdxInfo->aConstraintUsage[jj].omit = 0;
@@ -154339,10 +154693,22 @@
154339154693
static void rtreeFreeCallback(void *p){
154340154694
RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
154341154695
if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
154342154696
sqlite3_free(p);
154343154697
}
154698
+
154699
+/*
154700
+** This routine frees the BLOB that is returned by geomCallback().
154701
+*/
154702
+static void rtreeMatchArgFree(void *pArg){
154703
+ int i;
154704
+ RtreeMatchArg *p = (RtreeMatchArg*)pArg;
154705
+ for(i=0; i<p->nParam; i++){
154706
+ sqlite3_value_free(p->apSqlParam[i]);
154707
+ }
154708
+ sqlite3_free(p);
154709
+}
154344154710
154345154711
/*
154346154712
** Each call to sqlite3_rtree_geometry_callback() or
154347154713
** sqlite3_rtree_query_callback() creates an ordinary SQLite
154348154714
** scalar function that is implemented by this routine.
@@ -154358,28 +154724,38 @@
154358154724
*/
154359154725
static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
154360154726
RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
154361154727
RtreeMatchArg *pBlob;
154362154728
int nBlob;
154729
+ int memErr = 0;
154363154730
154364
- nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue);
154731
+ nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
154732
+ + nArg*sizeof(sqlite3_value*);
154365154733
pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
154366154734
if( !pBlob ){
154367154735
sqlite3_result_error_nomem(ctx);
154368154736
}else{
154369154737
int i;
154370154738
pBlob->magic = RTREE_GEOMETRY_MAGIC;
154371154739
pBlob->cb = pGeomCtx[0];
154740
+ pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
154372154741
pBlob->nParam = nArg;
154373154742
for(i=0; i<nArg; i++){
154743
+ pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
154744
+ if( pBlob->apSqlParam[i]==0 ) memErr = 1;
154374154745
#ifdef SQLITE_RTREE_INT_ONLY
154375154746
pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
154376154747
#else
154377154748
pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
154378154749
#endif
154379154750
}
154380
- sqlite3_result_blob(ctx, pBlob, nBlob, sqlite3_free);
154751
+ if( memErr ){
154752
+ sqlite3_result_error_nomem(ctx);
154753
+ rtreeMatchArgFree(pBlob);
154754
+ }else{
154755
+ sqlite3_result_blob(ctx, pBlob, nBlob, rtreeMatchArgFree);
154756
+ }
154381154757
}
154382154758
}
154383154759
154384154760
/*
154385154761
** Register a new geometry function for use with the r-tree MATCH operator.
@@ -155211,10 +155587,4095 @@
155211155587
155212155588
#endif /* defined(SQLITE_ENABLE_ICU) */
155213155589
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
155214155590
155215155591
/************** End of fts3_icu.c ********************************************/
155592
+/************** Begin file sqlite3ota.c **************************************/
155593
+/*
155594
+** 2014 August 30
155595
+**
155596
+** The author disclaims copyright to this source code. In place of
155597
+** a legal notice, here is a blessing:
155598
+**
155599
+** May you do good and not evil.
155600
+** May you find forgiveness for yourself and forgive others.
155601
+** May you share freely, never taking more than you give.
155602
+**
155603
+*************************************************************************
155604
+**
155605
+**
155606
+** OVERVIEW
155607
+**
155608
+** The OTA extension requires that the OTA update be packaged as an
155609
+** SQLite database. The tables it expects to find are described in
155610
+** sqlite3ota.h. Essentially, for each table xyz in the target database
155611
+** that the user wishes to write to, a corresponding data_xyz table is
155612
+** created in the OTA database and populated with one row for each row to
155613
+** update, insert or delete from the target table.
155614
+**
155615
+** The update proceeds in three stages:
155616
+**
155617
+** 1) The database is updated. The modified database pages are written
155618
+** to a *-oal file. A *-oal file is just like a *-wal file, except
155619
+** that it is named "<database>-oal" instead of "<database>-wal".
155620
+** Because regular SQLite clients do not look for file named
155621
+** "<database>-oal", they go on using the original database in
155622
+** rollback mode while the *-oal file is being generated.
155623
+**
155624
+** During this stage OTA does not update the database by writing
155625
+** directly to the target tables. Instead it creates "imposter"
155626
+** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
155627
+** to update each b-tree individually. All updates required by each
155628
+** b-tree are completed before moving on to the next, and all
155629
+** updates are done in sorted key order.
155630
+**
155631
+** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
155632
+** location using a call to rename(2). Before doing this the OTA
155633
+** module takes an EXCLUSIVE lock on the database file, ensuring
155634
+** that there are no other active readers.
155635
+**
155636
+** Once the EXCLUSIVE lock is released, any other database readers
155637
+** detect the new *-wal file and read the database in wal mode. At
155638
+** this point they see the new version of the database - including
155639
+** the updates made as part of the OTA update.
155640
+**
155641
+** 3) The new *-wal file is checkpointed. This proceeds in the same way
155642
+** as a regular database checkpoint, except that a single frame is
155643
+** checkpointed each time sqlite3ota_step() is called. If the OTA
155644
+** handle is closed before the entire *-wal file is checkpointed,
155645
+** the checkpoint progress is saved in the OTA database and the
155646
+** checkpoint can be resumed by another OTA client at some point in
155647
+** the future.
155648
+**
155649
+** POTENTIAL PROBLEMS
155650
+**
155651
+** The rename() call might not be portable. And OTA is not currently
155652
+** syncing the directory after renaming the file.
155653
+**
155654
+** When state is saved, any commit to the *-oal file and the commit to
155655
+** the OTA update database are not atomic. So if the power fails at the
155656
+** wrong moment they might get out of sync. As the main database will be
155657
+** committed before the OTA update database this will likely either just
155658
+** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
155659
+** constraint violations).
155660
+**
155661
+** If some client does modify the target database mid OTA update, or some
155662
+** other error occurs, the OTA extension will keep throwing errors. It's
155663
+** not really clear how to get out of this state. The system could just
155664
+** by delete the OTA update database and *-oal file and have the device
155665
+** download the update again and start over.
155666
+**
155667
+** At present, for an UPDATE, both the new.* and old.* records are
155668
+** collected in the ota_xyz table. And for both UPDATEs and DELETEs all
155669
+** fields are collected. This means we're probably writing a lot more
155670
+** data to disk when saving the state of an ongoing update to the OTA
155671
+** update database than is strictly necessary.
155672
+**
155673
+*/
155674
+
155675
+/* #include <assert.h> */
155676
+/* #include <string.h> */
155677
+/* #include <stdio.h> */
155678
+/* #include <unistd.h> */
155679
+
155680
+
155681
+#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_OTA)
155682
+/************** Include sqlite3ota.h in the middle of sqlite3ota.c ***********/
155683
+/************** Begin file sqlite3ota.h **************************************/
155684
+/*
155685
+** 2014 August 30
155686
+**
155687
+** The author disclaims copyright to this source code. In place of
155688
+** a legal notice, here is a blessing:
155689
+**
155690
+** May you do good and not evil.
155691
+** May you find forgiveness for yourself and forgive others.
155692
+** May you share freely, never taking more than you give.
155693
+**
155694
+*************************************************************************
155695
+**
155696
+** This file contains the public interface for the OTA extension.
155697
+*/
155698
+
155699
+/*
155700
+** SUMMARY
155701
+**
155702
+** Writing a transaction containing a large number of operations on
155703
+** b-tree indexes that are collectively larger than the available cache
155704
+** memory can be very inefficient.
155705
+**
155706
+** The problem is that in order to update a b-tree, the leaf page (at least)
155707
+** containing the entry being inserted or deleted must be modified. If the
155708
+** working set of leaves is larger than the available cache memory, then a
155709
+** single leaf that is modified more than once as part of the transaction
155710
+** may be loaded from or written to the persistent media multiple times.
155711
+** Additionally, because the index updates are likely to be applied in
155712
+** random order, access to pages within the database is also likely to be in
155713
+** random order, which is itself quite inefficient.
155714
+**
155715
+** One way to improve the situation is to sort the operations on each index
155716
+** by index key before applying them to the b-tree. This leads to an IO
155717
+** pattern that resembles a single linear scan through the index b-tree,
155718
+** and all but guarantees each modified leaf page is loaded and stored
155719
+** exactly once. SQLite uses this trick to improve the performance of
155720
+** CREATE INDEX commands. This extension allows it to be used to improve
155721
+** the performance of large transactions on existing databases.
155722
+**
155723
+** Additionally, this extension allows the work involved in writing the
155724
+** large transaction to be broken down into sub-transactions performed
155725
+** sequentially by separate processes. This is useful if the system cannot
155726
+** guarantee that a single update process will run for long enough to apply
155727
+** the entire update, for example because the update is being applied on a
155728
+** mobile device that is frequently rebooted. Even after the writer process
155729
+** has committed one or more sub-transactions, other database clients continue
155730
+** to read from the original database snapshot. In other words, partially
155731
+** applied transactions are not visible to other clients.
155732
+**
155733
+** "OTA" stands for "Over The Air" update. As in a large database update
155734
+** transmitted via a wireless network to a mobile device. A transaction
155735
+** applied using this extension is hence refered to as an "OTA update".
155736
+**
155737
+**
155738
+** LIMITATIONS
155739
+**
155740
+** An "OTA update" transaction is subject to the following limitations:
155741
+**
155742
+** * The transaction must consist of INSERT, UPDATE and DELETE operations
155743
+** only.
155744
+**
155745
+** * INSERT statements may not use any default values.
155746
+**
155747
+** * UPDATE and DELETE statements must identify their target rows by
155748
+** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
155749
+** KEY fields may not be updated or deleted. If the table being written
155750
+** has no PRIMARY KEY, affected rows must be identified by rowid.
155751
+**
155752
+** * UPDATE statements may not modify PRIMARY KEY columns.
155753
+**
155754
+** * No triggers will be fired.
155755
+**
155756
+** * No foreign key violations are detected or reported.
155757
+**
155758
+** * CHECK constraints are not enforced.
155759
+**
155760
+** * No constraint handling mode except for "OR ROLLBACK" is supported.
155761
+**
155762
+**
155763
+** PREPARATION
155764
+**
155765
+** An "OTA update" is stored as a separate SQLite database. A database
155766
+** containing an OTA update is an "OTA database". For each table in the
155767
+** target database to be updated, the OTA database should contain a table
155768
+** named "data_<target name>" containing the same set of columns as the
155769
+** target table, and one more - "ota_control". The data_% table should
155770
+** have no PRIMARY KEY or UNIQUE constraints, but each column should have
155771
+** the same type as the corresponding column in the target database.
155772
+** The "ota_control" column should have no type at all. For example, if
155773
+** the target database contains:
155774
+**
155775
+** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
155776
+**
155777
+** Then the OTA database should contain:
155778
+**
155779
+** CREATE TABLE data_t1(a INTEGER, b TEXT, c, ota_control);
155780
+**
155781
+** The order of the columns in the data_% table does not matter.
155782
+**
155783
+** If the target database table is a virtual table or a table that has no
155784
+** PRIMARY KEY declaration, the data_% table must also contain a column
155785
+** named "ota_rowid". This column is mapped to the tables implicit primary
155786
+** key column - "rowid". Virtual tables for which the "rowid" column does
155787
+** not function like a primary key value cannot be updated using OTA. For
155788
+** example, if the target db contains either of the following:
155789
+**
155790
+** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
155791
+** CREATE TABLE x1(a, b)
155792
+**
155793
+** then the OTA database should contain:
155794
+**
155795
+** CREATE TABLE data_x1(a, b, ota_rowid, ota_control);
155796
+**
155797
+** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
155798
+** target table must be present in the input table. For virtual tables,
155799
+** hidden columns are optional - they are updated by OTA if present in
155800
+** the input table, or not otherwise. For example, to write to an fts4
155801
+** table with a hidden languageid column such as:
155802
+**
155803
+** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
155804
+**
155805
+** Either of the following input table schemas may be used:
155806
+**
155807
+** CREATE TABLE data_ft1(a, b, langid, ota_rowid, ota_control);
155808
+** CREATE TABLE data_ft1(a, b, ota_rowid, ota_control);
155809
+**
155810
+** For each row to INSERT into the target database as part of the OTA
155811
+** update, the corresponding data_% table should contain a single record
155812
+** with the "ota_control" column set to contain integer value 0. The
155813
+** other columns should be set to the values that make up the new record
155814
+** to insert.
155815
+**
155816
+** If the target database table has an INTEGER PRIMARY KEY, it is not
155817
+** possible to insert a NULL value into the IPK column. Attempting to
155818
+** do so results in an SQLITE_MISMATCH error.
155819
+**
155820
+** For each row to DELETE from the target database as part of the OTA
155821
+** update, the corresponding data_% table should contain a single record
155822
+** with the "ota_control" column set to contain integer value 1. The
155823
+** real primary key values of the row to delete should be stored in the
155824
+** corresponding columns of the data_% table. The values stored in the
155825
+** other columns are not used.
155826
+**
155827
+** For each row to UPDATE from the target database as part of the OTA
155828
+** update, the corresponding data_% table should contain a single record
155829
+** with the "ota_control" column set to contain a value of type text.
155830
+** The real primary key values identifying the row to update should be
155831
+** stored in the corresponding columns of the data_% table row, as should
155832
+** the new values of all columns being update. The text value in the
155833
+** "ota_control" column must contain the same number of characters as
155834
+** there are columns in the target database table, and must consist entirely
155835
+** of 'x' and '.' characters (or in some special cases 'd' - see below). For
155836
+** each column that is being updated, the corresponding character is set to
155837
+** 'x'. For those that remain as they are, the corresponding character of the
155838
+** ota_control value should be set to '.'. For example, given the tables
155839
+** above, the update statement:
155840
+**
155841
+** UPDATE t1 SET c = 'usa' WHERE a = 4;
155842
+**
155843
+** is represented by the data_t1 row created by:
155844
+**
155845
+** INSERT INTO data_t1(a, b, c, ota_control) VALUES(4, NULL, 'usa', '..x');
155846
+**
155847
+** Instead of an 'x' character, characters of the ota_control value specified
155848
+** for UPDATEs may also be set to 'd'. In this case, instead of updating the
155849
+** target table with the value stored in the corresponding data_% column, the
155850
+** user-defined SQL function "ota_delta()" is invoked and the result stored in
155851
+** the target table column. ota_delta() is invoked with two arguments - the
155852
+** original value currently stored in the target table column and the
155853
+** value specified in the data_xxx table.
155854
+**
155855
+** For example, this row:
155856
+**
155857
+** INSERT INTO data_t1(a, b, c, ota_control) VALUES(4, NULL, 'usa', '..d');
155858
+**
155859
+** is similar to an UPDATE statement such as:
155860
+**
155861
+** UPDATE t1 SET c = ota_delta(c, 'usa') WHERE a = 4;
155862
+**
155863
+** If the target database table is a virtual table or a table with no PRIMARY
155864
+** KEY, the ota_control value should not include a character corresponding
155865
+** to the ota_rowid value. For example, this:
155866
+**
155867
+** INSERT INTO data_ft1(a, b, ota_rowid, ota_control)
155868
+** VALUES(NULL, 'usa', 12, '.x');
155869
+**
155870
+** causes a result similar to:
155871
+**
155872
+** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
155873
+**
155874
+** The data_xxx tables themselves should have no PRIMARY KEY declarations.
155875
+** However, OTA is more efficient if reading the rows in from each data_xxx
155876
+** table in "rowid" order is roughly the same as reading them sorted by
155877
+** the PRIMARY KEY of the corresponding target database table. In other
155878
+** words, rows should be sorted using the destination table PRIMARY KEY
155879
+** fields before they are inserted into the data_xxx tables.
155880
+**
155881
+** USAGE
155882
+**
155883
+** The API declared below allows an application to apply an OTA update
155884
+** stored on disk to an existing target database. Essentially, the
155885
+** application:
155886
+**
155887
+** 1) Opens an OTA handle using the sqlite3ota_open() function.
155888
+**
155889
+** 2) Registers any required virtual table modules with the database
155890
+** handle returned by sqlite3ota_db(). Also, if required, register
155891
+** the ota_delta() implementation.
155892
+**
155893
+** 3) Calls the sqlite3ota_step() function one or more times on
155894
+** the new handle. Each call to sqlite3ota_step() performs a single
155895
+** b-tree operation, so thousands of calls may be required to apply
155896
+** a complete update.
155897
+**
155898
+** 4) Calls sqlite3ota_close() to close the OTA update handle. If
155899
+** sqlite3ota_step() has been called enough times to completely
155900
+** apply the update to the target database, then the OTA database
155901
+** is marked as fully applied. Otherwise, the state of the OTA
155902
+** update application is saved in the OTA database for later
155903
+** resumption.
155904
+**
155905
+** See comments below for more detail on APIs.
155906
+**
155907
+** If an update is only partially applied to the target database by the
155908
+** time sqlite3ota_close() is called, various state information is saved
155909
+** within the OTA database. This allows subsequent processes to automatically
155910
+** resume the OTA update from where it left off.
155911
+**
155912
+** To remove all OTA extension state information, returning an OTA database
155913
+** to its original contents, it is sufficient to drop all tables that begin
155914
+** with the prefix "ota_"
155915
+**
155916
+** DATABASE LOCKING
155917
+**
155918
+** An OTA update may not be applied to a database in WAL mode. Attempting
155919
+** to do so is an error (SQLITE_ERROR).
155920
+**
155921
+** While an OTA handle is open, a SHARED lock may be held on the target
155922
+** database file. This means it is possible for other clients to read the
155923
+** database, but not to write it.
155924
+**
155925
+** If an OTA update is started and then suspended before it is completed,
155926
+** then an external client writes to the database, then attempting to resume
155927
+** the suspended OTA update is also an error (SQLITE_BUSY).
155928
+*/
155929
+
155930
+#ifndef _SQLITE3OTA_H
155931
+#define _SQLITE3OTA_H
155932
+
155933
+
155934
+typedef struct sqlite3ota sqlite3ota;
155935
+
155936
+/*
155937
+** Open an OTA handle.
155938
+**
155939
+** Argument zTarget is the path to the target database. Argument zOta is
155940
+** the path to the OTA database. Each call to this function must be matched
155941
+** by a call to sqlite3ota_close(). When opening the databases, OTA passes
155942
+** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
155943
+** or zOta begin with "file:", it will be interpreted as an SQLite
155944
+** database URI, not a regular file name.
155945
+**
155946
+** If the zState argument is passed a NULL value, the OTA extension stores
155947
+** the current state of the update (how many rows have been updated, which
155948
+** indexes are yet to be updated etc.) within the OTA database itself. This
155949
+** can be convenient, as it means that the OTA application does not need to
155950
+** organize removing a separate state file after the update is concluded.
155951
+** Or, if zState is non-NULL, it must be a path to a database file in which
155952
+** the OTA extension can store the state of the update.
155953
+**
155954
+** When resuming an OTA update, the zState argument must be passed the same
155955
+** value as when the OTA update was started.
155956
+**
155957
+** Once the OTA update is finished, the OTA extension does not
155958
+** automatically remove any zState database file, even if it created it.
155959
+**
155960
+** By default, OTA uses the default VFS to access the files on disk. To
155961
+** use a VFS other than the default, an SQLite "file:" URI containing a
155962
+** "vfs=..." option may be passed as the zTarget option.
155963
+**
155964
+** IMPORTANT NOTE FOR ZIPVFS USERS: The OTA extension works with all of
155965
+** SQLite's built-in VFSs, including the multiplexor VFS. However it does
155966
+** not work out of the box with zipvfs. Refer to the comment describing
155967
+** the zipvfs_create_vfs() API below for details on using OTA with zipvfs.
155968
+*/
155969
+SQLITE_API sqlite3ota *SQLITE_STDCALL sqlite3ota_open(
155970
+ const char *zTarget,
155971
+ const char *zOta,
155972
+ const char *zState
155973
+);
155974
+
155975
+/*
155976
+** Internally, each OTA connection uses a separate SQLite database
155977
+** connection to access the target and ota update databases. This
155978
+** API allows the application direct access to these database handles.
155979
+**
155980
+** The first argument passed to this function must be a valid, open, OTA
155981
+** handle. The second argument should be passed zero to access the target
155982
+** database handle, or non-zero to access the ota update database handle.
155983
+** Accessing the underlying database handles may be useful in the
155984
+** following scenarios:
155985
+**
155986
+** * If any target tables are virtual tables, it may be necessary to
155987
+** call sqlite3_create_module() on the target database handle to
155988
+** register the required virtual table implementations.
155989
+**
155990
+** * If the data_xxx tables in the OTA source database are virtual
155991
+** tables, the application may need to call sqlite3_create_module() on
155992
+** the ota update db handle to any required virtual table
155993
+** implementations.
155994
+**
155995
+** * If the application uses the "ota_delta()" feature described above,
155996
+** it must use sqlite3_create_function() or similar to register the
155997
+** ota_delta() implementation with the target database handle.
155998
+**
155999
+** If an error has occurred, either while opening or stepping the OTA object,
156000
+** this function may return NULL. The error code and message may be collected
156001
+** when sqlite3ota_close() is called.
156002
+*/
156003
+SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3ota_db(sqlite3ota*, int bOta);
156004
+
156005
+/*
156006
+** Do some work towards applying the OTA update to the target db.
156007
+**
156008
+** Return SQLITE_DONE if the update has been completely applied, or
156009
+** SQLITE_OK if no error occurs but there remains work to do to apply
156010
+** the OTA update. If an error does occur, some other error code is
156011
+** returned.
156012
+**
156013
+** Once a call to sqlite3ota_step() has returned a value other than
156014
+** SQLITE_OK, all subsequent calls on the same OTA handle are no-ops
156015
+** that immediately return the same value.
156016
+*/
156017
+SQLITE_API int SQLITE_STDCALL sqlite3ota_step(sqlite3ota *pOta);
156018
+
156019
+/*
156020
+** Close an OTA handle.
156021
+**
156022
+** If the OTA update has been completely applied, mark the OTA database
156023
+** as fully applied. Otherwise, assuming no error has occurred, save the
156024
+** current state of the OTA update appliation to the OTA database.
156025
+**
156026
+** If an error has already occurred as part of an sqlite3ota_step()
156027
+** or sqlite3ota_open() call, or if one occurs within this function, an
156028
+** SQLite error code is returned. Additionally, *pzErrmsg may be set to
156029
+** point to a buffer containing a utf-8 formatted English language error
156030
+** message. It is the responsibility of the caller to eventually free any
156031
+** such buffer using sqlite3_free().
156032
+**
156033
+** Otherwise, if no error occurs, this function returns SQLITE_OK if the
156034
+** update has been partially applied, or SQLITE_DONE if it has been
156035
+** completely applied.
156036
+*/
156037
+SQLITE_API int SQLITE_STDCALL sqlite3ota_close(sqlite3ota *pOta, char **pzErrmsg);
156038
+
156039
+/*
156040
+** Return the total number of key-value operations (inserts, deletes or
156041
+** updates) that have been performed on the target database since the
156042
+** current OTA update was started.
156043
+*/
156044
+SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3ota_progress(sqlite3ota *pOta);
156045
+
156046
+/*
156047
+** Create an OTA VFS named zName that accesses the underlying file-system
156048
+** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
156049
+** then the new OTA VFS uses the default system VFS to access the file-system.
156050
+** The new object is registered as a non-default VFS with SQLite before
156051
+** returning.
156052
+**
156053
+** Part of the OTA implementation uses a custom VFS object. Usually, this
156054
+** object is created and deleted automatically by OTA.
156055
+**
156056
+** The exception is for applications that also use zipvfs. In this case,
156057
+** the custom VFS must be explicitly created by the user before the OTA
156058
+** handle is opened. The OTA VFS should be installed so that the zipvfs
156059
+** VFS uses the OTA VFS, which in turn uses any other VFS layers in use
156060
+** (for example multiplexor) to access the file-system. For example,
156061
+** to assemble an OTA enabled VFS stack that uses both zipvfs and
156062
+** multiplexor (error checking omitted):
156063
+**
156064
+** // Create a VFS named "multiplex" (not the default).
156065
+** sqlite3_multiplex_initialize(0, 0);
156066
+**
156067
+** // Create an ota VFS named "ota" that uses multiplexor. If the
156068
+** // second argument were replaced with NULL, the "ota" VFS would
156069
+** // access the file-system via the system default VFS, bypassing the
156070
+** // multiplexor.
156071
+** sqlite3ota_create_vfs("ota", "multiplex");
156072
+**
156073
+** // Create a zipvfs VFS named "zipvfs" that uses ota.
156074
+** zipvfs_create_vfs_v3("zipvfs", "ota", 0, xCompressorAlgorithmDetector);
156075
+**
156076
+** // Make zipvfs the default VFS.
156077
+** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
156078
+**
156079
+** Because the default VFS created above includes a OTA functionality, it
156080
+** may be used by OTA clients. Attempting to use OTA with a zipvfs VFS stack
156081
+** that does not include the OTA layer results in an error.
156082
+**
156083
+** The overhead of adding the "ota" VFS to the system is negligible for
156084
+** non-OTA users. There is no harm in an application accessing the
156085
+** file-system via "ota" all the time, even if it only uses OTA functionality
156086
+** occasionally.
156087
+*/
156088
+SQLITE_API int SQLITE_STDCALL sqlite3ota_create_vfs(const char *zName, const char *zParent);
156089
+
156090
+/*
156091
+** Deregister and destroy an OTA vfs created by an earlier call to
156092
+** sqlite3ota_create_vfs().
156093
+**
156094
+** VFS objects are not reference counted. If a VFS object is destroyed
156095
+** before all database handles that use it have been closed, the results
156096
+** are undefined.
156097
+*/
156098
+SQLITE_API void SQLITE_STDCALL sqlite3ota_destroy_vfs(const char *zName);
156099
+
156100
+#endif /* _SQLITE3OTA_H */
156101
+
156102
+
156103
+/************** End of sqlite3ota.h ******************************************/
156104
+/************** Continuing where we left off in sqlite3ota.c *****************/
156105
+
156106
+/* Maximum number of prepared UPDATE statements held by this module */
156107
+#define SQLITE_OTA_UPDATE_CACHESIZE 16
156108
+
156109
+/*
156110
+** Swap two objects of type TYPE.
156111
+*/
156112
+#if !defined(SQLITE_AMALGAMATION)
156113
+# define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
156114
+#endif
156115
+
156116
+/*
156117
+** The ota_state table is used to save the state of a partially applied
156118
+** update so that it can be resumed later. The table consists of integer
156119
+** keys mapped to values as follows:
156120
+**
156121
+** OTA_STATE_STAGE:
156122
+** May be set to integer values 1, 2, 4 or 5. As follows:
156123
+** 1: the *-ota file is currently under construction.
156124
+** 2: the *-ota file has been constructed, but not yet moved
156125
+** to the *-wal path.
156126
+** 4: the checkpoint is underway.
156127
+** 5: the ota update has been checkpointed.
156128
+**
156129
+** OTA_STATE_TBL:
156130
+** Only valid if STAGE==1. The target database name of the table
156131
+** currently being written.
156132
+**
156133
+** OTA_STATE_IDX:
156134
+** Only valid if STAGE==1. The target database name of the index
156135
+** currently being written, or NULL if the main table is currently being
156136
+** updated.
156137
+**
156138
+** OTA_STATE_ROW:
156139
+** Only valid if STAGE==1. Number of rows already processed for the current
156140
+** table/index.
156141
+**
156142
+** OTA_STATE_PROGRESS:
156143
+** Total number of sqlite3ota_step() calls made so far as part of this
156144
+** ota update.
156145
+**
156146
+** OTA_STATE_CKPT:
156147
+** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
156148
+** header created by recovering the *-wal file. This is used to detect
156149
+** cases when another client appends frames to the *-wal file in the
156150
+** middle of an incremental checkpoint (an incremental checkpoint cannot
156151
+** be continued if this happens).
156152
+**
156153
+** OTA_STATE_COOKIE:
156154
+** Valid if STAGE==1. The current change-counter cookie value in the
156155
+** target db file.
156156
+**
156157
+** OTA_STATE_OALSZ:
156158
+** Valid if STAGE==1. The size in bytes of the *-oal file.
156159
+*/
156160
+#define OTA_STATE_STAGE 1
156161
+#define OTA_STATE_TBL 2
156162
+#define OTA_STATE_IDX 3
156163
+#define OTA_STATE_ROW 4
156164
+#define OTA_STATE_PROGRESS 5
156165
+#define OTA_STATE_CKPT 6
156166
+#define OTA_STATE_COOKIE 7
156167
+#define OTA_STATE_OALSZ 8
156168
+
156169
+#define OTA_STAGE_OAL 1
156170
+#define OTA_STAGE_MOVE 2
156171
+#define OTA_STAGE_CAPTURE 3
156172
+#define OTA_STAGE_CKPT 4
156173
+#define OTA_STAGE_DONE 5
156174
+
156175
+
156176
+#define OTA_CREATE_STATE \
156177
+ "CREATE TABLE IF NOT EXISTS %s.ota_state(k INTEGER PRIMARY KEY, v)"
156178
+
156179
+typedef struct OtaFrame OtaFrame;
156180
+typedef struct OtaObjIter OtaObjIter;
156181
+typedef struct OtaState OtaState;
156182
+typedef struct ota_vfs ota_vfs;
156183
+typedef struct ota_file ota_file;
156184
+typedef struct OtaUpdateStmt OtaUpdateStmt;
156185
+
156186
+#if !defined(SQLITE_AMALGAMATION)
156187
+typedef unsigned int u32;
156188
+typedef unsigned char u8;
156189
+typedef sqlite3_int64 i64;
156190
+#endif
156191
+
156192
+/*
156193
+** These values must match the values defined in wal.c for the equivalent
156194
+** locks. These are not magic numbers as they are part of the SQLite file
156195
+** format.
156196
+*/
156197
+#define WAL_LOCK_WRITE 0
156198
+#define WAL_LOCK_CKPT 1
156199
+#define WAL_LOCK_READ0 3
156200
+
156201
+/*
156202
+** A structure to store values read from the ota_state table in memory.
156203
+*/
156204
+struct OtaState {
156205
+ int eStage;
156206
+ char *zTbl;
156207
+ char *zIdx;
156208
+ i64 iWalCksum;
156209
+ int nRow;
156210
+ i64 nProgress;
156211
+ u32 iCookie;
156212
+ i64 iOalSz;
156213
+};
156214
+
156215
+struct OtaUpdateStmt {
156216
+ char *zMask; /* Copy of update mask used with pUpdate */
156217
+ sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
156218
+ OtaUpdateStmt *pNext;
156219
+};
156220
+
156221
+/*
156222
+** An iterator of this type is used to iterate through all objects in
156223
+** the target database that require updating. For each such table, the
156224
+** iterator visits, in order:
156225
+**
156226
+** * the table itself,
156227
+** * each index of the table (zero or more points to visit), and
156228
+** * a special "cleanup table" state.
156229
+**
156230
+** abIndexed:
156231
+** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
156232
+** it points to an array of flags nTblCol elements in size. The flag is
156233
+** set for each column that is either a part of the PK or a part of an
156234
+** index. Or clear otherwise.
156235
+**
156236
+*/
156237
+struct OtaObjIter {
156238
+ sqlite3_stmt *pTblIter; /* Iterate through tables */
156239
+ sqlite3_stmt *pIdxIter; /* Index iterator */
156240
+ int nTblCol; /* Size of azTblCol[] array */
156241
+ char **azTblCol; /* Array of unquoted target column names */
156242
+ char **azTblType; /* Array of target column types */
156243
+ int *aiSrcOrder; /* src table col -> target table col */
156244
+ u8 *abTblPk; /* Array of flags, set on target PK columns */
156245
+ u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
156246
+ u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
156247
+ int eType; /* Table type - an OTA_PK_XXX value */
156248
+
156249
+ /* Output variables. zTbl==0 implies EOF. */
156250
+ int bCleanup; /* True in "cleanup" state */
156251
+ const char *zTbl; /* Name of target db table */
156252
+ const char *zIdx; /* Name of target db index (or null) */
156253
+ int iTnum; /* Root page of current object */
156254
+ int iPkTnum; /* If eType==EXTERNAL, root of PK index */
156255
+ int bUnique; /* Current index is unique */
156256
+
156257
+ /* Statements created by otaObjIterPrepareAll() */
156258
+ int nCol; /* Number of columns in current object */
156259
+ sqlite3_stmt *pSelect; /* Source data */
156260
+ sqlite3_stmt *pInsert; /* Statement for INSERT operations */
156261
+ sqlite3_stmt *pDelete; /* Statement for DELETE ops */
156262
+ sqlite3_stmt *pTmpInsert; /* Insert into ota_tmp_$zTbl */
156263
+
156264
+ /* Last UPDATE used (for PK b-tree updates only), or NULL. */
156265
+ OtaUpdateStmt *pOtaUpdate;
156266
+};
156267
+
156268
+/*
156269
+** Values for OtaObjIter.eType
156270
+**
156271
+** 0: Table does not exist (error)
156272
+** 1: Table has an implicit rowid.
156273
+** 2: Table has an explicit IPK column.
156274
+** 3: Table has an external PK index.
156275
+** 4: Table is WITHOUT ROWID.
156276
+** 5: Table is a virtual table.
156277
+*/
156278
+#define OTA_PK_NOTABLE 0
156279
+#define OTA_PK_NONE 1
156280
+#define OTA_PK_IPK 2
156281
+#define OTA_PK_EXTERNAL 3
156282
+#define OTA_PK_WITHOUT_ROWID 4
156283
+#define OTA_PK_VTAB 5
156284
+
156285
+
156286
+/*
156287
+** Within the OTA_STAGE_OAL stage, each call to sqlite3ota_step() performs
156288
+** one of the following operations.
156289
+*/
156290
+#define OTA_INSERT 1 /* Insert on a main table b-tree */
156291
+#define OTA_DELETE 2 /* Delete a row from a main table b-tree */
156292
+#define OTA_IDX_DELETE 3 /* Delete a row from an aux. index b-tree */
156293
+#define OTA_IDX_INSERT 4 /* Insert on an aux. index b-tree */
156294
+#define OTA_UPDATE 5 /* Update a row in a main table b-tree */
156295
+
156296
+
156297
+/*
156298
+** A single step of an incremental checkpoint - frame iWalFrame of the wal
156299
+** file should be copied to page iDbPage of the database file.
156300
+*/
156301
+struct OtaFrame {
156302
+ u32 iDbPage;
156303
+ u32 iWalFrame;
156304
+};
156305
+
156306
+/*
156307
+** OTA handle.
156308
+*/
156309
+struct sqlite3ota {
156310
+ int eStage; /* Value of OTA_STATE_STAGE field */
156311
+ sqlite3 *dbMain; /* target database handle */
156312
+ sqlite3 *dbOta; /* ota database handle */
156313
+ char *zTarget; /* Path to target db */
156314
+ char *zOta; /* Path to ota db */
156315
+ char *zState; /* Path to state db (or NULL if zOta) */
156316
+ char zStateDb[5]; /* Db name for state ("stat" or "main") */
156317
+ int rc; /* Value returned by last ota_step() call */
156318
+ char *zErrmsg; /* Error message if rc!=SQLITE_OK */
156319
+ int nStep; /* Rows processed for current object */
156320
+ int nProgress; /* Rows processed for all objects */
156321
+ OtaObjIter objiter; /* Iterator for skipping through tbl/idx */
156322
+ const char *zVfsName; /* Name of automatically created ota vfs */
156323
+ ota_file *pTargetFd; /* File handle open on target db */
156324
+ i64 iOalSz;
156325
+
156326
+ /* The following state variables are used as part of the incremental
156327
+ ** checkpoint stage (eStage==OTA_STAGE_CKPT). See comments surrounding
156328
+ ** function otaSetupCheckpoint() for details. */
156329
+ u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
156330
+ u32 mLock;
156331
+ int nFrame; /* Entries in aFrame[] array */
156332
+ int nFrameAlloc; /* Allocated size of aFrame[] array */
156333
+ OtaFrame *aFrame;
156334
+ int pgsz;
156335
+ u8 *aBuf;
156336
+ i64 iWalCksum;
156337
+};
156338
+
156339
+/*
156340
+** An ota VFS is implemented using an instance of this structure.
156341
+*/
156342
+struct ota_vfs {
156343
+ sqlite3_vfs base; /* ota VFS shim methods */
156344
+ sqlite3_vfs *pRealVfs; /* Underlying VFS */
156345
+ sqlite3_mutex *mutex; /* Mutex to protect pMain */
156346
+ ota_file *pMain; /* Linked list of main db files */
156347
+};
156348
+
156349
+/*
156350
+** Each file opened by an ota VFS is represented by an instance of
156351
+** the following structure.
156352
+*/
156353
+struct ota_file {
156354
+ sqlite3_file base; /* sqlite3_file methods */
156355
+ sqlite3_file *pReal; /* Underlying file handle */
156356
+ ota_vfs *pOtaVfs; /* Pointer to the ota_vfs object */
156357
+ sqlite3ota *pOta; /* Pointer to ota object (ota target only) */
156358
+
156359
+ int openFlags; /* Flags this file was opened with */
156360
+ u32 iCookie; /* Cookie value for main db files */
156361
+ u8 iWriteVer; /* "write-version" value for main db files */
156362
+
156363
+ int nShm; /* Number of entries in apShm[] array */
156364
+ char **apShm; /* Array of mmap'd *-shm regions */
156365
+ char *zDel; /* Delete this when closing file */
156366
+
156367
+ const char *zWal; /* Wal filename for this main db file */
156368
+ ota_file *pWalFd; /* Wal file descriptor for this main db */
156369
+ ota_file *pMainNext; /* Next MAIN_DB file */
156370
+};
156371
+
156372
+
156373
+/*
156374
+** Prepare the SQL statement in buffer zSql against database handle db.
156375
+** If successful, set *ppStmt to point to the new statement and return
156376
+** SQLITE_OK.
156377
+**
156378
+** Otherwise, if an error does occur, set *ppStmt to NULL and return
156379
+** an SQLite error code. Additionally, set output variable *pzErrmsg to
156380
+** point to a buffer containing an error message. It is the responsibility
156381
+** of the caller to (eventually) free this buffer using sqlite3_free().
156382
+*/
156383
+static int prepareAndCollectError(
156384
+ sqlite3 *db,
156385
+ sqlite3_stmt **ppStmt,
156386
+ char **pzErrmsg,
156387
+ const char *zSql
156388
+){
156389
+ int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
156390
+ if( rc!=SQLITE_OK ){
156391
+ *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
156392
+ *ppStmt = 0;
156393
+ }
156394
+ return rc;
156395
+}
156396
+
156397
+/*
156398
+** Reset the SQL statement passed as the first argument. Return a copy
156399
+** of the value returned by sqlite3_reset().
156400
+**
156401
+** If an error has occurred, then set *pzErrmsg to point to a buffer
156402
+** containing an error message. It is the responsibility of the caller
156403
+** to eventually free this buffer using sqlite3_free().
156404
+*/
156405
+static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
156406
+ int rc = sqlite3_reset(pStmt);
156407
+ if( rc!=SQLITE_OK ){
156408
+ *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
156409
+ }
156410
+ return rc;
156411
+}
156412
+
156413
+/*
156414
+** Unless it is NULL, argument zSql points to a buffer allocated using
156415
+** sqlite3_malloc containing an SQL statement. This function prepares the SQL
156416
+** statement against database db and frees the buffer. If statement
156417
+** compilation is successful, *ppStmt is set to point to the new statement
156418
+** handle and SQLITE_OK is returned.
156419
+**
156420
+** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
156421
+** returned. In this case, *pzErrmsg may also be set to point to an error
156422
+** message. It is the responsibility of the caller to free this error message
156423
+** buffer using sqlite3_free().
156424
+**
156425
+** If argument zSql is NULL, this function assumes that an OOM has occurred.
156426
+** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
156427
+*/
156428
+static int prepareFreeAndCollectError(
156429
+ sqlite3 *db,
156430
+ sqlite3_stmt **ppStmt,
156431
+ char **pzErrmsg,
156432
+ char *zSql
156433
+){
156434
+ int rc;
156435
+ assert( *pzErrmsg==0 );
156436
+ if( zSql==0 ){
156437
+ rc = SQLITE_NOMEM;
156438
+ *ppStmt = 0;
156439
+ }else{
156440
+ rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
156441
+ sqlite3_free(zSql);
156442
+ }
156443
+ return rc;
156444
+}
156445
+
156446
+/*
156447
+** Free the OtaObjIter.azTblCol[] and OtaObjIter.abTblPk[] arrays allocated
156448
+** by an earlier call to otaObjIterCacheTableInfo().
156449
+*/
156450
+static void otaObjIterFreeCols(OtaObjIter *pIter){
156451
+ int i;
156452
+ for(i=0; i<pIter->nTblCol; i++){
156453
+ sqlite3_free(pIter->azTblCol[i]);
156454
+ sqlite3_free(pIter->azTblType[i]);
156455
+ }
156456
+ sqlite3_free(pIter->azTblCol);
156457
+ pIter->azTblCol = 0;
156458
+ pIter->azTblType = 0;
156459
+ pIter->aiSrcOrder = 0;
156460
+ pIter->abTblPk = 0;
156461
+ pIter->abNotNull = 0;
156462
+ pIter->nTblCol = 0;
156463
+ pIter->eType = 0; /* Invalid value */
156464
+}
156465
+
156466
+/*
156467
+** Finalize all statements and free all allocations that are specific to
156468
+** the current object (table/index pair).
156469
+*/
156470
+static void otaObjIterClearStatements(OtaObjIter *pIter){
156471
+ OtaUpdateStmt *pUp;
156472
+
156473
+ sqlite3_finalize(pIter->pSelect);
156474
+ sqlite3_finalize(pIter->pInsert);
156475
+ sqlite3_finalize(pIter->pDelete);
156476
+ sqlite3_finalize(pIter->pTmpInsert);
156477
+ pUp = pIter->pOtaUpdate;
156478
+ while( pUp ){
156479
+ OtaUpdateStmt *pTmp = pUp->pNext;
156480
+ sqlite3_finalize(pUp->pUpdate);
156481
+ sqlite3_free(pUp);
156482
+ pUp = pTmp;
156483
+ }
156484
+
156485
+ pIter->pSelect = 0;
156486
+ pIter->pInsert = 0;
156487
+ pIter->pDelete = 0;
156488
+ pIter->pOtaUpdate = 0;
156489
+ pIter->pTmpInsert = 0;
156490
+ pIter->nCol = 0;
156491
+}
156492
+
156493
+/*
156494
+** Clean up any resources allocated as part of the iterator object passed
156495
+** as the only argument.
156496
+*/
156497
+static void otaObjIterFinalize(OtaObjIter *pIter){
156498
+ otaObjIterClearStatements(pIter);
156499
+ sqlite3_finalize(pIter->pTblIter);
156500
+ sqlite3_finalize(pIter->pIdxIter);
156501
+ otaObjIterFreeCols(pIter);
156502
+ memset(pIter, 0, sizeof(OtaObjIter));
156503
+}
156504
+
156505
+/*
156506
+** Advance the iterator to the next position.
156507
+**
156508
+** If no error occurs, SQLITE_OK is returned and the iterator is left
156509
+** pointing to the next entry. Otherwise, an error code and message is
156510
+** left in the OTA handle passed as the first argument. A copy of the
156511
+** error code is returned.
156512
+*/
156513
+static int otaObjIterNext(sqlite3ota *p, OtaObjIter *pIter){
156514
+ int rc = p->rc;
156515
+ if( rc==SQLITE_OK ){
156516
+
156517
+ /* Free any SQLite statements used while processing the previous object */
156518
+ otaObjIterClearStatements(pIter);
156519
+ if( pIter->zIdx==0 ){
156520
+ rc = sqlite3_exec(p->dbMain,
156521
+ "DROP TRIGGER IF EXISTS temp.ota_insert_tr;"
156522
+ "DROP TRIGGER IF EXISTS temp.ota_update1_tr;"
156523
+ "DROP TRIGGER IF EXISTS temp.ota_update2_tr;"
156524
+ "DROP TRIGGER IF EXISTS temp.ota_delete_tr;"
156525
+ , 0, 0, &p->zErrmsg
156526
+ );
156527
+ }
156528
+
156529
+ if( rc==SQLITE_OK ){
156530
+ if( pIter->bCleanup ){
156531
+ otaObjIterFreeCols(pIter);
156532
+ pIter->bCleanup = 0;
156533
+ rc = sqlite3_step(pIter->pTblIter);
156534
+ if( rc!=SQLITE_ROW ){
156535
+ rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
156536
+ pIter->zTbl = 0;
156537
+ }else{
156538
+ pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
156539
+ rc = pIter->zTbl ? SQLITE_OK : SQLITE_NOMEM;
156540
+ }
156541
+ }else{
156542
+ if( pIter->zIdx==0 ){
156543
+ sqlite3_stmt *pIdx = pIter->pIdxIter;
156544
+ rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
156545
+ }
156546
+ if( rc==SQLITE_OK ){
156547
+ rc = sqlite3_step(pIter->pIdxIter);
156548
+ if( rc!=SQLITE_ROW ){
156549
+ rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
156550
+ pIter->bCleanup = 1;
156551
+ pIter->zIdx = 0;
156552
+ }else{
156553
+ pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
156554
+ pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
156555
+ pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
156556
+ rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
156557
+ }
156558
+ }
156559
+ }
156560
+ }
156561
+ }
156562
+
156563
+ if( rc!=SQLITE_OK ){
156564
+ otaObjIterFinalize(pIter);
156565
+ p->rc = rc;
156566
+ }
156567
+ return rc;
156568
+}
156569
+
156570
+/*
156571
+** Initialize the iterator structure passed as the second argument.
156572
+**
156573
+** If no error occurs, SQLITE_OK is returned and the iterator is left
156574
+** pointing to the first entry. Otherwise, an error code and message is
156575
+** left in the OTA handle passed as the first argument. A copy of the
156576
+** error code is returned.
156577
+*/
156578
+static int otaObjIterFirst(sqlite3ota *p, OtaObjIter *pIter){
156579
+ int rc;
156580
+ memset(pIter, 0, sizeof(OtaObjIter));
156581
+
156582
+ rc = prepareAndCollectError(p->dbOta, &pIter->pTblIter, &p->zErrmsg,
156583
+ "SELECT substr(name, 6) FROM sqlite_master "
156584
+ "WHERE type='table' AND name LIKE 'data_%'"
156585
+ );
156586
+
156587
+ if( rc==SQLITE_OK ){
156588
+ rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
156589
+ "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
156590
+ " FROM main.sqlite_master "
156591
+ " WHERE type='index' AND tbl_name = ?"
156592
+ );
156593
+ }
156594
+
156595
+ pIter->bCleanup = 1;
156596
+ p->rc = rc;
156597
+ return otaObjIterNext(p, pIter);
156598
+}
156599
+
156600
+/*
156601
+** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
156602
+** an error code is stored in the OTA handle passed as the first argument.
156603
+**
156604
+** If an error has already occurred (p->rc is already set to something other
156605
+** than SQLITE_OK), then this function returns NULL without modifying the
156606
+** stored error code. In this case it still calls sqlite3_free() on any
156607
+** printf() parameters associated with %z conversions.
156608
+*/
156609
+static char *otaMPrintf(sqlite3ota *p, const char *zFmt, ...){
156610
+ char *zSql = 0;
156611
+ va_list ap;
156612
+ va_start(ap, zFmt);
156613
+ zSql = sqlite3_vmprintf(zFmt, ap);
156614
+ if( p->rc==SQLITE_OK ){
156615
+ if( zSql==0 ) p->rc = SQLITE_NOMEM;
156616
+ }else{
156617
+ sqlite3_free(zSql);
156618
+ zSql = 0;
156619
+ }
156620
+ va_end(ap);
156621
+ return zSql;
156622
+}
156623
+
156624
+/*
156625
+** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
156626
+** arguments are the usual subsitution values. This function performs
156627
+** the printf() style substitutions and executes the result as an SQL
156628
+** statement on the OTA handles database.
156629
+**
156630
+** If an error occurs, an error code and error message is stored in the
156631
+** OTA handle. If an error has already occurred when this function is
156632
+** called, it is a no-op.
156633
+*/
156634
+static int otaMPrintfExec(sqlite3ota *p, sqlite3 *db, const char *zFmt, ...){
156635
+ va_list ap;
156636
+ va_start(ap, zFmt);
156637
+ char *zSql = sqlite3_vmprintf(zFmt, ap);
156638
+ if( p->rc==SQLITE_OK ){
156639
+ if( zSql==0 ){
156640
+ p->rc = SQLITE_NOMEM;
156641
+ }else{
156642
+ p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
156643
+ }
156644
+ }
156645
+ sqlite3_free(zSql);
156646
+ va_end(ap);
156647
+ return p->rc;
156648
+}
156649
+
156650
+/*
156651
+** Attempt to allocate and return a pointer to a zeroed block of nByte
156652
+** bytes.
156653
+**
156654
+** If an error (i.e. an OOM condition) occurs, return NULL and leave an
156655
+** error code in the ota handle passed as the first argument. Or, if an
156656
+** error has already occurred when this function is called, return NULL
156657
+** immediately without attempting the allocation or modifying the stored
156658
+** error code.
156659
+*/
156660
+static void *otaMalloc(sqlite3ota *p, int nByte){
156661
+ void *pRet = 0;
156662
+ if( p->rc==SQLITE_OK ){
156663
+ assert( nByte>0 );
156664
+ pRet = sqlite3_malloc(nByte);
156665
+ if( pRet==0 ){
156666
+ p->rc = SQLITE_NOMEM;
156667
+ }else{
156668
+ memset(pRet, 0, nByte);
156669
+ }
156670
+ }
156671
+ return pRet;
156672
+}
156673
+
156674
+
156675
+/*
156676
+** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
156677
+** there is room for at least nCol elements. If an OOM occurs, store an
156678
+** error code in the OTA handle passed as the first argument.
156679
+*/
156680
+static void otaAllocateIterArrays(sqlite3ota *p, OtaObjIter *pIter, int nCol){
156681
+ int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
156682
+ char **azNew;
156683
+
156684
+ azNew = (char**)otaMalloc(p, nByte);
156685
+ if( azNew ){
156686
+ pIter->azTblCol = azNew;
156687
+ pIter->azTblType = &azNew[nCol];
156688
+ pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
156689
+ pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
156690
+ pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
156691
+ pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
156692
+ }
156693
+}
156694
+
156695
+/*
156696
+** The first argument must be a nul-terminated string. This function
156697
+** returns a copy of the string in memory obtained from sqlite3_malloc().
156698
+** It is the responsibility of the caller to eventually free this memory
156699
+** using sqlite3_free().
156700
+**
156701
+** If an OOM condition is encountered when attempting to allocate memory,
156702
+** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
156703
+** if the allocation succeeds, (*pRc) is left unchanged.
156704
+*/
156705
+static char *otaStrndup(const char *zStr, int *pRc){
156706
+ char *zRet = 0;
156707
+
156708
+ assert( *pRc==SQLITE_OK );
156709
+ if( zStr ){
156710
+ int nCopy = strlen(zStr) + 1;
156711
+ zRet = (char*)sqlite3_malloc(nCopy);
156712
+ if( zRet ){
156713
+ memcpy(zRet, zStr, nCopy);
156714
+ }else{
156715
+ *pRc = SQLITE_NOMEM;
156716
+ }
156717
+ }
156718
+
156719
+ return zRet;
156720
+}
156721
+
156722
+/*
156723
+** Finalize the statement passed as the second argument.
156724
+**
156725
+** If the sqlite3_finalize() call indicates that an error occurs, and the
156726
+** ota handle error code is not already set, set the error code and error
156727
+** message accordingly.
156728
+*/
156729
+static void otaFinalize(sqlite3ota *p, sqlite3_stmt *pStmt){
156730
+ sqlite3 *db = sqlite3_db_handle(pStmt);
156731
+ int rc = sqlite3_finalize(pStmt);
156732
+ if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
156733
+ p->rc = rc;
156734
+ p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
156735
+ }
156736
+}
156737
+
156738
+/* Determine the type of a table.
156739
+**
156740
+** peType is of type (int*), a pointer to an output parameter of type
156741
+** (int). This call sets the output parameter as follows, depending
156742
+** on the type of the table specified by parameters dbName and zTbl.
156743
+**
156744
+** OTA_PK_NOTABLE: No such table.
156745
+** OTA_PK_NONE: Table has an implicit rowid.
156746
+** OTA_PK_IPK: Table has an explicit IPK column.
156747
+** OTA_PK_EXTERNAL: Table has an external PK index.
156748
+** OTA_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
156749
+** OTA_PK_VTAB: Table is a virtual table.
156750
+**
156751
+** Argument *piPk is also of type (int*), and also points to an output
156752
+** parameter. Unless the table has an external primary key index
156753
+** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
156754
+** if the table does have an external primary key index, then *piPk
156755
+** is set to the root page number of the primary key index before
156756
+** returning.
156757
+**
156758
+** ALGORITHM:
156759
+**
156760
+** if( no entry exists in sqlite_master ){
156761
+** return OTA_PK_NOTABLE
156762
+** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
156763
+** return OTA_PK_VTAB
156764
+** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
156765
+** if( the index that is the pk exists in sqlite_master ){
156766
+** *piPK = rootpage of that index.
156767
+** return OTA_PK_EXTERNAL
156768
+** }else{
156769
+** return OTA_PK_WITHOUT_ROWID
156770
+** }
156771
+** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
156772
+** return OTA_PK_IPK
156773
+** }else{
156774
+** return OTA_PK_NONE
156775
+** }
156776
+*/
156777
+static void otaTableType(
156778
+ sqlite3ota *p,
156779
+ const char *zTab,
156780
+ int *peType,
156781
+ int *piTnum,
156782
+ int *piPk
156783
+){
156784
+ /*
156785
+ ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
156786
+ ** 1) PRAGMA index_list = ?
156787
+ ** 2) SELECT count(*) FROM sqlite_master where name=%Q
156788
+ ** 3) PRAGMA table_info = ?
156789
+ */
156790
+ sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
156791
+
156792
+ *peType = OTA_PK_NOTABLE;
156793
+ *piPk = 0;
156794
+
156795
+ assert( p->rc==SQLITE_OK );
156796
+ p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
156797
+ sqlite3_mprintf(
156798
+ "SELECT (sql LIKE 'create virtual%%'), rootpage"
156799
+ " FROM sqlite_master"
156800
+ " WHERE name=%Q", zTab
156801
+ ));
156802
+ if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
156803
+ /* Either an error, or no such table. */
156804
+ goto otaTableType_end;
156805
+ }
156806
+ if( sqlite3_column_int(aStmt[0], 0) ){
156807
+ *peType = OTA_PK_VTAB; /* virtual table */
156808
+ goto otaTableType_end;
156809
+ }
156810
+ *piTnum = sqlite3_column_int(aStmt[0], 1);
156811
+
156812
+ p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
156813
+ sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
156814
+ );
156815
+ if( p->rc ) goto otaTableType_end;
156816
+ while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
156817
+ const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
156818
+ const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
156819
+ if( zOrig && zIdx && zOrig[0]=='p' ){
156820
+ p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
156821
+ sqlite3_mprintf(
156822
+ "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
156823
+ ));
156824
+ if( p->rc==SQLITE_OK ){
156825
+ if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
156826
+ *piPk = sqlite3_column_int(aStmt[2], 0);
156827
+ *peType = OTA_PK_EXTERNAL;
156828
+ }else{
156829
+ *peType = OTA_PK_WITHOUT_ROWID;
156830
+ }
156831
+ }
156832
+ goto otaTableType_end;
156833
+ }
156834
+ }
156835
+
156836
+ p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
156837
+ sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
156838
+ );
156839
+ if( p->rc==SQLITE_OK ){
156840
+ while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
156841
+ if( sqlite3_column_int(aStmt[3],5)>0 ){
156842
+ *peType = OTA_PK_IPK; /* explicit IPK column */
156843
+ goto otaTableType_end;
156844
+ }
156845
+ }
156846
+ *peType = OTA_PK_NONE;
156847
+ }
156848
+
156849
+otaTableType_end: {
156850
+ int i;
156851
+ for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
156852
+ otaFinalize(p, aStmt[i]);
156853
+ }
156854
+ }
156855
+}
156856
+
156857
+/*
156858
+** This is a helper function for otaObjIterCacheTableInfo(). It populates
156859
+** the pIter->abIndexed[] array.
156860
+*/
156861
+static void otaObjIterCacheIndexedCols(sqlite3ota *p, OtaObjIter *pIter){
156862
+ sqlite3_stmt *pList = 0;
156863
+ int bIndex = 0;
156864
+
156865
+ if( p->rc==SQLITE_OK ){
156866
+ memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
156867
+ p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
156868
+ sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
156869
+ );
156870
+ }
156871
+
156872
+ while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
156873
+ const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
156874
+ sqlite3_stmt *pXInfo = 0;
156875
+ if( zIdx==0 ) break;
156876
+ p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
156877
+ sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
156878
+ );
156879
+ while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
156880
+ int iCid = sqlite3_column_int(pXInfo, 1);
156881
+ if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
156882
+ }
156883
+ otaFinalize(p, pXInfo);
156884
+ bIndex = 1;
156885
+ }
156886
+
156887
+ otaFinalize(p, pList);
156888
+ if( bIndex==0 ) pIter->abIndexed = 0;
156889
+}
156890
+
156891
+
156892
+/*
156893
+** If they are not already populated, populate the pIter->azTblCol[],
156894
+** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
156895
+** the table (not index) that the iterator currently points to.
156896
+**
156897
+** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
156898
+** an error does occur, an error code and error message are also left in
156899
+** the OTA handle.
156900
+*/
156901
+static int otaObjIterCacheTableInfo(sqlite3ota *p, OtaObjIter *pIter){
156902
+ if( pIter->azTblCol==0 ){
156903
+ sqlite3_stmt *pStmt = 0;
156904
+ int nCol = 0;
156905
+ int i; /* for() loop iterator variable */
156906
+ int bOtaRowid = 0; /* If input table has column "ota_rowid" */
156907
+ int iOrder = 0;
156908
+ int iTnum = 0;
156909
+
156910
+ /* Figure out the type of table this step will deal with. */
156911
+ assert( pIter->eType==0 );
156912
+ otaTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
156913
+ if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_NOTABLE ){
156914
+ p->rc = SQLITE_ERROR;
156915
+ p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
156916
+ }
156917
+ if( p->rc ) return p->rc;
156918
+ if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
156919
+
156920
+ assert( pIter->eType==OTA_PK_NONE || pIter->eType==OTA_PK_IPK
156921
+ || pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_WITHOUT_ROWID
156922
+ || pIter->eType==OTA_PK_VTAB
156923
+ );
156924
+
156925
+ /* Populate the azTblCol[] and nTblCol variables based on the columns
156926
+ ** of the input table. Ignore any input table columns that begin with
156927
+ ** "ota_". */
156928
+ p->rc = prepareFreeAndCollectError(p->dbOta, &pStmt, &p->zErrmsg,
156929
+ sqlite3_mprintf("SELECT * FROM 'data_%q'", pIter->zTbl)
156930
+ );
156931
+ if( p->rc==SQLITE_OK ){
156932
+ nCol = sqlite3_column_count(pStmt);
156933
+ otaAllocateIterArrays(p, pIter, nCol);
156934
+ }
156935
+ for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
156936
+ const char *zName = (const char*)sqlite3_column_name(pStmt, i);
156937
+ if( sqlite3_strnicmp("ota_", zName, 4) ){
156938
+ char *zCopy = otaStrndup(zName, &p->rc);
156939
+ pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
156940
+ pIter->azTblCol[pIter->nTblCol++] = zCopy;
156941
+ }
156942
+ else if( 0==sqlite3_stricmp("ota_rowid", zName) ){
156943
+ bOtaRowid = 1;
156944
+ }
156945
+ }
156946
+ sqlite3_finalize(pStmt);
156947
+ pStmt = 0;
156948
+
156949
+ if( p->rc==SQLITE_OK
156950
+ && bOtaRowid!=(pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
156951
+ ){
156952
+ p->rc = SQLITE_ERROR;
156953
+ p->zErrmsg = sqlite3_mprintf(
156954
+ "table data_%q %s ota_rowid column", pIter->zTbl,
156955
+ (bOtaRowid ? "may not have" : "requires")
156956
+ );
156957
+ }
156958
+
156959
+ /* Check that all non-HIDDEN columns in the destination table are also
156960
+ ** present in the input table. Populate the abTblPk[], azTblType[] and
156961
+ ** aiTblOrder[] arrays at the same time. */
156962
+ if( p->rc==SQLITE_OK ){
156963
+ p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
156964
+ sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
156965
+ );
156966
+ }
156967
+ while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
156968
+ const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
156969
+ if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
156970
+ for(i=iOrder; i<pIter->nTblCol; i++){
156971
+ if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
156972
+ }
156973
+ if( i==pIter->nTblCol ){
156974
+ p->rc = SQLITE_ERROR;
156975
+ p->zErrmsg = sqlite3_mprintf("column missing from data_%q: %s",
156976
+ pIter->zTbl, zName
156977
+ );
156978
+ }else{
156979
+ int iPk = sqlite3_column_int(pStmt, 5);
156980
+ int bNotNull = sqlite3_column_int(pStmt, 3);
156981
+ const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
156982
+
156983
+ if( i!=iOrder ){
156984
+ SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
156985
+ SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
156986
+ }
156987
+
156988
+ pIter->azTblType[iOrder] = otaStrndup(zType, &p->rc);
156989
+ pIter->abTblPk[iOrder] = (iPk!=0);
156990
+ pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
156991
+ iOrder++;
156992
+ }
156993
+ }
156994
+
156995
+ otaFinalize(p, pStmt);
156996
+ otaObjIterCacheIndexedCols(p, pIter);
156997
+ assert( pIter->eType!=OTA_PK_VTAB || pIter->abIndexed==0 );
156998
+ }
156999
+
157000
+ return p->rc;
157001
+}
157002
+
157003
+/*
157004
+** This function constructs and returns a pointer to a nul-terminated
157005
+** string containing some SQL clause or list based on one or more of the
157006
+** column names currently stored in the pIter->azTblCol[] array.
157007
+*/
157008
+static char *otaObjIterGetCollist(
157009
+ sqlite3ota *p, /* OTA object */
157010
+ OtaObjIter *pIter /* Object iterator for column names */
157011
+){
157012
+ char *zList = 0;
157013
+ const char *zSep = "";
157014
+ int i;
157015
+ for(i=0; i<pIter->nTblCol; i++){
157016
+ const char *z = pIter->azTblCol[i];
157017
+ zList = otaMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
157018
+ zSep = ", ";
157019
+ }
157020
+ return zList;
157021
+}
157022
+
157023
+/*
157024
+** This function is used to create a SELECT list (the list of SQL
157025
+** expressions that follows a SELECT keyword) for a SELECT statement
157026
+** used to read from an data_xxx or ota_tmp_xxx table while updating the
157027
+** index object currently indicated by the iterator object passed as the
157028
+** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
157029
+** to obtain the required information.
157030
+**
157031
+** If the index is of the following form:
157032
+**
157033
+** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
157034
+**
157035
+** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
157036
+** "ipk", the returned string is:
157037
+**
157038
+** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
157039
+**
157040
+** As well as the returned string, three other malloc'd strings are
157041
+** returned via output parameters. As follows:
157042
+**
157043
+** pzImposterCols: ...
157044
+** pzImposterPk: ...
157045
+** pzWhere: ...
157046
+*/
157047
+static char *otaObjIterGetIndexCols(
157048
+ sqlite3ota *p, /* OTA object */
157049
+ OtaObjIter *pIter, /* Object iterator for column names */
157050
+ char **pzImposterCols, /* OUT: Columns for imposter table */
157051
+ char **pzImposterPk, /* OUT: Imposter PK clause */
157052
+ char **pzWhere, /* OUT: WHERE clause */
157053
+ int *pnBind /* OUT: Total number of columns */
157054
+){
157055
+ int rc = p->rc; /* Error code */
157056
+ int rc2; /* sqlite3_finalize() return code */
157057
+ char *zRet = 0; /* String to return */
157058
+ char *zImpCols = 0; /* String to return via *pzImposterCols */
157059
+ char *zImpPK = 0; /* String to return via *pzImposterPK */
157060
+ char *zWhere = 0; /* String to return via *pzWhere */
157061
+ int nBind = 0; /* Value to return via *pnBind */
157062
+ const char *zCom = ""; /* Set to ", " later on */
157063
+ const char *zAnd = ""; /* Set to " AND " later on */
157064
+ sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */
157065
+
157066
+ if( rc==SQLITE_OK ){
157067
+ assert( p->zErrmsg==0 );
157068
+ rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
157069
+ sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
157070
+ );
157071
+ }
157072
+
157073
+ while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
157074
+ int iCid = sqlite3_column_int(pXInfo, 1);
157075
+ int bDesc = sqlite3_column_int(pXInfo, 3);
157076
+ const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
157077
+ const char *zCol;
157078
+ const char *zType;
157079
+
157080
+ if( iCid<0 ){
157081
+ /* An integer primary key. If the table has an explicit IPK, use
157082
+ ** its name. Otherwise, use "ota_rowid". */
157083
+ if( pIter->eType==OTA_PK_IPK ){
157084
+ int i;
157085
+ for(i=0; pIter->abTblPk[i]==0; i++);
157086
+ assert( i<pIter->nTblCol );
157087
+ zCol = pIter->azTblCol[i];
157088
+ }else{
157089
+ zCol = "ota_rowid";
157090
+ }
157091
+ zType = "INTEGER";
157092
+ }else{
157093
+ zCol = pIter->azTblCol[iCid];
157094
+ zType = pIter->azTblType[iCid];
157095
+ }
157096
+
157097
+ zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
157098
+ if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
157099
+ const char *zOrder = (bDesc ? " DESC" : "");
157100
+ zImpPK = sqlite3_mprintf("%z%s\"ota_imp_%d%w\"%s",
157101
+ zImpPK, zCom, nBind, zCol, zOrder
157102
+ );
157103
+ }
157104
+ zImpCols = sqlite3_mprintf("%z%s\"ota_imp_%d%w\" %s COLLATE %Q",
157105
+ zImpCols, zCom, nBind, zCol, zType, zCollate
157106
+ );
157107
+ zWhere = sqlite3_mprintf(
157108
+ "%z%s\"ota_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
157109
+ );
157110
+ if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
157111
+ zCom = ", ";
157112
+ zAnd = " AND ";
157113
+ nBind++;
157114
+ }
157115
+
157116
+ rc2 = sqlite3_finalize(pXInfo);
157117
+ if( rc==SQLITE_OK ) rc = rc2;
157118
+
157119
+ if( rc!=SQLITE_OK ){
157120
+ sqlite3_free(zRet);
157121
+ sqlite3_free(zImpCols);
157122
+ sqlite3_free(zImpPK);
157123
+ sqlite3_free(zWhere);
157124
+ zRet = 0;
157125
+ zImpCols = 0;
157126
+ zImpPK = 0;
157127
+ zWhere = 0;
157128
+ p->rc = rc;
157129
+ }
157130
+
157131
+ *pzImposterCols = zImpCols;
157132
+ *pzImposterPk = zImpPK;
157133
+ *pzWhere = zWhere;
157134
+ *pnBind = nBind;
157135
+ return zRet;
157136
+}
157137
+
157138
+/*
157139
+** Assuming the current table columns are "a", "b" and "c", and the zObj
157140
+** paramter is passed "old", return a string of the form:
157141
+**
157142
+** "old.a, old.b, old.b"
157143
+**
157144
+** With the column names escaped.
157145
+**
157146
+** For tables with implicit rowids - OTA_PK_EXTERNAL and OTA_PK_NONE, append
157147
+** the text ", old._rowid_" to the returned value.
157148
+*/
157149
+static char *otaObjIterGetOldlist(
157150
+ sqlite3ota *p,
157151
+ OtaObjIter *pIter,
157152
+ const char *zObj
157153
+){
157154
+ char *zList = 0;
157155
+ if( p->rc==SQLITE_OK && pIter->abIndexed ){
157156
+ const char *zS = "";
157157
+ int i;
157158
+ for(i=0; i<pIter->nTblCol; i++){
157159
+ if( pIter->abIndexed[i] ){
157160
+ const char *zCol = pIter->azTblCol[i];
157161
+ zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
157162
+ }else{
157163
+ zList = sqlite3_mprintf("%z%sNULL", zList, zS);
157164
+ }
157165
+ zS = ", ";
157166
+ if( zList==0 ){
157167
+ p->rc = SQLITE_NOMEM;
157168
+ break;
157169
+ }
157170
+ }
157171
+
157172
+ /* For a table with implicit rowids, append "old._rowid_" to the list. */
157173
+ if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
157174
+ zList = otaMPrintf(p, "%z, %s._rowid_", zList, zObj);
157175
+ }
157176
+ }
157177
+ return zList;
157178
+}
157179
+
157180
+/*
157181
+** Return an expression that can be used in a WHERE clause to match the
157182
+** primary key of the current table. For example, if the table is:
157183
+**
157184
+** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
157185
+**
157186
+** Return the string:
157187
+**
157188
+** "b = ?1 AND c = ?2"
157189
+*/
157190
+static char *otaObjIterGetWhere(
157191
+ sqlite3ota *p,
157192
+ OtaObjIter *pIter
157193
+){
157194
+ char *zList = 0;
157195
+ if( pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE ){
157196
+ zList = otaMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
157197
+ }else if( pIter->eType==OTA_PK_EXTERNAL ){
157198
+ const char *zSep = "";
157199
+ int i;
157200
+ for(i=0; i<pIter->nTblCol; i++){
157201
+ if( pIter->abTblPk[i] ){
157202
+ zList = otaMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
157203
+ zSep = " AND ";
157204
+ }
157205
+ }
157206
+ zList = otaMPrintf(p,
157207
+ "_rowid_ = (SELECT id FROM ota_imposter2 WHERE %z)", zList
157208
+ );
157209
+
157210
+ }else{
157211
+ const char *zSep = "";
157212
+ int i;
157213
+ for(i=0; i<pIter->nTblCol; i++){
157214
+ if( pIter->abTblPk[i] ){
157215
+ const char *zCol = pIter->azTblCol[i];
157216
+ zList = otaMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
157217
+ zSep = " AND ";
157218
+ }
157219
+ }
157220
+ }
157221
+ return zList;
157222
+}
157223
+
157224
+/*
157225
+** The SELECT statement iterating through the keys for the current object
157226
+** (p->objiter.pSelect) currently points to a valid row. However, there
157227
+** is something wrong with the ota_control value in the ota_control value
157228
+** stored in the (p->nCol+1)'th column. Set the error code and error message
157229
+** of the OTA handle to something reflecting this.
157230
+*/
157231
+static void otaBadControlError(sqlite3ota *p){
157232
+ p->rc = SQLITE_ERROR;
157233
+ p->zErrmsg = sqlite3_mprintf("invalid ota_control value");
157234
+}
157235
+
157236
+
157237
+/*
157238
+** Return a nul-terminated string containing the comma separated list of
157239
+** assignments that should be included following the "SET" keyword of
157240
+** an UPDATE statement used to update the table object that the iterator
157241
+** passed as the second argument currently points to if the ota_control
157242
+** column of the data_xxx table entry is set to zMask.
157243
+**
157244
+** The memory for the returned string is obtained from sqlite3_malloc().
157245
+** It is the responsibility of the caller to eventually free it using
157246
+** sqlite3_free().
157247
+**
157248
+** If an OOM error is encountered when allocating space for the new
157249
+** string, an error code is left in the ota handle passed as the first
157250
+** argument and NULL is returned. Or, if an error has already occurred
157251
+** when this function is called, NULL is returned immediately, without
157252
+** attempting the allocation or modifying the stored error code.
157253
+*/
157254
+static char *otaObjIterGetSetlist(
157255
+ sqlite3ota *p,
157256
+ OtaObjIter *pIter,
157257
+ const char *zMask
157258
+){
157259
+ char *zList = 0;
157260
+ if( p->rc==SQLITE_OK ){
157261
+ int i;
157262
+
157263
+ if( strlen(zMask)!=pIter->nTblCol ){
157264
+ otaBadControlError(p);
157265
+ }else{
157266
+ const char *zSep = "";
157267
+ for(i=0; i<pIter->nTblCol; i++){
157268
+ char c = zMask[pIter->aiSrcOrder[i]];
157269
+ if( c=='x' ){
157270
+ zList = otaMPrintf(p, "%z%s\"%w\"=?%d",
157271
+ zList, zSep, pIter->azTblCol[i], i+1
157272
+ );
157273
+ zSep = ", ";
157274
+ }
157275
+ if( c=='d' ){
157276
+ zList = otaMPrintf(p, "%z%s\"%w\"=ota_delta(\"%w\", ?%d)",
157277
+ zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
157278
+ );
157279
+ zSep = ", ";
157280
+ }
157281
+ }
157282
+ }
157283
+ }
157284
+ return zList;
157285
+}
157286
+
157287
+/*
157288
+** Return a nul-terminated string consisting of nByte comma separated
157289
+** "?" expressions. For example, if nByte is 3, return a pointer to
157290
+** a buffer containing the string "?,?,?".
157291
+**
157292
+** The memory for the returned string is obtained from sqlite3_malloc().
157293
+** It is the responsibility of the caller to eventually free it using
157294
+** sqlite3_free().
157295
+**
157296
+** If an OOM error is encountered when allocating space for the new
157297
+** string, an error code is left in the ota handle passed as the first
157298
+** argument and NULL is returned. Or, if an error has already occurred
157299
+** when this function is called, NULL is returned immediately, without
157300
+** attempting the allocation or modifying the stored error code.
157301
+*/
157302
+static char *otaObjIterGetBindlist(sqlite3ota *p, int nBind){
157303
+ char *zRet = 0;
157304
+ int nByte = nBind*2 + 1;
157305
+
157306
+ zRet = (char*)otaMalloc(p, nByte);
157307
+ if( zRet ){
157308
+ int i;
157309
+ for(i=0; i<nBind; i++){
157310
+ zRet[i*2] = '?';
157311
+ zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
157312
+ }
157313
+ }
157314
+ return zRet;
157315
+}
157316
+
157317
+/*
157318
+** The iterator currently points to a table (not index) of type
157319
+** OTA_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
157320
+** declaration for the corresponding imposter table. For example,
157321
+** if the iterator points to a table created as:
157322
+**
157323
+** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
157324
+**
157325
+** this function returns:
157326
+**
157327
+** PRIMARY KEY("b", "a" DESC)
157328
+*/
157329
+static char *otaWithoutRowidPK(sqlite3ota *p, OtaObjIter *pIter){
157330
+ char *z = 0;
157331
+ assert( pIter->zIdx==0 );
157332
+ if( p->rc==SQLITE_OK ){
157333
+ const char *zSep = "PRIMARY KEY(";
157334
+ sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
157335
+ sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
157336
+
157337
+ p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
157338
+ sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
157339
+ );
157340
+ while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
157341
+ const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
157342
+ if( zOrig && strcmp(zOrig, "pk")==0 ){
157343
+ const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
157344
+ if( zIdx ){
157345
+ p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
157346
+ sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
157347
+ );
157348
+ }
157349
+ break;
157350
+ }
157351
+ }
157352
+ otaFinalize(p, pXList);
157353
+
157354
+ while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
157355
+ if( sqlite3_column_int(pXInfo, 5) ){
157356
+ /* int iCid = sqlite3_column_int(pXInfo, 0); */
157357
+ const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
157358
+ const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
157359
+ z = otaMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
157360
+ zSep = ", ";
157361
+ }
157362
+ }
157363
+ z = otaMPrintf(p, "%z)", z);
157364
+ otaFinalize(p, pXInfo);
157365
+ }
157366
+ return z;
157367
+}
157368
+
157369
+/*
157370
+** This function creates the second imposter table used when writing to
157371
+** a table b-tree where the table has an external primary key. If the
157372
+** iterator passed as the second argument does not currently point to
157373
+** a table (not index) with an external primary key, this function is a
157374
+** no-op.
157375
+**
157376
+** Assuming the iterator does point to a table with an external PK, this
157377
+** function creates a WITHOUT ROWID imposter table named "ota_imposter2"
157378
+** used to access that PK index. For example, if the target table is
157379
+** declared as follows:
157380
+**
157381
+** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
157382
+**
157383
+** then the imposter table schema is:
157384
+**
157385
+** CREATE TABLE ota_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
157386
+**
157387
+*/
157388
+static void otaCreateImposterTable2(sqlite3ota *p, OtaObjIter *pIter){
157389
+ if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_EXTERNAL ){
157390
+ int tnum = pIter->iPkTnum; /* Root page of PK index */
157391
+ sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
157392
+ const char *zIdx = 0; /* Name of PK index */
157393
+ sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
157394
+ const char *zComma = "";
157395
+ char *zCols = 0; /* Used to build up list of table cols */
157396
+ char *zPk = 0; /* Used to build up table PK declaration */
157397
+
157398
+ /* Figure out the name of the primary key index for the current table.
157399
+ ** This is needed for the argument to "PRAGMA index_xinfo". Set
157400
+ ** zIdx to point to a nul-terminated string containing this name. */
157401
+ p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
157402
+ "SELECT name FROM sqlite_master WHERE rootpage = ?"
157403
+ );
157404
+ if( p->rc==SQLITE_OK ){
157405
+ sqlite3_bind_int(pQuery, 1, tnum);
157406
+ if( SQLITE_ROW==sqlite3_step(pQuery) ){
157407
+ zIdx = (const char*)sqlite3_column_text(pQuery, 0);
157408
+ }
157409
+ }
157410
+ if( zIdx ){
157411
+ p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
157412
+ sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
157413
+ );
157414
+ }
157415
+ otaFinalize(p, pQuery);
157416
+
157417
+ while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
157418
+ int bKey = sqlite3_column_int(pXInfo, 5);
157419
+ if( bKey ){
157420
+ int iCid = sqlite3_column_int(pXInfo, 1);
157421
+ int bDesc = sqlite3_column_int(pXInfo, 3);
157422
+ const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
157423
+ zCols = otaMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
157424
+ iCid, pIter->azTblType[iCid], zCollate
157425
+ );
157426
+ zPk = otaMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
157427
+ zComma = ", ";
157428
+ }
157429
+ }
157430
+ zCols = otaMPrintf(p, "%z, id INTEGER", zCols);
157431
+ otaFinalize(p, pXInfo);
157432
+
157433
+ sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
157434
+ otaMPrintfExec(p, p->dbMain,
157435
+ "CREATE TABLE ota_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
157436
+ zCols, zPk
157437
+ );
157438
+ sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
157439
+ }
157440
+}
157441
+
157442
+/*
157443
+** If an error has already occurred when this function is called, it
157444
+** immediately returns zero (without doing any work). Or, if an error
157445
+** occurs during the execution of this function, it sets the error code
157446
+** in the sqlite3ota object indicated by the first argument and returns
157447
+** zero.
157448
+**
157449
+** The iterator passed as the second argument is guaranteed to point to
157450
+** a table (not an index) when this function is called. This function
157451
+** attempts to create any imposter table required to write to the main
157452
+** table b-tree of the table before returning. Non-zero is returned if
157453
+** an imposter table are created, or zero otherwise.
157454
+**
157455
+** An imposter table is required in all cases except OTA_PK_VTAB. Only
157456
+** virtual tables are written to directly. The imposter table has the
157457
+** same schema as the actual target table (less any UNIQUE constraints).
157458
+** More precisely, the "same schema" means the same columns, types,
157459
+** collation sequences. For tables that do not have an external PRIMARY
157460
+** KEY, it also means the same PRIMARY KEY declaration.
157461
+*/
157462
+static void otaCreateImposterTable(sqlite3ota *p, OtaObjIter *pIter){
157463
+ if( p->rc==SQLITE_OK && pIter->eType!=OTA_PK_VTAB ){
157464
+ int tnum = pIter->iTnum;
157465
+ const char *zComma = "";
157466
+ char *zSql = 0;
157467
+ int iCol;
157468
+ sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
157469
+
157470
+ for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
157471
+ const char *zPk = "";
157472
+ const char *zCol = pIter->azTblCol[iCol];
157473
+ const char *zColl = 0;
157474
+
157475
+ p->rc = sqlite3_table_column_metadata(
157476
+ p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
157477
+ );
157478
+
157479
+ if( pIter->eType==OTA_PK_IPK && pIter->abTblPk[iCol] ){
157480
+ /* If the target table column is an "INTEGER PRIMARY KEY", add
157481
+ ** "PRIMARY KEY" to the imposter table column declaration. */
157482
+ zPk = "PRIMARY KEY ";
157483
+ }
157484
+ zSql = otaMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
157485
+ zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
157486
+ (pIter->abNotNull[iCol] ? " NOT NULL" : "")
157487
+ );
157488
+ zComma = ", ";
157489
+ }
157490
+
157491
+ if( pIter->eType==OTA_PK_WITHOUT_ROWID ){
157492
+ char *zPk = otaWithoutRowidPK(p, pIter);
157493
+ if( zPk ){
157494
+ zSql = otaMPrintf(p, "%z, %z", zSql, zPk);
157495
+ }
157496
+ }
157497
+
157498
+ sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
157499
+ otaMPrintfExec(p, p->dbMain, "CREATE TABLE \"ota_imp_%w\"(%z)%s",
157500
+ pIter->zTbl, zSql,
157501
+ (pIter->eType==OTA_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
157502
+ );
157503
+ sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
157504
+ }
157505
+}
157506
+
157507
+/*
157508
+** Prepare a statement used to insert rows into the "ota_tmp_xxx" table.
157509
+** Specifically a statement of the form:
157510
+**
157511
+** INSERT INTO ota_tmp_xxx VALUES(?, ?, ? ...);
157512
+**
157513
+** The number of bound variables is equal to the number of columns in
157514
+** the target table, plus one (for the ota_control column), plus one more
157515
+** (for the ota_rowid column) if the target table is an implicit IPK or
157516
+** virtual table.
157517
+*/
157518
+static void otaObjIterPrepareTmpInsert(
157519
+ sqlite3ota *p,
157520
+ OtaObjIter *pIter,
157521
+ const char *zCollist,
157522
+ const char *zOtaRowid
157523
+){
157524
+ int bOtaRowid = (pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE);
157525
+ char *zBind = otaObjIterGetBindlist(p, pIter->nTblCol + 1 + bOtaRowid);
157526
+ if( zBind ){
157527
+ assert( pIter->pTmpInsert==0 );
157528
+ p->rc = prepareFreeAndCollectError(
157529
+ p->dbOta, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
157530
+ "INSERT INTO %s.'ota_tmp_%q'(ota_control,%s%s) VALUES(%z)",
157531
+ p->zStateDb, pIter->zTbl, zCollist, zOtaRowid, zBind
157532
+ ));
157533
+ }
157534
+}
157535
+
157536
+static void otaTmpInsertFunc(
157537
+ sqlite3_context *pCtx,
157538
+ int nVal,
157539
+ sqlite3_value **apVal
157540
+){
157541
+ sqlite3ota *p = sqlite3_user_data(pCtx);
157542
+ int rc = SQLITE_OK;
157543
+ int i;
157544
+
157545
+ for(i=0; rc==SQLITE_OK && i<nVal; i++){
157546
+ rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
157547
+ }
157548
+ if( rc==SQLITE_OK ){
157549
+ sqlite3_step(p->objiter.pTmpInsert);
157550
+ rc = sqlite3_reset(p->objiter.pTmpInsert);
157551
+ }
157552
+
157553
+ if( rc!=SQLITE_OK ){
157554
+ sqlite3_result_error_code(pCtx, rc);
157555
+ }
157556
+}
157557
+
157558
+/*
157559
+** Ensure that the SQLite statement handles required to update the
157560
+** target database object currently indicated by the iterator passed
157561
+** as the second argument are available.
157562
+*/
157563
+static int otaObjIterPrepareAll(
157564
+ sqlite3ota *p,
157565
+ OtaObjIter *pIter,
157566
+ int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
157567
+){
157568
+ assert( pIter->bCleanup==0 );
157569
+ if( pIter->pSelect==0 && otaObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
157570
+ const int tnum = pIter->iTnum;
157571
+ char *zCollist = 0; /* List of indexed columns */
157572
+ char **pz = &p->zErrmsg;
157573
+ const char *zIdx = pIter->zIdx;
157574
+ char *zLimit = 0;
157575
+
157576
+ if( nOffset ){
157577
+ zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
157578
+ if( !zLimit ) p->rc = SQLITE_NOMEM;
157579
+ }
157580
+
157581
+ if( zIdx ){
157582
+ const char *zTbl = pIter->zTbl;
157583
+ char *zImposterCols = 0; /* Columns for imposter table */
157584
+ char *zImposterPK = 0; /* Primary key declaration for imposter */
157585
+ char *zWhere = 0; /* WHERE clause on PK columns */
157586
+ char *zBind = 0;
157587
+ int nBind = 0;
157588
+
157589
+ assert( pIter->eType!=OTA_PK_VTAB );
157590
+ zCollist = otaObjIterGetIndexCols(
157591
+ p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
157592
+ );
157593
+ zBind = otaObjIterGetBindlist(p, nBind);
157594
+
157595
+ /* Create the imposter table used to write to this index. */
157596
+ sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
157597
+ sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
157598
+ otaMPrintfExec(p, p->dbMain,
157599
+ "CREATE TABLE \"ota_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
157600
+ zTbl, zImposterCols, zImposterPK
157601
+ );
157602
+ sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
157603
+
157604
+ /* Create the statement to insert index entries */
157605
+ pIter->nCol = nBind;
157606
+ if( p->rc==SQLITE_OK ){
157607
+ p->rc = prepareFreeAndCollectError(
157608
+ p->dbMain, &pIter->pInsert, &p->zErrmsg,
157609
+ sqlite3_mprintf("INSERT INTO \"ota_imp_%w\" VALUES(%s)", zTbl, zBind)
157610
+ );
157611
+ }
157612
+
157613
+ /* And to delete index entries */
157614
+ if( p->rc==SQLITE_OK ){
157615
+ p->rc = prepareFreeAndCollectError(
157616
+ p->dbMain, &pIter->pDelete, &p->zErrmsg,
157617
+ sqlite3_mprintf("DELETE FROM \"ota_imp_%w\" WHERE %s", zTbl, zWhere)
157618
+ );
157619
+ }
157620
+
157621
+ /* Create the SELECT statement to read keys in sorted order */
157622
+ if( p->rc==SQLITE_OK ){
157623
+ char *zSql;
157624
+ if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
157625
+ zSql = sqlite3_mprintf(
157626
+ "SELECT %s, ota_control FROM %s.'ota_tmp_%q' ORDER BY %s%s",
157627
+ zCollist, p->zStateDb, pIter->zTbl,
157628
+ zCollist, zLimit
157629
+ );
157630
+ }else{
157631
+ zSql = sqlite3_mprintf(
157632
+ "SELECT %s, ota_control FROM 'data_%q' "
157633
+ "WHERE typeof(ota_control)='integer' AND ota_control!=1 "
157634
+ "UNION ALL "
157635
+ "SELECT %s, ota_control FROM %s.'ota_tmp_%q' "
157636
+ "ORDER BY %s%s",
157637
+ zCollist, pIter->zTbl,
157638
+ zCollist, p->zStateDb, pIter->zTbl,
157639
+ zCollist, zLimit
157640
+ );
157641
+ }
157642
+ p->rc = prepareFreeAndCollectError(p->dbOta, &pIter->pSelect, pz, zSql);
157643
+ }
157644
+
157645
+ sqlite3_free(zImposterCols);
157646
+ sqlite3_free(zImposterPK);
157647
+ sqlite3_free(zWhere);
157648
+ sqlite3_free(zBind);
157649
+ }else{
157650
+ int bOtaRowid = (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE);
157651
+ const char *zTbl = pIter->zTbl; /* Table this step applies to */
157652
+ const char *zWrite; /* Imposter table name */
157653
+
157654
+ char *zBindings = otaObjIterGetBindlist(p, pIter->nTblCol + bOtaRowid);
157655
+ char *zWhere = otaObjIterGetWhere(p, pIter);
157656
+ char *zOldlist = otaObjIterGetOldlist(p, pIter, "old");
157657
+ char *zNewlist = otaObjIterGetOldlist(p, pIter, "new");
157658
+
157659
+ zCollist = otaObjIterGetCollist(p, pIter);
157660
+ pIter->nCol = pIter->nTblCol;
157661
+
157662
+ /* Create the SELECT statement to read keys from data_xxx */
157663
+ if( p->rc==SQLITE_OK ){
157664
+ p->rc = prepareFreeAndCollectError(p->dbOta, &pIter->pSelect, pz,
157665
+ sqlite3_mprintf(
157666
+ "SELECT %s, ota_control%s FROM 'data_%q'%s",
157667
+ zCollist, (bOtaRowid ? ", ota_rowid" : ""), zTbl, zLimit
157668
+ )
157669
+ );
157670
+ }
157671
+
157672
+ /* Create the imposter table or tables (if required). */
157673
+ otaCreateImposterTable(p, pIter);
157674
+ otaCreateImposterTable2(p, pIter);
157675
+ zWrite = (pIter->eType==OTA_PK_VTAB ? "" : "ota_imp_");
157676
+
157677
+ /* Create the INSERT statement to write to the target PK b-tree */
157678
+ if( p->rc==SQLITE_OK ){
157679
+ p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
157680
+ sqlite3_mprintf(
157681
+ "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
157682
+ zWrite, zTbl, zCollist, (bOtaRowid ? ", _rowid_" : ""), zBindings
157683
+ )
157684
+ );
157685
+ }
157686
+
157687
+ /* Create the DELETE statement to write to the target PK b-tree */
157688
+ if( p->rc==SQLITE_OK ){
157689
+ p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
157690
+ sqlite3_mprintf(
157691
+ "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
157692
+ )
157693
+ );
157694
+ }
157695
+
157696
+ if( pIter->abIndexed ){
157697
+ const char *zOtaRowid = "";
157698
+ if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
157699
+ zOtaRowid = ", ota_rowid";
157700
+ }
157701
+
157702
+ /* Create the ota_tmp_xxx table and the triggers to populate it. */
157703
+ otaMPrintfExec(p, p->dbOta,
157704
+ "CREATE TABLE IF NOT EXISTS %s.'ota_tmp_%q' AS "
157705
+ "SELECT *%s FROM 'data_%q' WHERE 0;"
157706
+ , p->zStateDb
157707
+ , zTbl, (pIter->eType==OTA_PK_EXTERNAL ? ", 0 AS ota_rowid" : "")
157708
+ , zTbl
157709
+ );
157710
+
157711
+ otaMPrintfExec(p, p->dbMain,
157712
+ "CREATE TEMP TRIGGER ota_delete_tr BEFORE DELETE ON \"%s%w\" "
157713
+ "BEGIN "
157714
+ " SELECT ota_tmp_insert(2, %s);"
157715
+ "END;"
157716
+
157717
+ "CREATE TEMP TRIGGER ota_update1_tr BEFORE UPDATE ON \"%s%w\" "
157718
+ "BEGIN "
157719
+ " SELECT ota_tmp_insert(2, %s);"
157720
+ "END;"
157721
+
157722
+ "CREATE TEMP TRIGGER ota_update2_tr AFTER UPDATE ON \"%s%w\" "
157723
+ "BEGIN "
157724
+ " SELECT ota_tmp_insert(3, %s);"
157725
+ "END;",
157726
+ zWrite, zTbl, zOldlist,
157727
+ zWrite, zTbl, zOldlist,
157728
+ zWrite, zTbl, zNewlist
157729
+ );
157730
+
157731
+ if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
157732
+ otaMPrintfExec(p, p->dbMain,
157733
+ "CREATE TEMP TRIGGER ota_insert_tr AFTER INSERT ON \"%s%w\" "
157734
+ "BEGIN "
157735
+ " SELECT ota_tmp_insert(0, %s);"
157736
+ "END;",
157737
+ zWrite, zTbl, zNewlist
157738
+ );
157739
+ }
157740
+
157741
+ otaObjIterPrepareTmpInsert(p, pIter, zCollist, zOtaRowid);
157742
+ }
157743
+
157744
+ sqlite3_free(zWhere);
157745
+ sqlite3_free(zOldlist);
157746
+ sqlite3_free(zNewlist);
157747
+ sqlite3_free(zBindings);
157748
+ }
157749
+ sqlite3_free(zCollist);
157750
+ sqlite3_free(zLimit);
157751
+ }
157752
+
157753
+ return p->rc;
157754
+}
157755
+
157756
+/*
157757
+** Set output variable *ppStmt to point to an UPDATE statement that may
157758
+** be used to update the imposter table for the main table b-tree of the
157759
+** table object that pIter currently points to, assuming that the
157760
+** ota_control column of the data_xyz table contains zMask.
157761
+**
157762
+** If the zMask string does not specify any columns to update, then this
157763
+** is not an error. Output variable *ppStmt is set to NULL in this case.
157764
+*/
157765
+static int otaGetUpdateStmt(
157766
+ sqlite3ota *p, /* OTA handle */
157767
+ OtaObjIter *pIter, /* Object iterator */
157768
+ const char *zMask, /* ota_control value ('x.x.') */
157769
+ sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
157770
+){
157771
+ OtaUpdateStmt **pp;
157772
+ OtaUpdateStmt *pUp = 0;
157773
+ int nUp = 0;
157774
+
157775
+ /* In case an error occurs */
157776
+ *ppStmt = 0;
157777
+
157778
+ /* Search for an existing statement. If one is found, shift it to the front
157779
+ ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
157780
+ ** to the number of statements currently in the cache and pUp to the
157781
+ ** last object in the list. */
157782
+ for(pp=&pIter->pOtaUpdate; *pp; pp=&((*pp)->pNext)){
157783
+ pUp = *pp;
157784
+ if( strcmp(pUp->zMask, zMask)==0 ){
157785
+ *pp = pUp->pNext;
157786
+ pUp->pNext = pIter->pOtaUpdate;
157787
+ pIter->pOtaUpdate = pUp;
157788
+ *ppStmt = pUp->pUpdate;
157789
+ return SQLITE_OK;
157790
+ }
157791
+ nUp++;
157792
+ }
157793
+ assert( pUp==0 || pUp->pNext==0 );
157794
+
157795
+ if( nUp>=SQLITE_OTA_UPDATE_CACHESIZE ){
157796
+ for(pp=&pIter->pOtaUpdate; *pp!=pUp; pp=&((*pp)->pNext));
157797
+ *pp = 0;
157798
+ sqlite3_finalize(pUp->pUpdate);
157799
+ pUp->pUpdate = 0;
157800
+ }else{
157801
+ pUp = (OtaUpdateStmt*)otaMalloc(p, sizeof(OtaUpdateStmt)+pIter->nTblCol+1);
157802
+ }
157803
+
157804
+ if( pUp ){
157805
+ char *zWhere = otaObjIterGetWhere(p, pIter);
157806
+ char *zSet = otaObjIterGetSetlist(p, pIter, zMask);
157807
+ char *zUpdate = 0;
157808
+
157809
+ pUp->zMask = (char*)&pUp[1];
157810
+ memcpy(pUp->zMask, zMask, pIter->nTblCol);
157811
+ pUp->pNext = pIter->pOtaUpdate;
157812
+ pIter->pOtaUpdate = pUp;
157813
+
157814
+ if( zSet ){
157815
+ const char *zPrefix = "";
157816
+
157817
+ if( pIter->eType!=OTA_PK_VTAB ) zPrefix = "ota_imp_";
157818
+ zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
157819
+ zPrefix, pIter->zTbl, zSet, zWhere
157820
+ );
157821
+ p->rc = prepareFreeAndCollectError(
157822
+ p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
157823
+ );
157824
+ *ppStmt = pUp->pUpdate;
157825
+ }
157826
+ sqlite3_free(zWhere);
157827
+ sqlite3_free(zSet);
157828
+ }
157829
+
157830
+ return p->rc;
157831
+}
157832
+
157833
+static sqlite3 *otaOpenDbhandle(sqlite3ota *p, const char *zName){
157834
+ sqlite3 *db = 0;
157835
+ if( p->rc==SQLITE_OK ){
157836
+ const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
157837
+ p->rc = sqlite3_open_v2(zName, &db, flags, p->zVfsName);
157838
+ if( p->rc ){
157839
+ p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
157840
+ sqlite3_close(db);
157841
+ db = 0;
157842
+ }
157843
+ }
157844
+ return db;
157845
+}
157846
+
157847
+/*
157848
+** Open the database handle and attach the OTA database as "ota". If an
157849
+** error occurs, leave an error code and message in the OTA handle.
157850
+*/
157851
+static void otaOpenDatabase(sqlite3ota *p){
157852
+ assert( p->rc==SQLITE_OK );
157853
+ assert( p->dbMain==0 && p->dbOta==0 );
157854
+
157855
+ p->eStage = 0;
157856
+ p->dbMain = otaOpenDbhandle(p, p->zTarget);
157857
+ p->dbOta = otaOpenDbhandle(p, p->zOta);
157858
+
157859
+ /* If using separate OTA and state databases, attach the state database to
157860
+ ** the OTA db handle now. */
157861
+ if( p->zState ){
157862
+ otaMPrintfExec(p, p->dbOta, "ATTACH %Q AS stat", p->zState);
157863
+ memcpy(p->zStateDb, "stat", 4);
157864
+ }else{
157865
+ memcpy(p->zStateDb, "main", 4);
157866
+ }
157867
+
157868
+ if( p->rc==SQLITE_OK ){
157869
+ p->rc = sqlite3_create_function(p->dbMain,
157870
+ "ota_tmp_insert", -1, SQLITE_UTF8, (void*)p, otaTmpInsertFunc, 0, 0
157871
+ );
157872
+ }
157873
+
157874
+ if( p->rc==SQLITE_OK ){
157875
+ p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_OTA, (void*)p);
157876
+ }
157877
+ otaMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
157878
+
157879
+ /* Mark the database file just opened as an OTA target database. If
157880
+ ** this call returns SQLITE_NOTFOUND, then the OTA vfs is not in use.
157881
+ ** This is an error. */
157882
+ if( p->rc==SQLITE_OK ){
157883
+ p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_OTA, (void*)p);
157884
+ }
157885
+
157886
+ if( p->rc==SQLITE_NOTFOUND ){
157887
+ p->rc = SQLITE_ERROR;
157888
+ p->zErrmsg = sqlite3_mprintf("ota vfs not found");
157889
+ }
157890
+}
157891
+
157892
+/*
157893
+** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
157894
+** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
157895
+**
157896
+** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
157897
+** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
157898
+** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
157899
+** three characters, then shorten the suffix on z[] to be the last three
157900
+** characters of the original suffix.
157901
+**
157902
+** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
157903
+** do the suffix shortening regardless of URI parameter.
157904
+**
157905
+** Examples:
157906
+**
157907
+** test.db-journal => test.nal
157908
+** test.db-wal => test.wal
157909
+** test.db-shm => test.shm
157910
+** test.db-mj7f3319fa => test.9fa
157911
+*/
157912
+static void otaFileSuffix3(const char *zBase, char *z){
157913
+#ifdef SQLITE_ENABLE_8_3_NAMES
157914
+#if SQLITE_ENABLE_8_3_NAMES<2
157915
+ if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
157916
+#endif
157917
+ {
157918
+ int i, sz;
157919
+ sz = sqlite3Strlen30(z);
157920
+ for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
157921
+ if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
157922
+ }
157923
+#endif
157924
+}
157925
+
157926
+/*
157927
+** Return the current wal-index header checksum for the target database
157928
+** as a 64-bit integer.
157929
+**
157930
+** The checksum is store in the first page of xShmMap memory as an 8-byte
157931
+** blob starting at byte offset 40.
157932
+*/
157933
+static i64 otaShmChecksum(sqlite3ota *p){
157934
+ i64 iRet = 0;
157935
+ if( p->rc==SQLITE_OK ){
157936
+ sqlite3_file *pDb = p->pTargetFd->pReal;
157937
+ u32 volatile *ptr;
157938
+ p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
157939
+ if( p->rc==SQLITE_OK ){
157940
+ iRet = ((i64)ptr[10] << 32) + ptr[11];
157941
+ }
157942
+ }
157943
+ return iRet;
157944
+}
157945
+
157946
+/*
157947
+** This function is called as part of initializing or reinitializing an
157948
+** incremental checkpoint.
157949
+**
157950
+** It populates the sqlite3ota.aFrame[] array with the set of
157951
+** (wal frame -> db page) copy operations required to checkpoint the
157952
+** current wal file, and obtains the set of shm locks required to safely
157953
+** perform the copy operations directly on the file-system.
157954
+**
157955
+** If argument pState is not NULL, then the incremental checkpoint is
157956
+** being resumed. In this case, if the checksum of the wal-index-header
157957
+** following recovery is not the same as the checksum saved in the OtaState
157958
+** object, then the ota handle is set to DONE state. This occurs if some
157959
+** other client appends a transaction to the wal file in the middle of
157960
+** an incremental checkpoint.
157961
+*/
157962
+static void otaSetupCheckpoint(sqlite3ota *p, OtaState *pState){
157963
+
157964
+ /* If pState is NULL, then the wal file may not have been opened and
157965
+ ** recovered. Running a read-statement here to ensure that doing so
157966
+ ** does not interfere with the "capture" process below. */
157967
+ if( pState==0 ){
157968
+ p->eStage = 0;
157969
+ if( p->rc==SQLITE_OK ){
157970
+ p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
157971
+ }
157972
+ }
157973
+
157974
+ /* Assuming no error has occurred, run a "restart" checkpoint with the
157975
+ ** sqlite3ota.eStage variable set to CAPTURE. This turns on the following
157976
+ ** special behaviour in the ota VFS:
157977
+ **
157978
+ ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
157979
+ ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
157980
+ ** proceed with running a passive checkpoint instead of failing).
157981
+ **
157982
+ ** * Attempts to read from the *-wal file or write to the database file
157983
+ ** do not perform any IO. Instead, the frame/page combinations that
157984
+ ** would be read/written are recorded in the sqlite3ota.aFrame[]
157985
+ ** array.
157986
+ **
157987
+ ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
157988
+ ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
157989
+ ** no-ops. These locks will not be released until the connection
157990
+ ** is closed.
157991
+ **
157992
+ ** * Attempting to xSync() the database file causes an SQLITE_INTERNAL
157993
+ ** error.
157994
+ **
157995
+ ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
157996
+ ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
157997
+ ** array populated with a set of (frame -> page) mappings. Because the
157998
+ ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
157999
+ ** data from the wal file into the database file according to the
158000
+ ** contents of aFrame[].
158001
+ */
158002
+ if( p->rc==SQLITE_OK ){
158003
+ int rc2;
158004
+ p->eStage = OTA_STAGE_CAPTURE;
158005
+ rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
158006
+ if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
158007
+ }
158008
+
158009
+ if( p->rc==SQLITE_OK ){
158010
+ p->eStage = OTA_STAGE_CKPT;
158011
+ p->nStep = (pState ? pState->nRow : 0);
158012
+ p->aBuf = otaMalloc(p, p->pgsz);
158013
+ p->iWalCksum = otaShmChecksum(p);
158014
+ }
158015
+
158016
+ if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
158017
+ p->rc = SQLITE_DONE;
158018
+ p->eStage = OTA_STAGE_DONE;
158019
+ }
158020
+}
158021
+
158022
+/*
158023
+** Called when iAmt bytes are read from offset iOff of the wal file while
158024
+** the ota object is in capture mode. Record the frame number of the frame
158025
+** being read in the aFrame[] array.
158026
+*/
158027
+static int otaCaptureWalRead(sqlite3ota *pOta, i64 iOff, int iAmt){
158028
+ const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
158029
+ u32 iFrame;
158030
+
158031
+ if( pOta->mLock!=mReq ){
158032
+ pOta->rc = SQLITE_BUSY;
158033
+ return SQLITE_INTERNAL;
158034
+ }
158035
+
158036
+ pOta->pgsz = iAmt;
158037
+ if( pOta->nFrame==pOta->nFrameAlloc ){
158038
+ int nNew = (pOta->nFrameAlloc ? pOta->nFrameAlloc : 64) * 2;
158039
+ OtaFrame *aNew;
158040
+ aNew = (OtaFrame*)sqlite3_realloc(pOta->aFrame, nNew * sizeof(OtaFrame));
158041
+ if( aNew==0 ) return SQLITE_NOMEM;
158042
+ pOta->aFrame = aNew;
158043
+ pOta->nFrameAlloc = nNew;
158044
+ }
158045
+
158046
+ iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
158047
+ if( pOta->iMaxFrame<iFrame ) pOta->iMaxFrame = iFrame;
158048
+ pOta->aFrame[pOta->nFrame].iWalFrame = iFrame;
158049
+ pOta->aFrame[pOta->nFrame].iDbPage = 0;
158050
+ pOta->nFrame++;
158051
+ return SQLITE_OK;
158052
+}
158053
+
158054
+/*
158055
+** Called when a page of data is written to offset iOff of the database
158056
+** file while the ota handle is in capture mode. Record the page number
158057
+** of the page being written in the aFrame[] array.
158058
+*/
158059
+static int otaCaptureDbWrite(sqlite3ota *pOta, i64 iOff){
158060
+ pOta->aFrame[pOta->nFrame-1].iDbPage = (u32)(iOff / pOta->pgsz) + 1;
158061
+ return SQLITE_OK;
158062
+}
158063
+
158064
+/*
158065
+** This is called as part of an incremental checkpoint operation. Copy
158066
+** a single frame of data from the wal file into the database file, as
158067
+** indicated by the OtaFrame object.
158068
+*/
158069
+static void otaCheckpointFrame(sqlite3ota *p, OtaFrame *pFrame){
158070
+ sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
158071
+ sqlite3_file *pDb = p->pTargetFd->pReal;
158072
+ i64 iOff;
158073
+
158074
+ assert( p->rc==SQLITE_OK );
158075
+ iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
158076
+ p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
158077
+ if( p->rc ) return;
158078
+
158079
+ iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
158080
+ p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
158081
+}
158082
+
158083
+
158084
+/*
158085
+** Take an EXCLUSIVE lock on the database file.
158086
+*/
158087
+static void otaLockDatabase(sqlite3ota *p){
158088
+ sqlite3_file *pReal = p->pTargetFd->pReal;
158089
+ assert( p->rc==SQLITE_OK );
158090
+ p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
158091
+ if( p->rc==SQLITE_OK ){
158092
+ p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
158093
+ }
158094
+}
158095
+
158096
+/*
158097
+** The OTA handle is currently in OTA_STAGE_OAL state, with a SHARED lock
158098
+** on the database file. This proc moves the *-oal file to the *-wal path,
158099
+** then reopens the database file (this time in vanilla, non-oal, WAL mode).
158100
+** If an error occurs, leave an error code and error message in the ota
158101
+** handle.
158102
+*/
158103
+static void otaMoveOalFile(sqlite3ota *p){
158104
+ const char *zBase = sqlite3_db_filename(p->dbMain, "main");
158105
+
158106
+ char *zWal = sqlite3_mprintf("%s-wal", zBase);
158107
+ char *zOal = sqlite3_mprintf("%s-oal", zBase);
158108
+
158109
+ assert( p->eStage==OTA_STAGE_MOVE );
158110
+ assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
158111
+ if( zWal==0 || zOal==0 ){
158112
+ p->rc = SQLITE_NOMEM;
158113
+ }else{
158114
+ /* Move the *-oal file to *-wal. At this point connection p->db is
158115
+ ** holding a SHARED lock on the target database file (because it is
158116
+ ** in WAL mode). So no other connection may be writing the db.
158117
+ **
158118
+ ** In order to ensure that there are no database readers, an EXCLUSIVE
158119
+ ** lock is obtained here before the *-oal is moved to *-wal.
158120
+ */
158121
+ otaLockDatabase(p);
158122
+ if( p->rc==SQLITE_OK ){
158123
+ otaFileSuffix3(zBase, zWal);
158124
+ otaFileSuffix3(zBase, zOal);
158125
+
158126
+ /* Re-open the databases. */
158127
+ otaObjIterFinalize(&p->objiter);
158128
+ sqlite3_close(p->dbMain);
158129
+ sqlite3_close(p->dbOta);
158130
+ p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
158131
+ if( p->rc==SQLITE_OK ){
158132
+ p->dbMain = 0;
158133
+ p->dbOta = 0;
158134
+ otaOpenDatabase(p);
158135
+ otaSetupCheckpoint(p, 0);
158136
+ }
158137
+ }
158138
+ }
158139
+
158140
+ sqlite3_free(zWal);
158141
+ sqlite3_free(zOal);
158142
+}
158143
+
158144
+/*
158145
+** The SELECT statement iterating through the keys for the current object
158146
+** (p->objiter.pSelect) currently points to a valid row. This function
158147
+** determines the type of operation requested by this row and returns
158148
+** one of the following values to indicate the result:
158149
+**
158150
+** * OTA_INSERT
158151
+** * OTA_DELETE
158152
+** * OTA_IDX_DELETE
158153
+** * OTA_UPDATE
158154
+**
158155
+** If OTA_UPDATE is returned, then output variable *pzMask is set to
158156
+** point to the text value indicating the columns to update.
158157
+**
158158
+** If the ota_control field contains an invalid value, an error code and
158159
+** message are left in the OTA handle and zero returned.
158160
+*/
158161
+static int otaStepType(sqlite3ota *p, const char **pzMask){
158162
+ int iCol = p->objiter.nCol; /* Index of ota_control column */
158163
+ int res = 0; /* Return value */
158164
+
158165
+ switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
158166
+ case SQLITE_INTEGER: {
158167
+ int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
158168
+ if( iVal==0 ){
158169
+ res = OTA_INSERT;
158170
+ }else if( iVal==1 ){
158171
+ res = OTA_DELETE;
158172
+ }else if( iVal==2 ){
158173
+ res = OTA_IDX_DELETE;
158174
+ }else if( iVal==3 ){
158175
+ res = OTA_IDX_INSERT;
158176
+ }
158177
+ break;
158178
+ }
158179
+
158180
+ case SQLITE_TEXT: {
158181
+ const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
158182
+ if( z==0 ){
158183
+ p->rc = SQLITE_NOMEM;
158184
+ }else{
158185
+ *pzMask = (const char*)z;
158186
+ }
158187
+ res = OTA_UPDATE;
158188
+
158189
+ break;
158190
+ }
158191
+
158192
+ default:
158193
+ break;
158194
+ }
158195
+
158196
+ if( res==0 ){
158197
+ otaBadControlError(p);
158198
+ }
158199
+ return res;
158200
+}
158201
+
158202
+#ifdef SQLITE_DEBUG
158203
+/*
158204
+** Assert that column iCol of statement pStmt is named zName.
158205
+*/
158206
+static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
158207
+ const char *zCol = sqlite3_column_name(pStmt, iCol);
158208
+ assert( 0==sqlite3_stricmp(zName, zCol) );
158209
+}
158210
+#else
158211
+# define assertColumnName(x,y,z)
158212
+#endif
158213
+
158214
+/*
158215
+** This function does the work for an sqlite3ota_step() call.
158216
+**
158217
+** The object-iterator (p->objiter) currently points to a valid object,
158218
+** and the input cursor (p->objiter.pSelect) currently points to a valid
158219
+** input row. Perform whatever processing is required and return.
158220
+**
158221
+** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
158222
+** and message is left in the OTA handle and a copy of the error code
158223
+** returned.
158224
+*/
158225
+static int otaStep(sqlite3ota *p){
158226
+ OtaObjIter *pIter = &p->objiter;
158227
+ const char *zMask = 0;
158228
+ int i;
158229
+ int eType = otaStepType(p, &zMask);
158230
+
158231
+ if( eType ){
158232
+ assert( eType!=OTA_UPDATE || pIter->zIdx==0 );
158233
+
158234
+ if( pIter->zIdx==0 && eType==OTA_IDX_DELETE ){
158235
+ otaBadControlError(p);
158236
+ }
158237
+ else if(
158238
+ eType==OTA_INSERT
158239
+ || eType==OTA_DELETE
158240
+ || eType==OTA_IDX_DELETE
158241
+ || eType==OTA_IDX_INSERT
158242
+ ){
158243
+ sqlite3_value *pVal;
158244
+ sqlite3_stmt *pWriter;
158245
+
158246
+ assert( eType!=OTA_UPDATE );
158247
+ assert( eType!=OTA_DELETE || pIter->zIdx==0 );
158248
+
158249
+ if( eType==OTA_IDX_DELETE || eType==OTA_DELETE ){
158250
+ pWriter = pIter->pDelete;
158251
+ }else{
158252
+ pWriter = pIter->pInsert;
158253
+ }
158254
+
158255
+ for(i=0; i<pIter->nCol; i++){
158256
+ /* If this is an INSERT into a table b-tree and the table has an
158257
+ ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
158258
+ ** to write a NULL into the IPK column. That is not permitted. */
158259
+ if( eType==OTA_INSERT
158260
+ && pIter->zIdx==0 && pIter->eType==OTA_PK_IPK && pIter->abTblPk[i]
158261
+ && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
158262
+ ){
158263
+ p->rc = SQLITE_MISMATCH;
158264
+ p->zErrmsg = sqlite3_mprintf("datatype mismatch");
158265
+ goto step_out;
158266
+ }
158267
+
158268
+ if( eType==OTA_DELETE && pIter->abTblPk[i]==0 ){
158269
+ continue;
158270
+ }
158271
+
158272
+ pVal = sqlite3_column_value(pIter->pSelect, i);
158273
+ p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
158274
+ if( p->rc ) goto step_out;
158275
+ }
158276
+ if( pIter->zIdx==0
158277
+ && (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
158278
+ ){
158279
+ /* For a virtual table, or a table with no primary key, the
158280
+ ** SELECT statement is:
158281
+ **
158282
+ ** SELECT <cols>, ota_control, ota_rowid FROM ....
158283
+ **
158284
+ ** Hence column_value(pIter->nCol+1).
158285
+ */
158286
+ assertColumnName(pIter->pSelect, pIter->nCol+1, "ota_rowid");
158287
+ pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
158288
+ p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
158289
+ }
158290
+ if( p->rc==SQLITE_OK ){
158291
+ sqlite3_step(pWriter);
158292
+ p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
158293
+ }
158294
+ }else{
158295
+ sqlite3_value *pVal;
158296
+ sqlite3_stmt *pUpdate = 0;
158297
+ assert( eType==OTA_UPDATE );
158298
+ otaGetUpdateStmt(p, pIter, zMask, &pUpdate);
158299
+ if( pUpdate ){
158300
+ for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
158301
+ char c = zMask[pIter->aiSrcOrder[i]];
158302
+ pVal = sqlite3_column_value(pIter->pSelect, i);
158303
+ if( pIter->abTblPk[i] || c=='x' || c=='d' ){
158304
+ p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
158305
+ }
158306
+ }
158307
+ if( p->rc==SQLITE_OK
158308
+ && (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
158309
+ ){
158310
+ /* Bind the ota_rowid value to column _rowid_ */
158311
+ assertColumnName(pIter->pSelect, pIter->nCol+1, "ota_rowid");
158312
+ pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
158313
+ p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
158314
+ }
158315
+ if( p->rc==SQLITE_OK ){
158316
+ sqlite3_step(pUpdate);
158317
+ p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
158318
+ }
158319
+ }
158320
+ }
158321
+ }
158322
+
158323
+ step_out:
158324
+ return p->rc;
158325
+}
158326
+
158327
+/*
158328
+** Increment the schema cookie of the main database opened by p->dbMain.
158329
+*/
158330
+static void otaIncrSchemaCookie(sqlite3ota *p){
158331
+ if( p->rc==SQLITE_OK ){
158332
+ int iCookie = 1000000;
158333
+ sqlite3_stmt *pStmt;
158334
+
158335
+ p->rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
158336
+ "PRAGMA schema_version"
158337
+ );
158338
+ if( p->rc==SQLITE_OK ){
158339
+ /* Coverage: it may be that this sqlite3_step() cannot fail. There
158340
+ ** is already a transaction open, so the prepared statement cannot
158341
+ ** throw an SQLITE_SCHEMA exception. The only database page the
158342
+ ** statement reads is page 1, which is guaranteed to be in the cache.
158343
+ ** And no memory allocations are required. */
158344
+ if( SQLITE_ROW==sqlite3_step(pStmt) ){
158345
+ iCookie = sqlite3_column_int(pStmt, 0);
158346
+ }
158347
+ otaFinalize(p, pStmt);
158348
+ }
158349
+ if( p->rc==SQLITE_OK ){
158350
+ otaMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
158351
+ }
158352
+ }
158353
+}
158354
+
158355
+/*
158356
+** Update the contents of the ota_state table within the ota database. The
158357
+** value stored in the OTA_STATE_STAGE column is eStage. All other values
158358
+** are determined by inspecting the ota handle passed as the first argument.
158359
+*/
158360
+static void otaSaveState(sqlite3ota *p, int eStage){
158361
+ if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
158362
+ sqlite3_stmt *pInsert = 0;
158363
+ int rc;
158364
+
158365
+ assert( p->zErrmsg==0 );
158366
+ rc = prepareFreeAndCollectError(p->dbOta, &pInsert, &p->zErrmsg,
158367
+ sqlite3_mprintf(
158368
+ "INSERT OR REPLACE INTO %s.ota_state(k, v) VALUES "
158369
+ "(%d, %d), "
158370
+ "(%d, %Q), "
158371
+ "(%d, %Q), "
158372
+ "(%d, %d), "
158373
+ "(%d, %d), "
158374
+ "(%d, %lld), "
158375
+ "(%d, %lld), "
158376
+ "(%d, %lld) ",
158377
+ p->zStateDb,
158378
+ OTA_STATE_STAGE, eStage,
158379
+ OTA_STATE_TBL, p->objiter.zTbl,
158380
+ OTA_STATE_IDX, p->objiter.zIdx,
158381
+ OTA_STATE_ROW, p->nStep,
158382
+ OTA_STATE_PROGRESS, p->nProgress,
158383
+ OTA_STATE_CKPT, p->iWalCksum,
158384
+ OTA_STATE_COOKIE, (i64)p->pTargetFd->iCookie,
158385
+ OTA_STATE_OALSZ, p->iOalSz
158386
+ )
158387
+ );
158388
+ assert( pInsert==0 || rc==SQLITE_OK );
158389
+
158390
+ if( rc==SQLITE_OK ){
158391
+ sqlite3_step(pInsert);
158392
+ rc = sqlite3_finalize(pInsert);
158393
+ }
158394
+ if( rc!=SQLITE_OK ) p->rc = rc;
158395
+ }
158396
+}
158397
+
158398
+
158399
+/*
158400
+** Step the OTA object.
158401
+*/
158402
+SQLITE_API int SQLITE_STDCALL sqlite3ota_step(sqlite3ota *p){
158403
+ if( p ){
158404
+ switch( p->eStage ){
158405
+ case OTA_STAGE_OAL: {
158406
+ OtaObjIter *pIter = &p->objiter;
158407
+ while( p->rc==SQLITE_OK && pIter->zTbl ){
158408
+
158409
+ if( pIter->bCleanup ){
158410
+ /* Clean up the ota_tmp_xxx table for the previous table. It
158411
+ ** cannot be dropped as there are currently active SQL statements.
158412
+ ** But the contents can be deleted. */
158413
+ if( pIter->abIndexed ){
158414
+ otaMPrintfExec(p, p->dbOta,
158415
+ "DELETE FROM %s.'ota_tmp_%q'", p->zStateDb, pIter->zTbl
158416
+ );
158417
+ }
158418
+ }else{
158419
+ otaObjIterPrepareAll(p, pIter, 0);
158420
+
158421
+ /* Advance to the next row to process. */
158422
+ if( p->rc==SQLITE_OK ){
158423
+ int rc = sqlite3_step(pIter->pSelect);
158424
+ if( rc==SQLITE_ROW ){
158425
+ p->nProgress++;
158426
+ p->nStep++;
158427
+ return otaStep(p);
158428
+ }
158429
+ p->rc = sqlite3_reset(pIter->pSelect);
158430
+ p->nStep = 0;
158431
+ }
158432
+ }
158433
+
158434
+ otaObjIterNext(p, pIter);
158435
+ }
158436
+
158437
+ if( p->rc==SQLITE_OK ){
158438
+ assert( pIter->zTbl==0 );
158439
+ otaSaveState(p, OTA_STAGE_MOVE);
158440
+ otaIncrSchemaCookie(p);
158441
+ if( p->rc==SQLITE_OK ){
158442
+ p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
158443
+ }
158444
+ if( p->rc==SQLITE_OK ){
158445
+ p->rc = sqlite3_exec(p->dbOta, "COMMIT", 0, 0, &p->zErrmsg);
158446
+ }
158447
+ p->eStage = OTA_STAGE_MOVE;
158448
+ }
158449
+ break;
158450
+ }
158451
+
158452
+ case OTA_STAGE_MOVE: {
158453
+ if( p->rc==SQLITE_OK ){
158454
+ otaMoveOalFile(p);
158455
+ p->nProgress++;
158456
+ }
158457
+ break;
158458
+ }
158459
+
158460
+ case OTA_STAGE_CKPT: {
158461
+ if( p->rc==SQLITE_OK ){
158462
+ if( p->nStep>=p->nFrame ){
158463
+ sqlite3_file *pDb = p->pTargetFd->pReal;
158464
+
158465
+ /* Sync the db file */
158466
+ p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
158467
+
158468
+ /* Update nBackfill */
158469
+ if( p->rc==SQLITE_OK ){
158470
+ void volatile *ptr;
158471
+ p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
158472
+ if( p->rc==SQLITE_OK ){
158473
+ ((u32 volatile*)ptr)[24] = p->iMaxFrame;
158474
+ }
158475
+ }
158476
+
158477
+ if( p->rc==SQLITE_OK ){
158478
+ p->eStage = OTA_STAGE_DONE;
158479
+ p->rc = SQLITE_DONE;
158480
+ }
158481
+ }else{
158482
+ OtaFrame *pFrame = &p->aFrame[p->nStep];
158483
+ otaCheckpointFrame(p, pFrame);
158484
+ p->nStep++;
158485
+ }
158486
+ p->nProgress++;
158487
+ }
158488
+ break;
158489
+ }
158490
+
158491
+ default:
158492
+ break;
158493
+ }
158494
+ return p->rc;
158495
+ }else{
158496
+ return SQLITE_NOMEM;
158497
+ }
158498
+}
158499
+
158500
+/*
158501
+** Free an OtaState object allocated by otaLoadState().
158502
+*/
158503
+static void otaFreeState(OtaState *p){
158504
+ if( p ){
158505
+ sqlite3_free(p->zTbl);
158506
+ sqlite3_free(p->zIdx);
158507
+ sqlite3_free(p);
158508
+ }
158509
+}
158510
+
158511
+/*
158512
+** Allocate an OtaState object and load the contents of the ota_state
158513
+** table into it. Return a pointer to the new object. It is the
158514
+** responsibility of the caller to eventually free the object using
158515
+** sqlite3_free().
158516
+**
158517
+** If an error occurs, leave an error code and message in the ota handle
158518
+** and return NULL.
158519
+*/
158520
+static OtaState *otaLoadState(sqlite3ota *p){
158521
+ OtaState *pRet = 0;
158522
+ sqlite3_stmt *pStmt = 0;
158523
+ int rc;
158524
+ int rc2;
158525
+
158526
+ pRet = (OtaState*)otaMalloc(p, sizeof(OtaState));
158527
+ if( pRet==0 ) return 0;
158528
+
158529
+ rc = prepareFreeAndCollectError(p->dbOta, &pStmt, &p->zErrmsg,
158530
+ sqlite3_mprintf("SELECT k, v FROM %s.ota_state", p->zStateDb)
158531
+ );
158532
+ while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
158533
+ switch( sqlite3_column_int(pStmt, 0) ){
158534
+ case OTA_STATE_STAGE:
158535
+ pRet->eStage = sqlite3_column_int(pStmt, 1);
158536
+ if( pRet->eStage!=OTA_STAGE_OAL
158537
+ && pRet->eStage!=OTA_STAGE_MOVE
158538
+ && pRet->eStage!=OTA_STAGE_CKPT
158539
+ ){
158540
+ p->rc = SQLITE_CORRUPT;
158541
+ }
158542
+ break;
158543
+
158544
+ case OTA_STATE_TBL:
158545
+ pRet->zTbl = otaStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
158546
+ break;
158547
+
158548
+ case OTA_STATE_IDX:
158549
+ pRet->zIdx = otaStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
158550
+ break;
158551
+
158552
+ case OTA_STATE_ROW:
158553
+ pRet->nRow = sqlite3_column_int(pStmt, 1);
158554
+ break;
158555
+
158556
+ case OTA_STATE_PROGRESS:
158557
+ pRet->nProgress = sqlite3_column_int64(pStmt, 1);
158558
+ break;
158559
+
158560
+ case OTA_STATE_CKPT:
158561
+ pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
158562
+ break;
158563
+
158564
+ case OTA_STATE_COOKIE:
158565
+ pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
158566
+ break;
158567
+
158568
+ case OTA_STATE_OALSZ:
158569
+ pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
158570
+ break;
158571
+
158572
+ default:
158573
+ rc = SQLITE_CORRUPT;
158574
+ break;
158575
+ }
158576
+ }
158577
+ rc2 = sqlite3_finalize(pStmt);
158578
+ if( rc==SQLITE_OK ) rc = rc2;
158579
+
158580
+ p->rc = rc;
158581
+ return pRet;
158582
+}
158583
+
158584
+/*
158585
+** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
158586
+** otherwise. Either or both argument may be NULL. Two NULL values are
158587
+** considered equal, and NULL is considered distinct from all other values.
158588
+*/
158589
+static int otaStrCompare(const char *z1, const char *z2){
158590
+ if( z1==0 && z2==0 ) return 0;
158591
+ if( z1==0 || z2==0 ) return 1;
158592
+ return (sqlite3_stricmp(z1, z2)!=0);
158593
+}
158594
+
158595
+/*
158596
+** This function is called as part of sqlite3ota_open() when initializing
158597
+** an ota handle in OAL stage. If the ota update has not started (i.e.
158598
+** the ota_state table was empty) it is a no-op. Otherwise, it arranges
158599
+** things so that the next call to sqlite3ota_step() continues on from
158600
+** where the previous ota handle left off.
158601
+**
158602
+** If an error occurs, an error code and error message are left in the
158603
+** ota handle passed as the first argument.
158604
+*/
158605
+static void otaSetupOal(sqlite3ota *p, OtaState *pState){
158606
+ assert( p->rc==SQLITE_OK );
158607
+ if( pState->zTbl ){
158608
+ OtaObjIter *pIter = &p->objiter;
158609
+ int rc = SQLITE_OK;
158610
+
158611
+ while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
158612
+ || otaStrCompare(pIter->zIdx, pState->zIdx)
158613
+ || otaStrCompare(pIter->zTbl, pState->zTbl)
158614
+ )){
158615
+ rc = otaObjIterNext(p, pIter);
158616
+ }
158617
+
158618
+ if( rc==SQLITE_OK && !pIter->zTbl ){
158619
+ rc = SQLITE_ERROR;
158620
+ p->zErrmsg = sqlite3_mprintf("ota_state mismatch error");
158621
+ }
158622
+
158623
+ if( rc==SQLITE_OK ){
158624
+ p->nStep = pState->nRow;
158625
+ rc = otaObjIterPrepareAll(p, &p->objiter, p->nStep);
158626
+ }
158627
+
158628
+ p->rc = rc;
158629
+ }
158630
+}
158631
+
158632
+/*
158633
+** If there is a "*-oal" file in the file-system corresponding to the
158634
+** target database in the file-system, delete it. If an error occurs,
158635
+** leave an error code and error message in the ota handle.
158636
+*/
158637
+static void otaDeleteOalFile(sqlite3ota *p){
158638
+ char *zOal = sqlite3_mprintf("%s-oal", p->zTarget);
158639
+ assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
158640
+ unlink(zOal);
158641
+ sqlite3_free(zOal);
158642
+}
158643
+
158644
+/*
158645
+** Allocate a private ota VFS for the ota handle passed as the only
158646
+** argument. This VFS will be used unless the call to sqlite3ota_open()
158647
+** specified a URI with a vfs=? option in place of a target database
158648
+** file name.
158649
+*/
158650
+static void otaCreateVfs(sqlite3ota *p){
158651
+ int rnd;
158652
+ char zRnd[64];
158653
+
158654
+ assert( p->rc==SQLITE_OK );
158655
+ sqlite3_randomness(sizeof(int), (void*)&rnd);
158656
+ sprintf(zRnd, "ota_vfs_%d", rnd);
158657
+ p->rc = sqlite3ota_create_vfs(zRnd, 0);
158658
+ if( p->rc==SQLITE_OK ){
158659
+ sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
158660
+ assert( pVfs );
158661
+ p->zVfsName = pVfs->zName;
158662
+ }
158663
+}
158664
+
158665
+/*
158666
+** Destroy the private VFS created for the ota handle passed as the only
158667
+** argument by an earlier call to otaCreateVfs().
158668
+*/
158669
+static void otaDeleteVfs(sqlite3ota *p){
158670
+ if( p->zVfsName ){
158671
+ sqlite3ota_destroy_vfs(p->zVfsName);
158672
+ p->zVfsName = 0;
158673
+ }
158674
+}
158675
+
158676
+/*
158677
+** Open and return a new OTA handle.
158678
+*/
158679
+SQLITE_API sqlite3ota *SQLITE_STDCALL sqlite3ota_open(
158680
+ const char *zTarget,
158681
+ const char *zOta,
158682
+ const char *zState
158683
+){
158684
+ sqlite3ota *p;
158685
+ int nTarget = strlen(zTarget);
158686
+ int nOta = strlen(zOta);
158687
+ int nState = zState ? strlen(zState) : 0;
158688
+
158689
+ p = (sqlite3ota*)sqlite3_malloc(sizeof(sqlite3ota)+nTarget+1+nOta+1+nState+1);
158690
+ if( p ){
158691
+ OtaState *pState = 0;
158692
+
158693
+ /* Create the custom VFS. */
158694
+ memset(p, 0, sizeof(sqlite3ota));
158695
+ otaCreateVfs(p);
158696
+
158697
+ /* Open the target database */
158698
+ if( p->rc==SQLITE_OK ){
158699
+ p->zTarget = (char*)&p[1];
158700
+ memcpy(p->zTarget, zTarget, nTarget+1);
158701
+ p->zOta = &p->zTarget[nTarget+1];
158702
+ memcpy(p->zOta, zOta, nOta+1);
158703
+ if( zState ){
158704
+ p->zState = &p->zOta[nOta+1];
158705
+ memcpy(p->zState, zState, nState+1);
158706
+ }
158707
+ otaOpenDatabase(p);
158708
+ }
158709
+
158710
+ /* If it has not already been created, create the ota_state table */
158711
+ otaMPrintfExec(p, p->dbOta, OTA_CREATE_STATE, p->zStateDb);
158712
+
158713
+ if( p->rc==SQLITE_OK ){
158714
+ pState = otaLoadState(p);
158715
+ assert( pState || p->rc!=SQLITE_OK );
158716
+ if( p->rc==SQLITE_OK ){
158717
+
158718
+ if( pState->eStage==0 ){
158719
+ otaDeleteOalFile(p);
158720
+ p->eStage = OTA_STAGE_OAL;
158721
+ }else{
158722
+ p->eStage = pState->eStage;
158723
+ }
158724
+ p->nProgress = pState->nProgress;
158725
+ p->iOalSz = pState->iOalSz;
158726
+ }
158727
+ }
158728
+ assert( p->rc!=SQLITE_OK || p->eStage!=0 );
158729
+
158730
+ if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
158731
+ if( p->eStage==OTA_STAGE_OAL ){
158732
+ p->rc = SQLITE_ERROR;
158733
+ p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
158734
+ }else if( p->eStage==OTA_STAGE_MOVE ){
158735
+ p->eStage = OTA_STAGE_CKPT;
158736
+ p->nStep = 0;
158737
+ }
158738
+ }
158739
+
158740
+ if( p->rc==SQLITE_OK
158741
+ && (p->eStage==OTA_STAGE_OAL || p->eStage==OTA_STAGE_MOVE)
158742
+ && pState->eStage!=0 && p->pTargetFd->iCookie!=pState->iCookie
158743
+ ){
158744
+ /* At this point (pTargetFd->iCookie) contains the value of the
158745
+ ** change-counter cookie (the thing that gets incremented when a
158746
+ ** transaction is committed in rollback mode) currently stored on
158747
+ ** page 1 of the database file. */
158748
+ p->rc = SQLITE_BUSY;
158749
+ p->zErrmsg = sqlite3_mprintf("database modified during ota update");
158750
+ }
158751
+
158752
+ if( p->rc==SQLITE_OK ){
158753
+ if( p->eStage==OTA_STAGE_OAL ){
158754
+
158755
+ /* Open transactions both databases. The *-oal file is opened or
158756
+ ** created at this point. */
158757
+ p->rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
158758
+ if( p->rc==SQLITE_OK ){
158759
+ p->rc = sqlite3_exec(p->dbOta, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
158760
+ }
158761
+
158762
+ /* Point the object iterator at the first object */
158763
+ if( p->rc==SQLITE_OK ){
158764
+ p->rc = otaObjIterFirst(p, &p->objiter);
158765
+ }
158766
+
158767
+ /* If the OTA database contains no data_xxx tables, declare the OTA
158768
+ ** update finished. */
158769
+ if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
158770
+ p->rc = SQLITE_DONE;
158771
+ }
158772
+
158773
+ if( p->rc==SQLITE_OK ){
158774
+ otaSetupOal(p, pState);
158775
+ }
158776
+
158777
+ }else if( p->eStage==OTA_STAGE_MOVE ){
158778
+ /* no-op */
158779
+ }else if( p->eStage==OTA_STAGE_CKPT ){
158780
+ otaSetupCheckpoint(p, pState);
158781
+ }else if( p->eStage==OTA_STAGE_DONE ){
158782
+ p->rc = SQLITE_DONE;
158783
+ }else{
158784
+ p->rc = SQLITE_CORRUPT;
158785
+ }
158786
+ }
158787
+
158788
+ otaFreeState(pState);
158789
+ }
158790
+
158791
+ return p;
158792
+}
158793
+
158794
+
158795
+/*
158796
+** Return the database handle used by pOta.
158797
+*/
158798
+SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3ota_db(sqlite3ota *pOta, int bOta){
158799
+ sqlite3 *db = 0;
158800
+ if( pOta ){
158801
+ db = (bOta ? pOta->dbOta : pOta->dbMain);
158802
+ }
158803
+ return db;
158804
+}
158805
+
158806
+
158807
+/*
158808
+** If the error code currently stored in the OTA handle is SQLITE_CONSTRAINT,
158809
+** then edit any error message string so as to remove all occurrences of
158810
+** the pattern "ota_imp_[0-9]*".
158811
+*/
158812
+static void otaEditErrmsg(sqlite3ota *p){
158813
+ if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
158814
+ int i;
158815
+ int nErrmsg = strlen(p->zErrmsg);
158816
+ for(i=0; i<(nErrmsg-8); i++){
158817
+ if( memcmp(&p->zErrmsg[i], "ota_imp_", 8)==0 ){
158818
+ int nDel = 8;
158819
+ while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
158820
+ memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
158821
+ nErrmsg -= nDel;
158822
+ }
158823
+ }
158824
+ }
158825
+}
158826
+
158827
+/*
158828
+** Close the OTA handle.
158829
+*/
158830
+SQLITE_API int SQLITE_STDCALL sqlite3ota_close(sqlite3ota *p, char **pzErrmsg){
158831
+ int rc;
158832
+ if( p ){
158833
+
158834
+ /* Commit the transaction to the *-oal file. */
158835
+ if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
158836
+ p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
158837
+ }
158838
+
158839
+ otaSaveState(p, p->eStage);
158840
+
158841
+ if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
158842
+ p->rc = sqlite3_exec(p->dbOta, "COMMIT", 0, 0, &p->zErrmsg);
158843
+ }
158844
+
158845
+ /* Close any open statement handles. */
158846
+ otaObjIterFinalize(&p->objiter);
158847
+
158848
+ /* Close the open database handle and VFS object. */
158849
+ sqlite3_close(p->dbMain);
158850
+ sqlite3_close(p->dbOta);
158851
+ otaDeleteVfs(p);
158852
+ sqlite3_free(p->aBuf);
158853
+ sqlite3_free(p->aFrame);
158854
+
158855
+ otaEditErrmsg(p);
158856
+ rc = p->rc;
158857
+ *pzErrmsg = p->zErrmsg;
158858
+ sqlite3_free(p);
158859
+ }else{
158860
+ rc = SQLITE_NOMEM;
158861
+ *pzErrmsg = 0;
158862
+ }
158863
+ return rc;
158864
+}
158865
+
158866
+/*
158867
+** Return the total number of key-value operations (inserts, deletes or
158868
+** updates) that have been performed on the target database since the
158869
+** current OTA update was started.
158870
+*/
158871
+SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3ota_progress(sqlite3ota *pOta){
158872
+ return pOta->nProgress;
158873
+}
158874
+
158875
+/**************************************************************************
158876
+** Beginning of OTA VFS shim methods. The VFS shim modifies the behaviour
158877
+** of a standard VFS in the following ways:
158878
+**
158879
+** 1. Whenever the first page of a main database file is read or
158880
+** written, the value of the change-counter cookie is stored in
158881
+** ota_file.iCookie. Similarly, the value of the "write-version"
158882
+** database header field is stored in ota_file.iWriteVer. This ensures
158883
+** that the values are always trustworthy within an open transaction.
158884
+**
158885
+** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (ota_file.pWalFd)
158886
+** member variable of the associated database file descriptor is set
158887
+** to point to the new file. A mutex protected linked list of all main
158888
+** db fds opened using a particular OTA VFS is maintained at
158889
+** ota_vfs.pMain to facilitate this.
158890
+**
158891
+** 3. Using a new file-control "SQLITE_FCNTL_OTA", a main db ota_file
158892
+** object can be marked as the target database of an OTA update. This
158893
+** turns on the following extra special behaviour:
158894
+**
158895
+** 3a. If xAccess() is called to check if there exists a *-wal file
158896
+** associated with an OTA target database currently in OTA_STAGE_OAL
158897
+** stage (preparing the *-oal file), the following special handling
158898
+** applies:
158899
+**
158900
+** * if the *-wal file does exist, return SQLITE_CANTOPEN. An OTA
158901
+** target database may not be in wal mode already.
158902
+**
158903
+** * if the *-wal file does not exist, set the output parameter to
158904
+** non-zero (to tell SQLite that it does exist) anyway.
158905
+**
158906
+** Then, when xOpen() is called to open the *-wal file associated with
158907
+** the OTA target in OTA_STAGE_OAL stage, instead of opening the *-wal
158908
+** file, the ota vfs opens the corresponding *-oal file instead.
158909
+**
158910
+** 3b. The *-shm pages returned by xShmMap() for a target db file in
158911
+** OTA_STAGE_OAL mode are actually stored in heap memory. This is to
158912
+** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
158913
+** are no-ops on target database files in OTA_STAGE_OAL mode. This is
158914
+** because assert() statements in some VFS implementations fail if
158915
+** xShmLock() is called before xShmMap().
158916
+**
158917
+** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
158918
+** mode except OTA_STAGE_DONE (all work completed and checkpointed), it
158919
+** fails with an SQLITE_BUSY error. This is to stop OTA connections
158920
+** from automatically checkpointing a *-wal (or *-oal) file from within
158921
+** sqlite3_close().
158922
+**
158923
+** 3d. In OTA_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
158924
+** all xWrite() calls on the target database file perform no IO.
158925
+** Instead the frame and page numbers that would be read and written
158926
+** are recorded. Additionally, successful attempts to obtain exclusive
158927
+** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
158928
+** database file are recorded. xShmLock() calls to unlock the same
158929
+** locks are no-ops (so that once obtained, these locks are never
158930
+** relinquished). Finally, calls to xSync() on the target database
158931
+** file fail with SQLITE_INTERNAL errors.
158932
+*/
158933
+
158934
+static void otaUnlockShm(ota_file *p){
158935
+ if( p->pOta ){
158936
+ int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
158937
+ int i;
158938
+ for(i=0; i<SQLITE_SHM_NLOCK;i++){
158939
+ if( (1<<i) & p->pOta->mLock ){
158940
+ xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
158941
+ }
158942
+ }
158943
+ p->pOta->mLock = 0;
158944
+ }
158945
+}
158946
+
158947
+/*
158948
+** Close an ota file.
158949
+*/
158950
+static int otaVfsClose(sqlite3_file *pFile){
158951
+ ota_file *p = (ota_file*)pFile;
158952
+ int rc;
158953
+ int i;
158954
+
158955
+ /* Free the contents of the apShm[] array. And the array itself. */
158956
+ for(i=0; i<p->nShm; i++){
158957
+ sqlite3_free(p->apShm[i]);
158958
+ }
158959
+ sqlite3_free(p->apShm);
158960
+ p->apShm = 0;
158961
+ sqlite3_free(p->zDel);
158962
+
158963
+ if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
158964
+ ota_file **pp;
158965
+ sqlite3_mutex_enter(p->pOtaVfs->mutex);
158966
+ for(pp=&p->pOtaVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
158967
+ *pp = p->pMainNext;
158968
+ sqlite3_mutex_leave(p->pOtaVfs->mutex);
158969
+ otaUnlockShm(p);
158970
+ p->pReal->pMethods->xShmUnmap(p->pReal, 0);
158971
+ }
158972
+
158973
+ /* Close the underlying file handle */
158974
+ rc = p->pReal->pMethods->xClose(p->pReal);
158975
+ return rc;
158976
+}
158977
+
158978
+
158979
+/*
158980
+** Read and return an unsigned 32-bit big-endian integer from the buffer
158981
+** passed as the only argument.
158982
+*/
158983
+static u32 otaGetU32(u8 *aBuf){
158984
+ return ((u32)aBuf[0] << 24)
158985
+ + ((u32)aBuf[1] << 16)
158986
+ + ((u32)aBuf[2] << 8)
158987
+ + ((u32)aBuf[3]);
158988
+}
158989
+
158990
+/*
158991
+** Read data from an otaVfs-file.
158992
+*/
158993
+static int otaVfsRead(
158994
+ sqlite3_file *pFile,
158995
+ void *zBuf,
158996
+ int iAmt,
158997
+ sqlite_int64 iOfst
158998
+){
158999
+ ota_file *p = (ota_file*)pFile;
159000
+ sqlite3ota *pOta = p->pOta;
159001
+ int rc;
159002
+
159003
+ if( pOta && pOta->eStage==OTA_STAGE_CAPTURE ){
159004
+ assert( p->openFlags & SQLITE_OPEN_WAL );
159005
+ rc = otaCaptureWalRead(p->pOta, iOfst, iAmt);
159006
+ }else{
159007
+ if( pOta && pOta->eStage==OTA_STAGE_OAL
159008
+ && (p->openFlags & SQLITE_OPEN_WAL)
159009
+ && iOfst>=pOta->iOalSz
159010
+ ){
159011
+ rc = SQLITE_OK;
159012
+ memset(zBuf, 0, iAmt);
159013
+ }else{
159014
+ rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
159015
+ }
159016
+ if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
159017
+ /* These look like magic numbers. But they are stable, as they are part
159018
+ ** of the definition of the SQLite file format, which may not change. */
159019
+ u8 *pBuf = (u8*)zBuf;
159020
+ p->iCookie = otaGetU32(&pBuf[24]);
159021
+ p->iWriteVer = pBuf[19];
159022
+ }
159023
+ }
159024
+ return rc;
159025
+}
159026
+
159027
+/*
159028
+** Write data to an otaVfs-file.
159029
+*/
159030
+static int otaVfsWrite(
159031
+ sqlite3_file *pFile,
159032
+ const void *zBuf,
159033
+ int iAmt,
159034
+ sqlite_int64 iOfst
159035
+){
159036
+ ota_file *p = (ota_file*)pFile;
159037
+ sqlite3ota *pOta = p->pOta;
159038
+ int rc;
159039
+
159040
+ if( pOta && pOta->eStage==OTA_STAGE_CAPTURE ){
159041
+ assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
159042
+ rc = otaCaptureDbWrite(p->pOta, iOfst);
159043
+ }else{
159044
+ if( pOta && pOta->eStage==OTA_STAGE_OAL
159045
+ && (p->openFlags & SQLITE_OPEN_WAL)
159046
+ && iOfst>=pOta->iOalSz
159047
+ ){
159048
+ pOta->iOalSz = iAmt + iOfst;
159049
+ }
159050
+ rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
159051
+ if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
159052
+ /* These look like magic numbers. But they are stable, as they are part
159053
+ ** of the definition of the SQLite file format, which may not change. */
159054
+ u8 *pBuf = (u8*)zBuf;
159055
+ p->iCookie = otaGetU32(&pBuf[24]);
159056
+ p->iWriteVer = pBuf[19];
159057
+ }
159058
+ }
159059
+ return rc;
159060
+}
159061
+
159062
+/*
159063
+** Truncate an otaVfs-file.
159064
+*/
159065
+static int otaVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
159066
+ ota_file *p = (ota_file*)pFile;
159067
+ return p->pReal->pMethods->xTruncate(p->pReal, size);
159068
+}
159069
+
159070
+/*
159071
+** Sync an otaVfs-file.
159072
+*/
159073
+static int otaVfsSync(sqlite3_file *pFile, int flags){
159074
+ ota_file *p = (ota_file *)pFile;
159075
+ if( p->pOta && p->pOta->eStage==OTA_STAGE_CAPTURE ){
159076
+ if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
159077
+ return SQLITE_INTERNAL;
159078
+ }
159079
+ return SQLITE_OK;
159080
+ }
159081
+ return p->pReal->pMethods->xSync(p->pReal, flags);
159082
+}
159083
+
159084
+/*
159085
+** Return the current file-size of an otaVfs-file.
159086
+*/
159087
+static int otaVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
159088
+ ota_file *p = (ota_file *)pFile;
159089
+ return p->pReal->pMethods->xFileSize(p->pReal, pSize);
159090
+}
159091
+
159092
+/*
159093
+** Lock an otaVfs-file.
159094
+*/
159095
+static int otaVfsLock(sqlite3_file *pFile, int eLock){
159096
+ ota_file *p = (ota_file*)pFile;
159097
+ sqlite3ota *pOta = p->pOta;
159098
+ int rc = SQLITE_OK;
159099
+
159100
+ assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
159101
+ if( pOta && eLock==SQLITE_LOCK_EXCLUSIVE && pOta->eStage!=OTA_STAGE_DONE ){
159102
+ /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
159103
+ ** prevents it from checkpointing the database from sqlite3_close(). */
159104
+ rc = SQLITE_BUSY;
159105
+ }else{
159106
+ rc = p->pReal->pMethods->xLock(p->pReal, eLock);
159107
+ }
159108
+
159109
+ return rc;
159110
+}
159111
+
159112
+/*
159113
+** Unlock an otaVfs-file.
159114
+*/
159115
+static int otaVfsUnlock(sqlite3_file *pFile, int eLock){
159116
+ ota_file *p = (ota_file *)pFile;
159117
+ return p->pReal->pMethods->xUnlock(p->pReal, eLock);
159118
+}
159119
+
159120
+/*
159121
+** Check if another file-handle holds a RESERVED lock on an otaVfs-file.
159122
+*/
159123
+static int otaVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
159124
+ ota_file *p = (ota_file *)pFile;
159125
+ return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
159126
+}
159127
+
159128
+/*
159129
+** File control method. For custom operations on an otaVfs-file.
159130
+*/
159131
+static int otaVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
159132
+ ota_file *p = (ota_file *)pFile;
159133
+ int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
159134
+ int rc;
159135
+
159136
+ assert( p->openFlags &
159137
+ (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB|SQLITE_OPEN_TRANSIENT_DB)
159138
+ );
159139
+ if( op==SQLITE_FCNTL_OTA ){
159140
+ sqlite3ota *pOta = (sqlite3ota*)pArg;
159141
+
159142
+ /* First try to find another OTA vfs lower down in the vfs stack. If
159143
+ ** one is found, this vfs will operate in pass-through mode. The lower
159144
+ ** level vfs will do the special OTA handling. */
159145
+ rc = xControl(p->pReal, op, pArg);
159146
+
159147
+ if( rc==SQLITE_NOTFOUND ){
159148
+ /* Now search for a zipvfs instance lower down in the VFS stack. If
159149
+ ** one is found, this is an error. */
159150
+ void *dummy = 0;
159151
+ rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
159152
+ if( rc==SQLITE_OK ){
159153
+ rc = SQLITE_ERROR;
159154
+ pOta->zErrmsg = sqlite3_mprintf("ota/zipvfs setup error");
159155
+ }else if( rc==SQLITE_NOTFOUND ){
159156
+ pOta->pTargetFd = p;
159157
+ p->pOta = pOta;
159158
+ if( p->pWalFd ) p->pWalFd->pOta = pOta;
159159
+ rc = SQLITE_OK;
159160
+ }
159161
+ }
159162
+ return rc;
159163
+ }
159164
+
159165
+ rc = xControl(p->pReal, op, pArg);
159166
+ if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
159167
+ ota_vfs *pOtaVfs = p->pOtaVfs;
159168
+ char *zIn = *(char**)pArg;
159169
+ char *zOut = sqlite3_mprintf("ota(%s)/%z", pOtaVfs->base.zName, zIn);
159170
+ *(char**)pArg = zOut;
159171
+ if( zOut==0 ) rc = SQLITE_NOMEM;
159172
+ }
159173
+
159174
+ return rc;
159175
+}
159176
+
159177
+/*
159178
+** Return the sector-size in bytes for an otaVfs-file.
159179
+*/
159180
+static int otaVfsSectorSize(sqlite3_file *pFile){
159181
+ ota_file *p = (ota_file *)pFile;
159182
+ return p->pReal->pMethods->xSectorSize(p->pReal);
159183
+}
159184
+
159185
+/*
159186
+** Return the device characteristic flags supported by an otaVfs-file.
159187
+*/
159188
+static int otaVfsDeviceCharacteristics(sqlite3_file *pFile){
159189
+ ota_file *p = (ota_file *)pFile;
159190
+ return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
159191
+}
159192
+
159193
+/*
159194
+** Take or release a shared-memory lock.
159195
+*/
159196
+static int otaVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
159197
+ ota_file *p = (ota_file*)pFile;
159198
+ sqlite3ota *pOta = p->pOta;
159199
+ int rc = SQLITE_OK;
159200
+
159201
+#ifdef SQLITE_AMALGAMATION
159202
+ assert( WAL_CKPT_LOCK==1 );
159203
+#endif
159204
+
159205
+ assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
159206
+ if( pOta && (pOta->eStage==OTA_STAGE_OAL || pOta->eStage==OTA_STAGE_MOVE) ){
159207
+ /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
159208
+ ** taking this lock also prevents any checkpoints from occurring.
159209
+ ** todo: really, it's not clear why this might occur, as
159210
+ ** wal_autocheckpoint ought to be turned off. */
159211
+ if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
159212
+ }else{
159213
+ int bCapture = 0;
159214
+ if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
159215
+ && pOta && pOta->eStage==OTA_STAGE_CAPTURE
159216
+ && (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
159217
+ ){
159218
+ bCapture = 1;
159219
+ }
159220
+
159221
+ if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
159222
+ rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
159223
+ if( bCapture && rc==SQLITE_OK ){
159224
+ pOta->mLock |= (1 << ofst);
159225
+ }
159226
+ }
159227
+ }
159228
+
159229
+ return rc;
159230
+}
159231
+
159232
+/*
159233
+** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
159234
+*/
159235
+static int otaVfsShmMap(
159236
+ sqlite3_file *pFile,
159237
+ int iRegion,
159238
+ int szRegion,
159239
+ int isWrite,
159240
+ void volatile **pp
159241
+){
159242
+ ota_file *p = (ota_file*)pFile;
159243
+ int rc = SQLITE_OK;
159244
+ int eStage = (p->pOta ? p->pOta->eStage : 0);
159245
+
159246
+ /* If not in OTA_STAGE_OAL, allow this call to pass through. Or, if this
159247
+ ** ota is in the OTA_STAGE_OAL state, use heap memory for *-shm space
159248
+ ** instead of a file on disk. */
159249
+ assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
159250
+ if( eStage==OTA_STAGE_OAL || eStage==OTA_STAGE_MOVE ){
159251
+ if( iRegion<=p->nShm ){
159252
+ int nByte = (iRegion+1) * sizeof(char*);
159253
+ char **apNew = (char**)sqlite3_realloc(p->apShm, nByte);
159254
+ if( apNew==0 ){
159255
+ rc = SQLITE_NOMEM;
159256
+ }else{
159257
+ memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
159258
+ p->apShm = apNew;
159259
+ p->nShm = iRegion+1;
159260
+ }
159261
+ }
159262
+
159263
+ if( rc==SQLITE_OK && p->apShm[iRegion]==0 ){
159264
+ char *pNew = (char*)sqlite3_malloc(szRegion);
159265
+ if( pNew==0 ){
159266
+ rc = SQLITE_NOMEM;
159267
+ }else{
159268
+ memset(pNew, 0, szRegion);
159269
+ p->apShm[iRegion] = pNew;
159270
+ }
159271
+ }
159272
+
159273
+ if( rc==SQLITE_OK ){
159274
+ *pp = p->apShm[iRegion];
159275
+ }else{
159276
+ *pp = 0;
159277
+ }
159278
+ }else{
159279
+ assert( p->apShm==0 );
159280
+ rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
159281
+ }
159282
+
159283
+ return rc;
159284
+}
159285
+
159286
+/*
159287
+** Memory barrier.
159288
+*/
159289
+static void otaVfsShmBarrier(sqlite3_file *pFile){
159290
+ ota_file *p = (ota_file *)pFile;
159291
+ p->pReal->pMethods->xShmBarrier(p->pReal);
159292
+}
159293
+
159294
+/*
159295
+** The xShmUnmap method.
159296
+*/
159297
+static int otaVfsShmUnmap(sqlite3_file *pFile, int delFlag){
159298
+ ota_file *p = (ota_file*)pFile;
159299
+ int rc = SQLITE_OK;
159300
+ int eStage = (p->pOta ? p->pOta->eStage : 0);
159301
+
159302
+ assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
159303
+ if( eStage==OTA_STAGE_OAL || eStage==OTA_STAGE_MOVE ){
159304
+ /* no-op */
159305
+ }else{
159306
+ /* Release the checkpointer and writer locks */
159307
+ otaUnlockShm(p);
159308
+ rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
159309
+ }
159310
+ return rc;
159311
+}
159312
+
159313
+/*
159314
+** Given that zWal points to a buffer containing a wal file name passed to
159315
+** either the xOpen() or xAccess() VFS method, return a pointer to the
159316
+** file-handle opened by the same database connection on the corresponding
159317
+** database file.
159318
+*/
159319
+static ota_file *otaFindMaindb(ota_vfs *pOtaVfs, const char *zWal){
159320
+ ota_file *pDb;
159321
+ sqlite3_mutex_enter(pOtaVfs->mutex);
159322
+ for(pDb=pOtaVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext);
159323
+ sqlite3_mutex_leave(pOtaVfs->mutex);
159324
+ return pDb;
159325
+}
159326
+
159327
+/*
159328
+** Open an ota file handle.
159329
+*/
159330
+static int otaVfsOpen(
159331
+ sqlite3_vfs *pVfs,
159332
+ const char *zName,
159333
+ sqlite3_file *pFile,
159334
+ int flags,
159335
+ int *pOutFlags
159336
+){
159337
+ static sqlite3_io_methods otavfs_io_methods = {
159338
+ 2, /* iVersion */
159339
+ otaVfsClose, /* xClose */
159340
+ otaVfsRead, /* xRead */
159341
+ otaVfsWrite, /* xWrite */
159342
+ otaVfsTruncate, /* xTruncate */
159343
+ otaVfsSync, /* xSync */
159344
+ otaVfsFileSize, /* xFileSize */
159345
+ otaVfsLock, /* xLock */
159346
+ otaVfsUnlock, /* xUnlock */
159347
+ otaVfsCheckReservedLock, /* xCheckReservedLock */
159348
+ otaVfsFileControl, /* xFileControl */
159349
+ otaVfsSectorSize, /* xSectorSize */
159350
+ otaVfsDeviceCharacteristics, /* xDeviceCharacteristics */
159351
+ otaVfsShmMap, /* xShmMap */
159352
+ otaVfsShmLock, /* xShmLock */
159353
+ otaVfsShmBarrier, /* xShmBarrier */
159354
+ otaVfsShmUnmap /* xShmUnmap */
159355
+ };
159356
+ ota_vfs *pOtaVfs = (ota_vfs*)pVfs;
159357
+ sqlite3_vfs *pRealVfs = pOtaVfs->pRealVfs;
159358
+ ota_file *pFd = (ota_file *)pFile;
159359
+ int rc = SQLITE_OK;
159360
+ const char *zOpen = zName;
159361
+
159362
+ memset(pFd, 0, sizeof(ota_file));
159363
+ pFd->pReal = (sqlite3_file*)&pFd[1];
159364
+ pFd->pOtaVfs = pOtaVfs;
159365
+ pFd->openFlags = flags;
159366
+ if( zName ){
159367
+ if( flags & SQLITE_OPEN_MAIN_DB ){
159368
+ /* A main database has just been opened. The following block sets
159369
+ ** (pFd->zWal) to point to a buffer owned by SQLite that contains
159370
+ ** the name of the *-wal file this db connection will use. SQLite
159371
+ ** happens to pass a pointer to this buffer when using xAccess()
159372
+ ** or xOpen() to operate on the *-wal file. */
159373
+ int n = strlen(zName);
159374
+ const char *z = &zName[n];
159375
+ if( flags & SQLITE_OPEN_URI ){
159376
+ int odd = 0;
159377
+ while( 1 ){
159378
+ if( z[0]==0 ){
159379
+ odd = 1 - odd;
159380
+ if( odd && z[1]==0 ) break;
159381
+ }
159382
+ z++;
159383
+ }
159384
+ z += 2;
159385
+ }else{
159386
+ while( *z==0 ) z++;
159387
+ }
159388
+ z += (n + 8 + 1);
159389
+ pFd->zWal = z;
159390
+ }
159391
+ else if( flags & SQLITE_OPEN_WAL ){
159392
+ ota_file *pDb = otaFindMaindb(pOtaVfs, zName);
159393
+ if( pDb ){
159394
+ if( pDb->pOta && pDb->pOta->eStage==OTA_STAGE_OAL ){
159395
+ /* This call is to open a *-wal file. Intead, open the *-oal. This
159396
+ ** code ensures that the string passed to xOpen() is terminated by a
159397
+ ** pair of '\0' bytes in case the VFS attempts to extract a URI
159398
+ ** parameter from it. */
159399
+ int nCopy = strlen(zName);
159400
+ char *zCopy = sqlite3_malloc(nCopy+2);
159401
+ if( zCopy ){
159402
+ memcpy(zCopy, zName, nCopy);
159403
+ zCopy[nCopy-3] = 'o';
159404
+ zCopy[nCopy] = '\0';
159405
+ zCopy[nCopy+1] = '\0';
159406
+ zOpen = (const char*)(pFd->zDel = zCopy);
159407
+ }else{
159408
+ rc = SQLITE_NOMEM;
159409
+ }
159410
+ pFd->pOta = pDb->pOta;
159411
+ }
159412
+ pDb->pWalFd = pFd;
159413
+ }
159414
+ }
159415
+ }
159416
+
159417
+ if( rc==SQLITE_OK ){
159418
+ rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, flags, pOutFlags);
159419
+ }
159420
+ if( pFd->pReal->pMethods ){
159421
+ /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
159422
+ ** pointer and, if the file is a main database file, link it into the
159423
+ ** mutex protected linked list of all such files. */
159424
+ pFile->pMethods = &otavfs_io_methods;
159425
+ if( flags & SQLITE_OPEN_MAIN_DB ){
159426
+ sqlite3_mutex_enter(pOtaVfs->mutex);
159427
+ pFd->pMainNext = pOtaVfs->pMain;
159428
+ pOtaVfs->pMain = pFd;
159429
+ sqlite3_mutex_leave(pOtaVfs->mutex);
159430
+ }
159431
+ }else{
159432
+ sqlite3_free(pFd->zDel);
159433
+ }
159434
+
159435
+ return rc;
159436
+}
159437
+
159438
+/*
159439
+** Delete the file located at zPath.
159440
+*/
159441
+static int otaVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
159442
+ sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159443
+ return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
159444
+}
159445
+
159446
+/*
159447
+** Test for access permissions. Return true if the requested permission
159448
+** is available, or false otherwise.
159449
+*/
159450
+static int otaVfsAccess(
159451
+ sqlite3_vfs *pVfs,
159452
+ const char *zPath,
159453
+ int flags,
159454
+ int *pResOut
159455
+){
159456
+ ota_vfs *pOtaVfs = (ota_vfs*)pVfs;
159457
+ sqlite3_vfs *pRealVfs = pOtaVfs->pRealVfs;
159458
+ int rc;
159459
+
159460
+ rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
159461
+
159462
+ /* If this call is to check if a *-wal file associated with an OTA target
159463
+ ** database connection exists, and the OTA update is in OTA_STAGE_OAL,
159464
+ ** the following special handling is activated:
159465
+ **
159466
+ ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
159467
+ ** ensures that the OTA extension never tries to update a database
159468
+ ** in wal mode, even if the first page of the database file has
159469
+ ** been damaged.
159470
+ **
159471
+ ** b) if the *-wal file does not exist, claim that it does anyway,
159472
+ ** causing SQLite to call xOpen() to open it. This call will also
159473
+ ** be intercepted (see the otaVfsOpen() function) and the *-oal
159474
+ ** file opened instead.
159475
+ */
159476
+ if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
159477
+ ota_file *pDb = otaFindMaindb(pOtaVfs, zPath);
159478
+ if( pDb && pDb->pOta && pDb->pOta->eStage==OTA_STAGE_OAL ){
159479
+ if( *pResOut ){
159480
+ rc = SQLITE_CANTOPEN;
159481
+ }else{
159482
+ *pResOut = 1;
159483
+ }
159484
+ }
159485
+ }
159486
+
159487
+ return rc;
159488
+}
159489
+
159490
+/*
159491
+** Populate buffer zOut with the full canonical pathname corresponding
159492
+** to the pathname in zPath. zOut is guaranteed to point to a buffer
159493
+** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
159494
+*/
159495
+static int otaVfsFullPathname(
159496
+ sqlite3_vfs *pVfs,
159497
+ const char *zPath,
159498
+ int nOut,
159499
+ char *zOut
159500
+){
159501
+ sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159502
+ return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
159503
+}
159504
+
159505
+#ifndef SQLITE_OMIT_LOAD_EXTENSION
159506
+/*
159507
+** Open the dynamic library located at zPath and return a handle.
159508
+*/
159509
+static void *otaVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
159510
+ sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159511
+ return pRealVfs->xDlOpen(pRealVfs, zPath);
159512
+}
159513
+
159514
+/*
159515
+** Populate the buffer zErrMsg (size nByte bytes) with a human readable
159516
+** utf-8 string describing the most recent error encountered associated
159517
+** with dynamic libraries.
159518
+*/
159519
+static void otaVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
159520
+ sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159521
+ pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
159522
+}
159523
+
159524
+/*
159525
+** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
159526
+*/
159527
+static void (*otaVfsDlSym(
159528
+ sqlite3_vfs *pVfs,
159529
+ void *pArg,
159530
+ const char *zSym
159531
+))(void){
159532
+ sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159533
+ return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
159534
+}
159535
+
159536
+/*
159537
+** Close the dynamic library handle pHandle.
159538
+*/
159539
+static void otaVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
159540
+ sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159541
+ return pRealVfs->xDlClose(pRealVfs, pHandle);
159542
+}
159543
+#endif /* SQLITE_OMIT_LOAD_EXTENSION */
159544
+
159545
+/*
159546
+** Populate the buffer pointed to by zBufOut with nByte bytes of
159547
+** random data.
159548
+*/
159549
+static int otaVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
159550
+ sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159551
+ return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
159552
+}
159553
+
159554
+/*
159555
+** Sleep for nMicro microseconds. Return the number of microseconds
159556
+** actually slept.
159557
+*/
159558
+static int otaVfsSleep(sqlite3_vfs *pVfs, int nMicro){
159559
+ sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159560
+ return pRealVfs->xSleep(pRealVfs, nMicro);
159561
+}
159562
+
159563
+/*
159564
+** Return the current time as a Julian Day number in *pTimeOut.
159565
+*/
159566
+static int otaVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
159567
+ sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159568
+ return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
159569
+}
159570
+
159571
+/*
159572
+** No-op.
159573
+*/
159574
+static int otaVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
159575
+ return 0;
159576
+}
159577
+
159578
+/*
159579
+** Deregister and destroy an OTA vfs created by an earlier call to
159580
+** sqlite3ota_create_vfs().
159581
+*/
159582
+SQLITE_API void SQLITE_STDCALL sqlite3ota_destroy_vfs(const char *zName){
159583
+ sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
159584
+ if( pVfs && pVfs->xOpen==otaVfsOpen ){
159585
+ sqlite3_mutex_free(((ota_vfs*)pVfs)->mutex);
159586
+ sqlite3_vfs_unregister(pVfs);
159587
+ sqlite3_free(pVfs);
159588
+ }
159589
+}
159590
+
159591
+/*
159592
+** Create an OTA VFS named zName that accesses the underlying file-system
159593
+** via existing VFS zParent. The new object is registered as a non-default
159594
+** VFS with SQLite before returning.
159595
+*/
159596
+SQLITE_API int SQLITE_STDCALL sqlite3ota_create_vfs(const char *zName, const char *zParent){
159597
+
159598
+ /* Template for VFS */
159599
+ static sqlite3_vfs vfs_template = {
159600
+ 1, /* iVersion */
159601
+ 0, /* szOsFile */
159602
+ 0, /* mxPathname */
159603
+ 0, /* pNext */
159604
+ 0, /* zName */
159605
+ 0, /* pAppData */
159606
+ otaVfsOpen, /* xOpen */
159607
+ otaVfsDelete, /* xDelete */
159608
+ otaVfsAccess, /* xAccess */
159609
+ otaVfsFullPathname, /* xFullPathname */
159610
+
159611
+#ifndef SQLITE_OMIT_LOAD_EXTENSION
159612
+ otaVfsDlOpen, /* xDlOpen */
159613
+ otaVfsDlError, /* xDlError */
159614
+ otaVfsDlSym, /* xDlSym */
159615
+ otaVfsDlClose, /* xDlClose */
159616
+#else
159617
+ 0, 0, 0, 0,
159618
+#endif
159619
+
159620
+ otaVfsRandomness, /* xRandomness */
159621
+ otaVfsSleep, /* xSleep */
159622
+ otaVfsCurrentTime, /* xCurrentTime */
159623
+ otaVfsGetLastError, /* xGetLastError */
159624
+ 0, /* xCurrentTimeInt64 (version 2) */
159625
+ 0, 0, 0 /* Unimplemented version 3 methods */
159626
+ };
159627
+
159628
+ ota_vfs *pNew = 0; /* Newly allocated VFS */
159629
+ int nName;
159630
+ int rc = SQLITE_OK;
159631
+
159632
+ int nByte;
159633
+ nName = strlen(zName);
159634
+ nByte = sizeof(ota_vfs) + nName + 1;
159635
+ pNew = (ota_vfs*)sqlite3_malloc(nByte);
159636
+ if( pNew==0 ){
159637
+ rc = SQLITE_NOMEM;
159638
+ }else{
159639
+ sqlite3_vfs *pParent; /* Parent VFS */
159640
+ memset(pNew, 0, nByte);
159641
+ pParent = sqlite3_vfs_find(zParent);
159642
+ if( pParent==0 ){
159643
+ rc = SQLITE_NOTFOUND;
159644
+ }else{
159645
+ char *zSpace;
159646
+ memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
159647
+ pNew->base.mxPathname = pParent->mxPathname;
159648
+ pNew->base.szOsFile = sizeof(ota_file) + pParent->szOsFile;
159649
+ pNew->pRealVfs = pParent;
159650
+ pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
159651
+ memcpy(zSpace, zName, nName);
159652
+
159653
+ /* Allocate the mutex and register the new VFS (not as the default) */
159654
+ pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
159655
+ if( pNew->mutex==0 ){
159656
+ rc = SQLITE_NOMEM;
159657
+ }else{
159658
+ rc = sqlite3_vfs_register(&pNew->base, 0);
159659
+ }
159660
+ }
159661
+
159662
+ if( rc!=SQLITE_OK ){
159663
+ sqlite3_mutex_free(pNew->mutex);
159664
+ sqlite3_free(pNew);
159665
+ }
159666
+ }
159667
+
159668
+ return rc;
159669
+}
159670
+
159671
+
159672
+/**************************************************************************/
159673
+
159674
+#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_OTA) */
159675
+
159676
+/************** End of sqlite3ota.c ******************************************/
155216159677
/************** Begin file dbstat.c ******************************************/
155217159678
/*
155218159679
** 2010 July 12
155219159680
**
155220159681
** The author disclaims copyright to this source code. In place of
@@ -155834,11 +160295,11 @@
155834160295
}
155835160296
155836160297
/*
155837160298
** Invoke this routine to register the "dbstat" virtual table module
155838160299
*/
155839
-SQLITE_API int SQLITE_STDCALL sqlite3_dbstat_register(sqlite3 *db){
160300
+SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){
155840160301
static sqlite3_module dbstat_module = {
155841160302
0, /* iVersion */
155842160303
statConnect, /* xCreate */
155843160304
statConnect, /* xConnect */
155844160305
statBestIndex, /* xBestIndex */
@@ -155859,8 +160320,10 @@
155859160320
0, /* xFindMethod */
155860160321
0, /* xRename */
155861160322
};
155862160323
return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
155863160324
}
160325
+#elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
160326
+SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
155864160327
#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
155865160328
155866160329
/************** End of dbstat.c **********************************************/
155867160330
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.8.10.2. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -316,13 +316,13 @@
316 **
317 ** See also: [sqlite3_libversion()],
318 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
319 ** [sqlite_version()] and [sqlite_source_id()].
320 */
321 #define SQLITE_VERSION "3.8.10.2"
322 #define SQLITE_VERSION_NUMBER 3008010
323 #define SQLITE_SOURCE_ID "2015-05-20 18:17:19 2ef4f3a5b1d1d0c4338f8243d40a2452cc1f7fe4"
324
325 /*
326 ** CAPI3REF: Run-Time Library Version Numbers
327 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
328 **
@@ -1161,17 +1161,25 @@
1161 ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
1162 ** opcode causes the xFileControl method to swap the file handle with the one
1163 ** pointed to by the pArg argument. This capability is used during testing
1164 ** and only needs to be supported when SQLITE_TEST is defined.
1165 **
1166 ** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1167 ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
1168 ** be advantageous to block on the next WAL lock if the lock is not immediately
1169 ** available. The WAL subsystem issues this signal during rare
1170 ** circumstances in order to fix a problem with priority inversion.
1171 ** Applications should <em>not</em> use this file-control.
1172 **
 
 
 
 
 
 
 
 
1173 ** </ul>
1174 */
1175 #define SQLITE_FCNTL_LOCKSTATE 1
1176 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
1177 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1193,10 +1201,12 @@
1193 #define SQLITE_FCNTL_HAS_MOVED 20
1194 #define SQLITE_FCNTL_SYNC 21
1195 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
1196 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
1197 #define SQLITE_FCNTL_WAL_BLOCK 24
 
 
1198
1199 /* deprecated names */
1200 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1201 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1202 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -3595,11 +3605,13 @@
3595 **
3596 ** An sqlite3_value object may be either "protected" or "unprotected".
3597 ** Some interfaces require a protected sqlite3_value. Other interfaces
3598 ** will accept either a protected or an unprotected sqlite3_value.
3599 ** Every interface that accepts sqlite3_value arguments specifies
3600 ** whether or not it requires a protected sqlite3_value.
 
 
3601 **
3602 ** The terms "protected" and "unprotected" refer to whether or not
3603 ** a mutex is held. An internal mutex is held for a protected
3604 ** sqlite3_value object but no mutex is held for an unprotected
3605 ** sqlite3_value object. If SQLite is compiled to be single-threaded
@@ -4098,12 +4110,10 @@
4098 /*
4099 ** CAPI3REF: Result Values From A Query
4100 ** KEYWORDS: {column access functions}
4101 ** METHOD: sqlite3_stmt
4102 **
4103 ** These routines form the "result set" interface.
4104 **
4105 ** ^These routines return information about a single column of the current
4106 ** result row of a query. ^In every case the first argument is a pointer
4107 ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
4108 ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
4109 ** and the second argument is the index of the column for which information
@@ -4159,17 +4169,18 @@
4159 **
4160 ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
4161 ** even empty strings, are always zero-terminated. ^The return
4162 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
4163 **
4164 ** ^The object returned by [sqlite3_column_value()] is an
4165 ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object
4166 ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
 
4167 ** If the [unprotected sqlite3_value] object returned by
4168 ** [sqlite3_column_value()] is used in any other way, including calls
4169 ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
4170 ** or [sqlite3_value_bytes()], then the behavior is undefined.
4171 **
4172 ** These routines attempt to convert the value where appropriate. ^For
4173 ** example, if the internal representation is FLOAT and a text result
4174 ** is requested, [sqlite3_snprintf()] is used internally to perform the
4175 ** conversion automatically. ^(The following table details the conversions
@@ -4196,16 +4207,10 @@
4196 ** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
4197 ** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
4198 ** </table>
4199 ** </blockquote>)^
4200 **
4201 ** The table above makes reference to standard C library functions atoi()
4202 ** and atof(). SQLite does not really use these functions. It has its
4203 ** own equivalent internal routines. The atoi() and atof() names are
4204 ** used in the table for brevity and because they are familiar to most
4205 ** C programmers.
4206 **
4207 ** Note that when type conversions occur, pointers returned by prior
4208 ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
4209 ** sqlite3_column_text16() may be invalidated.
4210 ** Type conversions and pointer invalidations might occur
4211 ** in the following cases:
@@ -4226,11 +4231,11 @@
4226 ** not invalidate a prior pointer, though of course the content of the buffer
4227 ** that the prior pointer references will have been modified. Other kinds
4228 ** of conversion are done in place when it is possible, but sometimes they
4229 ** are not possible and in those cases prior pointers are invalidated.
4230 **
4231 ** The safest and easiest to remember policy is to invoke these routines
4232 ** in one of the following ways:
4233 **
4234 ** <ul>
4235 ** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
4236 ** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
@@ -4246,11 +4251,11 @@
4246 ** with calls to sqlite3_column_bytes().
4247 **
4248 ** ^The pointers returned are valid until a type conversion occurs as
4249 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
4250 ** [sqlite3_finalize()] is called. ^The memory space used to hold strings
4251 ** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned
4252 ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
4253 ** [sqlite3_free()].
4254 **
4255 ** ^(If a memory allocation error occurs during the evaluation of any
4256 ** of these routines, a default value is returned. The default value
@@ -4496,16 +4501,16 @@
4496 SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
4497 void*,sqlite3_int64);
4498 #endif
4499
4500 /*
4501 ** CAPI3REF: Obtaining SQL Function Parameter Values
4502 ** METHOD: sqlite3_value
4503 **
4504 ** The C-language implementation of SQL functions and aggregates uses
4505 ** this set of interface routines to access the parameter values on
4506 ** the function or aggregate.
4507 **
4508 ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
4509 ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
4510 ** define callbacks that implement the SQL functions and aggregates.
4511 ** The 3rd parameter to these callbacks is an array of pointers to
@@ -4554,10 +4559,27 @@
4554 SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value*);
4555 SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value*);
4556 SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value*);
4557 SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*);
4558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4559 /*
4560 ** CAPI3REF: Obtain Aggregate Function Context
4561 ** METHOD: sqlite3_context
4562 **
4563 ** Implementations of aggregate SQL functions use this
@@ -4801,11 +4823,11 @@
4801 ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
4802 ** then SQLite makes a copy of the result into space obtained from
4803 ** from [sqlite3_malloc()] before it returns.
4804 **
4805 ** ^The sqlite3_result_value() interface sets the result of
4806 ** the application-defined function to be a copy the
4807 ** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
4808 ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
4809 ** so that the [sqlite3_value] specified in the parameter may change or
4810 ** be deallocated after sqlite3_result_value() returns without harm.
4811 ** ^A [protected sqlite3_value] object may always be used where an
@@ -6077,11 +6099,11 @@
6077 ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
6078 ** always returns zero.
6079 **
6080 ** ^This function sets the database handle error code and message.
6081 */
6082 SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
6083
6084 /*
6085 ** CAPI3REF: Close A BLOB Handle
6086 ** DESTRUCTOR: sqlite3_blob
6087 **
@@ -7887,11 +7909,11 @@
7887 ** as if the loop did not exist - it returns non-zero and leave the variable
7888 ** that pOut points to unchanged.
7889 **
7890 ** See also: [sqlite3_stmt_scanstatus_reset()]
7891 */
7892 SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_stmt_scanstatus(
7893 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
7894 int idx, /* Index of loop to report on */
7895 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
7896 void *pOut /* Result written here */
7897 );
@@ -7903,11 +7925,11 @@
7903 ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
7904 **
7905 ** This API is only available if the library is built with pre-processor
7906 ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
7907 */
7908 SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
7909
7910
7911 /*
7912 ** Undo the hack that converts floating point types to integer for
7913 ** builds on processors without floating point support.
@@ -8018,10 +8040,12 @@
8018 sqlite3_int64 iRowid; /* Rowid for current entry */
8019 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
8020 int eParentWithin; /* Visibility of parent node */
8021 int eWithin; /* OUT: Visiblity */
8022 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
 
 
8023 };
8024
8025 /*
8026 ** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
8027 */
@@ -11167,10 +11191,11 @@
11167 #define SQLITE_EnableTrigger 0x00800000 /* True to enable triggers */
11168 #define SQLITE_DeferFKs 0x01000000 /* Defer all FK constraints */
11169 #define SQLITE_QueryOnly 0x02000000 /* Disable database changes */
11170 #define SQLITE_VdbeEQP 0x04000000 /* Debug EXPLAIN QUERY PLAN */
11171 #define SQLITE_Vacuum 0x08000000 /* Currently in a VACUUM */
 
11172
11173
11174 /*
11175 ** Bits of the sqlite3.dbOptFlags field that are used by the
11176 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
@@ -11548,12 +11573,13 @@
11548 #define TF_Readonly 0x01 /* Read-only system table */
11549 #define TF_Ephemeral 0x02 /* An ephemeral table */
11550 #define TF_HasPrimaryKey 0x04 /* Table has a primary key */
11551 #define TF_Autoincrement 0x08 /* Integer primary key is autoincrement */
11552 #define TF_Virtual 0x10 /* Is a virtual table */
11553 #define TF_WithoutRowid 0x20 /* No rowid used. PRIMARY KEY is the key */
11554 #define TF_OOOHidden 0x40 /* Out-of-Order hidden columns */
 
11555
11556
11557 /*
11558 ** Test to see whether or not a table is a virtual table. This is
11559 ** done as a macro so that it will be optimized out when virtual
@@ -11567,10 +11593,11 @@
11567 # define IsHiddenColumn(X) 0
11568 #endif
11569
11570 /* Does the table have a rowid */
11571 #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
 
11572
11573 /*
11574 ** Each foreign key constraint is an instance of the following structure.
11575 **
11576 ** A foreign key is associated with two tables. The "from" table is
@@ -11725,10 +11752,18 @@
11725 ** must be unique and what to do if they are not. When Index.onError=OE_None,
11726 ** it means this is not a unique index. Otherwise it is a unique index
11727 ** and the value of Index.onError indicate the which conflict resolution
11728 ** algorithm to employ whenever an attempt is made to insert a non-unique
11729 ** element.
 
 
 
 
 
 
 
 
11730 */
11731 struct Index {
11732 char *zName; /* Name of this index */
11733 i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
11734 LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
@@ -12299,23 +12334,24 @@
12299 /*
12300 ** Allowed values for Select.selFlags. The "SF" prefix stands for
12301 ** "Select Flag".
12302 */
12303 #define SF_Distinct 0x0001 /* Output should be DISTINCT */
12304 #define SF_Resolved 0x0002 /* Identifiers have been resolved */
12305 #define SF_Aggregate 0x0004 /* Contains aggregate functions */
12306 #define SF_UsesEphemeral 0x0008 /* Uses the OpenEphemeral opcode */
12307 #define SF_Expanded 0x0010 /* sqlite3SelectExpand() called on this */
12308 #define SF_HasTypeInfo 0x0020 /* FROM subqueries have Table metadata */
12309 #define SF_Compound 0x0040 /* Part of a compound query */
12310 #define SF_Values 0x0080 /* Synthesized from VALUES clause */
12311 #define SF_MultiValue 0x0100 /* Single VALUES term with multiple rows */
12312 #define SF_NestedFrom 0x0200 /* Part of a parenthesized FROM clause */
12313 #define SF_MaybeConvert 0x0400 /* Need convertCompoundSelectToSubquery() */
12314 #define SF_Recursive 0x0800 /* The recursive part of a recursive CTE */
12315 #define SF_MinMaxAgg 0x1000 /* Aggregate containing min() or max() */
12316 #define SF_Converted 0x2000 /* By convertCompoundSelectToSubquery() */
 
12317
12318
12319 /*
12320 ** The results of a SELECT can be distributed in several ways, as defined
12321 ** by one of the following macros. The "SRT" prefix means "SELECT Result
@@ -12553,11 +12589,10 @@
12553
12554 /* Information used while coding trigger programs. */
12555 Parse *pToplevel; /* Parse structure for main program (or NULL) */
12556 Table *pTriggerTab; /* Table triggers are being coded for */
12557 int addrCrTab; /* Address of OP_CreateTable opcode on CREATE TABLE */
12558 int addrSkipPK; /* Address of instruction to skip PRIMARY KEY index */
12559 u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
12560 u32 oldmask; /* Mask of old.* columns referenced */
12561 u32 newmask; /* Mask of new.* columns referenced */
12562 u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
12563 u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
@@ -13065,11 +13100,10 @@
13065 #define SQLITE_PRINTF_SQLFUNC 0x02
13066 SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
13067 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
13068 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
13069 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
13070 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
13071 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
13072 SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
13073 #endif
13074 #if defined(SQLITE_TEST)
13075 SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
@@ -13084,11 +13118,11 @@
13084 SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
13085 SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
13086 #endif
13087
13088
13089 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
13090 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
13091 SQLITE_PRIVATE int sqlite3Dequote(char*);
13092 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
13093 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
13094 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
@@ -13780,10 +13814,14 @@
13780 */
13781 #if SQLITE_MAX_WORKER_THREADS>0
13782 SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
13783 SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
13784 #endif
 
 
 
 
13785
13786 #endif /* _SQLITEINT_H_ */
13787
13788 /************** End of sqliteInt.h *******************************************/
13789 /************** Begin file global.c ******************************************/
@@ -14685,10 +14723,16 @@
14685 Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */
14686 void *pFiller; /* So that sizeof(Mem) is a multiple of 8 */
14687 #endif
14688 };
14689
 
 
 
 
 
 
14690 /* One or more of the following flags are set to indicate the validOK
14691 ** representations of the value stored in the Mem struct.
14692 **
14693 ** If the MEM_Null flag is set, then the value is an SQL NULL value.
14694 ** No other flags may be set in this case.
@@ -14890,10 +14934,11 @@
14890 #define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */
14891
14892 /*
14893 ** Function prototypes
14894 */
 
14895 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
14896 void sqliteVdbePopStack(Vdbe*,int);
14897 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
14898 SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
14899 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
@@ -20738,28 +20783,24 @@
20738
20739 /*
20740 ** Return the amount of memory currently checked out.
20741 */
20742 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void){
20743 int n, mx;
20744 sqlite3_int64 res;
20745 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
20746 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
20747 return res;
20748 }
20749
20750 /*
20751 ** Return the maximum amount of memory that has ever been
20752 ** checked out since either the beginning of this process
20753 ** or since the most recent reset.
20754 */
20755 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag){
20756 int n, mx;
20757 sqlite3_int64 res;
20758 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
20759 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
20760 return res;
20761 }
20762
20763 /*
20764 ** Trigger the alarm
20765 */
@@ -21287,23 +21328,15 @@
21287 }
21288 return zNew;
21289 }
21290
21291 /*
21292 ** Create a string from the zFromat argument and the va_list that follows.
21293 ** Store the string in memory obtained from sqliteMalloc() and make *pz
21294 ** point to that string.
21295 */
21296 SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
21297 va_list ap;
21298 char *z;
21299
21300 va_start(ap, zFormat);
21301 z = sqlite3VMPrintf(db, zFormat, ap);
21302 va_end(ap);
21303 sqlite3DbFree(db, *pz);
21304 *pz = z;
21305 }
21306
21307 /*
21308 ** Take actions at the end of an API call to indicate an OOM error
21309 */
@@ -22271,28 +22304,10 @@
22271 z = sqlite3VMPrintf(db, zFormat, ap);
22272 va_end(ap);
22273 return z;
22274 }
22275
22276 /*
22277 ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
22278 ** the string and before returning. This routine is intended to be used
22279 ** to modify an existing string. For example:
22280 **
22281 ** x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
22282 **
22283 */
22284 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
22285 va_list ap;
22286 char *z;
22287 va_start(ap, zFormat);
22288 z = sqlite3VMPrintf(db, zFormat, ap);
22289 va_end(ap);
22290 sqlite3DbFree(db, zStr);
22291 return z;
22292 }
22293
22294 /*
22295 ** Print into memory obtained from sqlite3_malloc(). Omit the internal
22296 ** %-conversion extensions.
22297 */
22298 SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char *zFormat, va_list ap){
@@ -36220,10 +36235,16 @@
36220 */
36221 if( pFile->locktype>=locktype ){
36222 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
36223 return SQLITE_OK;
36224 }
 
 
 
 
 
 
36225
36226 /* Make sure the locking sequence is correct
36227 */
36228 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
36229 assert( locktype!=PENDING_LOCK );
@@ -38617,18 +38638,18 @@
38617 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
38618 if( sizeof(UUID)<=nBuf-n ){
38619 UUID id;
38620 memset(&id, 0, sizeof(UUID));
38621 osUuidCreate(&id);
38622 memcpy(zBuf, &id, sizeof(UUID));
38623 n += sizeof(UUID);
38624 }
38625 if( sizeof(UUID)<=nBuf-n ){
38626 UUID id;
38627 memset(&id, 0, sizeof(UUID));
38628 osUuidCreateSequential(&id);
38629 memcpy(zBuf, &id, sizeof(UUID));
38630 n += sizeof(UUID);
38631 }
38632 #endif
38633 #endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */
38634 return n;
@@ -44777,15 +44798,14 @@
44777 */
44778 assert( pPager->eState==PAGER_OPEN );
44779 assert( pPager->eLock>=SHARED_LOCK );
44780 nPage = sqlite3WalDbsize(pPager->pWal);
44781
44782 /* If the database size was not available from the WAL sub-system,
44783 ** determine it based on the size of the database file. If the size
44784 ** of the database file is not an integer multiple of the page-size,
44785 ** round down to the nearest page. Except, any file larger than 0
44786 ** bytes in size is considered to contain at least one page.
44787 */
44788 if( nPage==0 ){
44789 i64 n = 0; /* Size of db file in bytes */
44790 assert( isOpen(pPager->fd) || pPager->tempFile );
44791 if( isOpen(pPager->fd) ){
@@ -54245,30 +54265,22 @@
54245 u8 *pAddr; /* The i-th cell pointer */
54246 pAddr = &data[cellOffset + i*2];
54247 pc = get2byte(pAddr);
54248 testcase( pc==iCellFirst );
54249 testcase( pc==iCellLast );
54250 #if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
54251 /* These conditions have already been verified in btreeInitPage()
54252 ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined
54253 */
54254 if( pc<iCellFirst || pc>iCellLast ){
54255 return SQLITE_CORRUPT_BKPT;
54256 }
54257 #endif
54258 assert( pc>=iCellFirst && pc<=iCellLast );
54259 size = cellSizePtr(pPage, &src[pc]);
54260 cbrk -= size;
54261 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
54262 if( cbrk<iCellFirst ){
54263 return SQLITE_CORRUPT_BKPT;
54264 }
54265 #else
54266 if( cbrk<iCellFirst || pc+size>usableSize ){
54267 return SQLITE_CORRUPT_BKPT;
54268 }
54269 #endif
54270 assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
54271 testcase( cbrk+size==usableSize );
54272 testcase( pc+size==usableSize );
54273 put2byte(pAddr, cbrk);
54274 if( temp==0 ){
@@ -54340,11 +54352,11 @@
54340 }
54341 /* Remove the slot from the free-list. Update the number of
54342 ** fragmented bytes within the page. */
54343 memcpy(&aData[iAddr], &aData[pc], 2);
54344 aData[hdr+7] += (u8)x;
54345 }else if( size+pc > usableSize ){
54346 *pRc = SQLITE_CORRUPT_BKPT;
54347 return 0;
54348 }else{
54349 /* The slot remains on the free-list. Reduce its size to account
54350 ** for the portion used by the new allocation. */
@@ -54392,11 +54404,15 @@
54392 ** and the reserved space is zero (the usual value for reserved space)
54393 ** then the cell content offset of an empty page wants to be 65536.
54394 ** However, that integer is too large to be stored in a 2-byte unsigned
54395 ** integer, so a value of 0 is used in its place. */
54396 top = get2byteNotZero(&data[hdr+5]);
54397 if( gap>top ) return SQLITE_CORRUPT_BKPT;
 
 
 
 
54398
54399 /* If there is enough space between gap and top for one more cell pointer
54400 ** array entry offset, and if the freelist is not empty, then search the
54401 ** freelist looking for a free slot big enough to satisfy the request.
54402 */
@@ -54465,11 +54481,11 @@
54465 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
54466 unsigned char *data = pPage->aData; /* Page content */
54467
54468 assert( pPage->pBt!=0 );
54469 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
54470 assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
54471 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
54472 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54473 assert( iSize>=4 ); /* Minimum cell size is 4 */
54474 assert( iStart<=iLast );
54475
@@ -54605,10 +54621,11 @@
54605 ** we failed to detect any corruption.
54606 */
54607 static int btreeInitPage(MemPage *pPage){
54608
54609 assert( pPage->pBt!=0 );
 
54610 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54611 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
54612 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
54613 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
54614
@@ -54663,12 +54680,11 @@
54663 ** past the end of a page boundary and causes SQLITE_CORRUPT to be
54664 ** returned if it does.
54665 */
54666 iCellFirst = cellOffset + 2*pPage->nCell;
54667 iCellLast = usableSize - 4;
54668 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
54669 {
54670 int i; /* Index into the cell pointer array */
54671 int sz; /* Size of a cell */
54672
54673 if( !pPage->leaf ) iCellLast--;
54674 for(i=0; i<pPage->nCell; i++){
@@ -54684,11 +54700,10 @@
54684 return SQLITE_CORRUPT_BKPT;
54685 }
54686 }
54687 if( !pPage->leaf ) iCellLast++;
54688 }
54689 #endif
54690
54691 /* Compute the total free space on the page
54692 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
54693 ** start of the first freeblock on the page, or is zero if there are no
54694 ** freeblocks. */
@@ -54781,14 +54796,14 @@
54781 return pPage;
54782 }
54783
54784 /*
54785 ** Get a page from the pager. Initialize the MemPage.pBt and
54786 ** MemPage.aData elements if needed.
54787 **
54788 ** If the noContent flag is set, it means that we do not care about
54789 ** the content of the page at this time. So do not go to the disk
54790 ** to fetch the content. Just fill in the content with zeros for now.
54791 ** If in the future we call sqlite3PagerWrite() on this page, that
54792 ** means we have started to be concerned about content and the disk
54793 ** read should occur at that point.
54794 */
@@ -54885,10 +54900,40 @@
54885 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
54886 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54887 sqlite3PagerUnrefNotNull(pPage->pDbPage);
54888 }
54889 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54890
54891 /*
54892 ** During a rollback, when the pager reloads information into the cache
54893 ** so that the cache is restored to its original state at the start of
54894 ** the transaction, for each page restored this routine is called.
@@ -56133,12 +56178,14 @@
56133 put4byte(pPage->aData, iTo);
56134 }else{
56135 u8 isInitOrig = pPage->isInit;
56136 int i;
56137 int nCell;
 
56138
56139 btreeInitPage(pPage);
 
56140 nCell = pPage->nCell;
56141
56142 for(i=0; i<nCell; i++){
56143 u8 *pCell = findCell(pPage, i);
56144 if( eType==PTRMAP_OVERFLOW1 ){
@@ -56935,13 +56982,17 @@
56935 int wrFlag, /* 1 to write. 0 read-only */
56936 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
56937 BtCursor *pCur /* Write new cursor here */
56938 ){
56939 int rc;
56940 sqlite3BtreeEnter(p);
56941 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
56942 sqlite3BtreeLeave(p);
 
 
 
 
56943 return rc;
56944 }
56945
56946 /*
56947 ** Return the size of a BtCursor object in bytes.
@@ -57965,11 +58016,11 @@
57965 assert( lwr+upr>=0 );
57966 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
57967 }
57968 }else{
57969 for(;;){
57970 int nCell;
57971 pCell = findCell(pPage, idx) + pPage->childPtrSize;
57972
57973 /* The maximum supported page-size is 65536 bytes. This means that
57974 ** the maximum number of record bytes stored on an index B-Tree
57975 ** page is less than 16384 bytes and may be stored as a 2-byte
@@ -57994,16 +58045,29 @@
57994 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
57995 }else{
57996 /* The record flows over onto one or more overflow pages. In
57997 ** this case the whole cell needs to be parsed, a buffer allocated
57998 ** and accessPayload() used to retrieve the record into the
57999 ** buffer before VdbeRecordCompare() can be called. */
 
 
 
 
 
58000 void *pCellKey;
58001 u8 * const pCellBody = pCell - pPage->childPtrSize;
58002 btreeParseCellPtr(pPage, pCellBody, &pCur->info);
58003 nCell = (int)pCur->info.nKey;
58004 pCellKey = sqlite3Malloc( nCell );
 
 
 
 
 
 
 
 
58005 if( pCellKey==0 ){
58006 rc = SQLITE_NOMEM;
58007 goto moveto_finish;
58008 }
58009 pCur->aiIdx[pCur->iPage] = (u16)idx;
@@ -58387,11 +58451,11 @@
58387 }
58388 testcase( iTrunk==mxPage );
58389 if( iTrunk>mxPage ){
58390 rc = SQLITE_CORRUPT_BKPT;
58391 }else{
58392 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
58393 }
58394 if( rc ){
58395 pTrunk = 0;
58396 goto end_allocate_page;
58397 }
@@ -58452,11 +58516,11 @@
58452 if( iNewTrunk>mxPage ){
58453 rc = SQLITE_CORRUPT_BKPT;
58454 goto end_allocate_page;
58455 }
58456 testcase( iNewTrunk==mxPage );
58457 rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
58458 if( rc!=SQLITE_OK ){
58459 goto end_allocate_page;
58460 }
58461 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
58462 if( rc!=SQLITE_OK ){
@@ -58532,11 +58596,11 @@
58532 if( closest<k-1 ){
58533 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
58534 }
58535 put4byte(&aData[4], k-1);
58536 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
58537 rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
58538 if( rc==SQLITE_OK ){
58539 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
58540 if( rc!=SQLITE_OK ){
58541 releasePage(*ppPage);
58542 }
@@ -58580,11 +58644,11 @@
58580 ** becomes a new pointer-map page, the second is used by the caller.
58581 */
58582 MemPage *pPg = 0;
58583 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
58584 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
58585 rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent);
58586 if( rc==SQLITE_OK ){
58587 rc = sqlite3PagerWrite(pPg->pDbPage);
58588 releasePage(pPg);
58589 }
58590 if( rc ) return rc;
@@ -58594,35 +58658,27 @@
58594 #endif
58595 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
58596 *pPgno = pBt->nPage;
58597
58598 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
58599 rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent);
58600 if( rc ) return rc;
58601 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
58602 if( rc!=SQLITE_OK ){
58603 releasePage(*ppPage);
 
58604 }
58605 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
58606 }
58607
58608 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
58609
58610 end_allocate_page:
58611 releasePage(pTrunk);
58612 releasePage(pPrevTrunk);
58613 if( rc==SQLITE_OK ){
58614 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
58615 releasePage(*ppPage);
58616 *ppPage = 0;
58617 return SQLITE_CORRUPT_BKPT;
58618 }
58619 (*ppPage)->isInit = 0;
58620 }else{
58621 *ppPage = 0;
58622 }
58623 assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
58624 return rc;
58625 }
58626
58627 /*
58628 ** This function is used to add page iPage to the database file free-list.
@@ -58643,13 +58699,14 @@
58643 MemPage *pPage; /* Page being freed. May be NULL. */
58644 int rc; /* Return Code */
58645 int nFree; /* Initial number of pages on free-list */
58646
58647 assert( sqlite3_mutex_held(pBt->mutex) );
58648 assert( iPage>1 );
58649 assert( !pMemPage || pMemPage->pgno==iPage );
58650
 
58651 if( pMemPage ){
58652 pPage = pMemPage;
58653 sqlite3PagerRef(pPage->pDbPage);
58654 }else{
58655 pPage = btreePageLookup(pBt, iPage);
@@ -58797,11 +58854,13 @@
58797 }
58798 ovflPgno = get4byte(&pCell[info.iOverflow]);
58799 assert( pBt->usableSize > 4 );
58800 ovflPageSize = pBt->usableSize - 4;
58801 nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
58802 assert( ovflPgno==0 || nOvfl>0 );
 
 
58803 while( nOvfl-- ){
58804 Pgno iNext = 0;
58805 MemPage *pOvfl = 0;
58806 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
58807 /* 0 is not a legal page number and page 1 cannot be an
@@ -59052,11 +59111,11 @@
59052 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
59053
59054 if( *pRC ) return;
59055
59056 assert( idx>=0 && idx<pPage->nCell );
59057 assert( sz==cellSize(pPage, idx) );
59058 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
59059 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
59060 data = pPage->aData;
59061 ptr = &pPage->aCellIdx[2*idx];
59062 pc = get2byte(ptr);
@@ -59216,11 +59275,12 @@
59216 }
59217 pData -= szCell[i];
59218 memcpy(pData, pCell, szCell[i]);
59219 put2byte(pCellptr, (pData - aData));
59220 pCellptr += 2;
59221 assert( szCell[i]==cellSizePtr(pPg, pCell) );
 
59222 }
59223
59224 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
59225 pPg->nCell = nCell;
59226 pPg->nOverflow = 0;
@@ -59893,10 +59953,18 @@
59893 leafCorrection = apOld[0]->leaf*4;
59894 leafData = apOld[0]->intKeyLeaf;
59895 for(i=0; i<nOld; i++){
59896 int limit;
59897 MemPage *pOld = apOld[i];
 
 
 
 
 
 
 
 
59898
59899 limit = pOld->nCell+pOld->nOverflow;
59900 if( pOld->nOverflow>0 ){
59901 for(j=0; j<limit; j++){
59902 assert( nCell<nMaxCells );
@@ -59935,17 +60003,17 @@
59935 /* The right pointer of the child page pOld becomes the left
59936 ** pointer of the divider cell */
59937 memcpy(apCell[nCell], &pOld->aData[8], 4);
59938 }else{
59939 assert( leafCorrection==4 );
59940 if( szCell[nCell]<4 ){
59941 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
59942 ** does exist, pad it with 0x00 bytes. */
59943 assert( szCell[nCell]==3 );
59944 assert( apCell[nCell]==&aSpace1[iSpace1-3] );
59945 aSpace1[iSpace1++] = 0x00;
59946 szCell[nCell] = 4;
59947 }
59948 }
59949 nCell++;
59950 }
59951 }
@@ -60032,14 +60100,10 @@
60032 ));
60033
60034 /*
60035 ** Allocate k new pages. Reuse old pages where possible.
60036 */
60037 if( apOld[0]->pgno<=1 ){
60038 rc = SQLITE_CORRUPT_BKPT;
60039 goto balance_cleanup;
60040 }
60041 pageFlags = apOld[0]->aData[0];
60042 for(i=0; i<k; i++){
60043 MemPage *pNew;
60044 if( i<nOld ){
60045 pNew = apNew[i] = apOld[i];
@@ -60817,10 +60881,11 @@
60817 int nCell;
60818 Pgno n = pCur->apPage[iCellDepth+1]->pgno;
60819 unsigned char *pTmp;
60820
60821 pCell = findCell(pLeaf, pLeaf->nCell-1);
 
60822 nCell = cellSizePtr(pLeaf, pCell);
60823 assert( MX_CELL_SIZE(pBt) >= nCell );
60824 pTmp = pBt->pTmpSpace;
60825 assert( pTmp!=0 );
60826 rc = sqlite3PagerWrite(pLeaf->pDbPage);
@@ -60909,11 +60974,12 @@
60909 */
60910 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
60911 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
60912 pgnoRoot++;
60913 }
60914 assert( pgnoRoot>=3 );
 
60915
60916 /* Allocate a page. The page that currently resides at pgnoRoot will
60917 ** be moved to the allocated page (unless the allocated page happens
60918 ** to reside at pgnoRoot).
60919 */
@@ -61059,11 +61125,12 @@
61059 }
61060 if( !pPage->leaf ){
61061 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
61062 if( rc ) goto cleardatabasepage_out;
61063 }else if( pnChange ){
61064 assert( pPage->intKey );
 
61065 *pnChange += pPage->nCell;
61066 }
61067 if( freePageFlag ){
61068 freePage(pPage, &rc);
61069 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
@@ -63854,14 +63921,10 @@
63854 }
63855 pMem->pScopyFrom = 0;
63856 }
63857 #endif /* SQLITE_DEBUG */
63858
63859 /*
63860 ** Size of struct Mem not including the Mem.zMalloc member.
63861 */
63862 #define MEMCELLSIZE offsetof(Mem,zMalloc)
63863
63864 /*
63865 ** Make an shallow copy of pFrom into pTo. Prior contents of
63866 ** pTo are freed. The pFrom->z field is not duplicated. If
63867 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
@@ -63884,11 +63947,14 @@
63884 ** freed before the copy is made.
63885 */
63886 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
63887 int rc = SQLITE_OK;
63888
63889 assert( pTo->db==pFrom->db );
 
 
 
63890 assert( (pFrom->flags & MEM_RowSet)==0 );
63891 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
63892 memcpy(pTo, pFrom, MEMCELLSIZE);
63893 pTo->flags &= ~MEM_Dyn;
63894 if( pTo->flags&(MEM_Str|MEM_Blob) ){
@@ -64817,10 +64883,21 @@
64817 assert( pParse->aLabel==0 );
64818 assert( pParse->nLabel==0 );
64819 assert( pParse->nOpAlloc==0 );
64820 return p;
64821 }
 
 
 
 
 
 
 
 
 
 
 
64822
64823 /*
64824 ** Remember the SQL string for a prepared statement.
64825 */
64826 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
@@ -66174,11 +66251,11 @@
66174 p->rc = SQLITE_OK;
66175 rc = SQLITE_DONE;
66176 }else if( db->u1.isInterrupted ){
66177 p->rc = SQLITE_INTERRUPT;
66178 rc = SQLITE_ERROR;
66179 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
66180 }else{
66181 char *zP4;
66182 Op *pOp;
66183 if( i<p->nOp ){
66184 /* The output line number is small enough that we are still in the
@@ -67077,11 +67154,11 @@
67077 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
67078 || (!deferred && p->nFkConstraint>0)
67079 ){
67080 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
67081 p->errorAction = OE_Abort;
67082 sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed");
67083 return SQLITE_ERROR;
67084 }
67085 return SQLITE_OK;
67086 }
67087 #endif
@@ -68420,11 +68497,11 @@
68420
68421 /* RHS is an integer */
68422 if( pRhs->flags & MEM_Int ){
68423 serial_type = aKey1[idx1];
68424 testcase( serial_type==12 );
68425 if( serial_type>=12 ){
68426 rc = +1;
68427 }else if( serial_type==0 ){
68428 rc = -1;
68429 }else if( serial_type==7 ){
68430 double rhs = (double)pRhs->u.i;
@@ -68446,11 +68523,15 @@
68446 }
68447
68448 /* RHS is real */
68449 else if( pRhs->flags & MEM_Real ){
68450 serial_type = aKey1[idx1];
68451 if( serial_type>=12 ){
 
 
 
 
68452 rc = +1;
68453 }else if( serial_type==0 ){
68454 rc = -1;
68455 }else{
68456 double rhs = pRhs->u.r;
@@ -69184,10 +69265,40 @@
69184 SQLITE_INTEGER, /* 0x1e */
69185 SQLITE_NULL, /* 0x1f */
69186 };
69187 return aType[pVal->flags&MEM_AffMask];
69188 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69189
69190 /**************************** sqlite3_result_ *******************************
69191 ** The following routines are used by user-defined functions to specify
69192 ** the function result.
69193 **
@@ -71798,16 +71909,15 @@
71798 zType = 0;
71799 }
71800 assert( zType!=0 || pOp->p4.z!=0 );
71801 zLogFmt = "abort at %d in [%s]: %s";
71802 if( zType && pOp->p4.z ){
71803 sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s",
71804 zType, pOp->p4.z);
71805 }else if( pOp->p4.z ){
71806 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
71807 }else{
71808 sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType);
71809 }
71810 sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg);
71811 }
71812 rc = sqlite3VdbeHalt(p);
71813 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
@@ -72435,11 +72545,11 @@
72435 lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */
72436
72437 /* If the function returned an error, throw an exception */
72438 if( ctx.fErrorOrAux ){
72439 if( ctx.isError ){
72440 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut));
72441 rc = ctx.isError;
72442 }
72443 sqlite3VdbeDeleteAuxData(p, (int)(pOp - aOp), pOp->p1);
72444 }
72445
@@ -73622,12 +73732,11 @@
73622 if( p1==SAVEPOINT_BEGIN ){
73623 if( db->nVdbeWrite>0 ){
73624 /* A new savepoint cannot be created if there are active write
73625 ** statements (i.e. open read/write incremental blob handles).
73626 */
73627 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
73628 "SQL statements in progress");
73629 rc = SQLITE_BUSY;
73630 }else{
73631 nName = sqlite3Strlen30(zName);
73632
73633 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -73674,19 +73783,18 @@
73674 pSavepoint = pSavepoint->pNext
73675 ){
73676 iSavepoint++;
73677 }
73678 if( !pSavepoint ){
73679 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
73680 rc = SQLITE_ERROR;
73681 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
73682 /* It is not possible to release (commit) a savepoint if there are
73683 ** active write statements.
73684 */
73685 sqlite3SetString(&p->zErrMsg, db,
73686 "cannot release savepoint - SQL statements in progress"
73687 );
73688 rc = SQLITE_BUSY;
73689 }else{
73690
73691 /* Determine whether or not this is a transaction savepoint. If so,
73692 ** and this is a RELEASE command, then the current transaction
@@ -73788,27 +73896,16 @@
73788 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
73789 assert( desiredAutoCommit==1 || iRollback==0 );
73790 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
73791 assert( p->bIsReader );
73792
73793 #if 0
73794 if( turnOnAC && iRollback && db->nVdbeActive>1 ){
73795 /* If this instruction implements a ROLLBACK and other VMs are
73796 ** still running, and a transaction is active, return an error indicating
73797 ** that the other VMs must complete first.
73798 */
73799 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
73800 "SQL statements in progress");
73801 rc = SQLITE_BUSY;
73802 }else
73803 #endif
73804 if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
73805 /* If this instruction implements a COMMIT and other VMs are writing
73806 ** return an error indicating that the other VMs must complete first.
73807 */
73808 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
73809 "SQL statements in progress");
73810 rc = SQLITE_BUSY;
73811 }else if( desiredAutoCommit!=db->autoCommit ){
73812 if( iRollback ){
73813 assert( desiredAutoCommit==1 );
73814 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
@@ -73831,11 +73928,11 @@
73831 }else{
73832 rc = SQLITE_ERROR;
73833 }
73834 goto vdbe_return;
73835 }else{
73836 sqlite3SetString(&p->zErrMsg, db,
73837 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
73838 (iRollback)?"cannot rollback - no transaction is active":
73839 "cannot commit - no transaction is active"));
73840
73841 rc = SQLITE_ERROR;
@@ -76264,11 +76361,11 @@
76264 if( pFrame ) break;
76265 }
76266
76267 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
76268 rc = SQLITE_ERROR;
76269 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
76270 break;
76271 }
76272
76273 /* Register pRt is used to store the memory required to save the state
76274 ** of the current program, and the memory required at runtime to execute
@@ -76567,11 +76664,11 @@
76567 ctx.pVdbe = p;
76568 ctx.iOp = (int)(pOp - aOp);
76569 ctx.skipFlag = 0;
76570 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
76571 if( ctx.isError ){
76572 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t));
76573 rc = ctx.isError;
76574 }
76575 if( ctx.skipFlag ){
76576 assert( pOp[-1].opcode==OP_CollSeq );
76577 i = pOp[-1].p1;
@@ -76599,11 +76696,11 @@
76599 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
76600 pMem = &aMem[pOp->p1];
76601 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
76602 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
76603 if( rc ){
76604 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
76605 }
76606 sqlite3VdbeChangeEncoding(pMem, encoding);
76607 UPDATE_MAX_BLOBSIZE(pMem);
76608 if( sqlite3VdbeMemTooBig(pMem) ){
76609 goto too_big;
@@ -76704,11 +76801,11 @@
76704 if( (eNew!=eOld)
76705 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
76706 ){
76707 if( !db->autoCommit || db->nVdbeRead>1 ){
76708 rc = SQLITE_ERROR;
76709 sqlite3SetString(&p->zErrMsg, db,
76710 "cannot change %s wal mode from within a transaction",
76711 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
76712 );
76713 break;
76714 }else{
@@ -76835,11 +76932,11 @@
76835 assert( DbMaskTest(p->btreeMask, p1) );
76836 assert( isWriteLock==0 || isWriteLock==1 );
76837 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
76838 if( (rc&0xFF)==SQLITE_LOCKED ){
76839 const char *z = pOp->p4.z;
76840 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
76841 }
76842 }
76843 break;
76844 }
76845 #endif /* SQLITE_OMIT_SHARED_CACHE */
@@ -77383,19 +77480,19 @@
77383
77384 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
77385 ** is encountered.
77386 */
77387 too_big:
77388 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
77389 rc = SQLITE_TOOBIG;
77390 goto vdbe_error_halt;
77391
77392 /* Jump to here if a malloc() fails.
77393 */
77394 no_mem:
77395 db->mallocFailed = 1;
77396 sqlite3SetString(&p->zErrMsg, db, "out of memory");
77397 rc = SQLITE_NOMEM;
77398 goto vdbe_error_halt;
77399
77400 /* Jump to here for any other kind of fatal error. The "rc" variable
77401 ** should hold the error number.
@@ -77402,11 +77499,11 @@
77402 */
77403 abort_due_to_error:
77404 assert( p->zErrMsg==0 );
77405 if( db->mallocFailed ) rc = SQLITE_NOMEM;
77406 if( rc!=SQLITE_IOERR_NOMEM ){
77407 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
77408 }
77409 goto vdbe_error_halt;
77410
77411 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
77412 ** flag.
@@ -77413,11 +77510,11 @@
77413 */
77414 abort_due_to_interrupt:
77415 assert( db->u1.isInterrupted );
77416 rc = SQLITE_INTERRUPT;
77417 p->rc = rc;
77418 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
77419 goto vdbe_error_halt;
77420 }
77421
77422
77423 /************** End of vdbe.c ************************************************/
@@ -81652,11 +81749,11 @@
81652 iCol = -1;
81653 }
81654 break;
81655 }
81656 }
81657 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
81658 /* IMP: R-51414-32910 */
81659 /* IMP: R-44911-55124 */
81660 iCol = -1;
81661 }
81662 if( iCol<pTab->nCol ){
@@ -81682,11 +81779,11 @@
81682
81683 /*
81684 ** Perhaps the name is a reference to the ROWID
81685 */
81686 if( cnt==0 && cntTab==1 && pMatch && sqlite3IsRowid(zCol)
81687 && HasRowid(pMatch->pTab) ){
81688 cnt = 1;
81689 pExpr->iColumn = -1; /* IMP: R-44911-55124 */
81690 pExpr->affinity = SQLITE_AFF_INTEGER;
81691 }
81692
@@ -92126,18 +92223,15 @@
92126 #ifndef SQLITE_OMIT_AUTOINCREMENT
92127 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
92128 "INTEGER PRIMARY KEY");
92129 #endif
92130 }else{
92131 Vdbe *v = pParse->pVdbe;
92132 Index *p;
92133 if( v ) pParse->addrSkipPK = sqlite3VdbeAddOp0(v, OP_Noop);
92134 p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
92135 0, sortOrder, 0);
92136 if( p ){
92137 p->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
92138 if( v ) sqlite3VdbeJumpHere(v, pParse->addrSkipPK);
92139 }
92140 pList = 0;
92141 }
92142
92143 primary_key_exit:
@@ -92486,18 +92580,10 @@
92486 if( pParse->addrCrTab ){
92487 assert( v );
92488 sqlite3VdbeGetOp(v, pParse->addrCrTab)->opcode = OP_CreateIndex;
92489 }
92490
92491 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
92492 ** table entry.
92493 */
92494 if( pParse->addrSkipPK ){
92495 assert( v );
92496 sqlite3VdbeGetOp(v, pParse->addrSkipPK)->opcode = OP_Goto;
92497 }
92498
92499 /* Locate the PRIMARY KEY index. Or, if this table was originally
92500 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
92501 */
92502 if( pTab->iPKey>=0 ){
92503 ExprList *pList;
@@ -92511,10 +92597,20 @@
92511 if( pPk==0 ) return;
92512 pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
92513 pTab->iPKey = -1;
92514 }else{
92515 pPk = sqlite3PrimaryKeyIndex(pTab);
 
 
 
 
 
 
 
 
 
 
92516 /*
92517 ** Remove all redundant columns from the PRIMARY KEY. For example, change
92518 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
92519 ** code assumes the PRIMARY KEY contains no repeated columns.
92520 */
@@ -92646,11 +92742,11 @@
92646 return;
92647 }
92648 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
92649 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
92650 }else{
92651 p->tabFlags |= TF_WithoutRowid;
92652 convertToWithoutRowidTable(pParse, p);
92653 }
92654 }
92655
92656 iDb = sqlite3SchemaToIndex(db, p->pSchema);
@@ -92714,30 +92810,49 @@
92714 ** as a schema-lock must have already been obtained to create it. Since
92715 ** a schema-lock excludes all other database users, the write-lock would
92716 ** be redundant.
92717 */
92718 if( pSelect ){
92719 SelectDest dest;
92720 Table *pSelTab;
 
 
 
 
 
92721
 
 
 
92722 assert(pParse->nTab==1);
92723 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
92724 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
92725 pParse->nTab = 2;
92726 sqlite3SelectDestInit(&dest, SRT_Table, 1);
 
 
92727 sqlite3Select(pParse, pSelect, &dest);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92728 sqlite3VdbeAddOp1(v, OP_Close, 1);
92729 if( pParse->nErr==0 ){
92730 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
92731 if( pSelTab==0 ) return;
92732 assert( p->aCol==0 );
92733 p->nCol = pSelTab->nCol;
92734 p->aCol = pSelTab->aCol;
92735 pSelTab->nCol = 0;
92736 pSelTab->aCol = 0;
92737 sqlite3DeleteTable(db, pSelTab);
92738 }
92739 }
92740
92741 /* Compute the complete text of the CREATE statement */
92742 if( pSelect ){
92743 zStmt = createTableStmt(db, p);
@@ -94032,14 +94147,19 @@
94032 int iMem = ++pParse->nMem;
94033
94034 v = sqlite3GetVdbe(pParse);
94035 if( v==0 ) goto exit_create_index;
94036
94037
94038 /* Create the rootpage for the index
94039 */
94040 sqlite3BeginWriteOperation(pParse, 1, iDb);
 
 
 
 
 
 
 
 
94041 sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
94042
94043 /* Gather the complete text of the CREATE INDEX statement into
94044 ** the zStmt variable
94045 */
@@ -94075,10 +94195,12 @@
94075 sqlite3ChangeCookie(pParse, iDb);
94076 sqlite3VdbeAddParseSchemaOp(v, iDb,
94077 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
94078 sqlite3VdbeAddOp1(v, OP_Expire, 0);
94079 }
 
 
94080 }
94081
94082 /* When adding an index to the list of indices for a table, make
94083 ** sure all indices labeled OE_Replace come after all those labeled
94084 ** OE_Ignore. This is necessary for the correct constraint check
@@ -99670,11 +99792,11 @@
99670 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
99671 VdbeComment((v, "%s", pTab->zName));
99672 }else{
99673 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
99674 assert( pPk!=0 );
99675 assert( pPk->tnum=pTab->tnum );
99676 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
99677 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
99678 VdbeComment((v, "%s", pTab->zName));
99679 }
99680 }
@@ -102120,10 +102242,12 @@
102120 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
102121 void(*)(void*));
102122 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
102123 void(*)(void*), unsigned char);
102124 int (*strglob)(const char*,const char*);
 
 
102125 };
102126
102127 /*
102128 ** The following macros redefine the API routines so that they are
102129 ** redirected through the global sqlite3_api structure.
@@ -102350,10 +102474,13 @@
102350 #define sqlite3_realloc64 sqlite3_api->realloc64
102351 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
102352 #define sqlite3_result_blob64 sqlite3_api->result_blob64
102353 #define sqlite3_result_text64 sqlite3_api->result_text64
102354 #define sqlite3_strglob sqlite3_api->strglob
 
 
 
102355 #endif /* SQLITE_CORE */
102356
102357 #ifndef SQLITE_CORE
102358 /* This case when the file really is being compiled as a loadable
102359 ** extension */
@@ -103256,10 +103383,14 @@
103256 #endif
103257 { /* zName: */ "case_sensitive_like",
103258 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE,
103259 /* ePragFlag: */ 0,
103260 /* iArg: */ 0 },
 
 
 
 
103261 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
103262 { /* zName: */ "checkpoint_fullfsync",
103263 /* ePragTyp: */ PragTyp_FLAG,
103264 /* ePragFlag: */ 0,
103265 /* iArg: */ SQLITE_CkptFullFSync },
@@ -103613,11 +103744,11 @@
103613 /* ePragTyp: */ PragTyp_FLAG,
103614 /* ePragFlag: */ 0,
103615 /* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
103616 #endif
103617 };
103618 /* Number of pragmas: 59 on by default, 72 total. */
103619
103620 /************** End of pragma.h **********************************************/
103621 /************** Continuing where we left off in pragma.c *********************/
103622
103623 /*
@@ -105596,17 +105727,17 @@
105596 const char *zObj, /* Object being parsed at the point of error */
105597 const char *zExtra /* Error information */
105598 ){
105599 sqlite3 *db = pData->db;
105600 if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
 
105601 if( zObj==0 ) zObj = "?";
105602 sqlite3SetString(pData->pzErrMsg, db,
105603 "malformed database schema (%s)", zObj);
105604 if( zExtra ){
105605 *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg,
105606 "%s - %s", *pData->pzErrMsg, zExtra);
105607 }
105608 }
105609 pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
105610 }
105611
105612 /*
@@ -105794,11 +105925,11 @@
105794 ** will be closed before this function returns. */
105795 sqlite3BtreeEnter(pDb->pBt);
105796 if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
105797 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
105798 if( rc!=SQLITE_OK ){
105799 sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
105800 goto initone_error_out;
105801 }
105802 openedTransaction = 1;
105803 }
105804
@@ -107178,12 +107309,17 @@
107178 }
107179 }else if( eDest!=SRT_Exists ){
107180 /* If the destination is an EXISTS(...) expression, the actual
107181 ** values returned by the SELECT are not required.
107182 */
107183 sqlite3ExprCodeExprList(pParse, pEList, regResult,
107184 (eDest==SRT_Output||eDest==SRT_Coroutine)?SQLITE_ECEL_DUP:0);
 
 
 
 
 
107185 }
107186
107187 /* If the DISTINCT keyword was present on the SELECT statement
107188 ** and this row has been seen before, then do not make this row
107189 ** part of the result.
@@ -107276,10 +107412,12 @@
107276 case SRT_Table:
107277 case SRT_EphemTab: {
107278 int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
107279 testcase( eDest==SRT_Table );
107280 testcase( eDest==SRT_EphemTab );
 
 
107281 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
107282 #ifndef SQLITE_OMIT_CTE
107283 if( eDest==SRT_DistFifo ){
107284 /* If the destination is DistFifo, then cursor (iParm+1) is open
107285 ** on an ephemeral index. If the current row is already present
@@ -107691,14 +107829,11 @@
107691 for(i=0; i<nSortData; i++){
107692 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq+i, regRow+i);
107693 VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
107694 }
107695 switch( eDest ){
107696 case SRT_Table:
107697 case SRT_EphemTab: {
107698 testcase( eDest==SRT_Table );
107699 testcase( eDest==SRT_EphemTab );
107700 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
107701 sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
107702 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
107703 break;
107704 }
@@ -109043,19 +109178,18 @@
109043
109044 /* Suppress the first OFFSET entries if there is an OFFSET clause
109045 */
109046 codeOffset(v, p->iOffset, iContinue);
109047
 
 
109048 switch( pDest->eDest ){
109049 /* Store the result as data using a unique key.
109050 */
109051 case SRT_Table:
109052 case SRT_EphemTab: {
109053 int r1 = sqlite3GetTempReg(pParse);
109054 int r2 = sqlite3GetTempReg(pParse);
109055 testcase( pDest->eDest==SRT_Table );
109056 testcase( pDest->eDest==SRT_EphemTab );
109057 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
109058 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
109059 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
109060 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
109061 sqlite3ReleaseTempReg(pParse, r2);
@@ -109079,20 +109213,10 @@
109079 sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
109080 sqlite3ReleaseTempReg(pParse, r1);
109081 break;
109082 }
109083
109084 #if 0 /* Never occurs on an ORDER BY query */
109085 /* If any row exist in the result set, record that fact and abort.
109086 */
109087 case SRT_Exists: {
109088 sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm);
109089 /* The LIMIT clause will terminate the loop for us */
109090 break;
109091 }
109092 #endif
109093
109094 /* If this is a scalar select that is part of an expression, then
109095 ** store the results in the appropriate memory cell and break out
109096 ** of the scan loop.
109097 */
109098 case SRT_Mem: {
@@ -110463,11 +110587,11 @@
110463 if( pTab==0 ) return WRC_Abort;
110464 pTab->nRef = 1;
110465 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
110466 pTab->iPKey = -1;
110467 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
110468 pTab->tabFlags |= TF_Ephemeral;
110469 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
110470 if( db->mallocFailed ) return SQLITE_NOMEM;
110471 assert( pFrom->pSelect );
110472
110473 /* Check if this is a recursive CTE. */
@@ -110708,17 +110832,10 @@
110708 ExprList *pNew = 0;
110709 int flags = pParse->db->flags;
110710 int longNames = (flags & SQLITE_FullColNames)!=0
110711 && (flags & SQLITE_ShortColNames)==0;
110712
110713 /* When processing FROM-clause subqueries, it is always the case
110714 ** that full_column_names=OFF and short_column_names=ON. The
110715 ** sqlite3ResultSetOfSelect() routine makes it so. */
110716 assert( (p->selFlags & SF_NestedFrom)==0
110717 || ((flags & SQLITE_FullColNames)==0 &&
110718 (flags & SQLITE_ShortColNames)!=0) );
110719
110720 for(k=0; k<pEList->nExpr; k++){
110721 pE = a[k].pExpr;
110722 pRight = pE->pRight;
110723 assert( pE->op!=TK_DOT || pRight!=0 );
110724 if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pRight->op!=TK_ALL) ){
@@ -111296,10 +111413,11 @@
111296 isAgg = 1;
111297 p->selFlags |= SF_Aggregate;
111298 }
111299 i = -1;
111300 }else if( pTabList->nSrc==1
 
111301 && OptimizationEnabled(db, SQLITE_SubqCoroutine)
111302 ){
111303 /* Implement a co-routine that will return a single row of the result
111304 ** set on each invocation.
111305 */
@@ -111983,13 +112101,13 @@
111983 ** Generate a human-readable description of a the Select object.
111984 */
111985 SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
111986 int n = 0;
111987 pView = sqlite3TreeViewPush(pView, moreToFollow);
111988 sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p)",
111989 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
111990 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p
111991 );
111992 if( p->pSrc && p->pSrc->nSrc ) n++;
111993 if( p->pWhere ) n++;
111994 if( p->pGroupBy ) n++;
111995 if( p->pHaving ) n++;
@@ -114139,16 +114257,14 @@
114139 /* Create the ephemeral table into which the update results will
114140 ** be stored.
114141 */
114142 assert( v );
114143 ephemTab = pParse->nTab++;
114144 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
114145 sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
114146
114147 /* fill the ephemeral table
114148 */
114149 sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
114150 sqlite3Select(pParse, pSelect, &dest);
114151
114152 /* Generate code to scan the ephemeral table and call VUpdate. */
114153 iReg = ++pParse->nMem;
114154 pParse->nMem += pTab->nCol+1;
@@ -115999,10 +116115,11 @@
115999 # define TERM_VNULL 0x00 /* Disabled if not using stat3 */
116000 #endif
116001 #define TERM_LIKEOPT 0x100 /* Virtual terms from the LIKE optimization */
116002 #define TERM_LIKECOND 0x200 /* Conditionally this LIKE operator term */
116003 #define TERM_LIKE 0x400 /* The original LIKE operator */
 
116004
116005 /*
116006 ** An instance of the WhereScan object is used as an iterator for locating
116007 ** terms in the WHERE clause that are useful to the query planner.
116008 */
@@ -116147,25 +116264,26 @@
116147 ** Bitmasks for the operators on WhereTerm objects. These are all
116148 ** operators that are of interest to the query planner. An
116149 ** OR-ed combination of these values can be used when searching for
116150 ** particular WhereTerms within a WhereClause.
116151 */
116152 #define WO_IN 0x001
116153 #define WO_EQ 0x002
116154 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
116155 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
116156 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
116157 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
116158 #define WO_MATCH 0x040
116159 #define WO_ISNULL 0x080
116160 #define WO_OR 0x100 /* Two or more OR-connected terms */
116161 #define WO_AND 0x200 /* Two or more AND-connected terms */
116162 #define WO_EQUIV 0x400 /* Of the form A==B, both columns */
116163 #define WO_NOOP 0x800 /* This term does not restrict search space */
116164
116165 #define WO_ALL 0xfff /* Mask of all possible WO_* values */
116166 #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */
 
116167
116168 /*
116169 ** These are definitions of bits in the WhereLoop.wsFlags field.
116170 ** The particular combination of bits in each WhereLoop help to
116171 ** determine the algorithm that WhereLoop represents.
@@ -116535,11 +116653,11 @@
116535 static int allowedOp(int op){
116536 assert( TK_GT>TK_EQ && TK_GT<TK_GE );
116537 assert( TK_LT>TK_EQ && TK_LT<TK_GE );
116538 assert( TK_LE>TK_EQ && TK_LE<TK_GE );
116539 assert( TK_GE==TK_EQ+4 );
116540 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
116541 }
116542
116543 /*
116544 ** Commute a comparison operator. Expressions of the form "X op Y"
116545 ** are converted into "Y op X".
@@ -116588,10 +116706,12 @@
116588 assert( allowedOp(op) );
116589 if( op==TK_IN ){
116590 c = WO_IN;
116591 }else if( op==TK_ISNULL ){
116592 c = WO_ISNULL;
 
 
116593 }else{
116594 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
116595 c = (u16)(WO_EQ<<(op-TK_EQ));
116596 }
116597 assert( op!=TK_ISNULL || c==WO_ISNULL );
@@ -116599,10 +116719,11 @@
116599 assert( op!=TK_EQ || c==WO_EQ );
116600 assert( op!=TK_LT || c==WO_LT );
116601 assert( op!=TK_LE || c==WO_LE );
116602 assert( op!=TK_GT || c==WO_GT );
116603 assert( op!=TK_GE || c==WO_GE );
 
116604 return c;
116605 }
116606
116607 /*
116608 ** Advance to the next WhereTerm that matches according to the criteria
@@ -116659,15 +116780,16 @@
116659 if( pColl==0 ) pColl = pParse->db->pDfltColl;
116660 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
116661 continue;
116662 }
116663 }
116664 if( (pTerm->eOperator & WO_EQ)!=0
116665 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
116666 && pX->iTable==pScan->aEquiv[0]
116667 && pX->iColumn==pScan->aEquiv[1]
116668 ){
 
116669 continue;
116670 }
116671 pScan->k = k+1;
116672 return pTerm;
116673 }
@@ -116765,13 +116887,15 @@
116765 WhereTerm *pResult = 0;
116766 WhereTerm *p;
116767 WhereScan scan;
116768
116769 p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
 
116770 while( p ){
116771 if( (p->prereqRight & notReady)==0 ){
116772 if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){
 
116773 return p;
116774 }
116775 if( pResult==0 ) pResult = p;
116776 }
116777 p = whereScanNext(&scan);
@@ -116802,11 +116926,11 @@
116802 ** so and false if not.
116803 **
116804 ** In order for the operator to be optimizible, the RHS must be a string
116805 ** literal that does not begin with a wildcard. The LHS must be a column
116806 ** that may only be NULL, a string, or a BLOB, never a number. (This means
116807 ** that virtual tables cannot participate in the LIKE optimization.) If the
116808 ** collating sequence for the column on the LHS must be appropriate for
116809 ** the operator.
116810 */
116811 static int isLikeOrGlob(
116812 Parse *pParse, /* Parsing and code generating context */
@@ -117347,10 +117471,50 @@
117347 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */
117348 }
117349 }
117350 }
117351 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117352
117353 /*
117354 ** The input to this routine is an WhereTerm structure with only the
117355 ** "pExpr" field filled in. The job of this routine is to analyze the
117356 ** subexpression and populate all the other fields of the WhereTerm
@@ -117426,10 +117590,11 @@
117426 if( pLeft->op==TK_COLUMN ){
117427 pTerm->leftCursor = pLeft->iTable;
117428 pTerm->u.leftColumn = pLeft->iColumn;
117429 pTerm->eOperator = operatorMask(op) & opMask;
117430 }
 
117431 if( pRight && pRight->op==TK_COLUMN ){
117432 WhereTerm *pNew;
117433 Expr *pDup;
117434 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
117435 if( pTerm->leftCursor>=0 ){
@@ -117441,16 +117606,15 @@
117441 }
117442 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
117443 if( idxNew==0 ) return;
117444 pNew = &pWC->a[idxNew];
117445 markTermAsChild(pWC, idxNew, idxTerm);
 
117446 pTerm = &pWC->a[idxTerm];
117447 pTerm->wtFlags |= TERM_COPIED;
117448 if( pExpr->op==TK_EQ
117449 && !ExprHasProperty(pExpr, EP_FromJoin)
117450 && OptimizationEnabled(db, SQLITE_Transitive)
117451 ){
117452 pTerm->eOperator |= WO_EQUIV;
117453 eExtraOp = WO_EQUIV;
117454 }
117455 }else{
117456 pDup = pExpr;
@@ -117640,14 +117804,11 @@
117640 /* When sqlite_stat3 histogram data is available an operator of the
117641 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
117642 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
117643 ** virtual term of that form.
117644 **
117645 ** Note that the virtual term must be tagged with TERM_VNULL. This
117646 ** TERM_VNULL tag will suppress the not-null check at the beginning
117647 ** of the loop. Without the TERM_VNULL flag, the not-null check at
117648 ** the start of the loop will prevent any results from being returned.
117649 */
117650 if( pExpr->op==TK_NOTNULL
117651 && pExpr->pLeft->op==TK_COLUMN
117652 && pExpr->pLeft->iColumn>=0
117653 && OptimizationEnabled(db, SQLITE_Stat34)
@@ -117788,10 +117949,40 @@
117788 ** Estimate the logarithm of the input value to base 2.
117789 */
117790 static LogEst estLog(LogEst N){
117791 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
117792 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117793
117794 /*
117795 ** Two routines for printing the content of an sqlite3_index_info
117796 ** structure. Used for testing and debugging only. If neither
117797 ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
@@ -117847,15 +118038,16 @@
117847 struct SrcList_item *pSrc, /* Table we are trying to access */
117848 Bitmask notReady /* Tables in outer loops of the join */
117849 ){
117850 char aff;
117851 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
117852 if( (pTerm->eOperator & WO_EQ)==0 ) return 0;
117853 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
117854 if( pTerm->u.leftColumn<0 ) return 0;
117855 aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
117856 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
 
117857 return 1;
117858 }
117859 #endif
117860
117861
@@ -117890,10 +118082,11 @@
117890 Bitmask idxCols; /* Bitmap of columns used for indexing */
117891 Bitmask extraCols; /* Bitmap of additional columns */
117892 u8 sentWarning = 0; /* True if a warnning has been issued */
117893 Expr *pPartial = 0; /* Partial Index Expression */
117894 int iContinue = 0; /* Jump here to skip excluded rows */
 
117895
117896 /* Generate code to skip over the creation and initialization of the
117897 ** transient index on 2nd and subsequent iterations of the loop. */
117898 v = pParse->pVdbe;
117899 assert( v!=0 );
@@ -118015,11 +118208,20 @@
118015 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
118016 VdbeComment((v, "for %s", pTable->zName));
118017
118018 /* Fill the automatic index with content */
118019 sqlite3ExprCachePush(pParse);
118020 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
 
 
 
 
 
 
 
 
 
118021 if( pPartial ){
118022 iContinue = sqlite3VdbeMakeLabel(v);
118023 sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL);
118024 pLoop->wsFlags |= WHERE_PARTIALIDX;
118025 }
@@ -118026,11 +118228,17 @@
118026 regRecord = sqlite3GetTempReg(pParse);
118027 sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0);
118028 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
118029 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
118030 if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
118031 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
 
 
 
 
 
 
118032 sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
118033 sqlite3VdbeJumpHere(v, addrTop);
118034 sqlite3ReleaseTempReg(pParse, regRecord);
118035 sqlite3ExprCachePop(pParse);
118036
@@ -118068,12 +118276,13 @@
118068 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
118069 if( pTerm->leftCursor != pSrc->iCursor ) continue;
118070 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
118071 testcase( pTerm->eOperator & WO_IN );
118072 testcase( pTerm->eOperator & WO_ISNULL );
 
118073 testcase( pTerm->eOperator & WO_ALL );
118074 if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
118075 if( pTerm->wtFlags & TERM_VNULL ) continue;
118076 nTerm++;
118077 }
118078
118079 /* If the ORDER BY clause contains only columns in the current
@@ -118120,13 +118329,14 @@
118120 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
118121 u8 op;
118122 if( pTerm->leftCursor != pSrc->iCursor ) continue;
118123 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
118124 testcase( pTerm->eOperator & WO_IN );
 
118125 testcase( pTerm->eOperator & WO_ISNULL );
118126 testcase( pTerm->eOperator & WO_ALL );
118127 if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
118128 if( pTerm->wtFlags & TERM_VNULL ) continue;
118129 pIdxCons[j].iColumn = pTerm->u.leftColumn;
118130 pIdxCons[j].iTermOffset = i;
118131 op = (u8)pTerm->eOperator & WO_ALL;
118132 if( op==WO_IN ) op = WO_EQ;
@@ -118964,11 +119174,11 @@
118964 Expr *pX = pTerm->pExpr;
118965 Vdbe *v = pParse->pVdbe;
118966 int iReg; /* Register holding results */
118967
118968 assert( iTarget>0 );
118969 if( pX->op==TK_EQ ){
118970 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
118971 }else if( pX->op==TK_ISNULL ){
118972 iReg = iTarget;
118973 sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
118974 #ifndef SQLITE_OMIT_SUBQUERY
@@ -119149,11 +119359,11 @@
119149 }
119150 testcase( pTerm->eOperator & WO_ISNULL );
119151 testcase( pTerm->eOperator & WO_IN );
119152 if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
119153 Expr *pRight = pTerm->pExpr->pRight;
119154 if( sqlite3ExprCanBeNull(pRight) ){
119155 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
119156 VdbeCoverage(v);
119157 }
119158 if( zAff ){
119159 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
@@ -120271,20 +120481,23 @@
120271 */
120272 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
120273 Expr *pE, *pEAlt;
120274 WhereTerm *pAlt;
120275 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
120276 if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue;
 
120277 if( pTerm->leftCursor!=iCur ) continue;
120278 if( pLevel->iLeftJoin ) continue;
120279 pE = pTerm->pExpr;
120280 assert( !ExprHasProperty(pE, EP_FromJoin) );
120281 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
120282 pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
 
120283 if( pAlt==0 ) continue;
120284 if( pAlt->wtFlags & (TERM_CODED) ) continue;
120285 testcase( pAlt->eOperator & WO_EQ );
 
120286 testcase( pAlt->eOperator & WO_IN );
120287 VdbeModuleComment((v, "begin transitive constraint"));
120288 pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
120289 if( pEAlt ){
120290 *pEAlt = *pAlt->pExpr;
@@ -120330,13 +120543,14 @@
120330 char zType[4];
120331 memcpy(zType, "...", 4);
120332 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
120333 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
120334 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
120335 sqlite3DebugPrintf("TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x\n",
120336 iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb,
120337 pTerm->eOperator);
 
120338 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
120339 }
120340 }
120341 #endif
120342
@@ -120822,12 +121036,13 @@
120822 pLoop->nOut += pTerm->truthProb;
120823 }else{
120824 /* In the absence of explicit truth probabilities, use heuristics to
120825 ** guess a reasonable truth probability. */
120826 pLoop->nOut--;
120827 if( pTerm->eOperator&WO_EQ ){
120828 Expr *pRight = pTerm->pExpr->pRight;
 
120829 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
120830 k = 10;
120831 }else{
120832 k = 20;
120833 }
@@ -120891,14 +121106,14 @@
120891
120892 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
120893 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
120894 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
120895 opMask = WO_LT|WO_LE;
120896 }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){
120897 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE;
120898 }else{
120899 opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE;
120900 }
120901 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
120902
120903 assert( pNew->u.btree.nEq<pProbe->nColumn );
120904 iCol = pProbe->aiColumn[pNew->u.btree.nEq];
@@ -120957,11 +121172,11 @@
120957 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
120958 }
120959 assert( nIn>0 ); /* RHS always has 2 or more terms... The parser
120960 ** changes "x IN (?)" into "x=?". */
120961
120962 }else if( eOp & (WO_EQ) ){
120963 pNew->wsFlags |= WHERE_COLUMN_EQ;
120964 if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
120965 if( iCol>=0 && pProbe->uniqNotNull==0 ){
120966 pNew->wsFlags |= WHERE_UNQ_WANTED;
120967 }else{
@@ -121007,11 +121222,11 @@
121007 /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4
121008 ** data, using some other estimate. */
121009 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
121010 }else{
121011 int nEq = ++pNew->u.btree.nEq;
121012 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN) );
121013
121014 assert( pNew->nOut==saved_nOut );
121015 if( pTerm->truthProb<=0 && iCol>=0 ){
121016 assert( (eOp & WO_IN) || nIn==0 );
121017 testcase( eOp & WO_IN );
@@ -121024,12 +121239,13 @@
121024 && pProbe->nSample
121025 && pNew->u.btree.nEq<=pProbe->nSampleCol
121026 && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
121027 ){
121028 Expr *pExpr = pTerm->pExpr;
121029 if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){
121030 testcase( eOp & WO_EQ );
 
121031 testcase( eOp & WO_ISNULL );
121032 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
121033 }else{
121034 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
121035 }
@@ -121294,19 +121510,18 @@
121294 rSize = pTab->nRowLogEst;
121295 rLogSize = estLog(rSize);
121296
121297 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
121298 /* Automatic indexes */
121299 if( !pBuilder->pOrSet
121300 && (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0
121301 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
121302 && pSrc->pIndex==0
121303 && !pSrc->viaCoroutine
121304 && !pSrc->notIndexed
121305 && HasRowid(pTab)
121306 && !pSrc->isCorrelated
121307 && !pSrc->isRecursive
121308 ){
121309 /* Generate auto-index WhereLoops */
121310 WhereTerm *pTerm;
121311 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
121312 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
@@ -121862,21 +122077,22 @@
121862 if( MASKBIT(i) & obSat ) continue;
121863 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
121864 if( pOBExpr->op!=TK_COLUMN ) continue;
121865 if( pOBExpr->iTable!=iCur ) continue;
121866 pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
121867 ~ready, WO_EQ|WO_ISNULL, 0);
121868 if( pTerm==0 ) continue;
121869 if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){
121870 const char *z1, *z2;
121871 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
121872 if( !pColl ) pColl = db->pDfltColl;
121873 z1 = pColl->zName;
121874 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr);
121875 if( !pColl ) pColl = db->pDfltColl;
121876 z2 = pColl->zName;
121877 if( sqlite3StrICmp(z1, z2)!=0 ) continue;
 
121878 }
121879 obSat |= MASKBIT(i);
121880 }
121881
121882 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
@@ -121903,11 +122119,11 @@
121903 u8 bOnce; /* True to run the ORDER BY search loop */
121904
121905 /* Skip over == and IS NULL terms */
121906 if( j<pLoop->u.btree.nEq
121907 && pLoop->nSkip==0
121908 && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0
121909 ){
121910 if( i & WO_ISNULL ){
121911 testcase( isOrderDistinct );
121912 isOrderDistinct = 0;
121913 }
@@ -122476,28 +122692,32 @@
122476 iCur = pItem->iCursor;
122477 pWC = &pWInfo->sWC;
122478 pLoop = pBuilder->pNew;
122479 pLoop->wsFlags = 0;
122480 pLoop->nSkip = 0;
122481 pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0);
122482 if( pTerm ){
 
122483 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
122484 pLoop->aLTerm[0] = pTerm;
122485 pLoop->nLTerm = 1;
122486 pLoop->u.btree.nEq = 1;
122487 /* TUNING: Cost of a rowid lookup is 10 */
122488 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
122489 }else{
122490 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 
122491 assert( pLoop->aLTermSpace==pLoop->aLTerm );
122492 if( !IsUniqueIndex(pIdx)
122493 || pIdx->pPartIdxWhere!=0
122494 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
122495 ) continue;
 
122496 for(j=0; j<pIdx->nKeyCol; j++){
122497 pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
122498 if( pTerm==0 ) break;
 
122499 pLoop->aLTerm[j] = pTerm;
122500 }
122501 if( j!=pIdx->nKeyCol ) continue;
122502 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
122503 if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
@@ -123132,30 +123352,16 @@
123132 Table *pTab = pTabItem->pTab;
123133 assert( pTab!=0 );
123134 pLoop = pLevel->pWLoop;
123135
123136 /* For a co-routine, change all OP_Column references to the table of
123137 ** the co-routine into OP_SCopy of result contained in a register.
123138 ** OP_Rowid becomes OP_Null.
123139 */
123140 if( pTabItem->viaCoroutine && !db->mallocFailed ){
123141 last = sqlite3VdbeCurrentAddr(v);
123142 k = pLevel->addrBody;
123143 pOp = sqlite3VdbeGetOp(v, k);
123144 for(; k<last; k++, pOp++){
123145 if( pOp->p1!=pLevel->iTabCur ) continue;
123146 if( pOp->opcode==OP_Column ){
123147 pOp->opcode = OP_Copy;
123148 pOp->p1 = pOp->p2 + pTabItem->regResult;
123149 pOp->p2 = pOp->p3;
123150 pOp->p3 = 0;
123151 }else if( pOp->opcode==OP_Rowid ){
123152 pOp->opcode = OP_Null;
123153 pOp->p1 = 0;
123154 pOp->p3 = 0;
123155 }
123156 }
123157 continue;
123158 }
123159
123160 /* Close all of the cursors that were opened by sqlite3WhereBegin.
123161 ** Except, do not close cursors that will be reused by the OR optimization
@@ -125419,11 +125625,11 @@
125419 {yygotominor.yy186 = 0;}
125420 break;
125421 case 35: /* table_options ::= WITHOUT nm */
125422 {
125423 if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
125424 yygotominor.yy186 = TF_WithoutRowid;
125425 }else{
125426 yygotominor.yy186 = 0;
125427 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
125428 }
125429 }
@@ -125643,10 +125849,11 @@
125643 {yygotominor.yy3 = yymsp[0].minor.yy3;}
125644 break;
125645 case 114: /* selectnowith ::= selectnowith multiselect_op oneselect */
125646 {
125647 Select *pRhs = yymsp[0].minor.yy3;
 
125648 if( pRhs && pRhs->pPrior ){
125649 SrcList *pFrom;
125650 Token x;
125651 x.n = 0;
125652 parserDoubleLinkSelect(pParse, pRhs);
@@ -125653,15 +125860,16 @@
125653 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
125654 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0);
125655 }
125656 if( pRhs ){
125657 pRhs->op = (u8)yymsp[-1].minor.yy328;
125658 pRhs->pPrior = yymsp[-2].minor.yy3;
 
125659 pRhs->selFlags &= ~SF_MultiValue;
125660 if( yymsp[-1].minor.yy328!=TK_ALL ) pParse->hasCompound = 1;
125661 }else{
125662 sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy3);
125663 }
125664 yygotominor.yy3 = pRhs;
125665 }
125666 break;
125667 case 116: /* multiselect_op ::= UNION ALL */
@@ -125718,11 +125926,13 @@
125718 break;
125719 case 122: /* distinct ::= DISTINCT */
125720 {yygotominor.yy381 = SF_Distinct;}
125721 break;
125722 case 123: /* distinct ::= ALL */
125723 case 124: /* distinct ::= */ yytestcase(yyruleno==124);
 
 
125724 {yygotominor.yy381 = 0;}
125725 break;
125726 case 125: /* sclp ::= selcollist COMMA */
125727 case 243: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==243);
125728 {yygotominor.yy14 = yymsp[-1].minor.yy14;}
@@ -126013,11 +126223,11 @@
126013 if( yymsp[-1].minor.yy14 && yymsp[-1].minor.yy14->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
126014 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
126015 }
126016 yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy14, &yymsp[-4].minor.yy0);
126017 spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
126018 if( yymsp[-2].minor.yy381 && yygotominor.yy346.pExpr ){
126019 yygotominor.yy346.pExpr->flags |= EP_Distinct;
126020 }
126021 }
126022 break;
126023 case 196: /* expr ::= ID|INDEXED LP STAR RP */
@@ -127534,11 +127744,12 @@
127534 }
127535 }
127536 }
127537 abort_parse:
127538 assert( nErr==0 );
127539 if( zSql[i]==0 && pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
 
127540 if( lastTokenParsed!=TK_SEMI ){
127541 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
127542 pParse->zTail = &zSql[i];
127543 }
127544 if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
@@ -127556,11 +127767,11 @@
127556 db->lookaside.bEnabled = enableLookaside;
127557 if( db->mallocFailed ){
127558 pParse->rc = SQLITE_NOMEM;
127559 }
127560 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
127561 sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
127562 }
127563 assert( pzErrMsg!=0 );
127564 if( pParse->zErrMsg ){
127565 *pzErrMsg = pParse->zErrMsg;
127566 sqlite3_log(pParse->rc, "%s", *pzErrMsg);
@@ -130748,10 +130959,13 @@
130748 | SQLITE_ForeignKeys
130749 #endif
130750 #if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
130751 | SQLITE_ReverseOrder
130752 #endif
 
 
 
130753 ;
130754 sqlite3HashInit(&db->aCollSeq);
130755 #ifndef SQLITE_OMIT_VIRTUALTABLE
130756 sqlite3HashInit(&db->aModule);
130757 #endif
@@ -130867,12 +131081,11 @@
130867 }
130868 #endif
130869
130870 #ifdef SQLITE_ENABLE_DBSTAT_VTAB
130871 if( !db->mallocFailed && rc==SQLITE_OK){
130872 int sqlite3_dbstat_register(sqlite3*);
130873 rc = sqlite3_dbstat_register(db);
130874 }
130875 #endif
130876
130877 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
130878 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
@@ -132872,10 +133085,12 @@
132872 typedef struct Fts3SegFilter Fts3SegFilter;
132873 typedef struct Fts3DeferredToken Fts3DeferredToken;
132874 typedef struct Fts3SegReader Fts3SegReader;
132875 typedef struct Fts3MultiSegReader Fts3MultiSegReader;
132876
 
 
132877 /*
132878 ** A connection to a fulltext index is an instance of the following
132879 ** structure. The xCreate and xConnect methods create an instance
132880 ** of this structure and xDestroy and xDisconnect free that instance.
132881 ** All other methods receive a pointer to the structure as one of their
@@ -132981,13 +133196,11 @@
132981 int nRowAvg; /* Average size of database rows, in pages */
132982 sqlite3_int64 nDoc; /* Documents in table */
132983 i64 iMinDocid; /* Minimum docid to return */
132984 i64 iMaxDocid; /* Maximum docid to return */
132985 int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */
132986 u32 *aMatchinfo; /* Information about most recent match */
132987 int nMatchinfo; /* Number of elements in aMatchinfo[] */
132988 char *zMatchinfo; /* Matchinfo specification */
132989 };
132990
132991 #define FTS3_EVAL_FILTER 0
132992 #define FTS3_EVAL_NEXT 1
132993 #define FTS3_EVAL_MATCHINFO 2
@@ -133103,11 +133316,13 @@
133103 sqlite3_int64 iDocid; /* Current docid */
133104 u8 bEof; /* True this expression is at EOF already */
133105 u8 bStart; /* True if iDocid is valid */
133106 u8 bDeferred; /* True if this expression is entirely deferred */
133107
133108 u32 *aMI;
 
 
133109 };
133110
133111 /*
133112 ** Candidate values for Fts3Query.eType. Note that the order of the first
133113 ** four values is in order of precedence when parsing expressions. For
@@ -133224,10 +133439,11 @@
133224 SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
133225 SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
133226 SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
133227 SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
133228 SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
 
133229
133230 /* fts3_tokenizer.c */
133231 SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
133232 SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
133233 SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
@@ -133239,10 +133455,11 @@
133239 SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
133240 SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
133241 const char *, const char *, int, int
133242 );
133243 SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
 
133244
133245 /* fts3_expr.c */
133246 SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
133247 char **, int, int, int, const char *, int, Fts3Expr **, char **
133248 );
@@ -134666,11 +134883,11 @@
134666 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
134667 sqlite3_finalize(pCsr->pStmt);
134668 sqlite3Fts3ExprFree(pCsr->pExpr);
134669 sqlite3Fts3FreeDeferredTokens(pCsr);
134670 sqlite3_free(pCsr->aDoclist);
134671 sqlite3_free(pCsr->aMatchinfo);
134672 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
134673 sqlite3_free(pCsr);
134674 return SQLITE_OK;
134675 }
134676
@@ -136167,11 +136384,11 @@
136167 assert( iIdx==nVal );
136168
136169 /* In case the cursor has been used before, clear it now. */
136170 sqlite3_finalize(pCsr->pStmt);
136171 sqlite3_free(pCsr->aDoclist);
136172 sqlite3_free(pCsr->aMatchinfo);
136173 sqlite3Fts3ExprFree(pCsr->pExpr);
136174 memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
136175
136176 /* Set the lower and upper bounds on docids to return */
136177 pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
@@ -138065,11 +138282,11 @@
138065 ** is populated as for "A * C" before returning.
138066 **
138067 ** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is
138068 ** advanced to point to the next row that matches "x AND y".
138069 **
138070 ** See fts3EvalTestDeferredAndNear() for details on testing if a row is
138071 ** really a match, taking into account deferred tokens and NEAR operators.
138072 */
138073 static void fts3EvalNextRow(
138074 Fts3Cursor *pCsr, /* FTS Cursor handle */
138075 Fts3Expr *pExpr, /* Expr. to advance to next matching row */
@@ -138285,11 +138502,11 @@
138285
138286 return res;
138287 }
138288
138289 /*
138290 ** This function is a helper function for fts3EvalTestDeferredAndNear().
138291 ** Assuming no error occurs or has occurred, It returns non-zero if the
138292 ** expression passed as the second argument matches the row that pCsr
138293 ** currently points to, or zero if it does not.
138294 **
138295 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
@@ -138406,11 +138623,11 @@
138406 ** it is determined that the row does *not* match the query.
138407 **
138408 ** Or, if no error occurs and it seems the current row does match the FTS
138409 ** query, return 0.
138410 */
138411 static int fts3EvalTestDeferredAndNear(Fts3Cursor *pCsr, int *pRc){
138412 int rc = *pRc;
138413 int bMiss = 0;
138414 if( rc==SQLITE_OK ){
138415
138416 /* If there are one or more deferred tokens, load the current row into
@@ -138453,11 +138670,11 @@
138453 fts3EvalNextRow(pCsr, pExpr, &rc);
138454 pCsr->isEof = pExpr->bEof;
138455 pCsr->isRequireSeek = 1;
138456 pCsr->isMatchinfoNeeded = 1;
138457 pCsr->iPrevId = pExpr->iDocid;
138458 }while( pCsr->isEof==0 && fts3EvalTestDeferredAndNear(pCsr, &rc) );
138459 }
138460
138461 /* Check if the cursor is past the end of the docid range specified
138462 ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */
138463 if( rc==SQLITE_OK && (
@@ -138614,11 +138831,11 @@
138614 pCsr->isRequireSeek = 1;
138615 pCsr->isMatchinfoNeeded = 1;
138616 pCsr->iPrevId = pRoot->iDocid;
138617 }while( pCsr->isEof==0
138618 && pRoot->eType==FTSQUERY_NEAR
138619 && fts3EvalTestDeferredAndNear(pCsr, &rc)
138620 );
138621
138622 if( rc==SQLITE_OK && pCsr->isEof==0 ){
138623 fts3EvalUpdateCounts(pRoot);
138624 }
@@ -138639,11 +138856,10 @@
138639 fts3EvalRestart(pCsr, pRoot, &rc);
138640 do {
138641 fts3EvalNextRow(pCsr, pRoot, &rc);
138642 assert( pRoot->bEof==0 );
138643 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
138644 fts3EvalTestDeferredAndNear(pCsr, &rc);
138645 }
138646 }
138647 return rc;
138648 }
138649
@@ -148651,10 +148867,11 @@
148651 #define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */
148652 #define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */
148653 #define FTS3_MATCHINFO_LCS 's' /* nCol values */
148654 #define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */
148655 #define FTS3_MATCHINFO_LHITS 'y' /* nCol*nPhrase values */
 
148656
148657 /*
148658 ** The default value for the second argument to matchinfo().
148659 */
148660 #define FTS3_MATCHINFO_DEFAULT "pcx"
@@ -148712,13 +148929,26 @@
148712 struct MatchInfo {
148713 Fts3Cursor *pCursor; /* FTS3 Cursor */
148714 int nCol; /* Number of columns in table */
148715 int nPhrase; /* Number of matchable phrases in query */
148716 sqlite3_int64 nDoc; /* Number of docs in database */
 
148717 u32 *aMatchinfo; /* Pre-allocated buffer */
148718 };
148719
 
 
 
 
 
 
 
 
 
 
 
 
148720
148721
148722 /*
148723 ** The snippet() and offsets() functions both return text values. An instance
148724 ** of the following structure is used to accumulate those values while the
@@ -148729,10 +148959,101 @@
148729 char *z; /* Pointer to buffer containing string */
148730 int n; /* Length of z in bytes (excl. nul-term) */
148731 int nAlloc; /* Allocated size of buffer z in bytes */
148732 };
148733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148734
148735 /*
148736 ** This function is used to help iterate through a position-list. A position
148737 ** list is a list of unique integers, sorted from smallest to largest. Each
148738 ** element of the list is represented by an FTS3 varint that takes the value
@@ -148766,11 +149087,11 @@
148766 int *piPhrase, /* Pointer to phrase counter */
148767 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
148768 void *pCtx /* Second argument to pass to callback */
148769 ){
148770 int rc; /* Return code */
148771 int eType = pExpr->eType; /* Type of expression node pExpr */
148772
148773 if( eType!=FTSQUERY_PHRASE ){
148774 assert( pExpr->pLeft && pExpr->pRight );
148775 rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
148776 if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
@@ -148799,10 +149120,11 @@
148799 void *pCtx /* Second argument to pass to callback */
148800 ){
148801 int iPhrase = 0; /* Variable used as the phrase counter */
148802 return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
148803 }
 
148804
148805 /*
148806 ** This is an fts3ExprIterate() callback used while loading the doclists
148807 ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
148808 ** fts3ExprLoadDoclists().
@@ -148844,12 +149166,11 @@
148844 return rc;
148845 }
148846
148847 static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
148848 (*(int *)ctx)++;
148849 UNUSED_PARAMETER(pExpr);
148850 UNUSED_PARAMETER(iPhrase);
148851 return SQLITE_OK;
148852 }
148853 static int fts3ExprPhraseCount(Fts3Expr *pExpr){
148854 int nPhrase = 0;
148855 (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
@@ -149066,11 +149387,11 @@
149066 sIter.pCsr = pCsr;
149067 sIter.iCol = iCol;
149068 sIter.nSnippet = nSnippet;
149069 sIter.nPhrase = nList;
149070 sIter.iCurrent = -1;
149071 rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter);
149072 if( rc==SQLITE_OK ){
149073
149074 /* Set the *pmSeen output variable. */
149075 for(i=0; i<nList; i++){
149076 if( sIter.aPhrase[i].pHead ){
@@ -149366,10 +149687,64 @@
149366 }
149367
149368 *ppCollist = pEnd;
149369 return nEntry;
149370 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149371
149372 /*
149373 ** fts3ExprIterate() callback used to collect the "global" matchinfo stats
149374 ** for a single query.
149375 **
@@ -149433,55 +149808,10 @@
149433 }
149434
149435 return rc;
149436 }
149437
149438 /*
149439 ** fts3ExprIterate() callback used to gather information for the matchinfo
149440 ** directive 'y'.
149441 */
149442 static int fts3ExprLHitsCb(
149443 Fts3Expr *pExpr, /* Phrase expression node */
149444 int iPhrase, /* Phrase number */
149445 void *pCtx /* Pointer to MatchInfo structure */
149446 ){
149447 MatchInfo *p = (MatchInfo *)pCtx;
149448 Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
149449 int rc = SQLITE_OK;
149450 int iStart = iPhrase * p->nCol;
149451 Fts3Expr *pEof; /* Ancestor node already at EOF */
149452
149453 /* This must be a phrase */
149454 assert( pExpr->pPhrase );
149455
149456 /* Initialize all output integers to zero. */
149457 memset(&p->aMatchinfo[iStart], 0, sizeof(u32) * p->nCol);
149458
149459 /* Check if this or any parent node is at EOF. If so, then all output
149460 ** values are zero. */
149461 for(pEof=pExpr; pEof && pEof->bEof==0; pEof=pEof->pParent);
149462
149463 if( pEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
149464 Fts3Phrase *pPhrase = pExpr->pPhrase;
149465 char *pIter = pPhrase->doclist.pList;
149466 int iCol = 0;
149467
149468 while( 1 ){
149469 int nHit = fts3ColumnlistCount(&pIter);
149470 if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
149471 p->aMatchinfo[iStart + iCol] = (u32)nHit;
149472 }
149473 assert( *pIter==0x00 || *pIter==0x01 );
149474 if( *pIter!=0x01 ) break;
149475 pIter++;
149476 pIter += fts3GetVarint32(pIter, &iCol);
149477 }
149478 }
149479
149480 return rc;
149481 }
149482
149483 static int fts3MatchinfoCheck(
149484 Fts3Table *pTab,
149485 char cArg,
149486 char **pzErr
149487 ){
@@ -149491,10 +149821,11 @@
149491 || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
149492 || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
149493 || (cArg==FTS3_MATCHINFO_LCS)
149494 || (cArg==FTS3_MATCHINFO_HITS)
149495 || (cArg==FTS3_MATCHINFO_LHITS)
 
149496 ){
149497 return SQLITE_OK;
149498 }
149499 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
149500 return SQLITE_ERROR;
@@ -149517,10 +149848,14 @@
149517 break;
149518
149519 case FTS3_MATCHINFO_LHITS:
149520 nVal = pInfo->nCol * pInfo->nPhrase;
149521 break;
 
 
 
 
149522
149523 default:
149524 assert( cArg==FTS3_MATCHINFO_HITS );
149525 nVal = pInfo->nCol * pInfo->nPhrase * 3;
149526 break;
@@ -149712,11 +150047,11 @@
149712 int i;
149713 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
149714 sqlite3_stmt *pSelect = 0;
149715
149716 for(i=0; rc==SQLITE_OK && zArg[i]; i++){
149717
149718 switch( zArg[i] ){
149719 case FTS3_MATCHINFO_NPHRASE:
149720 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
149721 break;
149722
@@ -149772,13 +150107,17 @@
149772 if( rc==SQLITE_OK ){
149773 rc = fts3MatchinfoLcs(pCsr, pInfo);
149774 }
149775 break;
149776
149777 case FTS3_MATCHINFO_LHITS:
149778 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprLHitsCb, (void*)pInfo);
 
 
 
149779 break;
 
149780
149781 default: {
149782 Fts3Expr *pExpr;
149783 assert( zArg[i]==FTS3_MATCHINFO_HITS );
149784 pExpr = pCsr->pExpr;
@@ -149788,10 +150127,11 @@
149788 if( pCsr->pDeferred ){
149789 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
149790 if( rc!=SQLITE_OK ) break;
149791 }
149792 rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
 
149793 if( rc!=SQLITE_OK ) break;
149794 }
149795 (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
149796 break;
149797 }
@@ -149807,73 +150147,90 @@
149807
149808 /*
149809 ** Populate pCsr->aMatchinfo[] with data for the current row. The
149810 ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
149811 */
149812 static int fts3GetMatchinfo(
 
149813 Fts3Cursor *pCsr, /* FTS3 Cursor object */
149814 const char *zArg /* Second argument to matchinfo() function */
149815 ){
149816 MatchInfo sInfo;
149817 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
149818 int rc = SQLITE_OK;
149819 int bGlobal = 0; /* Collect 'global' stats as well as local */
149820
 
 
 
149821 memset(&sInfo, 0, sizeof(MatchInfo));
149822 sInfo.pCursor = pCsr;
149823 sInfo.nCol = pTab->nColumn;
149824
149825 /* If there is cached matchinfo() data, but the format string for the
149826 ** cache does not match the format string for this request, discard
149827 ** the cached data. */
149828 if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){
149829 assert( pCsr->aMatchinfo );
149830 sqlite3_free(pCsr->aMatchinfo);
149831 pCsr->zMatchinfo = 0;
149832 pCsr->aMatchinfo = 0;
149833 }
149834
149835 /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the
149836 ** matchinfo function has been called for this query. In this case
149837 ** allocate the array used to accumulate the matchinfo data and
149838 ** initialize those elements that are constant for every row.
149839 */
149840 if( pCsr->aMatchinfo==0 ){
149841 int nMatchinfo = 0; /* Number of u32 elements in match-info */
149842 int nArg; /* Bytes in zArg */
149843 int i; /* Used to iterate through zArg */
149844
149845 /* Determine the number of phrases in the query */
149846 pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
149847 sInfo.nPhrase = pCsr->nPhrase;
149848
149849 /* Determine the number of integers in the buffer returned by this call. */
149850 for(i=0; zArg[i]; i++){
 
 
 
 
 
 
149851 nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
149852 }
149853
149854 /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
149855 nArg = (int)strlen(zArg);
149856 pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1);
149857 if( !pCsr->aMatchinfo ) return SQLITE_NOMEM;
149858
149859 pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo];
149860 pCsr->nMatchinfo = nMatchinfo;
149861 memcpy(pCsr->zMatchinfo, zArg, nArg+1);
149862 memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo);
149863 pCsr->isMatchinfoNeeded = 1;
149864 bGlobal = 1;
149865 }
149866
149867 sInfo.aMatchinfo = pCsr->aMatchinfo;
149868 sInfo.nPhrase = pCsr->nPhrase;
149869 if( pCsr->isMatchinfoNeeded ){
 
 
 
 
 
 
 
149870 rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
149871 pCsr->isMatchinfoNeeded = 0;
 
 
149872 }
149873
149874 return rc;
 
 
 
 
 
 
149875 }
149876
149877 /*
149878 ** Implementation of snippet() function.
149879 */
@@ -150075,11 +150432,11 @@
150075 ** no way that this operation can fail, so the return code from
150076 ** fts3ExprIterate() can be discarded.
150077 */
150078 sCtx.iCol = iCol;
150079 sCtx.iTerm = 0;
150080 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx);
150081
150082 /* Retreive the text stored in column iCol. If an SQL NULL is stored
150083 ** in column iCol, jump immediately to the next iteration of the loop.
150084 ** If an OOM occurs while retrieving the data (this can happen if SQLite
150085 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
@@ -150167,42 +150524,25 @@
150167 sqlite3_context *pContext, /* Function call context */
150168 Fts3Cursor *pCsr, /* FTS3 table cursor */
150169 const char *zArg /* Second arg to matchinfo() function */
150170 ){
150171 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
150172 int rc;
150173 int i;
150174 const char *zFormat;
150175
150176 if( zArg ){
150177 for(i=0; zArg[i]; i++){
150178 char *zErr = 0;
150179 if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
150180 sqlite3_result_error(pContext, zErr, -1);
150181 sqlite3_free(zErr);
150182 return;
150183 }
150184 }
150185 zFormat = zArg;
150186 }else{
150187 zFormat = FTS3_MATCHINFO_DEFAULT;
150188 }
150189
150190 if( !pCsr->pExpr ){
150191 sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
150192 return;
150193 }
150194
150195 /* Retrieve matchinfo() data. */
150196 rc = fts3GetMatchinfo(pCsr, zFormat);
150197 sqlite3Fts3SegmentsClose(pTab);
150198
150199 if( rc!=SQLITE_OK ){
150200 sqlite3_result_error_code(pContext, rc);
150201 }else{
150202 int n = pCsr->nMatchinfo * sizeof(u32);
150203 sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT);
 
150204 }
150205 }
150206
150207 #endif
150208
@@ -151319,10 +151659,11 @@
151319 */
151320 struct RtreeMatchArg {
151321 u32 magic; /* Always RTREE_GEOMETRY_MAGIC */
151322 RtreeGeomCallback cb; /* Info about the callback functions */
151323 int nParam; /* Number of parameters to the SQL function */
 
151324 RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
151325 };
151326
151327 #ifndef MAX
151328 # define MAX(x,y) ((x) < (y) ? (y) : (x))
@@ -152450,13 +152791,11 @@
152450 /* Check that value is actually a blob. */
152451 if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
152452
152453 /* Check that the blob is roughly the right size. */
152454 nBlob = sqlite3_value_bytes(pValue);
152455 if( nBlob<(int)sizeof(RtreeMatchArg)
152456 || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
152457 ){
152458 return SQLITE_ERROR;
152459 }
152460
152461 pInfo = (sqlite3_rtree_query_info*)sqlite3_malloc( sizeof(*pInfo)+nBlob );
152462 if( !pInfo ) return SQLITE_NOMEM;
@@ -152463,18 +152802,20 @@
152463 memset(pInfo, 0, sizeof(*pInfo));
152464 pBlob = (RtreeMatchArg*)&pInfo[1];
152465
152466 memcpy(pBlob, sqlite3_value_blob(pValue), nBlob);
152467 nExpected = (int)(sizeof(RtreeMatchArg) +
 
152468 (pBlob->nParam-1)*sizeof(RtreeDValue));
152469 if( pBlob->magic!=RTREE_GEOMETRY_MAGIC || nBlob!=nExpected ){
152470 sqlite3_free(pInfo);
152471 return SQLITE_ERROR;
152472 }
152473 pInfo->pContext = pBlob->cb.pContext;
152474 pInfo->nParam = pBlob->nParam;
152475 pInfo->aParam = pBlob->aParam;
 
152476
152477 if( pBlob->cb.xGeom ){
152478 pCons->u.xGeom = pBlob->cb.xGeom;
152479 }else{
152480 pCons->op = RTREE_QUERY;
@@ -152637,21 +152978,34 @@
152637 */
152638 static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
152639 Rtree *pRtree = (Rtree*)tab;
152640 int rc = SQLITE_OK;
152641 int ii;
 
152642 i64 nRow; /* Estimated rows returned by this scan */
152643
152644 int iIdx = 0;
152645 char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
152646 memset(zIdxStr, 0, sizeof(zIdxStr));
 
 
 
 
 
 
 
 
 
 
152647
152648 assert( pIdxInfo->idxStr==0 );
152649 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
152650 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
152651
152652 if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
 
 
152653 /* We have an equality constraint on the rowid. Use strategy 1. */
152654 int jj;
152655 for(jj=0; jj<ii; jj++){
152656 pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
152657 pIdxInfo->aConstraintUsage[jj].omit = 0;
@@ -154339,10 +154693,22 @@
154339 static void rtreeFreeCallback(void *p){
154340 RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
154341 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
154342 sqlite3_free(p);
154343 }
 
 
 
 
 
 
 
 
 
 
 
 
154344
154345 /*
154346 ** Each call to sqlite3_rtree_geometry_callback() or
154347 ** sqlite3_rtree_query_callback() creates an ordinary SQLite
154348 ** scalar function that is implemented by this routine.
@@ -154358,28 +154724,38 @@
154358 */
154359 static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
154360 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
154361 RtreeMatchArg *pBlob;
154362 int nBlob;
 
154363
154364 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue);
 
154365 pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
154366 if( !pBlob ){
154367 sqlite3_result_error_nomem(ctx);
154368 }else{
154369 int i;
154370 pBlob->magic = RTREE_GEOMETRY_MAGIC;
154371 pBlob->cb = pGeomCtx[0];
 
154372 pBlob->nParam = nArg;
154373 for(i=0; i<nArg; i++){
 
 
154374 #ifdef SQLITE_RTREE_INT_ONLY
154375 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
154376 #else
154377 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
154378 #endif
154379 }
154380 sqlite3_result_blob(ctx, pBlob, nBlob, sqlite3_free);
 
 
 
 
 
154381 }
154382 }
154383
154384 /*
154385 ** Register a new geometry function for use with the r-tree MATCH operator.
@@ -155211,10 +155587,4095 @@
155211
155212 #endif /* defined(SQLITE_ENABLE_ICU) */
155213 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
155214
155215 /************** End of fts3_icu.c ********************************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155216 /************** Begin file dbstat.c ******************************************/
155217 /*
155218 ** 2010 July 12
155219 **
155220 ** The author disclaims copyright to this source code. In place of
@@ -155834,11 +160295,11 @@
155834 }
155835
155836 /*
155837 ** Invoke this routine to register the "dbstat" virtual table module
155838 */
155839 SQLITE_API int SQLITE_STDCALL sqlite3_dbstat_register(sqlite3 *db){
155840 static sqlite3_module dbstat_module = {
155841 0, /* iVersion */
155842 statConnect, /* xCreate */
155843 statConnect, /* xConnect */
155844 statBestIndex, /* xBestIndex */
@@ -155859,8 +160320,10 @@
155859 0, /* xFindMethod */
155860 0, /* xRename */
155861 };
155862 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
155863 }
 
 
155864 #endif /* SQLITE_ENABLE_DBSTAT_VTAB */
155865
155866 /************** End of dbstat.c **********************************************/
155867
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.8.11. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -316,13 +316,13 @@
316 **
317 ** See also: [sqlite3_libversion()],
318 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
319 ** [sqlite_version()] and [sqlite_source_id()].
320 */
321 #define SQLITE_VERSION "3.8.11"
322 #define SQLITE_VERSION_NUMBER 3008011
323 #define SQLITE_SOURCE_ID "2015-05-29 17:51:16 db4e9728fae5f7b0fad6aa0a5be317a7c9e7c417"
324
325 /*
326 ** CAPI3REF: Run-Time Library Version Numbers
327 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
328 **
@@ -1161,17 +1161,25 @@
1161 ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
1162 ** opcode causes the xFileControl method to swap the file handle with the one
1163 ** pointed to by the pArg argument. This capability is used during testing
1164 ** and only needs to be supported when SQLITE_TEST is defined.
1165 **
1166 * <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1167 ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
1168 ** be advantageous to block on the next WAL lock if the lock is not immediately
1169 ** available. The WAL subsystem issues this signal during rare
1170 ** circumstances in order to fix a problem with priority inversion.
1171 ** Applications should <em>not</em> use this file-control.
1172 **
1173 ** <li>[[SQLITE_FCNTL_ZIPVFS]]
1174 ** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
1175 ** VFS should return SQLITE_NOTFOUND for this opcode.
1176 **
1177 ** <li>[[SQLITE_FCNTL_OTA]]
1178 ** The [SQLITE_FCNTL_OTA] opcode is implemented by the special VFS used by
1179 ** the OTA extension only. All other VFS should return SQLITE_NOTFOUND for
1180 ** this opcode.
1181 ** </ul>
1182 */
1183 #define SQLITE_FCNTL_LOCKSTATE 1
1184 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
1185 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1193,10 +1201,12 @@
1201 #define SQLITE_FCNTL_HAS_MOVED 20
1202 #define SQLITE_FCNTL_SYNC 21
1203 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
1204 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
1205 #define SQLITE_FCNTL_WAL_BLOCK 24
1206 #define SQLITE_FCNTL_ZIPVFS 25
1207 #define SQLITE_FCNTL_OTA 26
1208
1209 /* deprecated names */
1210 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1211 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1212 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -3595,11 +3605,13 @@
3605 **
3606 ** An sqlite3_value object may be either "protected" or "unprotected".
3607 ** Some interfaces require a protected sqlite3_value. Other interfaces
3608 ** will accept either a protected or an unprotected sqlite3_value.
3609 ** Every interface that accepts sqlite3_value arguments specifies
3610 ** whether or not it requires a protected sqlite3_value. The
3611 ** [sqlite3_value_dup()] interface can be used to construct a new
3612 ** protected sqlite3_value from an unprotected sqlite3_value.
3613 **
3614 ** The terms "protected" and "unprotected" refer to whether or not
3615 ** a mutex is held. An internal mutex is held for a protected
3616 ** sqlite3_value object but no mutex is held for an unprotected
3617 ** sqlite3_value object. If SQLite is compiled to be single-threaded
@@ -4098,12 +4110,10 @@
4110 /*
4111 ** CAPI3REF: Result Values From A Query
4112 ** KEYWORDS: {column access functions}
4113 ** METHOD: sqlite3_stmt
4114 **
 
 
4115 ** ^These routines return information about a single column of the current
4116 ** result row of a query. ^In every case the first argument is a pointer
4117 ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
4118 ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
4119 ** and the second argument is the index of the column for which information
@@ -4159,17 +4169,18 @@
4169 **
4170 ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
4171 ** even empty strings, are always zero-terminated. ^The return
4172 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
4173 **
4174 ** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
4175 ** [unprotected sqlite3_value] object. In a multithreaded environment,
4176 ** an unprotected sqlite3_value object may only be used safely with
4177 ** [sqlite3_bind_value()] and [sqlite3_result_value()].
4178 ** If the [unprotected sqlite3_value] object returned by
4179 ** [sqlite3_column_value()] is used in any other way, including calls
4180 ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
4181 ** or [sqlite3_value_bytes()], the behavior is not threadsafe.
4182 **
4183 ** These routines attempt to convert the value where appropriate. ^For
4184 ** example, if the internal representation is FLOAT and a text result
4185 ** is requested, [sqlite3_snprintf()] is used internally to perform the
4186 ** conversion automatically. ^(The following table details the conversions
@@ -4196,16 +4207,10 @@
4207 ** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
4208 ** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
4209 ** </table>
4210 ** </blockquote>)^
4211 **
 
 
 
 
 
 
4212 ** Note that when type conversions occur, pointers returned by prior
4213 ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
4214 ** sqlite3_column_text16() may be invalidated.
4215 ** Type conversions and pointer invalidations might occur
4216 ** in the following cases:
@@ -4226,11 +4231,11 @@
4231 ** not invalidate a prior pointer, though of course the content of the buffer
4232 ** that the prior pointer references will have been modified. Other kinds
4233 ** of conversion are done in place when it is possible, but sometimes they
4234 ** are not possible and in those cases prior pointers are invalidated.
4235 **
4236 ** The safest policy is to invoke these routines
4237 ** in one of the following ways:
4238 **
4239 ** <ul>
4240 ** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
4241 ** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
@@ -4246,11 +4251,11 @@
4251 ** with calls to sqlite3_column_bytes().
4252 **
4253 ** ^The pointers returned are valid until a type conversion occurs as
4254 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
4255 ** [sqlite3_finalize()] is called. ^The memory space used to hold strings
4256 ** and BLOBs is freed automatically. Do <em>not</em> pass the pointers returned
4257 ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
4258 ** [sqlite3_free()].
4259 **
4260 ** ^(If a memory allocation error occurs during the evaluation of any
4261 ** of these routines, a default value is returned. The default value
@@ -4496,16 +4501,16 @@
4501 SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
4502 void*,sqlite3_int64);
4503 #endif
4504
4505 /*
4506 ** CAPI3REF: Obtaining SQL Values
4507 ** METHOD: sqlite3_value
4508 **
4509 ** The C-language implementation of SQL functions and aggregates uses
4510 ** this set of interface routines to access the parameter values on
4511 ** the function or aggregate.
4512 **
4513 ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
4514 ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
4515 ** define callbacks that implement the SQL functions and aggregates.
4516 ** The 3rd parameter to these callbacks is an array of pointers to
@@ -4554,10 +4559,27 @@
4559 SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value*);
4560 SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value*);
4561 SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value*);
4562 SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*);
4563
4564 /*
4565 ** CAPI3REF: Copy And Free SQL Values
4566 ** METHOD: sqlite3_value
4567 **
4568 ** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
4569 ** object D and returns a pointer to that copy. ^The [sqlite3_value] returned
4570 ** is a [protected sqlite3_value] object even if the input is not.
4571 ** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
4572 ** memory allocation fails.
4573 **
4574 ** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
4575 ** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer
4576 ** then sqlite3_value_free(V) is a harmless no-op.
4577 */
4578 SQLITE_API SQLITE_EXPERIMENTAL sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value*);
4579 SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_value_free(sqlite3_value*);
4580
4581 /*
4582 ** CAPI3REF: Obtain Aggregate Function Context
4583 ** METHOD: sqlite3_context
4584 **
4585 ** Implementations of aggregate SQL functions use this
@@ -4801,11 +4823,11 @@
4823 ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
4824 ** then SQLite makes a copy of the result into space obtained from
4825 ** from [sqlite3_malloc()] before it returns.
4826 **
4827 ** ^The sqlite3_result_value() interface sets the result of
4828 ** the application-defined function to be a copy of the
4829 ** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
4830 ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
4831 ** so that the [sqlite3_value] specified in the parameter may change or
4832 ** be deallocated after sqlite3_result_value() returns without harm.
4833 ** ^A [protected sqlite3_value] object may always be used where an
@@ -6077,11 +6099,11 @@
6099 ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
6100 ** always returns zero.
6101 **
6102 ** ^This function sets the database handle error code and message.
6103 */
6104 SQLITE_API int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
6105
6106 /*
6107 ** CAPI3REF: Close A BLOB Handle
6108 ** DESTRUCTOR: sqlite3_blob
6109 **
@@ -7887,11 +7909,11 @@
7909 ** as if the loop did not exist - it returns non-zero and leave the variable
7910 ** that pOut points to unchanged.
7911 **
7912 ** See also: [sqlite3_stmt_scanstatus_reset()]
7913 */
7914 SQLITE_API int SQLITE_STDCALL sqlite3_stmt_scanstatus(
7915 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
7916 int idx, /* Index of loop to report on */
7917 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
7918 void *pOut /* Result written here */
7919 );
@@ -7903,11 +7925,11 @@
7925 ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
7926 **
7927 ** This API is only available if the library is built with pre-processor
7928 ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
7929 */
7930 SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
7931
7932
7933 /*
7934 ** Undo the hack that converts floating point types to integer for
7935 ** builds on processors without floating point support.
@@ -8018,10 +8040,12 @@
8040 sqlite3_int64 iRowid; /* Rowid for current entry */
8041 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
8042 int eParentWithin; /* Visibility of parent node */
8043 int eWithin; /* OUT: Visiblity */
8044 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
8045 /* The following fields are only available in 3.8.11 and later */
8046 sqlite3_value **apSqlParam; /* Original SQL values of parameters */
8047 };
8048
8049 /*
8050 ** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
8051 */
@@ -11167,10 +11191,11 @@
11191 #define SQLITE_EnableTrigger 0x00800000 /* True to enable triggers */
11192 #define SQLITE_DeferFKs 0x01000000 /* Defer all FK constraints */
11193 #define SQLITE_QueryOnly 0x02000000 /* Disable database changes */
11194 #define SQLITE_VdbeEQP 0x04000000 /* Debug EXPLAIN QUERY PLAN */
11195 #define SQLITE_Vacuum 0x08000000 /* Currently in a VACUUM */
11196 #define SQLITE_CellSizeCk 0x10000000 /* Check btree cell sizes on load */
11197
11198
11199 /*
11200 ** Bits of the sqlite3.dbOptFlags field that are used by the
11201 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
@@ -11548,12 +11573,13 @@
11573 #define TF_Readonly 0x01 /* Read-only system table */
11574 #define TF_Ephemeral 0x02 /* An ephemeral table */
11575 #define TF_HasPrimaryKey 0x04 /* Table has a primary key */
11576 #define TF_Autoincrement 0x08 /* Integer primary key is autoincrement */
11577 #define TF_Virtual 0x10 /* Is a virtual table */
11578 #define TF_WithoutRowid 0x20 /* No rowid. PRIMARY KEY is the key */
11579 #define TF_NoVisibleRowid 0x40 /* No user-visible "rowid" column */
11580 #define TF_OOOHidden 0x80 /* Out-of-Order hidden columns */
11581
11582
11583 /*
11584 ** Test to see whether or not a table is a virtual table. This is
11585 ** done as a macro so that it will be optimized out when virtual
@@ -11567,10 +11593,11 @@
11593 # define IsHiddenColumn(X) 0
11594 #endif
11595
11596 /* Does the table have a rowid */
11597 #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
11598 #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
11599
11600 /*
11601 ** Each foreign key constraint is an instance of the following structure.
11602 **
11603 ** A foreign key is associated with two tables. The "from" table is
@@ -11725,10 +11752,18 @@
11752 ** must be unique and what to do if they are not. When Index.onError=OE_None,
11753 ** it means this is not a unique index. Otherwise it is a unique index
11754 ** and the value of Index.onError indicate the which conflict resolution
11755 ** algorithm to employ whenever an attempt is made to insert a non-unique
11756 ** element.
11757 **
11758 ** While parsing a CREATE TABLE or CREATE INDEX statement in order to
11759 ** generate VDBE code (as opposed to parsing one read from an sqlite_master
11760 ** table as part of parsing an existing database schema), transient instances
11761 ** of this structure may be created. In this case the Index.tnum variable is
11762 ** used to store the address of a VDBE instruction, not a database page
11763 ** number (it cannot - the database page is not allocated until the VDBE
11764 ** program is executed). See convertToWithoutRowidTable() for details.
11765 */
11766 struct Index {
11767 char *zName; /* Name of this index */
11768 i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
11769 LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
@@ -12299,23 +12334,24 @@
12334 /*
12335 ** Allowed values for Select.selFlags. The "SF" prefix stands for
12336 ** "Select Flag".
12337 */
12338 #define SF_Distinct 0x0001 /* Output should be DISTINCT */
12339 #define SF_All 0x0002 /* Includes the ALL keyword */
12340 #define SF_Resolved 0x0004 /* Identifiers have been resolved */
12341 #define SF_Aggregate 0x0008 /* Contains aggregate functions */
12342 #define SF_UsesEphemeral 0x0010 /* Uses the OpenEphemeral opcode */
12343 #define SF_Expanded 0x0020 /* sqlite3SelectExpand() called on this */
12344 #define SF_HasTypeInfo 0x0040 /* FROM subqueries have Table metadata */
12345 #define SF_Compound 0x0080 /* Part of a compound query */
12346 #define SF_Values 0x0100 /* Synthesized from VALUES clause */
12347 #define SF_MultiValue 0x0200 /* Single VALUES term with multiple rows */
12348 #define SF_NestedFrom 0x0400 /* Part of a parenthesized FROM clause */
12349 #define SF_MaybeConvert 0x0800 /* Need convertCompoundSelectToSubquery() */
12350 #define SF_MinMaxAgg 0x1000 /* Aggregate containing min() or max() */
12351 #define SF_Recursive 0x2000 /* The recursive part of a recursive CTE */
12352 #define SF_Converted 0x4000 /* By convertCompoundSelectToSubquery() */
12353
12354
12355 /*
12356 ** The results of a SELECT can be distributed in several ways, as defined
12357 ** by one of the following macros. The "SRT" prefix means "SELECT Result
@@ -12553,11 +12589,10 @@
12589
12590 /* Information used while coding trigger programs. */
12591 Parse *pToplevel; /* Parse structure for main program (or NULL) */
12592 Table *pTriggerTab; /* Table triggers are being coded for */
12593 int addrCrTab; /* Address of OP_CreateTable opcode on CREATE TABLE */
 
12594 u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
12595 u32 oldmask; /* Mask of old.* columns referenced */
12596 u32 newmask; /* Mask of new.* columns referenced */
12597 u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
12598 u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
@@ -13065,11 +13100,10 @@
13100 #define SQLITE_PRINTF_SQLFUNC 0x02
13101 SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
13102 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
13103 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
13104 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
 
13105 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
13106 SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
13107 #endif
13108 #if defined(SQLITE_TEST)
13109 SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
@@ -13084,11 +13118,11 @@
13118 SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
13119 SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
13120 #endif
13121
13122
13123 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
13124 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
13125 SQLITE_PRIVATE int sqlite3Dequote(char*);
13126 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
13127 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
13128 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
@@ -13780,10 +13814,14 @@
13814 */
13815 #if SQLITE_MAX_WORKER_THREADS>0
13816 SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
13817 SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
13818 #endif
13819
13820 #if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
13821 SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*);
13822 #endif
13823
13824 #endif /* _SQLITEINT_H_ */
13825
13826 /************** End of sqliteInt.h *******************************************/
13827 /************** Begin file global.c ******************************************/
@@ -14685,10 +14723,16 @@
14723 Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */
14724 void *pFiller; /* So that sizeof(Mem) is a multiple of 8 */
14725 #endif
14726 };
14727
14728 /*
14729 ** Size of struct Mem not including the Mem.zMalloc member or anything that
14730 ** follows.
14731 */
14732 #define MEMCELLSIZE offsetof(Mem,zMalloc)
14733
14734 /* One or more of the following flags are set to indicate the validOK
14735 ** representations of the value stored in the Mem struct.
14736 **
14737 ** If the MEM_Null flag is set, then the value is an SQL NULL value.
14738 ** No other flags may be set in this case.
@@ -14890,10 +14934,11 @@
14934 #define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */
14935
14936 /*
14937 ** Function prototypes
14938 */
14939 SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
14940 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
14941 void sqliteVdbePopStack(Vdbe*,int);
14942 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
14943 SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
14944 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
@@ -20738,28 +20783,24 @@
20783
20784 /*
20785 ** Return the amount of memory currently checked out.
20786 */
20787 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void){
20788 sqlite3_int64 res, mx;
20789 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
 
 
20790 return res;
20791 }
20792
20793 /*
20794 ** Return the maximum amount of memory that has ever been
20795 ** checked out since either the beginning of this process
20796 ** or since the most recent reset.
20797 */
20798 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag){
20799 sqlite3_int64 res, mx;
20800 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
20801 return mx;
 
 
20802 }
20803
20804 /*
20805 ** Trigger the alarm
20806 */
@@ -21287,23 +21328,15 @@
21328 }
21329 return zNew;
21330 }
21331
21332 /*
21333 ** Free any prior content in *pz and replace it with a copy of zNew.
 
 
21334 */
21335 SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
 
 
 
 
 
 
21336 sqlite3DbFree(db, *pz);
21337 *pz = sqlite3DbStrDup(db, zNew);
21338 }
21339
21340 /*
21341 ** Take actions at the end of an API call to indicate an OOM error
21342 */
@@ -22271,28 +22304,10 @@
22304 z = sqlite3VMPrintf(db, zFormat, ap);
22305 va_end(ap);
22306 return z;
22307 }
22308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22309 /*
22310 ** Print into memory obtained from sqlite3_malloc(). Omit the internal
22311 ** %-conversion extensions.
22312 */
22313 SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char *zFormat, va_list ap){
@@ -36220,10 +36235,16 @@
36235 */
36236 if( pFile->locktype>=locktype ){
36237 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
36238 return SQLITE_OK;
36239 }
36240
36241 /* Do not allow any kind of write-lock on a read-only database
36242 */
36243 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
36244 return SQLITE_IOERR_LOCK;
36245 }
36246
36247 /* Make sure the locking sequence is correct
36248 */
36249 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
36250 assert( locktype!=PENDING_LOCK );
@@ -38617,18 +38638,18 @@
38638 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
38639 if( sizeof(UUID)<=nBuf-n ){
38640 UUID id;
38641 memset(&id, 0, sizeof(UUID));
38642 osUuidCreate(&id);
38643 memcpy(&zBuf[n], &id, sizeof(UUID));
38644 n += sizeof(UUID);
38645 }
38646 if( sizeof(UUID)<=nBuf-n ){
38647 UUID id;
38648 memset(&id, 0, sizeof(UUID));
38649 osUuidCreateSequential(&id);
38650 memcpy(&zBuf[n], &id, sizeof(UUID));
38651 n += sizeof(UUID);
38652 }
38653 #endif
38654 #endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */
38655 return n;
@@ -44777,15 +44798,14 @@
44798 */
44799 assert( pPager->eState==PAGER_OPEN );
44800 assert( pPager->eLock>=SHARED_LOCK );
44801 nPage = sqlite3WalDbsize(pPager->pWal);
44802
44803 /* If the number of pages in the database is not available from the
44804 ** WAL sub-system, determine the page counte based on the size of
44805 ** the database file. If the size of the database file is not an
44806 ** integer multiple of the page-size, round up the result.
 
44807 */
44808 if( nPage==0 ){
44809 i64 n = 0; /* Size of db file in bytes */
44810 assert( isOpen(pPager->fd) || pPager->tempFile );
44811 if( isOpen(pPager->fd) ){
@@ -54245,30 +54265,22 @@
54265 u8 *pAddr; /* The i-th cell pointer */
54266 pAddr = &data[cellOffset + i*2];
54267 pc = get2byte(pAddr);
54268 testcase( pc==iCellFirst );
54269 testcase( pc==iCellLast );
 
54270 /* These conditions have already been verified in btreeInitPage()
54271 ** if PRAGMA cell_size_check=ON.
54272 */
54273 if( pc<iCellFirst || pc>iCellLast ){
54274 return SQLITE_CORRUPT_BKPT;
54275 }
 
54276 assert( pc>=iCellFirst && pc<=iCellLast );
54277 size = cellSizePtr(pPage, &src[pc]);
54278 cbrk -= size;
 
 
 
 
 
54279 if( cbrk<iCellFirst || pc+size>usableSize ){
54280 return SQLITE_CORRUPT_BKPT;
54281 }
 
54282 assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
54283 testcase( cbrk+size==usableSize );
54284 testcase( pc+size==usableSize );
54285 put2byte(pAddr, cbrk);
54286 if( temp==0 ){
@@ -54340,11 +54352,11 @@
54352 }
54353 /* Remove the slot from the free-list. Update the number of
54354 ** fragmented bytes within the page. */
54355 memcpy(&aData[iAddr], &aData[pc], 2);
54356 aData[hdr+7] += (u8)x;
54357 }else if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){
54358 *pRc = SQLITE_CORRUPT_BKPT;
54359 return 0;
54360 }else{
54361 /* The slot remains on the free-list. Reduce its size to account
54362 ** for the portion used by the new allocation. */
@@ -54392,11 +54404,15 @@
54404 ** and the reserved space is zero (the usual value for reserved space)
54405 ** then the cell content offset of an empty page wants to be 65536.
54406 ** However, that integer is too large to be stored in a 2-byte unsigned
54407 ** integer, so a value of 0 is used in its place. */
54408 top = get2byteNotZero(&data[hdr+5]);
54409 if( gap>top || NEVER((u32)top>pPage->pBt->usableSize) ){
54410 /* The NEVER() is because a oversize "top" value will be blocked from
54411 ** reaching this point by btreeInitPage() or btreeGetUnusedPage() */
54412 return SQLITE_CORRUPT_BKPT;
54413 }
54414
54415 /* If there is enough space between gap and top for one more cell pointer
54416 ** array entry offset, and if the freelist is not empty, then search the
54417 ** freelist looking for a free slot big enough to satisfy the request.
54418 */
@@ -54465,11 +54481,11 @@
54481 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
54482 unsigned char *data = pPage->aData; /* Page content */
54483
54484 assert( pPage->pBt!=0 );
54485 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
54486 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
54487 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
54488 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54489 assert( iSize>=4 ); /* Minimum cell size is 4 */
54490 assert( iStart<=iLast );
54491
@@ -54605,10 +54621,11 @@
54621 ** we failed to detect any corruption.
54622 */
54623 static int btreeInitPage(MemPage *pPage){
54624
54625 assert( pPage->pBt!=0 );
54626 assert( pPage->pBt->db!=0 );
54627 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54628 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
54629 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
54630 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
54631
@@ -54663,12 +54680,11 @@
54680 ** past the end of a page boundary and causes SQLITE_CORRUPT to be
54681 ** returned if it does.
54682 */
54683 iCellFirst = cellOffset + 2*pPage->nCell;
54684 iCellLast = usableSize - 4;
54685 if( pBt->db->flags & SQLITE_CellSizeCk ){
 
54686 int i; /* Index into the cell pointer array */
54687 int sz; /* Size of a cell */
54688
54689 if( !pPage->leaf ) iCellLast--;
54690 for(i=0; i<pPage->nCell; i++){
@@ -54684,11 +54700,10 @@
54700 return SQLITE_CORRUPT_BKPT;
54701 }
54702 }
54703 if( !pPage->leaf ) iCellLast++;
54704 }
 
54705
54706 /* Compute the total free space on the page
54707 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
54708 ** start of the first freeblock on the page, or is zero if there are no
54709 ** freeblocks. */
@@ -54781,14 +54796,14 @@
54796 return pPage;
54797 }
54798
54799 /*
54800 ** Get a page from the pager. Initialize the MemPage.pBt and
54801 ** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
54802 **
54803 ** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
54804 ** about the content of the page at this time. So do not go to the disk
54805 ** to fetch the content. Just fill in the content with zeros for now.
54806 ** If in the future we call sqlite3PagerWrite() on this page, that
54807 ** means we have started to be concerned about content and the disk
54808 ** read should occur at that point.
54809 */
@@ -54885,10 +54900,40 @@
54900 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
54901 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54902 sqlite3PagerUnrefNotNull(pPage->pDbPage);
54903 }
54904 }
54905
54906 /*
54907 ** Get an unused page.
54908 **
54909 ** This works just like btreeGetPage() with the addition:
54910 **
54911 ** * If the page is already in use for some other purpose, immediately
54912 ** release it and return an SQLITE_CURRUPT error.
54913 ** * Make sure the isInit flag is clear
54914 */
54915 static int btreeGetUnusedPage(
54916 BtShared *pBt, /* The btree */
54917 Pgno pgno, /* Number of the page to fetch */
54918 MemPage **ppPage, /* Return the page in this parameter */
54919 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
54920 ){
54921 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
54922 if( rc==SQLITE_OK ){
54923 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
54924 releasePage(*ppPage);
54925 *ppPage = 0;
54926 return SQLITE_CORRUPT_BKPT;
54927 }
54928 (*ppPage)->isInit = 0;
54929 }else{
54930 *ppPage = 0;
54931 }
54932 return rc;
54933 }
54934
54935
54936 /*
54937 ** During a rollback, when the pager reloads information into the cache
54938 ** so that the cache is restored to its original state at the start of
54939 ** the transaction, for each page restored this routine is called.
@@ -56133,12 +56178,14 @@
56178 put4byte(pPage->aData, iTo);
56179 }else{
56180 u8 isInitOrig = pPage->isInit;
56181 int i;
56182 int nCell;
56183 int rc;
56184
56185 rc = btreeInitPage(pPage);
56186 if( rc ) return rc;
56187 nCell = pPage->nCell;
56188
56189 for(i=0; i<nCell; i++){
56190 u8 *pCell = findCell(pPage, i);
56191 if( eType==PTRMAP_OVERFLOW1 ){
@@ -56935,13 +56982,17 @@
56982 int wrFlag, /* 1 to write. 0 read-only */
56983 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
56984 BtCursor *pCur /* Write new cursor here */
56985 ){
56986 int rc;
56987 if( iTable<1 ){
56988 rc = SQLITE_CORRUPT_BKPT;
56989 }else{
56990 sqlite3BtreeEnter(p);
56991 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
56992 sqlite3BtreeLeave(p);
56993 }
56994 return rc;
56995 }
56996
56997 /*
56998 ** Return the size of a BtCursor object in bytes.
@@ -57965,11 +58016,11 @@
58016 assert( lwr+upr>=0 );
58017 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
58018 }
58019 }else{
58020 for(;;){
58021 int nCell; /* Size of the pCell cell in bytes */
58022 pCell = findCell(pPage, idx) + pPage->childPtrSize;
58023
58024 /* The maximum supported page-size is 65536 bytes. This means that
58025 ** the maximum number of record bytes stored on an index B-Tree
58026 ** page is less than 16384 bytes and may be stored as a 2-byte
@@ -57994,16 +58045,29 @@
58045 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
58046 }else{
58047 /* The record flows over onto one or more overflow pages. In
58048 ** this case the whole cell needs to be parsed, a buffer allocated
58049 ** and accessPayload() used to retrieve the record into the
58050 ** buffer before VdbeRecordCompare() can be called.
58051 **
58052 ** If the record is corrupt, the xRecordCompare routine may read
58053 ** up to two varints past the end of the buffer. An extra 18
58054 ** bytes of padding is allocated at the end of the buffer in
58055 ** case this happens. */
58056 void *pCellKey;
58057 u8 * const pCellBody = pCell - pPage->childPtrSize;
58058 btreeParseCellPtr(pPage, pCellBody, &pCur->info);
58059 nCell = (int)pCur->info.nKey;
58060 testcase( nCell<0 ); /* True if key size is 2^32 or more */
58061 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
58062 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
58063 testcase( nCell==2 ); /* Minimum legal index key size */
58064 if( nCell<2 ){
58065 rc = SQLITE_CORRUPT_BKPT;
58066 goto moveto_finish;
58067 }
58068 pCellKey = sqlite3Malloc( nCell+18 );
58069 if( pCellKey==0 ){
58070 rc = SQLITE_NOMEM;
58071 goto moveto_finish;
58072 }
58073 pCur->aiIdx[pCur->iPage] = (u16)idx;
@@ -58387,11 +58451,11 @@
58451 }
58452 testcase( iTrunk==mxPage );
58453 if( iTrunk>mxPage ){
58454 rc = SQLITE_CORRUPT_BKPT;
58455 }else{
58456 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
58457 }
58458 if( rc ){
58459 pTrunk = 0;
58460 goto end_allocate_page;
58461 }
@@ -58452,11 +58516,11 @@
58516 if( iNewTrunk>mxPage ){
58517 rc = SQLITE_CORRUPT_BKPT;
58518 goto end_allocate_page;
58519 }
58520 testcase( iNewTrunk==mxPage );
58521 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
58522 if( rc!=SQLITE_OK ){
58523 goto end_allocate_page;
58524 }
58525 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
58526 if( rc!=SQLITE_OK ){
@@ -58532,11 +58596,11 @@
58596 if( closest<k-1 ){
58597 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
58598 }
58599 put4byte(&aData[4], k-1);
58600 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
58601 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
58602 if( rc==SQLITE_OK ){
58603 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
58604 if( rc!=SQLITE_OK ){
58605 releasePage(*ppPage);
58606 }
@@ -58580,11 +58644,11 @@
58644 ** becomes a new pointer-map page, the second is used by the caller.
58645 */
58646 MemPage *pPg = 0;
58647 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
58648 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
58649 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
58650 if( rc==SQLITE_OK ){
58651 rc = sqlite3PagerWrite(pPg->pDbPage);
58652 releasePage(pPg);
58653 }
58654 if( rc ) return rc;
@@ -58594,35 +58658,27 @@
58658 #endif
58659 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
58660 *pPgno = pBt->nPage;
58661
58662 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
58663 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
58664 if( rc ) return rc;
58665 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
58666 if( rc!=SQLITE_OK ){
58667 releasePage(*ppPage);
58668 *ppPage = 0;
58669 }
58670 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
58671 }
58672
58673 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
58674
58675 end_allocate_page:
58676 releasePage(pTrunk);
58677 releasePage(pPrevTrunk);
58678 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
58679 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
 
 
 
 
 
 
 
 
 
58680 return rc;
58681 }
58682
58683 /*
58684 ** This function is used to add page iPage to the database file free-list.
@@ -58643,13 +58699,14 @@
58699 MemPage *pPage; /* Page being freed. May be NULL. */
58700 int rc; /* Return Code */
58701 int nFree; /* Initial number of pages on free-list */
58702
58703 assert( sqlite3_mutex_held(pBt->mutex) );
58704 assert( CORRUPT_DB || iPage>1 );
58705 assert( !pMemPage || pMemPage->pgno==iPage );
58706
58707 if( iPage<2 ) return SQLITE_CORRUPT_BKPT;
58708 if( pMemPage ){
58709 pPage = pMemPage;
58710 sqlite3PagerRef(pPage->pDbPage);
58711 }else{
58712 pPage = btreePageLookup(pBt, iPage);
@@ -58797,11 +58854,13 @@
58854 }
58855 ovflPgno = get4byte(&pCell[info.iOverflow]);
58856 assert( pBt->usableSize > 4 );
58857 ovflPageSize = pBt->usableSize - 4;
58858 nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
58859 assert( nOvfl>0 ||
58860 (CORRUPT_DB && (info.nPayload + ovflPageSize)<ovflPageSize)
58861 );
58862 while( nOvfl-- ){
58863 Pgno iNext = 0;
58864 MemPage *pOvfl = 0;
58865 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
58866 /* 0 is not a legal page number and page 1 cannot be an
@@ -59052,11 +59111,11 @@
59111 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
59112
59113 if( *pRC ) return;
59114
59115 assert( idx>=0 && idx<pPage->nCell );
59116 assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
59117 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
59118 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
59119 data = pPage->aData;
59120 ptr = &pPage->aCellIdx[2*idx];
59121 pc = get2byte(ptr);
@@ -59216,11 +59275,12 @@
59275 }
59276 pData -= szCell[i];
59277 memcpy(pData, pCell, szCell[i]);
59278 put2byte(pCellptr, (pData - aData));
59279 pCellptr += 2;
59280 assert( szCell[i]==cellSizePtr(pPg, pCell) || CORRUPT_DB );
59281 testcase( szCell[i]==cellSizePtr(pPg,pCell) );
59282 }
59283
59284 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
59285 pPg->nCell = nCell;
59286 pPg->nOverflow = 0;
@@ -59893,10 +59953,18 @@
59953 leafCorrection = apOld[0]->leaf*4;
59954 leafData = apOld[0]->intKeyLeaf;
59955 for(i=0; i<nOld; i++){
59956 int limit;
59957 MemPage *pOld = apOld[i];
59958
59959 /* Verify that all sibling pages are of the same "type" (table-leaf,
59960 ** table-interior, index-leaf, or index-interior).
59961 */
59962 if( pOld->aData[0]!=apOld[0]->aData[0] ){
59963 rc = SQLITE_CORRUPT_BKPT;
59964 goto balance_cleanup;
59965 }
59966
59967 limit = pOld->nCell+pOld->nOverflow;
59968 if( pOld->nOverflow>0 ){
59969 for(j=0; j<limit; j++){
59970 assert( nCell<nMaxCells );
@@ -59935,17 +60003,17 @@
60003 /* The right pointer of the child page pOld becomes the left
60004 ** pointer of the divider cell */
60005 memcpy(apCell[nCell], &pOld->aData[8], 4);
60006 }else{
60007 assert( leafCorrection==4 );
60008 while( szCell[nCell]<4 ){
60009 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
60010 ** does exist, pad it with 0x00 bytes. */
60011 assert( szCell[nCell]==3 || CORRUPT_DB );
60012 assert( apCell[nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
60013 aSpace1[iSpace1++] = 0x00;
60014 szCell[nCell]++;
60015 }
60016 }
60017 nCell++;
60018 }
60019 }
@@ -60032,14 +60100,10 @@
60100 ));
60101
60102 /*
60103 ** Allocate k new pages. Reuse old pages where possible.
60104 */
 
 
 
 
60105 pageFlags = apOld[0]->aData[0];
60106 for(i=0; i<k; i++){
60107 MemPage *pNew;
60108 if( i<nOld ){
60109 pNew = apNew[i] = apOld[i];
@@ -60817,10 +60881,11 @@
60881 int nCell;
60882 Pgno n = pCur->apPage[iCellDepth+1]->pgno;
60883 unsigned char *pTmp;
60884
60885 pCell = findCell(pLeaf, pLeaf->nCell-1);
60886 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
60887 nCell = cellSizePtr(pLeaf, pCell);
60888 assert( MX_CELL_SIZE(pBt) >= nCell );
60889 pTmp = pBt->pTmpSpace;
60890 assert( pTmp!=0 );
60891 rc = sqlite3PagerWrite(pLeaf->pDbPage);
@@ -60909,11 +60974,12 @@
60974 */
60975 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
60976 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
60977 pgnoRoot++;
60978 }
60979 assert( pgnoRoot>=3 || CORRUPT_DB );
60980 testcase( pgnoRoot<3 );
60981
60982 /* Allocate a page. The page that currently resides at pgnoRoot will
60983 ** be moved to the allocated page (unless the allocated page happens
60984 ** to reside at pgnoRoot).
60985 */
@@ -61059,11 +61125,12 @@
61125 }
61126 if( !pPage->leaf ){
61127 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
61128 if( rc ) goto cleardatabasepage_out;
61129 }else if( pnChange ){
61130 assert( pPage->intKey || CORRUPT_DB );
61131 testcase( !pPage->intKey );
61132 *pnChange += pPage->nCell;
61133 }
61134 if( freePageFlag ){
61135 freePage(pPage, &rc);
61136 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
@@ -63854,14 +63921,10 @@
63921 }
63922 pMem->pScopyFrom = 0;
63923 }
63924 #endif /* SQLITE_DEBUG */
63925
 
 
 
 
63926
63927 /*
63928 ** Make an shallow copy of pFrom into pTo. Prior contents of
63929 ** pTo are freed. The pFrom->z field is not duplicated. If
63930 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
@@ -63884,11 +63947,14 @@
63947 ** freed before the copy is made.
63948 */
63949 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
63950 int rc = SQLITE_OK;
63951
63952 /* The pFrom==0 case in the following assert() is when an sqlite3_value
63953 ** from sqlite3_value_dup() is used as the argument
63954 ** to sqlite3_result_value(). */
63955 assert( pTo->db==pFrom->db || pFrom->db==0 );
63956 assert( (pFrom->flags & MEM_RowSet)==0 );
63957 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
63958 memcpy(pTo, pFrom, MEMCELLSIZE);
63959 pTo->flags &= ~MEM_Dyn;
63960 if( pTo->flags&(MEM_Str|MEM_Blob) ){
@@ -64817,10 +64883,21 @@
64883 assert( pParse->aLabel==0 );
64884 assert( pParse->nLabel==0 );
64885 assert( pParse->nOpAlloc==0 );
64886 return p;
64887 }
64888
64889 /*
64890 ** Change the error string stored in Vdbe.zErrMsg
64891 */
64892 SQLITE_PRIVATE void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
64893 va_list ap;
64894 sqlite3DbFree(p->db, p->zErrMsg);
64895 va_start(ap, zFormat);
64896 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
64897 va_end(ap);
64898 }
64899
64900 /*
64901 ** Remember the SQL string for a prepared statement.
64902 */
64903 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
@@ -66174,11 +66251,11 @@
66251 p->rc = SQLITE_OK;
66252 rc = SQLITE_DONE;
66253 }else if( db->u1.isInterrupted ){
66254 p->rc = SQLITE_INTERRUPT;
66255 rc = SQLITE_ERROR;
66256 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
66257 }else{
66258 char *zP4;
66259 Op *pOp;
66260 if( i<p->nOp ){
66261 /* The output line number is small enough that we are still in the
@@ -67077,11 +67154,11 @@
67154 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
67155 || (!deferred && p->nFkConstraint>0)
67156 ){
67157 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
67158 p->errorAction = OE_Abort;
67159 sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
67160 return SQLITE_ERROR;
67161 }
67162 return SQLITE_OK;
67163 }
67164 #endif
@@ -68420,11 +68497,11 @@
68497
68498 /* RHS is an integer */
68499 if( pRhs->flags & MEM_Int ){
68500 serial_type = aKey1[idx1];
68501 testcase( serial_type==12 );
68502 if( serial_type>=10 ){
68503 rc = +1;
68504 }else if( serial_type==0 ){
68505 rc = -1;
68506 }else if( serial_type==7 ){
68507 double rhs = (double)pRhs->u.i;
@@ -68446,11 +68523,15 @@
68523 }
68524
68525 /* RHS is real */
68526 else if( pRhs->flags & MEM_Real ){
68527 serial_type = aKey1[idx1];
68528 if( serial_type>=10 ){
68529 /* Serial types 12 or greater are strings and blobs (greater than
68530 ** numbers). Types 10 and 11 are currently "reserved for future
68531 ** use", so it doesn't really matter what the results of comparing
68532 ** them to numberic values are. */
68533 rc = +1;
68534 }else if( serial_type==0 ){
68535 rc = -1;
68536 }else{
68537 double rhs = pRhs->u.r;
@@ -69184,10 +69265,40 @@
69265 SQLITE_INTEGER, /* 0x1e */
69266 SQLITE_NULL, /* 0x1f */
69267 };
69268 return aType[pVal->flags&MEM_AffMask];
69269 }
69270
69271 /* Make a copy of an sqlite3_value object
69272 */
69273 SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value *pOrig){
69274 sqlite3_value *pNew;
69275 if( pOrig==0 ) return 0;
69276 pNew = sqlite3_malloc( sizeof(*pNew) );
69277 if( pNew==0 ) return 0;
69278 memset(pNew, 0, sizeof(*pNew));
69279 memcpy(pNew, pOrig, MEMCELLSIZE);
69280 pNew->flags &= ~MEM_Dyn;
69281 pNew->db = 0;
69282 if( pNew->flags&(MEM_Str|MEM_Blob) ){
69283 pNew->flags &= ~(MEM_Static|MEM_Dyn);
69284 pNew->flags |= MEM_Ephem;
69285 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
69286 sqlite3ValueFree(pNew);
69287 pNew = 0;
69288 }
69289 }
69290 return pNew;
69291 }
69292
69293 /* Destroy an sqlite3_value object previously obtained from
69294 ** sqlite3_value_dup().
69295 */
69296 SQLITE_API void SQLITE_STDCALL sqlite3_value_free(sqlite3_value *pOld){
69297 sqlite3ValueFree(pOld);
69298 }
69299
69300
69301 /**************************** sqlite3_result_ *******************************
69302 ** The following routines are used by user-defined functions to specify
69303 ** the function result.
69304 **
@@ -71798,16 +71909,15 @@
71909 zType = 0;
71910 }
71911 assert( zType!=0 || pOp->p4.z!=0 );
71912 zLogFmt = "abort at %d in [%s]: %s";
71913 if( zType && pOp->p4.z ){
71914 sqlite3VdbeError(p, "%s constraint failed: %s", zType, pOp->p4.z);
 
71915 }else if( pOp->p4.z ){
71916 sqlite3VdbeError(p, "%s", pOp->p4.z);
71917 }else{
71918 sqlite3VdbeError(p, "%s constraint failed", zType);
71919 }
71920 sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg);
71921 }
71922 rc = sqlite3VdbeHalt(p);
71923 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
@@ -72435,11 +72545,11 @@
72545 lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */
72546
72547 /* If the function returned an error, throw an exception */
72548 if( ctx.fErrorOrAux ){
72549 if( ctx.isError ){
72550 sqlite3VdbeError(p, "%s", sqlite3_value_text(ctx.pOut));
72551 rc = ctx.isError;
72552 }
72553 sqlite3VdbeDeleteAuxData(p, (int)(pOp - aOp), pOp->p1);
72554 }
72555
@@ -73622,12 +73732,11 @@
73732 if( p1==SAVEPOINT_BEGIN ){
73733 if( db->nVdbeWrite>0 ){
73734 /* A new savepoint cannot be created if there are active write
73735 ** statements (i.e. open read/write incremental blob handles).
73736 */
73737 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
 
73738 rc = SQLITE_BUSY;
73739 }else{
73740 nName = sqlite3Strlen30(zName);
73741
73742 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -73674,19 +73783,18 @@
73783 pSavepoint = pSavepoint->pNext
73784 ){
73785 iSavepoint++;
73786 }
73787 if( !pSavepoint ){
73788 sqlite3VdbeError(p, "no such savepoint: %s", zName);
73789 rc = SQLITE_ERROR;
73790 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
73791 /* It is not possible to release (commit) a savepoint if there are
73792 ** active write statements.
73793 */
73794 sqlite3VdbeError(p, "cannot release savepoint - "
73795 "SQL statements in progress");
 
73796 rc = SQLITE_BUSY;
73797 }else{
73798
73799 /* Determine whether or not this is a transaction savepoint. If so,
73800 ** and this is a RELEASE command, then the current transaction
@@ -73788,27 +73896,16 @@
73896 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
73897 assert( desiredAutoCommit==1 || iRollback==0 );
73898 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
73899 assert( p->bIsReader );
73900
 
 
 
 
 
 
 
 
 
 
 
73901 if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
73902 /* If this instruction implements a COMMIT and other VMs are writing
73903 ** return an error indicating that the other VMs must complete first.
73904 */
73905 sqlite3VdbeError(p, "cannot commit transaction - "
73906 "SQL statements in progress");
73907 rc = SQLITE_BUSY;
73908 }else if( desiredAutoCommit!=db->autoCommit ){
73909 if( iRollback ){
73910 assert( desiredAutoCommit==1 );
73911 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
@@ -73831,11 +73928,11 @@
73928 }else{
73929 rc = SQLITE_ERROR;
73930 }
73931 goto vdbe_return;
73932 }else{
73933 sqlite3VdbeError(p,
73934 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
73935 (iRollback)?"cannot rollback - no transaction is active":
73936 "cannot commit - no transaction is active"));
73937
73938 rc = SQLITE_ERROR;
@@ -76264,11 +76361,11 @@
76361 if( pFrame ) break;
76362 }
76363
76364 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
76365 rc = SQLITE_ERROR;
76366 sqlite3VdbeError(p, "too many levels of trigger recursion");
76367 break;
76368 }
76369
76370 /* Register pRt is used to store the memory required to save the state
76371 ** of the current program, and the memory required at runtime to execute
@@ -76567,11 +76664,11 @@
76664 ctx.pVdbe = p;
76665 ctx.iOp = (int)(pOp - aOp);
76666 ctx.skipFlag = 0;
76667 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
76668 if( ctx.isError ){
76669 sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
76670 rc = ctx.isError;
76671 }
76672 if( ctx.skipFlag ){
76673 assert( pOp[-1].opcode==OP_CollSeq );
76674 i = pOp[-1].p1;
@@ -76599,11 +76696,11 @@
76696 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
76697 pMem = &aMem[pOp->p1];
76698 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
76699 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
76700 if( rc ){
76701 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
76702 }
76703 sqlite3VdbeChangeEncoding(pMem, encoding);
76704 UPDATE_MAX_BLOBSIZE(pMem);
76705 if( sqlite3VdbeMemTooBig(pMem) ){
76706 goto too_big;
@@ -76704,11 +76801,11 @@
76801 if( (eNew!=eOld)
76802 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
76803 ){
76804 if( !db->autoCommit || db->nVdbeRead>1 ){
76805 rc = SQLITE_ERROR;
76806 sqlite3VdbeError(p,
76807 "cannot change %s wal mode from within a transaction",
76808 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
76809 );
76810 break;
76811 }else{
@@ -76835,11 +76932,11 @@
76932 assert( DbMaskTest(p->btreeMask, p1) );
76933 assert( isWriteLock==0 || isWriteLock==1 );
76934 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
76935 if( (rc&0xFF)==SQLITE_LOCKED ){
76936 const char *z = pOp->p4.z;
76937 sqlite3VdbeError(p, "database table is locked: %s", z);
76938 }
76939 }
76940 break;
76941 }
76942 #endif /* SQLITE_OMIT_SHARED_CACHE */
@@ -77383,19 +77480,19 @@
77480
77481 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
77482 ** is encountered.
77483 */
77484 too_big:
77485 sqlite3VdbeError(p, "string or blob too big");
77486 rc = SQLITE_TOOBIG;
77487 goto vdbe_error_halt;
77488
77489 /* Jump to here if a malloc() fails.
77490 */
77491 no_mem:
77492 db->mallocFailed = 1;
77493 sqlite3VdbeError(p, "out of memory");
77494 rc = SQLITE_NOMEM;
77495 goto vdbe_error_halt;
77496
77497 /* Jump to here for any other kind of fatal error. The "rc" variable
77498 ** should hold the error number.
@@ -77402,11 +77499,11 @@
77499 */
77500 abort_due_to_error:
77501 assert( p->zErrMsg==0 );
77502 if( db->mallocFailed ) rc = SQLITE_NOMEM;
77503 if( rc!=SQLITE_IOERR_NOMEM ){
77504 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
77505 }
77506 goto vdbe_error_halt;
77507
77508 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
77509 ** flag.
@@ -77413,11 +77510,11 @@
77510 */
77511 abort_due_to_interrupt:
77512 assert( db->u1.isInterrupted );
77513 rc = SQLITE_INTERRUPT;
77514 p->rc = rc;
77515 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
77516 goto vdbe_error_halt;
77517 }
77518
77519
77520 /************** End of vdbe.c ************************************************/
@@ -81652,11 +81749,11 @@
81749 iCol = -1;
81750 }
81751 break;
81752 }
81753 }
81754 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
81755 /* IMP: R-51414-32910 */
81756 /* IMP: R-44911-55124 */
81757 iCol = -1;
81758 }
81759 if( iCol<pTab->nCol ){
@@ -81682,11 +81779,11 @@
81779
81780 /*
81781 ** Perhaps the name is a reference to the ROWID
81782 */
81783 if( cnt==0 && cntTab==1 && pMatch && sqlite3IsRowid(zCol)
81784 && VisibleRowid(pMatch->pTab) ){
81785 cnt = 1;
81786 pExpr->iColumn = -1; /* IMP: R-44911-55124 */
81787 pExpr->affinity = SQLITE_AFF_INTEGER;
81788 }
81789
@@ -92126,18 +92223,15 @@
92223 #ifndef SQLITE_OMIT_AUTOINCREMENT
92224 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
92225 "INTEGER PRIMARY KEY");
92226 #endif
92227 }else{
 
92228 Index *p;
 
92229 p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
92230 0, sortOrder, 0);
92231 if( p ){
92232 p->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
 
92233 }
92234 pList = 0;
92235 }
92236
92237 primary_key_exit:
@@ -92486,18 +92580,10 @@
92580 if( pParse->addrCrTab ){
92581 assert( v );
92582 sqlite3VdbeGetOp(v, pParse->addrCrTab)->opcode = OP_CreateIndex;
92583 }
92584
 
 
 
 
 
 
 
 
92585 /* Locate the PRIMARY KEY index. Or, if this table was originally
92586 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
92587 */
92588 if( pTab->iPKey>=0 ){
92589 ExprList *pList;
@@ -92511,10 +92597,20 @@
92597 if( pPk==0 ) return;
92598 pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
92599 pTab->iPKey = -1;
92600 }else{
92601 pPk = sqlite3PrimaryKeyIndex(pTab);
92602
92603 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
92604 ** table entry. This is only required if currently generating VDBE
92605 ** code for a CREATE TABLE (not when parsing one as part of reading
92606 ** a database schema). */
92607 if( v ){
92608 assert( db->init.busy==0 );
92609 sqlite3VdbeGetOp(v, pPk->tnum)->opcode = OP_Goto;
92610 }
92611
92612 /*
92613 ** Remove all redundant columns from the PRIMARY KEY. For example, change
92614 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
92615 ** code assumes the PRIMARY KEY contains no repeated columns.
92616 */
@@ -92646,11 +92742,11 @@
92742 return;
92743 }
92744 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
92745 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
92746 }else{
92747 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
92748 convertToWithoutRowidTable(pParse, p);
92749 }
92750 }
92751
92752 iDb = sqlite3SchemaToIndex(db, p->pSchema);
@@ -92714,30 +92810,49 @@
92810 ** as a schema-lock must have already been obtained to create it. Since
92811 ** a schema-lock excludes all other database users, the write-lock would
92812 ** be redundant.
92813 */
92814 if( pSelect ){
92815 SelectDest dest; /* Where the SELECT should store results */
92816 int regYield; /* Register holding co-routine entry-point */
92817 int addrTop; /* Top of the co-routine */
92818 int regRec; /* A record to be insert into the new table */
92819 int regRowid; /* Rowid of the next row to insert */
92820 int addrInsLoop; /* Top of the loop for inserting rows */
92821 Table *pSelTab; /* A table that describes the SELECT results */
92822
92823 regYield = ++pParse->nMem;
92824 regRec = ++pParse->nMem;
92825 regRowid = ++pParse->nMem;
92826 assert(pParse->nTab==1);
92827 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
92828 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
92829 pParse->nTab = 2;
92830 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
92831 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
92832 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
92833 sqlite3Select(pParse, pSelect, &dest);
92834 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
92835 sqlite3VdbeJumpHere(v, addrTop - 1);
92836 if( pParse->nErr ) return;
92837 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
92838 if( pSelTab==0 ) return;
92839 assert( p->aCol==0 );
92840 p->nCol = pSelTab->nCol;
92841 p->aCol = pSelTab->aCol;
92842 pSelTab->nCol = 0;
92843 pSelTab->aCol = 0;
92844 sqlite3DeleteTable(db, pSelTab);
92845 addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
92846 VdbeCoverage(v);
92847 sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
92848 sqlite3TableAffinity(v, p, 0);
92849 sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
92850 sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
92851 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrInsLoop);
92852 sqlite3VdbeJumpHere(v, addrInsLoop);
92853 sqlite3VdbeAddOp1(v, OP_Close, 1);
 
 
 
 
 
 
 
 
 
 
92854 }
92855
92856 /* Compute the complete text of the CREATE statement */
92857 if( pSelect ){
92858 zStmt = createTableStmt(db, p);
@@ -94032,14 +94147,19 @@
94147 int iMem = ++pParse->nMem;
94148
94149 v = sqlite3GetVdbe(pParse);
94150 if( v==0 ) goto exit_create_index;
94151
 
 
 
94152 sqlite3BeginWriteOperation(pParse, 1, iDb);
94153
94154 /* Create the rootpage for the index using CreateIndex. But before
94155 ** doing so, code a Noop instruction and store its address in
94156 ** Index.tnum. This is required in case this index is actually a
94157 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
94158 ** that case the convertToWithoutRowidTable() routine will replace
94159 ** the Noop with a Goto to jump over the VDBE code generated below. */
94160 pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
94161 sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
94162
94163 /* Gather the complete text of the CREATE INDEX statement into
94164 ** the zStmt variable
94165 */
@@ -94075,10 +94195,12 @@
94195 sqlite3ChangeCookie(pParse, iDb);
94196 sqlite3VdbeAddParseSchemaOp(v, iDb,
94197 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
94198 sqlite3VdbeAddOp1(v, OP_Expire, 0);
94199 }
94200
94201 sqlite3VdbeJumpHere(v, pIndex->tnum);
94202 }
94203
94204 /* When adding an index to the list of indices for a table, make
94205 ** sure all indices labeled OE_Replace come after all those labeled
94206 ** OE_Ignore. This is necessary for the correct constraint check
@@ -99670,11 +99792,11 @@
99792 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
99793 VdbeComment((v, "%s", pTab->zName));
99794 }else{
99795 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
99796 assert( pPk!=0 );
99797 assert( pPk->tnum==pTab->tnum );
99798 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
99799 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
99800 VdbeComment((v, "%s", pTab->zName));
99801 }
99802 }
@@ -102120,10 +102242,12 @@
102242 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
102243 void(*)(void*));
102244 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
102245 void(*)(void*), unsigned char);
102246 int (*strglob)(const char*,const char*);
102247 sqlite3_value (*value_dup)(const sqlite3_value*);
102248 void (*value_free)(sqlite3_value*);
102249 };
102250
102251 /*
102252 ** The following macros redefine the API routines so that they are
102253 ** redirected through the global sqlite3_api structure.
@@ -102350,10 +102474,13 @@
102474 #define sqlite3_realloc64 sqlite3_api->realloc64
102475 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
102476 #define sqlite3_result_blob64 sqlite3_api->result_blob64
102477 #define sqlite3_result_text64 sqlite3_api->result_text64
102478 #define sqlite3_strglob sqlite3_api->strglob
102479 /* Version 3.8.11 and later */
102480 #define sqlite3_value_dup sqlite3_api->value_dup
102481 #define sqlite3_value_free sqlite3_api->value_free
102482 #endif /* SQLITE_CORE */
102483
102484 #ifndef SQLITE_CORE
102485 /* This case when the file really is being compiled as a loadable
102486 ** extension */
@@ -103256,10 +103383,14 @@
103383 #endif
103384 { /* zName: */ "case_sensitive_like",
103385 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE,
103386 /* ePragFlag: */ 0,
103387 /* iArg: */ 0 },
103388 { /* zName: */ "cell_size_check",
103389 /* ePragTyp: */ PragTyp_FLAG,
103390 /* ePragFlag: */ 0,
103391 /* iArg: */ SQLITE_CellSizeCk },
103392 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
103393 { /* zName: */ "checkpoint_fullfsync",
103394 /* ePragTyp: */ PragTyp_FLAG,
103395 /* ePragFlag: */ 0,
103396 /* iArg: */ SQLITE_CkptFullFSync },
@@ -103613,11 +103744,11 @@
103744 /* ePragTyp: */ PragTyp_FLAG,
103745 /* ePragFlag: */ 0,
103746 /* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
103747 #endif
103748 };
103749 /* Number of pragmas: 60 on by default, 73 total. */
103750
103751 /************** End of pragma.h **********************************************/
103752 /************** Continuing where we left off in pragma.c *********************/
103753
103754 /*
@@ -105596,17 +105727,17 @@
105727 const char *zObj, /* Object being parsed at the point of error */
105728 const char *zExtra /* Error information */
105729 ){
105730 sqlite3 *db = pData->db;
105731 if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
105732 char *z;
105733 if( zObj==0 ) zObj = "?";
105734 z = sqlite3_mprintf("malformed database schema (%s)", zObj);
105735 if( z && zExtra ) z = sqlite3_mprintf("%z - %s", z, zExtra);
105736 sqlite3DbFree(db, *pData->pzErrMsg);
105737 *pData->pzErrMsg = z;
105738 if( z==0 ) db->mallocFailed = 1;
 
105739 }
105740 pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
105741 }
105742
105743 /*
@@ -105794,11 +105925,11 @@
105925 ** will be closed before this function returns. */
105926 sqlite3BtreeEnter(pDb->pBt);
105927 if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
105928 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
105929 if( rc!=SQLITE_OK ){
105930 sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
105931 goto initone_error_out;
105932 }
105933 openedTransaction = 1;
105934 }
105935
@@ -107178,12 +107309,17 @@
107309 }
107310 }else if( eDest!=SRT_Exists ){
107311 /* If the destination is an EXISTS(...) expression, the actual
107312 ** values returned by the SELECT are not required.
107313 */
107314 u8 ecelFlags;
107315 if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
107316 ecelFlags = SQLITE_ECEL_DUP;
107317 }else{
107318 ecelFlags = 0;
107319 }
107320 sqlite3ExprCodeExprList(pParse, pEList, regResult, ecelFlags);
107321 }
107322
107323 /* If the DISTINCT keyword was present on the SELECT statement
107324 ** and this row has been seen before, then do not make this row
107325 ** part of the result.
@@ -107276,10 +107412,12 @@
107412 case SRT_Table:
107413 case SRT_EphemTab: {
107414 int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
107415 testcase( eDest==SRT_Table );
107416 testcase( eDest==SRT_EphemTab );
107417 testcase( eDest==SRT_Fifo );
107418 testcase( eDest==SRT_DistFifo );
107419 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
107420 #ifndef SQLITE_OMIT_CTE
107421 if( eDest==SRT_DistFifo ){
107422 /* If the destination is DistFifo, then cursor (iParm+1) is open
107423 ** on an ephemeral index. If the current row is already present
@@ -107691,14 +107829,11 @@
107829 for(i=0; i<nSortData; i++){
107830 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq+i, regRow+i);
107831 VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
107832 }
107833 switch( eDest ){
 
107834 case SRT_EphemTab: {
 
 
107835 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
107836 sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
107837 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
107838 break;
107839 }
@@ -109043,19 +109178,18 @@
109178
109179 /* Suppress the first OFFSET entries if there is an OFFSET clause
109180 */
109181 codeOffset(v, p->iOffset, iContinue);
109182
109183 assert( pDest->eDest!=SRT_Exists );
109184 assert( pDest->eDest!=SRT_Table );
109185 switch( pDest->eDest ){
109186 /* Store the result as data using a unique key.
109187 */
 
109188 case SRT_EphemTab: {
109189 int r1 = sqlite3GetTempReg(pParse);
109190 int r2 = sqlite3GetTempReg(pParse);
 
 
109191 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
109192 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
109193 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
109194 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
109195 sqlite3ReleaseTempReg(pParse, r2);
@@ -109079,20 +109213,10 @@
109213 sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
109214 sqlite3ReleaseTempReg(pParse, r1);
109215 break;
109216 }
109217
 
 
 
 
 
 
 
 
 
 
109218 /* If this is a scalar select that is part of an expression, then
109219 ** store the results in the appropriate memory cell and break out
109220 ** of the scan loop.
109221 */
109222 case SRT_Mem: {
@@ -110463,11 +110587,11 @@
110587 if( pTab==0 ) return WRC_Abort;
110588 pTab->nRef = 1;
110589 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
110590 pTab->iPKey = -1;
110591 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
110592 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
110593 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
110594 if( db->mallocFailed ) return SQLITE_NOMEM;
110595 assert( pFrom->pSelect );
110596
110597 /* Check if this is a recursive CTE. */
@@ -110708,17 +110832,10 @@
110832 ExprList *pNew = 0;
110833 int flags = pParse->db->flags;
110834 int longNames = (flags & SQLITE_FullColNames)!=0
110835 && (flags & SQLITE_ShortColNames)==0;
110836
 
 
 
 
 
 
 
110837 for(k=0; k<pEList->nExpr; k++){
110838 pE = a[k].pExpr;
110839 pRight = pE->pRight;
110840 assert( pE->op!=TK_DOT || pRight!=0 );
110841 if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pRight->op!=TK_ALL) ){
@@ -111296,10 +111413,11 @@
111413 isAgg = 1;
111414 p->selFlags |= SF_Aggregate;
111415 }
111416 i = -1;
111417 }else if( pTabList->nSrc==1
111418 && (p->selFlags & SF_All)==0
111419 && OptimizationEnabled(db, SQLITE_SubqCoroutine)
111420 ){
111421 /* Implement a co-routine that will return a single row of the result
111422 ** set on each invocation.
111423 */
@@ -111983,13 +112101,13 @@
112101 ** Generate a human-readable description of a the Select object.
112102 */
112103 SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
112104 int n = 0;
112105 pView = sqlite3TreeViewPush(pView, moreToFollow);
112106 sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
112107 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
112108 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
112109 );
112110 if( p->pSrc && p->pSrc->nSrc ) n++;
112111 if( p->pWhere ) n++;
112112 if( p->pGroupBy ) n++;
112113 if( p->pHaving ) n++;
@@ -114139,16 +114257,14 @@
114257 /* Create the ephemeral table into which the update results will
114258 ** be stored.
114259 */
114260 assert( v );
114261 ephemTab = pParse->nTab++;
 
 
114262
114263 /* fill the ephemeral table
114264 */
114265 sqlite3SelectDestInit(&dest, SRT_EphemTab, ephemTab);
114266 sqlite3Select(pParse, pSelect, &dest);
114267
114268 /* Generate code to scan the ephemeral table and call VUpdate. */
114269 iReg = ++pParse->nMem;
114270 pParse->nMem += pTab->nCol+1;
@@ -115999,10 +116115,11 @@
116115 # define TERM_VNULL 0x00 /* Disabled if not using stat3 */
116116 #endif
116117 #define TERM_LIKEOPT 0x100 /* Virtual terms from the LIKE optimization */
116118 #define TERM_LIKECOND 0x200 /* Conditionally this LIKE operator term */
116119 #define TERM_LIKE 0x400 /* The original LIKE operator */
116120 #define TERM_IS 0x800 /* Term.pExpr is an IS operator */
116121
116122 /*
116123 ** An instance of the WhereScan object is used as an iterator for locating
116124 ** terms in the WHERE clause that are useful to the query planner.
116125 */
@@ -116147,25 +116264,26 @@
116264 ** Bitmasks for the operators on WhereTerm objects. These are all
116265 ** operators that are of interest to the query planner. An
116266 ** OR-ed combination of these values can be used when searching for
116267 ** particular WhereTerms within a WhereClause.
116268 */
116269 #define WO_IN 0x0001
116270 #define WO_EQ 0x0002
116271 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
116272 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
116273 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
116274 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
116275 #define WO_MATCH 0x0040
116276 #define WO_IS 0x0080
116277 #define WO_ISNULL 0x0100
116278 #define WO_OR 0x0200 /* Two or more OR-connected terms */
116279 #define WO_AND 0x0400 /* Two or more AND-connected terms */
116280 #define WO_EQUIV 0x0800 /* Of the form A==B, both columns */
116281 #define WO_NOOP 0x1000 /* This term does not restrict search space */
116282
116283 #define WO_ALL 0x1fff /* Mask of all possible WO_* values */
116284 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
116285
116286 /*
116287 ** These are definitions of bits in the WhereLoop.wsFlags field.
116288 ** The particular combination of bits in each WhereLoop help to
116289 ** determine the algorithm that WhereLoop represents.
@@ -116535,11 +116653,11 @@
116653 static int allowedOp(int op){
116654 assert( TK_GT>TK_EQ && TK_GT<TK_GE );
116655 assert( TK_LT>TK_EQ && TK_LT<TK_GE );
116656 assert( TK_LE>TK_EQ && TK_LE<TK_GE );
116657 assert( TK_GE==TK_EQ+4 );
116658 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
116659 }
116660
116661 /*
116662 ** Commute a comparison operator. Expressions of the form "X op Y"
116663 ** are converted into "Y op X".
@@ -116588,10 +116706,12 @@
116706 assert( allowedOp(op) );
116707 if( op==TK_IN ){
116708 c = WO_IN;
116709 }else if( op==TK_ISNULL ){
116710 c = WO_ISNULL;
116711 }else if( op==TK_IS ){
116712 c = WO_IS;
116713 }else{
116714 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
116715 c = (u16)(WO_EQ<<(op-TK_EQ));
116716 }
116717 assert( op!=TK_ISNULL || c==WO_ISNULL );
@@ -116599,10 +116719,11 @@
116719 assert( op!=TK_EQ || c==WO_EQ );
116720 assert( op!=TK_LT || c==WO_LT );
116721 assert( op!=TK_LE || c==WO_LE );
116722 assert( op!=TK_GT || c==WO_GT );
116723 assert( op!=TK_GE || c==WO_GE );
116724 assert( op!=TK_IS || c==WO_IS );
116725 return c;
116726 }
116727
116728 /*
116729 ** Advance to the next WhereTerm that matches according to the criteria
@@ -116659,15 +116780,16 @@
116780 if( pColl==0 ) pColl = pParse->db->pDfltColl;
116781 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
116782 continue;
116783 }
116784 }
116785 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
116786 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
116787 && pX->iTable==pScan->aEquiv[0]
116788 && pX->iColumn==pScan->aEquiv[1]
116789 ){
116790 testcase( pTerm->eOperator & WO_IS );
116791 continue;
116792 }
116793 pScan->k = k+1;
116794 return pTerm;
116795 }
@@ -116765,13 +116887,15 @@
116887 WhereTerm *pResult = 0;
116888 WhereTerm *p;
116889 WhereScan scan;
116890
116891 p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
116892 op &= WO_EQ|WO_IS;
116893 while( p ){
116894 if( (p->prereqRight & notReady)==0 ){
116895 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
116896 testcase( p->eOperator & WO_IS );
116897 return p;
116898 }
116899 if( pResult==0 ) pResult = p;
116900 }
116901 p = whereScanNext(&scan);
@@ -116802,11 +116926,11 @@
116926 ** so and false if not.
116927 **
116928 ** In order for the operator to be optimizible, the RHS must be a string
116929 ** literal that does not begin with a wildcard. The LHS must be a column
116930 ** that may only be NULL, a string, or a BLOB, never a number. (This means
116931 ** that virtual tables cannot participate in the LIKE optimization.) The
116932 ** collating sequence for the column on the LHS must be appropriate for
116933 ** the operator.
116934 */
116935 static int isLikeOrGlob(
116936 Parse *pParse, /* Parsing and code generating context */
@@ -117347,10 +117471,50 @@
117471 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */
117472 }
117473 }
117474 }
117475 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
117476
117477 /*
117478 ** We already know that pExpr is a binary operator where both operands are
117479 ** column references. This routine checks to see if pExpr is an equivalence
117480 ** relation:
117481 ** 1. The SQLITE_Transitive optimization must be enabled
117482 ** 2. Must be either an == or an IS operator
117483 ** 3. Not originating the ON clause of an OUTER JOIN
117484 ** 4. The affinities of A and B must be compatible
117485 ** 5a. Both operands use the same collating sequence OR
117486 ** 5b. The overall collating sequence is BINARY
117487 ** If this routine returns TRUE, that means that the RHS can be substituted
117488 ** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
117489 ** This is an optimization. No harm comes from returning 0. But if 1 is
117490 ** returned when it should not be, then incorrect answers might result.
117491 */
117492 static int termIsEquivalence(Parse *pParse, Expr *pExpr){
117493 char aff1, aff2;
117494 CollSeq *pColl;
117495 const char *zColl1, *zColl2;
117496 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
117497 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
117498 if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
117499 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
117500 aff2 = sqlite3ExprAffinity(pExpr->pRight);
117501 if( aff1!=aff2
117502 && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
117503 ){
117504 return 0;
117505 }
117506 pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
117507 if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1;
117508 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
117509 /* Since pLeft and pRight are both a column references, their collating
117510 ** sequence should always be defined. */
117511 zColl1 = ALWAYS(pColl) ? pColl->zName : 0;
117512 pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
117513 zColl2 = ALWAYS(pColl) ? pColl->zName : 0;
117514 return sqlite3StrICmp(zColl1, zColl2)==0;
117515 }
117516
117517 /*
117518 ** The input to this routine is an WhereTerm structure with only the
117519 ** "pExpr" field filled in. The job of this routine is to analyze the
117520 ** subexpression and populate all the other fields of the WhereTerm
@@ -117426,10 +117590,11 @@
117590 if( pLeft->op==TK_COLUMN ){
117591 pTerm->leftCursor = pLeft->iTable;
117592 pTerm->u.leftColumn = pLeft->iColumn;
117593 pTerm->eOperator = operatorMask(op) & opMask;
117594 }
117595 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
117596 if( pRight && pRight->op==TK_COLUMN ){
117597 WhereTerm *pNew;
117598 Expr *pDup;
117599 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
117600 if( pTerm->leftCursor>=0 ){
@@ -117441,16 +117606,15 @@
117606 }
117607 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
117608 if( idxNew==0 ) return;
117609 pNew = &pWC->a[idxNew];
117610 markTermAsChild(pWC, idxNew, idxTerm);
117611 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
117612 pTerm = &pWC->a[idxTerm];
117613 pTerm->wtFlags |= TERM_COPIED;
117614
117615 if( termIsEquivalence(pParse, pDup) ){
 
 
117616 pTerm->eOperator |= WO_EQUIV;
117617 eExtraOp = WO_EQUIV;
117618 }
117619 }else{
117620 pDup = pExpr;
@@ -117640,14 +117804,11 @@
117804 /* When sqlite_stat3 histogram data is available an operator of the
117805 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
117806 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
117807 ** virtual term of that form.
117808 **
117809 ** Note that the virtual term must be tagged with TERM_VNULL.
 
 
 
117810 */
117811 if( pExpr->op==TK_NOTNULL
117812 && pExpr->pLeft->op==TK_COLUMN
117813 && pExpr->pLeft->iColumn>=0
117814 && OptimizationEnabled(db, SQLITE_Stat34)
@@ -117788,10 +117949,40 @@
117949 ** Estimate the logarithm of the input value to base 2.
117950 */
117951 static LogEst estLog(LogEst N){
117952 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
117953 }
117954
117955 /*
117956 ** Convert OP_Column opcodes to OP_Copy in previously generated code.
117957 **
117958 ** This routine runs over generated VDBE code and translates OP_Column
117959 ** opcodes into OP_Copy, and OP_Rowid into OP_Null, when the table is being
117960 ** accessed via co-routine instead of via table lookup.
117961 */
117962 static void translateColumnToCopy(
117963 Vdbe *v, /* The VDBE containing code to translate */
117964 int iStart, /* Translate from this opcode to the end */
117965 int iTabCur, /* OP_Column/OP_Rowid references to this table */
117966 int iRegister /* The first column is in this register */
117967 ){
117968 VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
117969 int iEnd = sqlite3VdbeCurrentAddr(v);
117970 for(; iStart<iEnd; iStart++, pOp++){
117971 if( pOp->p1!=iTabCur ) continue;
117972 if( pOp->opcode==OP_Column ){
117973 pOp->opcode = OP_Copy;
117974 pOp->p1 = pOp->p2 + iRegister;
117975 pOp->p2 = pOp->p3;
117976 pOp->p3 = 0;
117977 }else if( pOp->opcode==OP_Rowid ){
117978 pOp->opcode = OP_Null;
117979 pOp->p1 = 0;
117980 pOp->p3 = 0;
117981 }
117982 }
117983 }
117984
117985 /*
117986 ** Two routines for printing the content of an sqlite3_index_info
117987 ** structure. Used for testing and debugging only. If neither
117988 ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
@@ -117847,15 +118038,16 @@
118038 struct SrcList_item *pSrc, /* Table we are trying to access */
118039 Bitmask notReady /* Tables in outer loops of the join */
118040 ){
118041 char aff;
118042 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
118043 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
118044 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
118045 if( pTerm->u.leftColumn<0 ) return 0;
118046 aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
118047 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
118048 testcase( pTerm->pExpr->op==TK_IS );
118049 return 1;
118050 }
118051 #endif
118052
118053
@@ -117890,10 +118082,11 @@
118082 Bitmask idxCols; /* Bitmap of columns used for indexing */
118083 Bitmask extraCols; /* Bitmap of additional columns */
118084 u8 sentWarning = 0; /* True if a warnning has been issued */
118085 Expr *pPartial = 0; /* Partial Index Expression */
118086 int iContinue = 0; /* Jump here to skip excluded rows */
118087 struct SrcList_item *pTabItem; /* FROM clause term being indexed */
118088
118089 /* Generate code to skip over the creation and initialization of the
118090 ** transient index on 2nd and subsequent iterations of the loop. */
118091 v = pParse->pVdbe;
118092 assert( v!=0 );
@@ -118015,11 +118208,20 @@
118208 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
118209 VdbeComment((v, "for %s", pTable->zName));
118210
118211 /* Fill the automatic index with content */
118212 sqlite3ExprCachePush(pParse);
118213 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
118214 if( pTabItem->viaCoroutine ){
118215 int regYield = pTabItem->regReturn;
118216 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
118217 addrTop = sqlite3VdbeAddOp1(v, OP_Yield, regYield);
118218 VdbeCoverage(v);
118219 VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
118220 }else{
118221 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
118222 }
118223 if( pPartial ){
118224 iContinue = sqlite3VdbeMakeLabel(v);
118225 sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL);
118226 pLoop->wsFlags |= WHERE_PARTIALIDX;
118227 }
@@ -118026,11 +118228,17 @@
118228 regRecord = sqlite3GetTempReg(pParse);
118229 sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0);
118230 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
118231 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
118232 if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
118233 if( pTabItem->viaCoroutine ){
118234 translateColumnToCopy(v, addrTop, pLevel->iTabCur, pTabItem->regResult);
118235 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
118236 pTabItem->viaCoroutine = 0;
118237 }else{
118238 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
118239 }
118240 sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
118241 sqlite3VdbeJumpHere(v, addrTop);
118242 sqlite3ReleaseTempReg(pParse, regRecord);
118243 sqlite3ExprCachePop(pParse);
118244
@@ -118068,12 +118276,13 @@
118276 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
118277 if( pTerm->leftCursor != pSrc->iCursor ) continue;
118278 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
118279 testcase( pTerm->eOperator & WO_IN );
118280 testcase( pTerm->eOperator & WO_ISNULL );
118281 testcase( pTerm->eOperator & WO_IS );
118282 testcase( pTerm->eOperator & WO_ALL );
118283 if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
118284 if( pTerm->wtFlags & TERM_VNULL ) continue;
118285 nTerm++;
118286 }
118287
118288 /* If the ORDER BY clause contains only columns in the current
@@ -118120,13 +118329,14 @@
118329 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
118330 u8 op;
118331 if( pTerm->leftCursor != pSrc->iCursor ) continue;
118332 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
118333 testcase( pTerm->eOperator & WO_IN );
118334 testcase( pTerm->eOperator & WO_IS );
118335 testcase( pTerm->eOperator & WO_ISNULL );
118336 testcase( pTerm->eOperator & WO_ALL );
118337 if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
118338 if( pTerm->wtFlags & TERM_VNULL ) continue;
118339 pIdxCons[j].iColumn = pTerm->u.leftColumn;
118340 pIdxCons[j].iTermOffset = i;
118341 op = (u8)pTerm->eOperator & WO_ALL;
118342 if( op==WO_IN ) op = WO_EQ;
@@ -118964,11 +119174,11 @@
119174 Expr *pX = pTerm->pExpr;
119175 Vdbe *v = pParse->pVdbe;
119176 int iReg; /* Register holding results */
119177
119178 assert( iTarget>0 );
119179 if( pX->op==TK_EQ || pX->op==TK_IS ){
119180 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
119181 }else if( pX->op==TK_ISNULL ){
119182 iReg = iTarget;
119183 sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
119184 #ifndef SQLITE_OMIT_SUBQUERY
@@ -119149,11 +119359,11 @@
119359 }
119360 testcase( pTerm->eOperator & WO_ISNULL );
119361 testcase( pTerm->eOperator & WO_IN );
119362 if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
119363 Expr *pRight = pTerm->pExpr->pRight;
119364 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
119365 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
119366 VdbeCoverage(v);
119367 }
119368 if( zAff ){
119369 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
@@ -120271,20 +120481,23 @@
120481 */
120482 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
120483 Expr *pE, *pEAlt;
120484 WhereTerm *pAlt;
120485 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
120486 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
120487 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
120488 if( pTerm->leftCursor!=iCur ) continue;
120489 if( pLevel->iLeftJoin ) continue;
120490 pE = pTerm->pExpr;
120491 assert( !ExprHasProperty(pE, EP_FromJoin) );
120492 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
120493 pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
120494 WO_EQ|WO_IN|WO_IS, 0);
120495 if( pAlt==0 ) continue;
120496 if( pAlt->wtFlags & (TERM_CODED) ) continue;
120497 testcase( pAlt->eOperator & WO_EQ );
120498 testcase( pAlt->eOperator & WO_IS );
120499 testcase( pAlt->eOperator & WO_IN );
120500 VdbeModuleComment((v, "begin transitive constraint"));
120501 pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
120502 if( pEAlt ){
120503 *pEAlt = *pAlt->pExpr;
@@ -120330,13 +120543,14 @@
120543 char zType[4];
120544 memcpy(zType, "...", 4);
120545 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
120546 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
120547 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
120548 sqlite3DebugPrintf(
120549 "TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x wtFlags=0x%04x\n",
120550 iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb,
120551 pTerm->eOperator, pTerm->wtFlags);
120552 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
120553 }
120554 }
120555 #endif
120556
@@ -120822,12 +121036,13 @@
121036 pLoop->nOut += pTerm->truthProb;
121037 }else{
121038 /* In the absence of explicit truth probabilities, use heuristics to
121039 ** guess a reasonable truth probability. */
121040 pLoop->nOut--;
121041 if( pTerm->eOperator&(WO_EQ|WO_IS) ){
121042 Expr *pRight = pTerm->pExpr->pRight;
121043 testcase( pTerm->pExpr->op==TK_IS );
121044 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
121045 k = 10;
121046 }else{
121047 k = 20;
121048 }
@@ -120891,14 +121106,14 @@
121106
121107 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
121108 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
121109 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
121110 opMask = WO_LT|WO_LE;
121111 }else if( /*pProbe->tnum<=0 ||*/ (pSrc->jointype & JT_LEFT)!=0 ){
121112 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE;
121113 }else{
121114 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS;
121115 }
121116 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
121117
121118 assert( pNew->u.btree.nEq<pProbe->nColumn );
121119 iCol = pProbe->aiColumn[pNew->u.btree.nEq];
@@ -120957,11 +121172,11 @@
121172 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
121173 }
121174 assert( nIn>0 ); /* RHS always has 2 or more terms... The parser
121175 ** changes "x IN (?)" into "x=?". */
121176
121177 }else if( eOp & (WO_EQ|WO_IS) ){
121178 pNew->wsFlags |= WHERE_COLUMN_EQ;
121179 if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
121180 if( iCol>=0 && pProbe->uniqNotNull==0 ){
121181 pNew->wsFlags |= WHERE_UNQ_WANTED;
121182 }else{
@@ -121007,11 +121222,11 @@
121222 /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4
121223 ** data, using some other estimate. */
121224 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
121225 }else{
121226 int nEq = ++pNew->u.btree.nEq;
121227 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) );
121228
121229 assert( pNew->nOut==saved_nOut );
121230 if( pTerm->truthProb<=0 && iCol>=0 ){
121231 assert( (eOp & WO_IN) || nIn==0 );
121232 testcase( eOp & WO_IN );
@@ -121024,12 +121239,13 @@
121239 && pProbe->nSample
121240 && pNew->u.btree.nEq<=pProbe->nSampleCol
121241 && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
121242 ){
121243 Expr *pExpr = pTerm->pExpr;
121244 if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){
121245 testcase( eOp & WO_EQ );
121246 testcase( eOp & WO_IS );
121247 testcase( eOp & WO_ISNULL );
121248 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
121249 }else{
121250 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
121251 }
@@ -121294,19 +121510,18 @@
121510 rSize = pTab->nRowLogEst;
121511 rLogSize = estLog(rSize);
121512
121513 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
121514 /* Automatic indexes */
121515 if( !pBuilder->pOrSet /* Not part of an OR optimization */
121516 && (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0
121517 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
121518 && pSrc->pIndex==0 /* Has no INDEXED BY clause */
121519 && !pSrc->notIndexed /* Has no NOT INDEXED clause */
121520 && HasRowid(pTab) /* Is not a WITHOUT ROWID table. (FIXME: Why not?) */
121521 && !pSrc->isCorrelated /* Not a correlated subquery */
121522 && !pSrc->isRecursive /* Not a recursive common table expression. */
 
121523 ){
121524 /* Generate auto-index WhereLoops */
121525 WhereTerm *pTerm;
121526 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
121527 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
@@ -121862,21 +122077,22 @@
122077 if( MASKBIT(i) & obSat ) continue;
122078 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
122079 if( pOBExpr->op!=TK_COLUMN ) continue;
122080 if( pOBExpr->iTable!=iCur ) continue;
122081 pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
122082 ~ready, WO_EQ|WO_ISNULL|WO_IS, 0);
122083 if( pTerm==0 ) continue;
122084 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
122085 const char *z1, *z2;
122086 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
122087 if( !pColl ) pColl = db->pDfltColl;
122088 z1 = pColl->zName;
122089 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr);
122090 if( !pColl ) pColl = db->pDfltColl;
122091 z2 = pColl->zName;
122092 if( sqlite3StrICmp(z1, z2)!=0 ) continue;
122093 testcase( pTerm->pExpr->op==TK_IS );
122094 }
122095 obSat |= MASKBIT(i);
122096 }
122097
122098 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
@@ -121903,11 +122119,11 @@
122119 u8 bOnce; /* True to run the ORDER BY search loop */
122120
122121 /* Skip over == and IS NULL terms */
122122 if( j<pLoop->u.btree.nEq
122123 && pLoop->nSkip==0
122124 && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL|WO_IS))!=0
122125 ){
122126 if( i & WO_ISNULL ){
122127 testcase( isOrderDistinct );
122128 isOrderDistinct = 0;
122129 }
@@ -122476,28 +122692,32 @@
122692 iCur = pItem->iCursor;
122693 pWC = &pWInfo->sWC;
122694 pLoop = pBuilder->pNew;
122695 pLoop->wsFlags = 0;
122696 pLoop->nSkip = 0;
122697 pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
122698 if( pTerm ){
122699 testcase( pTerm->eOperator & WO_IS );
122700 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
122701 pLoop->aLTerm[0] = pTerm;
122702 pLoop->nLTerm = 1;
122703 pLoop->u.btree.nEq = 1;
122704 /* TUNING: Cost of a rowid lookup is 10 */
122705 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
122706 }else{
122707 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
122708 int opMask;
122709 assert( pLoop->aLTermSpace==pLoop->aLTerm );
122710 if( !IsUniqueIndex(pIdx)
122711 || pIdx->pPartIdxWhere!=0
122712 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
122713 ) continue;
122714 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
122715 for(j=0; j<pIdx->nKeyCol; j++){
122716 pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx);
122717 if( pTerm==0 ) break;
122718 testcase( pTerm->eOperator & WO_IS );
122719 pLoop->aLTerm[j] = pTerm;
122720 }
122721 if( j!=pIdx->nKeyCol ) continue;
122722 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
122723 if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
@@ -123132,30 +123352,16 @@
123352 Table *pTab = pTabItem->pTab;
123353 assert( pTab!=0 );
123354 pLoop = pLevel->pWLoop;
123355
123356 /* For a co-routine, change all OP_Column references to the table of
123357 ** the co-routine into OP_Copy of result contained in a register.
123358 ** OP_Rowid becomes OP_Null.
123359 */
123360 if( pTabItem->viaCoroutine && !db->mallocFailed ){
123361 translateColumnToCopy(v, pLevel->addrBody, pLevel->iTabCur,
123362 pTabItem->regResult);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123363 continue;
123364 }
123365
123366 /* Close all of the cursors that were opened by sqlite3WhereBegin.
123367 ** Except, do not close cursors that will be reused by the OR optimization
@@ -125419,11 +125625,11 @@
125625 {yygotominor.yy186 = 0;}
125626 break;
125627 case 35: /* table_options ::= WITHOUT nm */
125628 {
125629 if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
125630 yygotominor.yy186 = TF_WithoutRowid | TF_NoVisibleRowid;
125631 }else{
125632 yygotominor.yy186 = 0;
125633 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
125634 }
125635 }
@@ -125643,10 +125849,11 @@
125849 {yygotominor.yy3 = yymsp[0].minor.yy3;}
125850 break;
125851 case 114: /* selectnowith ::= selectnowith multiselect_op oneselect */
125852 {
125853 Select *pRhs = yymsp[0].minor.yy3;
125854 Select *pLhs = yymsp[-2].minor.yy3;
125855 if( pRhs && pRhs->pPrior ){
125856 SrcList *pFrom;
125857 Token x;
125858 x.n = 0;
125859 parserDoubleLinkSelect(pParse, pRhs);
@@ -125653,15 +125860,16 @@
125860 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
125861 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0);
125862 }
125863 if( pRhs ){
125864 pRhs->op = (u8)yymsp[-1].minor.yy328;
125865 pRhs->pPrior = pLhs;
125866 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
125867 pRhs->selFlags &= ~SF_MultiValue;
125868 if( yymsp[-1].minor.yy328!=TK_ALL ) pParse->hasCompound = 1;
125869 }else{
125870 sqlite3SelectDelete(pParse->db, pLhs);
125871 }
125872 yygotominor.yy3 = pRhs;
125873 }
125874 break;
125875 case 116: /* multiselect_op ::= UNION ALL */
@@ -125718,11 +125926,13 @@
125926 break;
125927 case 122: /* distinct ::= DISTINCT */
125928 {yygotominor.yy381 = SF_Distinct;}
125929 break;
125930 case 123: /* distinct ::= ALL */
125931 {yygotominor.yy381 = SF_All;}
125932 break;
125933 case 124: /* distinct ::= */
125934 {yygotominor.yy381 = 0;}
125935 break;
125936 case 125: /* sclp ::= selcollist COMMA */
125937 case 243: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==243);
125938 {yygotominor.yy14 = yymsp[-1].minor.yy14;}
@@ -126013,11 +126223,11 @@
126223 if( yymsp[-1].minor.yy14 && yymsp[-1].minor.yy14->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
126224 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
126225 }
126226 yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy14, &yymsp[-4].minor.yy0);
126227 spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
126228 if( yymsp[-2].minor.yy381==SF_Distinct && yygotominor.yy346.pExpr ){
126229 yygotominor.yy346.pExpr->flags |= EP_Distinct;
126230 }
126231 }
126232 break;
126233 case 196: /* expr ::= ID|INDEXED LP STAR RP */
@@ -127534,11 +127744,12 @@
127744 }
127745 }
127746 }
127747 abort_parse:
127748 assert( nErr==0 );
127749 if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
127750 assert( zSql[i]==0 );
127751 if( lastTokenParsed!=TK_SEMI ){
127752 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
127753 pParse->zTail = &zSql[i];
127754 }
127755 if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
@@ -127556,11 +127767,11 @@
127767 db->lookaside.bEnabled = enableLookaside;
127768 if( db->mallocFailed ){
127769 pParse->rc = SQLITE_NOMEM;
127770 }
127771 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
127772 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
127773 }
127774 assert( pzErrMsg!=0 );
127775 if( pParse->zErrMsg ){
127776 *pzErrMsg = pParse->zErrMsg;
127777 sqlite3_log(pParse->rc, "%s", *pzErrMsg);
@@ -130748,10 +130959,13 @@
130959 | SQLITE_ForeignKeys
130960 #endif
130961 #if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
130962 | SQLITE_ReverseOrder
130963 #endif
130964 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
130965 | SQLITE_CellSizeCk
130966 #endif
130967 ;
130968 sqlite3HashInit(&db->aCollSeq);
130969 #ifndef SQLITE_OMIT_VIRTUALTABLE
130970 sqlite3HashInit(&db->aModule);
130971 #endif
@@ -130867,12 +131081,11 @@
131081 }
131082 #endif
131083
131084 #ifdef SQLITE_ENABLE_DBSTAT_VTAB
131085 if( !db->mallocFailed && rc==SQLITE_OK){
131086 rc = sqlite3DbstatRegister(db);
 
131087 }
131088 #endif
131089
131090 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
131091 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
@@ -132872,10 +133085,12 @@
133085 typedef struct Fts3SegFilter Fts3SegFilter;
133086 typedef struct Fts3DeferredToken Fts3DeferredToken;
133087 typedef struct Fts3SegReader Fts3SegReader;
133088 typedef struct Fts3MultiSegReader Fts3MultiSegReader;
133089
133090 typedef struct MatchinfoBuffer MatchinfoBuffer;
133091
133092 /*
133093 ** A connection to a fulltext index is an instance of the following
133094 ** structure. The xCreate and xConnect methods create an instance
133095 ** of this structure and xDestroy and xDisconnect free that instance.
133096 ** All other methods receive a pointer to the structure as one of their
@@ -132981,13 +133196,11 @@
133196 int nRowAvg; /* Average size of database rows, in pages */
133197 sqlite3_int64 nDoc; /* Documents in table */
133198 i64 iMinDocid; /* Minimum docid to return */
133199 i64 iMaxDocid; /* Maximum docid to return */
133200 int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */
133201 MatchinfoBuffer *pMIBuffer; /* Buffer for matchinfo data */
 
 
133202 };
133203
133204 #define FTS3_EVAL_FILTER 0
133205 #define FTS3_EVAL_NEXT 1
133206 #define FTS3_EVAL_MATCHINFO 2
@@ -133103,11 +133316,13 @@
133316 sqlite3_int64 iDocid; /* Current docid */
133317 u8 bEof; /* True this expression is at EOF already */
133318 u8 bStart; /* True if iDocid is valid */
133319 u8 bDeferred; /* True if this expression is entirely deferred */
133320
133321 /* The following are used by the fts3_snippet.c module. */
133322 int iPhrase; /* Index of this phrase in matchinfo() results */
133323 u32 *aMI; /* See above */
133324 };
133325
133326 /*
133327 ** Candidate values for Fts3Query.eType. Note that the order of the first
133328 ** four values is in order of precedence when parsing expressions. For
@@ -133224,10 +133439,11 @@
133439 SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
133440 SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
133441 SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
133442 SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
133443 SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
133444 SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
133445
133446 /* fts3_tokenizer.c */
133447 SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
133448 SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
133449 SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
@@ -133239,10 +133455,11 @@
133455 SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
133456 SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
133457 const char *, const char *, int, int
133458 );
133459 SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
133460 SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
133461
133462 /* fts3_expr.c */
133463 SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
133464 char **, int, int, int, const char *, int, Fts3Expr **, char **
133465 );
@@ -134666,11 +134883,11 @@
134883 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
134884 sqlite3_finalize(pCsr->pStmt);
134885 sqlite3Fts3ExprFree(pCsr->pExpr);
134886 sqlite3Fts3FreeDeferredTokens(pCsr);
134887 sqlite3_free(pCsr->aDoclist);
134888 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
134889 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
134890 sqlite3_free(pCsr);
134891 return SQLITE_OK;
134892 }
134893
@@ -136167,11 +136384,11 @@
136384 assert( iIdx==nVal );
136385
136386 /* In case the cursor has been used before, clear it now. */
136387 sqlite3_finalize(pCsr->pStmt);
136388 sqlite3_free(pCsr->aDoclist);
136389 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
136390 sqlite3Fts3ExprFree(pCsr->pExpr);
136391 memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
136392
136393 /* Set the lower and upper bounds on docids to return */
136394 pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
@@ -138065,11 +138282,11 @@
138282 ** is populated as for "A * C" before returning.
138283 **
138284 ** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is
138285 ** advanced to point to the next row that matches "x AND y".
138286 **
138287 ** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
138288 ** really a match, taking into account deferred tokens and NEAR operators.
138289 */
138290 static void fts3EvalNextRow(
138291 Fts3Cursor *pCsr, /* FTS Cursor handle */
138292 Fts3Expr *pExpr, /* Expr. to advance to next matching row */
@@ -138285,11 +138502,11 @@
138502
138503 return res;
138504 }
138505
138506 /*
138507 ** This function is a helper function for sqlite3Fts3EvalTestDeferred().
138508 ** Assuming no error occurs or has occurred, It returns non-zero if the
138509 ** expression passed as the second argument matches the row that pCsr
138510 ** currently points to, or zero if it does not.
138511 **
138512 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
@@ -138406,11 +138623,11 @@
138623 ** it is determined that the row does *not* match the query.
138624 **
138625 ** Or, if no error occurs and it seems the current row does match the FTS
138626 ** query, return 0.
138627 */
138628 SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
138629 int rc = *pRc;
138630 int bMiss = 0;
138631 if( rc==SQLITE_OK ){
138632
138633 /* If there are one or more deferred tokens, load the current row into
@@ -138453,11 +138670,11 @@
138670 fts3EvalNextRow(pCsr, pExpr, &rc);
138671 pCsr->isEof = pExpr->bEof;
138672 pCsr->isRequireSeek = 1;
138673 pCsr->isMatchinfoNeeded = 1;
138674 pCsr->iPrevId = pExpr->iDocid;
138675 }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
138676 }
138677
138678 /* Check if the cursor is past the end of the docid range specified
138679 ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */
138680 if( rc==SQLITE_OK && (
@@ -138614,11 +138831,11 @@
138831 pCsr->isRequireSeek = 1;
138832 pCsr->isMatchinfoNeeded = 1;
138833 pCsr->iPrevId = pRoot->iDocid;
138834 }while( pCsr->isEof==0
138835 && pRoot->eType==FTSQUERY_NEAR
138836 && sqlite3Fts3EvalTestDeferred(pCsr, &rc)
138837 );
138838
138839 if( rc==SQLITE_OK && pCsr->isEof==0 ){
138840 fts3EvalUpdateCounts(pRoot);
138841 }
@@ -138639,11 +138856,10 @@
138856 fts3EvalRestart(pCsr, pRoot, &rc);
138857 do {
138858 fts3EvalNextRow(pCsr, pRoot, &rc);
138859 assert( pRoot->bEof==0 );
138860 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
 
138861 }
138862 }
138863 return rc;
138864 }
138865
@@ -148651,10 +148867,11 @@
148867 #define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */
148868 #define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */
148869 #define FTS3_MATCHINFO_LCS 's' /* nCol values */
148870 #define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */
148871 #define FTS3_MATCHINFO_LHITS 'y' /* nCol*nPhrase values */
148872 #define FTS3_MATCHINFO_LHITS_BM 'b' /* nCol*nPhrase values */
148873
148874 /*
148875 ** The default value for the second argument to matchinfo().
148876 */
148877 #define FTS3_MATCHINFO_DEFAULT "pcx"
@@ -148712,13 +148929,26 @@
148929 struct MatchInfo {
148930 Fts3Cursor *pCursor; /* FTS3 Cursor */
148931 int nCol; /* Number of columns in table */
148932 int nPhrase; /* Number of matchable phrases in query */
148933 sqlite3_int64 nDoc; /* Number of docs in database */
148934 char flag;
148935 u32 *aMatchinfo; /* Pre-allocated buffer */
148936 };
148937
148938 /*
148939 ** An instance of this structure is used to manage a pair of buffers, each
148940 ** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
148941 ** for details.
148942 */
148943 struct MatchinfoBuffer {
148944 u8 aRef[3];
148945 int nElem;
148946 int bGlobal; /* Set if global data is loaded */
148947 char *zMatchinfo;
148948 u32 aMatchinfo[1];
148949 };
148950
148951
148952 /*
148953 ** The snippet() and offsets() functions both return text values. An instance
148954 ** of the following structure is used to accumulate those values while the
@@ -148729,10 +148959,101 @@
148959 char *z; /* Pointer to buffer containing string */
148960 int n; /* Length of z in bytes (excl. nul-term) */
148961 int nAlloc; /* Allocated size of buffer z in bytes */
148962 };
148963
148964
148965 /*************************************************************************
148966 ** Start of MatchinfoBuffer code.
148967 */
148968
148969 /*
148970 ** Allocate a two-slot MatchinfoBuffer object.
148971 */
148972 static MatchinfoBuffer *fts3MIBufferNew(int nElem, const char *zMatchinfo){
148973 MatchinfoBuffer *pRet;
148974 int nByte = sizeof(u32) * (2*nElem + 1) + sizeof(MatchinfoBuffer);
148975 int nStr = (int)strlen(zMatchinfo);
148976
148977 pRet = sqlite3_malloc(nByte + nStr+1);
148978 if( pRet ){
148979 memset(pRet, 0, nByte);
148980 pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
148981 pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0] + sizeof(u32)*(nElem+1);
148982 pRet->nElem = nElem;
148983 pRet->zMatchinfo = ((char*)pRet) + nByte;
148984 memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
148985 pRet->aRef[0] = 1;
148986 }
148987
148988 return pRet;
148989 }
148990
148991 static void fts3MIBufferFree(void *p){
148992 MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
148993
148994 assert( (u32*)p==&pBuf->aMatchinfo[1]
148995 || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2]
148996 );
148997 if( (u32*)p==&pBuf->aMatchinfo[1] ){
148998 pBuf->aRef[1] = 0;
148999 }else{
149000 pBuf->aRef[2] = 0;
149001 }
149002
149003 if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
149004 sqlite3_free(pBuf);
149005 }
149006 }
149007
149008 static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
149009 void (*xRet)(void*) = 0;
149010 u32 *aOut = 0;
149011
149012 if( p->aRef[1]==0 ){
149013 p->aRef[1] = 1;
149014 aOut = &p->aMatchinfo[1];
149015 xRet = fts3MIBufferFree;
149016 }
149017 else if( p->aRef[2]==0 ){
149018 p->aRef[2] = 1;
149019 aOut = &p->aMatchinfo[p->nElem+2];
149020 xRet = fts3MIBufferFree;
149021 }else{
149022 aOut = (u32*)sqlite3_malloc(p->nElem * sizeof(u32));
149023 if( aOut ){
149024 xRet = sqlite3_free;
149025 if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32));
149026 }
149027 }
149028
149029 *paOut = aOut;
149030 return xRet;
149031 }
149032
149033 static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
149034 p->bGlobal = 1;
149035 memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32));
149036 }
149037
149038 /*
149039 ** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
149040 */
149041 SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
149042 if( p ){
149043 assert( p->aRef[0]==1 );
149044 p->aRef[0] = 0;
149045 if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
149046 sqlite3_free(p);
149047 }
149048 }
149049 }
149050
149051 /*
149052 ** End of MatchinfoBuffer code.
149053 *************************************************************************/
149054
149055
149056 /*
149057 ** This function is used to help iterate through a position-list. A position
149058 ** list is a list of unique integers, sorted from smallest to largest. Each
149059 ** element of the list is represented by an FTS3 varint that takes the value
@@ -148766,11 +149087,11 @@
149087 int *piPhrase, /* Pointer to phrase counter */
149088 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
149089 void *pCtx /* Second argument to pass to callback */
149090 ){
149091 int rc; /* Return code */
149092 int eType = pExpr->eType; /* Type of expression node pExpr */
149093
149094 if( eType!=FTSQUERY_PHRASE ){
149095 assert( pExpr->pLeft && pExpr->pRight );
149096 rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
149097 if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
@@ -148799,10 +149120,11 @@
149120 void *pCtx /* Second argument to pass to callback */
149121 ){
149122 int iPhrase = 0; /* Variable used as the phrase counter */
149123 return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
149124 }
149125
149126
149127 /*
149128 ** This is an fts3ExprIterate() callback used while loading the doclists
149129 ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
149130 ** fts3ExprLoadDoclists().
@@ -148844,12 +149166,11 @@
149166 return rc;
149167 }
149168
149169 static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
149170 (*(int *)ctx)++;
149171 pExpr->iPhrase = iPhrase;
 
149172 return SQLITE_OK;
149173 }
149174 static int fts3ExprPhraseCount(Fts3Expr *pExpr){
149175 int nPhrase = 0;
149176 (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
@@ -149066,11 +149387,11 @@
149387 sIter.pCsr = pCsr;
149388 sIter.iCol = iCol;
149389 sIter.nSnippet = nSnippet;
149390 sIter.nPhrase = nList;
149391 sIter.iCurrent = -1;
149392 rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter);
149393 if( rc==SQLITE_OK ){
149394
149395 /* Set the *pmSeen output variable. */
149396 for(i=0; i<nList; i++){
149397 if( sIter.aPhrase[i].pHead ){
@@ -149366,10 +149687,64 @@
149687 }
149688
149689 *ppCollist = pEnd;
149690 return nEntry;
149691 }
149692
149693 /*
149694 ** This function gathers 'y' or 'b' data for a single phrase.
149695 */
149696 static void fts3ExprLHits(
149697 Fts3Expr *pExpr, /* Phrase expression node */
149698 MatchInfo *p /* Matchinfo context */
149699 ){
149700 Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
149701 int iStart;
149702 Fts3Phrase *pPhrase = pExpr->pPhrase;
149703 char *pIter = pPhrase->doclist.pList;
149704 int iCol = 0;
149705
149706 assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS );
149707 if( p->flag==FTS3_MATCHINFO_LHITS ){
149708 iStart = pExpr->iPhrase * p->nCol;
149709 }else{
149710 iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
149711 }
149712
149713 while( 1 ){
149714 int nHit = fts3ColumnlistCount(&pIter);
149715 if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
149716 if( p->flag==FTS3_MATCHINFO_LHITS ){
149717 p->aMatchinfo[iStart + iCol] = (u32)nHit;
149718 }else if( nHit ){
149719 p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
149720 }
149721 }
149722 assert( *pIter==0x00 || *pIter==0x01 );
149723 if( *pIter!=0x01 ) break;
149724 pIter++;
149725 pIter += fts3GetVarint32(pIter, &iCol);
149726 }
149727 }
149728
149729 /*
149730 ** Gather the results for matchinfo directives 'y' and 'b'.
149731 */
149732 static void fts3ExprLHitGather(
149733 Fts3Expr *pExpr,
149734 MatchInfo *p
149735 ){
149736 assert( (pExpr->pLeft==0)==(pExpr->pRight==0) );
149737 if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
149738 if( pExpr->pLeft ){
149739 fts3ExprLHitGather(pExpr->pLeft, p);
149740 fts3ExprLHitGather(pExpr->pRight, p);
149741 }else{
149742 fts3ExprLHits(pExpr, p);
149743 }
149744 }
149745 }
149746
149747 /*
149748 ** fts3ExprIterate() callback used to collect the "global" matchinfo stats
149749 ** for a single query.
149750 **
@@ -149433,55 +149808,10 @@
149808 }
149809
149810 return rc;
149811 }
149812
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149813 static int fts3MatchinfoCheck(
149814 Fts3Table *pTab,
149815 char cArg,
149816 char **pzErr
149817 ){
@@ -149491,10 +149821,11 @@
149821 || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
149822 || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
149823 || (cArg==FTS3_MATCHINFO_LCS)
149824 || (cArg==FTS3_MATCHINFO_HITS)
149825 || (cArg==FTS3_MATCHINFO_LHITS)
149826 || (cArg==FTS3_MATCHINFO_LHITS_BM)
149827 ){
149828 return SQLITE_OK;
149829 }
149830 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
149831 return SQLITE_ERROR;
@@ -149517,10 +149848,14 @@
149848 break;
149849
149850 case FTS3_MATCHINFO_LHITS:
149851 nVal = pInfo->nCol * pInfo->nPhrase;
149852 break;
149853
149854 case FTS3_MATCHINFO_LHITS_BM:
149855 nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
149856 break;
149857
149858 default:
149859 assert( cArg==FTS3_MATCHINFO_HITS );
149860 nVal = pInfo->nCol * pInfo->nPhrase * 3;
149861 break;
@@ -149712,11 +150047,11 @@
150047 int i;
150048 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
150049 sqlite3_stmt *pSelect = 0;
150050
150051 for(i=0; rc==SQLITE_OK && zArg[i]; i++){
150052 pInfo->flag = zArg[i];
150053 switch( zArg[i] ){
150054 case FTS3_MATCHINFO_NPHRASE:
150055 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
150056 break;
150057
@@ -149772,13 +150107,17 @@
150107 if( rc==SQLITE_OK ){
150108 rc = fts3MatchinfoLcs(pCsr, pInfo);
150109 }
150110 break;
150111
150112 case FTS3_MATCHINFO_LHITS_BM:
150113 case FTS3_MATCHINFO_LHITS: {
150114 int nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32);
150115 memset(pInfo->aMatchinfo, 0, nZero);
150116 fts3ExprLHitGather(pCsr->pExpr, pInfo);
150117 break;
150118 }
150119
150120 default: {
150121 Fts3Expr *pExpr;
150122 assert( zArg[i]==FTS3_MATCHINFO_HITS );
150123 pExpr = pCsr->pExpr;
@@ -149788,10 +150127,11 @@
150127 if( pCsr->pDeferred ){
150128 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
150129 if( rc!=SQLITE_OK ) break;
150130 }
150131 rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
150132 sqlite3Fts3EvalTestDeferred(pCsr, &rc);
150133 if( rc!=SQLITE_OK ) break;
150134 }
150135 (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
150136 break;
150137 }
@@ -149807,73 +150147,90 @@
150147
150148 /*
150149 ** Populate pCsr->aMatchinfo[] with data for the current row. The
150150 ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
150151 */
150152 static void fts3GetMatchinfo(
150153 sqlite3_context *pCtx, /* Return results here */
150154 Fts3Cursor *pCsr, /* FTS3 Cursor object */
150155 const char *zArg /* Second argument to matchinfo() function */
150156 ){
150157 MatchInfo sInfo;
150158 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
150159 int rc = SQLITE_OK;
150160 int bGlobal = 0; /* Collect 'global' stats as well as local */
150161
150162 u32 *aOut = 0;
150163 void (*xDestroyOut)(void*) = 0;
150164
150165 memset(&sInfo, 0, sizeof(MatchInfo));
150166 sInfo.pCursor = pCsr;
150167 sInfo.nCol = pTab->nColumn;
150168
150169 /* If there is cached matchinfo() data, but the format string for the
150170 ** cache does not match the format string for this request, discard
150171 ** the cached data. */
150172 if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
150173 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
150174 pCsr->pMIBuffer = 0;
 
 
150175 }
150176
150177 /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
150178 ** matchinfo function has been called for this query. In this case
150179 ** allocate the array used to accumulate the matchinfo data and
150180 ** initialize those elements that are constant for every row.
150181 */
150182 if( pCsr->pMIBuffer==0 ){
150183 int nMatchinfo = 0; /* Number of u32 elements in match-info */
 
150184 int i; /* Used to iterate through zArg */
150185
150186 /* Determine the number of phrases in the query */
150187 pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
150188 sInfo.nPhrase = pCsr->nPhrase;
150189
150190 /* Determine the number of integers in the buffer returned by this call. */
150191 for(i=0; zArg[i]; i++){
150192 char *zErr = 0;
150193 if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
150194 sqlite3_result_error(pCtx, zErr, -1);
150195 sqlite3_free(zErr);
150196 return;
150197 }
150198 nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
150199 }
150200
150201 /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
150202 pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
150203 if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM;
150204
 
 
 
 
 
150205 pCsr->isMatchinfoNeeded = 1;
150206 bGlobal = 1;
150207 }
150208
150209 if( rc==SQLITE_OK ){
150210 xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
150211 if( xDestroyOut==0 ){
150212 rc = SQLITE_NOMEM;
150213 }
150214 }
150215
150216 if( rc==SQLITE_OK ){
150217 sInfo.aMatchinfo = aOut;
150218 sInfo.nPhrase = pCsr->nPhrase;
150219 rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
150220 if( bGlobal ){
150221 fts3MIBufferSetGlobal(pCsr->pMIBuffer);
150222 }
150223 }
150224
150225 if( rc!=SQLITE_OK ){
150226 sqlite3_result_error_code(pCtx, rc);
150227 if( xDestroyOut ) xDestroyOut(aOut);
150228 }else{
150229 int n = pCsr->pMIBuffer->nElem * sizeof(u32);
150230 sqlite3_result_blob(pCtx, aOut, n, xDestroyOut);
150231 }
150232 }
150233
150234 /*
150235 ** Implementation of snippet() function.
150236 */
@@ -150075,11 +150432,11 @@
150432 ** no way that this operation can fail, so the return code from
150433 ** fts3ExprIterate() can be discarded.
150434 */
150435 sCtx.iCol = iCol;
150436 sCtx.iTerm = 0;
150437 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx);
150438
150439 /* Retreive the text stored in column iCol. If an SQL NULL is stored
150440 ** in column iCol, jump immediately to the next iteration of the loop.
150441 ** If an OOM occurs while retrieving the data (this can happen if SQLite
150442 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
@@ -150167,42 +150524,25 @@
150524 sqlite3_context *pContext, /* Function call context */
150525 Fts3Cursor *pCsr, /* FTS3 table cursor */
150526 const char *zArg /* Second arg to matchinfo() function */
150527 ){
150528 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 
 
150529 const char *zFormat;
150530
150531 if( zArg ){
 
 
 
 
 
 
 
 
150532 zFormat = zArg;
150533 }else{
150534 zFormat = FTS3_MATCHINFO_DEFAULT;
150535 }
150536
150537 if( !pCsr->pExpr ){
150538 sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
150539 return;
 
 
 
 
 
 
 
 
150540 }else{
150541 /* Retrieve matchinfo() data. */
150542 fts3GetMatchinfo(pContext, pCsr, zFormat);
150543 sqlite3Fts3SegmentsClose(pTab);
150544 }
150545 }
150546
150547 #endif
150548
@@ -151319,10 +151659,11 @@
151659 */
151660 struct RtreeMatchArg {
151661 u32 magic; /* Always RTREE_GEOMETRY_MAGIC */
151662 RtreeGeomCallback cb; /* Info about the callback functions */
151663 int nParam; /* Number of parameters to the SQL function */
151664 sqlite3_value **apSqlParam; /* Original SQL parameter values */
151665 RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
151666 };
151667
151668 #ifndef MAX
151669 # define MAX(x,y) ((x) < (y) ? (y) : (x))
@@ -152450,13 +152791,11 @@
152791 /* Check that value is actually a blob. */
152792 if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
152793
152794 /* Check that the blob is roughly the right size. */
152795 nBlob = sqlite3_value_bytes(pValue);
152796 if( nBlob<(int)sizeof(RtreeMatchArg) ){
 
 
152797 return SQLITE_ERROR;
152798 }
152799
152800 pInfo = (sqlite3_rtree_query_info*)sqlite3_malloc( sizeof(*pInfo)+nBlob );
152801 if( !pInfo ) return SQLITE_NOMEM;
@@ -152463,18 +152802,20 @@
152802 memset(pInfo, 0, sizeof(*pInfo));
152803 pBlob = (RtreeMatchArg*)&pInfo[1];
152804
152805 memcpy(pBlob, sqlite3_value_blob(pValue), nBlob);
152806 nExpected = (int)(sizeof(RtreeMatchArg) +
152807 pBlob->nParam*sizeof(sqlite3_value*) +
152808 (pBlob->nParam-1)*sizeof(RtreeDValue));
152809 if( pBlob->magic!=RTREE_GEOMETRY_MAGIC || nBlob!=nExpected ){
152810 sqlite3_free(pInfo);
152811 return SQLITE_ERROR;
152812 }
152813 pInfo->pContext = pBlob->cb.pContext;
152814 pInfo->nParam = pBlob->nParam;
152815 pInfo->aParam = pBlob->aParam;
152816 pInfo->apSqlParam = pBlob->apSqlParam;
152817
152818 if( pBlob->cb.xGeom ){
152819 pCons->u.xGeom = pBlob->cb.xGeom;
152820 }else{
152821 pCons->op = RTREE_QUERY;
@@ -152637,21 +152978,34 @@
152978 */
152979 static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
152980 Rtree *pRtree = (Rtree*)tab;
152981 int rc = SQLITE_OK;
152982 int ii;
152983 int bMatch = 0; /* True if there exists a MATCH constraint */
152984 i64 nRow; /* Estimated rows returned by this scan */
152985
152986 int iIdx = 0;
152987 char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
152988 memset(zIdxStr, 0, sizeof(zIdxStr));
152989
152990 /* Check if there exists a MATCH constraint - even an unusable one. If there
152991 ** is, do not consider the lookup-by-rowid plan as using such a plan would
152992 ** require the VDBE to evaluate the MATCH constraint, which is not currently
152993 ** possible. */
152994 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
152995 if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){
152996 bMatch = 1;
152997 }
152998 }
152999
153000 assert( pIdxInfo->idxStr==0 );
153001 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
153002 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
153003
153004 if( bMatch==0 && p->usable
153005 && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ
153006 ){
153007 /* We have an equality constraint on the rowid. Use strategy 1. */
153008 int jj;
153009 for(jj=0; jj<ii; jj++){
153010 pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
153011 pIdxInfo->aConstraintUsage[jj].omit = 0;
@@ -154339,10 +154693,22 @@
154693 static void rtreeFreeCallback(void *p){
154694 RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
154695 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
154696 sqlite3_free(p);
154697 }
154698
154699 /*
154700 ** This routine frees the BLOB that is returned by geomCallback().
154701 */
154702 static void rtreeMatchArgFree(void *pArg){
154703 int i;
154704 RtreeMatchArg *p = (RtreeMatchArg*)pArg;
154705 for(i=0; i<p->nParam; i++){
154706 sqlite3_value_free(p->apSqlParam[i]);
154707 }
154708 sqlite3_free(p);
154709 }
154710
154711 /*
154712 ** Each call to sqlite3_rtree_geometry_callback() or
154713 ** sqlite3_rtree_query_callback() creates an ordinary SQLite
154714 ** scalar function that is implemented by this routine.
@@ -154358,28 +154724,38 @@
154724 */
154725 static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
154726 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
154727 RtreeMatchArg *pBlob;
154728 int nBlob;
154729 int memErr = 0;
154730
154731 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
154732 + nArg*sizeof(sqlite3_value*);
154733 pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
154734 if( !pBlob ){
154735 sqlite3_result_error_nomem(ctx);
154736 }else{
154737 int i;
154738 pBlob->magic = RTREE_GEOMETRY_MAGIC;
154739 pBlob->cb = pGeomCtx[0];
154740 pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
154741 pBlob->nParam = nArg;
154742 for(i=0; i<nArg; i++){
154743 pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
154744 if( pBlob->apSqlParam[i]==0 ) memErr = 1;
154745 #ifdef SQLITE_RTREE_INT_ONLY
154746 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
154747 #else
154748 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
154749 #endif
154750 }
154751 if( memErr ){
154752 sqlite3_result_error_nomem(ctx);
154753 rtreeMatchArgFree(pBlob);
154754 }else{
154755 sqlite3_result_blob(ctx, pBlob, nBlob, rtreeMatchArgFree);
154756 }
154757 }
154758 }
154759
154760 /*
154761 ** Register a new geometry function for use with the r-tree MATCH operator.
@@ -155211,10 +155587,4095 @@
155587
155588 #endif /* defined(SQLITE_ENABLE_ICU) */
155589 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
155590
155591 /************** End of fts3_icu.c ********************************************/
155592 /************** Begin file sqlite3ota.c **************************************/
155593 /*
155594 ** 2014 August 30
155595 **
155596 ** The author disclaims copyright to this source code. In place of
155597 ** a legal notice, here is a blessing:
155598 **
155599 ** May you do good and not evil.
155600 ** May you find forgiveness for yourself and forgive others.
155601 ** May you share freely, never taking more than you give.
155602 **
155603 *************************************************************************
155604 **
155605 **
155606 ** OVERVIEW
155607 **
155608 ** The OTA extension requires that the OTA update be packaged as an
155609 ** SQLite database. The tables it expects to find are described in
155610 ** sqlite3ota.h. Essentially, for each table xyz in the target database
155611 ** that the user wishes to write to, a corresponding data_xyz table is
155612 ** created in the OTA database and populated with one row for each row to
155613 ** update, insert or delete from the target table.
155614 **
155615 ** The update proceeds in three stages:
155616 **
155617 ** 1) The database is updated. The modified database pages are written
155618 ** to a *-oal file. A *-oal file is just like a *-wal file, except
155619 ** that it is named "<database>-oal" instead of "<database>-wal".
155620 ** Because regular SQLite clients do not look for file named
155621 ** "<database>-oal", they go on using the original database in
155622 ** rollback mode while the *-oal file is being generated.
155623 **
155624 ** During this stage OTA does not update the database by writing
155625 ** directly to the target tables. Instead it creates "imposter"
155626 ** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
155627 ** to update each b-tree individually. All updates required by each
155628 ** b-tree are completed before moving on to the next, and all
155629 ** updates are done in sorted key order.
155630 **
155631 ** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
155632 ** location using a call to rename(2). Before doing this the OTA
155633 ** module takes an EXCLUSIVE lock on the database file, ensuring
155634 ** that there are no other active readers.
155635 **
155636 ** Once the EXCLUSIVE lock is released, any other database readers
155637 ** detect the new *-wal file and read the database in wal mode. At
155638 ** this point they see the new version of the database - including
155639 ** the updates made as part of the OTA update.
155640 **
155641 ** 3) The new *-wal file is checkpointed. This proceeds in the same way
155642 ** as a regular database checkpoint, except that a single frame is
155643 ** checkpointed each time sqlite3ota_step() is called. If the OTA
155644 ** handle is closed before the entire *-wal file is checkpointed,
155645 ** the checkpoint progress is saved in the OTA database and the
155646 ** checkpoint can be resumed by another OTA client at some point in
155647 ** the future.
155648 **
155649 ** POTENTIAL PROBLEMS
155650 **
155651 ** The rename() call might not be portable. And OTA is not currently
155652 ** syncing the directory after renaming the file.
155653 **
155654 ** When state is saved, any commit to the *-oal file and the commit to
155655 ** the OTA update database are not atomic. So if the power fails at the
155656 ** wrong moment they might get out of sync. As the main database will be
155657 ** committed before the OTA update database this will likely either just
155658 ** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
155659 ** constraint violations).
155660 **
155661 ** If some client does modify the target database mid OTA update, or some
155662 ** other error occurs, the OTA extension will keep throwing errors. It's
155663 ** not really clear how to get out of this state. The system could just
155664 ** by delete the OTA update database and *-oal file and have the device
155665 ** download the update again and start over.
155666 **
155667 ** At present, for an UPDATE, both the new.* and old.* records are
155668 ** collected in the ota_xyz table. And for both UPDATEs and DELETEs all
155669 ** fields are collected. This means we're probably writing a lot more
155670 ** data to disk when saving the state of an ongoing update to the OTA
155671 ** update database than is strictly necessary.
155672 **
155673 */
155674
155675 /* #include <assert.h> */
155676 /* #include <string.h> */
155677 /* #include <stdio.h> */
155678 /* #include <unistd.h> */
155679
155680
155681 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_OTA)
155682 /************** Include sqlite3ota.h in the middle of sqlite3ota.c ***********/
155683 /************** Begin file sqlite3ota.h **************************************/
155684 /*
155685 ** 2014 August 30
155686 **
155687 ** The author disclaims copyright to this source code. In place of
155688 ** a legal notice, here is a blessing:
155689 **
155690 ** May you do good and not evil.
155691 ** May you find forgiveness for yourself and forgive others.
155692 ** May you share freely, never taking more than you give.
155693 **
155694 *************************************************************************
155695 **
155696 ** This file contains the public interface for the OTA extension.
155697 */
155698
155699 /*
155700 ** SUMMARY
155701 **
155702 ** Writing a transaction containing a large number of operations on
155703 ** b-tree indexes that are collectively larger than the available cache
155704 ** memory can be very inefficient.
155705 **
155706 ** The problem is that in order to update a b-tree, the leaf page (at least)
155707 ** containing the entry being inserted or deleted must be modified. If the
155708 ** working set of leaves is larger than the available cache memory, then a
155709 ** single leaf that is modified more than once as part of the transaction
155710 ** may be loaded from or written to the persistent media multiple times.
155711 ** Additionally, because the index updates are likely to be applied in
155712 ** random order, access to pages within the database is also likely to be in
155713 ** random order, which is itself quite inefficient.
155714 **
155715 ** One way to improve the situation is to sort the operations on each index
155716 ** by index key before applying them to the b-tree. This leads to an IO
155717 ** pattern that resembles a single linear scan through the index b-tree,
155718 ** and all but guarantees each modified leaf page is loaded and stored
155719 ** exactly once. SQLite uses this trick to improve the performance of
155720 ** CREATE INDEX commands. This extension allows it to be used to improve
155721 ** the performance of large transactions on existing databases.
155722 **
155723 ** Additionally, this extension allows the work involved in writing the
155724 ** large transaction to be broken down into sub-transactions performed
155725 ** sequentially by separate processes. This is useful if the system cannot
155726 ** guarantee that a single update process will run for long enough to apply
155727 ** the entire update, for example because the update is being applied on a
155728 ** mobile device that is frequently rebooted. Even after the writer process
155729 ** has committed one or more sub-transactions, other database clients continue
155730 ** to read from the original database snapshot. In other words, partially
155731 ** applied transactions are not visible to other clients.
155732 **
155733 ** "OTA" stands for "Over The Air" update. As in a large database update
155734 ** transmitted via a wireless network to a mobile device. A transaction
155735 ** applied using this extension is hence refered to as an "OTA update".
155736 **
155737 **
155738 ** LIMITATIONS
155739 **
155740 ** An "OTA update" transaction is subject to the following limitations:
155741 **
155742 ** * The transaction must consist of INSERT, UPDATE and DELETE operations
155743 ** only.
155744 **
155745 ** * INSERT statements may not use any default values.
155746 **
155747 ** * UPDATE and DELETE statements must identify their target rows by
155748 ** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
155749 ** KEY fields may not be updated or deleted. If the table being written
155750 ** has no PRIMARY KEY, affected rows must be identified by rowid.
155751 **
155752 ** * UPDATE statements may not modify PRIMARY KEY columns.
155753 **
155754 ** * No triggers will be fired.
155755 **
155756 ** * No foreign key violations are detected or reported.
155757 **
155758 ** * CHECK constraints are not enforced.
155759 **
155760 ** * No constraint handling mode except for "OR ROLLBACK" is supported.
155761 **
155762 **
155763 ** PREPARATION
155764 **
155765 ** An "OTA update" is stored as a separate SQLite database. A database
155766 ** containing an OTA update is an "OTA database". For each table in the
155767 ** target database to be updated, the OTA database should contain a table
155768 ** named "data_<target name>" containing the same set of columns as the
155769 ** target table, and one more - "ota_control". The data_% table should
155770 ** have no PRIMARY KEY or UNIQUE constraints, but each column should have
155771 ** the same type as the corresponding column in the target database.
155772 ** The "ota_control" column should have no type at all. For example, if
155773 ** the target database contains:
155774 **
155775 ** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
155776 **
155777 ** Then the OTA database should contain:
155778 **
155779 ** CREATE TABLE data_t1(a INTEGER, b TEXT, c, ota_control);
155780 **
155781 ** The order of the columns in the data_% table does not matter.
155782 **
155783 ** If the target database table is a virtual table or a table that has no
155784 ** PRIMARY KEY declaration, the data_% table must also contain a column
155785 ** named "ota_rowid". This column is mapped to the tables implicit primary
155786 ** key column - "rowid". Virtual tables for which the "rowid" column does
155787 ** not function like a primary key value cannot be updated using OTA. For
155788 ** example, if the target db contains either of the following:
155789 **
155790 ** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
155791 ** CREATE TABLE x1(a, b)
155792 **
155793 ** then the OTA database should contain:
155794 **
155795 ** CREATE TABLE data_x1(a, b, ota_rowid, ota_control);
155796 **
155797 ** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
155798 ** target table must be present in the input table. For virtual tables,
155799 ** hidden columns are optional - they are updated by OTA if present in
155800 ** the input table, or not otherwise. For example, to write to an fts4
155801 ** table with a hidden languageid column such as:
155802 **
155803 ** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
155804 **
155805 ** Either of the following input table schemas may be used:
155806 **
155807 ** CREATE TABLE data_ft1(a, b, langid, ota_rowid, ota_control);
155808 ** CREATE TABLE data_ft1(a, b, ota_rowid, ota_control);
155809 **
155810 ** For each row to INSERT into the target database as part of the OTA
155811 ** update, the corresponding data_% table should contain a single record
155812 ** with the "ota_control" column set to contain integer value 0. The
155813 ** other columns should be set to the values that make up the new record
155814 ** to insert.
155815 **
155816 ** If the target database table has an INTEGER PRIMARY KEY, it is not
155817 ** possible to insert a NULL value into the IPK column. Attempting to
155818 ** do so results in an SQLITE_MISMATCH error.
155819 **
155820 ** For each row to DELETE from the target database as part of the OTA
155821 ** update, the corresponding data_% table should contain a single record
155822 ** with the "ota_control" column set to contain integer value 1. The
155823 ** real primary key values of the row to delete should be stored in the
155824 ** corresponding columns of the data_% table. The values stored in the
155825 ** other columns are not used.
155826 **
155827 ** For each row to UPDATE from the target database as part of the OTA
155828 ** update, the corresponding data_% table should contain a single record
155829 ** with the "ota_control" column set to contain a value of type text.
155830 ** The real primary key values identifying the row to update should be
155831 ** stored in the corresponding columns of the data_% table row, as should
155832 ** the new values of all columns being update. The text value in the
155833 ** "ota_control" column must contain the same number of characters as
155834 ** there are columns in the target database table, and must consist entirely
155835 ** of 'x' and '.' characters (or in some special cases 'd' - see below). For
155836 ** each column that is being updated, the corresponding character is set to
155837 ** 'x'. For those that remain as they are, the corresponding character of the
155838 ** ota_control value should be set to '.'. For example, given the tables
155839 ** above, the update statement:
155840 **
155841 ** UPDATE t1 SET c = 'usa' WHERE a = 4;
155842 **
155843 ** is represented by the data_t1 row created by:
155844 **
155845 ** INSERT INTO data_t1(a, b, c, ota_control) VALUES(4, NULL, 'usa', '..x');
155846 **
155847 ** Instead of an 'x' character, characters of the ota_control value specified
155848 ** for UPDATEs may also be set to 'd'. In this case, instead of updating the
155849 ** target table with the value stored in the corresponding data_% column, the
155850 ** user-defined SQL function "ota_delta()" is invoked and the result stored in
155851 ** the target table column. ota_delta() is invoked with two arguments - the
155852 ** original value currently stored in the target table column and the
155853 ** value specified in the data_xxx table.
155854 **
155855 ** For example, this row:
155856 **
155857 ** INSERT INTO data_t1(a, b, c, ota_control) VALUES(4, NULL, 'usa', '..d');
155858 **
155859 ** is similar to an UPDATE statement such as:
155860 **
155861 ** UPDATE t1 SET c = ota_delta(c, 'usa') WHERE a = 4;
155862 **
155863 ** If the target database table is a virtual table or a table with no PRIMARY
155864 ** KEY, the ota_control value should not include a character corresponding
155865 ** to the ota_rowid value. For example, this:
155866 **
155867 ** INSERT INTO data_ft1(a, b, ota_rowid, ota_control)
155868 ** VALUES(NULL, 'usa', 12, '.x');
155869 **
155870 ** causes a result similar to:
155871 **
155872 ** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
155873 **
155874 ** The data_xxx tables themselves should have no PRIMARY KEY declarations.
155875 ** However, OTA is more efficient if reading the rows in from each data_xxx
155876 ** table in "rowid" order is roughly the same as reading them sorted by
155877 ** the PRIMARY KEY of the corresponding target database table. In other
155878 ** words, rows should be sorted using the destination table PRIMARY KEY
155879 ** fields before they are inserted into the data_xxx tables.
155880 **
155881 ** USAGE
155882 **
155883 ** The API declared below allows an application to apply an OTA update
155884 ** stored on disk to an existing target database. Essentially, the
155885 ** application:
155886 **
155887 ** 1) Opens an OTA handle using the sqlite3ota_open() function.
155888 **
155889 ** 2) Registers any required virtual table modules with the database
155890 ** handle returned by sqlite3ota_db(). Also, if required, register
155891 ** the ota_delta() implementation.
155892 **
155893 ** 3) Calls the sqlite3ota_step() function one or more times on
155894 ** the new handle. Each call to sqlite3ota_step() performs a single
155895 ** b-tree operation, so thousands of calls may be required to apply
155896 ** a complete update.
155897 **
155898 ** 4) Calls sqlite3ota_close() to close the OTA update handle. If
155899 ** sqlite3ota_step() has been called enough times to completely
155900 ** apply the update to the target database, then the OTA database
155901 ** is marked as fully applied. Otherwise, the state of the OTA
155902 ** update application is saved in the OTA database for later
155903 ** resumption.
155904 **
155905 ** See comments below for more detail on APIs.
155906 **
155907 ** If an update is only partially applied to the target database by the
155908 ** time sqlite3ota_close() is called, various state information is saved
155909 ** within the OTA database. This allows subsequent processes to automatically
155910 ** resume the OTA update from where it left off.
155911 **
155912 ** To remove all OTA extension state information, returning an OTA database
155913 ** to its original contents, it is sufficient to drop all tables that begin
155914 ** with the prefix "ota_"
155915 **
155916 ** DATABASE LOCKING
155917 **
155918 ** An OTA update may not be applied to a database in WAL mode. Attempting
155919 ** to do so is an error (SQLITE_ERROR).
155920 **
155921 ** While an OTA handle is open, a SHARED lock may be held on the target
155922 ** database file. This means it is possible for other clients to read the
155923 ** database, but not to write it.
155924 **
155925 ** If an OTA update is started and then suspended before it is completed,
155926 ** then an external client writes to the database, then attempting to resume
155927 ** the suspended OTA update is also an error (SQLITE_BUSY).
155928 */
155929
155930 #ifndef _SQLITE3OTA_H
155931 #define _SQLITE3OTA_H
155932
155933
155934 typedef struct sqlite3ota sqlite3ota;
155935
155936 /*
155937 ** Open an OTA handle.
155938 **
155939 ** Argument zTarget is the path to the target database. Argument zOta is
155940 ** the path to the OTA database. Each call to this function must be matched
155941 ** by a call to sqlite3ota_close(). When opening the databases, OTA passes
155942 ** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
155943 ** or zOta begin with "file:", it will be interpreted as an SQLite
155944 ** database URI, not a regular file name.
155945 **
155946 ** If the zState argument is passed a NULL value, the OTA extension stores
155947 ** the current state of the update (how many rows have been updated, which
155948 ** indexes are yet to be updated etc.) within the OTA database itself. This
155949 ** can be convenient, as it means that the OTA application does not need to
155950 ** organize removing a separate state file after the update is concluded.
155951 ** Or, if zState is non-NULL, it must be a path to a database file in which
155952 ** the OTA extension can store the state of the update.
155953 **
155954 ** When resuming an OTA update, the zState argument must be passed the same
155955 ** value as when the OTA update was started.
155956 **
155957 ** Once the OTA update is finished, the OTA extension does not
155958 ** automatically remove any zState database file, even if it created it.
155959 **
155960 ** By default, OTA uses the default VFS to access the files on disk. To
155961 ** use a VFS other than the default, an SQLite "file:" URI containing a
155962 ** "vfs=..." option may be passed as the zTarget option.
155963 **
155964 ** IMPORTANT NOTE FOR ZIPVFS USERS: The OTA extension works with all of
155965 ** SQLite's built-in VFSs, including the multiplexor VFS. However it does
155966 ** not work out of the box with zipvfs. Refer to the comment describing
155967 ** the zipvfs_create_vfs() API below for details on using OTA with zipvfs.
155968 */
155969 SQLITE_API sqlite3ota *SQLITE_STDCALL sqlite3ota_open(
155970 const char *zTarget,
155971 const char *zOta,
155972 const char *zState
155973 );
155974
155975 /*
155976 ** Internally, each OTA connection uses a separate SQLite database
155977 ** connection to access the target and ota update databases. This
155978 ** API allows the application direct access to these database handles.
155979 **
155980 ** The first argument passed to this function must be a valid, open, OTA
155981 ** handle. The second argument should be passed zero to access the target
155982 ** database handle, or non-zero to access the ota update database handle.
155983 ** Accessing the underlying database handles may be useful in the
155984 ** following scenarios:
155985 **
155986 ** * If any target tables are virtual tables, it may be necessary to
155987 ** call sqlite3_create_module() on the target database handle to
155988 ** register the required virtual table implementations.
155989 **
155990 ** * If the data_xxx tables in the OTA source database are virtual
155991 ** tables, the application may need to call sqlite3_create_module() on
155992 ** the ota update db handle to any required virtual table
155993 ** implementations.
155994 **
155995 ** * If the application uses the "ota_delta()" feature described above,
155996 ** it must use sqlite3_create_function() or similar to register the
155997 ** ota_delta() implementation with the target database handle.
155998 **
155999 ** If an error has occurred, either while opening or stepping the OTA object,
156000 ** this function may return NULL. The error code and message may be collected
156001 ** when sqlite3ota_close() is called.
156002 */
156003 SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3ota_db(sqlite3ota*, int bOta);
156004
156005 /*
156006 ** Do some work towards applying the OTA update to the target db.
156007 **
156008 ** Return SQLITE_DONE if the update has been completely applied, or
156009 ** SQLITE_OK if no error occurs but there remains work to do to apply
156010 ** the OTA update. If an error does occur, some other error code is
156011 ** returned.
156012 **
156013 ** Once a call to sqlite3ota_step() has returned a value other than
156014 ** SQLITE_OK, all subsequent calls on the same OTA handle are no-ops
156015 ** that immediately return the same value.
156016 */
156017 SQLITE_API int SQLITE_STDCALL sqlite3ota_step(sqlite3ota *pOta);
156018
156019 /*
156020 ** Close an OTA handle.
156021 **
156022 ** If the OTA update has been completely applied, mark the OTA database
156023 ** as fully applied. Otherwise, assuming no error has occurred, save the
156024 ** current state of the OTA update appliation to the OTA database.
156025 **
156026 ** If an error has already occurred as part of an sqlite3ota_step()
156027 ** or sqlite3ota_open() call, or if one occurs within this function, an
156028 ** SQLite error code is returned. Additionally, *pzErrmsg may be set to
156029 ** point to a buffer containing a utf-8 formatted English language error
156030 ** message. It is the responsibility of the caller to eventually free any
156031 ** such buffer using sqlite3_free().
156032 **
156033 ** Otherwise, if no error occurs, this function returns SQLITE_OK if the
156034 ** update has been partially applied, or SQLITE_DONE if it has been
156035 ** completely applied.
156036 */
156037 SQLITE_API int SQLITE_STDCALL sqlite3ota_close(sqlite3ota *pOta, char **pzErrmsg);
156038
156039 /*
156040 ** Return the total number of key-value operations (inserts, deletes or
156041 ** updates) that have been performed on the target database since the
156042 ** current OTA update was started.
156043 */
156044 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3ota_progress(sqlite3ota *pOta);
156045
156046 /*
156047 ** Create an OTA VFS named zName that accesses the underlying file-system
156048 ** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
156049 ** then the new OTA VFS uses the default system VFS to access the file-system.
156050 ** The new object is registered as a non-default VFS with SQLite before
156051 ** returning.
156052 **
156053 ** Part of the OTA implementation uses a custom VFS object. Usually, this
156054 ** object is created and deleted automatically by OTA.
156055 **
156056 ** The exception is for applications that also use zipvfs. In this case,
156057 ** the custom VFS must be explicitly created by the user before the OTA
156058 ** handle is opened. The OTA VFS should be installed so that the zipvfs
156059 ** VFS uses the OTA VFS, which in turn uses any other VFS layers in use
156060 ** (for example multiplexor) to access the file-system. For example,
156061 ** to assemble an OTA enabled VFS stack that uses both zipvfs and
156062 ** multiplexor (error checking omitted):
156063 **
156064 ** // Create a VFS named "multiplex" (not the default).
156065 ** sqlite3_multiplex_initialize(0, 0);
156066 **
156067 ** // Create an ota VFS named "ota" that uses multiplexor. If the
156068 ** // second argument were replaced with NULL, the "ota" VFS would
156069 ** // access the file-system via the system default VFS, bypassing the
156070 ** // multiplexor.
156071 ** sqlite3ota_create_vfs("ota", "multiplex");
156072 **
156073 ** // Create a zipvfs VFS named "zipvfs" that uses ota.
156074 ** zipvfs_create_vfs_v3("zipvfs", "ota", 0, xCompressorAlgorithmDetector);
156075 **
156076 ** // Make zipvfs the default VFS.
156077 ** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
156078 **
156079 ** Because the default VFS created above includes a OTA functionality, it
156080 ** may be used by OTA clients. Attempting to use OTA with a zipvfs VFS stack
156081 ** that does not include the OTA layer results in an error.
156082 **
156083 ** The overhead of adding the "ota" VFS to the system is negligible for
156084 ** non-OTA users. There is no harm in an application accessing the
156085 ** file-system via "ota" all the time, even if it only uses OTA functionality
156086 ** occasionally.
156087 */
156088 SQLITE_API int SQLITE_STDCALL sqlite3ota_create_vfs(const char *zName, const char *zParent);
156089
156090 /*
156091 ** Deregister and destroy an OTA vfs created by an earlier call to
156092 ** sqlite3ota_create_vfs().
156093 **
156094 ** VFS objects are not reference counted. If a VFS object is destroyed
156095 ** before all database handles that use it have been closed, the results
156096 ** are undefined.
156097 */
156098 SQLITE_API void SQLITE_STDCALL sqlite3ota_destroy_vfs(const char *zName);
156099
156100 #endif /* _SQLITE3OTA_H */
156101
156102
156103 /************** End of sqlite3ota.h ******************************************/
156104 /************** Continuing where we left off in sqlite3ota.c *****************/
156105
156106 /* Maximum number of prepared UPDATE statements held by this module */
156107 #define SQLITE_OTA_UPDATE_CACHESIZE 16
156108
156109 /*
156110 ** Swap two objects of type TYPE.
156111 */
156112 #if !defined(SQLITE_AMALGAMATION)
156113 # define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
156114 #endif
156115
156116 /*
156117 ** The ota_state table is used to save the state of a partially applied
156118 ** update so that it can be resumed later. The table consists of integer
156119 ** keys mapped to values as follows:
156120 **
156121 ** OTA_STATE_STAGE:
156122 ** May be set to integer values 1, 2, 4 or 5. As follows:
156123 ** 1: the *-ota file is currently under construction.
156124 ** 2: the *-ota file has been constructed, but not yet moved
156125 ** to the *-wal path.
156126 ** 4: the checkpoint is underway.
156127 ** 5: the ota update has been checkpointed.
156128 **
156129 ** OTA_STATE_TBL:
156130 ** Only valid if STAGE==1. The target database name of the table
156131 ** currently being written.
156132 **
156133 ** OTA_STATE_IDX:
156134 ** Only valid if STAGE==1. The target database name of the index
156135 ** currently being written, or NULL if the main table is currently being
156136 ** updated.
156137 **
156138 ** OTA_STATE_ROW:
156139 ** Only valid if STAGE==1. Number of rows already processed for the current
156140 ** table/index.
156141 **
156142 ** OTA_STATE_PROGRESS:
156143 ** Total number of sqlite3ota_step() calls made so far as part of this
156144 ** ota update.
156145 **
156146 ** OTA_STATE_CKPT:
156147 ** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
156148 ** header created by recovering the *-wal file. This is used to detect
156149 ** cases when another client appends frames to the *-wal file in the
156150 ** middle of an incremental checkpoint (an incremental checkpoint cannot
156151 ** be continued if this happens).
156152 **
156153 ** OTA_STATE_COOKIE:
156154 ** Valid if STAGE==1. The current change-counter cookie value in the
156155 ** target db file.
156156 **
156157 ** OTA_STATE_OALSZ:
156158 ** Valid if STAGE==1. The size in bytes of the *-oal file.
156159 */
156160 #define OTA_STATE_STAGE 1
156161 #define OTA_STATE_TBL 2
156162 #define OTA_STATE_IDX 3
156163 #define OTA_STATE_ROW 4
156164 #define OTA_STATE_PROGRESS 5
156165 #define OTA_STATE_CKPT 6
156166 #define OTA_STATE_COOKIE 7
156167 #define OTA_STATE_OALSZ 8
156168
156169 #define OTA_STAGE_OAL 1
156170 #define OTA_STAGE_MOVE 2
156171 #define OTA_STAGE_CAPTURE 3
156172 #define OTA_STAGE_CKPT 4
156173 #define OTA_STAGE_DONE 5
156174
156175
156176 #define OTA_CREATE_STATE \
156177 "CREATE TABLE IF NOT EXISTS %s.ota_state(k INTEGER PRIMARY KEY, v)"
156178
156179 typedef struct OtaFrame OtaFrame;
156180 typedef struct OtaObjIter OtaObjIter;
156181 typedef struct OtaState OtaState;
156182 typedef struct ota_vfs ota_vfs;
156183 typedef struct ota_file ota_file;
156184 typedef struct OtaUpdateStmt OtaUpdateStmt;
156185
156186 #if !defined(SQLITE_AMALGAMATION)
156187 typedef unsigned int u32;
156188 typedef unsigned char u8;
156189 typedef sqlite3_int64 i64;
156190 #endif
156191
156192 /*
156193 ** These values must match the values defined in wal.c for the equivalent
156194 ** locks. These are not magic numbers as they are part of the SQLite file
156195 ** format.
156196 */
156197 #define WAL_LOCK_WRITE 0
156198 #define WAL_LOCK_CKPT 1
156199 #define WAL_LOCK_READ0 3
156200
156201 /*
156202 ** A structure to store values read from the ota_state table in memory.
156203 */
156204 struct OtaState {
156205 int eStage;
156206 char *zTbl;
156207 char *zIdx;
156208 i64 iWalCksum;
156209 int nRow;
156210 i64 nProgress;
156211 u32 iCookie;
156212 i64 iOalSz;
156213 };
156214
156215 struct OtaUpdateStmt {
156216 char *zMask; /* Copy of update mask used with pUpdate */
156217 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
156218 OtaUpdateStmt *pNext;
156219 };
156220
156221 /*
156222 ** An iterator of this type is used to iterate through all objects in
156223 ** the target database that require updating. For each such table, the
156224 ** iterator visits, in order:
156225 **
156226 ** * the table itself,
156227 ** * each index of the table (zero or more points to visit), and
156228 ** * a special "cleanup table" state.
156229 **
156230 ** abIndexed:
156231 ** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
156232 ** it points to an array of flags nTblCol elements in size. The flag is
156233 ** set for each column that is either a part of the PK or a part of an
156234 ** index. Or clear otherwise.
156235 **
156236 */
156237 struct OtaObjIter {
156238 sqlite3_stmt *pTblIter; /* Iterate through tables */
156239 sqlite3_stmt *pIdxIter; /* Index iterator */
156240 int nTblCol; /* Size of azTblCol[] array */
156241 char **azTblCol; /* Array of unquoted target column names */
156242 char **azTblType; /* Array of target column types */
156243 int *aiSrcOrder; /* src table col -> target table col */
156244 u8 *abTblPk; /* Array of flags, set on target PK columns */
156245 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
156246 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
156247 int eType; /* Table type - an OTA_PK_XXX value */
156248
156249 /* Output variables. zTbl==0 implies EOF. */
156250 int bCleanup; /* True in "cleanup" state */
156251 const char *zTbl; /* Name of target db table */
156252 const char *zIdx; /* Name of target db index (or null) */
156253 int iTnum; /* Root page of current object */
156254 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
156255 int bUnique; /* Current index is unique */
156256
156257 /* Statements created by otaObjIterPrepareAll() */
156258 int nCol; /* Number of columns in current object */
156259 sqlite3_stmt *pSelect; /* Source data */
156260 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
156261 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
156262 sqlite3_stmt *pTmpInsert; /* Insert into ota_tmp_$zTbl */
156263
156264 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
156265 OtaUpdateStmt *pOtaUpdate;
156266 };
156267
156268 /*
156269 ** Values for OtaObjIter.eType
156270 **
156271 ** 0: Table does not exist (error)
156272 ** 1: Table has an implicit rowid.
156273 ** 2: Table has an explicit IPK column.
156274 ** 3: Table has an external PK index.
156275 ** 4: Table is WITHOUT ROWID.
156276 ** 5: Table is a virtual table.
156277 */
156278 #define OTA_PK_NOTABLE 0
156279 #define OTA_PK_NONE 1
156280 #define OTA_PK_IPK 2
156281 #define OTA_PK_EXTERNAL 3
156282 #define OTA_PK_WITHOUT_ROWID 4
156283 #define OTA_PK_VTAB 5
156284
156285
156286 /*
156287 ** Within the OTA_STAGE_OAL stage, each call to sqlite3ota_step() performs
156288 ** one of the following operations.
156289 */
156290 #define OTA_INSERT 1 /* Insert on a main table b-tree */
156291 #define OTA_DELETE 2 /* Delete a row from a main table b-tree */
156292 #define OTA_IDX_DELETE 3 /* Delete a row from an aux. index b-tree */
156293 #define OTA_IDX_INSERT 4 /* Insert on an aux. index b-tree */
156294 #define OTA_UPDATE 5 /* Update a row in a main table b-tree */
156295
156296
156297 /*
156298 ** A single step of an incremental checkpoint - frame iWalFrame of the wal
156299 ** file should be copied to page iDbPage of the database file.
156300 */
156301 struct OtaFrame {
156302 u32 iDbPage;
156303 u32 iWalFrame;
156304 };
156305
156306 /*
156307 ** OTA handle.
156308 */
156309 struct sqlite3ota {
156310 int eStage; /* Value of OTA_STATE_STAGE field */
156311 sqlite3 *dbMain; /* target database handle */
156312 sqlite3 *dbOta; /* ota database handle */
156313 char *zTarget; /* Path to target db */
156314 char *zOta; /* Path to ota db */
156315 char *zState; /* Path to state db (or NULL if zOta) */
156316 char zStateDb[5]; /* Db name for state ("stat" or "main") */
156317 int rc; /* Value returned by last ota_step() call */
156318 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
156319 int nStep; /* Rows processed for current object */
156320 int nProgress; /* Rows processed for all objects */
156321 OtaObjIter objiter; /* Iterator for skipping through tbl/idx */
156322 const char *zVfsName; /* Name of automatically created ota vfs */
156323 ota_file *pTargetFd; /* File handle open on target db */
156324 i64 iOalSz;
156325
156326 /* The following state variables are used as part of the incremental
156327 ** checkpoint stage (eStage==OTA_STAGE_CKPT). See comments surrounding
156328 ** function otaSetupCheckpoint() for details. */
156329 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
156330 u32 mLock;
156331 int nFrame; /* Entries in aFrame[] array */
156332 int nFrameAlloc; /* Allocated size of aFrame[] array */
156333 OtaFrame *aFrame;
156334 int pgsz;
156335 u8 *aBuf;
156336 i64 iWalCksum;
156337 };
156338
156339 /*
156340 ** An ota VFS is implemented using an instance of this structure.
156341 */
156342 struct ota_vfs {
156343 sqlite3_vfs base; /* ota VFS shim methods */
156344 sqlite3_vfs *pRealVfs; /* Underlying VFS */
156345 sqlite3_mutex *mutex; /* Mutex to protect pMain */
156346 ota_file *pMain; /* Linked list of main db files */
156347 };
156348
156349 /*
156350 ** Each file opened by an ota VFS is represented by an instance of
156351 ** the following structure.
156352 */
156353 struct ota_file {
156354 sqlite3_file base; /* sqlite3_file methods */
156355 sqlite3_file *pReal; /* Underlying file handle */
156356 ota_vfs *pOtaVfs; /* Pointer to the ota_vfs object */
156357 sqlite3ota *pOta; /* Pointer to ota object (ota target only) */
156358
156359 int openFlags; /* Flags this file was opened with */
156360 u32 iCookie; /* Cookie value for main db files */
156361 u8 iWriteVer; /* "write-version" value for main db files */
156362
156363 int nShm; /* Number of entries in apShm[] array */
156364 char **apShm; /* Array of mmap'd *-shm regions */
156365 char *zDel; /* Delete this when closing file */
156366
156367 const char *zWal; /* Wal filename for this main db file */
156368 ota_file *pWalFd; /* Wal file descriptor for this main db */
156369 ota_file *pMainNext; /* Next MAIN_DB file */
156370 };
156371
156372
156373 /*
156374 ** Prepare the SQL statement in buffer zSql against database handle db.
156375 ** If successful, set *ppStmt to point to the new statement and return
156376 ** SQLITE_OK.
156377 **
156378 ** Otherwise, if an error does occur, set *ppStmt to NULL and return
156379 ** an SQLite error code. Additionally, set output variable *pzErrmsg to
156380 ** point to a buffer containing an error message. It is the responsibility
156381 ** of the caller to (eventually) free this buffer using sqlite3_free().
156382 */
156383 static int prepareAndCollectError(
156384 sqlite3 *db,
156385 sqlite3_stmt **ppStmt,
156386 char **pzErrmsg,
156387 const char *zSql
156388 ){
156389 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
156390 if( rc!=SQLITE_OK ){
156391 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
156392 *ppStmt = 0;
156393 }
156394 return rc;
156395 }
156396
156397 /*
156398 ** Reset the SQL statement passed as the first argument. Return a copy
156399 ** of the value returned by sqlite3_reset().
156400 **
156401 ** If an error has occurred, then set *pzErrmsg to point to a buffer
156402 ** containing an error message. It is the responsibility of the caller
156403 ** to eventually free this buffer using sqlite3_free().
156404 */
156405 static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
156406 int rc = sqlite3_reset(pStmt);
156407 if( rc!=SQLITE_OK ){
156408 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
156409 }
156410 return rc;
156411 }
156412
156413 /*
156414 ** Unless it is NULL, argument zSql points to a buffer allocated using
156415 ** sqlite3_malloc containing an SQL statement. This function prepares the SQL
156416 ** statement against database db and frees the buffer. If statement
156417 ** compilation is successful, *ppStmt is set to point to the new statement
156418 ** handle and SQLITE_OK is returned.
156419 **
156420 ** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
156421 ** returned. In this case, *pzErrmsg may also be set to point to an error
156422 ** message. It is the responsibility of the caller to free this error message
156423 ** buffer using sqlite3_free().
156424 **
156425 ** If argument zSql is NULL, this function assumes that an OOM has occurred.
156426 ** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
156427 */
156428 static int prepareFreeAndCollectError(
156429 sqlite3 *db,
156430 sqlite3_stmt **ppStmt,
156431 char **pzErrmsg,
156432 char *zSql
156433 ){
156434 int rc;
156435 assert( *pzErrmsg==0 );
156436 if( zSql==0 ){
156437 rc = SQLITE_NOMEM;
156438 *ppStmt = 0;
156439 }else{
156440 rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
156441 sqlite3_free(zSql);
156442 }
156443 return rc;
156444 }
156445
156446 /*
156447 ** Free the OtaObjIter.azTblCol[] and OtaObjIter.abTblPk[] arrays allocated
156448 ** by an earlier call to otaObjIterCacheTableInfo().
156449 */
156450 static void otaObjIterFreeCols(OtaObjIter *pIter){
156451 int i;
156452 for(i=0; i<pIter->nTblCol; i++){
156453 sqlite3_free(pIter->azTblCol[i]);
156454 sqlite3_free(pIter->azTblType[i]);
156455 }
156456 sqlite3_free(pIter->azTblCol);
156457 pIter->azTblCol = 0;
156458 pIter->azTblType = 0;
156459 pIter->aiSrcOrder = 0;
156460 pIter->abTblPk = 0;
156461 pIter->abNotNull = 0;
156462 pIter->nTblCol = 0;
156463 pIter->eType = 0; /* Invalid value */
156464 }
156465
156466 /*
156467 ** Finalize all statements and free all allocations that are specific to
156468 ** the current object (table/index pair).
156469 */
156470 static void otaObjIterClearStatements(OtaObjIter *pIter){
156471 OtaUpdateStmt *pUp;
156472
156473 sqlite3_finalize(pIter->pSelect);
156474 sqlite3_finalize(pIter->pInsert);
156475 sqlite3_finalize(pIter->pDelete);
156476 sqlite3_finalize(pIter->pTmpInsert);
156477 pUp = pIter->pOtaUpdate;
156478 while( pUp ){
156479 OtaUpdateStmt *pTmp = pUp->pNext;
156480 sqlite3_finalize(pUp->pUpdate);
156481 sqlite3_free(pUp);
156482 pUp = pTmp;
156483 }
156484
156485 pIter->pSelect = 0;
156486 pIter->pInsert = 0;
156487 pIter->pDelete = 0;
156488 pIter->pOtaUpdate = 0;
156489 pIter->pTmpInsert = 0;
156490 pIter->nCol = 0;
156491 }
156492
156493 /*
156494 ** Clean up any resources allocated as part of the iterator object passed
156495 ** as the only argument.
156496 */
156497 static void otaObjIterFinalize(OtaObjIter *pIter){
156498 otaObjIterClearStatements(pIter);
156499 sqlite3_finalize(pIter->pTblIter);
156500 sqlite3_finalize(pIter->pIdxIter);
156501 otaObjIterFreeCols(pIter);
156502 memset(pIter, 0, sizeof(OtaObjIter));
156503 }
156504
156505 /*
156506 ** Advance the iterator to the next position.
156507 **
156508 ** If no error occurs, SQLITE_OK is returned and the iterator is left
156509 ** pointing to the next entry. Otherwise, an error code and message is
156510 ** left in the OTA handle passed as the first argument. A copy of the
156511 ** error code is returned.
156512 */
156513 static int otaObjIterNext(sqlite3ota *p, OtaObjIter *pIter){
156514 int rc = p->rc;
156515 if( rc==SQLITE_OK ){
156516
156517 /* Free any SQLite statements used while processing the previous object */
156518 otaObjIterClearStatements(pIter);
156519 if( pIter->zIdx==0 ){
156520 rc = sqlite3_exec(p->dbMain,
156521 "DROP TRIGGER IF EXISTS temp.ota_insert_tr;"
156522 "DROP TRIGGER IF EXISTS temp.ota_update1_tr;"
156523 "DROP TRIGGER IF EXISTS temp.ota_update2_tr;"
156524 "DROP TRIGGER IF EXISTS temp.ota_delete_tr;"
156525 , 0, 0, &p->zErrmsg
156526 );
156527 }
156528
156529 if( rc==SQLITE_OK ){
156530 if( pIter->bCleanup ){
156531 otaObjIterFreeCols(pIter);
156532 pIter->bCleanup = 0;
156533 rc = sqlite3_step(pIter->pTblIter);
156534 if( rc!=SQLITE_ROW ){
156535 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
156536 pIter->zTbl = 0;
156537 }else{
156538 pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
156539 rc = pIter->zTbl ? SQLITE_OK : SQLITE_NOMEM;
156540 }
156541 }else{
156542 if( pIter->zIdx==0 ){
156543 sqlite3_stmt *pIdx = pIter->pIdxIter;
156544 rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
156545 }
156546 if( rc==SQLITE_OK ){
156547 rc = sqlite3_step(pIter->pIdxIter);
156548 if( rc!=SQLITE_ROW ){
156549 rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
156550 pIter->bCleanup = 1;
156551 pIter->zIdx = 0;
156552 }else{
156553 pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
156554 pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
156555 pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
156556 rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
156557 }
156558 }
156559 }
156560 }
156561 }
156562
156563 if( rc!=SQLITE_OK ){
156564 otaObjIterFinalize(pIter);
156565 p->rc = rc;
156566 }
156567 return rc;
156568 }
156569
156570 /*
156571 ** Initialize the iterator structure passed as the second argument.
156572 **
156573 ** If no error occurs, SQLITE_OK is returned and the iterator is left
156574 ** pointing to the first entry. Otherwise, an error code and message is
156575 ** left in the OTA handle passed as the first argument. A copy of the
156576 ** error code is returned.
156577 */
156578 static int otaObjIterFirst(sqlite3ota *p, OtaObjIter *pIter){
156579 int rc;
156580 memset(pIter, 0, sizeof(OtaObjIter));
156581
156582 rc = prepareAndCollectError(p->dbOta, &pIter->pTblIter, &p->zErrmsg,
156583 "SELECT substr(name, 6) FROM sqlite_master "
156584 "WHERE type='table' AND name LIKE 'data_%'"
156585 );
156586
156587 if( rc==SQLITE_OK ){
156588 rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
156589 "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
156590 " FROM main.sqlite_master "
156591 " WHERE type='index' AND tbl_name = ?"
156592 );
156593 }
156594
156595 pIter->bCleanup = 1;
156596 p->rc = rc;
156597 return otaObjIterNext(p, pIter);
156598 }
156599
156600 /*
156601 ** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
156602 ** an error code is stored in the OTA handle passed as the first argument.
156603 **
156604 ** If an error has already occurred (p->rc is already set to something other
156605 ** than SQLITE_OK), then this function returns NULL without modifying the
156606 ** stored error code. In this case it still calls sqlite3_free() on any
156607 ** printf() parameters associated with %z conversions.
156608 */
156609 static char *otaMPrintf(sqlite3ota *p, const char *zFmt, ...){
156610 char *zSql = 0;
156611 va_list ap;
156612 va_start(ap, zFmt);
156613 zSql = sqlite3_vmprintf(zFmt, ap);
156614 if( p->rc==SQLITE_OK ){
156615 if( zSql==0 ) p->rc = SQLITE_NOMEM;
156616 }else{
156617 sqlite3_free(zSql);
156618 zSql = 0;
156619 }
156620 va_end(ap);
156621 return zSql;
156622 }
156623
156624 /*
156625 ** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
156626 ** arguments are the usual subsitution values. This function performs
156627 ** the printf() style substitutions and executes the result as an SQL
156628 ** statement on the OTA handles database.
156629 **
156630 ** If an error occurs, an error code and error message is stored in the
156631 ** OTA handle. If an error has already occurred when this function is
156632 ** called, it is a no-op.
156633 */
156634 static int otaMPrintfExec(sqlite3ota *p, sqlite3 *db, const char *zFmt, ...){
156635 va_list ap;
156636 va_start(ap, zFmt);
156637 char *zSql = sqlite3_vmprintf(zFmt, ap);
156638 if( p->rc==SQLITE_OK ){
156639 if( zSql==0 ){
156640 p->rc = SQLITE_NOMEM;
156641 }else{
156642 p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
156643 }
156644 }
156645 sqlite3_free(zSql);
156646 va_end(ap);
156647 return p->rc;
156648 }
156649
156650 /*
156651 ** Attempt to allocate and return a pointer to a zeroed block of nByte
156652 ** bytes.
156653 **
156654 ** If an error (i.e. an OOM condition) occurs, return NULL and leave an
156655 ** error code in the ota handle passed as the first argument. Or, if an
156656 ** error has already occurred when this function is called, return NULL
156657 ** immediately without attempting the allocation or modifying the stored
156658 ** error code.
156659 */
156660 static void *otaMalloc(sqlite3ota *p, int nByte){
156661 void *pRet = 0;
156662 if( p->rc==SQLITE_OK ){
156663 assert( nByte>0 );
156664 pRet = sqlite3_malloc(nByte);
156665 if( pRet==0 ){
156666 p->rc = SQLITE_NOMEM;
156667 }else{
156668 memset(pRet, 0, nByte);
156669 }
156670 }
156671 return pRet;
156672 }
156673
156674
156675 /*
156676 ** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
156677 ** there is room for at least nCol elements. If an OOM occurs, store an
156678 ** error code in the OTA handle passed as the first argument.
156679 */
156680 static void otaAllocateIterArrays(sqlite3ota *p, OtaObjIter *pIter, int nCol){
156681 int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
156682 char **azNew;
156683
156684 azNew = (char**)otaMalloc(p, nByte);
156685 if( azNew ){
156686 pIter->azTblCol = azNew;
156687 pIter->azTblType = &azNew[nCol];
156688 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
156689 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
156690 pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
156691 pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
156692 }
156693 }
156694
156695 /*
156696 ** The first argument must be a nul-terminated string. This function
156697 ** returns a copy of the string in memory obtained from sqlite3_malloc().
156698 ** It is the responsibility of the caller to eventually free this memory
156699 ** using sqlite3_free().
156700 **
156701 ** If an OOM condition is encountered when attempting to allocate memory,
156702 ** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
156703 ** if the allocation succeeds, (*pRc) is left unchanged.
156704 */
156705 static char *otaStrndup(const char *zStr, int *pRc){
156706 char *zRet = 0;
156707
156708 assert( *pRc==SQLITE_OK );
156709 if( zStr ){
156710 int nCopy = strlen(zStr) + 1;
156711 zRet = (char*)sqlite3_malloc(nCopy);
156712 if( zRet ){
156713 memcpy(zRet, zStr, nCopy);
156714 }else{
156715 *pRc = SQLITE_NOMEM;
156716 }
156717 }
156718
156719 return zRet;
156720 }
156721
156722 /*
156723 ** Finalize the statement passed as the second argument.
156724 **
156725 ** If the sqlite3_finalize() call indicates that an error occurs, and the
156726 ** ota handle error code is not already set, set the error code and error
156727 ** message accordingly.
156728 */
156729 static void otaFinalize(sqlite3ota *p, sqlite3_stmt *pStmt){
156730 sqlite3 *db = sqlite3_db_handle(pStmt);
156731 int rc = sqlite3_finalize(pStmt);
156732 if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
156733 p->rc = rc;
156734 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
156735 }
156736 }
156737
156738 /* Determine the type of a table.
156739 **
156740 ** peType is of type (int*), a pointer to an output parameter of type
156741 ** (int). This call sets the output parameter as follows, depending
156742 ** on the type of the table specified by parameters dbName and zTbl.
156743 **
156744 ** OTA_PK_NOTABLE: No such table.
156745 ** OTA_PK_NONE: Table has an implicit rowid.
156746 ** OTA_PK_IPK: Table has an explicit IPK column.
156747 ** OTA_PK_EXTERNAL: Table has an external PK index.
156748 ** OTA_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
156749 ** OTA_PK_VTAB: Table is a virtual table.
156750 **
156751 ** Argument *piPk is also of type (int*), and also points to an output
156752 ** parameter. Unless the table has an external primary key index
156753 ** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
156754 ** if the table does have an external primary key index, then *piPk
156755 ** is set to the root page number of the primary key index before
156756 ** returning.
156757 **
156758 ** ALGORITHM:
156759 **
156760 ** if( no entry exists in sqlite_master ){
156761 ** return OTA_PK_NOTABLE
156762 ** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
156763 ** return OTA_PK_VTAB
156764 ** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
156765 ** if( the index that is the pk exists in sqlite_master ){
156766 ** *piPK = rootpage of that index.
156767 ** return OTA_PK_EXTERNAL
156768 ** }else{
156769 ** return OTA_PK_WITHOUT_ROWID
156770 ** }
156771 ** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
156772 ** return OTA_PK_IPK
156773 ** }else{
156774 ** return OTA_PK_NONE
156775 ** }
156776 */
156777 static void otaTableType(
156778 sqlite3ota *p,
156779 const char *zTab,
156780 int *peType,
156781 int *piTnum,
156782 int *piPk
156783 ){
156784 /*
156785 ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
156786 ** 1) PRAGMA index_list = ?
156787 ** 2) SELECT count(*) FROM sqlite_master where name=%Q
156788 ** 3) PRAGMA table_info = ?
156789 */
156790 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
156791
156792 *peType = OTA_PK_NOTABLE;
156793 *piPk = 0;
156794
156795 assert( p->rc==SQLITE_OK );
156796 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
156797 sqlite3_mprintf(
156798 "SELECT (sql LIKE 'create virtual%%'), rootpage"
156799 " FROM sqlite_master"
156800 " WHERE name=%Q", zTab
156801 ));
156802 if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
156803 /* Either an error, or no such table. */
156804 goto otaTableType_end;
156805 }
156806 if( sqlite3_column_int(aStmt[0], 0) ){
156807 *peType = OTA_PK_VTAB; /* virtual table */
156808 goto otaTableType_end;
156809 }
156810 *piTnum = sqlite3_column_int(aStmt[0], 1);
156811
156812 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
156813 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
156814 );
156815 if( p->rc ) goto otaTableType_end;
156816 while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
156817 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
156818 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
156819 if( zOrig && zIdx && zOrig[0]=='p' ){
156820 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
156821 sqlite3_mprintf(
156822 "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
156823 ));
156824 if( p->rc==SQLITE_OK ){
156825 if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
156826 *piPk = sqlite3_column_int(aStmt[2], 0);
156827 *peType = OTA_PK_EXTERNAL;
156828 }else{
156829 *peType = OTA_PK_WITHOUT_ROWID;
156830 }
156831 }
156832 goto otaTableType_end;
156833 }
156834 }
156835
156836 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
156837 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
156838 );
156839 if( p->rc==SQLITE_OK ){
156840 while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
156841 if( sqlite3_column_int(aStmt[3],5)>0 ){
156842 *peType = OTA_PK_IPK; /* explicit IPK column */
156843 goto otaTableType_end;
156844 }
156845 }
156846 *peType = OTA_PK_NONE;
156847 }
156848
156849 otaTableType_end: {
156850 int i;
156851 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
156852 otaFinalize(p, aStmt[i]);
156853 }
156854 }
156855 }
156856
156857 /*
156858 ** This is a helper function for otaObjIterCacheTableInfo(). It populates
156859 ** the pIter->abIndexed[] array.
156860 */
156861 static void otaObjIterCacheIndexedCols(sqlite3ota *p, OtaObjIter *pIter){
156862 sqlite3_stmt *pList = 0;
156863 int bIndex = 0;
156864
156865 if( p->rc==SQLITE_OK ){
156866 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
156867 p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
156868 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
156869 );
156870 }
156871
156872 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
156873 const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
156874 sqlite3_stmt *pXInfo = 0;
156875 if( zIdx==0 ) break;
156876 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
156877 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
156878 );
156879 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
156880 int iCid = sqlite3_column_int(pXInfo, 1);
156881 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
156882 }
156883 otaFinalize(p, pXInfo);
156884 bIndex = 1;
156885 }
156886
156887 otaFinalize(p, pList);
156888 if( bIndex==0 ) pIter->abIndexed = 0;
156889 }
156890
156891
156892 /*
156893 ** If they are not already populated, populate the pIter->azTblCol[],
156894 ** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
156895 ** the table (not index) that the iterator currently points to.
156896 **
156897 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
156898 ** an error does occur, an error code and error message are also left in
156899 ** the OTA handle.
156900 */
156901 static int otaObjIterCacheTableInfo(sqlite3ota *p, OtaObjIter *pIter){
156902 if( pIter->azTblCol==0 ){
156903 sqlite3_stmt *pStmt = 0;
156904 int nCol = 0;
156905 int i; /* for() loop iterator variable */
156906 int bOtaRowid = 0; /* If input table has column "ota_rowid" */
156907 int iOrder = 0;
156908 int iTnum = 0;
156909
156910 /* Figure out the type of table this step will deal with. */
156911 assert( pIter->eType==0 );
156912 otaTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
156913 if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_NOTABLE ){
156914 p->rc = SQLITE_ERROR;
156915 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
156916 }
156917 if( p->rc ) return p->rc;
156918 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
156919
156920 assert( pIter->eType==OTA_PK_NONE || pIter->eType==OTA_PK_IPK
156921 || pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_WITHOUT_ROWID
156922 || pIter->eType==OTA_PK_VTAB
156923 );
156924
156925 /* Populate the azTblCol[] and nTblCol variables based on the columns
156926 ** of the input table. Ignore any input table columns that begin with
156927 ** "ota_". */
156928 p->rc = prepareFreeAndCollectError(p->dbOta, &pStmt, &p->zErrmsg,
156929 sqlite3_mprintf("SELECT * FROM 'data_%q'", pIter->zTbl)
156930 );
156931 if( p->rc==SQLITE_OK ){
156932 nCol = sqlite3_column_count(pStmt);
156933 otaAllocateIterArrays(p, pIter, nCol);
156934 }
156935 for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
156936 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
156937 if( sqlite3_strnicmp("ota_", zName, 4) ){
156938 char *zCopy = otaStrndup(zName, &p->rc);
156939 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
156940 pIter->azTblCol[pIter->nTblCol++] = zCopy;
156941 }
156942 else if( 0==sqlite3_stricmp("ota_rowid", zName) ){
156943 bOtaRowid = 1;
156944 }
156945 }
156946 sqlite3_finalize(pStmt);
156947 pStmt = 0;
156948
156949 if( p->rc==SQLITE_OK
156950 && bOtaRowid!=(pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
156951 ){
156952 p->rc = SQLITE_ERROR;
156953 p->zErrmsg = sqlite3_mprintf(
156954 "table data_%q %s ota_rowid column", pIter->zTbl,
156955 (bOtaRowid ? "may not have" : "requires")
156956 );
156957 }
156958
156959 /* Check that all non-HIDDEN columns in the destination table are also
156960 ** present in the input table. Populate the abTblPk[], azTblType[] and
156961 ** aiTblOrder[] arrays at the same time. */
156962 if( p->rc==SQLITE_OK ){
156963 p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
156964 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
156965 );
156966 }
156967 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
156968 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
156969 if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
156970 for(i=iOrder; i<pIter->nTblCol; i++){
156971 if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
156972 }
156973 if( i==pIter->nTblCol ){
156974 p->rc = SQLITE_ERROR;
156975 p->zErrmsg = sqlite3_mprintf("column missing from data_%q: %s",
156976 pIter->zTbl, zName
156977 );
156978 }else{
156979 int iPk = sqlite3_column_int(pStmt, 5);
156980 int bNotNull = sqlite3_column_int(pStmt, 3);
156981 const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
156982
156983 if( i!=iOrder ){
156984 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
156985 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
156986 }
156987
156988 pIter->azTblType[iOrder] = otaStrndup(zType, &p->rc);
156989 pIter->abTblPk[iOrder] = (iPk!=0);
156990 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
156991 iOrder++;
156992 }
156993 }
156994
156995 otaFinalize(p, pStmt);
156996 otaObjIterCacheIndexedCols(p, pIter);
156997 assert( pIter->eType!=OTA_PK_VTAB || pIter->abIndexed==0 );
156998 }
156999
157000 return p->rc;
157001 }
157002
157003 /*
157004 ** This function constructs and returns a pointer to a nul-terminated
157005 ** string containing some SQL clause or list based on one or more of the
157006 ** column names currently stored in the pIter->azTblCol[] array.
157007 */
157008 static char *otaObjIterGetCollist(
157009 sqlite3ota *p, /* OTA object */
157010 OtaObjIter *pIter /* Object iterator for column names */
157011 ){
157012 char *zList = 0;
157013 const char *zSep = "";
157014 int i;
157015 for(i=0; i<pIter->nTblCol; i++){
157016 const char *z = pIter->azTblCol[i];
157017 zList = otaMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
157018 zSep = ", ";
157019 }
157020 return zList;
157021 }
157022
157023 /*
157024 ** This function is used to create a SELECT list (the list of SQL
157025 ** expressions that follows a SELECT keyword) for a SELECT statement
157026 ** used to read from an data_xxx or ota_tmp_xxx table while updating the
157027 ** index object currently indicated by the iterator object passed as the
157028 ** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
157029 ** to obtain the required information.
157030 **
157031 ** If the index is of the following form:
157032 **
157033 ** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
157034 **
157035 ** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
157036 ** "ipk", the returned string is:
157037 **
157038 ** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
157039 **
157040 ** As well as the returned string, three other malloc'd strings are
157041 ** returned via output parameters. As follows:
157042 **
157043 ** pzImposterCols: ...
157044 ** pzImposterPk: ...
157045 ** pzWhere: ...
157046 */
157047 static char *otaObjIterGetIndexCols(
157048 sqlite3ota *p, /* OTA object */
157049 OtaObjIter *pIter, /* Object iterator for column names */
157050 char **pzImposterCols, /* OUT: Columns for imposter table */
157051 char **pzImposterPk, /* OUT: Imposter PK clause */
157052 char **pzWhere, /* OUT: WHERE clause */
157053 int *pnBind /* OUT: Total number of columns */
157054 ){
157055 int rc = p->rc; /* Error code */
157056 int rc2; /* sqlite3_finalize() return code */
157057 char *zRet = 0; /* String to return */
157058 char *zImpCols = 0; /* String to return via *pzImposterCols */
157059 char *zImpPK = 0; /* String to return via *pzImposterPK */
157060 char *zWhere = 0; /* String to return via *pzWhere */
157061 int nBind = 0; /* Value to return via *pnBind */
157062 const char *zCom = ""; /* Set to ", " later on */
157063 const char *zAnd = ""; /* Set to " AND " later on */
157064 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */
157065
157066 if( rc==SQLITE_OK ){
157067 assert( p->zErrmsg==0 );
157068 rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
157069 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
157070 );
157071 }
157072
157073 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
157074 int iCid = sqlite3_column_int(pXInfo, 1);
157075 int bDesc = sqlite3_column_int(pXInfo, 3);
157076 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
157077 const char *zCol;
157078 const char *zType;
157079
157080 if( iCid<0 ){
157081 /* An integer primary key. If the table has an explicit IPK, use
157082 ** its name. Otherwise, use "ota_rowid". */
157083 if( pIter->eType==OTA_PK_IPK ){
157084 int i;
157085 for(i=0; pIter->abTblPk[i]==0; i++);
157086 assert( i<pIter->nTblCol );
157087 zCol = pIter->azTblCol[i];
157088 }else{
157089 zCol = "ota_rowid";
157090 }
157091 zType = "INTEGER";
157092 }else{
157093 zCol = pIter->azTblCol[iCid];
157094 zType = pIter->azTblType[iCid];
157095 }
157096
157097 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
157098 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
157099 const char *zOrder = (bDesc ? " DESC" : "");
157100 zImpPK = sqlite3_mprintf("%z%s\"ota_imp_%d%w\"%s",
157101 zImpPK, zCom, nBind, zCol, zOrder
157102 );
157103 }
157104 zImpCols = sqlite3_mprintf("%z%s\"ota_imp_%d%w\" %s COLLATE %Q",
157105 zImpCols, zCom, nBind, zCol, zType, zCollate
157106 );
157107 zWhere = sqlite3_mprintf(
157108 "%z%s\"ota_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
157109 );
157110 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
157111 zCom = ", ";
157112 zAnd = " AND ";
157113 nBind++;
157114 }
157115
157116 rc2 = sqlite3_finalize(pXInfo);
157117 if( rc==SQLITE_OK ) rc = rc2;
157118
157119 if( rc!=SQLITE_OK ){
157120 sqlite3_free(zRet);
157121 sqlite3_free(zImpCols);
157122 sqlite3_free(zImpPK);
157123 sqlite3_free(zWhere);
157124 zRet = 0;
157125 zImpCols = 0;
157126 zImpPK = 0;
157127 zWhere = 0;
157128 p->rc = rc;
157129 }
157130
157131 *pzImposterCols = zImpCols;
157132 *pzImposterPk = zImpPK;
157133 *pzWhere = zWhere;
157134 *pnBind = nBind;
157135 return zRet;
157136 }
157137
157138 /*
157139 ** Assuming the current table columns are "a", "b" and "c", and the zObj
157140 ** paramter is passed "old", return a string of the form:
157141 **
157142 ** "old.a, old.b, old.b"
157143 **
157144 ** With the column names escaped.
157145 **
157146 ** For tables with implicit rowids - OTA_PK_EXTERNAL and OTA_PK_NONE, append
157147 ** the text ", old._rowid_" to the returned value.
157148 */
157149 static char *otaObjIterGetOldlist(
157150 sqlite3ota *p,
157151 OtaObjIter *pIter,
157152 const char *zObj
157153 ){
157154 char *zList = 0;
157155 if( p->rc==SQLITE_OK && pIter->abIndexed ){
157156 const char *zS = "";
157157 int i;
157158 for(i=0; i<pIter->nTblCol; i++){
157159 if( pIter->abIndexed[i] ){
157160 const char *zCol = pIter->azTblCol[i];
157161 zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
157162 }else{
157163 zList = sqlite3_mprintf("%z%sNULL", zList, zS);
157164 }
157165 zS = ", ";
157166 if( zList==0 ){
157167 p->rc = SQLITE_NOMEM;
157168 break;
157169 }
157170 }
157171
157172 /* For a table with implicit rowids, append "old._rowid_" to the list. */
157173 if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
157174 zList = otaMPrintf(p, "%z, %s._rowid_", zList, zObj);
157175 }
157176 }
157177 return zList;
157178 }
157179
157180 /*
157181 ** Return an expression that can be used in a WHERE clause to match the
157182 ** primary key of the current table. For example, if the table is:
157183 **
157184 ** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
157185 **
157186 ** Return the string:
157187 **
157188 ** "b = ?1 AND c = ?2"
157189 */
157190 static char *otaObjIterGetWhere(
157191 sqlite3ota *p,
157192 OtaObjIter *pIter
157193 ){
157194 char *zList = 0;
157195 if( pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE ){
157196 zList = otaMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
157197 }else if( pIter->eType==OTA_PK_EXTERNAL ){
157198 const char *zSep = "";
157199 int i;
157200 for(i=0; i<pIter->nTblCol; i++){
157201 if( pIter->abTblPk[i] ){
157202 zList = otaMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
157203 zSep = " AND ";
157204 }
157205 }
157206 zList = otaMPrintf(p,
157207 "_rowid_ = (SELECT id FROM ota_imposter2 WHERE %z)", zList
157208 );
157209
157210 }else{
157211 const char *zSep = "";
157212 int i;
157213 for(i=0; i<pIter->nTblCol; i++){
157214 if( pIter->abTblPk[i] ){
157215 const char *zCol = pIter->azTblCol[i];
157216 zList = otaMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
157217 zSep = " AND ";
157218 }
157219 }
157220 }
157221 return zList;
157222 }
157223
157224 /*
157225 ** The SELECT statement iterating through the keys for the current object
157226 ** (p->objiter.pSelect) currently points to a valid row. However, there
157227 ** is something wrong with the ota_control value in the ota_control value
157228 ** stored in the (p->nCol+1)'th column. Set the error code and error message
157229 ** of the OTA handle to something reflecting this.
157230 */
157231 static void otaBadControlError(sqlite3ota *p){
157232 p->rc = SQLITE_ERROR;
157233 p->zErrmsg = sqlite3_mprintf("invalid ota_control value");
157234 }
157235
157236
157237 /*
157238 ** Return a nul-terminated string containing the comma separated list of
157239 ** assignments that should be included following the "SET" keyword of
157240 ** an UPDATE statement used to update the table object that the iterator
157241 ** passed as the second argument currently points to if the ota_control
157242 ** column of the data_xxx table entry is set to zMask.
157243 **
157244 ** The memory for the returned string is obtained from sqlite3_malloc().
157245 ** It is the responsibility of the caller to eventually free it using
157246 ** sqlite3_free().
157247 **
157248 ** If an OOM error is encountered when allocating space for the new
157249 ** string, an error code is left in the ota handle passed as the first
157250 ** argument and NULL is returned. Or, if an error has already occurred
157251 ** when this function is called, NULL is returned immediately, without
157252 ** attempting the allocation or modifying the stored error code.
157253 */
157254 static char *otaObjIterGetSetlist(
157255 sqlite3ota *p,
157256 OtaObjIter *pIter,
157257 const char *zMask
157258 ){
157259 char *zList = 0;
157260 if( p->rc==SQLITE_OK ){
157261 int i;
157262
157263 if( strlen(zMask)!=pIter->nTblCol ){
157264 otaBadControlError(p);
157265 }else{
157266 const char *zSep = "";
157267 for(i=0; i<pIter->nTblCol; i++){
157268 char c = zMask[pIter->aiSrcOrder[i]];
157269 if( c=='x' ){
157270 zList = otaMPrintf(p, "%z%s\"%w\"=?%d",
157271 zList, zSep, pIter->azTblCol[i], i+1
157272 );
157273 zSep = ", ";
157274 }
157275 if( c=='d' ){
157276 zList = otaMPrintf(p, "%z%s\"%w\"=ota_delta(\"%w\", ?%d)",
157277 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
157278 );
157279 zSep = ", ";
157280 }
157281 }
157282 }
157283 }
157284 return zList;
157285 }
157286
157287 /*
157288 ** Return a nul-terminated string consisting of nByte comma separated
157289 ** "?" expressions. For example, if nByte is 3, return a pointer to
157290 ** a buffer containing the string "?,?,?".
157291 **
157292 ** The memory for the returned string is obtained from sqlite3_malloc().
157293 ** It is the responsibility of the caller to eventually free it using
157294 ** sqlite3_free().
157295 **
157296 ** If an OOM error is encountered when allocating space for the new
157297 ** string, an error code is left in the ota handle passed as the first
157298 ** argument and NULL is returned. Or, if an error has already occurred
157299 ** when this function is called, NULL is returned immediately, without
157300 ** attempting the allocation or modifying the stored error code.
157301 */
157302 static char *otaObjIterGetBindlist(sqlite3ota *p, int nBind){
157303 char *zRet = 0;
157304 int nByte = nBind*2 + 1;
157305
157306 zRet = (char*)otaMalloc(p, nByte);
157307 if( zRet ){
157308 int i;
157309 for(i=0; i<nBind; i++){
157310 zRet[i*2] = '?';
157311 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
157312 }
157313 }
157314 return zRet;
157315 }
157316
157317 /*
157318 ** The iterator currently points to a table (not index) of type
157319 ** OTA_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
157320 ** declaration for the corresponding imposter table. For example,
157321 ** if the iterator points to a table created as:
157322 **
157323 ** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
157324 **
157325 ** this function returns:
157326 **
157327 ** PRIMARY KEY("b", "a" DESC)
157328 */
157329 static char *otaWithoutRowidPK(sqlite3ota *p, OtaObjIter *pIter){
157330 char *z = 0;
157331 assert( pIter->zIdx==0 );
157332 if( p->rc==SQLITE_OK ){
157333 const char *zSep = "PRIMARY KEY(";
157334 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
157335 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
157336
157337 p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
157338 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
157339 );
157340 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
157341 const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
157342 if( zOrig && strcmp(zOrig, "pk")==0 ){
157343 const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
157344 if( zIdx ){
157345 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
157346 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
157347 );
157348 }
157349 break;
157350 }
157351 }
157352 otaFinalize(p, pXList);
157353
157354 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
157355 if( sqlite3_column_int(pXInfo, 5) ){
157356 /* int iCid = sqlite3_column_int(pXInfo, 0); */
157357 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
157358 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
157359 z = otaMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
157360 zSep = ", ";
157361 }
157362 }
157363 z = otaMPrintf(p, "%z)", z);
157364 otaFinalize(p, pXInfo);
157365 }
157366 return z;
157367 }
157368
157369 /*
157370 ** This function creates the second imposter table used when writing to
157371 ** a table b-tree where the table has an external primary key. If the
157372 ** iterator passed as the second argument does not currently point to
157373 ** a table (not index) with an external primary key, this function is a
157374 ** no-op.
157375 **
157376 ** Assuming the iterator does point to a table with an external PK, this
157377 ** function creates a WITHOUT ROWID imposter table named "ota_imposter2"
157378 ** used to access that PK index. For example, if the target table is
157379 ** declared as follows:
157380 **
157381 ** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
157382 **
157383 ** then the imposter table schema is:
157384 **
157385 ** CREATE TABLE ota_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
157386 **
157387 */
157388 static void otaCreateImposterTable2(sqlite3ota *p, OtaObjIter *pIter){
157389 if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_EXTERNAL ){
157390 int tnum = pIter->iPkTnum; /* Root page of PK index */
157391 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
157392 const char *zIdx = 0; /* Name of PK index */
157393 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
157394 const char *zComma = "";
157395 char *zCols = 0; /* Used to build up list of table cols */
157396 char *zPk = 0; /* Used to build up table PK declaration */
157397
157398 /* Figure out the name of the primary key index for the current table.
157399 ** This is needed for the argument to "PRAGMA index_xinfo". Set
157400 ** zIdx to point to a nul-terminated string containing this name. */
157401 p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
157402 "SELECT name FROM sqlite_master WHERE rootpage = ?"
157403 );
157404 if( p->rc==SQLITE_OK ){
157405 sqlite3_bind_int(pQuery, 1, tnum);
157406 if( SQLITE_ROW==sqlite3_step(pQuery) ){
157407 zIdx = (const char*)sqlite3_column_text(pQuery, 0);
157408 }
157409 }
157410 if( zIdx ){
157411 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
157412 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
157413 );
157414 }
157415 otaFinalize(p, pQuery);
157416
157417 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
157418 int bKey = sqlite3_column_int(pXInfo, 5);
157419 if( bKey ){
157420 int iCid = sqlite3_column_int(pXInfo, 1);
157421 int bDesc = sqlite3_column_int(pXInfo, 3);
157422 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
157423 zCols = otaMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
157424 iCid, pIter->azTblType[iCid], zCollate
157425 );
157426 zPk = otaMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
157427 zComma = ", ";
157428 }
157429 }
157430 zCols = otaMPrintf(p, "%z, id INTEGER", zCols);
157431 otaFinalize(p, pXInfo);
157432
157433 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
157434 otaMPrintfExec(p, p->dbMain,
157435 "CREATE TABLE ota_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
157436 zCols, zPk
157437 );
157438 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
157439 }
157440 }
157441
157442 /*
157443 ** If an error has already occurred when this function is called, it
157444 ** immediately returns zero (without doing any work). Or, if an error
157445 ** occurs during the execution of this function, it sets the error code
157446 ** in the sqlite3ota object indicated by the first argument and returns
157447 ** zero.
157448 **
157449 ** The iterator passed as the second argument is guaranteed to point to
157450 ** a table (not an index) when this function is called. This function
157451 ** attempts to create any imposter table required to write to the main
157452 ** table b-tree of the table before returning. Non-zero is returned if
157453 ** an imposter table are created, or zero otherwise.
157454 **
157455 ** An imposter table is required in all cases except OTA_PK_VTAB. Only
157456 ** virtual tables are written to directly. The imposter table has the
157457 ** same schema as the actual target table (less any UNIQUE constraints).
157458 ** More precisely, the "same schema" means the same columns, types,
157459 ** collation sequences. For tables that do not have an external PRIMARY
157460 ** KEY, it also means the same PRIMARY KEY declaration.
157461 */
157462 static void otaCreateImposterTable(sqlite3ota *p, OtaObjIter *pIter){
157463 if( p->rc==SQLITE_OK && pIter->eType!=OTA_PK_VTAB ){
157464 int tnum = pIter->iTnum;
157465 const char *zComma = "";
157466 char *zSql = 0;
157467 int iCol;
157468 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
157469
157470 for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
157471 const char *zPk = "";
157472 const char *zCol = pIter->azTblCol[iCol];
157473 const char *zColl = 0;
157474
157475 p->rc = sqlite3_table_column_metadata(
157476 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
157477 );
157478
157479 if( pIter->eType==OTA_PK_IPK && pIter->abTblPk[iCol] ){
157480 /* If the target table column is an "INTEGER PRIMARY KEY", add
157481 ** "PRIMARY KEY" to the imposter table column declaration. */
157482 zPk = "PRIMARY KEY ";
157483 }
157484 zSql = otaMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
157485 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
157486 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
157487 );
157488 zComma = ", ";
157489 }
157490
157491 if( pIter->eType==OTA_PK_WITHOUT_ROWID ){
157492 char *zPk = otaWithoutRowidPK(p, pIter);
157493 if( zPk ){
157494 zSql = otaMPrintf(p, "%z, %z", zSql, zPk);
157495 }
157496 }
157497
157498 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
157499 otaMPrintfExec(p, p->dbMain, "CREATE TABLE \"ota_imp_%w\"(%z)%s",
157500 pIter->zTbl, zSql,
157501 (pIter->eType==OTA_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
157502 );
157503 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
157504 }
157505 }
157506
157507 /*
157508 ** Prepare a statement used to insert rows into the "ota_tmp_xxx" table.
157509 ** Specifically a statement of the form:
157510 **
157511 ** INSERT INTO ota_tmp_xxx VALUES(?, ?, ? ...);
157512 **
157513 ** The number of bound variables is equal to the number of columns in
157514 ** the target table, plus one (for the ota_control column), plus one more
157515 ** (for the ota_rowid column) if the target table is an implicit IPK or
157516 ** virtual table.
157517 */
157518 static void otaObjIterPrepareTmpInsert(
157519 sqlite3ota *p,
157520 OtaObjIter *pIter,
157521 const char *zCollist,
157522 const char *zOtaRowid
157523 ){
157524 int bOtaRowid = (pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE);
157525 char *zBind = otaObjIterGetBindlist(p, pIter->nTblCol + 1 + bOtaRowid);
157526 if( zBind ){
157527 assert( pIter->pTmpInsert==0 );
157528 p->rc = prepareFreeAndCollectError(
157529 p->dbOta, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
157530 "INSERT INTO %s.'ota_tmp_%q'(ota_control,%s%s) VALUES(%z)",
157531 p->zStateDb, pIter->zTbl, zCollist, zOtaRowid, zBind
157532 ));
157533 }
157534 }
157535
157536 static void otaTmpInsertFunc(
157537 sqlite3_context *pCtx,
157538 int nVal,
157539 sqlite3_value **apVal
157540 ){
157541 sqlite3ota *p = sqlite3_user_data(pCtx);
157542 int rc = SQLITE_OK;
157543 int i;
157544
157545 for(i=0; rc==SQLITE_OK && i<nVal; i++){
157546 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
157547 }
157548 if( rc==SQLITE_OK ){
157549 sqlite3_step(p->objiter.pTmpInsert);
157550 rc = sqlite3_reset(p->objiter.pTmpInsert);
157551 }
157552
157553 if( rc!=SQLITE_OK ){
157554 sqlite3_result_error_code(pCtx, rc);
157555 }
157556 }
157557
157558 /*
157559 ** Ensure that the SQLite statement handles required to update the
157560 ** target database object currently indicated by the iterator passed
157561 ** as the second argument are available.
157562 */
157563 static int otaObjIterPrepareAll(
157564 sqlite3ota *p,
157565 OtaObjIter *pIter,
157566 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
157567 ){
157568 assert( pIter->bCleanup==0 );
157569 if( pIter->pSelect==0 && otaObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
157570 const int tnum = pIter->iTnum;
157571 char *zCollist = 0; /* List of indexed columns */
157572 char **pz = &p->zErrmsg;
157573 const char *zIdx = pIter->zIdx;
157574 char *zLimit = 0;
157575
157576 if( nOffset ){
157577 zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
157578 if( !zLimit ) p->rc = SQLITE_NOMEM;
157579 }
157580
157581 if( zIdx ){
157582 const char *zTbl = pIter->zTbl;
157583 char *zImposterCols = 0; /* Columns for imposter table */
157584 char *zImposterPK = 0; /* Primary key declaration for imposter */
157585 char *zWhere = 0; /* WHERE clause on PK columns */
157586 char *zBind = 0;
157587 int nBind = 0;
157588
157589 assert( pIter->eType!=OTA_PK_VTAB );
157590 zCollist = otaObjIterGetIndexCols(
157591 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
157592 );
157593 zBind = otaObjIterGetBindlist(p, nBind);
157594
157595 /* Create the imposter table used to write to this index. */
157596 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
157597 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
157598 otaMPrintfExec(p, p->dbMain,
157599 "CREATE TABLE \"ota_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
157600 zTbl, zImposterCols, zImposterPK
157601 );
157602 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
157603
157604 /* Create the statement to insert index entries */
157605 pIter->nCol = nBind;
157606 if( p->rc==SQLITE_OK ){
157607 p->rc = prepareFreeAndCollectError(
157608 p->dbMain, &pIter->pInsert, &p->zErrmsg,
157609 sqlite3_mprintf("INSERT INTO \"ota_imp_%w\" VALUES(%s)", zTbl, zBind)
157610 );
157611 }
157612
157613 /* And to delete index entries */
157614 if( p->rc==SQLITE_OK ){
157615 p->rc = prepareFreeAndCollectError(
157616 p->dbMain, &pIter->pDelete, &p->zErrmsg,
157617 sqlite3_mprintf("DELETE FROM \"ota_imp_%w\" WHERE %s", zTbl, zWhere)
157618 );
157619 }
157620
157621 /* Create the SELECT statement to read keys in sorted order */
157622 if( p->rc==SQLITE_OK ){
157623 char *zSql;
157624 if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
157625 zSql = sqlite3_mprintf(
157626 "SELECT %s, ota_control FROM %s.'ota_tmp_%q' ORDER BY %s%s",
157627 zCollist, p->zStateDb, pIter->zTbl,
157628 zCollist, zLimit
157629 );
157630 }else{
157631 zSql = sqlite3_mprintf(
157632 "SELECT %s, ota_control FROM 'data_%q' "
157633 "WHERE typeof(ota_control)='integer' AND ota_control!=1 "
157634 "UNION ALL "
157635 "SELECT %s, ota_control FROM %s.'ota_tmp_%q' "
157636 "ORDER BY %s%s",
157637 zCollist, pIter->zTbl,
157638 zCollist, p->zStateDb, pIter->zTbl,
157639 zCollist, zLimit
157640 );
157641 }
157642 p->rc = prepareFreeAndCollectError(p->dbOta, &pIter->pSelect, pz, zSql);
157643 }
157644
157645 sqlite3_free(zImposterCols);
157646 sqlite3_free(zImposterPK);
157647 sqlite3_free(zWhere);
157648 sqlite3_free(zBind);
157649 }else{
157650 int bOtaRowid = (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE);
157651 const char *zTbl = pIter->zTbl; /* Table this step applies to */
157652 const char *zWrite; /* Imposter table name */
157653
157654 char *zBindings = otaObjIterGetBindlist(p, pIter->nTblCol + bOtaRowid);
157655 char *zWhere = otaObjIterGetWhere(p, pIter);
157656 char *zOldlist = otaObjIterGetOldlist(p, pIter, "old");
157657 char *zNewlist = otaObjIterGetOldlist(p, pIter, "new");
157658
157659 zCollist = otaObjIterGetCollist(p, pIter);
157660 pIter->nCol = pIter->nTblCol;
157661
157662 /* Create the SELECT statement to read keys from data_xxx */
157663 if( p->rc==SQLITE_OK ){
157664 p->rc = prepareFreeAndCollectError(p->dbOta, &pIter->pSelect, pz,
157665 sqlite3_mprintf(
157666 "SELECT %s, ota_control%s FROM 'data_%q'%s",
157667 zCollist, (bOtaRowid ? ", ota_rowid" : ""), zTbl, zLimit
157668 )
157669 );
157670 }
157671
157672 /* Create the imposter table or tables (if required). */
157673 otaCreateImposterTable(p, pIter);
157674 otaCreateImposterTable2(p, pIter);
157675 zWrite = (pIter->eType==OTA_PK_VTAB ? "" : "ota_imp_");
157676
157677 /* Create the INSERT statement to write to the target PK b-tree */
157678 if( p->rc==SQLITE_OK ){
157679 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
157680 sqlite3_mprintf(
157681 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
157682 zWrite, zTbl, zCollist, (bOtaRowid ? ", _rowid_" : ""), zBindings
157683 )
157684 );
157685 }
157686
157687 /* Create the DELETE statement to write to the target PK b-tree */
157688 if( p->rc==SQLITE_OK ){
157689 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
157690 sqlite3_mprintf(
157691 "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
157692 )
157693 );
157694 }
157695
157696 if( pIter->abIndexed ){
157697 const char *zOtaRowid = "";
157698 if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
157699 zOtaRowid = ", ota_rowid";
157700 }
157701
157702 /* Create the ota_tmp_xxx table and the triggers to populate it. */
157703 otaMPrintfExec(p, p->dbOta,
157704 "CREATE TABLE IF NOT EXISTS %s.'ota_tmp_%q' AS "
157705 "SELECT *%s FROM 'data_%q' WHERE 0;"
157706 , p->zStateDb
157707 , zTbl, (pIter->eType==OTA_PK_EXTERNAL ? ", 0 AS ota_rowid" : "")
157708 , zTbl
157709 );
157710
157711 otaMPrintfExec(p, p->dbMain,
157712 "CREATE TEMP TRIGGER ota_delete_tr BEFORE DELETE ON \"%s%w\" "
157713 "BEGIN "
157714 " SELECT ota_tmp_insert(2, %s);"
157715 "END;"
157716
157717 "CREATE TEMP TRIGGER ota_update1_tr BEFORE UPDATE ON \"%s%w\" "
157718 "BEGIN "
157719 " SELECT ota_tmp_insert(2, %s);"
157720 "END;"
157721
157722 "CREATE TEMP TRIGGER ota_update2_tr AFTER UPDATE ON \"%s%w\" "
157723 "BEGIN "
157724 " SELECT ota_tmp_insert(3, %s);"
157725 "END;",
157726 zWrite, zTbl, zOldlist,
157727 zWrite, zTbl, zOldlist,
157728 zWrite, zTbl, zNewlist
157729 );
157730
157731 if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
157732 otaMPrintfExec(p, p->dbMain,
157733 "CREATE TEMP TRIGGER ota_insert_tr AFTER INSERT ON \"%s%w\" "
157734 "BEGIN "
157735 " SELECT ota_tmp_insert(0, %s);"
157736 "END;",
157737 zWrite, zTbl, zNewlist
157738 );
157739 }
157740
157741 otaObjIterPrepareTmpInsert(p, pIter, zCollist, zOtaRowid);
157742 }
157743
157744 sqlite3_free(zWhere);
157745 sqlite3_free(zOldlist);
157746 sqlite3_free(zNewlist);
157747 sqlite3_free(zBindings);
157748 }
157749 sqlite3_free(zCollist);
157750 sqlite3_free(zLimit);
157751 }
157752
157753 return p->rc;
157754 }
157755
157756 /*
157757 ** Set output variable *ppStmt to point to an UPDATE statement that may
157758 ** be used to update the imposter table for the main table b-tree of the
157759 ** table object that pIter currently points to, assuming that the
157760 ** ota_control column of the data_xyz table contains zMask.
157761 **
157762 ** If the zMask string does not specify any columns to update, then this
157763 ** is not an error. Output variable *ppStmt is set to NULL in this case.
157764 */
157765 static int otaGetUpdateStmt(
157766 sqlite3ota *p, /* OTA handle */
157767 OtaObjIter *pIter, /* Object iterator */
157768 const char *zMask, /* ota_control value ('x.x.') */
157769 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
157770 ){
157771 OtaUpdateStmt **pp;
157772 OtaUpdateStmt *pUp = 0;
157773 int nUp = 0;
157774
157775 /* In case an error occurs */
157776 *ppStmt = 0;
157777
157778 /* Search for an existing statement. If one is found, shift it to the front
157779 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
157780 ** to the number of statements currently in the cache and pUp to the
157781 ** last object in the list. */
157782 for(pp=&pIter->pOtaUpdate; *pp; pp=&((*pp)->pNext)){
157783 pUp = *pp;
157784 if( strcmp(pUp->zMask, zMask)==0 ){
157785 *pp = pUp->pNext;
157786 pUp->pNext = pIter->pOtaUpdate;
157787 pIter->pOtaUpdate = pUp;
157788 *ppStmt = pUp->pUpdate;
157789 return SQLITE_OK;
157790 }
157791 nUp++;
157792 }
157793 assert( pUp==0 || pUp->pNext==0 );
157794
157795 if( nUp>=SQLITE_OTA_UPDATE_CACHESIZE ){
157796 for(pp=&pIter->pOtaUpdate; *pp!=pUp; pp=&((*pp)->pNext));
157797 *pp = 0;
157798 sqlite3_finalize(pUp->pUpdate);
157799 pUp->pUpdate = 0;
157800 }else{
157801 pUp = (OtaUpdateStmt*)otaMalloc(p, sizeof(OtaUpdateStmt)+pIter->nTblCol+1);
157802 }
157803
157804 if( pUp ){
157805 char *zWhere = otaObjIterGetWhere(p, pIter);
157806 char *zSet = otaObjIterGetSetlist(p, pIter, zMask);
157807 char *zUpdate = 0;
157808
157809 pUp->zMask = (char*)&pUp[1];
157810 memcpy(pUp->zMask, zMask, pIter->nTblCol);
157811 pUp->pNext = pIter->pOtaUpdate;
157812 pIter->pOtaUpdate = pUp;
157813
157814 if( zSet ){
157815 const char *zPrefix = "";
157816
157817 if( pIter->eType!=OTA_PK_VTAB ) zPrefix = "ota_imp_";
157818 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
157819 zPrefix, pIter->zTbl, zSet, zWhere
157820 );
157821 p->rc = prepareFreeAndCollectError(
157822 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
157823 );
157824 *ppStmt = pUp->pUpdate;
157825 }
157826 sqlite3_free(zWhere);
157827 sqlite3_free(zSet);
157828 }
157829
157830 return p->rc;
157831 }
157832
157833 static sqlite3 *otaOpenDbhandle(sqlite3ota *p, const char *zName){
157834 sqlite3 *db = 0;
157835 if( p->rc==SQLITE_OK ){
157836 const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
157837 p->rc = sqlite3_open_v2(zName, &db, flags, p->zVfsName);
157838 if( p->rc ){
157839 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
157840 sqlite3_close(db);
157841 db = 0;
157842 }
157843 }
157844 return db;
157845 }
157846
157847 /*
157848 ** Open the database handle and attach the OTA database as "ota". If an
157849 ** error occurs, leave an error code and message in the OTA handle.
157850 */
157851 static void otaOpenDatabase(sqlite3ota *p){
157852 assert( p->rc==SQLITE_OK );
157853 assert( p->dbMain==0 && p->dbOta==0 );
157854
157855 p->eStage = 0;
157856 p->dbMain = otaOpenDbhandle(p, p->zTarget);
157857 p->dbOta = otaOpenDbhandle(p, p->zOta);
157858
157859 /* If using separate OTA and state databases, attach the state database to
157860 ** the OTA db handle now. */
157861 if( p->zState ){
157862 otaMPrintfExec(p, p->dbOta, "ATTACH %Q AS stat", p->zState);
157863 memcpy(p->zStateDb, "stat", 4);
157864 }else{
157865 memcpy(p->zStateDb, "main", 4);
157866 }
157867
157868 if( p->rc==SQLITE_OK ){
157869 p->rc = sqlite3_create_function(p->dbMain,
157870 "ota_tmp_insert", -1, SQLITE_UTF8, (void*)p, otaTmpInsertFunc, 0, 0
157871 );
157872 }
157873
157874 if( p->rc==SQLITE_OK ){
157875 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_OTA, (void*)p);
157876 }
157877 otaMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
157878
157879 /* Mark the database file just opened as an OTA target database. If
157880 ** this call returns SQLITE_NOTFOUND, then the OTA vfs is not in use.
157881 ** This is an error. */
157882 if( p->rc==SQLITE_OK ){
157883 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_OTA, (void*)p);
157884 }
157885
157886 if( p->rc==SQLITE_NOTFOUND ){
157887 p->rc = SQLITE_ERROR;
157888 p->zErrmsg = sqlite3_mprintf("ota vfs not found");
157889 }
157890 }
157891
157892 /*
157893 ** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
157894 ** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
157895 **
157896 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
157897 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
157898 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
157899 ** three characters, then shorten the suffix on z[] to be the last three
157900 ** characters of the original suffix.
157901 **
157902 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
157903 ** do the suffix shortening regardless of URI parameter.
157904 **
157905 ** Examples:
157906 **
157907 ** test.db-journal => test.nal
157908 ** test.db-wal => test.wal
157909 ** test.db-shm => test.shm
157910 ** test.db-mj7f3319fa => test.9fa
157911 */
157912 static void otaFileSuffix3(const char *zBase, char *z){
157913 #ifdef SQLITE_ENABLE_8_3_NAMES
157914 #if SQLITE_ENABLE_8_3_NAMES<2
157915 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
157916 #endif
157917 {
157918 int i, sz;
157919 sz = sqlite3Strlen30(z);
157920 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
157921 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
157922 }
157923 #endif
157924 }
157925
157926 /*
157927 ** Return the current wal-index header checksum for the target database
157928 ** as a 64-bit integer.
157929 **
157930 ** The checksum is store in the first page of xShmMap memory as an 8-byte
157931 ** blob starting at byte offset 40.
157932 */
157933 static i64 otaShmChecksum(sqlite3ota *p){
157934 i64 iRet = 0;
157935 if( p->rc==SQLITE_OK ){
157936 sqlite3_file *pDb = p->pTargetFd->pReal;
157937 u32 volatile *ptr;
157938 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
157939 if( p->rc==SQLITE_OK ){
157940 iRet = ((i64)ptr[10] << 32) + ptr[11];
157941 }
157942 }
157943 return iRet;
157944 }
157945
157946 /*
157947 ** This function is called as part of initializing or reinitializing an
157948 ** incremental checkpoint.
157949 **
157950 ** It populates the sqlite3ota.aFrame[] array with the set of
157951 ** (wal frame -> db page) copy operations required to checkpoint the
157952 ** current wal file, and obtains the set of shm locks required to safely
157953 ** perform the copy operations directly on the file-system.
157954 **
157955 ** If argument pState is not NULL, then the incremental checkpoint is
157956 ** being resumed. In this case, if the checksum of the wal-index-header
157957 ** following recovery is not the same as the checksum saved in the OtaState
157958 ** object, then the ota handle is set to DONE state. This occurs if some
157959 ** other client appends a transaction to the wal file in the middle of
157960 ** an incremental checkpoint.
157961 */
157962 static void otaSetupCheckpoint(sqlite3ota *p, OtaState *pState){
157963
157964 /* If pState is NULL, then the wal file may not have been opened and
157965 ** recovered. Running a read-statement here to ensure that doing so
157966 ** does not interfere with the "capture" process below. */
157967 if( pState==0 ){
157968 p->eStage = 0;
157969 if( p->rc==SQLITE_OK ){
157970 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
157971 }
157972 }
157973
157974 /* Assuming no error has occurred, run a "restart" checkpoint with the
157975 ** sqlite3ota.eStage variable set to CAPTURE. This turns on the following
157976 ** special behaviour in the ota VFS:
157977 **
157978 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
157979 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
157980 ** proceed with running a passive checkpoint instead of failing).
157981 **
157982 ** * Attempts to read from the *-wal file or write to the database file
157983 ** do not perform any IO. Instead, the frame/page combinations that
157984 ** would be read/written are recorded in the sqlite3ota.aFrame[]
157985 ** array.
157986 **
157987 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
157988 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
157989 ** no-ops. These locks will not be released until the connection
157990 ** is closed.
157991 **
157992 ** * Attempting to xSync() the database file causes an SQLITE_INTERNAL
157993 ** error.
157994 **
157995 ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
157996 ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
157997 ** array populated with a set of (frame -> page) mappings. Because the
157998 ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
157999 ** data from the wal file into the database file according to the
158000 ** contents of aFrame[].
158001 */
158002 if( p->rc==SQLITE_OK ){
158003 int rc2;
158004 p->eStage = OTA_STAGE_CAPTURE;
158005 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
158006 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
158007 }
158008
158009 if( p->rc==SQLITE_OK ){
158010 p->eStage = OTA_STAGE_CKPT;
158011 p->nStep = (pState ? pState->nRow : 0);
158012 p->aBuf = otaMalloc(p, p->pgsz);
158013 p->iWalCksum = otaShmChecksum(p);
158014 }
158015
158016 if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
158017 p->rc = SQLITE_DONE;
158018 p->eStage = OTA_STAGE_DONE;
158019 }
158020 }
158021
158022 /*
158023 ** Called when iAmt bytes are read from offset iOff of the wal file while
158024 ** the ota object is in capture mode. Record the frame number of the frame
158025 ** being read in the aFrame[] array.
158026 */
158027 static int otaCaptureWalRead(sqlite3ota *pOta, i64 iOff, int iAmt){
158028 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
158029 u32 iFrame;
158030
158031 if( pOta->mLock!=mReq ){
158032 pOta->rc = SQLITE_BUSY;
158033 return SQLITE_INTERNAL;
158034 }
158035
158036 pOta->pgsz = iAmt;
158037 if( pOta->nFrame==pOta->nFrameAlloc ){
158038 int nNew = (pOta->nFrameAlloc ? pOta->nFrameAlloc : 64) * 2;
158039 OtaFrame *aNew;
158040 aNew = (OtaFrame*)sqlite3_realloc(pOta->aFrame, nNew * sizeof(OtaFrame));
158041 if( aNew==0 ) return SQLITE_NOMEM;
158042 pOta->aFrame = aNew;
158043 pOta->nFrameAlloc = nNew;
158044 }
158045
158046 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
158047 if( pOta->iMaxFrame<iFrame ) pOta->iMaxFrame = iFrame;
158048 pOta->aFrame[pOta->nFrame].iWalFrame = iFrame;
158049 pOta->aFrame[pOta->nFrame].iDbPage = 0;
158050 pOta->nFrame++;
158051 return SQLITE_OK;
158052 }
158053
158054 /*
158055 ** Called when a page of data is written to offset iOff of the database
158056 ** file while the ota handle is in capture mode. Record the page number
158057 ** of the page being written in the aFrame[] array.
158058 */
158059 static int otaCaptureDbWrite(sqlite3ota *pOta, i64 iOff){
158060 pOta->aFrame[pOta->nFrame-1].iDbPage = (u32)(iOff / pOta->pgsz) + 1;
158061 return SQLITE_OK;
158062 }
158063
158064 /*
158065 ** This is called as part of an incremental checkpoint operation. Copy
158066 ** a single frame of data from the wal file into the database file, as
158067 ** indicated by the OtaFrame object.
158068 */
158069 static void otaCheckpointFrame(sqlite3ota *p, OtaFrame *pFrame){
158070 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
158071 sqlite3_file *pDb = p->pTargetFd->pReal;
158072 i64 iOff;
158073
158074 assert( p->rc==SQLITE_OK );
158075 iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
158076 p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
158077 if( p->rc ) return;
158078
158079 iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
158080 p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
158081 }
158082
158083
158084 /*
158085 ** Take an EXCLUSIVE lock on the database file.
158086 */
158087 static void otaLockDatabase(sqlite3ota *p){
158088 sqlite3_file *pReal = p->pTargetFd->pReal;
158089 assert( p->rc==SQLITE_OK );
158090 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
158091 if( p->rc==SQLITE_OK ){
158092 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
158093 }
158094 }
158095
158096 /*
158097 ** The OTA handle is currently in OTA_STAGE_OAL state, with a SHARED lock
158098 ** on the database file. This proc moves the *-oal file to the *-wal path,
158099 ** then reopens the database file (this time in vanilla, non-oal, WAL mode).
158100 ** If an error occurs, leave an error code and error message in the ota
158101 ** handle.
158102 */
158103 static void otaMoveOalFile(sqlite3ota *p){
158104 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
158105
158106 char *zWal = sqlite3_mprintf("%s-wal", zBase);
158107 char *zOal = sqlite3_mprintf("%s-oal", zBase);
158108
158109 assert( p->eStage==OTA_STAGE_MOVE );
158110 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
158111 if( zWal==0 || zOal==0 ){
158112 p->rc = SQLITE_NOMEM;
158113 }else{
158114 /* Move the *-oal file to *-wal. At this point connection p->db is
158115 ** holding a SHARED lock on the target database file (because it is
158116 ** in WAL mode). So no other connection may be writing the db.
158117 **
158118 ** In order to ensure that there are no database readers, an EXCLUSIVE
158119 ** lock is obtained here before the *-oal is moved to *-wal.
158120 */
158121 otaLockDatabase(p);
158122 if( p->rc==SQLITE_OK ){
158123 otaFileSuffix3(zBase, zWal);
158124 otaFileSuffix3(zBase, zOal);
158125
158126 /* Re-open the databases. */
158127 otaObjIterFinalize(&p->objiter);
158128 sqlite3_close(p->dbMain);
158129 sqlite3_close(p->dbOta);
158130 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
158131 if( p->rc==SQLITE_OK ){
158132 p->dbMain = 0;
158133 p->dbOta = 0;
158134 otaOpenDatabase(p);
158135 otaSetupCheckpoint(p, 0);
158136 }
158137 }
158138 }
158139
158140 sqlite3_free(zWal);
158141 sqlite3_free(zOal);
158142 }
158143
158144 /*
158145 ** The SELECT statement iterating through the keys for the current object
158146 ** (p->objiter.pSelect) currently points to a valid row. This function
158147 ** determines the type of operation requested by this row and returns
158148 ** one of the following values to indicate the result:
158149 **
158150 ** * OTA_INSERT
158151 ** * OTA_DELETE
158152 ** * OTA_IDX_DELETE
158153 ** * OTA_UPDATE
158154 **
158155 ** If OTA_UPDATE is returned, then output variable *pzMask is set to
158156 ** point to the text value indicating the columns to update.
158157 **
158158 ** If the ota_control field contains an invalid value, an error code and
158159 ** message are left in the OTA handle and zero returned.
158160 */
158161 static int otaStepType(sqlite3ota *p, const char **pzMask){
158162 int iCol = p->objiter.nCol; /* Index of ota_control column */
158163 int res = 0; /* Return value */
158164
158165 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
158166 case SQLITE_INTEGER: {
158167 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
158168 if( iVal==0 ){
158169 res = OTA_INSERT;
158170 }else if( iVal==1 ){
158171 res = OTA_DELETE;
158172 }else if( iVal==2 ){
158173 res = OTA_IDX_DELETE;
158174 }else if( iVal==3 ){
158175 res = OTA_IDX_INSERT;
158176 }
158177 break;
158178 }
158179
158180 case SQLITE_TEXT: {
158181 const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
158182 if( z==0 ){
158183 p->rc = SQLITE_NOMEM;
158184 }else{
158185 *pzMask = (const char*)z;
158186 }
158187 res = OTA_UPDATE;
158188
158189 break;
158190 }
158191
158192 default:
158193 break;
158194 }
158195
158196 if( res==0 ){
158197 otaBadControlError(p);
158198 }
158199 return res;
158200 }
158201
158202 #ifdef SQLITE_DEBUG
158203 /*
158204 ** Assert that column iCol of statement pStmt is named zName.
158205 */
158206 static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
158207 const char *zCol = sqlite3_column_name(pStmt, iCol);
158208 assert( 0==sqlite3_stricmp(zName, zCol) );
158209 }
158210 #else
158211 # define assertColumnName(x,y,z)
158212 #endif
158213
158214 /*
158215 ** This function does the work for an sqlite3ota_step() call.
158216 **
158217 ** The object-iterator (p->objiter) currently points to a valid object,
158218 ** and the input cursor (p->objiter.pSelect) currently points to a valid
158219 ** input row. Perform whatever processing is required and return.
158220 **
158221 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
158222 ** and message is left in the OTA handle and a copy of the error code
158223 ** returned.
158224 */
158225 static int otaStep(sqlite3ota *p){
158226 OtaObjIter *pIter = &p->objiter;
158227 const char *zMask = 0;
158228 int i;
158229 int eType = otaStepType(p, &zMask);
158230
158231 if( eType ){
158232 assert( eType!=OTA_UPDATE || pIter->zIdx==0 );
158233
158234 if( pIter->zIdx==0 && eType==OTA_IDX_DELETE ){
158235 otaBadControlError(p);
158236 }
158237 else if(
158238 eType==OTA_INSERT
158239 || eType==OTA_DELETE
158240 || eType==OTA_IDX_DELETE
158241 || eType==OTA_IDX_INSERT
158242 ){
158243 sqlite3_value *pVal;
158244 sqlite3_stmt *pWriter;
158245
158246 assert( eType!=OTA_UPDATE );
158247 assert( eType!=OTA_DELETE || pIter->zIdx==0 );
158248
158249 if( eType==OTA_IDX_DELETE || eType==OTA_DELETE ){
158250 pWriter = pIter->pDelete;
158251 }else{
158252 pWriter = pIter->pInsert;
158253 }
158254
158255 for(i=0; i<pIter->nCol; i++){
158256 /* If this is an INSERT into a table b-tree and the table has an
158257 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
158258 ** to write a NULL into the IPK column. That is not permitted. */
158259 if( eType==OTA_INSERT
158260 && pIter->zIdx==0 && pIter->eType==OTA_PK_IPK && pIter->abTblPk[i]
158261 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
158262 ){
158263 p->rc = SQLITE_MISMATCH;
158264 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
158265 goto step_out;
158266 }
158267
158268 if( eType==OTA_DELETE && pIter->abTblPk[i]==0 ){
158269 continue;
158270 }
158271
158272 pVal = sqlite3_column_value(pIter->pSelect, i);
158273 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
158274 if( p->rc ) goto step_out;
158275 }
158276 if( pIter->zIdx==0
158277 && (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
158278 ){
158279 /* For a virtual table, or a table with no primary key, the
158280 ** SELECT statement is:
158281 **
158282 ** SELECT <cols>, ota_control, ota_rowid FROM ....
158283 **
158284 ** Hence column_value(pIter->nCol+1).
158285 */
158286 assertColumnName(pIter->pSelect, pIter->nCol+1, "ota_rowid");
158287 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
158288 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
158289 }
158290 if( p->rc==SQLITE_OK ){
158291 sqlite3_step(pWriter);
158292 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
158293 }
158294 }else{
158295 sqlite3_value *pVal;
158296 sqlite3_stmt *pUpdate = 0;
158297 assert( eType==OTA_UPDATE );
158298 otaGetUpdateStmt(p, pIter, zMask, &pUpdate);
158299 if( pUpdate ){
158300 for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
158301 char c = zMask[pIter->aiSrcOrder[i]];
158302 pVal = sqlite3_column_value(pIter->pSelect, i);
158303 if( pIter->abTblPk[i] || c=='x' || c=='d' ){
158304 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
158305 }
158306 }
158307 if( p->rc==SQLITE_OK
158308 && (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
158309 ){
158310 /* Bind the ota_rowid value to column _rowid_ */
158311 assertColumnName(pIter->pSelect, pIter->nCol+1, "ota_rowid");
158312 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
158313 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
158314 }
158315 if( p->rc==SQLITE_OK ){
158316 sqlite3_step(pUpdate);
158317 p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
158318 }
158319 }
158320 }
158321 }
158322
158323 step_out:
158324 return p->rc;
158325 }
158326
158327 /*
158328 ** Increment the schema cookie of the main database opened by p->dbMain.
158329 */
158330 static void otaIncrSchemaCookie(sqlite3ota *p){
158331 if( p->rc==SQLITE_OK ){
158332 int iCookie = 1000000;
158333 sqlite3_stmt *pStmt;
158334
158335 p->rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
158336 "PRAGMA schema_version"
158337 );
158338 if( p->rc==SQLITE_OK ){
158339 /* Coverage: it may be that this sqlite3_step() cannot fail. There
158340 ** is already a transaction open, so the prepared statement cannot
158341 ** throw an SQLITE_SCHEMA exception. The only database page the
158342 ** statement reads is page 1, which is guaranteed to be in the cache.
158343 ** And no memory allocations are required. */
158344 if( SQLITE_ROW==sqlite3_step(pStmt) ){
158345 iCookie = sqlite3_column_int(pStmt, 0);
158346 }
158347 otaFinalize(p, pStmt);
158348 }
158349 if( p->rc==SQLITE_OK ){
158350 otaMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
158351 }
158352 }
158353 }
158354
158355 /*
158356 ** Update the contents of the ota_state table within the ota database. The
158357 ** value stored in the OTA_STATE_STAGE column is eStage. All other values
158358 ** are determined by inspecting the ota handle passed as the first argument.
158359 */
158360 static void otaSaveState(sqlite3ota *p, int eStage){
158361 if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
158362 sqlite3_stmt *pInsert = 0;
158363 int rc;
158364
158365 assert( p->zErrmsg==0 );
158366 rc = prepareFreeAndCollectError(p->dbOta, &pInsert, &p->zErrmsg,
158367 sqlite3_mprintf(
158368 "INSERT OR REPLACE INTO %s.ota_state(k, v) VALUES "
158369 "(%d, %d), "
158370 "(%d, %Q), "
158371 "(%d, %Q), "
158372 "(%d, %d), "
158373 "(%d, %d), "
158374 "(%d, %lld), "
158375 "(%d, %lld), "
158376 "(%d, %lld) ",
158377 p->zStateDb,
158378 OTA_STATE_STAGE, eStage,
158379 OTA_STATE_TBL, p->objiter.zTbl,
158380 OTA_STATE_IDX, p->objiter.zIdx,
158381 OTA_STATE_ROW, p->nStep,
158382 OTA_STATE_PROGRESS, p->nProgress,
158383 OTA_STATE_CKPT, p->iWalCksum,
158384 OTA_STATE_COOKIE, (i64)p->pTargetFd->iCookie,
158385 OTA_STATE_OALSZ, p->iOalSz
158386 )
158387 );
158388 assert( pInsert==0 || rc==SQLITE_OK );
158389
158390 if( rc==SQLITE_OK ){
158391 sqlite3_step(pInsert);
158392 rc = sqlite3_finalize(pInsert);
158393 }
158394 if( rc!=SQLITE_OK ) p->rc = rc;
158395 }
158396 }
158397
158398
158399 /*
158400 ** Step the OTA object.
158401 */
158402 SQLITE_API int SQLITE_STDCALL sqlite3ota_step(sqlite3ota *p){
158403 if( p ){
158404 switch( p->eStage ){
158405 case OTA_STAGE_OAL: {
158406 OtaObjIter *pIter = &p->objiter;
158407 while( p->rc==SQLITE_OK && pIter->zTbl ){
158408
158409 if( pIter->bCleanup ){
158410 /* Clean up the ota_tmp_xxx table for the previous table. It
158411 ** cannot be dropped as there are currently active SQL statements.
158412 ** But the contents can be deleted. */
158413 if( pIter->abIndexed ){
158414 otaMPrintfExec(p, p->dbOta,
158415 "DELETE FROM %s.'ota_tmp_%q'", p->zStateDb, pIter->zTbl
158416 );
158417 }
158418 }else{
158419 otaObjIterPrepareAll(p, pIter, 0);
158420
158421 /* Advance to the next row to process. */
158422 if( p->rc==SQLITE_OK ){
158423 int rc = sqlite3_step(pIter->pSelect);
158424 if( rc==SQLITE_ROW ){
158425 p->nProgress++;
158426 p->nStep++;
158427 return otaStep(p);
158428 }
158429 p->rc = sqlite3_reset(pIter->pSelect);
158430 p->nStep = 0;
158431 }
158432 }
158433
158434 otaObjIterNext(p, pIter);
158435 }
158436
158437 if( p->rc==SQLITE_OK ){
158438 assert( pIter->zTbl==0 );
158439 otaSaveState(p, OTA_STAGE_MOVE);
158440 otaIncrSchemaCookie(p);
158441 if( p->rc==SQLITE_OK ){
158442 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
158443 }
158444 if( p->rc==SQLITE_OK ){
158445 p->rc = sqlite3_exec(p->dbOta, "COMMIT", 0, 0, &p->zErrmsg);
158446 }
158447 p->eStage = OTA_STAGE_MOVE;
158448 }
158449 break;
158450 }
158451
158452 case OTA_STAGE_MOVE: {
158453 if( p->rc==SQLITE_OK ){
158454 otaMoveOalFile(p);
158455 p->nProgress++;
158456 }
158457 break;
158458 }
158459
158460 case OTA_STAGE_CKPT: {
158461 if( p->rc==SQLITE_OK ){
158462 if( p->nStep>=p->nFrame ){
158463 sqlite3_file *pDb = p->pTargetFd->pReal;
158464
158465 /* Sync the db file */
158466 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
158467
158468 /* Update nBackfill */
158469 if( p->rc==SQLITE_OK ){
158470 void volatile *ptr;
158471 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
158472 if( p->rc==SQLITE_OK ){
158473 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
158474 }
158475 }
158476
158477 if( p->rc==SQLITE_OK ){
158478 p->eStage = OTA_STAGE_DONE;
158479 p->rc = SQLITE_DONE;
158480 }
158481 }else{
158482 OtaFrame *pFrame = &p->aFrame[p->nStep];
158483 otaCheckpointFrame(p, pFrame);
158484 p->nStep++;
158485 }
158486 p->nProgress++;
158487 }
158488 break;
158489 }
158490
158491 default:
158492 break;
158493 }
158494 return p->rc;
158495 }else{
158496 return SQLITE_NOMEM;
158497 }
158498 }
158499
158500 /*
158501 ** Free an OtaState object allocated by otaLoadState().
158502 */
158503 static void otaFreeState(OtaState *p){
158504 if( p ){
158505 sqlite3_free(p->zTbl);
158506 sqlite3_free(p->zIdx);
158507 sqlite3_free(p);
158508 }
158509 }
158510
158511 /*
158512 ** Allocate an OtaState object and load the contents of the ota_state
158513 ** table into it. Return a pointer to the new object. It is the
158514 ** responsibility of the caller to eventually free the object using
158515 ** sqlite3_free().
158516 **
158517 ** If an error occurs, leave an error code and message in the ota handle
158518 ** and return NULL.
158519 */
158520 static OtaState *otaLoadState(sqlite3ota *p){
158521 OtaState *pRet = 0;
158522 sqlite3_stmt *pStmt = 0;
158523 int rc;
158524 int rc2;
158525
158526 pRet = (OtaState*)otaMalloc(p, sizeof(OtaState));
158527 if( pRet==0 ) return 0;
158528
158529 rc = prepareFreeAndCollectError(p->dbOta, &pStmt, &p->zErrmsg,
158530 sqlite3_mprintf("SELECT k, v FROM %s.ota_state", p->zStateDb)
158531 );
158532 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
158533 switch( sqlite3_column_int(pStmt, 0) ){
158534 case OTA_STATE_STAGE:
158535 pRet->eStage = sqlite3_column_int(pStmt, 1);
158536 if( pRet->eStage!=OTA_STAGE_OAL
158537 && pRet->eStage!=OTA_STAGE_MOVE
158538 && pRet->eStage!=OTA_STAGE_CKPT
158539 ){
158540 p->rc = SQLITE_CORRUPT;
158541 }
158542 break;
158543
158544 case OTA_STATE_TBL:
158545 pRet->zTbl = otaStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
158546 break;
158547
158548 case OTA_STATE_IDX:
158549 pRet->zIdx = otaStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
158550 break;
158551
158552 case OTA_STATE_ROW:
158553 pRet->nRow = sqlite3_column_int(pStmt, 1);
158554 break;
158555
158556 case OTA_STATE_PROGRESS:
158557 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
158558 break;
158559
158560 case OTA_STATE_CKPT:
158561 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
158562 break;
158563
158564 case OTA_STATE_COOKIE:
158565 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
158566 break;
158567
158568 case OTA_STATE_OALSZ:
158569 pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
158570 break;
158571
158572 default:
158573 rc = SQLITE_CORRUPT;
158574 break;
158575 }
158576 }
158577 rc2 = sqlite3_finalize(pStmt);
158578 if( rc==SQLITE_OK ) rc = rc2;
158579
158580 p->rc = rc;
158581 return pRet;
158582 }
158583
158584 /*
158585 ** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
158586 ** otherwise. Either or both argument may be NULL. Two NULL values are
158587 ** considered equal, and NULL is considered distinct from all other values.
158588 */
158589 static int otaStrCompare(const char *z1, const char *z2){
158590 if( z1==0 && z2==0 ) return 0;
158591 if( z1==0 || z2==0 ) return 1;
158592 return (sqlite3_stricmp(z1, z2)!=0);
158593 }
158594
158595 /*
158596 ** This function is called as part of sqlite3ota_open() when initializing
158597 ** an ota handle in OAL stage. If the ota update has not started (i.e.
158598 ** the ota_state table was empty) it is a no-op. Otherwise, it arranges
158599 ** things so that the next call to sqlite3ota_step() continues on from
158600 ** where the previous ota handle left off.
158601 **
158602 ** If an error occurs, an error code and error message are left in the
158603 ** ota handle passed as the first argument.
158604 */
158605 static void otaSetupOal(sqlite3ota *p, OtaState *pState){
158606 assert( p->rc==SQLITE_OK );
158607 if( pState->zTbl ){
158608 OtaObjIter *pIter = &p->objiter;
158609 int rc = SQLITE_OK;
158610
158611 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
158612 || otaStrCompare(pIter->zIdx, pState->zIdx)
158613 || otaStrCompare(pIter->zTbl, pState->zTbl)
158614 )){
158615 rc = otaObjIterNext(p, pIter);
158616 }
158617
158618 if( rc==SQLITE_OK && !pIter->zTbl ){
158619 rc = SQLITE_ERROR;
158620 p->zErrmsg = sqlite3_mprintf("ota_state mismatch error");
158621 }
158622
158623 if( rc==SQLITE_OK ){
158624 p->nStep = pState->nRow;
158625 rc = otaObjIterPrepareAll(p, &p->objiter, p->nStep);
158626 }
158627
158628 p->rc = rc;
158629 }
158630 }
158631
158632 /*
158633 ** If there is a "*-oal" file in the file-system corresponding to the
158634 ** target database in the file-system, delete it. If an error occurs,
158635 ** leave an error code and error message in the ota handle.
158636 */
158637 static void otaDeleteOalFile(sqlite3ota *p){
158638 char *zOal = sqlite3_mprintf("%s-oal", p->zTarget);
158639 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
158640 unlink(zOal);
158641 sqlite3_free(zOal);
158642 }
158643
158644 /*
158645 ** Allocate a private ota VFS for the ota handle passed as the only
158646 ** argument. This VFS will be used unless the call to sqlite3ota_open()
158647 ** specified a URI with a vfs=? option in place of a target database
158648 ** file name.
158649 */
158650 static void otaCreateVfs(sqlite3ota *p){
158651 int rnd;
158652 char zRnd[64];
158653
158654 assert( p->rc==SQLITE_OK );
158655 sqlite3_randomness(sizeof(int), (void*)&rnd);
158656 sprintf(zRnd, "ota_vfs_%d", rnd);
158657 p->rc = sqlite3ota_create_vfs(zRnd, 0);
158658 if( p->rc==SQLITE_OK ){
158659 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
158660 assert( pVfs );
158661 p->zVfsName = pVfs->zName;
158662 }
158663 }
158664
158665 /*
158666 ** Destroy the private VFS created for the ota handle passed as the only
158667 ** argument by an earlier call to otaCreateVfs().
158668 */
158669 static void otaDeleteVfs(sqlite3ota *p){
158670 if( p->zVfsName ){
158671 sqlite3ota_destroy_vfs(p->zVfsName);
158672 p->zVfsName = 0;
158673 }
158674 }
158675
158676 /*
158677 ** Open and return a new OTA handle.
158678 */
158679 SQLITE_API sqlite3ota *SQLITE_STDCALL sqlite3ota_open(
158680 const char *zTarget,
158681 const char *zOta,
158682 const char *zState
158683 ){
158684 sqlite3ota *p;
158685 int nTarget = strlen(zTarget);
158686 int nOta = strlen(zOta);
158687 int nState = zState ? strlen(zState) : 0;
158688
158689 p = (sqlite3ota*)sqlite3_malloc(sizeof(sqlite3ota)+nTarget+1+nOta+1+nState+1);
158690 if( p ){
158691 OtaState *pState = 0;
158692
158693 /* Create the custom VFS. */
158694 memset(p, 0, sizeof(sqlite3ota));
158695 otaCreateVfs(p);
158696
158697 /* Open the target database */
158698 if( p->rc==SQLITE_OK ){
158699 p->zTarget = (char*)&p[1];
158700 memcpy(p->zTarget, zTarget, nTarget+1);
158701 p->zOta = &p->zTarget[nTarget+1];
158702 memcpy(p->zOta, zOta, nOta+1);
158703 if( zState ){
158704 p->zState = &p->zOta[nOta+1];
158705 memcpy(p->zState, zState, nState+1);
158706 }
158707 otaOpenDatabase(p);
158708 }
158709
158710 /* If it has not already been created, create the ota_state table */
158711 otaMPrintfExec(p, p->dbOta, OTA_CREATE_STATE, p->zStateDb);
158712
158713 if( p->rc==SQLITE_OK ){
158714 pState = otaLoadState(p);
158715 assert( pState || p->rc!=SQLITE_OK );
158716 if( p->rc==SQLITE_OK ){
158717
158718 if( pState->eStage==0 ){
158719 otaDeleteOalFile(p);
158720 p->eStage = OTA_STAGE_OAL;
158721 }else{
158722 p->eStage = pState->eStage;
158723 }
158724 p->nProgress = pState->nProgress;
158725 p->iOalSz = pState->iOalSz;
158726 }
158727 }
158728 assert( p->rc!=SQLITE_OK || p->eStage!=0 );
158729
158730 if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
158731 if( p->eStage==OTA_STAGE_OAL ){
158732 p->rc = SQLITE_ERROR;
158733 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
158734 }else if( p->eStage==OTA_STAGE_MOVE ){
158735 p->eStage = OTA_STAGE_CKPT;
158736 p->nStep = 0;
158737 }
158738 }
158739
158740 if( p->rc==SQLITE_OK
158741 && (p->eStage==OTA_STAGE_OAL || p->eStage==OTA_STAGE_MOVE)
158742 && pState->eStage!=0 && p->pTargetFd->iCookie!=pState->iCookie
158743 ){
158744 /* At this point (pTargetFd->iCookie) contains the value of the
158745 ** change-counter cookie (the thing that gets incremented when a
158746 ** transaction is committed in rollback mode) currently stored on
158747 ** page 1 of the database file. */
158748 p->rc = SQLITE_BUSY;
158749 p->zErrmsg = sqlite3_mprintf("database modified during ota update");
158750 }
158751
158752 if( p->rc==SQLITE_OK ){
158753 if( p->eStage==OTA_STAGE_OAL ){
158754
158755 /* Open transactions both databases. The *-oal file is opened or
158756 ** created at this point. */
158757 p->rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
158758 if( p->rc==SQLITE_OK ){
158759 p->rc = sqlite3_exec(p->dbOta, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
158760 }
158761
158762 /* Point the object iterator at the first object */
158763 if( p->rc==SQLITE_OK ){
158764 p->rc = otaObjIterFirst(p, &p->objiter);
158765 }
158766
158767 /* If the OTA database contains no data_xxx tables, declare the OTA
158768 ** update finished. */
158769 if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
158770 p->rc = SQLITE_DONE;
158771 }
158772
158773 if( p->rc==SQLITE_OK ){
158774 otaSetupOal(p, pState);
158775 }
158776
158777 }else if( p->eStage==OTA_STAGE_MOVE ){
158778 /* no-op */
158779 }else if( p->eStage==OTA_STAGE_CKPT ){
158780 otaSetupCheckpoint(p, pState);
158781 }else if( p->eStage==OTA_STAGE_DONE ){
158782 p->rc = SQLITE_DONE;
158783 }else{
158784 p->rc = SQLITE_CORRUPT;
158785 }
158786 }
158787
158788 otaFreeState(pState);
158789 }
158790
158791 return p;
158792 }
158793
158794
158795 /*
158796 ** Return the database handle used by pOta.
158797 */
158798 SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3ota_db(sqlite3ota *pOta, int bOta){
158799 sqlite3 *db = 0;
158800 if( pOta ){
158801 db = (bOta ? pOta->dbOta : pOta->dbMain);
158802 }
158803 return db;
158804 }
158805
158806
158807 /*
158808 ** If the error code currently stored in the OTA handle is SQLITE_CONSTRAINT,
158809 ** then edit any error message string so as to remove all occurrences of
158810 ** the pattern "ota_imp_[0-9]*".
158811 */
158812 static void otaEditErrmsg(sqlite3ota *p){
158813 if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
158814 int i;
158815 int nErrmsg = strlen(p->zErrmsg);
158816 for(i=0; i<(nErrmsg-8); i++){
158817 if( memcmp(&p->zErrmsg[i], "ota_imp_", 8)==0 ){
158818 int nDel = 8;
158819 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
158820 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
158821 nErrmsg -= nDel;
158822 }
158823 }
158824 }
158825 }
158826
158827 /*
158828 ** Close the OTA handle.
158829 */
158830 SQLITE_API int SQLITE_STDCALL sqlite3ota_close(sqlite3ota *p, char **pzErrmsg){
158831 int rc;
158832 if( p ){
158833
158834 /* Commit the transaction to the *-oal file. */
158835 if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
158836 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
158837 }
158838
158839 otaSaveState(p, p->eStage);
158840
158841 if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
158842 p->rc = sqlite3_exec(p->dbOta, "COMMIT", 0, 0, &p->zErrmsg);
158843 }
158844
158845 /* Close any open statement handles. */
158846 otaObjIterFinalize(&p->objiter);
158847
158848 /* Close the open database handle and VFS object. */
158849 sqlite3_close(p->dbMain);
158850 sqlite3_close(p->dbOta);
158851 otaDeleteVfs(p);
158852 sqlite3_free(p->aBuf);
158853 sqlite3_free(p->aFrame);
158854
158855 otaEditErrmsg(p);
158856 rc = p->rc;
158857 *pzErrmsg = p->zErrmsg;
158858 sqlite3_free(p);
158859 }else{
158860 rc = SQLITE_NOMEM;
158861 *pzErrmsg = 0;
158862 }
158863 return rc;
158864 }
158865
158866 /*
158867 ** Return the total number of key-value operations (inserts, deletes or
158868 ** updates) that have been performed on the target database since the
158869 ** current OTA update was started.
158870 */
158871 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3ota_progress(sqlite3ota *pOta){
158872 return pOta->nProgress;
158873 }
158874
158875 /**************************************************************************
158876 ** Beginning of OTA VFS shim methods. The VFS shim modifies the behaviour
158877 ** of a standard VFS in the following ways:
158878 **
158879 ** 1. Whenever the first page of a main database file is read or
158880 ** written, the value of the change-counter cookie is stored in
158881 ** ota_file.iCookie. Similarly, the value of the "write-version"
158882 ** database header field is stored in ota_file.iWriteVer. This ensures
158883 ** that the values are always trustworthy within an open transaction.
158884 **
158885 ** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (ota_file.pWalFd)
158886 ** member variable of the associated database file descriptor is set
158887 ** to point to the new file. A mutex protected linked list of all main
158888 ** db fds opened using a particular OTA VFS is maintained at
158889 ** ota_vfs.pMain to facilitate this.
158890 **
158891 ** 3. Using a new file-control "SQLITE_FCNTL_OTA", a main db ota_file
158892 ** object can be marked as the target database of an OTA update. This
158893 ** turns on the following extra special behaviour:
158894 **
158895 ** 3a. If xAccess() is called to check if there exists a *-wal file
158896 ** associated with an OTA target database currently in OTA_STAGE_OAL
158897 ** stage (preparing the *-oal file), the following special handling
158898 ** applies:
158899 **
158900 ** * if the *-wal file does exist, return SQLITE_CANTOPEN. An OTA
158901 ** target database may not be in wal mode already.
158902 **
158903 ** * if the *-wal file does not exist, set the output parameter to
158904 ** non-zero (to tell SQLite that it does exist) anyway.
158905 **
158906 ** Then, when xOpen() is called to open the *-wal file associated with
158907 ** the OTA target in OTA_STAGE_OAL stage, instead of opening the *-wal
158908 ** file, the ota vfs opens the corresponding *-oal file instead.
158909 **
158910 ** 3b. The *-shm pages returned by xShmMap() for a target db file in
158911 ** OTA_STAGE_OAL mode are actually stored in heap memory. This is to
158912 ** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
158913 ** are no-ops on target database files in OTA_STAGE_OAL mode. This is
158914 ** because assert() statements in some VFS implementations fail if
158915 ** xShmLock() is called before xShmMap().
158916 **
158917 ** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
158918 ** mode except OTA_STAGE_DONE (all work completed and checkpointed), it
158919 ** fails with an SQLITE_BUSY error. This is to stop OTA connections
158920 ** from automatically checkpointing a *-wal (or *-oal) file from within
158921 ** sqlite3_close().
158922 **
158923 ** 3d. In OTA_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
158924 ** all xWrite() calls on the target database file perform no IO.
158925 ** Instead the frame and page numbers that would be read and written
158926 ** are recorded. Additionally, successful attempts to obtain exclusive
158927 ** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
158928 ** database file are recorded. xShmLock() calls to unlock the same
158929 ** locks are no-ops (so that once obtained, these locks are never
158930 ** relinquished). Finally, calls to xSync() on the target database
158931 ** file fail with SQLITE_INTERNAL errors.
158932 */
158933
158934 static void otaUnlockShm(ota_file *p){
158935 if( p->pOta ){
158936 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
158937 int i;
158938 for(i=0; i<SQLITE_SHM_NLOCK;i++){
158939 if( (1<<i) & p->pOta->mLock ){
158940 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
158941 }
158942 }
158943 p->pOta->mLock = 0;
158944 }
158945 }
158946
158947 /*
158948 ** Close an ota file.
158949 */
158950 static int otaVfsClose(sqlite3_file *pFile){
158951 ota_file *p = (ota_file*)pFile;
158952 int rc;
158953 int i;
158954
158955 /* Free the contents of the apShm[] array. And the array itself. */
158956 for(i=0; i<p->nShm; i++){
158957 sqlite3_free(p->apShm[i]);
158958 }
158959 sqlite3_free(p->apShm);
158960 p->apShm = 0;
158961 sqlite3_free(p->zDel);
158962
158963 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
158964 ota_file **pp;
158965 sqlite3_mutex_enter(p->pOtaVfs->mutex);
158966 for(pp=&p->pOtaVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
158967 *pp = p->pMainNext;
158968 sqlite3_mutex_leave(p->pOtaVfs->mutex);
158969 otaUnlockShm(p);
158970 p->pReal->pMethods->xShmUnmap(p->pReal, 0);
158971 }
158972
158973 /* Close the underlying file handle */
158974 rc = p->pReal->pMethods->xClose(p->pReal);
158975 return rc;
158976 }
158977
158978
158979 /*
158980 ** Read and return an unsigned 32-bit big-endian integer from the buffer
158981 ** passed as the only argument.
158982 */
158983 static u32 otaGetU32(u8 *aBuf){
158984 return ((u32)aBuf[0] << 24)
158985 + ((u32)aBuf[1] << 16)
158986 + ((u32)aBuf[2] << 8)
158987 + ((u32)aBuf[3]);
158988 }
158989
158990 /*
158991 ** Read data from an otaVfs-file.
158992 */
158993 static int otaVfsRead(
158994 sqlite3_file *pFile,
158995 void *zBuf,
158996 int iAmt,
158997 sqlite_int64 iOfst
158998 ){
158999 ota_file *p = (ota_file*)pFile;
159000 sqlite3ota *pOta = p->pOta;
159001 int rc;
159002
159003 if( pOta && pOta->eStage==OTA_STAGE_CAPTURE ){
159004 assert( p->openFlags & SQLITE_OPEN_WAL );
159005 rc = otaCaptureWalRead(p->pOta, iOfst, iAmt);
159006 }else{
159007 if( pOta && pOta->eStage==OTA_STAGE_OAL
159008 && (p->openFlags & SQLITE_OPEN_WAL)
159009 && iOfst>=pOta->iOalSz
159010 ){
159011 rc = SQLITE_OK;
159012 memset(zBuf, 0, iAmt);
159013 }else{
159014 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
159015 }
159016 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
159017 /* These look like magic numbers. But they are stable, as they are part
159018 ** of the definition of the SQLite file format, which may not change. */
159019 u8 *pBuf = (u8*)zBuf;
159020 p->iCookie = otaGetU32(&pBuf[24]);
159021 p->iWriteVer = pBuf[19];
159022 }
159023 }
159024 return rc;
159025 }
159026
159027 /*
159028 ** Write data to an otaVfs-file.
159029 */
159030 static int otaVfsWrite(
159031 sqlite3_file *pFile,
159032 const void *zBuf,
159033 int iAmt,
159034 sqlite_int64 iOfst
159035 ){
159036 ota_file *p = (ota_file*)pFile;
159037 sqlite3ota *pOta = p->pOta;
159038 int rc;
159039
159040 if( pOta && pOta->eStage==OTA_STAGE_CAPTURE ){
159041 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
159042 rc = otaCaptureDbWrite(p->pOta, iOfst);
159043 }else{
159044 if( pOta && pOta->eStage==OTA_STAGE_OAL
159045 && (p->openFlags & SQLITE_OPEN_WAL)
159046 && iOfst>=pOta->iOalSz
159047 ){
159048 pOta->iOalSz = iAmt + iOfst;
159049 }
159050 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
159051 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
159052 /* These look like magic numbers. But they are stable, as they are part
159053 ** of the definition of the SQLite file format, which may not change. */
159054 u8 *pBuf = (u8*)zBuf;
159055 p->iCookie = otaGetU32(&pBuf[24]);
159056 p->iWriteVer = pBuf[19];
159057 }
159058 }
159059 return rc;
159060 }
159061
159062 /*
159063 ** Truncate an otaVfs-file.
159064 */
159065 static int otaVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
159066 ota_file *p = (ota_file*)pFile;
159067 return p->pReal->pMethods->xTruncate(p->pReal, size);
159068 }
159069
159070 /*
159071 ** Sync an otaVfs-file.
159072 */
159073 static int otaVfsSync(sqlite3_file *pFile, int flags){
159074 ota_file *p = (ota_file *)pFile;
159075 if( p->pOta && p->pOta->eStage==OTA_STAGE_CAPTURE ){
159076 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
159077 return SQLITE_INTERNAL;
159078 }
159079 return SQLITE_OK;
159080 }
159081 return p->pReal->pMethods->xSync(p->pReal, flags);
159082 }
159083
159084 /*
159085 ** Return the current file-size of an otaVfs-file.
159086 */
159087 static int otaVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
159088 ota_file *p = (ota_file *)pFile;
159089 return p->pReal->pMethods->xFileSize(p->pReal, pSize);
159090 }
159091
159092 /*
159093 ** Lock an otaVfs-file.
159094 */
159095 static int otaVfsLock(sqlite3_file *pFile, int eLock){
159096 ota_file *p = (ota_file*)pFile;
159097 sqlite3ota *pOta = p->pOta;
159098 int rc = SQLITE_OK;
159099
159100 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
159101 if( pOta && eLock==SQLITE_LOCK_EXCLUSIVE && pOta->eStage!=OTA_STAGE_DONE ){
159102 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
159103 ** prevents it from checkpointing the database from sqlite3_close(). */
159104 rc = SQLITE_BUSY;
159105 }else{
159106 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
159107 }
159108
159109 return rc;
159110 }
159111
159112 /*
159113 ** Unlock an otaVfs-file.
159114 */
159115 static int otaVfsUnlock(sqlite3_file *pFile, int eLock){
159116 ota_file *p = (ota_file *)pFile;
159117 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
159118 }
159119
159120 /*
159121 ** Check if another file-handle holds a RESERVED lock on an otaVfs-file.
159122 */
159123 static int otaVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
159124 ota_file *p = (ota_file *)pFile;
159125 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
159126 }
159127
159128 /*
159129 ** File control method. For custom operations on an otaVfs-file.
159130 */
159131 static int otaVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
159132 ota_file *p = (ota_file *)pFile;
159133 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
159134 int rc;
159135
159136 assert( p->openFlags &
159137 (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB|SQLITE_OPEN_TRANSIENT_DB)
159138 );
159139 if( op==SQLITE_FCNTL_OTA ){
159140 sqlite3ota *pOta = (sqlite3ota*)pArg;
159141
159142 /* First try to find another OTA vfs lower down in the vfs stack. If
159143 ** one is found, this vfs will operate in pass-through mode. The lower
159144 ** level vfs will do the special OTA handling. */
159145 rc = xControl(p->pReal, op, pArg);
159146
159147 if( rc==SQLITE_NOTFOUND ){
159148 /* Now search for a zipvfs instance lower down in the VFS stack. If
159149 ** one is found, this is an error. */
159150 void *dummy = 0;
159151 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
159152 if( rc==SQLITE_OK ){
159153 rc = SQLITE_ERROR;
159154 pOta->zErrmsg = sqlite3_mprintf("ota/zipvfs setup error");
159155 }else if( rc==SQLITE_NOTFOUND ){
159156 pOta->pTargetFd = p;
159157 p->pOta = pOta;
159158 if( p->pWalFd ) p->pWalFd->pOta = pOta;
159159 rc = SQLITE_OK;
159160 }
159161 }
159162 return rc;
159163 }
159164
159165 rc = xControl(p->pReal, op, pArg);
159166 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
159167 ota_vfs *pOtaVfs = p->pOtaVfs;
159168 char *zIn = *(char**)pArg;
159169 char *zOut = sqlite3_mprintf("ota(%s)/%z", pOtaVfs->base.zName, zIn);
159170 *(char**)pArg = zOut;
159171 if( zOut==0 ) rc = SQLITE_NOMEM;
159172 }
159173
159174 return rc;
159175 }
159176
159177 /*
159178 ** Return the sector-size in bytes for an otaVfs-file.
159179 */
159180 static int otaVfsSectorSize(sqlite3_file *pFile){
159181 ota_file *p = (ota_file *)pFile;
159182 return p->pReal->pMethods->xSectorSize(p->pReal);
159183 }
159184
159185 /*
159186 ** Return the device characteristic flags supported by an otaVfs-file.
159187 */
159188 static int otaVfsDeviceCharacteristics(sqlite3_file *pFile){
159189 ota_file *p = (ota_file *)pFile;
159190 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
159191 }
159192
159193 /*
159194 ** Take or release a shared-memory lock.
159195 */
159196 static int otaVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
159197 ota_file *p = (ota_file*)pFile;
159198 sqlite3ota *pOta = p->pOta;
159199 int rc = SQLITE_OK;
159200
159201 #ifdef SQLITE_AMALGAMATION
159202 assert( WAL_CKPT_LOCK==1 );
159203 #endif
159204
159205 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
159206 if( pOta && (pOta->eStage==OTA_STAGE_OAL || pOta->eStage==OTA_STAGE_MOVE) ){
159207 /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
159208 ** taking this lock also prevents any checkpoints from occurring.
159209 ** todo: really, it's not clear why this might occur, as
159210 ** wal_autocheckpoint ought to be turned off. */
159211 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
159212 }else{
159213 int bCapture = 0;
159214 if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
159215 && pOta && pOta->eStage==OTA_STAGE_CAPTURE
159216 && (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
159217 ){
159218 bCapture = 1;
159219 }
159220
159221 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
159222 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
159223 if( bCapture && rc==SQLITE_OK ){
159224 pOta->mLock |= (1 << ofst);
159225 }
159226 }
159227 }
159228
159229 return rc;
159230 }
159231
159232 /*
159233 ** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
159234 */
159235 static int otaVfsShmMap(
159236 sqlite3_file *pFile,
159237 int iRegion,
159238 int szRegion,
159239 int isWrite,
159240 void volatile **pp
159241 ){
159242 ota_file *p = (ota_file*)pFile;
159243 int rc = SQLITE_OK;
159244 int eStage = (p->pOta ? p->pOta->eStage : 0);
159245
159246 /* If not in OTA_STAGE_OAL, allow this call to pass through. Or, if this
159247 ** ota is in the OTA_STAGE_OAL state, use heap memory for *-shm space
159248 ** instead of a file on disk. */
159249 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
159250 if( eStage==OTA_STAGE_OAL || eStage==OTA_STAGE_MOVE ){
159251 if( iRegion<=p->nShm ){
159252 int nByte = (iRegion+1) * sizeof(char*);
159253 char **apNew = (char**)sqlite3_realloc(p->apShm, nByte);
159254 if( apNew==0 ){
159255 rc = SQLITE_NOMEM;
159256 }else{
159257 memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
159258 p->apShm = apNew;
159259 p->nShm = iRegion+1;
159260 }
159261 }
159262
159263 if( rc==SQLITE_OK && p->apShm[iRegion]==0 ){
159264 char *pNew = (char*)sqlite3_malloc(szRegion);
159265 if( pNew==0 ){
159266 rc = SQLITE_NOMEM;
159267 }else{
159268 memset(pNew, 0, szRegion);
159269 p->apShm[iRegion] = pNew;
159270 }
159271 }
159272
159273 if( rc==SQLITE_OK ){
159274 *pp = p->apShm[iRegion];
159275 }else{
159276 *pp = 0;
159277 }
159278 }else{
159279 assert( p->apShm==0 );
159280 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
159281 }
159282
159283 return rc;
159284 }
159285
159286 /*
159287 ** Memory barrier.
159288 */
159289 static void otaVfsShmBarrier(sqlite3_file *pFile){
159290 ota_file *p = (ota_file *)pFile;
159291 p->pReal->pMethods->xShmBarrier(p->pReal);
159292 }
159293
159294 /*
159295 ** The xShmUnmap method.
159296 */
159297 static int otaVfsShmUnmap(sqlite3_file *pFile, int delFlag){
159298 ota_file *p = (ota_file*)pFile;
159299 int rc = SQLITE_OK;
159300 int eStage = (p->pOta ? p->pOta->eStage : 0);
159301
159302 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
159303 if( eStage==OTA_STAGE_OAL || eStage==OTA_STAGE_MOVE ){
159304 /* no-op */
159305 }else{
159306 /* Release the checkpointer and writer locks */
159307 otaUnlockShm(p);
159308 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
159309 }
159310 return rc;
159311 }
159312
159313 /*
159314 ** Given that zWal points to a buffer containing a wal file name passed to
159315 ** either the xOpen() or xAccess() VFS method, return a pointer to the
159316 ** file-handle opened by the same database connection on the corresponding
159317 ** database file.
159318 */
159319 static ota_file *otaFindMaindb(ota_vfs *pOtaVfs, const char *zWal){
159320 ota_file *pDb;
159321 sqlite3_mutex_enter(pOtaVfs->mutex);
159322 for(pDb=pOtaVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext);
159323 sqlite3_mutex_leave(pOtaVfs->mutex);
159324 return pDb;
159325 }
159326
159327 /*
159328 ** Open an ota file handle.
159329 */
159330 static int otaVfsOpen(
159331 sqlite3_vfs *pVfs,
159332 const char *zName,
159333 sqlite3_file *pFile,
159334 int flags,
159335 int *pOutFlags
159336 ){
159337 static sqlite3_io_methods otavfs_io_methods = {
159338 2, /* iVersion */
159339 otaVfsClose, /* xClose */
159340 otaVfsRead, /* xRead */
159341 otaVfsWrite, /* xWrite */
159342 otaVfsTruncate, /* xTruncate */
159343 otaVfsSync, /* xSync */
159344 otaVfsFileSize, /* xFileSize */
159345 otaVfsLock, /* xLock */
159346 otaVfsUnlock, /* xUnlock */
159347 otaVfsCheckReservedLock, /* xCheckReservedLock */
159348 otaVfsFileControl, /* xFileControl */
159349 otaVfsSectorSize, /* xSectorSize */
159350 otaVfsDeviceCharacteristics, /* xDeviceCharacteristics */
159351 otaVfsShmMap, /* xShmMap */
159352 otaVfsShmLock, /* xShmLock */
159353 otaVfsShmBarrier, /* xShmBarrier */
159354 otaVfsShmUnmap /* xShmUnmap */
159355 };
159356 ota_vfs *pOtaVfs = (ota_vfs*)pVfs;
159357 sqlite3_vfs *pRealVfs = pOtaVfs->pRealVfs;
159358 ota_file *pFd = (ota_file *)pFile;
159359 int rc = SQLITE_OK;
159360 const char *zOpen = zName;
159361
159362 memset(pFd, 0, sizeof(ota_file));
159363 pFd->pReal = (sqlite3_file*)&pFd[1];
159364 pFd->pOtaVfs = pOtaVfs;
159365 pFd->openFlags = flags;
159366 if( zName ){
159367 if( flags & SQLITE_OPEN_MAIN_DB ){
159368 /* A main database has just been opened. The following block sets
159369 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
159370 ** the name of the *-wal file this db connection will use. SQLite
159371 ** happens to pass a pointer to this buffer when using xAccess()
159372 ** or xOpen() to operate on the *-wal file. */
159373 int n = strlen(zName);
159374 const char *z = &zName[n];
159375 if( flags & SQLITE_OPEN_URI ){
159376 int odd = 0;
159377 while( 1 ){
159378 if( z[0]==0 ){
159379 odd = 1 - odd;
159380 if( odd && z[1]==0 ) break;
159381 }
159382 z++;
159383 }
159384 z += 2;
159385 }else{
159386 while( *z==0 ) z++;
159387 }
159388 z += (n + 8 + 1);
159389 pFd->zWal = z;
159390 }
159391 else if( flags & SQLITE_OPEN_WAL ){
159392 ota_file *pDb = otaFindMaindb(pOtaVfs, zName);
159393 if( pDb ){
159394 if( pDb->pOta && pDb->pOta->eStage==OTA_STAGE_OAL ){
159395 /* This call is to open a *-wal file. Intead, open the *-oal. This
159396 ** code ensures that the string passed to xOpen() is terminated by a
159397 ** pair of '\0' bytes in case the VFS attempts to extract a URI
159398 ** parameter from it. */
159399 int nCopy = strlen(zName);
159400 char *zCopy = sqlite3_malloc(nCopy+2);
159401 if( zCopy ){
159402 memcpy(zCopy, zName, nCopy);
159403 zCopy[nCopy-3] = 'o';
159404 zCopy[nCopy] = '\0';
159405 zCopy[nCopy+1] = '\0';
159406 zOpen = (const char*)(pFd->zDel = zCopy);
159407 }else{
159408 rc = SQLITE_NOMEM;
159409 }
159410 pFd->pOta = pDb->pOta;
159411 }
159412 pDb->pWalFd = pFd;
159413 }
159414 }
159415 }
159416
159417 if( rc==SQLITE_OK ){
159418 rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, flags, pOutFlags);
159419 }
159420 if( pFd->pReal->pMethods ){
159421 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
159422 ** pointer and, if the file is a main database file, link it into the
159423 ** mutex protected linked list of all such files. */
159424 pFile->pMethods = &otavfs_io_methods;
159425 if( flags & SQLITE_OPEN_MAIN_DB ){
159426 sqlite3_mutex_enter(pOtaVfs->mutex);
159427 pFd->pMainNext = pOtaVfs->pMain;
159428 pOtaVfs->pMain = pFd;
159429 sqlite3_mutex_leave(pOtaVfs->mutex);
159430 }
159431 }else{
159432 sqlite3_free(pFd->zDel);
159433 }
159434
159435 return rc;
159436 }
159437
159438 /*
159439 ** Delete the file located at zPath.
159440 */
159441 static int otaVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
159442 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159443 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
159444 }
159445
159446 /*
159447 ** Test for access permissions. Return true if the requested permission
159448 ** is available, or false otherwise.
159449 */
159450 static int otaVfsAccess(
159451 sqlite3_vfs *pVfs,
159452 const char *zPath,
159453 int flags,
159454 int *pResOut
159455 ){
159456 ota_vfs *pOtaVfs = (ota_vfs*)pVfs;
159457 sqlite3_vfs *pRealVfs = pOtaVfs->pRealVfs;
159458 int rc;
159459
159460 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
159461
159462 /* If this call is to check if a *-wal file associated with an OTA target
159463 ** database connection exists, and the OTA update is in OTA_STAGE_OAL,
159464 ** the following special handling is activated:
159465 **
159466 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
159467 ** ensures that the OTA extension never tries to update a database
159468 ** in wal mode, even if the first page of the database file has
159469 ** been damaged.
159470 **
159471 ** b) if the *-wal file does not exist, claim that it does anyway,
159472 ** causing SQLite to call xOpen() to open it. This call will also
159473 ** be intercepted (see the otaVfsOpen() function) and the *-oal
159474 ** file opened instead.
159475 */
159476 if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
159477 ota_file *pDb = otaFindMaindb(pOtaVfs, zPath);
159478 if( pDb && pDb->pOta && pDb->pOta->eStage==OTA_STAGE_OAL ){
159479 if( *pResOut ){
159480 rc = SQLITE_CANTOPEN;
159481 }else{
159482 *pResOut = 1;
159483 }
159484 }
159485 }
159486
159487 return rc;
159488 }
159489
159490 /*
159491 ** Populate buffer zOut with the full canonical pathname corresponding
159492 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
159493 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
159494 */
159495 static int otaVfsFullPathname(
159496 sqlite3_vfs *pVfs,
159497 const char *zPath,
159498 int nOut,
159499 char *zOut
159500 ){
159501 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159502 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
159503 }
159504
159505 #ifndef SQLITE_OMIT_LOAD_EXTENSION
159506 /*
159507 ** Open the dynamic library located at zPath and return a handle.
159508 */
159509 static void *otaVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
159510 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159511 return pRealVfs->xDlOpen(pRealVfs, zPath);
159512 }
159513
159514 /*
159515 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
159516 ** utf-8 string describing the most recent error encountered associated
159517 ** with dynamic libraries.
159518 */
159519 static void otaVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
159520 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159521 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
159522 }
159523
159524 /*
159525 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
159526 */
159527 static void (*otaVfsDlSym(
159528 sqlite3_vfs *pVfs,
159529 void *pArg,
159530 const char *zSym
159531 ))(void){
159532 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159533 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
159534 }
159535
159536 /*
159537 ** Close the dynamic library handle pHandle.
159538 */
159539 static void otaVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
159540 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159541 return pRealVfs->xDlClose(pRealVfs, pHandle);
159542 }
159543 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
159544
159545 /*
159546 ** Populate the buffer pointed to by zBufOut with nByte bytes of
159547 ** random data.
159548 */
159549 static int otaVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
159550 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159551 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
159552 }
159553
159554 /*
159555 ** Sleep for nMicro microseconds. Return the number of microseconds
159556 ** actually slept.
159557 */
159558 static int otaVfsSleep(sqlite3_vfs *pVfs, int nMicro){
159559 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159560 return pRealVfs->xSleep(pRealVfs, nMicro);
159561 }
159562
159563 /*
159564 ** Return the current time as a Julian Day number in *pTimeOut.
159565 */
159566 static int otaVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
159567 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
159568 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
159569 }
159570
159571 /*
159572 ** No-op.
159573 */
159574 static int otaVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
159575 return 0;
159576 }
159577
159578 /*
159579 ** Deregister and destroy an OTA vfs created by an earlier call to
159580 ** sqlite3ota_create_vfs().
159581 */
159582 SQLITE_API void SQLITE_STDCALL sqlite3ota_destroy_vfs(const char *zName){
159583 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
159584 if( pVfs && pVfs->xOpen==otaVfsOpen ){
159585 sqlite3_mutex_free(((ota_vfs*)pVfs)->mutex);
159586 sqlite3_vfs_unregister(pVfs);
159587 sqlite3_free(pVfs);
159588 }
159589 }
159590
159591 /*
159592 ** Create an OTA VFS named zName that accesses the underlying file-system
159593 ** via existing VFS zParent. The new object is registered as a non-default
159594 ** VFS with SQLite before returning.
159595 */
159596 SQLITE_API int SQLITE_STDCALL sqlite3ota_create_vfs(const char *zName, const char *zParent){
159597
159598 /* Template for VFS */
159599 static sqlite3_vfs vfs_template = {
159600 1, /* iVersion */
159601 0, /* szOsFile */
159602 0, /* mxPathname */
159603 0, /* pNext */
159604 0, /* zName */
159605 0, /* pAppData */
159606 otaVfsOpen, /* xOpen */
159607 otaVfsDelete, /* xDelete */
159608 otaVfsAccess, /* xAccess */
159609 otaVfsFullPathname, /* xFullPathname */
159610
159611 #ifndef SQLITE_OMIT_LOAD_EXTENSION
159612 otaVfsDlOpen, /* xDlOpen */
159613 otaVfsDlError, /* xDlError */
159614 otaVfsDlSym, /* xDlSym */
159615 otaVfsDlClose, /* xDlClose */
159616 #else
159617 0, 0, 0, 0,
159618 #endif
159619
159620 otaVfsRandomness, /* xRandomness */
159621 otaVfsSleep, /* xSleep */
159622 otaVfsCurrentTime, /* xCurrentTime */
159623 otaVfsGetLastError, /* xGetLastError */
159624 0, /* xCurrentTimeInt64 (version 2) */
159625 0, 0, 0 /* Unimplemented version 3 methods */
159626 };
159627
159628 ota_vfs *pNew = 0; /* Newly allocated VFS */
159629 int nName;
159630 int rc = SQLITE_OK;
159631
159632 int nByte;
159633 nName = strlen(zName);
159634 nByte = sizeof(ota_vfs) + nName + 1;
159635 pNew = (ota_vfs*)sqlite3_malloc(nByte);
159636 if( pNew==0 ){
159637 rc = SQLITE_NOMEM;
159638 }else{
159639 sqlite3_vfs *pParent; /* Parent VFS */
159640 memset(pNew, 0, nByte);
159641 pParent = sqlite3_vfs_find(zParent);
159642 if( pParent==0 ){
159643 rc = SQLITE_NOTFOUND;
159644 }else{
159645 char *zSpace;
159646 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
159647 pNew->base.mxPathname = pParent->mxPathname;
159648 pNew->base.szOsFile = sizeof(ota_file) + pParent->szOsFile;
159649 pNew->pRealVfs = pParent;
159650 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
159651 memcpy(zSpace, zName, nName);
159652
159653 /* Allocate the mutex and register the new VFS (not as the default) */
159654 pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
159655 if( pNew->mutex==0 ){
159656 rc = SQLITE_NOMEM;
159657 }else{
159658 rc = sqlite3_vfs_register(&pNew->base, 0);
159659 }
159660 }
159661
159662 if( rc!=SQLITE_OK ){
159663 sqlite3_mutex_free(pNew->mutex);
159664 sqlite3_free(pNew);
159665 }
159666 }
159667
159668 return rc;
159669 }
159670
159671
159672 /**************************************************************************/
159673
159674 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_OTA) */
159675
159676 /************** End of sqlite3ota.c ******************************************/
159677 /************** Begin file dbstat.c ******************************************/
159678 /*
159679 ** 2010 July 12
159680 **
159681 ** The author disclaims copyright to this source code. In place of
@@ -155834,11 +160295,11 @@
160295 }
160296
160297 /*
160298 ** Invoke this routine to register the "dbstat" virtual table module
160299 */
160300 SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){
160301 static sqlite3_module dbstat_module = {
160302 0, /* iVersion */
160303 statConnect, /* xCreate */
160304 statConnect, /* xConnect */
160305 statBestIndex, /* xBestIndex */
@@ -155859,8 +160320,10 @@
160320 0, /* xFindMethod */
160321 0, /* xRename */
160322 };
160323 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
160324 }
160325 #elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
160326 SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
160327 #endif /* SQLITE_ENABLE_DBSTAT_VTAB */
160328
160329 /************** End of dbstat.c **********************************************/
160330
+49 -25
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -109,13 +109,13 @@
109109
**
110110
** See also: [sqlite3_libversion()],
111111
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112112
** [sqlite_version()] and [sqlite_source_id()].
113113
*/
114
-#define SQLITE_VERSION "3.8.10.2"
115
-#define SQLITE_VERSION_NUMBER 3008010
116
-#define SQLITE_SOURCE_ID "2015-05-20 18:17:19 2ef4f3a5b1d1d0c4338f8243d40a2452cc1f7fe4"
114
+#define SQLITE_VERSION "3.8.11"
115
+#define SQLITE_VERSION_NUMBER 3008011
116
+#define SQLITE_SOURCE_ID "2015-05-29 17:51:16 db4e9728fae5f7b0fad6aa0a5be317a7c9e7c417"
117117
118118
/*
119119
** CAPI3REF: Run-Time Library Version Numbers
120120
** KEYWORDS: sqlite3_version, sqlite3_sourceid
121121
**
@@ -954,17 +954,25 @@
954954
** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
955955
** opcode causes the xFileControl method to swap the file handle with the one
956956
** pointed to by the pArg argument. This capability is used during testing
957957
** and only needs to be supported when SQLITE_TEST is defined.
958958
**
959
-** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
959
+* <li>[[SQLITE_FCNTL_WAL_BLOCK]]
960960
** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
961961
** be advantageous to block on the next WAL lock if the lock is not immediately
962962
** available. The WAL subsystem issues this signal during rare
963963
** circumstances in order to fix a problem with priority inversion.
964964
** Applications should <em>not</em> use this file-control.
965965
**
966
+** <li>[[SQLITE_FCNTL_ZIPVFS]]
967
+** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
968
+** VFS should return SQLITE_NOTFOUND for this opcode.
969
+**
970
+** <li>[[SQLITE_FCNTL_OTA]]
971
+** The [SQLITE_FCNTL_OTA] opcode is implemented by the special VFS used by
972
+** the OTA extension only. All other VFS should return SQLITE_NOTFOUND for
973
+** this opcode.
966974
** </ul>
967975
*/
968976
#define SQLITE_FCNTL_LOCKSTATE 1
969977
#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
970978
#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -986,10 +994,12 @@
986994
#define SQLITE_FCNTL_HAS_MOVED 20
987995
#define SQLITE_FCNTL_SYNC 21
988996
#define SQLITE_FCNTL_COMMIT_PHASETWO 22
989997
#define SQLITE_FCNTL_WIN32_SET_HANDLE 23
990998
#define SQLITE_FCNTL_WAL_BLOCK 24
999
+#define SQLITE_FCNTL_ZIPVFS 25
1000
+#define SQLITE_FCNTL_OTA 26
9911001
9921002
/* deprecated names */
9931003
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
9941004
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
9951005
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -3388,11 +3398,13 @@
33883398
**
33893399
** An sqlite3_value object may be either "protected" or "unprotected".
33903400
** Some interfaces require a protected sqlite3_value. Other interfaces
33913401
** will accept either a protected or an unprotected sqlite3_value.
33923402
** Every interface that accepts sqlite3_value arguments specifies
3393
-** whether or not it requires a protected sqlite3_value.
3403
+** whether or not it requires a protected sqlite3_value. The
3404
+** [sqlite3_value_dup()] interface can be used to construct a new
3405
+** protected sqlite3_value from an unprotected sqlite3_value.
33943406
**
33953407
** The terms "protected" and "unprotected" refer to whether or not
33963408
** a mutex is held. An internal mutex is held for a protected
33973409
** sqlite3_value object but no mutex is held for an unprotected
33983410
** sqlite3_value object. If SQLite is compiled to be single-threaded
@@ -3891,12 +3903,10 @@
38913903
/*
38923904
** CAPI3REF: Result Values From A Query
38933905
** KEYWORDS: {column access functions}
38943906
** METHOD: sqlite3_stmt
38953907
**
3896
-** These routines form the "result set" interface.
3897
-**
38983908
** ^These routines return information about a single column of the current
38993909
** result row of a query. ^In every case the first argument is a pointer
39003910
** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
39013911
** that was returned from [sqlite3_prepare_v2()] or one of its variants)
39023912
** and the second argument is the index of the column for which information
@@ -3952,17 +3962,18 @@
39523962
**
39533963
** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
39543964
** even empty strings, are always zero-terminated. ^The return
39553965
** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
39563966
**
3957
-** ^The object returned by [sqlite3_column_value()] is an
3958
-** [unprotected sqlite3_value] object. An unprotected sqlite3_value object
3959
-** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
3967
+** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
3968
+** [unprotected sqlite3_value] object. In a multithreaded environment,
3969
+** an unprotected sqlite3_value object may only be used safely with
3970
+** [sqlite3_bind_value()] and [sqlite3_result_value()].
39603971
** If the [unprotected sqlite3_value] object returned by
39613972
** [sqlite3_column_value()] is used in any other way, including calls
39623973
** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
3963
-** or [sqlite3_value_bytes()], then the behavior is undefined.
3974
+** or [sqlite3_value_bytes()], the behavior is not threadsafe.
39643975
**
39653976
** These routines attempt to convert the value where appropriate. ^For
39663977
** example, if the internal representation is FLOAT and a text result
39673978
** is requested, [sqlite3_snprintf()] is used internally to perform the
39683979
** conversion automatically. ^(The following table details the conversions
@@ -3989,16 +4000,10 @@
39894000
** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
39904001
** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
39914002
** </table>
39924003
** </blockquote>)^
39934004
**
3994
-** The table above makes reference to standard C library functions atoi()
3995
-** and atof(). SQLite does not really use these functions. It has its
3996
-** own equivalent internal routines. The atoi() and atof() names are
3997
-** used in the table for brevity and because they are familiar to most
3998
-** C programmers.
3999
-**
40004005
** Note that when type conversions occur, pointers returned by prior
40014006
** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
40024007
** sqlite3_column_text16() may be invalidated.
40034008
** Type conversions and pointer invalidations might occur
40044009
** in the following cases:
@@ -4019,11 +4024,11 @@
40194024
** not invalidate a prior pointer, though of course the content of the buffer
40204025
** that the prior pointer references will have been modified. Other kinds
40214026
** of conversion are done in place when it is possible, but sometimes they
40224027
** are not possible and in those cases prior pointers are invalidated.
40234028
**
4024
-** The safest and easiest to remember policy is to invoke these routines
4029
+** The safest policy is to invoke these routines
40254030
** in one of the following ways:
40264031
**
40274032
** <ul>
40284033
** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
40294034
** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
@@ -4039,11 +4044,11 @@
40394044
** with calls to sqlite3_column_bytes().
40404045
**
40414046
** ^The pointers returned are valid until a type conversion occurs as
40424047
** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
40434048
** [sqlite3_finalize()] is called. ^The memory space used to hold strings
4044
-** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned
4049
+** and BLOBs is freed automatically. Do <em>not</em> pass the pointers returned
40454050
** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
40464051
** [sqlite3_free()].
40474052
**
40484053
** ^(If a memory allocation error occurs during the evaluation of any
40494054
** of these routines, a default value is returned. The default value
@@ -4289,16 +4294,16 @@
42894294
SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
42904295
void*,sqlite3_int64);
42914296
#endif
42924297
42934298
/*
4294
-** CAPI3REF: Obtaining SQL Function Parameter Values
4299
+** CAPI3REF: Obtaining SQL Values
42954300
** METHOD: sqlite3_value
42964301
**
42974302
** The C-language implementation of SQL functions and aggregates uses
42984303
** this set of interface routines to access the parameter values on
4299
-** the function or aggregate.
4304
+** the function or aggregate.
43004305
**
43014306
** The xFunc (for scalar functions) or xStep (for aggregates) parameters
43024307
** to [sqlite3_create_function()] and [sqlite3_create_function16()]
43034308
** define callbacks that implement the SQL functions and aggregates.
43044309
** The 3rd parameter to these callbacks is an array of pointers to
@@ -4347,10 +4352,27 @@
43474352
SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value*);
43484353
SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value*);
43494354
SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value*);
43504355
SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*);
43514356
4357
+/*
4358
+** CAPI3REF: Copy And Free SQL Values
4359
+** METHOD: sqlite3_value
4360
+**
4361
+** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
4362
+** object D and returns a pointer to that copy. ^The [sqlite3_value] returned
4363
+** is a [protected sqlite3_value] object even if the input is not.
4364
+** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
4365
+** memory allocation fails.
4366
+**
4367
+** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
4368
+** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer
4369
+** then sqlite3_value_free(V) is a harmless no-op.
4370
+*/
4371
+SQLITE_API SQLITE_EXPERIMENTAL sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value*);
4372
+SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_value_free(sqlite3_value*);
4373
+
43524374
/*
43534375
** CAPI3REF: Obtain Aggregate Function Context
43544376
** METHOD: sqlite3_context
43554377
**
43564378
** Implementations of aggregate SQL functions use this
@@ -4594,11 +4616,11 @@
45944616
** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
45954617
** then SQLite makes a copy of the result into space obtained from
45964618
** from [sqlite3_malloc()] before it returns.
45974619
**
45984620
** ^The sqlite3_result_value() interface sets the result of
4599
-** the application-defined function to be a copy the
4621
+** the application-defined function to be a copy of the
46004622
** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
46014623
** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
46024624
** so that the [sqlite3_value] specified in the parameter may change or
46034625
** be deallocated after sqlite3_result_value() returns without harm.
46044626
** ^A [protected sqlite3_value] object may always be used where an
@@ -5870,11 +5892,11 @@
58705892
** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
58715893
** always returns zero.
58725894
**
58735895
** ^This function sets the database handle error code and message.
58745896
*/
5875
-SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
5897
+SQLITE_API int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
58765898
58775899
/*
58785900
** CAPI3REF: Close A BLOB Handle
58795901
** DESTRUCTOR: sqlite3_blob
58805902
**
@@ -7680,11 +7702,11 @@
76807702
** as if the loop did not exist - it returns non-zero and leave the variable
76817703
** that pOut points to unchanged.
76827704
**
76837705
** See also: [sqlite3_stmt_scanstatus_reset()]
76847706
*/
7685
-SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_stmt_scanstatus(
7707
+SQLITE_API int SQLITE_STDCALL sqlite3_stmt_scanstatus(
76867708
sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
76877709
int idx, /* Index of loop to report on */
76887710
int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
76897711
void *pOut /* Result written here */
76907712
);
@@ -7696,11 +7718,11 @@
76967718
** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
76977719
**
76987720
** This API is only available if the library is built with pre-processor
76997721
** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
77007722
*/
7701
-SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
7723
+SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
77027724
77037725
77047726
/*
77057727
** Undo the hack that converts floating point types to integer for
77067728
** builds on processors without floating point support.
@@ -7811,10 +7833,12 @@
78117833
sqlite3_int64 iRowid; /* Rowid for current entry */
78127834
sqlite3_rtree_dbl rParentScore; /* Score of parent node */
78137835
int eParentWithin; /* Visibility of parent node */
78147836
int eWithin; /* OUT: Visiblity */
78157837
sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
7838
+ /* The following fields are only available in 3.8.11 and later */
7839
+ sqlite3_value **apSqlParam; /* Original SQL values of parameters */
78167840
};
78177841
78187842
/*
78197843
** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
78207844
*/
78217845
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -109,13 +109,13 @@
109 **
110 ** See also: [sqlite3_libversion()],
111 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112 ** [sqlite_version()] and [sqlite_source_id()].
113 */
114 #define SQLITE_VERSION "3.8.10.2"
115 #define SQLITE_VERSION_NUMBER 3008010
116 #define SQLITE_SOURCE_ID "2015-05-20 18:17:19 2ef4f3a5b1d1d0c4338f8243d40a2452cc1f7fe4"
117
118 /*
119 ** CAPI3REF: Run-Time Library Version Numbers
120 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
121 **
@@ -954,17 +954,25 @@
954 ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
955 ** opcode causes the xFileControl method to swap the file handle with the one
956 ** pointed to by the pArg argument. This capability is used during testing
957 ** and only needs to be supported when SQLITE_TEST is defined.
958 **
959 ** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
960 ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
961 ** be advantageous to block on the next WAL lock if the lock is not immediately
962 ** available. The WAL subsystem issues this signal during rare
963 ** circumstances in order to fix a problem with priority inversion.
964 ** Applications should <em>not</em> use this file-control.
965 **
 
 
 
 
 
 
 
 
966 ** </ul>
967 */
968 #define SQLITE_FCNTL_LOCKSTATE 1
969 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
970 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -986,10 +994,12 @@
986 #define SQLITE_FCNTL_HAS_MOVED 20
987 #define SQLITE_FCNTL_SYNC 21
988 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
989 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
990 #define SQLITE_FCNTL_WAL_BLOCK 24
 
 
991
992 /* deprecated names */
993 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
994 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
995 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -3388,11 +3398,13 @@
3388 **
3389 ** An sqlite3_value object may be either "protected" or "unprotected".
3390 ** Some interfaces require a protected sqlite3_value. Other interfaces
3391 ** will accept either a protected or an unprotected sqlite3_value.
3392 ** Every interface that accepts sqlite3_value arguments specifies
3393 ** whether or not it requires a protected sqlite3_value.
 
 
3394 **
3395 ** The terms "protected" and "unprotected" refer to whether or not
3396 ** a mutex is held. An internal mutex is held for a protected
3397 ** sqlite3_value object but no mutex is held for an unprotected
3398 ** sqlite3_value object. If SQLite is compiled to be single-threaded
@@ -3891,12 +3903,10 @@
3891 /*
3892 ** CAPI3REF: Result Values From A Query
3893 ** KEYWORDS: {column access functions}
3894 ** METHOD: sqlite3_stmt
3895 **
3896 ** These routines form the "result set" interface.
3897 **
3898 ** ^These routines return information about a single column of the current
3899 ** result row of a query. ^In every case the first argument is a pointer
3900 ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
3901 ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
3902 ** and the second argument is the index of the column for which information
@@ -3952,17 +3962,18 @@
3952 **
3953 ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
3954 ** even empty strings, are always zero-terminated. ^The return
3955 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
3956 **
3957 ** ^The object returned by [sqlite3_column_value()] is an
3958 ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object
3959 ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
 
3960 ** If the [unprotected sqlite3_value] object returned by
3961 ** [sqlite3_column_value()] is used in any other way, including calls
3962 ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
3963 ** or [sqlite3_value_bytes()], then the behavior is undefined.
3964 **
3965 ** These routines attempt to convert the value where appropriate. ^For
3966 ** example, if the internal representation is FLOAT and a text result
3967 ** is requested, [sqlite3_snprintf()] is used internally to perform the
3968 ** conversion automatically. ^(The following table details the conversions
@@ -3989,16 +4000,10 @@
3989 ** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
3990 ** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
3991 ** </table>
3992 ** </blockquote>)^
3993 **
3994 ** The table above makes reference to standard C library functions atoi()
3995 ** and atof(). SQLite does not really use these functions. It has its
3996 ** own equivalent internal routines. The atoi() and atof() names are
3997 ** used in the table for brevity and because they are familiar to most
3998 ** C programmers.
3999 **
4000 ** Note that when type conversions occur, pointers returned by prior
4001 ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
4002 ** sqlite3_column_text16() may be invalidated.
4003 ** Type conversions and pointer invalidations might occur
4004 ** in the following cases:
@@ -4019,11 +4024,11 @@
4019 ** not invalidate a prior pointer, though of course the content of the buffer
4020 ** that the prior pointer references will have been modified. Other kinds
4021 ** of conversion are done in place when it is possible, but sometimes they
4022 ** are not possible and in those cases prior pointers are invalidated.
4023 **
4024 ** The safest and easiest to remember policy is to invoke these routines
4025 ** in one of the following ways:
4026 **
4027 ** <ul>
4028 ** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
4029 ** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
@@ -4039,11 +4044,11 @@
4039 ** with calls to sqlite3_column_bytes().
4040 **
4041 ** ^The pointers returned are valid until a type conversion occurs as
4042 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
4043 ** [sqlite3_finalize()] is called. ^The memory space used to hold strings
4044 ** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned
4045 ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
4046 ** [sqlite3_free()].
4047 **
4048 ** ^(If a memory allocation error occurs during the evaluation of any
4049 ** of these routines, a default value is returned. The default value
@@ -4289,16 +4294,16 @@
4289 SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
4290 void*,sqlite3_int64);
4291 #endif
4292
4293 /*
4294 ** CAPI3REF: Obtaining SQL Function Parameter Values
4295 ** METHOD: sqlite3_value
4296 **
4297 ** The C-language implementation of SQL functions and aggregates uses
4298 ** this set of interface routines to access the parameter values on
4299 ** the function or aggregate.
4300 **
4301 ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
4302 ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
4303 ** define callbacks that implement the SQL functions and aggregates.
4304 ** The 3rd parameter to these callbacks is an array of pointers to
@@ -4347,10 +4352,27 @@
4347 SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value*);
4348 SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value*);
4349 SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value*);
4350 SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*);
4351
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4352 /*
4353 ** CAPI3REF: Obtain Aggregate Function Context
4354 ** METHOD: sqlite3_context
4355 **
4356 ** Implementations of aggregate SQL functions use this
@@ -4594,11 +4616,11 @@
4594 ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
4595 ** then SQLite makes a copy of the result into space obtained from
4596 ** from [sqlite3_malloc()] before it returns.
4597 **
4598 ** ^The sqlite3_result_value() interface sets the result of
4599 ** the application-defined function to be a copy the
4600 ** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
4601 ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
4602 ** so that the [sqlite3_value] specified in the parameter may change or
4603 ** be deallocated after sqlite3_result_value() returns without harm.
4604 ** ^A [protected sqlite3_value] object may always be used where an
@@ -5870,11 +5892,11 @@
5870 ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
5871 ** always returns zero.
5872 **
5873 ** ^This function sets the database handle error code and message.
5874 */
5875 SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
5876
5877 /*
5878 ** CAPI3REF: Close A BLOB Handle
5879 ** DESTRUCTOR: sqlite3_blob
5880 **
@@ -7680,11 +7702,11 @@
7680 ** as if the loop did not exist - it returns non-zero and leave the variable
7681 ** that pOut points to unchanged.
7682 **
7683 ** See also: [sqlite3_stmt_scanstatus_reset()]
7684 */
7685 SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_stmt_scanstatus(
7686 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
7687 int idx, /* Index of loop to report on */
7688 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
7689 void *pOut /* Result written here */
7690 );
@@ -7696,11 +7718,11 @@
7696 ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
7697 **
7698 ** This API is only available if the library is built with pre-processor
7699 ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
7700 */
7701 SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
7702
7703
7704 /*
7705 ** Undo the hack that converts floating point types to integer for
7706 ** builds on processors without floating point support.
@@ -7811,10 +7833,12 @@
7811 sqlite3_int64 iRowid; /* Rowid for current entry */
7812 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
7813 int eParentWithin; /* Visibility of parent node */
7814 int eWithin; /* OUT: Visiblity */
7815 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
 
 
7816 };
7817
7818 /*
7819 ** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
7820 */
7821
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -109,13 +109,13 @@
109 **
110 ** See also: [sqlite3_libversion()],
111 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112 ** [sqlite_version()] and [sqlite_source_id()].
113 */
114 #define SQLITE_VERSION "3.8.11"
115 #define SQLITE_VERSION_NUMBER 3008011
116 #define SQLITE_SOURCE_ID "2015-05-29 17:51:16 db4e9728fae5f7b0fad6aa0a5be317a7c9e7c417"
117
118 /*
119 ** CAPI3REF: Run-Time Library Version Numbers
120 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
121 **
@@ -954,17 +954,25 @@
954 ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
955 ** opcode causes the xFileControl method to swap the file handle with the one
956 ** pointed to by the pArg argument. This capability is used during testing
957 ** and only needs to be supported when SQLITE_TEST is defined.
958 **
959 * <li>[[SQLITE_FCNTL_WAL_BLOCK]]
960 ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
961 ** be advantageous to block on the next WAL lock if the lock is not immediately
962 ** available. The WAL subsystem issues this signal during rare
963 ** circumstances in order to fix a problem with priority inversion.
964 ** Applications should <em>not</em> use this file-control.
965 **
966 ** <li>[[SQLITE_FCNTL_ZIPVFS]]
967 ** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
968 ** VFS should return SQLITE_NOTFOUND for this opcode.
969 **
970 ** <li>[[SQLITE_FCNTL_OTA]]
971 ** The [SQLITE_FCNTL_OTA] opcode is implemented by the special VFS used by
972 ** the OTA extension only. All other VFS should return SQLITE_NOTFOUND for
973 ** this opcode.
974 ** </ul>
975 */
976 #define SQLITE_FCNTL_LOCKSTATE 1
977 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
978 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -986,10 +994,12 @@
994 #define SQLITE_FCNTL_HAS_MOVED 20
995 #define SQLITE_FCNTL_SYNC 21
996 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
997 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
998 #define SQLITE_FCNTL_WAL_BLOCK 24
999 #define SQLITE_FCNTL_ZIPVFS 25
1000 #define SQLITE_FCNTL_OTA 26
1001
1002 /* deprecated names */
1003 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1004 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1005 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -3388,11 +3398,13 @@
3398 **
3399 ** An sqlite3_value object may be either "protected" or "unprotected".
3400 ** Some interfaces require a protected sqlite3_value. Other interfaces
3401 ** will accept either a protected or an unprotected sqlite3_value.
3402 ** Every interface that accepts sqlite3_value arguments specifies
3403 ** whether or not it requires a protected sqlite3_value. The
3404 ** [sqlite3_value_dup()] interface can be used to construct a new
3405 ** protected sqlite3_value from an unprotected sqlite3_value.
3406 **
3407 ** The terms "protected" and "unprotected" refer to whether or not
3408 ** a mutex is held. An internal mutex is held for a protected
3409 ** sqlite3_value object but no mutex is held for an unprotected
3410 ** sqlite3_value object. If SQLite is compiled to be single-threaded
@@ -3891,12 +3903,10 @@
3903 /*
3904 ** CAPI3REF: Result Values From A Query
3905 ** KEYWORDS: {column access functions}
3906 ** METHOD: sqlite3_stmt
3907 **
 
 
3908 ** ^These routines return information about a single column of the current
3909 ** result row of a query. ^In every case the first argument is a pointer
3910 ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
3911 ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
3912 ** and the second argument is the index of the column for which information
@@ -3952,17 +3962,18 @@
3962 **
3963 ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
3964 ** even empty strings, are always zero-terminated. ^The return
3965 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
3966 **
3967 ** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
3968 ** [unprotected sqlite3_value] object. In a multithreaded environment,
3969 ** an unprotected sqlite3_value object may only be used safely with
3970 ** [sqlite3_bind_value()] and [sqlite3_result_value()].
3971 ** If the [unprotected sqlite3_value] object returned by
3972 ** [sqlite3_column_value()] is used in any other way, including calls
3973 ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
3974 ** or [sqlite3_value_bytes()], the behavior is not threadsafe.
3975 **
3976 ** These routines attempt to convert the value where appropriate. ^For
3977 ** example, if the internal representation is FLOAT and a text result
3978 ** is requested, [sqlite3_snprintf()] is used internally to perform the
3979 ** conversion automatically. ^(The following table details the conversions
@@ -3989,16 +4000,10 @@
4000 ** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
4001 ** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
4002 ** </table>
4003 ** </blockquote>)^
4004 **
 
 
 
 
 
 
4005 ** Note that when type conversions occur, pointers returned by prior
4006 ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
4007 ** sqlite3_column_text16() may be invalidated.
4008 ** Type conversions and pointer invalidations might occur
4009 ** in the following cases:
@@ -4019,11 +4024,11 @@
4024 ** not invalidate a prior pointer, though of course the content of the buffer
4025 ** that the prior pointer references will have been modified. Other kinds
4026 ** of conversion are done in place when it is possible, but sometimes they
4027 ** are not possible and in those cases prior pointers are invalidated.
4028 **
4029 ** The safest policy is to invoke these routines
4030 ** in one of the following ways:
4031 **
4032 ** <ul>
4033 ** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
4034 ** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
@@ -4039,11 +4044,11 @@
4044 ** with calls to sqlite3_column_bytes().
4045 **
4046 ** ^The pointers returned are valid until a type conversion occurs as
4047 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
4048 ** [sqlite3_finalize()] is called. ^The memory space used to hold strings
4049 ** and BLOBs is freed automatically. Do <em>not</em> pass the pointers returned
4050 ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
4051 ** [sqlite3_free()].
4052 **
4053 ** ^(If a memory allocation error occurs during the evaluation of any
4054 ** of these routines, a default value is returned. The default value
@@ -4289,16 +4294,16 @@
4294 SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
4295 void*,sqlite3_int64);
4296 #endif
4297
4298 /*
4299 ** CAPI3REF: Obtaining SQL Values
4300 ** METHOD: sqlite3_value
4301 **
4302 ** The C-language implementation of SQL functions and aggregates uses
4303 ** this set of interface routines to access the parameter values on
4304 ** the function or aggregate.
4305 **
4306 ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
4307 ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
4308 ** define callbacks that implement the SQL functions and aggregates.
4309 ** The 3rd parameter to these callbacks is an array of pointers to
@@ -4347,10 +4352,27 @@
4352 SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value*);
4353 SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value*);
4354 SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value*);
4355 SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*);
4356
4357 /*
4358 ** CAPI3REF: Copy And Free SQL Values
4359 ** METHOD: sqlite3_value
4360 **
4361 ** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
4362 ** object D and returns a pointer to that copy. ^The [sqlite3_value] returned
4363 ** is a [protected sqlite3_value] object even if the input is not.
4364 ** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
4365 ** memory allocation fails.
4366 **
4367 ** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
4368 ** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer
4369 ** then sqlite3_value_free(V) is a harmless no-op.
4370 */
4371 SQLITE_API SQLITE_EXPERIMENTAL sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value*);
4372 SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_value_free(sqlite3_value*);
4373
4374 /*
4375 ** CAPI3REF: Obtain Aggregate Function Context
4376 ** METHOD: sqlite3_context
4377 **
4378 ** Implementations of aggregate SQL functions use this
@@ -4594,11 +4616,11 @@
4616 ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
4617 ** then SQLite makes a copy of the result into space obtained from
4618 ** from [sqlite3_malloc()] before it returns.
4619 **
4620 ** ^The sqlite3_result_value() interface sets the result of
4621 ** the application-defined function to be a copy of the
4622 ** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
4623 ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
4624 ** so that the [sqlite3_value] specified in the parameter may change or
4625 ** be deallocated after sqlite3_result_value() returns without harm.
4626 ** ^A [protected sqlite3_value] object may always be used where an
@@ -5870,11 +5892,11 @@
5892 ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
5893 ** always returns zero.
5894 **
5895 ** ^This function sets the database handle error code and message.
5896 */
5897 SQLITE_API int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
5898
5899 /*
5900 ** CAPI3REF: Close A BLOB Handle
5901 ** DESTRUCTOR: sqlite3_blob
5902 **
@@ -7680,11 +7702,11 @@
7702 ** as if the loop did not exist - it returns non-zero and leave the variable
7703 ** that pOut points to unchanged.
7704 **
7705 ** See also: [sqlite3_stmt_scanstatus_reset()]
7706 */
7707 SQLITE_API int SQLITE_STDCALL sqlite3_stmt_scanstatus(
7708 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
7709 int idx, /* Index of loop to report on */
7710 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
7711 void *pOut /* Result written here */
7712 );
@@ -7696,11 +7718,11 @@
7718 ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
7719 **
7720 ** This API is only available if the library is built with pre-processor
7721 ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
7722 */
7723 SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
7724
7725
7726 /*
7727 ** Undo the hack that converts floating point types to integer for
7728 ** builds on processors without floating point support.
@@ -7811,10 +7833,12 @@
7833 sqlite3_int64 iRowid; /* Rowid for current entry */
7834 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
7835 int eParentWithin; /* Visibility of parent node */
7836 int eWithin; /* OUT: Visiblity */
7837 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
7838 /* The following fields are only available in 3.8.11 and later */
7839 sqlite3_value **apSqlParam; /* Original SQL values of parameters */
7840 };
7841
7842 /*
7843 ** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
7844 */
7845

Keyboard Shortcuts

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