| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.8.10.1. 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 |
| 4 | 4 | ** single large file, the entire code can be compiled as a single translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -316,13 +316,13 @@ |
| 316 | 316 | ** |
| 317 | 317 | ** See also: [sqlite3_libversion()], |
| 318 | 318 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 319 | 319 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 320 | 320 | */ |
| 321 | | -#define SQLITE_VERSION "3.8.10.1" |
| 322 | | -#define SQLITE_VERSION_NUMBER 3008010 |
| 323 | | -#define SQLITE_SOURCE_ID "2015-05-09 12:14:55 05b4b1f2a937c06c90db70c09890038f6c98ec40" |
| 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 | 324 | |
| 325 | 325 | /* |
| 326 | 326 | ** CAPI3REF: Run-Time Library Version Numbers |
| 327 | 327 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 328 | 328 | ** |
| | @@ -1161,17 +1161,25 @@ |
| 1161 | 1161 | ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This |
| 1162 | 1162 | ** opcode causes the xFileControl method to swap the file handle with the one |
| 1163 | 1163 | ** pointed to by the pArg argument. This capability is used during testing |
| 1164 | 1164 | ** and only needs to be supported when SQLITE_TEST is defined. |
| 1165 | 1165 | ** |
| 1166 | | -** <li>[[SQLITE_FCNTL_WAL_BLOCK]] |
| 1166 | +* <li>[[SQLITE_FCNTL_WAL_BLOCK]] |
| 1167 | 1167 | ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might |
| 1168 | 1168 | ** be advantageous to block on the next WAL lock if the lock is not immediately |
| 1169 | 1169 | ** available. The WAL subsystem issues this signal during rare |
| 1170 | 1170 | ** circumstances in order to fix a problem with priority inversion. |
| 1171 | 1171 | ** Applications should <em>not</em> use this file-control. |
| 1172 | 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. |
| 1173 | 1181 | ** </ul> |
| 1174 | 1182 | */ |
| 1175 | 1183 | #define SQLITE_FCNTL_LOCKSTATE 1 |
| 1176 | 1184 | #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2 |
| 1177 | 1185 | #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3 |
| | @@ -1193,10 +1201,12 @@ |
| 1193 | 1201 | #define SQLITE_FCNTL_HAS_MOVED 20 |
| 1194 | 1202 | #define SQLITE_FCNTL_SYNC 21 |
| 1195 | 1203 | #define SQLITE_FCNTL_COMMIT_PHASETWO 22 |
| 1196 | 1204 | #define SQLITE_FCNTL_WIN32_SET_HANDLE 23 |
| 1197 | 1205 | #define SQLITE_FCNTL_WAL_BLOCK 24 |
| 1206 | +#define SQLITE_FCNTL_ZIPVFS 25 |
| 1207 | +#define SQLITE_FCNTL_OTA 26 |
| 1198 | 1208 | |
| 1199 | 1209 | /* deprecated names */ |
| 1200 | 1210 | #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE |
| 1201 | 1211 | #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE |
| 1202 | 1212 | #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO |
| | @@ -3595,11 +3605,13 @@ |
| 3595 | 3605 | ** |
| 3596 | 3606 | ** An sqlite3_value object may be either "protected" or "unprotected". |
| 3597 | 3607 | ** Some interfaces require a protected sqlite3_value. Other interfaces |
| 3598 | 3608 | ** will accept either a protected or an unprotected sqlite3_value. |
| 3599 | 3609 | ** 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. |
| 3601 | 3613 | ** |
| 3602 | 3614 | ** The terms "protected" and "unprotected" refer to whether or not |
| 3603 | 3615 | ** a mutex is held. An internal mutex is held for a protected |
| 3604 | 3616 | ** sqlite3_value object but no mutex is held for an unprotected |
| 3605 | 3617 | ** sqlite3_value object. If SQLite is compiled to be single-threaded |
| | @@ -4098,12 +4110,10 @@ |
| 4098 | 4110 | /* |
| 4099 | 4111 | ** CAPI3REF: Result Values From A Query |
| 4100 | 4112 | ** KEYWORDS: {column access functions} |
| 4101 | 4113 | ** METHOD: sqlite3_stmt |
| 4102 | 4114 | ** |
| 4103 | | -** These routines form the "result set" interface. |
| 4104 | | -** |
| 4105 | 4115 | ** ^These routines return information about a single column of the current |
| 4106 | 4116 | ** result row of a query. ^In every case the first argument is a pointer |
| 4107 | 4117 | ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*] |
| 4108 | 4118 | ** that was returned from [sqlite3_prepare_v2()] or one of its variants) |
| 4109 | 4119 | ** and the second argument is the index of the column for which information |
| | @@ -4159,17 +4169,18 @@ |
| 4159 | 4169 | ** |
| 4160 | 4170 | ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(), |
| 4161 | 4171 | ** even empty strings, are always zero-terminated. ^The return |
| 4162 | 4172 | ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. |
| 4163 | 4173 | ** |
| 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()]. |
| 4167 | 4178 | ** If the [unprotected sqlite3_value] object returned by |
| 4168 | 4179 | ** [sqlite3_column_value()] is used in any other way, including calls |
| 4169 | 4180 | ** 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. |
| 4171 | 4182 | ** |
| 4172 | 4183 | ** These routines attempt to convert the value where appropriate. ^For |
| 4173 | 4184 | ** example, if the internal representation is FLOAT and a text result |
| 4174 | 4185 | ** is requested, [sqlite3_snprintf()] is used internally to perform the |
| 4175 | 4186 | ** conversion automatically. ^(The following table details the conversions |
| | @@ -4196,16 +4207,10 @@ |
| 4196 | 4207 | ** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL |
| 4197 | 4208 | ** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed |
| 4198 | 4209 | ** </table> |
| 4199 | 4210 | ** </blockquote>)^ |
| 4200 | 4211 | ** |
| 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 | 4212 | ** Note that when type conversions occur, pointers returned by prior |
| 4208 | 4213 | ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or |
| 4209 | 4214 | ** sqlite3_column_text16() may be invalidated. |
| 4210 | 4215 | ** Type conversions and pointer invalidations might occur |
| 4211 | 4216 | ** in the following cases: |
| | @@ -4226,11 +4231,11 @@ |
| 4226 | 4231 | ** not invalidate a prior pointer, though of course the content of the buffer |
| 4227 | 4232 | ** that the prior pointer references will have been modified. Other kinds |
| 4228 | 4233 | ** of conversion are done in place when it is possible, but sometimes they |
| 4229 | 4234 | ** are not possible and in those cases prior pointers are invalidated. |
| 4230 | 4235 | ** |
| 4231 | | -** The safest and easiest to remember policy is to invoke these routines |
| 4236 | +** The safest policy is to invoke these routines |
| 4232 | 4237 | ** in one of the following ways: |
| 4233 | 4238 | ** |
| 4234 | 4239 | ** <ul> |
| 4235 | 4240 | ** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li> |
| 4236 | 4241 | ** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li> |
| | @@ -4246,11 +4251,11 @@ |
| 4246 | 4251 | ** with calls to sqlite3_column_bytes(). |
| 4247 | 4252 | ** |
| 4248 | 4253 | ** ^The pointers returned are valid until a type conversion occurs as |
| 4249 | 4254 | ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or |
| 4250 | 4255 | ** [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 |
| 4252 | 4257 | ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into |
| 4253 | 4258 | ** [sqlite3_free()]. |
| 4254 | 4259 | ** |
| 4255 | 4260 | ** ^(If a memory allocation error occurs during the evaluation of any |
| 4256 | 4261 | ** of these routines, a default value is returned. The default value |
| | @@ -4496,16 +4501,16 @@ |
| 4496 | 4501 | SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int), |
| 4497 | 4502 | void*,sqlite3_int64); |
| 4498 | 4503 | #endif |
| 4499 | 4504 | |
| 4500 | 4505 | /* |
| 4501 | | -** CAPI3REF: Obtaining SQL Function Parameter Values |
| 4506 | +** CAPI3REF: Obtaining SQL Values |
| 4502 | 4507 | ** METHOD: sqlite3_value |
| 4503 | 4508 | ** |
| 4504 | 4509 | ** The C-language implementation of SQL functions and aggregates uses |
| 4505 | 4510 | ** this set of interface routines to access the parameter values on |
| 4506 | | -** the function or aggregate. |
| 4511 | +** the function or aggregate. |
| 4507 | 4512 | ** |
| 4508 | 4513 | ** The xFunc (for scalar functions) or xStep (for aggregates) parameters |
| 4509 | 4514 | ** to [sqlite3_create_function()] and [sqlite3_create_function16()] |
| 4510 | 4515 | ** define callbacks that implement the SQL functions and aggregates. |
| 4511 | 4516 | ** The 3rd parameter to these callbacks is an array of pointers to |
| | @@ -4554,10 +4559,27 @@ |
| 4554 | 4559 | SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value*); |
| 4555 | 4560 | SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value*); |
| 4556 | 4561 | SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value*); |
| 4557 | 4562 | SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*); |
| 4558 | 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 | + |
| 4559 | 4581 | /* |
| 4560 | 4582 | ** CAPI3REF: Obtain Aggregate Function Context |
| 4561 | 4583 | ** METHOD: sqlite3_context |
| 4562 | 4584 | ** |
| 4563 | 4585 | ** Implementations of aggregate SQL functions use this |
| | @@ -4801,11 +4823,11 @@ |
| 4801 | 4823 | ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT |
| 4802 | 4824 | ** then SQLite makes a copy of the result into space obtained from |
| 4803 | 4825 | ** from [sqlite3_malloc()] before it returns. |
| 4804 | 4826 | ** |
| 4805 | 4827 | ** ^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 |
| 4807 | 4829 | ** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The |
| 4808 | 4830 | ** sqlite3_result_value() interface makes a copy of the [sqlite3_value] |
| 4809 | 4831 | ** so that the [sqlite3_value] specified in the parameter may change or |
| 4810 | 4832 | ** be deallocated after sqlite3_result_value() returns without harm. |
| 4811 | 4833 | ** ^A [protected sqlite3_value] object may always be used where an |
| | @@ -6077,11 +6099,11 @@ |
| 6077 | 6099 | ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle |
| 6078 | 6100 | ** always returns zero. |
| 6079 | 6101 | ** |
| 6080 | 6102 | ** ^This function sets the database handle error code and message. |
| 6081 | 6103 | */ |
| 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); |
| 6083 | 6105 | |
| 6084 | 6106 | /* |
| 6085 | 6107 | ** CAPI3REF: Close A BLOB Handle |
| 6086 | 6108 | ** DESTRUCTOR: sqlite3_blob |
| 6087 | 6109 | ** |
| | @@ -7887,11 +7909,11 @@ |
| 7887 | 7909 | ** as if the loop did not exist - it returns non-zero and leave the variable |
| 7888 | 7910 | ** that pOut points to unchanged. |
| 7889 | 7911 | ** |
| 7890 | 7912 | ** See also: [sqlite3_stmt_scanstatus_reset()] |
| 7891 | 7913 | */ |
| 7892 | | -SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_stmt_scanstatus( |
| 7914 | +SQLITE_API int SQLITE_STDCALL sqlite3_stmt_scanstatus( |
| 7893 | 7915 | sqlite3_stmt *pStmt, /* Prepared statement for which info desired */ |
| 7894 | 7916 | int idx, /* Index of loop to report on */ |
| 7895 | 7917 | int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */ |
| 7896 | 7918 | void *pOut /* Result written here */ |
| 7897 | 7919 | ); |
| | @@ -7903,11 +7925,11 @@ |
| 7903 | 7925 | ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters. |
| 7904 | 7926 | ** |
| 7905 | 7927 | ** This API is only available if the library is built with pre-processor |
| 7906 | 7928 | ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. |
| 7907 | 7929 | */ |
| 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*); |
| 7909 | 7931 | |
| 7910 | 7932 | |
| 7911 | 7933 | /* |
| 7912 | 7934 | ** Undo the hack that converts floating point types to integer for |
| 7913 | 7935 | ** builds on processors without floating point support. |
| | @@ -8018,10 +8040,12 @@ |
| 8018 | 8040 | sqlite3_int64 iRowid; /* Rowid for current entry */ |
| 8019 | 8041 | sqlite3_rtree_dbl rParentScore; /* Score of parent node */ |
| 8020 | 8042 | int eParentWithin; /* Visibility of parent node */ |
| 8021 | 8043 | int eWithin; /* OUT: Visiblity */ |
| 8022 | 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 */ |
| 8023 | 8047 | }; |
| 8024 | 8048 | |
| 8025 | 8049 | /* |
| 8026 | 8050 | ** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin. |
| 8027 | 8051 | */ |
| | @@ -11167,10 +11191,11 @@ |
| 11167 | 11191 | #define SQLITE_EnableTrigger 0x00800000 /* True to enable triggers */ |
| 11168 | 11192 | #define SQLITE_DeferFKs 0x01000000 /* Defer all FK constraints */ |
| 11169 | 11193 | #define SQLITE_QueryOnly 0x02000000 /* Disable database changes */ |
| 11170 | 11194 | #define SQLITE_VdbeEQP 0x04000000 /* Debug EXPLAIN QUERY PLAN */ |
| 11171 | 11195 | #define SQLITE_Vacuum 0x08000000 /* Currently in a VACUUM */ |
| 11196 | +#define SQLITE_CellSizeCk 0x10000000 /* Check btree cell sizes on load */ |
| 11172 | 11197 | |
| 11173 | 11198 | |
| 11174 | 11199 | /* |
| 11175 | 11200 | ** Bits of the sqlite3.dbOptFlags field that are used by the |
| 11176 | 11201 | ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to |
| | @@ -11548,12 +11573,13 @@ |
| 11548 | 11573 | #define TF_Readonly 0x01 /* Read-only system table */ |
| 11549 | 11574 | #define TF_Ephemeral 0x02 /* An ephemeral table */ |
| 11550 | 11575 | #define TF_HasPrimaryKey 0x04 /* Table has a primary key */ |
| 11551 | 11576 | #define TF_Autoincrement 0x08 /* Integer primary key is autoincrement */ |
| 11552 | 11577 | #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 */ |
| 11555 | 11581 | |
| 11556 | 11582 | |
| 11557 | 11583 | /* |
| 11558 | 11584 | ** Test to see whether or not a table is a virtual table. This is |
| 11559 | 11585 | ** done as a macro so that it will be optimized out when virtual |
| | @@ -11567,10 +11593,11 @@ |
| 11567 | 11593 | # define IsHiddenColumn(X) 0 |
| 11568 | 11594 | #endif |
| 11569 | 11595 | |
| 11570 | 11596 | /* Does the table have a rowid */ |
| 11571 | 11597 | #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0) |
| 11598 | +#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0) |
| 11572 | 11599 | |
| 11573 | 11600 | /* |
| 11574 | 11601 | ** Each foreign key constraint is an instance of the following structure. |
| 11575 | 11602 | ** |
| 11576 | 11603 | ** A foreign key is associated with two tables. The "from" table is |
| | @@ -11725,10 +11752,18 @@ |
| 11725 | 11752 | ** must be unique and what to do if they are not. When Index.onError=OE_None, |
| 11726 | 11753 | ** it means this is not a unique index. Otherwise it is a unique index |
| 11727 | 11754 | ** and the value of Index.onError indicate the which conflict resolution |
| 11728 | 11755 | ** algorithm to employ whenever an attempt is made to insert a non-unique |
| 11729 | 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. |
| 11730 | 11765 | */ |
| 11731 | 11766 | struct Index { |
| 11732 | 11767 | char *zName; /* Name of this index */ |
| 11733 | 11768 | i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */ |
| 11734 | 11769 | LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */ |
| | @@ -12299,23 +12334,24 @@ |
| 12299 | 12334 | /* |
| 12300 | 12335 | ** Allowed values for Select.selFlags. The "SF" prefix stands for |
| 12301 | 12336 | ** "Select Flag". |
| 12302 | 12337 | */ |
| 12303 | 12338 | #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() */ |
| 12315 | 12350 | #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() */ |
| 12317 | 12353 | |
| 12318 | 12354 | |
| 12319 | 12355 | /* |
| 12320 | 12356 | ** The results of a SELECT can be distributed in several ways, as defined |
| 12321 | 12357 | ** by one of the following macros. The "SRT" prefix means "SELECT Result |
| | @@ -12553,11 +12589,10 @@ |
| 12553 | 12589 | |
| 12554 | 12590 | /* Information used while coding trigger programs. */ |
| 12555 | 12591 | Parse *pToplevel; /* Parse structure for main program (or NULL) */ |
| 12556 | 12592 | Table *pTriggerTab; /* Table triggers are being coded for */ |
| 12557 | 12593 | int addrCrTab; /* Address of OP_CreateTable opcode on CREATE TABLE */ |
| 12558 | | - int addrSkipPK; /* Address of instruction to skip PRIMARY KEY index */ |
| 12559 | 12594 | u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */ |
| 12560 | 12595 | u32 oldmask; /* Mask of old.* columns referenced */ |
| 12561 | 12596 | u32 newmask; /* Mask of new.* columns referenced */ |
| 12562 | 12597 | u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */ |
| 12563 | 12598 | u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */ |
| | @@ -13065,11 +13100,10 @@ |
| 13065 | 13100 | #define SQLITE_PRINTF_SQLFUNC 0x02 |
| 13066 | 13101 | SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list); |
| 13067 | 13102 | SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...); |
| 13068 | 13103 | SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...); |
| 13069 | 13104 | SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list); |
| 13070 | | -SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...); |
| 13071 | 13105 | #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 13072 | 13106 | SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...); |
| 13073 | 13107 | #endif |
| 13074 | 13108 | #if defined(SQLITE_TEST) |
| 13075 | 13109 | SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*); |
| | @@ -13084,11 +13118,11 @@ |
| 13084 | 13118 | SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*); |
| 13085 | 13119 | SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8); |
| 13086 | 13120 | #endif |
| 13087 | 13121 | |
| 13088 | 13122 | |
| 13089 | | -SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...); |
| 13123 | +SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*); |
| 13090 | 13124 | SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...); |
| 13091 | 13125 | SQLITE_PRIVATE int sqlite3Dequote(char*); |
| 13092 | 13126 | SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int); |
| 13093 | 13127 | SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **); |
| 13094 | 13128 | SQLITE_PRIVATE void sqlite3FinishCoding(Parse*); |
| | @@ -13780,10 +13814,14 @@ |
| 13780 | 13814 | */ |
| 13781 | 13815 | #if SQLITE_MAX_WORKER_THREADS>0 |
| 13782 | 13816 | SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*); |
| 13783 | 13817 | SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**); |
| 13784 | 13818 | #endif |
| 13819 | + |
| 13820 | +#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST) |
| 13821 | +SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*); |
| 13822 | +#endif |
| 13785 | 13823 | |
| 13786 | 13824 | #endif /* _SQLITEINT_H_ */ |
| 13787 | 13825 | |
| 13788 | 13826 | /************** End of sqliteInt.h *******************************************/ |
| 13789 | 13827 | /************** Begin file global.c ******************************************/ |
| | @@ -14685,10 +14723,16 @@ |
| 14685 | 14723 | Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */ |
| 14686 | 14724 | void *pFiller; /* So that sizeof(Mem) is a multiple of 8 */ |
| 14687 | 14725 | #endif |
| 14688 | 14726 | }; |
| 14689 | 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 | + |
| 14690 | 14734 | /* One or more of the following flags are set to indicate the validOK |
| 14691 | 14735 | ** representations of the value stored in the Mem struct. |
| 14692 | 14736 | ** |
| 14693 | 14737 | ** If the MEM_Null flag is set, then the value is an SQL NULL value. |
| 14694 | 14738 | ** No other flags may be set in this case. |
| | @@ -14890,10 +14934,11 @@ |
| 14890 | 14934 | #define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */ |
| 14891 | 14935 | |
| 14892 | 14936 | /* |
| 14893 | 14937 | ** Function prototypes |
| 14894 | 14938 | */ |
| 14939 | +SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...); |
| 14895 | 14940 | SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*); |
| 14896 | 14941 | void sqliteVdbePopStack(Vdbe*,int); |
| 14897 | 14942 | SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*); |
| 14898 | 14943 | SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*); |
| 14899 | 14944 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| | @@ -20738,28 +20783,24 @@ |
| 20738 | 20783 | |
| 20739 | 20784 | /* |
| 20740 | 20785 | ** Return the amount of memory currently checked out. |
| 20741 | 20786 | */ |
| 20742 | 20787 | 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); |
| 20747 | 20790 | return res; |
| 20748 | 20791 | } |
| 20749 | 20792 | |
| 20750 | 20793 | /* |
| 20751 | 20794 | ** Return the maximum amount of memory that has ever been |
| 20752 | 20795 | ** checked out since either the beginning of this process |
| 20753 | 20796 | ** or since the most recent reset. |
| 20754 | 20797 | */ |
| 20755 | 20798 | 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; |
| 20761 | 20802 | } |
| 20762 | 20803 | |
| 20763 | 20804 | /* |
| 20764 | 20805 | ** Trigger the alarm |
| 20765 | 20806 | */ |
| | @@ -21287,23 +21328,15 @@ |
| 21287 | 21328 | } |
| 21288 | 21329 | return zNew; |
| 21289 | 21330 | } |
| 21290 | 21331 | |
| 21291 | 21332 | /* |
| 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. |
| 21295 | 21334 | */ |
| 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){ |
| 21303 | 21336 | sqlite3DbFree(db, *pz); |
| 21304 | | - *pz = z; |
| 21337 | + *pz = sqlite3DbStrDup(db, zNew); |
| 21305 | 21338 | } |
| 21306 | 21339 | |
| 21307 | 21340 | /* |
| 21308 | 21341 | ** Take actions at the end of an API call to indicate an OOM error |
| 21309 | 21342 | */ |
| | @@ -22271,28 +22304,10 @@ |
| 22271 | 22304 | z = sqlite3VMPrintf(db, zFormat, ap); |
| 22272 | 22305 | va_end(ap); |
| 22273 | 22306 | return z; |
| 22274 | 22307 | } |
| 22275 | 22308 | |
| 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 | 22309 | /* |
| 22295 | 22310 | ** Print into memory obtained from sqlite3_malloc(). Omit the internal |
| 22296 | 22311 | ** %-conversion extensions. |
| 22297 | 22312 | */ |
| 22298 | 22313 | SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char *zFormat, va_list ap){ |
| | @@ -36220,10 +36235,16 @@ |
| 36220 | 36235 | */ |
| 36221 | 36236 | if( pFile->locktype>=locktype ){ |
| 36222 | 36237 | OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 36223 | 36238 | return SQLITE_OK; |
| 36224 | 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 | + } |
| 36225 | 36246 | |
| 36226 | 36247 | /* Make sure the locking sequence is correct |
| 36227 | 36248 | */ |
| 36228 | 36249 | assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
| 36229 | 36250 | assert( locktype!=PENDING_LOCK ); |
| | @@ -38617,18 +38638,18 @@ |
| 38617 | 38638 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID |
| 38618 | 38639 | if( sizeof(UUID)<=nBuf-n ){ |
| 38619 | 38640 | UUID id; |
| 38620 | 38641 | memset(&id, 0, sizeof(UUID)); |
| 38621 | 38642 | osUuidCreate(&id); |
| 38622 | | - memcpy(zBuf, &id, sizeof(UUID)); |
| 38643 | + memcpy(&zBuf[n], &id, sizeof(UUID)); |
| 38623 | 38644 | n += sizeof(UUID); |
| 38624 | 38645 | } |
| 38625 | 38646 | if( sizeof(UUID)<=nBuf-n ){ |
| 38626 | 38647 | UUID id; |
| 38627 | 38648 | memset(&id, 0, sizeof(UUID)); |
| 38628 | 38649 | osUuidCreateSequential(&id); |
| 38629 | | - memcpy(zBuf, &id, sizeof(UUID)); |
| 38650 | + memcpy(&zBuf[n], &id, sizeof(UUID)); |
| 38630 | 38651 | n += sizeof(UUID); |
| 38631 | 38652 | } |
| 38632 | 38653 | #endif |
| 38633 | 38654 | #endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */ |
| 38634 | 38655 | return n; |
| | @@ -44777,15 +44798,14 @@ |
| 44777 | 44798 | */ |
| 44778 | 44799 | assert( pPager->eState==PAGER_OPEN ); |
| 44779 | 44800 | assert( pPager->eLock>=SHARED_LOCK ); |
| 44780 | 44801 | nPage = sqlite3WalDbsize(pPager->pWal); |
| 44781 | 44802 | |
| 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. |
| 44787 | 44807 | */ |
| 44788 | 44808 | if( nPage==0 ){ |
| 44789 | 44809 | i64 n = 0; /* Size of db file in bytes */ |
| 44790 | 44810 | assert( isOpen(pPager->fd) || pPager->tempFile ); |
| 44791 | 44811 | if( isOpen(pPager->fd) ){ |
| | @@ -54245,30 +54265,22 @@ |
| 54245 | 54265 | u8 *pAddr; /* The i-th cell pointer */ |
| 54246 | 54266 | pAddr = &data[cellOffset + i*2]; |
| 54247 | 54267 | pc = get2byte(pAddr); |
| 54248 | 54268 | testcase( pc==iCellFirst ); |
| 54249 | 54269 | testcase( pc==iCellLast ); |
| 54250 | | -#if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
| 54251 | 54270 | /* These conditions have already been verified in btreeInitPage() |
| 54252 | | - ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined |
| 54271 | + ** if PRAGMA cell_size_check=ON. |
| 54253 | 54272 | */ |
| 54254 | 54273 | if( pc<iCellFirst || pc>iCellLast ){ |
| 54255 | 54274 | return SQLITE_CORRUPT_BKPT; |
| 54256 | 54275 | } |
| 54257 | | -#endif |
| 54258 | 54276 | assert( pc>=iCellFirst && pc<=iCellLast ); |
| 54259 | 54277 | size = cellSizePtr(pPage, &src[pc]); |
| 54260 | 54278 | cbrk -= size; |
| 54261 | | -#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
| 54262 | | - if( cbrk<iCellFirst ){ |
| 54263 | | - return SQLITE_CORRUPT_BKPT; |
| 54264 | | - } |
| 54265 | | -#else |
| 54266 | 54279 | if( cbrk<iCellFirst || pc+size>usableSize ){ |
| 54267 | 54280 | return SQLITE_CORRUPT_BKPT; |
| 54268 | 54281 | } |
| 54269 | | -#endif |
| 54270 | 54282 | assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); |
| 54271 | 54283 | testcase( cbrk+size==usableSize ); |
| 54272 | 54284 | testcase( pc+size==usableSize ); |
| 54273 | 54285 | put2byte(pAddr, cbrk); |
| 54274 | 54286 | if( temp==0 ){ |
| | @@ -54340,11 +54352,11 @@ |
| 54340 | 54352 | } |
| 54341 | 54353 | /* Remove the slot from the free-list. Update the number of |
| 54342 | 54354 | ** fragmented bytes within the page. */ |
| 54343 | 54355 | memcpy(&aData[iAddr], &aData[pc], 2); |
| 54344 | 54356 | aData[hdr+7] += (u8)x; |
| 54345 | | - }else if( size+pc > usableSize ){ |
| 54357 | + }else if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){ |
| 54346 | 54358 | *pRc = SQLITE_CORRUPT_BKPT; |
| 54347 | 54359 | return 0; |
| 54348 | 54360 | }else{ |
| 54349 | 54361 | /* The slot remains on the free-list. Reduce its size to account |
| 54350 | 54362 | ** for the portion used by the new allocation. */ |
| | @@ -54392,11 +54404,15 @@ |
| 54392 | 54404 | ** and the reserved space is zero (the usual value for reserved space) |
| 54393 | 54405 | ** then the cell content offset of an empty page wants to be 65536. |
| 54394 | 54406 | ** However, that integer is too large to be stored in a 2-byte unsigned |
| 54395 | 54407 | ** integer, so a value of 0 is used in its place. */ |
| 54396 | 54408 | 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 | + } |
| 54398 | 54414 | |
| 54399 | 54415 | /* If there is enough space between gap and top for one more cell pointer |
| 54400 | 54416 | ** array entry offset, and if the freelist is not empty, then search the |
| 54401 | 54417 | ** freelist looking for a free slot big enough to satisfy the request. |
| 54402 | 54418 | */ |
| | @@ -54465,11 +54481,11 @@ |
| 54465 | 54481 | u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */ |
| 54466 | 54482 | unsigned char *data = pPage->aData; /* Page content */ |
| 54467 | 54483 | |
| 54468 | 54484 | assert( pPage->pBt!=0 ); |
| 54469 | 54485 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 54470 | | - assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); |
| 54486 | + assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); |
| 54471 | 54487 | assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize ); |
| 54472 | 54488 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 54473 | 54489 | assert( iSize>=4 ); /* Minimum cell size is 4 */ |
| 54474 | 54490 | assert( iStart<=iLast ); |
| 54475 | 54491 | |
| | @@ -54605,10 +54621,11 @@ |
| 54605 | 54621 | ** we failed to detect any corruption. |
| 54606 | 54622 | */ |
| 54607 | 54623 | static int btreeInitPage(MemPage *pPage){ |
| 54608 | 54624 | |
| 54609 | 54625 | assert( pPage->pBt!=0 ); |
| 54626 | + assert( pPage->pBt->db!=0 ); |
| 54610 | 54627 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 54611 | 54628 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
| 54612 | 54629 | assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); |
| 54613 | 54630 | assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); |
| 54614 | 54631 | |
| | @@ -54663,12 +54680,11 @@ |
| 54663 | 54680 | ** past the end of a page boundary and causes SQLITE_CORRUPT to be |
| 54664 | 54681 | ** returned if it does. |
| 54665 | 54682 | */ |
| 54666 | 54683 | iCellFirst = cellOffset + 2*pPage->nCell; |
| 54667 | 54684 | iCellLast = usableSize - 4; |
| 54668 | | -#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
| 54669 | | - { |
| 54685 | + if( pBt->db->flags & SQLITE_CellSizeCk ){ |
| 54670 | 54686 | int i; /* Index into the cell pointer array */ |
| 54671 | 54687 | int sz; /* Size of a cell */ |
| 54672 | 54688 | |
| 54673 | 54689 | if( !pPage->leaf ) iCellLast--; |
| 54674 | 54690 | for(i=0; i<pPage->nCell; i++){ |
| | @@ -54684,11 +54700,10 @@ |
| 54684 | 54700 | return SQLITE_CORRUPT_BKPT; |
| 54685 | 54701 | } |
| 54686 | 54702 | } |
| 54687 | 54703 | if( !pPage->leaf ) iCellLast++; |
| 54688 | 54704 | } |
| 54689 | | -#endif |
| 54690 | 54705 | |
| 54691 | 54706 | /* Compute the total free space on the page |
| 54692 | 54707 | ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the |
| 54693 | 54708 | ** start of the first freeblock on the page, or is zero if there are no |
| 54694 | 54709 | ** freeblocks. */ |
| | @@ -54781,14 +54796,14 @@ |
| 54781 | 54796 | return pPage; |
| 54782 | 54797 | } |
| 54783 | 54798 | |
| 54784 | 54799 | /* |
| 54785 | 54800 | ** 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(). |
| 54787 | 54802 | ** |
| 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 |
| 54790 | 54805 | ** to fetch the content. Just fill in the content with zeros for now. |
| 54791 | 54806 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 54792 | 54807 | ** means we have started to be concerned about content and the disk |
| 54793 | 54808 | ** read should occur at that point. |
| 54794 | 54809 | */ |
| | @@ -54885,10 +54900,40 @@ |
| 54885 | 54900 | assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
| 54886 | 54901 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 54887 | 54902 | sqlite3PagerUnrefNotNull(pPage->pDbPage); |
| 54888 | 54903 | } |
| 54889 | 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 | + |
| 54890 | 54935 | |
| 54891 | 54936 | /* |
| 54892 | 54937 | ** During a rollback, when the pager reloads information into the cache |
| 54893 | 54938 | ** so that the cache is restored to its original state at the start of |
| 54894 | 54939 | ** the transaction, for each page restored this routine is called. |
| | @@ -56133,12 +56178,14 @@ |
| 56133 | 56178 | put4byte(pPage->aData, iTo); |
| 56134 | 56179 | }else{ |
| 56135 | 56180 | u8 isInitOrig = pPage->isInit; |
| 56136 | 56181 | int i; |
| 56137 | 56182 | int nCell; |
| 56183 | + int rc; |
| 56138 | 56184 | |
| 56139 | | - btreeInitPage(pPage); |
| 56185 | + rc = btreeInitPage(pPage); |
| 56186 | + if( rc ) return rc; |
| 56140 | 56187 | nCell = pPage->nCell; |
| 56141 | 56188 | |
| 56142 | 56189 | for(i=0; i<nCell; i++){ |
| 56143 | 56190 | u8 *pCell = findCell(pPage, i); |
| 56144 | 56191 | if( eType==PTRMAP_OVERFLOW1 ){ |
| | @@ -56935,13 +56982,17 @@ |
| 56935 | 56982 | int wrFlag, /* 1 to write. 0 read-only */ |
| 56936 | 56983 | struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ |
| 56937 | 56984 | BtCursor *pCur /* Write new cursor here */ |
| 56938 | 56985 | ){ |
| 56939 | 56986 | 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 | + } |
| 56943 | 56994 | return rc; |
| 56944 | 56995 | } |
| 56945 | 56996 | |
| 56946 | 56997 | /* |
| 56947 | 56998 | ** Return the size of a BtCursor object in bytes. |
| | @@ -57965,11 +58016,11 @@ |
| 57965 | 58016 | assert( lwr+upr>=0 ); |
| 57966 | 58017 | idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */ |
| 57967 | 58018 | } |
| 57968 | 58019 | }else{ |
| 57969 | 58020 | for(;;){ |
| 57970 | | - int nCell; |
| 58021 | + int nCell; /* Size of the pCell cell in bytes */ |
| 57971 | 58022 | pCell = findCell(pPage, idx) + pPage->childPtrSize; |
| 57972 | 58023 | |
| 57973 | 58024 | /* The maximum supported page-size is 65536 bytes. This means that |
| 57974 | 58025 | ** the maximum number of record bytes stored on an index B-Tree |
| 57975 | 58026 | ** page is less than 16384 bytes and may be stored as a 2-byte |
| | @@ -57994,16 +58045,29 @@ |
| 57994 | 58045 | c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey); |
| 57995 | 58046 | }else{ |
| 57996 | 58047 | /* The record flows over onto one or more overflow pages. In |
| 57997 | 58048 | ** this case the whole cell needs to be parsed, a buffer allocated |
| 57998 | 58049 | ** 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. */ |
| 58000 | 58056 | void *pCellKey; |
| 58001 | 58057 | u8 * const pCellBody = pCell - pPage->childPtrSize; |
| 58002 | 58058 | btreeParseCellPtr(pPage, pCellBody, &pCur->info); |
| 58003 | 58059 | 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 ); |
| 58005 | 58069 | if( pCellKey==0 ){ |
| 58006 | 58070 | rc = SQLITE_NOMEM; |
| 58007 | 58071 | goto moveto_finish; |
| 58008 | 58072 | } |
| 58009 | 58073 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
| | @@ -58387,11 +58451,11 @@ |
| 58387 | 58451 | } |
| 58388 | 58452 | testcase( iTrunk==mxPage ); |
| 58389 | 58453 | if( iTrunk>mxPage ){ |
| 58390 | 58454 | rc = SQLITE_CORRUPT_BKPT; |
| 58391 | 58455 | }else{ |
| 58392 | | - rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); |
| 58456 | + rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0); |
| 58393 | 58457 | } |
| 58394 | 58458 | if( rc ){ |
| 58395 | 58459 | pTrunk = 0; |
| 58396 | 58460 | goto end_allocate_page; |
| 58397 | 58461 | } |
| | @@ -58452,11 +58516,11 @@ |
| 58452 | 58516 | if( iNewTrunk>mxPage ){ |
| 58453 | 58517 | rc = SQLITE_CORRUPT_BKPT; |
| 58454 | 58518 | goto end_allocate_page; |
| 58455 | 58519 | } |
| 58456 | 58520 | testcase( iNewTrunk==mxPage ); |
| 58457 | | - rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
| 58521 | + rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0); |
| 58458 | 58522 | if( rc!=SQLITE_OK ){ |
| 58459 | 58523 | goto end_allocate_page; |
| 58460 | 58524 | } |
| 58461 | 58525 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
| 58462 | 58526 | if( rc!=SQLITE_OK ){ |
| | @@ -58532,11 +58596,11 @@ |
| 58532 | 58596 | if( closest<k-1 ){ |
| 58533 | 58597 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 58534 | 58598 | } |
| 58535 | 58599 | put4byte(&aData[4], k-1); |
| 58536 | 58600 | noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0; |
| 58537 | | - rc = btreeGetPage(pBt, *pPgno, ppPage, noContent); |
| 58601 | + rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent); |
| 58538 | 58602 | if( rc==SQLITE_OK ){ |
| 58539 | 58603 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
| 58540 | 58604 | if( rc!=SQLITE_OK ){ |
| 58541 | 58605 | releasePage(*ppPage); |
| 58542 | 58606 | } |
| | @@ -58580,11 +58644,11 @@ |
| 58580 | 58644 | ** becomes a new pointer-map page, the second is used by the caller. |
| 58581 | 58645 | */ |
| 58582 | 58646 | MemPage *pPg = 0; |
| 58583 | 58647 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); |
| 58584 | 58648 | assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); |
| 58585 | | - rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent); |
| 58649 | + rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent); |
| 58586 | 58650 | if( rc==SQLITE_OK ){ |
| 58587 | 58651 | rc = sqlite3PagerWrite(pPg->pDbPage); |
| 58588 | 58652 | releasePage(pPg); |
| 58589 | 58653 | } |
| 58590 | 58654 | if( rc ) return rc; |
| | @@ -58594,35 +58658,27 @@ |
| 58594 | 58658 | #endif |
| 58595 | 58659 | put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage); |
| 58596 | 58660 | *pPgno = pBt->nPage; |
| 58597 | 58661 | |
| 58598 | 58662 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 58599 | | - rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent); |
| 58663 | + rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent); |
| 58600 | 58664 | if( rc ) return rc; |
| 58601 | 58665 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
| 58602 | 58666 | if( rc!=SQLITE_OK ){ |
| 58603 | 58667 | releasePage(*ppPage); |
| 58668 | + *ppPage = 0; |
| 58604 | 58669 | } |
| 58605 | 58670 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
| 58606 | 58671 | } |
| 58607 | 58672 | |
| 58608 | 58673 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 58609 | 58674 | |
| 58610 | 58675 | end_allocate_page: |
| 58611 | 58676 | releasePage(pTrunk); |
| 58612 | 58677 | 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 ); |
| 58624 | 58680 | return rc; |
| 58625 | 58681 | } |
| 58626 | 58682 | |
| 58627 | 58683 | /* |
| 58628 | 58684 | ** This function is used to add page iPage to the database file free-list. |
| | @@ -58643,13 +58699,14 @@ |
| 58643 | 58699 | MemPage *pPage; /* Page being freed. May be NULL. */ |
| 58644 | 58700 | int rc; /* Return Code */ |
| 58645 | 58701 | int nFree; /* Initial number of pages on free-list */ |
| 58646 | 58702 | |
| 58647 | 58703 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 58648 | | - assert( iPage>1 ); |
| 58704 | + assert( CORRUPT_DB || iPage>1 ); |
| 58649 | 58705 | assert( !pMemPage || pMemPage->pgno==iPage ); |
| 58650 | 58706 | |
| 58707 | + if( iPage<2 ) return SQLITE_CORRUPT_BKPT; |
| 58651 | 58708 | if( pMemPage ){ |
| 58652 | 58709 | pPage = pMemPage; |
| 58653 | 58710 | sqlite3PagerRef(pPage->pDbPage); |
| 58654 | 58711 | }else{ |
| 58655 | 58712 | pPage = btreePageLookup(pBt, iPage); |
| | @@ -58797,11 +58854,13 @@ |
| 58797 | 58854 | } |
| 58798 | 58855 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
| 58799 | 58856 | assert( pBt->usableSize > 4 ); |
| 58800 | 58857 | ovflPageSize = pBt->usableSize - 4; |
| 58801 | 58858 | 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 | + ); |
| 58803 | 58862 | while( nOvfl-- ){ |
| 58804 | 58863 | Pgno iNext = 0; |
| 58805 | 58864 | MemPage *pOvfl = 0; |
| 58806 | 58865 | if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){ |
| 58807 | 58866 | /* 0 is not a legal page number and page 1 cannot be an |
| | @@ -59052,11 +59111,11 @@ |
| 59052 | 59111 | int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */ |
| 59053 | 59112 | |
| 59054 | 59113 | if( *pRC ) return; |
| 59055 | 59114 | |
| 59056 | 59115 | assert( idx>=0 && idx<pPage->nCell ); |
| 59057 | | - assert( sz==cellSize(pPage, idx) ); |
| 59116 | + assert( CORRUPT_DB || sz==cellSize(pPage, idx) ); |
| 59058 | 59117 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 59059 | 59118 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 59060 | 59119 | data = pPage->aData; |
| 59061 | 59120 | ptr = &pPage->aCellIdx[2*idx]; |
| 59062 | 59121 | pc = get2byte(ptr); |
| | @@ -59216,11 +59275,12 @@ |
| 59216 | 59275 | } |
| 59217 | 59276 | pData -= szCell[i]; |
| 59218 | 59277 | memcpy(pData, pCell, szCell[i]); |
| 59219 | 59278 | put2byte(pCellptr, (pData - aData)); |
| 59220 | 59279 | 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) ); |
| 59222 | 59282 | } |
| 59223 | 59283 | |
| 59224 | 59284 | /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ |
| 59225 | 59285 | pPg->nCell = nCell; |
| 59226 | 59286 | pPg->nOverflow = 0; |
| | @@ -59893,10 +59953,18 @@ |
| 59893 | 59953 | leafCorrection = apOld[0]->leaf*4; |
| 59894 | 59954 | leafData = apOld[0]->intKeyLeaf; |
| 59895 | 59955 | for(i=0; i<nOld; i++){ |
| 59896 | 59956 | int limit; |
| 59897 | 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 | + } |
| 59898 | 59966 | |
| 59899 | 59967 | limit = pOld->nCell+pOld->nOverflow; |
| 59900 | 59968 | if( pOld->nOverflow>0 ){ |
| 59901 | 59969 | for(j=0; j<limit; j++){ |
| 59902 | 59970 | assert( nCell<nMaxCells ); |
| | @@ -59935,17 +60003,17 @@ |
| 59935 | 60003 | /* The right pointer of the child page pOld becomes the left |
| 59936 | 60004 | ** pointer of the divider cell */ |
| 59937 | 60005 | memcpy(apCell[nCell], &pOld->aData[8], 4); |
| 59938 | 60006 | }else{ |
| 59939 | 60007 | assert( leafCorrection==4 ); |
| 59940 | | - if( szCell[nCell]<4 ){ |
| 60008 | + while( szCell[nCell]<4 ){ |
| 59941 | 60009 | /* Do not allow any cells smaller than 4 bytes. If a smaller cell |
| 59942 | 60010 | ** 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 ); |
| 59945 | 60013 | aSpace1[iSpace1++] = 0x00; |
| 59946 | | - szCell[nCell] = 4; |
| 60014 | + szCell[nCell]++; |
| 59947 | 60015 | } |
| 59948 | 60016 | } |
| 59949 | 60017 | nCell++; |
| 59950 | 60018 | } |
| 59951 | 60019 | } |
| | @@ -60032,14 +60100,10 @@ |
| 60032 | 60100 | )); |
| 60033 | 60101 | |
| 60034 | 60102 | /* |
| 60035 | 60103 | ** Allocate k new pages. Reuse old pages where possible. |
| 60036 | 60104 | */ |
| 60037 | | - if( apOld[0]->pgno<=1 ){ |
| 60038 | | - rc = SQLITE_CORRUPT_BKPT; |
| 60039 | | - goto balance_cleanup; |
| 60040 | | - } |
| 60041 | 60105 | pageFlags = apOld[0]->aData[0]; |
| 60042 | 60106 | for(i=0; i<k; i++){ |
| 60043 | 60107 | MemPage *pNew; |
| 60044 | 60108 | if( i<nOld ){ |
| 60045 | 60109 | pNew = apNew[i] = apOld[i]; |
| | @@ -60817,10 +60881,11 @@ |
| 60817 | 60881 | int nCell; |
| 60818 | 60882 | Pgno n = pCur->apPage[iCellDepth+1]->pgno; |
| 60819 | 60883 | unsigned char *pTmp; |
| 60820 | 60884 | |
| 60821 | 60885 | pCell = findCell(pLeaf, pLeaf->nCell-1); |
| 60886 | + if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT; |
| 60822 | 60887 | nCell = cellSizePtr(pLeaf, pCell); |
| 60823 | 60888 | assert( MX_CELL_SIZE(pBt) >= nCell ); |
| 60824 | 60889 | pTmp = pBt->pTmpSpace; |
| 60825 | 60890 | assert( pTmp!=0 ); |
| 60826 | 60891 | rc = sqlite3PagerWrite(pLeaf->pDbPage); |
| | @@ -60909,11 +60974,12 @@ |
| 60909 | 60974 | */ |
| 60910 | 60975 | while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
| 60911 | 60976 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
| 60912 | 60977 | pgnoRoot++; |
| 60913 | 60978 | } |
| 60914 | | - assert( pgnoRoot>=3 ); |
| 60979 | + assert( pgnoRoot>=3 || CORRUPT_DB ); |
| 60980 | + testcase( pgnoRoot<3 ); |
| 60915 | 60981 | |
| 60916 | 60982 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 60917 | 60983 | ** be moved to the allocated page (unless the allocated page happens |
| 60918 | 60984 | ** to reside at pgnoRoot). |
| 60919 | 60985 | */ |
| | @@ -61059,11 +61125,12 @@ |
| 61059 | 61125 | } |
| 61060 | 61126 | if( !pPage->leaf ){ |
| 61061 | 61127 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange); |
| 61062 | 61128 | if( rc ) goto cleardatabasepage_out; |
| 61063 | 61129 | }else if( pnChange ){ |
| 61064 | | - assert( pPage->intKey ); |
| 61130 | + assert( pPage->intKey || CORRUPT_DB ); |
| 61131 | + testcase( !pPage->intKey ); |
| 61065 | 61132 | *pnChange += pPage->nCell; |
| 61066 | 61133 | } |
| 61067 | 61134 | if( freePageFlag ){ |
| 61068 | 61135 | freePage(pPage, &rc); |
| 61069 | 61136 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
| | @@ -63854,14 +63921,10 @@ |
| 63854 | 63921 | } |
| 63855 | 63922 | pMem->pScopyFrom = 0; |
| 63856 | 63923 | } |
| 63857 | 63924 | #endif /* SQLITE_DEBUG */ |
| 63858 | 63925 | |
| 63859 | | -/* |
| 63860 | | -** Size of struct Mem not including the Mem.zMalloc member. |
| 63861 | | -*/ |
| 63862 | | -#define MEMCELLSIZE offsetof(Mem,zMalloc) |
| 63863 | 63926 | |
| 63864 | 63927 | /* |
| 63865 | 63928 | ** Make an shallow copy of pFrom into pTo. Prior contents of |
| 63866 | 63929 | ** pTo are freed. The pFrom->z field is not duplicated. If |
| 63867 | 63930 | ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z |
| | @@ -63884,11 +63947,14 @@ |
| 63884 | 63947 | ** freed before the copy is made. |
| 63885 | 63948 | */ |
| 63886 | 63949 | SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ |
| 63887 | 63950 | int rc = SQLITE_OK; |
| 63888 | 63951 | |
| 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 ); |
| 63890 | 63956 | assert( (pFrom->flags & MEM_RowSet)==0 ); |
| 63891 | 63957 | if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo); |
| 63892 | 63958 | memcpy(pTo, pFrom, MEMCELLSIZE); |
| 63893 | 63959 | pTo->flags &= ~MEM_Dyn; |
| 63894 | 63960 | if( pTo->flags&(MEM_Str|MEM_Blob) ){ |
| | @@ -64817,10 +64883,21 @@ |
| 64817 | 64883 | assert( pParse->aLabel==0 ); |
| 64818 | 64884 | assert( pParse->nLabel==0 ); |
| 64819 | 64885 | assert( pParse->nOpAlloc==0 ); |
| 64820 | 64886 | return p; |
| 64821 | 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 | +} |
| 64822 | 64899 | |
| 64823 | 64900 | /* |
| 64824 | 64901 | ** Remember the SQL string for a prepared statement. |
| 64825 | 64902 | */ |
| 64826 | 64903 | SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){ |
| | @@ -66174,11 +66251,11 @@ |
| 66174 | 66251 | p->rc = SQLITE_OK; |
| 66175 | 66252 | rc = SQLITE_DONE; |
| 66176 | 66253 | }else if( db->u1.isInterrupted ){ |
| 66177 | 66254 | p->rc = SQLITE_INTERRUPT; |
| 66178 | 66255 | rc = SQLITE_ERROR; |
| 66179 | | - sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc)); |
| 66256 | + sqlite3VdbeError(p, sqlite3ErrStr(p->rc)); |
| 66180 | 66257 | }else{ |
| 66181 | 66258 | char *zP4; |
| 66182 | 66259 | Op *pOp; |
| 66183 | 66260 | if( i<p->nOp ){ |
| 66184 | 66261 | /* The output line number is small enough that we are still in the |
| | @@ -67077,11 +67154,11 @@ |
| 67077 | 67154 | if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0) |
| 67078 | 67155 | || (!deferred && p->nFkConstraint>0) |
| 67079 | 67156 | ){ |
| 67080 | 67157 | p->rc = SQLITE_CONSTRAINT_FOREIGNKEY; |
| 67081 | 67158 | p->errorAction = OE_Abort; |
| 67082 | | - sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed"); |
| 67159 | + sqlite3VdbeError(p, "FOREIGN KEY constraint failed"); |
| 67083 | 67160 | return SQLITE_ERROR; |
| 67084 | 67161 | } |
| 67085 | 67162 | return SQLITE_OK; |
| 67086 | 67163 | } |
| 67087 | 67164 | #endif |
| | @@ -68420,11 +68497,11 @@ |
| 68420 | 68497 | |
| 68421 | 68498 | /* RHS is an integer */ |
| 68422 | 68499 | if( pRhs->flags & MEM_Int ){ |
| 68423 | 68500 | serial_type = aKey1[idx1]; |
| 68424 | 68501 | testcase( serial_type==12 ); |
| 68425 | | - if( serial_type>=12 ){ |
| 68502 | + if( serial_type>=10 ){ |
| 68426 | 68503 | rc = +1; |
| 68427 | 68504 | }else if( serial_type==0 ){ |
| 68428 | 68505 | rc = -1; |
| 68429 | 68506 | }else if( serial_type==7 ){ |
| 68430 | 68507 | double rhs = (double)pRhs->u.i; |
| | @@ -68446,11 +68523,15 @@ |
| 68446 | 68523 | } |
| 68447 | 68524 | |
| 68448 | 68525 | /* RHS is real */ |
| 68449 | 68526 | else if( pRhs->flags & MEM_Real ){ |
| 68450 | 68527 | 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. */ |
| 68452 | 68533 | rc = +1; |
| 68453 | 68534 | }else if( serial_type==0 ){ |
| 68454 | 68535 | rc = -1; |
| 68455 | 68536 | }else{ |
| 68456 | 68537 | double rhs = pRhs->u.r; |
| | @@ -69184,10 +69265,40 @@ |
| 69184 | 69265 | SQLITE_INTEGER, /* 0x1e */ |
| 69185 | 69266 | SQLITE_NULL, /* 0x1f */ |
| 69186 | 69267 | }; |
| 69187 | 69268 | return aType[pVal->flags&MEM_AffMask]; |
| 69188 | 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 | + |
| 69189 | 69300 | |
| 69190 | 69301 | /**************************** sqlite3_result_ ******************************* |
| 69191 | 69302 | ** The following routines are used by user-defined functions to specify |
| 69192 | 69303 | ** the function result. |
| 69193 | 69304 | ** |
| | @@ -71037,10 +71148,11 @@ |
| 71037 | 71148 | ** representation. |
| 71038 | 71149 | */ |
| 71039 | 71150 | if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ |
| 71040 | 71151 | sqlite3VdbeMemStringify(pRec, enc, 1); |
| 71041 | 71152 | } |
| 71153 | + pRec->flags &= ~(MEM_Real|MEM_Int); |
| 71042 | 71154 | } |
| 71043 | 71155 | } |
| 71044 | 71156 | |
| 71045 | 71157 | /* |
| 71046 | 71158 | ** Try to convert the type of a function argument or a result column |
| | @@ -71797,16 +71909,15 @@ |
| 71797 | 71909 | zType = 0; |
| 71798 | 71910 | } |
| 71799 | 71911 | assert( zType!=0 || pOp->p4.z!=0 ); |
| 71800 | 71912 | zLogFmt = "abort at %d in [%s]: %s"; |
| 71801 | 71913 | if( zType && pOp->p4.z ){ |
| 71802 | | - sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s", |
| 71803 | | - zType, pOp->p4.z); |
| 71914 | + sqlite3VdbeError(p, "%s constraint failed: %s", zType, pOp->p4.z); |
| 71804 | 71915 | }else if( pOp->p4.z ){ |
| 71805 | | - sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z); |
| 71916 | + sqlite3VdbeError(p, "%s", pOp->p4.z); |
| 71806 | 71917 | }else{ |
| 71807 | | - sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType); |
| 71918 | + sqlite3VdbeError(p, "%s constraint failed", zType); |
| 71808 | 71919 | } |
| 71809 | 71920 | sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg); |
| 71810 | 71921 | } |
| 71811 | 71922 | rc = sqlite3VdbeHalt(p); |
| 71812 | 71923 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); |
| | @@ -72434,11 +72545,11 @@ |
| 72434 | 72545 | lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */ |
| 72435 | 72546 | |
| 72436 | 72547 | /* If the function returned an error, throw an exception */ |
| 72437 | 72548 | if( ctx.fErrorOrAux ){ |
| 72438 | 72549 | if( ctx.isError ){ |
| 72439 | | - sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut)); |
| 72550 | + sqlite3VdbeError(p, "%s", sqlite3_value_text(ctx.pOut)); |
| 72440 | 72551 | rc = ctx.isError; |
| 72441 | 72552 | } |
| 72442 | 72553 | sqlite3VdbeDeleteAuxData(p, (int)(pOp - aOp), pOp->p1); |
| 72443 | 72554 | } |
| 72444 | 72555 | |
| | @@ -73621,12 +73732,11 @@ |
| 73621 | 73732 | if( p1==SAVEPOINT_BEGIN ){ |
| 73622 | 73733 | if( db->nVdbeWrite>0 ){ |
| 73623 | 73734 | /* A new savepoint cannot be created if there are active write |
| 73624 | 73735 | ** statements (i.e. open read/write incremental blob handles). |
| 73625 | 73736 | */ |
| 73626 | | - sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - " |
| 73627 | | - "SQL statements in progress"); |
| 73737 | + sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress"); |
| 73628 | 73738 | rc = SQLITE_BUSY; |
| 73629 | 73739 | }else{ |
| 73630 | 73740 | nName = sqlite3Strlen30(zName); |
| 73631 | 73741 | |
| 73632 | 73742 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -73673,19 +73783,18 @@ |
| 73673 | 73783 | pSavepoint = pSavepoint->pNext |
| 73674 | 73784 | ){ |
| 73675 | 73785 | iSavepoint++; |
| 73676 | 73786 | } |
| 73677 | 73787 | if( !pSavepoint ){ |
| 73678 | | - sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName); |
| 73788 | + sqlite3VdbeError(p, "no such savepoint: %s", zName); |
| 73679 | 73789 | rc = SQLITE_ERROR; |
| 73680 | 73790 | }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ |
| 73681 | 73791 | /* It is not possible to release (commit) a savepoint if there are |
| 73682 | 73792 | ** active write statements. |
| 73683 | 73793 | */ |
| 73684 | | - sqlite3SetString(&p->zErrMsg, db, |
| 73685 | | - "cannot release savepoint - SQL statements in progress" |
| 73686 | | - ); |
| 73794 | + sqlite3VdbeError(p, "cannot release savepoint - " |
| 73795 | + "SQL statements in progress"); |
| 73687 | 73796 | rc = SQLITE_BUSY; |
| 73688 | 73797 | }else{ |
| 73689 | 73798 | |
| 73690 | 73799 | /* Determine whether or not this is a transaction savepoint. If so, |
| 73691 | 73800 | ** and this is a RELEASE command, then the current transaction |
| | @@ -73787,27 +73896,16 @@ |
| 73787 | 73896 | assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); |
| 73788 | 73897 | assert( desiredAutoCommit==1 || iRollback==0 ); |
| 73789 | 73898 | assert( db->nVdbeActive>0 ); /* At least this one VM is active */ |
| 73790 | 73899 | assert( p->bIsReader ); |
| 73791 | 73900 | |
| 73792 | | -#if 0 |
| 73793 | | - if( turnOnAC && iRollback && db->nVdbeActive>1 ){ |
| 73794 | | - /* If this instruction implements a ROLLBACK and other VMs are |
| 73795 | | - ** still running, and a transaction is active, return an error indicating |
| 73796 | | - ** that the other VMs must complete first. |
| 73797 | | - */ |
| 73798 | | - sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - " |
| 73799 | | - "SQL statements in progress"); |
| 73800 | | - rc = SQLITE_BUSY; |
| 73801 | | - }else |
| 73802 | | -#endif |
| 73803 | 73901 | if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){ |
| 73804 | 73902 | /* If this instruction implements a COMMIT and other VMs are writing |
| 73805 | 73903 | ** return an error indicating that the other VMs must complete first. |
| 73806 | 73904 | */ |
| 73807 | | - sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - " |
| 73808 | | - "SQL statements in progress"); |
| 73905 | + sqlite3VdbeError(p, "cannot commit transaction - " |
| 73906 | + "SQL statements in progress"); |
| 73809 | 73907 | rc = SQLITE_BUSY; |
| 73810 | 73908 | }else if( desiredAutoCommit!=db->autoCommit ){ |
| 73811 | 73909 | if( iRollback ){ |
| 73812 | 73910 | assert( desiredAutoCommit==1 ); |
| 73813 | 73911 | sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| | @@ -73830,11 +73928,11 @@ |
| 73830 | 73928 | }else{ |
| 73831 | 73929 | rc = SQLITE_ERROR; |
| 73832 | 73930 | } |
| 73833 | 73931 | goto vdbe_return; |
| 73834 | 73932 | }else{ |
| 73835 | | - sqlite3SetString(&p->zErrMsg, db, |
| 73933 | + sqlite3VdbeError(p, |
| 73836 | 73934 | (!desiredAutoCommit)?"cannot start a transaction within a transaction":( |
| 73837 | 73935 | (iRollback)?"cannot rollback - no transaction is active": |
| 73838 | 73936 | "cannot commit - no transaction is active")); |
| 73839 | 73937 | |
| 73840 | 73938 | rc = SQLITE_ERROR; |
| | @@ -76263,11 +76361,11 @@ |
| 76263 | 76361 | if( pFrame ) break; |
| 76264 | 76362 | } |
| 76265 | 76363 | |
| 76266 | 76364 | if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ |
| 76267 | 76365 | rc = SQLITE_ERROR; |
| 76268 | | - sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion"); |
| 76366 | + sqlite3VdbeError(p, "too many levels of trigger recursion"); |
| 76269 | 76367 | break; |
| 76270 | 76368 | } |
| 76271 | 76369 | |
| 76272 | 76370 | /* Register pRt is used to store the memory required to save the state |
| 76273 | 76371 | ** of the current program, and the memory required at runtime to execute |
| | @@ -76566,11 +76664,11 @@ |
| 76566 | 76664 | ctx.pVdbe = p; |
| 76567 | 76665 | ctx.iOp = (int)(pOp - aOp); |
| 76568 | 76666 | ctx.skipFlag = 0; |
| 76569 | 76667 | (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */ |
| 76570 | 76668 | if( ctx.isError ){ |
| 76571 | | - sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t)); |
| 76669 | + sqlite3VdbeError(p, "%s", sqlite3_value_text(&t)); |
| 76572 | 76670 | rc = ctx.isError; |
| 76573 | 76671 | } |
| 76574 | 76672 | if( ctx.skipFlag ){ |
| 76575 | 76673 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 76576 | 76674 | i = pOp[-1].p1; |
| | @@ -76598,11 +76696,11 @@ |
| 76598 | 76696 | assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); |
| 76599 | 76697 | pMem = &aMem[pOp->p1]; |
| 76600 | 76698 | assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
| 76601 | 76699 | rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); |
| 76602 | 76700 | if( rc ){ |
| 76603 | | - sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem)); |
| 76701 | + sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); |
| 76604 | 76702 | } |
| 76605 | 76703 | sqlite3VdbeChangeEncoding(pMem, encoding); |
| 76606 | 76704 | UPDATE_MAX_BLOBSIZE(pMem); |
| 76607 | 76705 | if( sqlite3VdbeMemTooBig(pMem) ){ |
| 76608 | 76706 | goto too_big; |
| | @@ -76703,11 +76801,11 @@ |
| 76703 | 76801 | if( (eNew!=eOld) |
| 76704 | 76802 | && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) |
| 76705 | 76803 | ){ |
| 76706 | 76804 | if( !db->autoCommit || db->nVdbeRead>1 ){ |
| 76707 | 76805 | rc = SQLITE_ERROR; |
| 76708 | | - sqlite3SetString(&p->zErrMsg, db, |
| 76806 | + sqlite3VdbeError(p, |
| 76709 | 76807 | "cannot change %s wal mode from within a transaction", |
| 76710 | 76808 | (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") |
| 76711 | 76809 | ); |
| 76712 | 76810 | break; |
| 76713 | 76811 | }else{ |
| | @@ -76834,11 +76932,11 @@ |
| 76834 | 76932 | assert( DbMaskTest(p->btreeMask, p1) ); |
| 76835 | 76933 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 76836 | 76934 | rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); |
| 76837 | 76935 | if( (rc&0xFF)==SQLITE_LOCKED ){ |
| 76838 | 76936 | const char *z = pOp->p4.z; |
| 76839 | | - sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z); |
| 76937 | + sqlite3VdbeError(p, "database table is locked: %s", z); |
| 76840 | 76938 | } |
| 76841 | 76939 | } |
| 76842 | 76940 | break; |
| 76843 | 76941 | } |
| 76844 | 76942 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| | @@ -77382,19 +77480,19 @@ |
| 77382 | 77480 | |
| 77383 | 77481 | /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH |
| 77384 | 77482 | ** is encountered. |
| 77385 | 77483 | */ |
| 77386 | 77484 | too_big: |
| 77387 | | - sqlite3SetString(&p->zErrMsg, db, "string or blob too big"); |
| 77485 | + sqlite3VdbeError(p, "string or blob too big"); |
| 77388 | 77486 | rc = SQLITE_TOOBIG; |
| 77389 | 77487 | goto vdbe_error_halt; |
| 77390 | 77488 | |
| 77391 | 77489 | /* Jump to here if a malloc() fails. |
| 77392 | 77490 | */ |
| 77393 | 77491 | no_mem: |
| 77394 | 77492 | db->mallocFailed = 1; |
| 77395 | | - sqlite3SetString(&p->zErrMsg, db, "out of memory"); |
| 77493 | + sqlite3VdbeError(p, "out of memory"); |
| 77396 | 77494 | rc = SQLITE_NOMEM; |
| 77397 | 77495 | goto vdbe_error_halt; |
| 77398 | 77496 | |
| 77399 | 77497 | /* Jump to here for any other kind of fatal error. The "rc" variable |
| 77400 | 77498 | ** should hold the error number. |
| | @@ -77401,11 +77499,11 @@ |
| 77401 | 77499 | */ |
| 77402 | 77500 | abort_due_to_error: |
| 77403 | 77501 | assert( p->zErrMsg==0 ); |
| 77404 | 77502 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
| 77405 | 77503 | if( rc!=SQLITE_IOERR_NOMEM ){ |
| 77406 | | - sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); |
| 77504 | + sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); |
| 77407 | 77505 | } |
| 77408 | 77506 | goto vdbe_error_halt; |
| 77409 | 77507 | |
| 77410 | 77508 | /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
| 77411 | 77509 | ** flag. |
| | @@ -77412,11 +77510,11 @@ |
| 77412 | 77510 | */ |
| 77413 | 77511 | abort_due_to_interrupt: |
| 77414 | 77512 | assert( db->u1.isInterrupted ); |
| 77415 | 77513 | rc = SQLITE_INTERRUPT; |
| 77416 | 77514 | p->rc = rc; |
| 77417 | | - sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); |
| 77515 | + sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); |
| 77418 | 77516 | goto vdbe_error_halt; |
| 77419 | 77517 | } |
| 77420 | 77518 | |
| 77421 | 77519 | |
| 77422 | 77520 | /************** End of vdbe.c ************************************************/ |
| | @@ -81651,11 +81749,11 @@ |
| 81651 | 81749 | iCol = -1; |
| 81652 | 81750 | } |
| 81653 | 81751 | break; |
| 81654 | 81752 | } |
| 81655 | 81753 | } |
| 81656 | | - if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){ |
| 81754 | + if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){ |
| 81657 | 81755 | /* IMP: R-51414-32910 */ |
| 81658 | 81756 | /* IMP: R-44911-55124 */ |
| 81659 | 81757 | iCol = -1; |
| 81660 | 81758 | } |
| 81661 | 81759 | if( iCol<pTab->nCol ){ |
| | @@ -81681,11 +81779,11 @@ |
| 81681 | 81779 | |
| 81682 | 81780 | /* |
| 81683 | 81781 | ** Perhaps the name is a reference to the ROWID |
| 81684 | 81782 | */ |
| 81685 | 81783 | if( cnt==0 && cntTab==1 && pMatch && sqlite3IsRowid(zCol) |
| 81686 | | - && HasRowid(pMatch->pTab) ){ |
| 81784 | + && VisibleRowid(pMatch->pTab) ){ |
| 81687 | 81785 | cnt = 1; |
| 81688 | 81786 | pExpr->iColumn = -1; /* IMP: R-44911-55124 */ |
| 81689 | 81787 | pExpr->affinity = SQLITE_AFF_INTEGER; |
| 81690 | 81788 | } |
| 81691 | 81789 | |
| | @@ -92125,18 +92223,15 @@ |
| 92125 | 92223 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 92126 | 92224 | sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " |
| 92127 | 92225 | "INTEGER PRIMARY KEY"); |
| 92128 | 92226 | #endif |
| 92129 | 92227 | }else{ |
| 92130 | | - Vdbe *v = pParse->pVdbe; |
| 92131 | 92228 | Index *p; |
| 92132 | | - if( v ) pParse->addrSkipPK = sqlite3VdbeAddOp0(v, OP_Noop); |
| 92133 | 92229 | p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, |
| 92134 | 92230 | 0, sortOrder, 0); |
| 92135 | 92231 | if( p ){ |
| 92136 | 92232 | p->idxType = SQLITE_IDXTYPE_PRIMARYKEY; |
| 92137 | | - if( v ) sqlite3VdbeJumpHere(v, pParse->addrSkipPK); |
| 92138 | 92233 | } |
| 92139 | 92234 | pList = 0; |
| 92140 | 92235 | } |
| 92141 | 92236 | |
| 92142 | 92237 | primary_key_exit: |
| | @@ -92485,18 +92580,10 @@ |
| 92485 | 92580 | if( pParse->addrCrTab ){ |
| 92486 | 92581 | assert( v ); |
| 92487 | 92582 | sqlite3VdbeGetOp(v, pParse->addrCrTab)->opcode = OP_CreateIndex; |
| 92488 | 92583 | } |
| 92489 | 92584 | |
| 92490 | | - /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master |
| 92491 | | - ** table entry. |
| 92492 | | - */ |
| 92493 | | - if( pParse->addrSkipPK ){ |
| 92494 | | - assert( v ); |
| 92495 | | - sqlite3VdbeGetOp(v, pParse->addrSkipPK)->opcode = OP_Goto; |
| 92496 | | - } |
| 92497 | | - |
| 92498 | 92585 | /* Locate the PRIMARY KEY index. Or, if this table was originally |
| 92499 | 92586 | ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. |
| 92500 | 92587 | */ |
| 92501 | 92588 | if( pTab->iPKey>=0 ){ |
| 92502 | 92589 | ExprList *pList; |
| | @@ -92510,10 +92597,20 @@ |
| 92510 | 92597 | if( pPk==0 ) return; |
| 92511 | 92598 | pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY; |
| 92512 | 92599 | pTab->iPKey = -1; |
| 92513 | 92600 | }else{ |
| 92514 | 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 | + |
| 92515 | 92612 | /* |
| 92516 | 92613 | ** Remove all redundant columns from the PRIMARY KEY. For example, change |
| 92517 | 92614 | ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later |
| 92518 | 92615 | ** code assumes the PRIMARY KEY contains no repeated columns. |
| 92519 | 92616 | */ |
| | @@ -92645,11 +92742,11 @@ |
| 92645 | 92742 | return; |
| 92646 | 92743 | } |
| 92647 | 92744 | if( (p->tabFlags & TF_HasPrimaryKey)==0 ){ |
| 92648 | 92745 | sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName); |
| 92649 | 92746 | }else{ |
| 92650 | | - p->tabFlags |= TF_WithoutRowid; |
| 92747 | + p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid; |
| 92651 | 92748 | convertToWithoutRowidTable(pParse, p); |
| 92652 | 92749 | } |
| 92653 | 92750 | } |
| 92654 | 92751 | |
| 92655 | 92752 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
| | @@ -92713,30 +92810,49 @@ |
| 92713 | 92810 | ** as a schema-lock must have already been obtained to create it. Since |
| 92714 | 92811 | ** a schema-lock excludes all other database users, the write-lock would |
| 92715 | 92812 | ** be redundant. |
| 92716 | 92813 | */ |
| 92717 | 92814 | if( pSelect ){ |
| 92718 | | - SelectDest dest; |
| 92719 | | - 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 */ |
| 92720 | 92822 | |
| 92823 | + regYield = ++pParse->nMem; |
| 92824 | + regRec = ++pParse->nMem; |
| 92825 | + regRowid = ++pParse->nMem; |
| 92721 | 92826 | assert(pParse->nTab==1); |
| 92722 | 92827 | sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); |
| 92723 | 92828 | sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); |
| 92724 | 92829 | pParse->nTab = 2; |
| 92725 | | - sqlite3SelectDestInit(&dest, SRT_Table, 1); |
| 92830 | + addrTop = sqlite3VdbeCurrentAddr(v) + 1; |
| 92831 | + sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); |
| 92832 | + sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); |
| 92726 | 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); |
| 92727 | 92853 | sqlite3VdbeAddOp1(v, OP_Close, 1); |
| 92728 | | - if( pParse->nErr==0 ){ |
| 92729 | | - pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect); |
| 92730 | | - if( pSelTab==0 ) return; |
| 92731 | | - assert( p->aCol==0 ); |
| 92732 | | - p->nCol = pSelTab->nCol; |
| 92733 | | - p->aCol = pSelTab->aCol; |
| 92734 | | - pSelTab->nCol = 0; |
| 92735 | | - pSelTab->aCol = 0; |
| 92736 | | - sqlite3DeleteTable(db, pSelTab); |
| 92737 | | - } |
| 92738 | 92854 | } |
| 92739 | 92855 | |
| 92740 | 92856 | /* Compute the complete text of the CREATE statement */ |
| 92741 | 92857 | if( pSelect ){ |
| 92742 | 92858 | zStmt = createTableStmt(db, p); |
| | @@ -94031,14 +94147,19 @@ |
| 94031 | 94147 | int iMem = ++pParse->nMem; |
| 94032 | 94148 | |
| 94033 | 94149 | v = sqlite3GetVdbe(pParse); |
| 94034 | 94150 | if( v==0 ) goto exit_create_index; |
| 94035 | 94151 | |
| 94036 | | - |
| 94037 | | - /* Create the rootpage for the index |
| 94038 | | - */ |
| 94039 | 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); |
| 94040 | 94161 | sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem); |
| 94041 | 94162 | |
| 94042 | 94163 | /* Gather the complete text of the CREATE INDEX statement into |
| 94043 | 94164 | ** the zStmt variable |
| 94044 | 94165 | */ |
| | @@ -94074,10 +94195,12 @@ |
| 94074 | 94195 | sqlite3ChangeCookie(pParse, iDb); |
| 94075 | 94196 | sqlite3VdbeAddParseSchemaOp(v, iDb, |
| 94076 | 94197 | sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName)); |
| 94077 | 94198 | sqlite3VdbeAddOp1(v, OP_Expire, 0); |
| 94078 | 94199 | } |
| 94200 | + |
| 94201 | + sqlite3VdbeJumpHere(v, pIndex->tnum); |
| 94079 | 94202 | } |
| 94080 | 94203 | |
| 94081 | 94204 | /* When adding an index to the list of indices for a table, make |
| 94082 | 94205 | ** sure all indices labeled OE_Replace come after all those labeled |
| 94083 | 94206 | ** OE_Ignore. This is necessary for the correct constraint check |
| | @@ -99669,11 +99792,11 @@ |
| 99669 | 99792 | sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol); |
| 99670 | 99793 | VdbeComment((v, "%s", pTab->zName)); |
| 99671 | 99794 | }else{ |
| 99672 | 99795 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 99673 | 99796 | assert( pPk!=0 ); |
| 99674 | | - assert( pPk->tnum=pTab->tnum ); |
| 99797 | + assert( pPk->tnum==pTab->tnum ); |
| 99675 | 99798 | sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb); |
| 99676 | 99799 | sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
| 99677 | 99800 | VdbeComment((v, "%s", pTab->zName)); |
| 99678 | 99801 | } |
| 99679 | 99802 | } |
| | @@ -102119,10 +102242,12 @@ |
| 102119 | 102242 | void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64, |
| 102120 | 102243 | void(*)(void*)); |
| 102121 | 102244 | void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64, |
| 102122 | 102245 | void(*)(void*), unsigned char); |
| 102123 | 102246 | int (*strglob)(const char*,const char*); |
| 102247 | + sqlite3_value (*value_dup)(const sqlite3_value*); |
| 102248 | + void (*value_free)(sqlite3_value*); |
| 102124 | 102249 | }; |
| 102125 | 102250 | |
| 102126 | 102251 | /* |
| 102127 | 102252 | ** The following macros redefine the API routines so that they are |
| 102128 | 102253 | ** redirected through the global sqlite3_api structure. |
| | @@ -102349,10 +102474,13 @@ |
| 102349 | 102474 | #define sqlite3_realloc64 sqlite3_api->realloc64 |
| 102350 | 102475 | #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension |
| 102351 | 102476 | #define sqlite3_result_blob64 sqlite3_api->result_blob64 |
| 102352 | 102477 | #define sqlite3_result_text64 sqlite3_api->result_text64 |
| 102353 | 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 |
| 102354 | 102482 | #endif /* SQLITE_CORE */ |
| 102355 | 102483 | |
| 102356 | 102484 | #ifndef SQLITE_CORE |
| 102357 | 102485 | /* This case when the file really is being compiled as a loadable |
| 102358 | 102486 | ** extension */ |
| | @@ -103255,10 +103383,14 @@ |
| 103255 | 103383 | #endif |
| 103256 | 103384 | { /* zName: */ "case_sensitive_like", |
| 103257 | 103385 | /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE, |
| 103258 | 103386 | /* ePragFlag: */ 0, |
| 103259 | 103387 | /* iArg: */ 0 }, |
| 103388 | + { /* zName: */ "cell_size_check", |
| 103389 | + /* ePragTyp: */ PragTyp_FLAG, |
| 103390 | + /* ePragFlag: */ 0, |
| 103391 | + /* iArg: */ SQLITE_CellSizeCk }, |
| 103260 | 103392 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 103261 | 103393 | { /* zName: */ "checkpoint_fullfsync", |
| 103262 | 103394 | /* ePragTyp: */ PragTyp_FLAG, |
| 103263 | 103395 | /* ePragFlag: */ 0, |
| 103264 | 103396 | /* iArg: */ SQLITE_CkptFullFSync }, |
| | @@ -103612,11 +103744,11 @@ |
| 103612 | 103744 | /* ePragTyp: */ PragTyp_FLAG, |
| 103613 | 103745 | /* ePragFlag: */ 0, |
| 103614 | 103746 | /* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode }, |
| 103615 | 103747 | #endif |
| 103616 | 103748 | }; |
| 103617 | | -/* Number of pragmas: 59 on by default, 72 total. */ |
| 103749 | +/* Number of pragmas: 60 on by default, 73 total. */ |
| 103618 | 103750 | |
| 103619 | 103751 | /************** End of pragma.h **********************************************/ |
| 103620 | 103752 | /************** Continuing where we left off in pragma.c *********************/ |
| 103621 | 103753 | |
| 103622 | 103754 | /* |
| | @@ -105595,17 +105727,17 @@ |
| 105595 | 105727 | const char *zObj, /* Object being parsed at the point of error */ |
| 105596 | 105728 | const char *zExtra /* Error information */ |
| 105597 | 105729 | ){ |
| 105598 | 105730 | sqlite3 *db = pData->db; |
| 105599 | 105731 | if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){ |
| 105732 | + char *z; |
| 105600 | 105733 | if( zObj==0 ) zObj = "?"; |
| 105601 | | - sqlite3SetString(pData->pzErrMsg, db, |
| 105602 | | - "malformed database schema (%s)", zObj); |
| 105603 | | - if( zExtra ){ |
| 105604 | | - *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg, |
| 105605 | | - "%s - %s", *pData->pzErrMsg, zExtra); |
| 105606 | | - } |
| 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; |
| 105607 | 105739 | } |
| 105608 | 105740 | pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT; |
| 105609 | 105741 | } |
| 105610 | 105742 | |
| 105611 | 105743 | /* |
| | @@ -105793,11 +105925,11 @@ |
| 105793 | 105925 | ** will be closed before this function returns. */ |
| 105794 | 105926 | sqlite3BtreeEnter(pDb->pBt); |
| 105795 | 105927 | if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){ |
| 105796 | 105928 | rc = sqlite3BtreeBeginTrans(pDb->pBt, 0); |
| 105797 | 105929 | if( rc!=SQLITE_OK ){ |
| 105798 | | - sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc)); |
| 105930 | + sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc)); |
| 105799 | 105931 | goto initone_error_out; |
| 105800 | 105932 | } |
| 105801 | 105933 | openedTransaction = 1; |
| 105802 | 105934 | } |
| 105803 | 105935 | |
| | @@ -107177,12 +107309,17 @@ |
| 107177 | 107309 | } |
| 107178 | 107310 | }else if( eDest!=SRT_Exists ){ |
| 107179 | 107311 | /* If the destination is an EXISTS(...) expression, the actual |
| 107180 | 107312 | ** values returned by the SELECT are not required. |
| 107181 | 107313 | */ |
| 107182 | | - sqlite3ExprCodeExprList(pParse, pEList, regResult, |
| 107183 | | - (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); |
| 107184 | 107321 | } |
| 107185 | 107322 | |
| 107186 | 107323 | /* If the DISTINCT keyword was present on the SELECT statement |
| 107187 | 107324 | ** and this row has been seen before, then do not make this row |
| 107188 | 107325 | ** part of the result. |
| | @@ -107275,10 +107412,12 @@ |
| 107275 | 107412 | case SRT_Table: |
| 107276 | 107413 | case SRT_EphemTab: { |
| 107277 | 107414 | int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1); |
| 107278 | 107415 | testcase( eDest==SRT_Table ); |
| 107279 | 107416 | testcase( eDest==SRT_EphemTab ); |
| 107417 | + testcase( eDest==SRT_Fifo ); |
| 107418 | + testcase( eDest==SRT_DistFifo ); |
| 107280 | 107419 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg); |
| 107281 | 107420 | #ifndef SQLITE_OMIT_CTE |
| 107282 | 107421 | if( eDest==SRT_DistFifo ){ |
| 107283 | 107422 | /* If the destination is DistFifo, then cursor (iParm+1) is open |
| 107284 | 107423 | ** on an ephemeral index. If the current row is already present |
| | @@ -107690,14 +107829,11 @@ |
| 107690 | 107829 | for(i=0; i<nSortData; i++){ |
| 107691 | 107830 | sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq+i, regRow+i); |
| 107692 | 107831 | VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan)); |
| 107693 | 107832 | } |
| 107694 | 107833 | switch( eDest ){ |
| 107695 | | - case SRT_Table: |
| 107696 | 107834 | case SRT_EphemTab: { |
| 107697 | | - testcase( eDest==SRT_Table ); |
| 107698 | | - testcase( eDest==SRT_EphemTab ); |
| 107699 | 107835 | sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid); |
| 107700 | 107836 | sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid); |
| 107701 | 107837 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 107702 | 107838 | break; |
| 107703 | 107839 | } |
| | @@ -109042,19 +109178,18 @@ |
| 109042 | 109178 | |
| 109043 | 109179 | /* Suppress the first OFFSET entries if there is an OFFSET clause |
| 109044 | 109180 | */ |
| 109045 | 109181 | codeOffset(v, p->iOffset, iContinue); |
| 109046 | 109182 | |
| 109183 | + assert( pDest->eDest!=SRT_Exists ); |
| 109184 | + assert( pDest->eDest!=SRT_Table ); |
| 109047 | 109185 | switch( pDest->eDest ){ |
| 109048 | 109186 | /* Store the result as data using a unique key. |
| 109049 | 109187 | */ |
| 109050 | | - case SRT_Table: |
| 109051 | 109188 | case SRT_EphemTab: { |
| 109052 | 109189 | int r1 = sqlite3GetTempReg(pParse); |
| 109053 | 109190 | int r2 = sqlite3GetTempReg(pParse); |
| 109054 | | - testcase( pDest->eDest==SRT_Table ); |
| 109055 | | - testcase( pDest->eDest==SRT_EphemTab ); |
| 109056 | 109191 | sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1); |
| 109057 | 109192 | sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2); |
| 109058 | 109193 | sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2); |
| 109059 | 109194 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 109060 | 109195 | sqlite3ReleaseTempReg(pParse, r2); |
| | @@ -109078,20 +109213,10 @@ |
| 109078 | 109213 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1); |
| 109079 | 109214 | sqlite3ReleaseTempReg(pParse, r1); |
| 109080 | 109215 | break; |
| 109081 | 109216 | } |
| 109082 | 109217 | |
| 109083 | | -#if 0 /* Never occurs on an ORDER BY query */ |
| 109084 | | - /* If any row exist in the result set, record that fact and abort. |
| 109085 | | - */ |
| 109086 | | - case SRT_Exists: { |
| 109087 | | - sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm); |
| 109088 | | - /* The LIMIT clause will terminate the loop for us */ |
| 109089 | | - break; |
| 109090 | | - } |
| 109091 | | -#endif |
| 109092 | | - |
| 109093 | 109218 | /* If this is a scalar select that is part of an expression, then |
| 109094 | 109219 | ** store the results in the appropriate memory cell and break out |
| 109095 | 109220 | ** of the scan loop. |
| 109096 | 109221 | */ |
| 109097 | 109222 | case SRT_Mem: { |
| | @@ -110462,11 +110587,11 @@ |
| 110462 | 110587 | if( pTab==0 ) return WRC_Abort; |
| 110463 | 110588 | pTab->nRef = 1; |
| 110464 | 110589 | pTab->zName = sqlite3DbStrDup(db, pCte->zName); |
| 110465 | 110590 | pTab->iPKey = -1; |
| 110466 | 110591 | pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
| 110467 | | - pTab->tabFlags |= TF_Ephemeral; |
| 110592 | + pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid; |
| 110468 | 110593 | pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0); |
| 110469 | 110594 | if( db->mallocFailed ) return SQLITE_NOMEM; |
| 110470 | 110595 | assert( pFrom->pSelect ); |
| 110471 | 110596 | |
| 110472 | 110597 | /* Check if this is a recursive CTE. */ |
| | @@ -110707,17 +110832,10 @@ |
| 110707 | 110832 | ExprList *pNew = 0; |
| 110708 | 110833 | int flags = pParse->db->flags; |
| 110709 | 110834 | int longNames = (flags & SQLITE_FullColNames)!=0 |
| 110710 | 110835 | && (flags & SQLITE_ShortColNames)==0; |
| 110711 | 110836 | |
| 110712 | | - /* When processing FROM-clause subqueries, it is always the case |
| 110713 | | - ** that full_column_names=OFF and short_column_names=ON. The |
| 110714 | | - ** sqlite3ResultSetOfSelect() routine makes it so. */ |
| 110715 | | - assert( (p->selFlags & SF_NestedFrom)==0 |
| 110716 | | - || ((flags & SQLITE_FullColNames)==0 && |
| 110717 | | - (flags & SQLITE_ShortColNames)!=0) ); |
| 110718 | | - |
| 110719 | 110837 | for(k=0; k<pEList->nExpr; k++){ |
| 110720 | 110838 | pE = a[k].pExpr; |
| 110721 | 110839 | pRight = pE->pRight; |
| 110722 | 110840 | assert( pE->op!=TK_DOT || pRight!=0 ); |
| 110723 | 110841 | if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pRight->op!=TK_ALL) ){ |
| | @@ -111295,10 +111413,11 @@ |
| 111295 | 111413 | isAgg = 1; |
| 111296 | 111414 | p->selFlags |= SF_Aggregate; |
| 111297 | 111415 | } |
| 111298 | 111416 | i = -1; |
| 111299 | 111417 | }else if( pTabList->nSrc==1 |
| 111418 | + && (p->selFlags & SF_All)==0 |
| 111300 | 111419 | && OptimizationEnabled(db, SQLITE_SubqCoroutine) |
| 111301 | 111420 | ){ |
| 111302 | 111421 | /* Implement a co-routine that will return a single row of the result |
| 111303 | 111422 | ** set on each invocation. |
| 111304 | 111423 | */ |
| | @@ -111982,13 +112101,13 @@ |
| 111982 | 112101 | ** Generate a human-readable description of a the Select object. |
| 111983 | 112102 | */ |
| 111984 | 112103 | SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){ |
| 111985 | 112104 | int n = 0; |
| 111986 | 112105 | pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 111987 | | - sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p)", |
| 112106 | + sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x", |
| 111988 | 112107 | ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), |
| 111989 | | - ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p |
| 112108 | + ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags |
| 111990 | 112109 | ); |
| 111991 | 112110 | if( p->pSrc && p->pSrc->nSrc ) n++; |
| 111992 | 112111 | if( p->pWhere ) n++; |
| 111993 | 112112 | if( p->pGroupBy ) n++; |
| 111994 | 112113 | if( p->pHaving ) n++; |
| | @@ -114138,16 +114257,14 @@ |
| 114138 | 114257 | /* Create the ephemeral table into which the update results will |
| 114139 | 114258 | ** be stored. |
| 114140 | 114259 | */ |
| 114141 | 114260 | assert( v ); |
| 114142 | 114261 | ephemTab = pParse->nTab++; |
| 114143 | | - sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0)); |
| 114144 | | - sqlite3VdbeChangeP5(v, BTREE_UNORDERED); |
| 114145 | 114262 | |
| 114146 | 114263 | /* fill the ephemeral table |
| 114147 | 114264 | */ |
| 114148 | | - sqlite3SelectDestInit(&dest, SRT_Table, ephemTab); |
| 114265 | + sqlite3SelectDestInit(&dest, SRT_EphemTab, ephemTab); |
| 114149 | 114266 | sqlite3Select(pParse, pSelect, &dest); |
| 114150 | 114267 | |
| 114151 | 114268 | /* Generate code to scan the ephemeral table and call VUpdate. */ |
| 114152 | 114269 | iReg = ++pParse->nMem; |
| 114153 | 114270 | pParse->nMem += pTab->nCol+1; |
| | @@ -115998,10 +116115,11 @@ |
| 115998 | 116115 | # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
| 115999 | 116116 | #endif |
| 116000 | 116117 | #define TERM_LIKEOPT 0x100 /* Virtual terms from the LIKE optimization */ |
| 116001 | 116118 | #define TERM_LIKECOND 0x200 /* Conditionally this LIKE operator term */ |
| 116002 | 116119 | #define TERM_LIKE 0x400 /* The original LIKE operator */ |
| 116120 | +#define TERM_IS 0x800 /* Term.pExpr is an IS operator */ |
| 116003 | 116121 | |
| 116004 | 116122 | /* |
| 116005 | 116123 | ** An instance of the WhereScan object is used as an iterator for locating |
| 116006 | 116124 | ** terms in the WHERE clause that are useful to the query planner. |
| 116007 | 116125 | */ |
| | @@ -116146,25 +116264,26 @@ |
| 116146 | 116264 | ** Bitmasks for the operators on WhereTerm objects. These are all |
| 116147 | 116265 | ** operators that are of interest to the query planner. An |
| 116148 | 116266 | ** OR-ed combination of these values can be used when searching for |
| 116149 | 116267 | ** particular WhereTerms within a WhereClause. |
| 116150 | 116268 | */ |
| 116151 | | -#define WO_IN 0x001 |
| 116152 | | -#define WO_EQ 0x002 |
| 116269 | +#define WO_IN 0x0001 |
| 116270 | +#define WO_EQ 0x0002 |
| 116153 | 116271 | #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 116154 | 116272 | #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| 116155 | 116273 | #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) |
| 116156 | 116274 | #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) |
| 116157 | | -#define WO_MATCH 0x040 |
| 116158 | | -#define WO_ISNULL 0x080 |
| 116159 | | -#define WO_OR 0x100 /* Two or more OR-connected terms */ |
| 116160 | | -#define WO_AND 0x200 /* Two or more AND-connected terms */ |
| 116161 | | -#define WO_EQUIV 0x400 /* Of the form A==B, both columns */ |
| 116162 | | -#define WO_NOOP 0x800 /* This term does not restrict search space */ |
| 116163 | | - |
| 116164 | | -#define WO_ALL 0xfff /* Mask of all possible WO_* values */ |
| 116165 | | -#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 */ |
| 116166 | 116285 | |
| 116167 | 116286 | /* |
| 116168 | 116287 | ** These are definitions of bits in the WhereLoop.wsFlags field. |
| 116169 | 116288 | ** The particular combination of bits in each WhereLoop help to |
| 116170 | 116289 | ** determine the algorithm that WhereLoop represents. |
| | @@ -116534,11 +116653,11 @@ |
| 116534 | 116653 | static int allowedOp(int op){ |
| 116535 | 116654 | assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 116536 | 116655 | assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 116537 | 116656 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 116538 | 116657 | assert( TK_GE==TK_EQ+4 ); |
| 116539 | | - 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; |
| 116540 | 116659 | } |
| 116541 | 116660 | |
| 116542 | 116661 | /* |
| 116543 | 116662 | ** Commute a comparison operator. Expressions of the form "X op Y" |
| 116544 | 116663 | ** are converted into "Y op X". |
| | @@ -116587,10 +116706,12 @@ |
| 116587 | 116706 | assert( allowedOp(op) ); |
| 116588 | 116707 | if( op==TK_IN ){ |
| 116589 | 116708 | c = WO_IN; |
| 116590 | 116709 | }else if( op==TK_ISNULL ){ |
| 116591 | 116710 | c = WO_ISNULL; |
| 116711 | + }else if( op==TK_IS ){ |
| 116712 | + c = WO_IS; |
| 116592 | 116713 | }else{ |
| 116593 | 116714 | assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); |
| 116594 | 116715 | c = (u16)(WO_EQ<<(op-TK_EQ)); |
| 116595 | 116716 | } |
| 116596 | 116717 | assert( op!=TK_ISNULL || c==WO_ISNULL ); |
| | @@ -116598,10 +116719,11 @@ |
| 116598 | 116719 | assert( op!=TK_EQ || c==WO_EQ ); |
| 116599 | 116720 | assert( op!=TK_LT || c==WO_LT ); |
| 116600 | 116721 | assert( op!=TK_LE || c==WO_LE ); |
| 116601 | 116722 | assert( op!=TK_GT || c==WO_GT ); |
| 116602 | 116723 | assert( op!=TK_GE || c==WO_GE ); |
| 116724 | + assert( op!=TK_IS || c==WO_IS ); |
| 116603 | 116725 | return c; |
| 116604 | 116726 | } |
| 116605 | 116727 | |
| 116606 | 116728 | /* |
| 116607 | 116729 | ** Advance to the next WhereTerm that matches according to the criteria |
| | @@ -116658,15 +116780,16 @@ |
| 116658 | 116780 | if( pColl==0 ) pColl = pParse->db->pDfltColl; |
| 116659 | 116781 | if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 116660 | 116782 | continue; |
| 116661 | 116783 | } |
| 116662 | 116784 | } |
| 116663 | | - if( (pTerm->eOperator & WO_EQ)!=0 |
| 116785 | + if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0 |
| 116664 | 116786 | && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 116665 | 116787 | && pX->iTable==pScan->aEquiv[0] |
| 116666 | 116788 | && pX->iColumn==pScan->aEquiv[1] |
| 116667 | 116789 | ){ |
| 116790 | + testcase( pTerm->eOperator & WO_IS ); |
| 116668 | 116791 | continue; |
| 116669 | 116792 | } |
| 116670 | 116793 | pScan->k = k+1; |
| 116671 | 116794 | return pTerm; |
| 116672 | 116795 | } |
| | @@ -116764,13 +116887,15 @@ |
| 116764 | 116887 | WhereTerm *pResult = 0; |
| 116765 | 116888 | WhereTerm *p; |
| 116766 | 116889 | WhereScan scan; |
| 116767 | 116890 | |
| 116768 | 116891 | p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 116892 | + op &= WO_EQ|WO_IS; |
| 116769 | 116893 | while( p ){ |
| 116770 | 116894 | if( (p->prereqRight & notReady)==0 ){ |
| 116771 | | - if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){ |
| 116895 | + if( p->prereqRight==0 && (p->eOperator&op)!=0 ){ |
| 116896 | + testcase( p->eOperator & WO_IS ); |
| 116772 | 116897 | return p; |
| 116773 | 116898 | } |
| 116774 | 116899 | if( pResult==0 ) pResult = p; |
| 116775 | 116900 | } |
| 116776 | 116901 | p = whereScanNext(&scan); |
| | @@ -116801,11 +116926,11 @@ |
| 116801 | 116926 | ** so and false if not. |
| 116802 | 116927 | ** |
| 116803 | 116928 | ** In order for the operator to be optimizible, the RHS must be a string |
| 116804 | 116929 | ** literal that does not begin with a wildcard. The LHS must be a column |
| 116805 | 116930 | ** that may only be NULL, a string, or a BLOB, never a number. (This means |
| 116806 | | -** that virtual tables cannot participate in the LIKE optimization.) If the |
| 116931 | +** that virtual tables cannot participate in the LIKE optimization.) The |
| 116807 | 116932 | ** collating sequence for the column on the LHS must be appropriate for |
| 116808 | 116933 | ** the operator. |
| 116809 | 116934 | */ |
| 116810 | 116935 | static int isLikeOrGlob( |
| 116811 | 116936 | Parse *pParse, /* Parsing and code generating context */ |
| | @@ -117346,10 +117471,50 @@ |
| 117346 | 117471 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */ |
| 117347 | 117472 | } |
| 117348 | 117473 | } |
| 117349 | 117474 | } |
| 117350 | 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 | +} |
| 117351 | 117516 | |
| 117352 | 117517 | /* |
| 117353 | 117518 | ** The input to this routine is an WhereTerm structure with only the |
| 117354 | 117519 | ** "pExpr" field filled in. The job of this routine is to analyze the |
| 117355 | 117520 | ** subexpression and populate all the other fields of the WhereTerm |
| | @@ -117425,10 +117590,11 @@ |
| 117425 | 117590 | if( pLeft->op==TK_COLUMN ){ |
| 117426 | 117591 | pTerm->leftCursor = pLeft->iTable; |
| 117427 | 117592 | pTerm->u.leftColumn = pLeft->iColumn; |
| 117428 | 117593 | pTerm->eOperator = operatorMask(op) & opMask; |
| 117429 | 117594 | } |
| 117595 | + if( op==TK_IS ) pTerm->wtFlags |= TERM_IS; |
| 117430 | 117596 | if( pRight && pRight->op==TK_COLUMN ){ |
| 117431 | 117597 | WhereTerm *pNew; |
| 117432 | 117598 | Expr *pDup; |
| 117433 | 117599 | u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
| 117434 | 117600 | if( pTerm->leftCursor>=0 ){ |
| | @@ -117440,16 +117606,15 @@ |
| 117440 | 117606 | } |
| 117441 | 117607 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 117442 | 117608 | if( idxNew==0 ) return; |
| 117443 | 117609 | pNew = &pWC->a[idxNew]; |
| 117444 | 117610 | markTermAsChild(pWC, idxNew, idxTerm); |
| 117611 | + if( op==TK_IS ) pNew->wtFlags |= TERM_IS; |
| 117445 | 117612 | pTerm = &pWC->a[idxTerm]; |
| 117446 | 117613 | pTerm->wtFlags |= TERM_COPIED; |
| 117447 | | - if( pExpr->op==TK_EQ |
| 117448 | | - && !ExprHasProperty(pExpr, EP_FromJoin) |
| 117449 | | - && OptimizationEnabled(db, SQLITE_Transitive) |
| 117450 | | - ){ |
| 117614 | + |
| 117615 | + if( termIsEquivalence(pParse, pDup) ){ |
| 117451 | 117616 | pTerm->eOperator |= WO_EQUIV; |
| 117452 | 117617 | eExtraOp = WO_EQUIV; |
| 117453 | 117618 | } |
| 117454 | 117619 | }else{ |
| 117455 | 117620 | pDup = pExpr; |
| | @@ -117639,14 +117804,11 @@ |
| 117639 | 117804 | /* When sqlite_stat3 histogram data is available an operator of the |
| 117640 | 117805 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 117641 | 117806 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 117642 | 117807 | ** virtual term of that form. |
| 117643 | 117808 | ** |
| 117644 | | - ** Note that the virtual term must be tagged with TERM_VNULL. This |
| 117645 | | - ** TERM_VNULL tag will suppress the not-null check at the beginning |
| 117646 | | - ** of the loop. Without the TERM_VNULL flag, the not-null check at |
| 117647 | | - ** the start of the loop will prevent any results from being returned. |
| 117809 | + ** Note that the virtual term must be tagged with TERM_VNULL. |
| 117648 | 117810 | */ |
| 117649 | 117811 | if( pExpr->op==TK_NOTNULL |
| 117650 | 117812 | && pExpr->pLeft->op==TK_COLUMN |
| 117651 | 117813 | && pExpr->pLeft->iColumn>=0 |
| 117652 | 117814 | && OptimizationEnabled(db, SQLITE_Stat34) |
| | @@ -117787,10 +117949,40 @@ |
| 117787 | 117949 | ** Estimate the logarithm of the input value to base 2. |
| 117788 | 117950 | */ |
| 117789 | 117951 | static LogEst estLog(LogEst N){ |
| 117790 | 117952 | return N<=10 ? 0 : sqlite3LogEst(N) - 33; |
| 117791 | 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 | +} |
| 117792 | 117984 | |
| 117793 | 117985 | /* |
| 117794 | 117986 | ** Two routines for printing the content of an sqlite3_index_info |
| 117795 | 117987 | ** structure. Used for testing and debugging only. If neither |
| 117796 | 117988 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| | @@ -117846,15 +118038,16 @@ |
| 117846 | 118038 | struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 117847 | 118039 | Bitmask notReady /* Tables in outer loops of the join */ |
| 117848 | 118040 | ){ |
| 117849 | 118041 | char aff; |
| 117850 | 118042 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
| 117851 | | - if( (pTerm->eOperator & WO_EQ)==0 ) return 0; |
| 118043 | + if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0; |
| 117852 | 118044 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
| 117853 | 118045 | if( pTerm->u.leftColumn<0 ) return 0; |
| 117854 | 118046 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 117855 | 118047 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 118048 | + testcase( pTerm->pExpr->op==TK_IS ); |
| 117856 | 118049 | return 1; |
| 117857 | 118050 | } |
| 117858 | 118051 | #endif |
| 117859 | 118052 | |
| 117860 | 118053 | |
| | @@ -117889,10 +118082,11 @@ |
| 117889 | 118082 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 117890 | 118083 | Bitmask extraCols; /* Bitmap of additional columns */ |
| 117891 | 118084 | u8 sentWarning = 0; /* True if a warnning has been issued */ |
| 117892 | 118085 | Expr *pPartial = 0; /* Partial Index Expression */ |
| 117893 | 118086 | int iContinue = 0; /* Jump here to skip excluded rows */ |
| 118087 | + struct SrcList_item *pTabItem; /* FROM clause term being indexed */ |
| 117894 | 118088 | |
| 117895 | 118089 | /* Generate code to skip over the creation and initialization of the |
| 117896 | 118090 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 117897 | 118091 | v = pParse->pVdbe; |
| 117898 | 118092 | assert( v!=0 ); |
| | @@ -118014,11 +118208,20 @@ |
| 118014 | 118208 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 118015 | 118209 | VdbeComment((v, "for %s", pTable->zName)); |
| 118016 | 118210 | |
| 118017 | 118211 | /* Fill the automatic index with content */ |
| 118018 | 118212 | sqlite3ExprCachePush(pParse); |
| 118019 | | - 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 | + } |
| 118020 | 118223 | if( pPartial ){ |
| 118021 | 118224 | iContinue = sqlite3VdbeMakeLabel(v); |
| 118022 | 118225 | sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL); |
| 118023 | 118226 | pLoop->wsFlags |= WHERE_PARTIALIDX; |
| 118024 | 118227 | } |
| | @@ -118025,11 +118228,17 @@ |
| 118025 | 118228 | regRecord = sqlite3GetTempReg(pParse); |
| 118026 | 118229 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0); |
| 118027 | 118230 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 118028 | 118231 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 118029 | 118232 | if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue); |
| 118030 | | - 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 | + } |
| 118031 | 118240 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
| 118032 | 118241 | sqlite3VdbeJumpHere(v, addrTop); |
| 118033 | 118242 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 118034 | 118243 | sqlite3ExprCachePop(pParse); |
| 118035 | 118244 | |
| | @@ -118067,12 +118276,13 @@ |
| 118067 | 118276 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 118068 | 118277 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
| 118069 | 118278 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 118070 | 118279 | testcase( pTerm->eOperator & WO_IN ); |
| 118071 | 118280 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 118281 | + testcase( pTerm->eOperator & WO_IS ); |
| 118072 | 118282 | testcase( pTerm->eOperator & WO_ALL ); |
| 118073 | | - if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue; |
| 118283 | + if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue; |
| 118074 | 118284 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
| 118075 | 118285 | nTerm++; |
| 118076 | 118286 | } |
| 118077 | 118287 | |
| 118078 | 118288 | /* If the ORDER BY clause contains only columns in the current |
| | @@ -118119,13 +118329,14 @@ |
| 118119 | 118329 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 118120 | 118330 | u8 op; |
| 118121 | 118331 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
| 118122 | 118332 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 118123 | 118333 | testcase( pTerm->eOperator & WO_IN ); |
| 118334 | + testcase( pTerm->eOperator & WO_IS ); |
| 118124 | 118335 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 118125 | 118336 | testcase( pTerm->eOperator & WO_ALL ); |
| 118126 | | - if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue; |
| 118337 | + if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue; |
| 118127 | 118338 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
| 118128 | 118339 | pIdxCons[j].iColumn = pTerm->u.leftColumn; |
| 118129 | 118340 | pIdxCons[j].iTermOffset = i; |
| 118130 | 118341 | op = (u8)pTerm->eOperator & WO_ALL; |
| 118131 | 118342 | if( op==WO_IN ) op = WO_EQ; |
| | @@ -118963,11 +119174,11 @@ |
| 118963 | 119174 | Expr *pX = pTerm->pExpr; |
| 118964 | 119175 | Vdbe *v = pParse->pVdbe; |
| 118965 | 119176 | int iReg; /* Register holding results */ |
| 118966 | 119177 | |
| 118967 | 119178 | assert( iTarget>0 ); |
| 118968 | | - if( pX->op==TK_EQ ){ |
| 119179 | + if( pX->op==TK_EQ || pX->op==TK_IS ){ |
| 118969 | 119180 | iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
| 118970 | 119181 | }else if( pX->op==TK_ISNULL ){ |
| 118971 | 119182 | iReg = iTarget; |
| 118972 | 119183 | sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); |
| 118973 | 119184 | #ifndef SQLITE_OMIT_SUBQUERY |
| | @@ -119148,11 +119359,11 @@ |
| 119148 | 119359 | } |
| 119149 | 119360 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 119150 | 119361 | testcase( pTerm->eOperator & WO_IN ); |
| 119151 | 119362 | if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ |
| 119152 | 119363 | Expr *pRight = pTerm->pExpr->pRight; |
| 119153 | | - if( sqlite3ExprCanBeNull(pRight) ){ |
| 119364 | + if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){ |
| 119154 | 119365 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk); |
| 119155 | 119366 | VdbeCoverage(v); |
| 119156 | 119367 | } |
| 119157 | 119368 | if( zAff ){ |
| 119158 | 119369 | if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){ |
| | @@ -120270,20 +120481,23 @@ |
| 120270 | 120481 | */ |
| 120271 | 120482 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 120272 | 120483 | Expr *pE, *pEAlt; |
| 120273 | 120484 | WhereTerm *pAlt; |
| 120274 | 120485 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 120275 | | - 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; |
| 120276 | 120488 | if( pTerm->leftCursor!=iCur ) continue; |
| 120277 | 120489 | if( pLevel->iLeftJoin ) continue; |
| 120278 | 120490 | pE = pTerm->pExpr; |
| 120279 | 120491 | assert( !ExprHasProperty(pE, EP_FromJoin) ); |
| 120280 | 120492 | assert( (pTerm->prereqRight & pLevel->notReady)!=0 ); |
| 120281 | | - 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); |
| 120282 | 120495 | if( pAlt==0 ) continue; |
| 120283 | 120496 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
| 120284 | 120497 | testcase( pAlt->eOperator & WO_EQ ); |
| 120498 | + testcase( pAlt->eOperator & WO_IS ); |
| 120285 | 120499 | testcase( pAlt->eOperator & WO_IN ); |
| 120286 | 120500 | VdbeModuleComment((v, "begin transitive constraint")); |
| 120287 | 120501 | pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt)); |
| 120288 | 120502 | if( pEAlt ){ |
| 120289 | 120503 | *pEAlt = *pAlt->pExpr; |
| | @@ -120329,13 +120543,14 @@ |
| 120329 | 120543 | char zType[4]; |
| 120330 | 120544 | memcpy(zType, "...", 4); |
| 120331 | 120545 | if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; |
| 120332 | 120546 | if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; |
| 120333 | 120547 | if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; |
| 120334 | | - sqlite3DebugPrintf("TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x\n", |
| 120335 | | - iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb, |
| 120336 | | - 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); |
| 120337 | 120552 | sqlite3TreeViewExpr(0, pTerm->pExpr, 0); |
| 120338 | 120553 | } |
| 120339 | 120554 | } |
| 120340 | 120555 | #endif |
| 120341 | 120556 | |
| | @@ -120821,12 +121036,13 @@ |
| 120821 | 121036 | pLoop->nOut += pTerm->truthProb; |
| 120822 | 121037 | }else{ |
| 120823 | 121038 | /* In the absence of explicit truth probabilities, use heuristics to |
| 120824 | 121039 | ** guess a reasonable truth probability. */ |
| 120825 | 121040 | pLoop->nOut--; |
| 120826 | | - if( pTerm->eOperator&WO_EQ ){ |
| 121041 | + if( pTerm->eOperator&(WO_EQ|WO_IS) ){ |
| 120827 | 121042 | Expr *pRight = pTerm->pExpr->pRight; |
| 121043 | + testcase( pTerm->pExpr->op==TK_IS ); |
| 120828 | 121044 | if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){ |
| 120829 | 121045 | k = 10; |
| 120830 | 121046 | }else{ |
| 120831 | 121047 | k = 20; |
| 120832 | 121048 | } |
| | @@ -120890,14 +121106,14 @@ |
| 120890 | 121106 | |
| 120891 | 121107 | assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 120892 | 121108 | assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 120893 | 121109 | if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 120894 | 121110 | opMask = WO_LT|WO_LE; |
| 120895 | | - }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){ |
| 121111 | + }else if( /*pProbe->tnum<=0 ||*/ (pSrc->jointype & JT_LEFT)!=0 ){ |
| 120896 | 121112 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; |
| 120897 | 121113 | }else{ |
| 120898 | | - 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; |
| 120899 | 121115 | } |
| 120900 | 121116 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
| 120901 | 121117 | |
| 120902 | 121118 | assert( pNew->u.btree.nEq<pProbe->nColumn ); |
| 120903 | 121119 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
| | @@ -120956,11 +121172,11 @@ |
| 120956 | 121172 | nIn = sqlite3LogEst(pExpr->x.pList->nExpr); |
| 120957 | 121173 | } |
| 120958 | 121174 | assert( nIn>0 ); /* RHS always has 2 or more terms... The parser |
| 120959 | 121175 | ** changes "x IN (?)" into "x=?". */ |
| 120960 | 121176 | |
| 120961 | | - }else if( eOp & (WO_EQ) ){ |
| 121177 | + }else if( eOp & (WO_EQ|WO_IS) ){ |
| 120962 | 121178 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
| 120963 | 121179 | if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){ |
| 120964 | 121180 | if( iCol>=0 && pProbe->uniqNotNull==0 ){ |
| 120965 | 121181 | pNew->wsFlags |= WHERE_UNQ_WANTED; |
| 120966 | 121182 | }else{ |
| | @@ -121006,11 +121222,11 @@ |
| 121006 | 121222 | /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4 |
| 121007 | 121223 | ** data, using some other estimate. */ |
| 121008 | 121224 | whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew); |
| 121009 | 121225 | }else{ |
| 121010 | 121226 | int nEq = ++pNew->u.btree.nEq; |
| 121011 | | - assert( eOp & (WO_ISNULL|WO_EQ|WO_IN) ); |
| 121227 | + assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) ); |
| 121012 | 121228 | |
| 121013 | 121229 | assert( pNew->nOut==saved_nOut ); |
| 121014 | 121230 | if( pTerm->truthProb<=0 && iCol>=0 ){ |
| 121015 | 121231 | assert( (eOp & WO_IN) || nIn==0 ); |
| 121016 | 121232 | testcase( eOp & WO_IN ); |
| | @@ -121023,12 +121239,13 @@ |
| 121023 | 121239 | && pProbe->nSample |
| 121024 | 121240 | && pNew->u.btree.nEq<=pProbe->nSampleCol |
| 121025 | 121241 | && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)) |
| 121026 | 121242 | ){ |
| 121027 | 121243 | Expr *pExpr = pTerm->pExpr; |
| 121028 | | - if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){ |
| 121244 | + if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){ |
| 121029 | 121245 | testcase( eOp & WO_EQ ); |
| 121246 | + testcase( eOp & WO_IS ); |
| 121030 | 121247 | testcase( eOp & WO_ISNULL ); |
| 121031 | 121248 | rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut); |
| 121032 | 121249 | }else{ |
| 121033 | 121250 | rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut); |
| 121034 | 121251 | } |
| | @@ -121293,19 +121510,18 @@ |
| 121293 | 121510 | rSize = pTab->nRowLogEst; |
| 121294 | 121511 | rLogSize = estLog(rSize); |
| 121295 | 121512 | |
| 121296 | 121513 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 121297 | 121514 | /* Automatic indexes */ |
| 121298 | | - if( !pBuilder->pOrSet |
| 121515 | + if( !pBuilder->pOrSet /* Not part of an OR optimization */ |
| 121299 | 121516 | && (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0 |
| 121300 | 121517 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
| 121301 | | - && pSrc->pIndex==0 |
| 121302 | | - && !pSrc->viaCoroutine |
| 121303 | | - && !pSrc->notIndexed |
| 121304 | | - && HasRowid(pTab) |
| 121305 | | - && !pSrc->isCorrelated |
| 121306 | | - && !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. */ |
| 121307 | 121523 | ){ |
| 121308 | 121524 | /* Generate auto-index WhereLoops */ |
| 121309 | 121525 | WhereTerm *pTerm; |
| 121310 | 121526 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 121311 | 121527 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
| | @@ -121861,21 +122077,22 @@ |
| 121861 | 122077 | if( MASKBIT(i) & obSat ) continue; |
| 121862 | 122078 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 121863 | 122079 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 121864 | 122080 | if( pOBExpr->iTable!=iCur ) continue; |
| 121865 | 122081 | pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
| 121866 | | - ~ready, WO_EQ|WO_ISNULL, 0); |
| 122082 | + ~ready, WO_EQ|WO_ISNULL|WO_IS, 0); |
| 121867 | 122083 | if( pTerm==0 ) continue; |
| 121868 | | - if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){ |
| 122084 | + if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){ |
| 121869 | 122085 | const char *z1, *z2; |
| 121870 | 122086 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 121871 | 122087 | if( !pColl ) pColl = db->pDfltColl; |
| 121872 | 122088 | z1 = pColl->zName; |
| 121873 | 122089 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); |
| 121874 | 122090 | if( !pColl ) pColl = db->pDfltColl; |
| 121875 | 122091 | z2 = pColl->zName; |
| 121876 | 122092 | if( sqlite3StrICmp(z1, z2)!=0 ) continue; |
| 122093 | + testcase( pTerm->pExpr->op==TK_IS ); |
| 121877 | 122094 | } |
| 121878 | 122095 | obSat |= MASKBIT(i); |
| 121879 | 122096 | } |
| 121880 | 122097 | |
| 121881 | 122098 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| | @@ -121902,11 +122119,11 @@ |
| 121902 | 122119 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 121903 | 122120 | |
| 121904 | 122121 | /* Skip over == and IS NULL terms */ |
| 121905 | 122122 | if( j<pLoop->u.btree.nEq |
| 121906 | 122123 | && pLoop->nSkip==0 |
| 121907 | | - && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
| 122124 | + && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL|WO_IS))!=0 |
| 121908 | 122125 | ){ |
| 121909 | 122126 | if( i & WO_ISNULL ){ |
| 121910 | 122127 | testcase( isOrderDistinct ); |
| 121911 | 122128 | isOrderDistinct = 0; |
| 121912 | 122129 | } |
| | @@ -122475,28 +122692,32 @@ |
| 122475 | 122692 | iCur = pItem->iCursor; |
| 122476 | 122693 | pWC = &pWInfo->sWC; |
| 122477 | 122694 | pLoop = pBuilder->pNew; |
| 122478 | 122695 | pLoop->wsFlags = 0; |
| 122479 | 122696 | pLoop->nSkip = 0; |
| 122480 | | - pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
| 122697 | + pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0); |
| 122481 | 122698 | if( pTerm ){ |
| 122699 | + testcase( pTerm->eOperator & WO_IS ); |
| 122482 | 122700 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 122483 | 122701 | pLoop->aLTerm[0] = pTerm; |
| 122484 | 122702 | pLoop->nLTerm = 1; |
| 122485 | 122703 | pLoop->u.btree.nEq = 1; |
| 122486 | 122704 | /* TUNING: Cost of a rowid lookup is 10 */ |
| 122487 | 122705 | pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ |
| 122488 | 122706 | }else{ |
| 122489 | 122707 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 122708 | + int opMask; |
| 122490 | 122709 | assert( pLoop->aLTermSpace==pLoop->aLTerm ); |
| 122491 | 122710 | if( !IsUniqueIndex(pIdx) |
| 122492 | 122711 | || pIdx->pPartIdxWhere!=0 |
| 122493 | 122712 | || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) |
| 122494 | 122713 | ) continue; |
| 122714 | + opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ; |
| 122495 | 122715 | for(j=0; j<pIdx->nKeyCol; j++){ |
| 122496 | | - pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx); |
| 122716 | + pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx); |
| 122497 | 122717 | if( pTerm==0 ) break; |
| 122718 | + testcase( pTerm->eOperator & WO_IS ); |
| 122498 | 122719 | pLoop->aLTerm[j] = pTerm; |
| 122499 | 122720 | } |
| 122500 | 122721 | if( j!=pIdx->nKeyCol ) continue; |
| 122501 | 122722 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; |
| 122502 | 122723 | if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ |
| | @@ -123131,30 +123352,16 @@ |
| 123131 | 123352 | Table *pTab = pTabItem->pTab; |
| 123132 | 123353 | assert( pTab!=0 ); |
| 123133 | 123354 | pLoop = pLevel->pWLoop; |
| 123134 | 123355 | |
| 123135 | 123356 | /* For a co-routine, change all OP_Column references to the table of |
| 123136 | | - ** 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. |
| 123137 | 123358 | ** OP_Rowid becomes OP_Null. |
| 123138 | 123359 | */ |
| 123139 | 123360 | if( pTabItem->viaCoroutine && !db->mallocFailed ){ |
| 123140 | | - last = sqlite3VdbeCurrentAddr(v); |
| 123141 | | - k = pLevel->addrBody; |
| 123142 | | - pOp = sqlite3VdbeGetOp(v, k); |
| 123143 | | - for(; k<last; k++, pOp++){ |
| 123144 | | - if( pOp->p1!=pLevel->iTabCur ) continue; |
| 123145 | | - if( pOp->opcode==OP_Column ){ |
| 123146 | | - pOp->opcode = OP_Copy; |
| 123147 | | - pOp->p1 = pOp->p2 + pTabItem->regResult; |
| 123148 | | - pOp->p2 = pOp->p3; |
| 123149 | | - pOp->p3 = 0; |
| 123150 | | - }else if( pOp->opcode==OP_Rowid ){ |
| 123151 | | - pOp->opcode = OP_Null; |
| 123152 | | - pOp->p1 = 0; |
| 123153 | | - pOp->p3 = 0; |
| 123154 | | - } |
| 123155 | | - } |
| 123361 | + translateColumnToCopy(v, pLevel->addrBody, pLevel->iTabCur, |
| 123362 | + pTabItem->regResult); |
| 123156 | 123363 | continue; |
| 123157 | 123364 | } |
| 123158 | 123365 | |
| 123159 | 123366 | /* Close all of the cursors that were opened by sqlite3WhereBegin. |
| 123160 | 123367 | ** Except, do not close cursors that will be reused by the OR optimization |
| | @@ -125418,11 +125625,11 @@ |
| 125418 | 125625 | {yygotominor.yy186 = 0;} |
| 125419 | 125626 | break; |
| 125420 | 125627 | case 35: /* table_options ::= WITHOUT nm */ |
| 125421 | 125628 | { |
| 125422 | 125629 | if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){ |
| 125423 | | - yygotominor.yy186 = TF_WithoutRowid; |
| 125630 | + yygotominor.yy186 = TF_WithoutRowid | TF_NoVisibleRowid; |
| 125424 | 125631 | }else{ |
| 125425 | 125632 | yygotominor.yy186 = 0; |
| 125426 | 125633 | sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z); |
| 125427 | 125634 | } |
| 125428 | 125635 | } |
| | @@ -125642,10 +125849,11 @@ |
| 125642 | 125849 | {yygotominor.yy3 = yymsp[0].minor.yy3;} |
| 125643 | 125850 | break; |
| 125644 | 125851 | case 114: /* selectnowith ::= selectnowith multiselect_op oneselect */ |
| 125645 | 125852 | { |
| 125646 | 125853 | Select *pRhs = yymsp[0].minor.yy3; |
| 125854 | + Select *pLhs = yymsp[-2].minor.yy3; |
| 125647 | 125855 | if( pRhs && pRhs->pPrior ){ |
| 125648 | 125856 | SrcList *pFrom; |
| 125649 | 125857 | Token x; |
| 125650 | 125858 | x.n = 0; |
| 125651 | 125859 | parserDoubleLinkSelect(pParse, pRhs); |
| | @@ -125652,15 +125860,16 @@ |
| 125652 | 125860 | pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0); |
| 125653 | 125861 | pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0); |
| 125654 | 125862 | } |
| 125655 | 125863 | if( pRhs ){ |
| 125656 | 125864 | pRhs->op = (u8)yymsp[-1].minor.yy328; |
| 125657 | | - pRhs->pPrior = yymsp[-2].minor.yy3; |
| 125865 | + pRhs->pPrior = pLhs; |
| 125866 | + if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue; |
| 125658 | 125867 | pRhs->selFlags &= ~SF_MultiValue; |
| 125659 | 125868 | if( yymsp[-1].minor.yy328!=TK_ALL ) pParse->hasCompound = 1; |
| 125660 | 125869 | }else{ |
| 125661 | | - sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy3); |
| 125870 | + sqlite3SelectDelete(pParse->db, pLhs); |
| 125662 | 125871 | } |
| 125663 | 125872 | yygotominor.yy3 = pRhs; |
| 125664 | 125873 | } |
| 125665 | 125874 | break; |
| 125666 | 125875 | case 116: /* multiselect_op ::= UNION ALL */ |
| | @@ -125717,11 +125926,13 @@ |
| 125717 | 125926 | break; |
| 125718 | 125927 | case 122: /* distinct ::= DISTINCT */ |
| 125719 | 125928 | {yygotominor.yy381 = SF_Distinct;} |
| 125720 | 125929 | break; |
| 125721 | 125930 | case 123: /* distinct ::= ALL */ |
| 125722 | | - case 124: /* distinct ::= */ yytestcase(yyruleno==124); |
| 125931 | +{yygotominor.yy381 = SF_All;} |
| 125932 | + break; |
| 125933 | + case 124: /* distinct ::= */ |
| 125723 | 125934 | {yygotominor.yy381 = 0;} |
| 125724 | 125935 | break; |
| 125725 | 125936 | case 125: /* sclp ::= selcollist COMMA */ |
| 125726 | 125937 | case 243: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==243); |
| 125727 | 125938 | {yygotominor.yy14 = yymsp[-1].minor.yy14;} |
| | @@ -126012,11 +126223,11 @@ |
| 126012 | 126223 | if( yymsp[-1].minor.yy14 && yymsp[-1].minor.yy14->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){ |
| 126013 | 126224 | sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0); |
| 126014 | 126225 | } |
| 126015 | 126226 | yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy14, &yymsp[-4].minor.yy0); |
| 126016 | 126227 | spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0); |
| 126017 | | - if( yymsp[-2].minor.yy381 && yygotominor.yy346.pExpr ){ |
| 126228 | + if( yymsp[-2].minor.yy381==SF_Distinct && yygotominor.yy346.pExpr ){ |
| 126018 | 126229 | yygotominor.yy346.pExpr->flags |= EP_Distinct; |
| 126019 | 126230 | } |
| 126020 | 126231 | } |
| 126021 | 126232 | break; |
| 126022 | 126233 | case 196: /* expr ::= ID|INDEXED LP STAR RP */ |
| | @@ -127533,11 +127744,12 @@ |
| 127533 | 127744 | } |
| 127534 | 127745 | } |
| 127535 | 127746 | } |
| 127536 | 127747 | abort_parse: |
| 127537 | 127748 | assert( nErr==0 ); |
| 127538 | | - 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 ); |
| 127539 | 127751 | if( lastTokenParsed!=TK_SEMI ){ |
| 127540 | 127752 | sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); |
| 127541 | 127753 | pParse->zTail = &zSql[i]; |
| 127542 | 127754 | } |
| 127543 | 127755 | if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){ |
| | @@ -127555,11 +127767,11 @@ |
| 127555 | 127767 | db->lookaside.bEnabled = enableLookaside; |
| 127556 | 127768 | if( db->mallocFailed ){ |
| 127557 | 127769 | pParse->rc = SQLITE_NOMEM; |
| 127558 | 127770 | } |
| 127559 | 127771 | if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ |
| 127560 | | - sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc)); |
| 127772 | + pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); |
| 127561 | 127773 | } |
| 127562 | 127774 | assert( pzErrMsg!=0 ); |
| 127563 | 127775 | if( pParse->zErrMsg ){ |
| 127564 | 127776 | *pzErrMsg = pParse->zErrMsg; |
| 127565 | 127777 | sqlite3_log(pParse->rc, "%s", *pzErrMsg); |
| | @@ -130747,10 +130959,13 @@ |
| 130747 | 130959 | | SQLITE_ForeignKeys |
| 130748 | 130960 | #endif |
| 130749 | 130961 | #if defined(SQLITE_REVERSE_UNORDERED_SELECTS) |
| 130750 | 130962 | | SQLITE_ReverseOrder |
| 130751 | 130963 | #endif |
| 130964 | +#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
| 130965 | + | SQLITE_CellSizeCk |
| 130966 | +#endif |
| 130752 | 130967 | ; |
| 130753 | 130968 | sqlite3HashInit(&db->aCollSeq); |
| 130754 | 130969 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 130755 | 130970 | sqlite3HashInit(&db->aModule); |
| 130756 | 130971 | #endif |
| | @@ -130866,12 +131081,11 @@ |
| 130866 | 131081 | } |
| 130867 | 131082 | #endif |
| 130868 | 131083 | |
| 130869 | 131084 | #ifdef SQLITE_ENABLE_DBSTAT_VTAB |
| 130870 | 131085 | if( !db->mallocFailed && rc==SQLITE_OK){ |
| 130871 | | - int sqlite3_dbstat_register(sqlite3*); |
| 130872 | | - rc = sqlite3_dbstat_register(db); |
| 131086 | + rc = sqlite3DbstatRegister(db); |
| 130873 | 131087 | } |
| 130874 | 131088 | #endif |
| 130875 | 131089 | |
| 130876 | 131090 | /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking |
| 130877 | 131091 | ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking |
| | @@ -132871,10 +133085,12 @@ |
| 132871 | 133085 | typedef struct Fts3SegFilter Fts3SegFilter; |
| 132872 | 133086 | typedef struct Fts3DeferredToken Fts3DeferredToken; |
| 132873 | 133087 | typedef struct Fts3SegReader Fts3SegReader; |
| 132874 | 133088 | typedef struct Fts3MultiSegReader Fts3MultiSegReader; |
| 132875 | 133089 | |
| 133090 | +typedef struct MatchinfoBuffer MatchinfoBuffer; |
| 133091 | + |
| 132876 | 133092 | /* |
| 132877 | 133093 | ** A connection to a fulltext index is an instance of the following |
| 132878 | 133094 | ** structure. The xCreate and xConnect methods create an instance |
| 132879 | 133095 | ** of this structure and xDestroy and xDisconnect free that instance. |
| 132880 | 133096 | ** All other methods receive a pointer to the structure as one of their |
| | @@ -132980,13 +133196,11 @@ |
| 132980 | 133196 | int nRowAvg; /* Average size of database rows, in pages */ |
| 132981 | 133197 | sqlite3_int64 nDoc; /* Documents in table */ |
| 132982 | 133198 | i64 iMinDocid; /* Minimum docid to return */ |
| 132983 | 133199 | i64 iMaxDocid; /* Maximum docid to return */ |
| 132984 | 133200 | int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */ |
| 132985 | | - u32 *aMatchinfo; /* Information about most recent match */ |
| 132986 | | - int nMatchinfo; /* Number of elements in aMatchinfo[] */ |
| 132987 | | - char *zMatchinfo; /* Matchinfo specification */ |
| 133201 | + MatchinfoBuffer *pMIBuffer; /* Buffer for matchinfo data */ |
| 132988 | 133202 | }; |
| 132989 | 133203 | |
| 132990 | 133204 | #define FTS3_EVAL_FILTER 0 |
| 132991 | 133205 | #define FTS3_EVAL_NEXT 1 |
| 132992 | 133206 | #define FTS3_EVAL_MATCHINFO 2 |
| | @@ -133102,11 +133316,13 @@ |
| 133102 | 133316 | sqlite3_int64 iDocid; /* Current docid */ |
| 133103 | 133317 | u8 bEof; /* True this expression is at EOF already */ |
| 133104 | 133318 | u8 bStart; /* True if iDocid is valid */ |
| 133105 | 133319 | u8 bDeferred; /* True if this expression is entirely deferred */ |
| 133106 | 133320 | |
| 133107 | | - 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 */ |
| 133108 | 133324 | }; |
| 133109 | 133325 | |
| 133110 | 133326 | /* |
| 133111 | 133327 | ** Candidate values for Fts3Query.eType. Note that the order of the first |
| 133112 | 133328 | ** four values is in order of precedence when parsing expressions. For |
| | @@ -133223,10 +133439,11 @@ |
| 133223 | 133439 | SQLITE_PRIVATE void sqlite3Fts3Dequote(char *); |
| 133224 | 133440 | SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*); |
| 133225 | 133441 | SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *); |
| 133226 | 133442 | SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *); |
| 133227 | 133443 | SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*); |
| 133444 | +SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc); |
| 133228 | 133445 | |
| 133229 | 133446 | /* fts3_tokenizer.c */ |
| 133230 | 133447 | SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *); |
| 133231 | 133448 | SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *); |
| 133232 | 133449 | SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, |
| | @@ -133238,10 +133455,11 @@ |
| 133238 | 133455 | SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*); |
| 133239 | 133456 | SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *, |
| 133240 | 133457 | const char *, const char *, int, int |
| 133241 | 133458 | ); |
| 133242 | 133459 | SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *); |
| 133460 | +SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p); |
| 133243 | 133461 | |
| 133244 | 133462 | /* fts3_expr.c */ |
| 133245 | 133463 | SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int, |
| 133246 | 133464 | char **, int, int, int, const char *, int, Fts3Expr **, char ** |
| 133247 | 133465 | ); |
| | @@ -134665,11 +134883,11 @@ |
| 134665 | 134883 | assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); |
| 134666 | 134884 | sqlite3_finalize(pCsr->pStmt); |
| 134667 | 134885 | sqlite3Fts3ExprFree(pCsr->pExpr); |
| 134668 | 134886 | sqlite3Fts3FreeDeferredTokens(pCsr); |
| 134669 | 134887 | sqlite3_free(pCsr->aDoclist); |
| 134670 | | - sqlite3_free(pCsr->aMatchinfo); |
| 134888 | + sqlite3Fts3MIBufferFree(pCsr->pMIBuffer); |
| 134671 | 134889 | assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); |
| 134672 | 134890 | sqlite3_free(pCsr); |
| 134673 | 134891 | return SQLITE_OK; |
| 134674 | 134892 | } |
| 134675 | 134893 | |
| | @@ -136166,11 +136384,11 @@ |
| 136166 | 136384 | assert( iIdx==nVal ); |
| 136167 | 136385 | |
| 136168 | 136386 | /* In case the cursor has been used before, clear it now. */ |
| 136169 | 136387 | sqlite3_finalize(pCsr->pStmt); |
| 136170 | 136388 | sqlite3_free(pCsr->aDoclist); |
| 136171 | | - sqlite3_free(pCsr->aMatchinfo); |
| 136389 | + sqlite3Fts3MIBufferFree(pCsr->pMIBuffer); |
| 136172 | 136390 | sqlite3Fts3ExprFree(pCsr->pExpr); |
| 136173 | 136391 | memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor)); |
| 136174 | 136392 | |
| 136175 | 136393 | /* Set the lower and upper bounds on docids to return */ |
| 136176 | 136394 | pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64); |
| | @@ -138064,11 +138282,11 @@ |
| 138064 | 138282 | ** is populated as for "A * C" before returning. |
| 138065 | 138283 | ** |
| 138066 | 138284 | ** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is |
| 138067 | 138285 | ** advanced to point to the next row that matches "x AND y". |
| 138068 | 138286 | ** |
| 138069 | | -** See fts3EvalTestDeferredAndNear() for details on testing if a row is |
| 138287 | +** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is |
| 138070 | 138288 | ** really a match, taking into account deferred tokens and NEAR operators. |
| 138071 | 138289 | */ |
| 138072 | 138290 | static void fts3EvalNextRow( |
| 138073 | 138291 | Fts3Cursor *pCsr, /* FTS Cursor handle */ |
| 138074 | 138292 | Fts3Expr *pExpr, /* Expr. to advance to next matching row */ |
| | @@ -138284,11 +138502,11 @@ |
| 138284 | 138502 | |
| 138285 | 138503 | return res; |
| 138286 | 138504 | } |
| 138287 | 138505 | |
| 138288 | 138506 | /* |
| 138289 | | -** This function is a helper function for fts3EvalTestDeferredAndNear(). |
| 138507 | +** This function is a helper function for sqlite3Fts3EvalTestDeferred(). |
| 138290 | 138508 | ** Assuming no error occurs or has occurred, It returns non-zero if the |
| 138291 | 138509 | ** expression passed as the second argument matches the row that pCsr |
| 138292 | 138510 | ** currently points to, or zero if it does not. |
| 138293 | 138511 | ** |
| 138294 | 138512 | ** If *pRc is not SQLITE_OK when this function is called, it is a no-op. |
| | @@ -138405,11 +138623,11 @@ |
| 138405 | 138623 | ** it is determined that the row does *not* match the query. |
| 138406 | 138624 | ** |
| 138407 | 138625 | ** Or, if no error occurs and it seems the current row does match the FTS |
| 138408 | 138626 | ** query, return 0. |
| 138409 | 138627 | */ |
| 138410 | | -static int fts3EvalTestDeferredAndNear(Fts3Cursor *pCsr, int *pRc){ |
| 138628 | +SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){ |
| 138411 | 138629 | int rc = *pRc; |
| 138412 | 138630 | int bMiss = 0; |
| 138413 | 138631 | if( rc==SQLITE_OK ){ |
| 138414 | 138632 | |
| 138415 | 138633 | /* If there are one or more deferred tokens, load the current row into |
| | @@ -138452,11 +138670,11 @@ |
| 138452 | 138670 | fts3EvalNextRow(pCsr, pExpr, &rc); |
| 138453 | 138671 | pCsr->isEof = pExpr->bEof; |
| 138454 | 138672 | pCsr->isRequireSeek = 1; |
| 138455 | 138673 | pCsr->isMatchinfoNeeded = 1; |
| 138456 | 138674 | pCsr->iPrevId = pExpr->iDocid; |
| 138457 | | - }while( pCsr->isEof==0 && fts3EvalTestDeferredAndNear(pCsr, &rc) ); |
| 138675 | + }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) ); |
| 138458 | 138676 | } |
| 138459 | 138677 | |
| 138460 | 138678 | /* Check if the cursor is past the end of the docid range specified |
| 138461 | 138679 | ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */ |
| 138462 | 138680 | if( rc==SQLITE_OK && ( |
| | @@ -138613,11 +138831,11 @@ |
| 138613 | 138831 | pCsr->isRequireSeek = 1; |
| 138614 | 138832 | pCsr->isMatchinfoNeeded = 1; |
| 138615 | 138833 | pCsr->iPrevId = pRoot->iDocid; |
| 138616 | 138834 | }while( pCsr->isEof==0 |
| 138617 | 138835 | && pRoot->eType==FTSQUERY_NEAR |
| 138618 | | - && fts3EvalTestDeferredAndNear(pCsr, &rc) |
| 138836 | + && sqlite3Fts3EvalTestDeferred(pCsr, &rc) |
| 138619 | 138837 | ); |
| 138620 | 138838 | |
| 138621 | 138839 | if( rc==SQLITE_OK && pCsr->isEof==0 ){ |
| 138622 | 138840 | fts3EvalUpdateCounts(pRoot); |
| 138623 | 138841 | } |
| | @@ -138638,11 +138856,10 @@ |
| 138638 | 138856 | fts3EvalRestart(pCsr, pRoot, &rc); |
| 138639 | 138857 | do { |
| 138640 | 138858 | fts3EvalNextRow(pCsr, pRoot, &rc); |
| 138641 | 138859 | assert( pRoot->bEof==0 ); |
| 138642 | 138860 | }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK ); |
| 138643 | | - fts3EvalTestDeferredAndNear(pCsr, &rc); |
| 138644 | 138861 | } |
| 138645 | 138862 | } |
| 138646 | 138863 | return rc; |
| 138647 | 138864 | } |
| 138648 | 138865 | |
| | @@ -148650,10 +148867,11 @@ |
| 148650 | 148867 | #define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */ |
| 148651 | 148868 | #define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */ |
| 148652 | 148869 | #define FTS3_MATCHINFO_LCS 's' /* nCol values */ |
| 148653 | 148870 | #define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */ |
| 148654 | 148871 | #define FTS3_MATCHINFO_LHITS 'y' /* nCol*nPhrase values */ |
| 148872 | +#define FTS3_MATCHINFO_LHITS_BM 'b' /* nCol*nPhrase values */ |
| 148655 | 148873 | |
| 148656 | 148874 | /* |
| 148657 | 148875 | ** The default value for the second argument to matchinfo(). |
| 148658 | 148876 | */ |
| 148659 | 148877 | #define FTS3_MATCHINFO_DEFAULT "pcx" |
| | @@ -148711,13 +148929,26 @@ |
| 148711 | 148929 | struct MatchInfo { |
| 148712 | 148930 | Fts3Cursor *pCursor; /* FTS3 Cursor */ |
| 148713 | 148931 | int nCol; /* Number of columns in table */ |
| 148714 | 148932 | int nPhrase; /* Number of matchable phrases in query */ |
| 148715 | 148933 | sqlite3_int64 nDoc; /* Number of docs in database */ |
| 148934 | + char flag; |
| 148716 | 148935 | u32 *aMatchinfo; /* Pre-allocated buffer */ |
| 148717 | 148936 | }; |
| 148718 | 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 | +}; |
| 148719 | 148950 | |
| 148720 | 148951 | |
| 148721 | 148952 | /* |
| 148722 | 148953 | ** The snippet() and offsets() functions both return text values. An instance |
| 148723 | 148954 | ** of the following structure is used to accumulate those values while the |
| | @@ -148728,10 +148959,101 @@ |
| 148728 | 148959 | char *z; /* Pointer to buffer containing string */ |
| 148729 | 148960 | int n; /* Length of z in bytes (excl. nul-term) */ |
| 148730 | 148961 | int nAlloc; /* Allocated size of buffer z in bytes */ |
| 148731 | 148962 | }; |
| 148732 | 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 | + |
| 148733 | 149055 | |
| 148734 | 149056 | /* |
| 148735 | 149057 | ** This function is used to help iterate through a position-list. A position |
| 148736 | 149058 | ** list is a list of unique integers, sorted from smallest to largest. Each |
| 148737 | 149059 | ** element of the list is represented by an FTS3 varint that takes the value |
| | @@ -148765,11 +149087,11 @@ |
| 148765 | 149087 | int *piPhrase, /* Pointer to phrase counter */ |
| 148766 | 149088 | int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */ |
| 148767 | 149089 | void *pCtx /* Second argument to pass to callback */ |
| 148768 | 149090 | ){ |
| 148769 | 149091 | int rc; /* Return code */ |
| 148770 | | - int eType = pExpr->eType; /* Type of expression node pExpr */ |
| 149092 | + int eType = pExpr->eType; /* Type of expression node pExpr */ |
| 148771 | 149093 | |
| 148772 | 149094 | if( eType!=FTSQUERY_PHRASE ){ |
| 148773 | 149095 | assert( pExpr->pLeft && pExpr->pRight ); |
| 148774 | 149096 | rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx); |
| 148775 | 149097 | if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){ |
| | @@ -148798,10 +149120,11 @@ |
| 148798 | 149120 | void *pCtx /* Second argument to pass to callback */ |
| 148799 | 149121 | ){ |
| 148800 | 149122 | int iPhrase = 0; /* Variable used as the phrase counter */ |
| 148801 | 149123 | return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx); |
| 148802 | 149124 | } |
| 149125 | + |
| 148803 | 149126 | |
| 148804 | 149127 | /* |
| 148805 | 149128 | ** This is an fts3ExprIterate() callback used while loading the doclists |
| 148806 | 149129 | ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also |
| 148807 | 149130 | ** fts3ExprLoadDoclists(). |
| | @@ -148843,12 +149166,11 @@ |
| 148843 | 149166 | return rc; |
| 148844 | 149167 | } |
| 148845 | 149168 | |
| 148846 | 149169 | static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){ |
| 148847 | 149170 | (*(int *)ctx)++; |
| 148848 | | - UNUSED_PARAMETER(pExpr); |
| 148849 | | - UNUSED_PARAMETER(iPhrase); |
| 149171 | + pExpr->iPhrase = iPhrase; |
| 148850 | 149172 | return SQLITE_OK; |
| 148851 | 149173 | } |
| 148852 | 149174 | static int fts3ExprPhraseCount(Fts3Expr *pExpr){ |
| 148853 | 149175 | int nPhrase = 0; |
| 148854 | 149176 | (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase); |
| | @@ -149065,11 +149387,11 @@ |
| 149065 | 149387 | sIter.pCsr = pCsr; |
| 149066 | 149388 | sIter.iCol = iCol; |
| 149067 | 149389 | sIter.nSnippet = nSnippet; |
| 149068 | 149390 | sIter.nPhrase = nList; |
| 149069 | 149391 | sIter.iCurrent = -1; |
| 149070 | | - rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter); |
| 149392 | + rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter); |
| 149071 | 149393 | if( rc==SQLITE_OK ){ |
| 149072 | 149394 | |
| 149073 | 149395 | /* Set the *pmSeen output variable. */ |
| 149074 | 149396 | for(i=0; i<nList; i++){ |
| 149075 | 149397 | if( sIter.aPhrase[i].pHead ){ |
| | @@ -149365,10 +149687,64 @@ |
| 149365 | 149687 | } |
| 149366 | 149688 | |
| 149367 | 149689 | *ppCollist = pEnd; |
| 149368 | 149690 | return nEntry; |
| 149369 | 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 | +} |
| 149370 | 149746 | |
| 149371 | 149747 | /* |
| 149372 | 149748 | ** fts3ExprIterate() callback used to collect the "global" matchinfo stats |
| 149373 | 149749 | ** for a single query. |
| 149374 | 149750 | ** |
| | @@ -149432,55 +149808,10 @@ |
| 149432 | 149808 | } |
| 149433 | 149809 | |
| 149434 | 149810 | return rc; |
| 149435 | 149811 | } |
| 149436 | 149812 | |
| 149437 | | -/* |
| 149438 | | -** fts3ExprIterate() callback used to gather information for the matchinfo |
| 149439 | | -** directive 'y'. |
| 149440 | | -*/ |
| 149441 | | -static int fts3ExprLHitsCb( |
| 149442 | | - Fts3Expr *pExpr, /* Phrase expression node */ |
| 149443 | | - int iPhrase, /* Phrase number */ |
| 149444 | | - void *pCtx /* Pointer to MatchInfo structure */ |
| 149445 | | -){ |
| 149446 | | - MatchInfo *p = (MatchInfo *)pCtx; |
| 149447 | | - Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab; |
| 149448 | | - int rc = SQLITE_OK; |
| 149449 | | - int iStart = iPhrase * p->nCol; |
| 149450 | | - Fts3Expr *pEof; /* Ancestor node already at EOF */ |
| 149451 | | - |
| 149452 | | - /* This must be a phrase */ |
| 149453 | | - assert( pExpr->pPhrase ); |
| 149454 | | - |
| 149455 | | - /* Initialize all output integers to zero. */ |
| 149456 | | - memset(&p->aMatchinfo[iStart], 0, sizeof(u32) * p->nCol); |
| 149457 | | - |
| 149458 | | - /* Check if this or any parent node is at EOF. If so, then all output |
| 149459 | | - ** values are zero. */ |
| 149460 | | - for(pEof=pExpr; pEof && pEof->bEof==0; pEof=pEof->pParent); |
| 149461 | | - |
| 149462 | | - if( pEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){ |
| 149463 | | - Fts3Phrase *pPhrase = pExpr->pPhrase; |
| 149464 | | - char *pIter = pPhrase->doclist.pList; |
| 149465 | | - int iCol = 0; |
| 149466 | | - |
| 149467 | | - while( 1 ){ |
| 149468 | | - int nHit = fts3ColumnlistCount(&pIter); |
| 149469 | | - if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){ |
| 149470 | | - p->aMatchinfo[iStart + iCol] = (u32)nHit; |
| 149471 | | - } |
| 149472 | | - assert( *pIter==0x00 || *pIter==0x01 ); |
| 149473 | | - if( *pIter!=0x01 ) break; |
| 149474 | | - pIter++; |
| 149475 | | - pIter += fts3GetVarint32(pIter, &iCol); |
| 149476 | | - } |
| 149477 | | - } |
| 149478 | | - |
| 149479 | | - return rc; |
| 149480 | | -} |
| 149481 | | - |
| 149482 | 149813 | static int fts3MatchinfoCheck( |
| 149483 | 149814 | Fts3Table *pTab, |
| 149484 | 149815 | char cArg, |
| 149485 | 149816 | char **pzErr |
| 149486 | 149817 | ){ |
| | @@ -149490,10 +149821,11 @@ |
| 149490 | 149821 | || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4) |
| 149491 | 149822 | || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize) |
| 149492 | 149823 | || (cArg==FTS3_MATCHINFO_LCS) |
| 149493 | 149824 | || (cArg==FTS3_MATCHINFO_HITS) |
| 149494 | 149825 | || (cArg==FTS3_MATCHINFO_LHITS) |
| 149826 | + || (cArg==FTS3_MATCHINFO_LHITS_BM) |
| 149495 | 149827 | ){ |
| 149496 | 149828 | return SQLITE_OK; |
| 149497 | 149829 | } |
| 149498 | 149830 | sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg); |
| 149499 | 149831 | return SQLITE_ERROR; |
| | @@ -149516,10 +149848,14 @@ |
| 149516 | 149848 | break; |
| 149517 | 149849 | |
| 149518 | 149850 | case FTS3_MATCHINFO_LHITS: |
| 149519 | 149851 | nVal = pInfo->nCol * pInfo->nPhrase; |
| 149520 | 149852 | break; |
| 149853 | + |
| 149854 | + case FTS3_MATCHINFO_LHITS_BM: |
| 149855 | + nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32); |
| 149856 | + break; |
| 149521 | 149857 | |
| 149522 | 149858 | default: |
| 149523 | 149859 | assert( cArg==FTS3_MATCHINFO_HITS ); |
| 149524 | 149860 | nVal = pInfo->nCol * pInfo->nPhrase * 3; |
| 149525 | 149861 | break; |
| | @@ -149711,11 +150047,11 @@ |
| 149711 | 150047 | int i; |
| 149712 | 150048 | Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 149713 | 150049 | sqlite3_stmt *pSelect = 0; |
| 149714 | 150050 | |
| 149715 | 150051 | for(i=0; rc==SQLITE_OK && zArg[i]; i++){ |
| 149716 | | - |
| 150052 | + pInfo->flag = zArg[i]; |
| 149717 | 150053 | switch( zArg[i] ){ |
| 149718 | 150054 | case FTS3_MATCHINFO_NPHRASE: |
| 149719 | 150055 | if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase; |
| 149720 | 150056 | break; |
| 149721 | 150057 | |
| | @@ -149771,13 +150107,17 @@ |
| 149771 | 150107 | if( rc==SQLITE_OK ){ |
| 149772 | 150108 | rc = fts3MatchinfoLcs(pCsr, pInfo); |
| 149773 | 150109 | } |
| 149774 | 150110 | break; |
| 149775 | 150111 | |
| 149776 | | - case FTS3_MATCHINFO_LHITS: |
| 149777 | | - (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); |
| 149778 | 150117 | break; |
| 150118 | + } |
| 149779 | 150119 | |
| 149780 | 150120 | default: { |
| 149781 | 150121 | Fts3Expr *pExpr; |
| 149782 | 150122 | assert( zArg[i]==FTS3_MATCHINFO_HITS ); |
| 149783 | 150123 | pExpr = pCsr->pExpr; |
| | @@ -149787,10 +150127,11 @@ |
| 149787 | 150127 | if( pCsr->pDeferred ){ |
| 149788 | 150128 | rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0); |
| 149789 | 150129 | if( rc!=SQLITE_OK ) break; |
| 149790 | 150130 | } |
| 149791 | 150131 | rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo); |
| 150132 | + sqlite3Fts3EvalTestDeferred(pCsr, &rc); |
| 149792 | 150133 | if( rc!=SQLITE_OK ) break; |
| 149793 | 150134 | } |
| 149794 | 150135 | (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo); |
| 149795 | 150136 | break; |
| 149796 | 150137 | } |
| | @@ -149806,73 +150147,90 @@ |
| 149806 | 150147 | |
| 149807 | 150148 | /* |
| 149808 | 150149 | ** Populate pCsr->aMatchinfo[] with data for the current row. The |
| 149809 | 150150 | ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32). |
| 149810 | 150151 | */ |
| 149811 | | -static int fts3GetMatchinfo( |
| 150152 | +static void fts3GetMatchinfo( |
| 150153 | + sqlite3_context *pCtx, /* Return results here */ |
| 149812 | 150154 | Fts3Cursor *pCsr, /* FTS3 Cursor object */ |
| 149813 | 150155 | const char *zArg /* Second argument to matchinfo() function */ |
| 149814 | 150156 | ){ |
| 149815 | 150157 | MatchInfo sInfo; |
| 149816 | 150158 | Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 149817 | 150159 | int rc = SQLITE_OK; |
| 149818 | 150160 | int bGlobal = 0; /* Collect 'global' stats as well as local */ |
| 149819 | 150161 | |
| 150162 | + u32 *aOut = 0; |
| 150163 | + void (*xDestroyOut)(void*) = 0; |
| 150164 | + |
| 149820 | 150165 | memset(&sInfo, 0, sizeof(MatchInfo)); |
| 149821 | 150166 | sInfo.pCursor = pCsr; |
| 149822 | 150167 | sInfo.nCol = pTab->nColumn; |
| 149823 | 150168 | |
| 149824 | 150169 | /* If there is cached matchinfo() data, but the format string for the |
| 149825 | 150170 | ** cache does not match the format string for this request, discard |
| 149826 | 150171 | ** the cached data. */ |
| 149827 | | - if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){ |
| 149828 | | - assert( pCsr->aMatchinfo ); |
| 149829 | | - sqlite3_free(pCsr->aMatchinfo); |
| 149830 | | - pCsr->zMatchinfo = 0; |
| 149831 | | - pCsr->aMatchinfo = 0; |
| 150172 | + if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){ |
| 150173 | + sqlite3Fts3MIBufferFree(pCsr->pMIBuffer); |
| 150174 | + pCsr->pMIBuffer = 0; |
| 149832 | 150175 | } |
| 149833 | 150176 | |
| 149834 | | - /* 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 |
| 149835 | 150178 | ** matchinfo function has been called for this query. In this case |
| 149836 | 150179 | ** allocate the array used to accumulate the matchinfo data and |
| 149837 | 150180 | ** initialize those elements that are constant for every row. |
| 149838 | 150181 | */ |
| 149839 | | - if( pCsr->aMatchinfo==0 ){ |
| 150182 | + if( pCsr->pMIBuffer==0 ){ |
| 149840 | 150183 | int nMatchinfo = 0; /* Number of u32 elements in match-info */ |
| 149841 | | - int nArg; /* Bytes in zArg */ |
| 149842 | 150184 | int i; /* Used to iterate through zArg */ |
| 149843 | 150185 | |
| 149844 | 150186 | /* Determine the number of phrases in the query */ |
| 149845 | 150187 | pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr); |
| 149846 | 150188 | sInfo.nPhrase = pCsr->nPhrase; |
| 149847 | 150189 | |
| 149848 | 150190 | /* Determine the number of integers in the buffer returned by this call. */ |
| 149849 | 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 | + } |
| 149850 | 150198 | nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]); |
| 149851 | 150199 | } |
| 149852 | 150200 | |
| 149853 | 150201 | /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */ |
| 149854 | | - nArg = (int)strlen(zArg); |
| 149855 | | - pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1); |
| 149856 | | - if( !pCsr->aMatchinfo ) return SQLITE_NOMEM; |
| 149857 | | - |
| 149858 | | - pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo]; |
| 149859 | | - pCsr->nMatchinfo = nMatchinfo; |
| 149860 | | - memcpy(pCsr->zMatchinfo, zArg, nArg+1); |
| 149861 | | - memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo); |
| 150202 | + pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg); |
| 150203 | + if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM; |
| 150204 | + |
| 149862 | 150205 | pCsr->isMatchinfoNeeded = 1; |
| 149863 | 150206 | bGlobal = 1; |
| 149864 | 150207 | } |
| 149865 | 150208 | |
| 149866 | | - sInfo.aMatchinfo = pCsr->aMatchinfo; |
| 149867 | | - sInfo.nPhrase = pCsr->nPhrase; |
| 149868 | | - 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; |
| 149869 | 150219 | rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg); |
| 149870 | | - pCsr->isMatchinfoNeeded = 0; |
| 150220 | + if( bGlobal ){ |
| 150221 | + fts3MIBufferSetGlobal(pCsr->pMIBuffer); |
| 150222 | + } |
| 149871 | 150223 | } |
| 149872 | 150224 | |
| 149873 | | - 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 | + } |
| 149874 | 150232 | } |
| 149875 | 150233 | |
| 149876 | 150234 | /* |
| 149877 | 150235 | ** Implementation of snippet() function. |
| 149878 | 150236 | */ |
| | @@ -150074,11 +150432,11 @@ |
| 150074 | 150432 | ** no way that this operation can fail, so the return code from |
| 150075 | 150433 | ** fts3ExprIterate() can be discarded. |
| 150076 | 150434 | */ |
| 150077 | 150435 | sCtx.iCol = iCol; |
| 150078 | 150436 | sCtx.iTerm = 0; |
| 150079 | | - (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx); |
| 150437 | + (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx); |
| 150080 | 150438 | |
| 150081 | 150439 | /* Retreive the text stored in column iCol. If an SQL NULL is stored |
| 150082 | 150440 | ** in column iCol, jump immediately to the next iteration of the loop. |
| 150083 | 150441 | ** If an OOM occurs while retrieving the data (this can happen if SQLite |
| 150084 | 150442 | ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM |
| | @@ -150166,42 +150524,25 @@ |
| 150166 | 150524 | sqlite3_context *pContext, /* Function call context */ |
| 150167 | 150525 | Fts3Cursor *pCsr, /* FTS3 table cursor */ |
| 150168 | 150526 | const char *zArg /* Second arg to matchinfo() function */ |
| 150169 | 150527 | ){ |
| 150170 | 150528 | Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; |
| 150171 | | - int rc; |
| 150172 | | - int i; |
| 150173 | 150529 | const char *zFormat; |
| 150174 | 150530 | |
| 150175 | 150531 | if( zArg ){ |
| 150176 | | - for(i=0; zArg[i]; i++){ |
| 150177 | | - char *zErr = 0; |
| 150178 | | - if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){ |
| 150179 | | - sqlite3_result_error(pContext, zErr, -1); |
| 150180 | | - sqlite3_free(zErr); |
| 150181 | | - return; |
| 150182 | | - } |
| 150183 | | - } |
| 150184 | 150532 | zFormat = zArg; |
| 150185 | 150533 | }else{ |
| 150186 | 150534 | zFormat = FTS3_MATCHINFO_DEFAULT; |
| 150187 | 150535 | } |
| 150188 | 150536 | |
| 150189 | 150537 | if( !pCsr->pExpr ){ |
| 150190 | 150538 | sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC); |
| 150191 | 150539 | return; |
| 150192 | | - } |
| 150193 | | - |
| 150194 | | - /* Retrieve matchinfo() data. */ |
| 150195 | | - rc = fts3GetMatchinfo(pCsr, zFormat); |
| 150196 | | - sqlite3Fts3SegmentsClose(pTab); |
| 150197 | | - |
| 150198 | | - if( rc!=SQLITE_OK ){ |
| 150199 | | - sqlite3_result_error_code(pContext, rc); |
| 150200 | 150540 | }else{ |
| 150201 | | - int n = pCsr->nMatchinfo * sizeof(u32); |
| 150202 | | - sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT); |
| 150541 | + /* Retrieve matchinfo() data. */ |
| 150542 | + fts3GetMatchinfo(pContext, pCsr, zFormat); |
| 150543 | + sqlite3Fts3SegmentsClose(pTab); |
| 150203 | 150544 | } |
| 150204 | 150545 | } |
| 150205 | 150546 | |
| 150206 | 150547 | #endif |
| 150207 | 150548 | |
| | @@ -151318,10 +151659,11 @@ |
| 151318 | 151659 | */ |
| 151319 | 151660 | struct RtreeMatchArg { |
| 151320 | 151661 | u32 magic; /* Always RTREE_GEOMETRY_MAGIC */ |
| 151321 | 151662 | RtreeGeomCallback cb; /* Info about the callback functions */ |
| 151322 | 151663 | int nParam; /* Number of parameters to the SQL function */ |
| 151664 | + sqlite3_value **apSqlParam; /* Original SQL parameter values */ |
| 151323 | 151665 | RtreeDValue aParam[1]; /* Values for parameters to the SQL function */ |
| 151324 | 151666 | }; |
| 151325 | 151667 | |
| 151326 | 151668 | #ifndef MAX |
| 151327 | 151669 | # define MAX(x,y) ((x) < (y) ? (y) : (x)) |
| | @@ -152449,13 +152791,11 @@ |
| 152449 | 152791 | /* Check that value is actually a blob. */ |
| 152450 | 152792 | if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR; |
| 152451 | 152793 | |
| 152452 | 152794 | /* Check that the blob is roughly the right size. */ |
| 152453 | 152795 | nBlob = sqlite3_value_bytes(pValue); |
| 152454 | | - if( nBlob<(int)sizeof(RtreeMatchArg) |
| 152455 | | - || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0 |
| 152456 | | - ){ |
| 152796 | + if( nBlob<(int)sizeof(RtreeMatchArg) ){ |
| 152457 | 152797 | return SQLITE_ERROR; |
| 152458 | 152798 | } |
| 152459 | 152799 | |
| 152460 | 152800 | pInfo = (sqlite3_rtree_query_info*)sqlite3_malloc( sizeof(*pInfo)+nBlob ); |
| 152461 | 152801 | if( !pInfo ) return SQLITE_NOMEM; |
| | @@ -152462,18 +152802,20 @@ |
| 152462 | 152802 | memset(pInfo, 0, sizeof(*pInfo)); |
| 152463 | 152803 | pBlob = (RtreeMatchArg*)&pInfo[1]; |
| 152464 | 152804 | |
| 152465 | 152805 | memcpy(pBlob, sqlite3_value_blob(pValue), nBlob); |
| 152466 | 152806 | nExpected = (int)(sizeof(RtreeMatchArg) + |
| 152807 | + pBlob->nParam*sizeof(sqlite3_value*) + |
| 152467 | 152808 | (pBlob->nParam-1)*sizeof(RtreeDValue)); |
| 152468 | 152809 | if( pBlob->magic!=RTREE_GEOMETRY_MAGIC || nBlob!=nExpected ){ |
| 152469 | 152810 | sqlite3_free(pInfo); |
| 152470 | 152811 | return SQLITE_ERROR; |
| 152471 | 152812 | } |
| 152472 | 152813 | pInfo->pContext = pBlob->cb.pContext; |
| 152473 | 152814 | pInfo->nParam = pBlob->nParam; |
| 152474 | 152815 | pInfo->aParam = pBlob->aParam; |
| 152816 | + pInfo->apSqlParam = pBlob->apSqlParam; |
| 152475 | 152817 | |
| 152476 | 152818 | if( pBlob->cb.xGeom ){ |
| 152477 | 152819 | pCons->u.xGeom = pBlob->cb.xGeom; |
| 152478 | 152820 | }else{ |
| 152479 | 152821 | pCons->op = RTREE_QUERY; |
| | @@ -152636,21 +152978,34 @@ |
| 152636 | 152978 | */ |
| 152637 | 152979 | static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
| 152638 | 152980 | Rtree *pRtree = (Rtree*)tab; |
| 152639 | 152981 | int rc = SQLITE_OK; |
| 152640 | 152982 | int ii; |
| 152983 | + int bMatch = 0; /* True if there exists a MATCH constraint */ |
| 152641 | 152984 | i64 nRow; /* Estimated rows returned by this scan */ |
| 152642 | 152985 | |
| 152643 | 152986 | int iIdx = 0; |
| 152644 | 152987 | char zIdxStr[RTREE_MAX_DIMENSIONS*8+1]; |
| 152645 | 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 | + } |
| 152646 | 152999 | |
| 152647 | 153000 | assert( pIdxInfo->idxStr==0 ); |
| 152648 | 153001 | for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){ |
| 152649 | 153002 | struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii]; |
| 152650 | 153003 | |
| 152651 | | - 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 | + ){ |
| 152652 | 153007 | /* We have an equality constraint on the rowid. Use strategy 1. */ |
| 152653 | 153008 | int jj; |
| 152654 | 153009 | for(jj=0; jj<ii; jj++){ |
| 152655 | 153010 | pIdxInfo->aConstraintUsage[jj].argvIndex = 0; |
| 152656 | 153011 | pIdxInfo->aConstraintUsage[jj].omit = 0; |
| | @@ -154338,10 +154693,22 @@ |
| 154338 | 154693 | static void rtreeFreeCallback(void *p){ |
| 154339 | 154694 | RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p; |
| 154340 | 154695 | if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext); |
| 154341 | 154696 | sqlite3_free(p); |
| 154342 | 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 | +} |
| 154343 | 154710 | |
| 154344 | 154711 | /* |
| 154345 | 154712 | ** Each call to sqlite3_rtree_geometry_callback() or |
| 154346 | 154713 | ** sqlite3_rtree_query_callback() creates an ordinary SQLite |
| 154347 | 154714 | ** scalar function that is implemented by this routine. |
| | @@ -154357,28 +154724,38 @@ |
| 154357 | 154724 | */ |
| 154358 | 154725 | static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){ |
| 154359 | 154726 | RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx); |
| 154360 | 154727 | RtreeMatchArg *pBlob; |
| 154361 | 154728 | int nBlob; |
| 154729 | + int memErr = 0; |
| 154362 | 154730 | |
| 154363 | | - nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue); |
| 154731 | + nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue) |
| 154732 | + + nArg*sizeof(sqlite3_value*); |
| 154364 | 154733 | pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob); |
| 154365 | 154734 | if( !pBlob ){ |
| 154366 | 154735 | sqlite3_result_error_nomem(ctx); |
| 154367 | 154736 | }else{ |
| 154368 | 154737 | int i; |
| 154369 | 154738 | pBlob->magic = RTREE_GEOMETRY_MAGIC; |
| 154370 | 154739 | pBlob->cb = pGeomCtx[0]; |
| 154740 | + pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg]; |
| 154371 | 154741 | pBlob->nParam = nArg; |
| 154372 | 154742 | for(i=0; i<nArg; i++){ |
| 154743 | + pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]); |
| 154744 | + if( pBlob->apSqlParam[i]==0 ) memErr = 1; |
| 154373 | 154745 | #ifdef SQLITE_RTREE_INT_ONLY |
| 154374 | 154746 | pBlob->aParam[i] = sqlite3_value_int64(aArg[i]); |
| 154375 | 154747 | #else |
| 154376 | 154748 | pBlob->aParam[i] = sqlite3_value_double(aArg[i]); |
| 154377 | 154749 | #endif |
| 154378 | 154750 | } |
| 154379 | | - 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 | + } |
| 154380 | 154757 | } |
| 154381 | 154758 | } |
| 154382 | 154759 | |
| 154383 | 154760 | /* |
| 154384 | 154761 | ** Register a new geometry function for use with the r-tree MATCH operator. |
| | @@ -155210,10 +155587,4095 @@ |
| 155210 | 155587 | |
| 155211 | 155588 | #endif /* defined(SQLITE_ENABLE_ICU) */ |
| 155212 | 155589 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ |
| 155213 | 155590 | |
| 155214 | 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 ******************************************/ |
| 155215 | 159677 | /************** Begin file dbstat.c ******************************************/ |
| 155216 | 159678 | /* |
| 155217 | 159679 | ** 2010 July 12 |
| 155218 | 159680 | ** |
| 155219 | 159681 | ** The author disclaims copyright to this source code. In place of |
| | @@ -155833,11 +160295,11 @@ |
| 155833 | 160295 | } |
| 155834 | 160296 | |
| 155835 | 160297 | /* |
| 155836 | 160298 | ** Invoke this routine to register the "dbstat" virtual table module |
| 155837 | 160299 | */ |
| 155838 | | -SQLITE_API int SQLITE_STDCALL sqlite3_dbstat_register(sqlite3 *db){ |
| 160300 | +SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ |
| 155839 | 160301 | static sqlite3_module dbstat_module = { |
| 155840 | 160302 | 0, /* iVersion */ |
| 155841 | 160303 | statConnect, /* xCreate */ |
| 155842 | 160304 | statConnect, /* xConnect */ |
| 155843 | 160305 | statBestIndex, /* xBestIndex */ |
| | @@ -155858,8 +160320,10 @@ |
| 155858 | 160320 | 0, /* xFindMethod */ |
| 155859 | 160321 | 0, /* xRename */ |
| 155860 | 160322 | }; |
| 155861 | 160323 | return sqlite3_create_module(db, "dbstat", &dbstat_module, 0); |
| 155862 | 160324 | } |
| 160325 | +#elif defined(SQLITE_ENABLE_DBSTAT_VTAB) |
| 160326 | +SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; } |
| 155863 | 160327 | #endif /* SQLITE_ENABLE_DBSTAT_VTAB */ |
| 155864 | 160328 | |
| 155865 | 160329 | /************** End of dbstat.c **********************************************/ |
| 155866 | 160330 | |