| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.25.2. By combining all the individual C code files into this |
| 3 | +** version 3.26.0. 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. |
| | @@ -257,10 +257,13 @@ |
| 257 | 257 | #if SQLITE_ENABLE_FTS4 |
| 258 | 258 | "ENABLE_FTS4", |
| 259 | 259 | #endif |
| 260 | 260 | #if SQLITE_ENABLE_FTS5 |
| 261 | 261 | "ENABLE_FTS5", |
| 262 | +#endif |
| 263 | +#if SQLITE_ENABLE_GEOPOLY |
| 264 | + "ENABLE_GEOPOLY", |
| 262 | 265 | #endif |
| 263 | 266 | #if SQLITE_ENABLE_HIDDEN_COLUMNS |
| 264 | 267 | "ENABLE_HIDDEN_COLUMNS", |
| 265 | 268 | #endif |
| 266 | 269 | #if SQLITE_ENABLE_ICU |
| | @@ -287,10 +290,13 @@ |
| 287 | 290 | #if SQLITE_ENABLE_MEMSYS5 |
| 288 | 291 | "ENABLE_MEMSYS5", |
| 289 | 292 | #endif |
| 290 | 293 | #if SQLITE_ENABLE_MULTIPLEX |
| 291 | 294 | "ENABLE_MULTIPLEX", |
| 295 | +#endif |
| 296 | +#if SQLITE_ENABLE_NORMALIZE |
| 297 | + "ENABLE_NORMALIZE", |
| 292 | 298 | #endif |
| 293 | 299 | #if SQLITE_ENABLE_NULL_TRIM |
| 294 | 300 | "ENABLE_NULL_TRIM", |
| 295 | 301 | #endif |
| 296 | 302 | #if SQLITE_ENABLE_OVERSIZE_CELL_CHECK |
| | @@ -1154,13 +1160,13 @@ |
| 1154 | 1160 | ** |
| 1155 | 1161 | ** See also: [sqlite3_libversion()], |
| 1156 | 1162 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 1157 | 1163 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 1158 | 1164 | */ |
| 1159 | | -#define SQLITE_VERSION "3.25.2" |
| 1160 | | -#define SQLITE_VERSION_NUMBER 3025002 |
| 1161 | | -#define SQLITE_SOURCE_ID "2018-09-25 19:08:10 fb90e7189ae6d62e77ba3a308ca5d683f90bbe633cf681865365b8e92792d1c7" |
| 1165 | +#define SQLITE_VERSION "3.26.0" |
| 1166 | +#define SQLITE_VERSION_NUMBER 3026000 |
| 1167 | +#define SQLITE_SOURCE_ID "2018-12-01 12:34:55 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238b4f9" |
| 1162 | 1168 | |
| 1163 | 1169 | /* |
| 1164 | 1170 | ** CAPI3REF: Run-Time Library Version Numbers |
| 1165 | 1171 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 1166 | 1172 | ** |
| | @@ -3048,10 +3054,11 @@ |
| 3048 | 3054 | ** the call worked. ^The [sqlite3_db_config()] interface will return a |
| 3049 | 3055 | ** non-zero [error code] if a discontinued or unsupported configuration option |
| 3050 | 3056 | ** is invoked. |
| 3051 | 3057 | ** |
| 3052 | 3058 | ** <dl> |
| 3059 | +** [[SQLITE_DBCONFIG_LOOKASIDE]] |
| 3053 | 3060 | ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt> |
| 3054 | 3061 | ** <dd> ^This option takes three additional arguments that determine the |
| 3055 | 3062 | ** [lookaside memory allocator] configuration for the [database connection]. |
| 3056 | 3063 | ** ^The first argument (the third parameter to [sqlite3_db_config()] is a |
| 3057 | 3064 | ** pointer to a memory buffer to use for lookaside memory. |
| | @@ -3070,10 +3077,11 @@ |
| 3070 | 3077 | ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero. |
| 3071 | 3078 | ** Any attempt to change the lookaside memory configuration when lookaside |
| 3072 | 3079 | ** memory is in use leaves the configuration unchanged and returns |
| 3073 | 3080 | ** [SQLITE_BUSY].)^</dd> |
| 3074 | 3081 | ** |
| 3082 | +** [[SQLITE_DBCONFIG_ENABLE_FKEY]] |
| 3075 | 3083 | ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt> |
| 3076 | 3084 | ** <dd> ^This option is used to enable or disable the enforcement of |
| 3077 | 3085 | ** [foreign key constraints]. There should be two additional arguments. |
| 3078 | 3086 | ** The first argument is an integer which is 0 to disable FK enforcement, |
| 3079 | 3087 | ** positive to enable FK enforcement or negative to leave FK enforcement |
| | @@ -3080,10 +3088,11 @@ |
| 3080 | 3088 | ** unchanged. The second parameter is a pointer to an integer into which |
| 3081 | 3089 | ** is written 0 or 1 to indicate whether FK enforcement is off or on |
| 3082 | 3090 | ** following this call. The second parameter may be a NULL pointer, in |
| 3083 | 3091 | ** which case the FK enforcement setting is not reported back. </dd> |
| 3084 | 3092 | ** |
| 3093 | +** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]] |
| 3085 | 3094 | ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt> |
| 3086 | 3095 | ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers]. |
| 3087 | 3096 | ** There should be two additional arguments. |
| 3088 | 3097 | ** The first argument is an integer which is 0 to disable triggers, |
| 3089 | 3098 | ** positive to enable triggers or negative to leave the setting unchanged. |
| | @@ -3090,10 +3099,11 @@ |
| 3090 | 3099 | ** The second parameter is a pointer to an integer into which |
| 3091 | 3100 | ** is written 0 or 1 to indicate whether triggers are disabled or enabled |
| 3092 | 3101 | ** following this call. The second parameter may be a NULL pointer, in |
| 3093 | 3102 | ** which case the trigger setting is not reported back. </dd> |
| 3094 | 3103 | ** |
| 3104 | +** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]] |
| 3095 | 3105 | ** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt> |
| 3096 | 3106 | ** <dd> ^This option is used to enable or disable the two-argument |
| 3097 | 3107 | ** version of the [fts3_tokenizer()] function which is part of the |
| 3098 | 3108 | ** [FTS3] full-text search engine extension. |
| 3099 | 3109 | ** There should be two additional arguments. |
| | @@ -3103,10 +3113,11 @@ |
| 3103 | 3113 | ** The second parameter is a pointer to an integer into which |
| 3104 | 3114 | ** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled |
| 3105 | 3115 | ** following this call. The second parameter may be a NULL pointer, in |
| 3106 | 3116 | ** which case the new setting is not reported back. </dd> |
| 3107 | 3117 | ** |
| 3118 | +** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]] |
| 3108 | 3119 | ** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt> |
| 3109 | 3120 | ** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()] |
| 3110 | 3121 | ** interface independently of the [load_extension()] SQL function. |
| 3111 | 3122 | ** The [sqlite3_enable_load_extension()] API enables or disables both the |
| 3112 | 3123 | ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()]. |
| | @@ -3120,19 +3131,20 @@ |
| 3120 | 3131 | ** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface |
| 3121 | 3132 | ** is disabled or enabled following this call. The second parameter may |
| 3122 | 3133 | ** be a NULL pointer, in which case the new setting is not reported back. |
| 3123 | 3134 | ** </dd> |
| 3124 | 3135 | ** |
| 3125 | | -** <dt>SQLITE_DBCONFIG_MAINDBNAME</dt> |
| 3136 | +** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt> |
| 3126 | 3137 | ** <dd> ^This option is used to change the name of the "main" database |
| 3127 | 3138 | ** schema. ^The sole argument is a pointer to a constant UTF8 string |
| 3128 | 3139 | ** which will become the new schema name in place of "main". ^SQLite |
| 3129 | 3140 | ** does not make a copy of the new main schema name string, so the application |
| 3130 | 3141 | ** must ensure that the argument passed into this DBCONFIG option is unchanged |
| 3131 | 3142 | ** until after the database connection closes. |
| 3132 | 3143 | ** </dd> |
| 3133 | 3144 | ** |
| 3145 | +** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]] |
| 3134 | 3146 | ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt> |
| 3135 | 3147 | ** <dd> Usually, when a database in wal mode is closed or detached from a |
| 3136 | 3148 | ** database handle, SQLite checks if this will mean that there are now no |
| 3137 | 3149 | ** connections at all to the database. If so, it performs a checkpoint |
| 3138 | 3150 | ** operation before closing the connection. This option may be used to |
| | @@ -3142,11 +3154,11 @@ |
| 3142 | 3154 | ** The second parameter is a pointer to an integer |
| 3143 | 3155 | ** into which is written 0 or 1 to indicate whether checkpoints-on-close |
| 3144 | 3156 | ** have been disabled - 0 if they are not disabled, 1 if they are. |
| 3145 | 3157 | ** </dd> |
| 3146 | 3158 | ** |
| 3147 | | -** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt> |
| 3159 | +** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt> |
| 3148 | 3160 | ** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates |
| 3149 | 3161 | ** the [query planner stability guarantee] (QPSG). When the QPSG is active, |
| 3150 | 3162 | ** a single SQL query statement will always use the same algorithm regardless |
| 3151 | 3163 | ** of values of [bound parameters].)^ The QPSG disables some query optimizations |
| 3152 | 3164 | ** that look at the values of bound parameters, which can make some queries |
| | @@ -3158,11 +3170,11 @@ |
| 3158 | 3170 | ** unchanged. The second parameter is a pointer to an integer into which |
| 3159 | 3171 | ** is written 0 or 1 to indicate whether the QPSG is disabled or enabled |
| 3160 | 3172 | ** following this call. |
| 3161 | 3173 | ** </dd> |
| 3162 | 3174 | ** |
| 3163 | | -** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt> |
| 3175 | +** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt> |
| 3164 | 3176 | ** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not |
| 3165 | 3177 | ** include output for any operations performed by trigger programs. This |
| 3166 | 3178 | ** option is used to set or clear (the default) a flag that governs this |
| 3167 | 3179 | ** behavior. The first parameter passed to this operation is an integer - |
| 3168 | 3180 | ** positive to enable output for trigger programs, or zero to disable it, |
| | @@ -3170,11 +3182,11 @@ |
| 3170 | 3182 | ** The second parameter is a pointer to an integer into which is written |
| 3171 | 3183 | ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if |
| 3172 | 3184 | ** it is not disabled, 1 if it is. |
| 3173 | 3185 | ** </dd> |
| 3174 | 3186 | ** |
| 3175 | | -** <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt> |
| 3187 | +** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt> |
| 3176 | 3188 | ** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run |
| 3177 | 3189 | ** [VACUUM] in order to reset a database back to an empty database |
| 3178 | 3190 | ** with no schema and no content. The following process works even for |
| 3179 | 3191 | ** a badly corrupted database file: |
| 3180 | 3192 | ** <ol> |
| | @@ -3189,10 +3201,22 @@ |
| 3189 | 3201 | ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0); |
| 3190 | 3202 | ** </ol> |
| 3191 | 3203 | ** Because resetting a database is destructive and irreversible, the |
| 3192 | 3204 | ** process requires the use of this obscure API and multiple steps to help |
| 3193 | 3205 | ** ensure that it does not happen by accident. |
| 3206 | +** |
| 3207 | +** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt> |
| 3208 | +** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the |
| 3209 | +** "defensive" flag for a database connection. When the defensive |
| 3210 | +** flag is enabled, language features that allow ordinary SQL to |
| 3211 | +** deliberately corrupt the database file are disabled. The disabled |
| 3212 | +** features include but are not limited to the following: |
| 3213 | +** <ul> |
| 3214 | +** <li> The [PRAGMA writable_schema=ON] statement. |
| 3215 | +** <li> Writes to the [sqlite_dbpage] virtual table. |
| 3216 | +** <li> Direct writes to [shadow tables]. |
| 3217 | +** </ul> |
| 3194 | 3218 | ** </dd> |
| 3195 | 3219 | ** </dl> |
| 3196 | 3220 | */ |
| 3197 | 3221 | #define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */ |
| 3198 | 3222 | #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ |
| | @@ -3202,11 +3226,12 @@ |
| 3202 | 3226 | #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */ |
| 3203 | 3227 | #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */ |
| 3204 | 3228 | #define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */ |
| 3205 | 3229 | #define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */ |
| 3206 | 3230 | #define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */ |
| 3207 | | -#define SQLITE_DBCONFIG_MAX 1009 /* Largest DBCONFIG */ |
| 3231 | +#define SQLITE_DBCONFIG_DEFENSIVE 1010 /* int int* */ |
| 3232 | +#define SQLITE_DBCONFIG_MAX 1010 /* Largest DBCONFIG */ |
| 3208 | 3233 | |
| 3209 | 3234 | /* |
| 3210 | 3235 | ** CAPI3REF: Enable Or Disable Extended Result Codes |
| 3211 | 3236 | ** METHOD: sqlite3 |
| 3212 | 3237 | ** |
| | @@ -4640,13 +4665,23 @@ |
| 4640 | 4665 | ** be used just once or at most a few times and then destroyed using |
| 4641 | 4666 | ** [sqlite3_finalize()] relatively soon. The current implementation acts |
| 4642 | 4667 | ** on this hint by avoiding the use of [lookaside memory] so as not to |
| 4643 | 4668 | ** deplete the limited store of lookaside memory. Future versions of |
| 4644 | 4669 | ** SQLite may act on this hint differently. |
| 4670 | +** |
| 4671 | +** [[SQLITE_PREPARE_NORMALIZE]] ^(<dt>SQLITE_PREPARE_NORMALIZE</dt> |
| 4672 | +** <dd>The SQLITE_PREPARE_NORMALIZE flag indicates that a normalized |
| 4673 | +** representation of the SQL statement should be calculated and then |
| 4674 | +** associated with the prepared statement, which can be obtained via |
| 4675 | +** the [sqlite3_normalized_sql()] interface.)^ The semantics used to |
| 4676 | +** normalize a SQL statement are unspecified and subject to change. |
| 4677 | +** At a minimum, literal values will be replaced with suitable |
| 4678 | +** placeholders. |
| 4645 | 4679 | ** </dl> |
| 4646 | 4680 | */ |
| 4647 | 4681 | #define SQLITE_PREPARE_PERSISTENT 0x01 |
| 4682 | +#define SQLITE_PREPARE_NORMALIZE 0x02 |
| 4648 | 4683 | |
| 4649 | 4684 | /* |
| 4650 | 4685 | ** CAPI3REF: Compiling An SQL Statement |
| 4651 | 4686 | ** KEYWORDS: {SQL statement compiler} |
| 4652 | 4687 | ** METHOD: sqlite3 |
| | @@ -4800,10 +4835,15 @@ |
| 4800 | 4835 | ** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], |
| 4801 | 4836 | ** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()]. |
| 4802 | 4837 | ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8 |
| 4803 | 4838 | ** string containing the SQL text of prepared statement P with |
| 4804 | 4839 | ** [bound parameters] expanded. |
| 4840 | +** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8 |
| 4841 | +** string containing the normalized SQL text of prepared statement P. The |
| 4842 | +** semantics used to normalize a SQL statement are unspecified and subject |
| 4843 | +** to change. At a minimum, literal values will be replaced with suitable |
| 4844 | +** placeholders. |
| 4805 | 4845 | ** |
| 4806 | 4846 | ** ^(For example, if a prepared statement is created using the SQL |
| 4807 | 4847 | ** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345 |
| 4808 | 4848 | ** and parameter :xyz is unbound, then sqlite3_sql() will return |
| 4809 | 4849 | ** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql() |
| | @@ -4815,18 +4855,20 @@ |
| 4815 | 4855 | ** |
| 4816 | 4856 | ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of |
| 4817 | 4857 | ** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time |
| 4818 | 4858 | ** option causes sqlite3_expanded_sql() to always return NULL. |
| 4819 | 4859 | ** |
| 4820 | | -** ^The string returned by sqlite3_sql(P) is managed by SQLite and is |
| 4821 | | -** automatically freed when the prepared statement is finalized. |
| 4860 | +** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P) |
| 4861 | +** are managed by SQLite and are automatically freed when the prepared |
| 4862 | +** statement is finalized. |
| 4822 | 4863 | ** ^The string returned by sqlite3_expanded_sql(P), on the other hand, |
| 4823 | 4864 | ** is obtained from [sqlite3_malloc()] and must be free by the application |
| 4824 | 4865 | ** by passing it to [sqlite3_free()]. |
| 4825 | 4866 | */ |
| 4826 | 4867 | SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt); |
| 4827 | 4868 | SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt); |
| 4869 | +SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt); |
| 4828 | 4870 | |
| 4829 | 4871 | /* |
| 4830 | 4872 | ** CAPI3REF: Determine If An SQL Statement Writes The Database |
| 4831 | 4873 | ** METHOD: sqlite3_stmt |
| 4832 | 4874 | ** |
| | @@ -7312,10 +7354,13 @@ |
| 7312 | 7354 | /* The methods above are in version 1 of the sqlite_module object. Those |
| 7313 | 7355 | ** below are for version 2 and greater. */ |
| 7314 | 7356 | int (*xSavepoint)(sqlite3_vtab *pVTab, int); |
| 7315 | 7357 | int (*xRelease)(sqlite3_vtab *pVTab, int); |
| 7316 | 7358 | int (*xRollbackTo)(sqlite3_vtab *pVTab, int); |
| 7359 | + /* The methods above are in versions 1 and 2 of the sqlite_module object. |
| 7360 | + ** Those below are for version 3 and greater. */ |
| 7361 | + int (*xShadowName)(const char*); |
| 7317 | 7362 | }; |
| 7318 | 7363 | |
| 7319 | 7364 | /* |
| 7320 | 7365 | ** CAPI3REF: Virtual Table Indexing Information |
| 7321 | 7366 | ** KEYWORDS: sqlite3_index_info |
| | @@ -8234,10 +8279,11 @@ |
| 8234 | 8279 | #define SQLITE_TESTCTRL_ALWAYS 13 |
| 8235 | 8280 | #define SQLITE_TESTCTRL_RESERVE 14 |
| 8236 | 8281 | #define SQLITE_TESTCTRL_OPTIMIZATIONS 15 |
| 8237 | 8282 | #define SQLITE_TESTCTRL_ISKEYWORD 16 /* NOT USED */ |
| 8238 | 8283 | #define SQLITE_TESTCTRL_SCRATCHMALLOC 17 /* NOT USED */ |
| 8284 | +#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS 17 |
| 8239 | 8285 | #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18 |
| 8240 | 8286 | #define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */ |
| 8241 | 8287 | #define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD 19 |
| 8242 | 8288 | #define SQLITE_TESTCTRL_NEVER_CORRUPT 20 |
| 8243 | 8289 | #define SQLITE_TESTCTRL_VDBE_COVERAGE 21 |
| | @@ -9646,10 +9692,11 @@ |
| 9646 | 9692 | ** These macros define the various options to the |
| 9647 | 9693 | ** [sqlite3_vtab_config()] interface that [virtual table] implementations |
| 9648 | 9694 | ** can use to customize and optimize their behavior. |
| 9649 | 9695 | ** |
| 9650 | 9696 | ** <dl> |
| 9697 | +** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]] |
| 9651 | 9698 | ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT |
| 9652 | 9699 | ** <dd>Calls of the form |
| 9653 | 9700 | ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported, |
| 9654 | 9701 | ** where X is an integer. If X is zero, then the [virtual table] whose |
| 9655 | 9702 | ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not |
| | @@ -10415,11 +10462,11 @@ |
| 10415 | 10462 | int iLevel; /* Level of current node or entry */ |
| 10416 | 10463 | int mxLevel; /* The largest iLevel value in the tree */ |
| 10417 | 10464 | sqlite3_int64 iRowid; /* Rowid for current entry */ |
| 10418 | 10465 | sqlite3_rtree_dbl rParentScore; /* Score of parent node */ |
| 10419 | 10466 | int eParentWithin; /* Visibility of parent node */ |
| 10420 | | - int eWithin; /* OUT: Visiblity */ |
| 10467 | + int eWithin; /* OUT: Visibility */ |
| 10421 | 10468 | sqlite3_rtree_dbl rScore; /* OUT: Write the score here */ |
| 10422 | 10469 | /* The following fields are only available in 3.8.11 and later */ |
| 10423 | 10470 | sqlite3_value **apSqlParam; /* Original SQL values of parameters */ |
| 10424 | 10471 | }; |
| 10425 | 10472 | |
| | @@ -10911,16 +10958,42 @@ |
| 10911 | 10958 | ** an application iterates through a changeset using an iterator created by |
| 10912 | 10959 | ** this function, all changes that relate to a single table are visited |
| 10913 | 10960 | ** consecutively. There is no chance that the iterator will visit a change |
| 10914 | 10961 | ** the applies to table X, then one for table Y, and then later on visit |
| 10915 | 10962 | ** another change for table X. |
| 10963 | +** |
| 10964 | +** The behavior of sqlite3changeset_start_v2() and its streaming equivalent |
| 10965 | +** may be modified by passing a combination of |
| 10966 | +** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter. |
| 10967 | +** |
| 10968 | +** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b> |
| 10969 | +** and therefore subject to change. |
| 10916 | 10970 | */ |
| 10917 | 10971 | SQLITE_API int sqlite3changeset_start( |
| 10918 | 10972 | sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */ |
| 10919 | 10973 | int nChangeset, /* Size of changeset blob in bytes */ |
| 10920 | 10974 | void *pChangeset /* Pointer to blob containing changeset */ |
| 10921 | 10975 | ); |
| 10976 | +SQLITE_API int sqlite3changeset_start_v2( |
| 10977 | + sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */ |
| 10978 | + int nChangeset, /* Size of changeset blob in bytes */ |
| 10979 | + void *pChangeset, /* Pointer to blob containing changeset */ |
| 10980 | + int flags /* SESSION_CHANGESETSTART_* flags */ |
| 10981 | +); |
| 10982 | + |
| 10983 | +/* |
| 10984 | +** CAPI3REF: Flags for sqlite3changeset_start_v2 |
| 10985 | +** |
| 10986 | +** The following flags may passed via the 4th parameter to |
| 10987 | +** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]: |
| 10988 | +** |
| 10989 | +** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd> |
| 10990 | +** Invert the changeset while iterating through it. This is equivalent to |
| 10991 | +** inverting a changeset using sqlite3changeset_invert() before applying it. |
| 10992 | +** It is an error to specify this flag with a patchset. |
| 10993 | +*/ |
| 10994 | +#define SQLITE_CHANGESETSTART_INVERT 0x0002 |
| 10922 | 10995 | |
| 10923 | 10996 | |
| 10924 | 10997 | /* |
| 10925 | 10998 | ** CAPI3REF: Advance A Changeset Iterator |
| 10926 | 10999 | ** METHOD: sqlite3_changeset_iter |
| | @@ -11571,11 +11644,11 @@ |
| 11571 | 11644 | int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ |
| 11572 | 11645 | sqlite3_changeset_iter *p /* Handle describing change and conflict */ |
| 11573 | 11646 | ), |
| 11574 | 11647 | void *pCtx, /* First argument passed to xConflict */ |
| 11575 | 11648 | void **ppRebase, int *pnRebase, /* OUT: Rebase data */ |
| 11576 | | - int flags /* Combination of SESSION_APPLY_* flags */ |
| 11649 | + int flags /* SESSION_CHANGESETAPPLY_* flags */ |
| 11577 | 11650 | ); |
| 11578 | 11651 | |
| 11579 | 11652 | /* |
| 11580 | 11653 | ** CAPI3REF: Flags for sqlite3changeset_apply_v2 |
| 11581 | 11654 | ** |
| | @@ -11589,12 +11662,18 @@ |
| 11589 | 11662 | ** SAVEPOINT is committed if the changeset or patchset is successfully |
| 11590 | 11663 | ** applied, or rolled back if an error occurs. Specifying this flag |
| 11591 | 11664 | ** causes the sessions module to omit this savepoint. In this case, if the |
| 11592 | 11665 | ** caller has an open transaction or savepoint when apply_v2() is called, |
| 11593 | 11666 | ** it may revert the partially applied changeset by rolling it back. |
| 11667 | +** |
| 11668 | +** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd> |
| 11669 | +** Invert the changeset before applying it. This is equivalent to inverting |
| 11670 | +** a changeset using sqlite3changeset_invert() before applying it. It is |
| 11671 | +** an error to specify this flag with a patchset. |
| 11594 | 11672 | */ |
| 11595 | 11673 | #define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001 |
| 11674 | +#define SQLITE_CHANGESETAPPLY_INVERT 0x0002 |
| 11596 | 11675 | |
| 11597 | 11676 | /* |
| 11598 | 11677 | ** CAPI3REF: Constants Passed To The Conflict Handler |
| 11599 | 11678 | ** |
| 11600 | 11679 | ** Values that may be passed as the second argument to a conflict-handler. |
| | @@ -11983,10 +12062,16 @@ |
| 11983 | 12062 | ); |
| 11984 | 12063 | SQLITE_API int sqlite3changeset_start_strm( |
| 11985 | 12064 | sqlite3_changeset_iter **pp, |
| 11986 | 12065 | int (*xInput)(void *pIn, void *pData, int *pnData), |
| 11987 | 12066 | void *pIn |
| 12067 | +); |
| 12068 | +SQLITE_API int sqlite3changeset_start_v2_strm( |
| 12069 | + sqlite3_changeset_iter **pp, |
| 12070 | + int (*xInput)(void *pIn, void *pData, int *pnData), |
| 12071 | + void *pIn, |
| 12072 | + int flags |
| 11988 | 12073 | ); |
| 11989 | 12074 | SQLITE_API int sqlite3session_changeset_strm( |
| 11990 | 12075 | sqlite3_session *pSession, |
| 11991 | 12076 | int (*xOutput)(void *pOut, const void *pData, int nData), |
| 11992 | 12077 | void *pOut |
| | @@ -12010,10 +12095,49 @@ |
| 12010 | 12095 | void *pIn, |
| 12011 | 12096 | int (*xOutput)(void *pOut, const void *pData, int nData), |
| 12012 | 12097 | void *pOut |
| 12013 | 12098 | ); |
| 12014 | 12099 | |
| 12100 | +/* |
| 12101 | +** CAPI3REF: Configure global parameters |
| 12102 | +** |
| 12103 | +** The sqlite3session_config() interface is used to make global configuration |
| 12104 | +** changes to the sessions module in order to tune it to the specific needs |
| 12105 | +** of the application. |
| 12106 | +** |
| 12107 | +** The sqlite3session_config() interface is not threadsafe. If it is invoked |
| 12108 | +** while any other thread is inside any other sessions method then the |
| 12109 | +** results are undefined. Furthermore, if it is invoked after any sessions |
| 12110 | +** related objects have been created, the results are also undefined. |
| 12111 | +** |
| 12112 | +** The first argument to the sqlite3session_config() function must be one |
| 12113 | +** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The |
| 12114 | +** interpretation of the (void*) value passed as the second parameter and |
| 12115 | +** the effect of calling this function depends on the value of the first |
| 12116 | +** parameter. |
| 12117 | +** |
| 12118 | +** <dl> |
| 12119 | +** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd> |
| 12120 | +** By default, the sessions module streaming interfaces attempt to input |
| 12121 | +** and output data in approximately 1 KiB chunks. This operand may be used |
| 12122 | +** to set and query the value of this configuration setting. The pointer |
| 12123 | +** passed as the second argument must point to a value of type (int). |
| 12124 | +** If this value is greater than 0, it is used as the new streaming data |
| 12125 | +** chunk size for both input and output. Before returning, the (int) value |
| 12126 | +** pointed to by pArg is set to the final value of the streaming interface |
| 12127 | +** chunk size. |
| 12128 | +** </dl> |
| 12129 | +** |
| 12130 | +** This function returns SQLITE_OK if successful, or an SQLite error code |
| 12131 | +** otherwise. |
| 12132 | +*/ |
| 12133 | +SQLITE_API int sqlite3session_config(int op, void *pArg); |
| 12134 | + |
| 12135 | +/* |
| 12136 | +** CAPI3REF: Values for sqlite3session_config(). |
| 12137 | +*/ |
| 12138 | +#define SQLITE_SESSION_CONFIG_STRMSIZE 1 |
| 12015 | 12139 | |
| 12016 | 12140 | /* |
| 12017 | 12141 | ** Make sure we can call this stuff from C++. |
| 12018 | 12142 | */ |
| 12019 | 12143 | #if 0 |
| | @@ -15275,22 +15399,21 @@ |
| 15275 | 15399 | SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*); |
| 15276 | 15400 | SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager); |
| 15277 | 15401 | SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager); |
| 15278 | 15402 | SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen); |
| 15279 | 15403 | SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*); |
| 15280 | | -# ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 15281 | | -SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno); |
| 15282 | | -# endif |
| 15283 | 15404 | # ifdef SQLITE_ENABLE_SNAPSHOT |
| 15284 | 15405 | SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot); |
| 15285 | 15406 | SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot); |
| 15286 | 15407 | SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager); |
| 15287 | 15408 | SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot); |
| 15288 | 15409 | SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager); |
| 15289 | 15410 | # endif |
| 15290 | | -#else |
| 15291 | | -# define sqlite3PagerUseWal(x,y) 0 |
| 15411 | +#endif |
| 15412 | + |
| 15413 | +#ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 15414 | +SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno); |
| 15292 | 15415 | #endif |
| 15293 | 15416 | |
| 15294 | 15417 | #ifdef SQLITE_ENABLE_ZIPVFS |
| 15295 | 15418 | SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager); |
| 15296 | 15419 | #endif |
| | @@ -15530,10 +15653,14 @@ |
| 15530 | 15653 | SQLITE_PRIVATE int sqlite3HeaderSizePcache(void); |
| 15531 | 15654 | SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void); |
| 15532 | 15655 | |
| 15533 | 15656 | /* Number of dirty pages as a percentage of the configured cache size */ |
| 15534 | 15657 | SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*); |
| 15658 | + |
| 15659 | +#ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 15660 | +SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache); |
| 15661 | +#endif |
| 15535 | 15662 | |
| 15536 | 15663 | #endif /* _PCACHE_H_ */ |
| 15537 | 15664 | |
| 15538 | 15665 | /************** End of pcache.h **********************************************/ |
| 15539 | 15666 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| | @@ -16036,16 +16163,18 @@ |
| 16036 | 16163 | /* |
| 16037 | 16164 | ** A hash table for built-in function definitions. (Application-defined |
| 16038 | 16165 | ** functions use a regular table table from hash.h.) |
| 16039 | 16166 | ** |
| 16040 | 16167 | ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots. |
| 16041 | | -** Collisions are on the FuncDef.u.pHash chain. |
| 16168 | +** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH() |
| 16169 | +** macro to compute a hash on the function name. |
| 16042 | 16170 | */ |
| 16043 | 16171 | #define SQLITE_FUNC_HASH_SZ 23 |
| 16044 | 16172 | struct FuncDefHash { |
| 16045 | 16173 | FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */ |
| 16046 | 16174 | }; |
| 16175 | +#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ) |
| 16047 | 16176 | |
| 16048 | 16177 | #ifdef SQLITE_USER_AUTHENTICATION |
| 16049 | 16178 | /* |
| 16050 | 16179 | ** Information held in the "sqlite3" database connection object and used |
| 16051 | 16180 | ** to manage user authentication. |
| | @@ -16102,11 +16231,11 @@ |
| 16102 | 16231 | CollSeq *pDfltColl; /* The default collating sequence (BINARY) */ |
| 16103 | 16232 | sqlite3_mutex *mutex; /* Connection mutex */ |
| 16104 | 16233 | Db *aDb; /* All backends */ |
| 16105 | 16234 | int nDb; /* Number of backends currently in use */ |
| 16106 | 16235 | u32 mDbFlags; /* flags recording internal state */ |
| 16107 | | - u32 flags; /* flags settable by pragmas. See below */ |
| 16236 | + u64 flags; /* flags settable by pragmas. See below */ |
| 16108 | 16237 | i64 lastRowid; /* ROWID of most recent insert (see above) */ |
| 16109 | 16238 | i64 szMmap; /* Default mmap_size setting */ |
| 16110 | 16239 | u32 nSchemaLock; /* Do not reset the schema when non-zero */ |
| 16111 | 16240 | unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */ |
| 16112 | 16241 | int errCode; /* Most recent error code (SQLITE_*) */ |
| | @@ -16268,18 +16397,21 @@ |
| 16268 | 16397 | #define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */ |
| 16269 | 16398 | #define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/ |
| 16270 | 16399 | #define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */ |
| 16271 | 16400 | #define SQLITE_ResetDatabase 0x02000000 /* Reset the database */ |
| 16272 | 16401 | #define SQLITE_LegacyAlter 0x04000000 /* Legacy ALTER TABLE behaviour */ |
| 16402 | +#define SQLITE_NoSchemaError 0x08000000 /* Do not report schema parse errors*/ |
| 16403 | +#define SQLITE_Defensive 0x10000000 /* Input SQL is likely hostile */ |
| 16273 | 16404 | |
| 16274 | 16405 | /* Flags used only if debugging */ |
| 16406 | +#define HI(X) ((u64)(X)<<32) |
| 16275 | 16407 | #ifdef SQLITE_DEBUG |
| 16276 | | -#define SQLITE_SqlTrace 0x08000000 /* Debug print SQL as it executes */ |
| 16277 | | -#define SQLITE_VdbeListing 0x10000000 /* Debug listings of VDBE programs */ |
| 16278 | | -#define SQLITE_VdbeTrace 0x20000000 /* True to trace VDBE execution */ |
| 16279 | | -#define SQLITE_VdbeAddopTrace 0x40000000 /* Trace sqlite3VdbeAddOp() calls */ |
| 16280 | | -#define SQLITE_VdbeEQP 0x80000000 /* Debug EXPLAIN QUERY PLAN */ |
| 16408 | +#define SQLITE_SqlTrace HI(0x0001) /* Debug print SQL as it executes */ |
| 16409 | +#define SQLITE_VdbeListing HI(0x0002) /* Debug listings of VDBE progs */ |
| 16410 | +#define SQLITE_VdbeTrace HI(0x0004) /* True to trace VDBE execution */ |
| 16411 | +#define SQLITE_VdbeAddopTrace HI(0x0008) /* Trace sqlite3VdbeAddOp() calls */ |
| 16412 | +#define SQLITE_VdbeEQP HI(0x0010) /* Debug EXPLAIN QUERY PLAN */ |
| 16281 | 16413 | #endif |
| 16282 | 16414 | |
| 16283 | 16415 | /* |
| 16284 | 16416 | ** Allowed values for sqlite3.mDbFlags |
| 16285 | 16417 | */ |
| | @@ -16409,12 +16541,13 @@ |
| 16409 | 16541 | #define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */ |
| 16410 | 16542 | #define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a |
| 16411 | 16543 | ** single query - might change over time */ |
| 16412 | 16544 | #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */ |
| 16413 | 16545 | #define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */ |
| 16414 | | -#define SQLITE_FUNC_WINDOW 0x10000 /* Built-in window-only function */ |
| 16415 | | -#define SQLITE_FUNC_WINDOW_SIZE 0x20000 /* Requires partition size as arg. */ |
| 16546 | +#define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */ |
| 16547 | +#define SQLITE_FUNC_WINDOW_SIZE 0x20000 /* Requires partition size as arg. */ |
| 16548 | +#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */ |
| 16416 | 16549 | |
| 16417 | 16550 | /* |
| 16418 | 16551 | ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are |
| 16419 | 16552 | ** used to create the initializers for the FuncDef structures. |
| 16420 | 16553 | ** |
| | @@ -16486,14 +16619,17 @@ |
| 16486 | 16619 | {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \ |
| 16487 | 16620 | SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,0,#zName, {0}} |
| 16488 | 16621 | #define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \ |
| 16489 | 16622 | {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \ |
| 16490 | 16623 | SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xFinal,0,#zName, {0}} |
| 16491 | | - |
| 16492 | 16624 | #define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \ |
| 16493 | 16625 | {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \ |
| 16494 | 16626 | SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}} |
| 16627 | +#define INTERNAL_FUNCTION(zName, nArg, xFunc) \ |
| 16628 | + {nArg, SQLITE_FUNC_INTERNAL|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \ |
| 16629 | + 0, 0, xFunc, 0, 0, 0, #zName, {0} } |
| 16630 | + |
| 16495 | 16631 | |
| 16496 | 16632 | /* |
| 16497 | 16633 | ** All current savepoints are stored in a linked list starting at |
| 16498 | 16634 | ** sqlite3.pSavepoint. The first element in the list is the most recently |
| 16499 | 16635 | ** opened savepoint. Savepoints are added to the list by the vdbe |
| | @@ -16674,10 +16810,13 @@ |
| 16674 | 16810 | ** by an instance of the following structure. |
| 16675 | 16811 | */ |
| 16676 | 16812 | struct Table { |
| 16677 | 16813 | char *zName; /* Name of the table or view */ |
| 16678 | 16814 | Column *aCol; /* Information about each column */ |
| 16815 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 16816 | + Hash *pColHash; /* All columns indexed by name */ |
| 16817 | +#endif |
| 16679 | 16818 | Index *pIndex; /* List of SQL indexes on this table. */ |
| 16680 | 16819 | Select *pSelect; /* NULL for tables. Points to definition if a view. */ |
| 16681 | 16820 | FKey *pFKey; /* Linked list of all foreign keys in this table */ |
| 16682 | 16821 | char *zColAff; /* String defining the affinity of each column */ |
| 16683 | 16822 | ExprList *pCheck; /* All CHECK constraints */ |
| | @@ -16724,10 +16863,11 @@ |
| 16724 | 16863 | #define TF_NoVisibleRowid 0x0040 /* No user-visible "rowid" column */ |
| 16725 | 16864 | #define TF_OOOHidden 0x0080 /* Out-of-Order hidden columns */ |
| 16726 | 16865 | #define TF_StatsUsed 0x0100 /* Query planner decisions affected by |
| 16727 | 16866 | ** Index.aiRowLogEst[] values */ |
| 16728 | 16867 | #define TF_HasNotNull 0x0200 /* Contains NOT NULL constraints */ |
| 16868 | +#define TF_Shadow 0x0400 /* True for a shadow table */ |
| 16729 | 16869 | |
| 16730 | 16870 | /* |
| 16731 | 16871 | ** Test to see whether or not a table is a virtual table. This is |
| 16732 | 16872 | ** done as a macro so that it will be optimized out when virtual |
| 16733 | 16873 | ** table support is omitted from the build. |
| | @@ -17010,10 +17150,16 @@ |
| 17010 | 17150 | tRowcnt *anEq; /* Est. number of rows where the key equals this sample */ |
| 17011 | 17151 | tRowcnt *anLt; /* Est. number of rows where key is less than this sample */ |
| 17012 | 17152 | tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */ |
| 17013 | 17153 | }; |
| 17014 | 17154 | |
| 17155 | +/* |
| 17156 | +** Possible values to use within the flags argument to sqlite3GetToken(). |
| 17157 | +*/ |
| 17158 | +#define SQLITE_TOKEN_QUOTED 0x1 /* Token is a quoted identifier. */ |
| 17159 | +#define SQLITE_TOKEN_KEYWORD 0x2 /* Token is a keyword. */ |
| 17160 | + |
| 17015 | 17161 | /* |
| 17016 | 17162 | ** Each token coming out of the lexer is an instance of |
| 17017 | 17163 | ** this structure. Tokens are also used as part of an expression. |
| 17018 | 17164 | ** |
| 17019 | 17165 | ** The memory that "z" points to is owned by other objects. Take care |
| | @@ -17191,15 +17337,15 @@ |
| 17191 | 17337 | i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */ |
| 17192 | 17338 | u8 op2; /* TK_REGISTER: original value of Expr.op |
| 17193 | 17339 | ** TK_COLUMN: the value of p5 for OP_Column |
| 17194 | 17340 | ** TK_AGG_FUNCTION: nesting depth */ |
| 17195 | 17341 | AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */ |
| 17196 | | - Table *pTab; /* Table for TK_COLUMN expressions. Can be NULL |
| 17197 | | - ** for a column of an index on an expression */ |
| 17198 | | -#ifndef SQLITE_OMIT_WINDOWFUNC |
| 17199 | | - Window *pWin; /* Window definition for window functions */ |
| 17200 | | -#endif |
| 17342 | + union { |
| 17343 | + Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL |
| 17344 | + ** for a column of an index on an expression */ |
| 17345 | + Window *pWin; /* TK_FUNCTION: Window definition for the func */ |
| 17346 | + } y; |
| 17201 | 17347 | }; |
| 17202 | 17348 | |
| 17203 | 17349 | /* |
| 17204 | 17350 | ** The following are the meanings of bits in the Expr.flags field. |
| 17205 | 17351 | */ |
| | @@ -17225,10 +17371,11 @@ |
| 17225 | 17371 | #define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */ |
| 17226 | 17372 | #define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */ |
| 17227 | 17373 | #define EP_Subquery 0x200000 /* Tree contains a TK_SELECT operator */ |
| 17228 | 17374 | #define EP_Alias 0x400000 /* Is an alias for a result set column */ |
| 17229 | 17375 | #define EP_Leaf 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */ |
| 17376 | +#define EP_WinFunc 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */ |
| 17230 | 17377 | |
| 17231 | 17378 | /* |
| 17232 | 17379 | ** The EP_Propagate mask is a set of properties that automatically propagate |
| 17233 | 17380 | ** upwards into parent nodes. |
| 17234 | 17381 | */ |
| | @@ -17910,10 +18057,11 @@ |
| 17910 | 18057 | ** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION |
| 17911 | 18058 | ** OPFLAG_AUXDELETE == BTREE_AUXDELETE |
| 17912 | 18059 | */ |
| 17913 | 18060 | #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */ |
| 17914 | 18061 | /* Also used in P2 (not P5) of OP_Delete */ |
| 18062 | +#define OPFLAG_NOCHNG 0x01 /* OP_VColumn nochange for UPDATE */ |
| 17915 | 18063 | #define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */ |
| 17916 | 18064 | #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */ |
| 17917 | 18065 | #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */ |
| 17918 | 18066 | #define OPFLAG_APPEND 0x08 /* This is likely to be an append */ |
| 17919 | 18067 | #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */ |
| | @@ -18128,10 +18276,11 @@ |
| 18128 | 18276 | #endif |
| 18129 | 18277 | #ifndef SQLITE_UNTESTABLE |
| 18130 | 18278 | int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */ |
| 18131 | 18279 | #endif |
| 18132 | 18280 | int bLocaltimeFault; /* True to fail localtime() calls */ |
| 18281 | + int bInternalFunctions; /* Internal SQL functions are visible */ |
| 18133 | 18282 | int iOnceResetThreshold; /* When to reset OP_Once counters */ |
| 18134 | 18283 | u32 szSorterRef; /* Min size in bytes to use sorter-refs */ |
| 18135 | 18284 | }; |
| 18136 | 18285 | |
| 18137 | 18286 | /* |
| | @@ -18381,10 +18530,11 @@ |
| 18381 | 18530 | /* |
| 18382 | 18531 | ** Internal function prototypes |
| 18383 | 18532 | */ |
| 18384 | 18533 | SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*); |
| 18385 | 18534 | SQLITE_PRIVATE int sqlite3Strlen30(const char*); |
| 18535 | +#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff) |
| 18386 | 18536 | SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*); |
| 18387 | 18537 | #define sqlite3StrNICmp sqlite3_strnicmp |
| 18388 | 18538 | |
| 18389 | 18539 | SQLITE_PRIVATE int sqlite3MallocInit(void); |
| 18390 | 18540 | SQLITE_PRIVATE void sqlite3MallocEnd(void); |
| | @@ -18497,10 +18647,11 @@ |
| 18497 | 18647 | |
| 18498 | 18648 | #if defined(SQLITE_DEBUG) |
| 18499 | 18649 | SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8); |
| 18500 | 18650 | SQLITE_PRIVATE void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*); |
| 18501 | 18651 | SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*); |
| 18652 | +SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView*, const SrcList*); |
| 18502 | 18653 | SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8); |
| 18503 | 18654 | SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView*, const With*, u8); |
| 18504 | 18655 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 18505 | 18656 | SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView*, const Window*, u8); |
| 18506 | 18657 | SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8); |
| | @@ -18729,15 +18880,19 @@ |
| 18729 | 18880 | #endif |
| 18730 | 18881 | SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*); |
| 18731 | 18882 | SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*); |
| 18732 | 18883 | SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char); |
| 18733 | 18884 | SQLITE_PRIVATE int sqlite3IsRowid(const char*); |
| 18885 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 18886 | +SQLITE_PRIVATE int sqlite3IsRowidN(const char*, int); |
| 18887 | +#endif |
| 18734 | 18888 | SQLITE_PRIVATE void sqlite3GenerateRowDelete( |
| 18735 | 18889 | Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int); |
| 18736 | 18890 | SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int); |
| 18737 | 18891 | SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int); |
| 18738 | 18892 | SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int); |
| 18893 | +SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int); |
| 18739 | 18894 | SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int, |
| 18740 | 18895 | u8,u8,int,int*,int*,Upsert*); |
| 18741 | 18896 | #ifdef SQLITE_ENABLE_NULL_TRIM |
| 18742 | 18897 | SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe*,Table*); |
| 18743 | 18898 | #else |
| | @@ -18754,10 +18909,13 @@ |
| 18754 | 18909 | SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int); |
| 18755 | 18910 | SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int); |
| 18756 | 18911 | SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int); |
| 18757 | 18912 | SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*); |
| 18758 | 18913 | SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int); |
| 18914 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 18915 | +SQLITE_PRIVATE FuncDef *sqlite3FunctionSearchN(int,const char*,int); |
| 18916 | +#endif |
| 18759 | 18917 | SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int); |
| 18760 | 18918 | SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8); |
| 18761 | 18919 | SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void); |
| 18762 | 18920 | SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void); |
| 18763 | 18921 | SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*); |
| | @@ -18911,10 +19069,11 @@ |
| 18911 | 19069 | SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*); |
| 18912 | 19070 | SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int); |
| 18913 | 19071 | SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*); |
| 18914 | 19072 | SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*); |
| 18915 | 19073 | SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *); |
| 19074 | +SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3*); |
| 18916 | 19075 | SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *); |
| 18917 | 19076 | SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int); |
| 18918 | 19077 | SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64); |
| 18919 | 19078 | SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64); |
| 18920 | 19079 | SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64); |
| | @@ -18957,10 +19116,13 @@ |
| 18957 | 19116 | SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*); |
| 18958 | 19117 | SQLITE_PRIVATE void sqlite3AlterFunctions(void); |
| 18959 | 19118 | SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*); |
| 18960 | 19119 | SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*); |
| 18961 | 19120 | SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *); |
| 19121 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 19122 | +SQLITE_PRIVATE int sqlite3GetTokenNormalized(const unsigned char *, int *, int *); |
| 19123 | +#endif |
| 18962 | 19124 | SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...); |
| 18963 | 19125 | SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int); |
| 18964 | 19126 | SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr *, int, int); |
| 18965 | 19127 | SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*); |
| 18966 | 19128 | SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p); |
| | @@ -19114,10 +19276,13 @@ |
| 19114 | 19276 | SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*); |
| 19115 | 19277 | SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*); |
| 19116 | 19278 | SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int); |
| 19117 | 19279 | SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *); |
| 19118 | 19280 | SQLITE_PRIVATE void sqlite3ParserReset(Parse*); |
| 19281 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 19282 | +SQLITE_PRIVATE void sqlite3Normalize(Vdbe*, const char*, int, u8); |
| 19283 | +#endif |
| 19119 | 19284 | SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*); |
| 19120 | 19285 | SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*); |
| 19121 | 19286 | SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *); |
| 19122 | 19287 | SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*); |
| 19123 | 19288 | SQLITE_PRIVATE const char *sqlite3JournalModename(int); |
| | @@ -19575,10 +19740,11 @@ |
| 19575 | 19740 | #endif |
| 19576 | 19741 | #ifndef SQLITE_UNTESTABLE |
| 19577 | 19742 | 0, /* xTestCallback */ |
| 19578 | 19743 | #endif |
| 19579 | 19744 | 0, /* bLocaltimeFault */ |
| 19745 | + 0, /* bInternalFunctions */ |
| 19580 | 19746 | 0x7ffffffe, /* iOnceResetThreshold */ |
| 19581 | 19747 | SQLITE_DEFAULT_SORTERREF_SIZE /* szSorterRef */ |
| 19582 | 19748 | }; |
| 19583 | 19749 | |
| 19584 | 19750 | /* |
| | @@ -20066,10 +20232,13 @@ |
| 20066 | 20232 | bft bIsReader:1; /* True for statements that read */ |
| 20067 | 20233 | yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */ |
| 20068 | 20234 | yDbMask lockMask; /* Subset of btreeMask that requires a lock */ |
| 20069 | 20235 | u32 aCounter[7]; /* Counters used by sqlite3_stmt_status() */ |
| 20070 | 20236 | char *zSql; /* Text of the SQL statement that generated this */ |
| 20237 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 20238 | + char *zNormSql; /* Normalization of the associated SQL statement */ |
| 20239 | +#endif |
| 20071 | 20240 | void *pFree; /* Free this when deleting the vdbe */ |
| 20072 | 20241 | VdbeFrame *pFrame; /* Parent frame */ |
| 20073 | 20242 | VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */ |
| 20074 | 20243 | int nFrame; /* Number of frames in pFrame list */ |
| 20075 | 20244 | u32 expmask; /* Binding to these vars invalidates VM */ |
| | @@ -20128,11 +20297,13 @@ |
| 20128 | 20297 | |
| 20129 | 20298 | int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *); |
| 20130 | 20299 | SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*); |
| 20131 | 20300 | SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*); |
| 20132 | 20301 | SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*); |
| 20302 | +#ifndef SQLITE_OMIT_EXPLAIN |
| 20133 | 20303 | SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*); |
| 20304 | +#endif |
| 20134 | 20305 | SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*); |
| 20135 | 20306 | SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int); |
| 20136 | 20307 | SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*); |
| 20137 | 20308 | SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*); |
| 20138 | 20309 | SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int); |
| | @@ -20167,11 +20338,13 @@ |
| 20167 | 20338 | SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p); |
| 20168 | 20339 | SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*); |
| 20169 | 20340 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 20170 | 20341 | SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*); |
| 20171 | 20342 | #endif |
| 20343 | +#ifndef SQLITE_OMIT_EXPLAIN |
| 20172 | 20344 | SQLITE_PRIVATE const char *sqlite3OpcodeName(int); |
| 20345 | +#endif |
| 20173 | 20346 | SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); |
| 20174 | 20347 | SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n); |
| 20175 | 20348 | SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int); |
| 20176 | 20349 | #ifdef SQLITE_DEBUG |
| 20177 | 20350 | SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame*); |
| | @@ -28294,10 +28467,46 @@ |
| 28294 | 28467 | } |
| 28295 | 28468 | sqlite3TreeViewPop(pView); |
| 28296 | 28469 | } |
| 28297 | 28470 | } |
| 28298 | 28471 | |
| 28472 | +/* |
| 28473 | +** Generate a human-readable description of a SrcList object. |
| 28474 | +*/ |
| 28475 | +SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){ |
| 28476 | + int i; |
| 28477 | + for(i=0; i<pSrc->nSrc; i++){ |
| 28478 | + const struct SrcList_item *pItem = &pSrc->a[i]; |
| 28479 | + StrAccum x; |
| 28480 | + char zLine[100]; |
| 28481 | + sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); |
| 28482 | + sqlite3_str_appendf(&x, "{%d,*}", pItem->iCursor); |
| 28483 | + if( pItem->zDatabase ){ |
| 28484 | + sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName); |
| 28485 | + }else if( pItem->zName ){ |
| 28486 | + sqlite3_str_appendf(&x, " %s", pItem->zName); |
| 28487 | + } |
| 28488 | + if( pItem->pTab ){ |
| 28489 | + sqlite3_str_appendf(&x, " tabname=%Q", pItem->pTab->zName); |
| 28490 | + } |
| 28491 | + if( pItem->zAlias ){ |
| 28492 | + sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias); |
| 28493 | + } |
| 28494 | + if( pItem->fg.jointype & JT_LEFT ){ |
| 28495 | + sqlite3_str_appendf(&x, " LEFT-JOIN"); |
| 28496 | + } |
| 28497 | + sqlite3StrAccumFinish(&x); |
| 28498 | + sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1); |
| 28499 | + if( pItem->pSelect ){ |
| 28500 | + sqlite3TreeViewSelect(pView, pItem->pSelect, 0); |
| 28501 | + } |
| 28502 | + if( pItem->fg.isTabFunc ){ |
| 28503 | + sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); |
| 28504 | + } |
| 28505 | + sqlite3TreeViewPop(pView); |
| 28506 | + } |
| 28507 | +} |
| 28299 | 28508 | |
| 28300 | 28509 | /* |
| 28301 | 28510 | ** Generate a human-readable description of a Select object. |
| 28302 | 28511 | */ |
| 28303 | 28512 | SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){ |
| | @@ -28348,43 +28557,13 @@ |
| 28348 | 28557 | } |
| 28349 | 28558 | sqlite3TreeViewPop(pView); |
| 28350 | 28559 | } |
| 28351 | 28560 | #endif |
| 28352 | 28561 | if( p->pSrc && p->pSrc->nSrc ){ |
| 28353 | | - int i; |
| 28354 | 28562 | pView = sqlite3TreeViewPush(pView, (n--)>0); |
| 28355 | 28563 | sqlite3TreeViewLine(pView, "FROM"); |
| 28356 | | - for(i=0; i<p->pSrc->nSrc; i++){ |
| 28357 | | - struct SrcList_item *pItem = &p->pSrc->a[i]; |
| 28358 | | - StrAccum x; |
| 28359 | | - char zLine[100]; |
| 28360 | | - sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); |
| 28361 | | - sqlite3_str_appendf(&x, "{%d,*}", pItem->iCursor); |
| 28362 | | - if( pItem->zDatabase ){ |
| 28363 | | - sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName); |
| 28364 | | - }else if( pItem->zName ){ |
| 28365 | | - sqlite3_str_appendf(&x, " %s", pItem->zName); |
| 28366 | | - } |
| 28367 | | - if( pItem->pTab ){ |
| 28368 | | - sqlite3_str_appendf(&x, " tabname=%Q", pItem->pTab->zName); |
| 28369 | | - } |
| 28370 | | - if( pItem->zAlias ){ |
| 28371 | | - sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias); |
| 28372 | | - } |
| 28373 | | - if( pItem->fg.jointype & JT_LEFT ){ |
| 28374 | | - sqlite3_str_appendf(&x, " LEFT-JOIN"); |
| 28375 | | - } |
| 28376 | | - sqlite3StrAccumFinish(&x); |
| 28377 | | - sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); |
| 28378 | | - if( pItem->pSelect ){ |
| 28379 | | - sqlite3TreeViewSelect(pView, pItem->pSelect, 0); |
| 28380 | | - } |
| 28381 | | - if( pItem->fg.isTabFunc ){ |
| 28382 | | - sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); |
| 28383 | | - } |
| 28384 | | - sqlite3TreeViewPop(pView); |
| 28385 | | - } |
| 28564 | + sqlite3TreeViewSrcList(pView, p->pSrc); |
| 28386 | 28565 | sqlite3TreeViewPop(pView); |
| 28387 | 28566 | } |
| 28388 | 28567 | if( p->pWhere ){ |
| 28389 | 28568 | sqlite3TreeViewItem(pView, "WHERE", (n--)>0); |
| 28390 | 28569 | sqlite3TreeViewExpr(pView, p->pWhere, 0); |
| | @@ -28670,11 +28849,11 @@ |
| 28670 | 28849 | pFarg = 0; |
| 28671 | 28850 | pWin = 0; |
| 28672 | 28851 | }else{ |
| 28673 | 28852 | pFarg = pExpr->x.pList; |
| 28674 | 28853 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 28675 | | - pWin = pExpr->pWin; |
| 28854 | + pWin = pExpr->y.pWin; |
| 28676 | 28855 | #else |
| 28677 | 28856 | pWin = 0; |
| 28678 | 28857 | #endif |
| 28679 | 28858 | } |
| 28680 | 28859 | if( pExpr->op==TK_AGG_FUNCTION ){ |
| | @@ -31498,10 +31677,24 @@ |
| 31498 | 31677 | h += sqlite3UpperToLower[c]; |
| 31499 | 31678 | h *= 0x9e3779b1; |
| 31500 | 31679 | } |
| 31501 | 31680 | return h; |
| 31502 | 31681 | } |
| 31682 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 31683 | +static unsigned int strHashN(const char *z, int n){ |
| 31684 | + unsigned int h = 0; |
| 31685 | + int i; |
| 31686 | + for(i=0; i<n; i++){ |
| 31687 | + /* Knuth multiplicative hashing. (Sorting & Searching, p. 510). |
| 31688 | + ** 0x9e3779b1 is 2654435761 which is the closest prime number to |
| 31689 | + ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */ |
| 31690 | + h += sqlite3UpperToLower[z[i]]; |
| 31691 | + h *= 0x9e3779b1; |
| 31692 | + } |
| 31693 | + return h; |
| 31694 | +} |
| 31695 | +#endif /* SQLITE_ENABLE_NORMALIZE */ |
| 31503 | 31696 | |
| 31504 | 31697 | |
| 31505 | 31698 | /* Link pNew element into the hash table pH. If pEntry!=0 then also |
| 31506 | 31699 | ** insert pNew into the pEntry hash bucket. |
| 31507 | 31700 | */ |
| | @@ -31609,10 +31802,44 @@ |
| 31609 | 31802 | } |
| 31610 | 31803 | elem = elem->next; |
| 31611 | 31804 | } |
| 31612 | 31805 | return &nullElement; |
| 31613 | 31806 | } |
| 31807 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 31808 | +static HashElem *findElementWithHashN( |
| 31809 | + const Hash *pH, /* The pH to be searched */ |
| 31810 | + const char *pKey, /* The key we are searching for */ |
| 31811 | + int nKey, /* Number of key bytes to use */ |
| 31812 | + unsigned int *pHash /* Write the hash value here */ |
| 31813 | +){ |
| 31814 | + HashElem *elem; /* Used to loop thru the element list */ |
| 31815 | + int count; /* Number of elements left to test */ |
| 31816 | + unsigned int h; /* The computed hash */ |
| 31817 | + static HashElem nullElement = { 0, 0, 0, 0 }; |
| 31818 | + |
| 31819 | + if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/ |
| 31820 | + struct _ht *pEntry; |
| 31821 | + h = strHashN(pKey, nKey) % pH->htsize; |
| 31822 | + pEntry = &pH->ht[h]; |
| 31823 | + elem = pEntry->chain; |
| 31824 | + count = pEntry->count; |
| 31825 | + }else{ |
| 31826 | + h = 0; |
| 31827 | + elem = pH->first; |
| 31828 | + count = pH->count; |
| 31829 | + } |
| 31830 | + if( pHash ) *pHash = h; |
| 31831 | + while( count-- ){ |
| 31832 | + assert( elem!=0 ); |
| 31833 | + if( sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){ |
| 31834 | + return elem; |
| 31835 | + } |
| 31836 | + elem = elem->next; |
| 31837 | + } |
| 31838 | + return &nullElement; |
| 31839 | +} |
| 31840 | +#endif /* SQLITE_ENABLE_NORMALIZE */ |
| 31614 | 31841 | |
| 31615 | 31842 | /* Remove a single entry from the hash table given a pointer to that |
| 31616 | 31843 | ** element and a hash on the element's key. |
| 31617 | 31844 | */ |
| 31618 | 31845 | static void removeElementGivenHash( |
| | @@ -31653,10 +31880,18 @@ |
| 31653 | 31880 | SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){ |
| 31654 | 31881 | assert( pH!=0 ); |
| 31655 | 31882 | assert( pKey!=0 ); |
| 31656 | 31883 | return findElementWithHash(pH, pKey, 0)->data; |
| 31657 | 31884 | } |
| 31885 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 31886 | +SQLITE_PRIVATE void *sqlite3HashFindN(const Hash *pH, const char *pKey, int nKey){ |
| 31887 | + assert( pH!=0 ); |
| 31888 | + assert( pKey!=0 ); |
| 31889 | + assert( nKey>=0 ); |
| 31890 | + return findElementWithHashN(pH, pKey, nKey, 0)->data; |
| 31891 | +} |
| 31892 | +#endif /* SQLITE_ENABLE_NORMALIZE */ |
| 31658 | 31893 | |
| 31659 | 31894 | /* Insert an element into the hash table pH. The key is pKey |
| 31660 | 31895 | ** and the data is "data". |
| 31661 | 31896 | ** |
| 31662 | 31897 | ** If no element exists with a matching key, then a new |
| | @@ -32035,16 +32270,14 @@ |
| 32035 | 32270 | ** Allowed values of unixFile.fsFlags |
| 32036 | 32271 | */ |
| 32037 | 32272 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 32038 | 32273 | |
| 32039 | 32274 | /* |
| 32040 | | -** If we are to be thread-safe, include the pthreads header and define |
| 32041 | | -** the SQLITE_UNIX_THREADS macro. |
| 32275 | +** If we are to be thread-safe, include the pthreads header. |
| 32042 | 32276 | */ |
| 32043 | 32277 | #if SQLITE_THREADSAFE |
| 32044 | 32278 | /* # include <pthread.h> */ |
| 32045 | | -# define SQLITE_UNIX_THREADS 1 |
| 32046 | 32279 | #endif |
| 32047 | 32280 | |
| 32048 | 32281 | /* |
| 32049 | 32282 | ** Default permissions when creating a new file |
| 32050 | 32283 | */ |
| | @@ -33216,12 +33449,11 @@ |
| 33216 | 33449 | #endif |
| 33217 | 33450 | }; |
| 33218 | 33451 | |
| 33219 | 33452 | /* |
| 33220 | 33453 | ** An instance of the following structure is allocated for each open |
| 33221 | | -** inode. Or, on LinuxThreads, there is one of these structures for |
| 33222 | | -** each inode opened by each thread. |
| 33454 | +** inode. |
| 33223 | 33455 | ** |
| 33224 | 33456 | ** A single inode can have multiple file descriptors, so each unixFile |
| 33225 | 33457 | ** structure contains a pointer to an instance of this object and this |
| 33226 | 33458 | ** object keeps a count of the number of unixFile pointing to it. |
| 33227 | 33459 | ** |
| | @@ -33263,17 +33495,20 @@ |
| 33263 | 33495 | #endif |
| 33264 | 33496 | }; |
| 33265 | 33497 | |
| 33266 | 33498 | /* |
| 33267 | 33499 | ** A lists of all unixInodeInfo objects. |
| 33500 | +** |
| 33501 | +** Must hold unixBigLock in order to read or write this variable. |
| 33268 | 33502 | */ |
| 33269 | 33503 | static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */ |
| 33270 | 33504 | |
| 33271 | 33505 | #ifdef SQLITE_DEBUG |
| 33272 | 33506 | /* |
| 33273 | | -** True if the inode mutex is held, or not. Used only within assert() |
| 33274 | | -** to help verify correct mutex usage. |
| 33507 | +** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not. |
| 33508 | +** This routine is used only within assert() to help verify correct mutex |
| 33509 | +** usage. |
| 33275 | 33510 | */ |
| 33276 | 33511 | int unixFileMutexHeld(unixFile *pFile){ |
| 33277 | 33512 | assert( pFile->pInode ); |
| 33278 | 33513 | return sqlite3_mutex_held(pFile->pInode->pLockMutex); |
| 33279 | 33514 | } |
| | @@ -33397,12 +33632,12 @@ |
| 33397 | 33632 | } |
| 33398 | 33633 | |
| 33399 | 33634 | /* |
| 33400 | 33635 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
| 33401 | 33636 | ** |
| 33402 | | -** The mutex entered using the unixEnterMutex() function must be held |
| 33403 | | -** when this function is called. |
| 33637 | +** The global mutex must be held when this routine is called, but the mutex |
| 33638 | +** on the inode being deleted must NOT be held. |
| 33404 | 33639 | */ |
| 33405 | 33640 | static void releaseInodeInfo(unixFile *pFile){ |
| 33406 | 33641 | unixInodeInfo *pInode = pFile->pInode; |
| 33407 | 33642 | assert( unixMutexHeld() ); |
| 33408 | 33643 | assert( unixFileMutexNotheld(pFile) ); |
| | @@ -33433,12 +33668,11 @@ |
| 33433 | 33668 | /* |
| 33434 | 33669 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 33435 | 33670 | ** describes that file descriptor. Create a new one if necessary. The |
| 33436 | 33671 | ** return value might be uninitialized if an error occurs. |
| 33437 | 33672 | ** |
| 33438 | | -** The mutex entered using the unixEnterMutex() function must be held |
| 33439 | | -** when this function is called. |
| 33673 | +** The global mutex must held when calling this routine. |
| 33440 | 33674 | ** |
| 33441 | 33675 | ** Return an appropriate error code. |
| 33442 | 33676 | */ |
| 33443 | 33677 | static int findInodeInfo( |
| 33444 | 33678 | unixFile *pFile, /* Unix file with file desc used in the key */ |
| | @@ -33495,10 +33729,11 @@ |
| 33495 | 33729 | #if OS_VXWORKS |
| 33496 | 33730 | fileId.pId = pFile->pId; |
| 33497 | 33731 | #else |
| 33498 | 33732 | fileId.ino = (u64)statbuf.st_ino; |
| 33499 | 33733 | #endif |
| 33734 | + assert( unixMutexHeld() ); |
| 33500 | 33735 | pInode = inodeList; |
| 33501 | 33736 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 33502 | 33737 | pInode = pInode->pNext; |
| 33503 | 33738 | } |
| 33504 | 33739 | if( pInode==0 ){ |
| | @@ -33514,10 +33749,11 @@ |
| 33514 | 33749 | sqlite3_free(pInode); |
| 33515 | 33750 | return SQLITE_NOMEM_BKPT; |
| 33516 | 33751 | } |
| 33517 | 33752 | } |
| 33518 | 33753 | pInode->nRef = 1; |
| 33754 | + assert( unixMutexHeld() ); |
| 33519 | 33755 | pInode->pNext = inodeList; |
| 33520 | 33756 | pInode->pPrev = 0; |
| 33521 | 33757 | if( inodeList ) inodeList->pPrev = pInode; |
| 33522 | 33758 | inodeList = pInode; |
| 33523 | 33759 | }else{ |
| | @@ -36311,22 +36547,22 @@ |
| 36311 | 36547 | ** |
| 36312 | 36548 | ** nRef |
| 36313 | 36549 | ** |
| 36314 | 36550 | ** The following fields are read-only after the object is created: |
| 36315 | 36551 | ** |
| 36316 | | -** fid |
| 36552 | +** hShm |
| 36317 | 36553 | ** zFilename |
| 36318 | 36554 | ** |
| 36319 | | -** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
| 36555 | +** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and |
| 36320 | 36556 | ** unixMutexHeld() is true when reading or writing any other field |
| 36321 | 36557 | ** in this structure. |
| 36322 | 36558 | */ |
| 36323 | 36559 | struct unixShmNode { |
| 36324 | 36560 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
| 36325 | | - sqlite3_mutex *mutex; /* Mutex to access this object */ |
| 36561 | + sqlite3_mutex *pShmMutex; /* Mutex to access this object */ |
| 36326 | 36562 | char *zFilename; /* Name of the mmapped file */ |
| 36327 | | - int h; /* Open file descriptor */ |
| 36563 | + int hShm; /* Open file descriptor */ |
| 36328 | 36564 | int szRegion; /* Size of shared-memory regions */ |
| 36329 | 36565 | u16 nRegion; /* Size of array apRegion */ |
| 36330 | 36566 | u8 isReadonly; /* True if read-only */ |
| 36331 | 36567 | u8 isUnlocked; /* True if no DMS lock held */ |
| 36332 | 36568 | char **apRegion; /* Array of mapped shared-memory regions */ |
| | @@ -36344,20 +36580,20 @@ |
| 36344 | 36580 | ** open shared memory connection. |
| 36345 | 36581 | ** |
| 36346 | 36582 | ** The following fields are initialized when this object is created and |
| 36347 | 36583 | ** are read-only thereafter: |
| 36348 | 36584 | ** |
| 36349 | | -** unixShm.pFile |
| 36585 | +** unixShm.pShmNode |
| 36350 | 36586 | ** unixShm.id |
| 36351 | 36587 | ** |
| 36352 | | -** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 36353 | | -** while accessing any read/write fields. |
| 36588 | +** All other fields are read/write. The unixShm.pShmNode->pShmMutex must |
| 36589 | +** be held while accessing any read/write fields. |
| 36354 | 36590 | */ |
| 36355 | 36591 | struct unixShm { |
| 36356 | 36592 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 36357 | 36593 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
| 36358 | | - u8 hasMutex; /* True if holding the unixShmNode mutex */ |
| 36594 | + u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */ |
| 36359 | 36595 | u8 id; /* Id of this connection within its unixShmNode */ |
| 36360 | 36596 | u16 sharedMask; /* Mask of shared locks held */ |
| 36361 | 36597 | u16 exclMask; /* Mask of exclusive locks held */ |
| 36362 | 36598 | }; |
| 36363 | 36599 | |
| | @@ -36383,25 +36619,26 @@ |
| 36383 | 36619 | struct flock f; /* The posix advisory locking structure */ |
| 36384 | 36620 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
| 36385 | 36621 | |
| 36386 | 36622 | /* Access to the unixShmNode object is serialized by the caller */ |
| 36387 | 36623 | pShmNode = pFile->pInode->pShmNode; |
| 36388 | | - assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->mutex) ); |
| 36624 | + assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) ); |
| 36625 | + assert( pShmNode->nRef>0 || unixMutexHeld() ); |
| 36389 | 36626 | |
| 36390 | 36627 | /* Shared locks never span more than one byte */ |
| 36391 | 36628 | assert( n==1 || lockType!=F_RDLCK ); |
| 36392 | 36629 | |
| 36393 | 36630 | /* Locks are within range */ |
| 36394 | 36631 | assert( n>=1 && n<=SQLITE_SHM_NLOCK ); |
| 36395 | 36632 | |
| 36396 | | - if( pShmNode->h>=0 ){ |
| 36633 | + if( pShmNode->hShm>=0 ){ |
| 36397 | 36634 | /* Initialize the locking parameters */ |
| 36398 | 36635 | f.l_type = lockType; |
| 36399 | 36636 | f.l_whence = SEEK_SET; |
| 36400 | 36637 | f.l_start = ofst; |
| 36401 | 36638 | f.l_len = n; |
| 36402 | | - rc = osSetPosixAdvisoryLock(pShmNode->h, &f, pFile); |
| 36639 | + rc = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile); |
| 36403 | 36640 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 36404 | 36641 | } |
| 36405 | 36642 | |
| 36406 | 36643 | /* Update the global lock state and do debug tracing */ |
| 36407 | 36644 | #ifdef SQLITE_DEBUG |
| | @@ -36469,22 +36706,22 @@ |
| 36469 | 36706 | assert( unixMutexHeld() ); |
| 36470 | 36707 | if( p && ALWAYS(p->nRef==0) ){ |
| 36471 | 36708 | int nShmPerMap = unixShmRegionPerMap(); |
| 36472 | 36709 | int i; |
| 36473 | 36710 | assert( p->pInode==pFd->pInode ); |
| 36474 | | - sqlite3_mutex_free(p->mutex); |
| 36711 | + sqlite3_mutex_free(p->pShmMutex); |
| 36475 | 36712 | for(i=0; i<p->nRegion; i+=nShmPerMap){ |
| 36476 | | - if( p->h>=0 ){ |
| 36713 | + if( p->hShm>=0 ){ |
| 36477 | 36714 | osMunmap(p->apRegion[i], p->szRegion); |
| 36478 | 36715 | }else{ |
| 36479 | 36716 | sqlite3_free(p->apRegion[i]); |
| 36480 | 36717 | } |
| 36481 | 36718 | } |
| 36482 | 36719 | sqlite3_free(p->apRegion); |
| 36483 | | - if( p->h>=0 ){ |
| 36484 | | - robust_close(pFd, p->h, __LINE__); |
| 36485 | | - p->h = -1; |
| 36720 | + if( p->hShm>=0 ){ |
| 36721 | + robust_close(pFd, p->hShm, __LINE__); |
| 36722 | + p->hShm = -1; |
| 36486 | 36723 | } |
| 36487 | 36724 | p->pInode->pShmNode = 0; |
| 36488 | 36725 | sqlite3_free(p); |
| 36489 | 36726 | } |
| 36490 | 36727 | } |
| | @@ -36522,19 +36759,24 @@ |
| 36522 | 36759 | ** system crash, the database itself may also become corrupt. */ |
| 36523 | 36760 | lock.l_whence = SEEK_SET; |
| 36524 | 36761 | lock.l_start = UNIX_SHM_DMS; |
| 36525 | 36762 | lock.l_len = 1; |
| 36526 | 36763 | lock.l_type = F_WRLCK; |
| 36527 | | - if( osFcntl(pShmNode->h, F_GETLK, &lock)!=0 ) { |
| 36764 | + if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) { |
| 36528 | 36765 | rc = SQLITE_IOERR_LOCK; |
| 36529 | 36766 | }else if( lock.l_type==F_UNLCK ){ |
| 36530 | 36767 | if( pShmNode->isReadonly ){ |
| 36531 | 36768 | pShmNode->isUnlocked = 1; |
| 36532 | 36769 | rc = SQLITE_READONLY_CANTINIT; |
| 36533 | 36770 | }else{ |
| 36534 | 36771 | rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1); |
| 36535 | | - if( rc==SQLITE_OK && robust_ftruncate(pShmNode->h, 0) ){ |
| 36772 | + /* The first connection to attach must truncate the -shm file. We |
| 36773 | + ** truncate to 3 bytes (an arbitrary small number, less than the |
| 36774 | + ** -shm header size) rather than 0 as a system debugging aid, to |
| 36775 | + ** help detect if a -shm file truncation is legitimate or is the work |
| 36776 | + ** or a rogue process. */ |
| 36777 | + if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){ |
| 36536 | 36778 | rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename); |
| 36537 | 36779 | } |
| 36538 | 36780 | } |
| 36539 | 36781 | }else if( lock.l_type==F_WRLCK ){ |
| 36540 | 36782 | rc = SQLITE_BUSY; |
| | @@ -36636,28 +36878,28 @@ |
| 36636 | 36878 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 36637 | 36879 | #else |
| 36638 | 36880 | sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath); |
| 36639 | 36881 | sqlite3FileSuffix3(pDbFd->zPath, zShm); |
| 36640 | 36882 | #endif |
| 36641 | | - pShmNode->h = -1; |
| 36883 | + pShmNode->hShm = -1; |
| 36642 | 36884 | pDbFd->pInode->pShmNode = pShmNode; |
| 36643 | 36885 | pShmNode->pInode = pDbFd->pInode; |
| 36644 | 36886 | if( sqlite3GlobalConfig.bCoreMutex ){ |
| 36645 | | - pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 36646 | | - if( pShmNode->mutex==0 ){ |
| 36887 | + pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 36888 | + if( pShmNode->pShmMutex==0 ){ |
| 36647 | 36889 | rc = SQLITE_NOMEM_BKPT; |
| 36648 | 36890 | goto shm_open_err; |
| 36649 | 36891 | } |
| 36650 | 36892 | } |
| 36651 | 36893 | |
| 36652 | 36894 | if( pInode->bProcessLock==0 ){ |
| 36653 | 36895 | if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ |
| 36654 | | - pShmNode->h = robust_open(zShm, O_RDWR|O_CREAT, (sStat.st_mode&0777)); |
| 36896 | + pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT,(sStat.st_mode&0777)); |
| 36655 | 36897 | } |
| 36656 | | - if( pShmNode->h<0 ){ |
| 36657 | | - pShmNode->h = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777)); |
| 36658 | | - if( pShmNode->h<0 ){ |
| 36898 | + if( pShmNode->hShm<0 ){ |
| 36899 | + pShmNode->hShm = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777)); |
| 36900 | + if( pShmNode->hShm<0 ){ |
| 36659 | 36901 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm); |
| 36660 | 36902 | goto shm_open_err; |
| 36661 | 36903 | } |
| 36662 | 36904 | pShmNode->isReadonly = 1; |
| 36663 | 36905 | } |
| | @@ -36664,11 +36906,11 @@ |
| 36664 | 36906 | |
| 36665 | 36907 | /* If this process is running as root, make sure that the SHM file |
| 36666 | 36908 | ** is owned by the same user that owns the original database. Otherwise, |
| 36667 | 36909 | ** the original owner will not be able to connect. |
| 36668 | 36910 | */ |
| 36669 | | - robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid); |
| 36911 | + robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid); |
| 36670 | 36912 | |
| 36671 | 36913 | rc = unixLockSharedMemory(pDbFd, pShmNode); |
| 36672 | 36914 | if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err; |
| 36673 | 36915 | } |
| 36674 | 36916 | } |
| | @@ -36684,17 +36926,17 @@ |
| 36684 | 36926 | |
| 36685 | 36927 | /* The reference count on pShmNode has already been incremented under |
| 36686 | 36928 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 36687 | 36929 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 36688 | 36930 | ** left to do is to link the new object into the linked list starting |
| 36689 | | - ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 36690 | | - ** mutex. |
| 36931 | + ** at pShmNode->pFirst. This must be done while holding the |
| 36932 | + ** pShmNode->pShmMutex. |
| 36691 | 36933 | */ |
| 36692 | | - sqlite3_mutex_enter(pShmNode->mutex); |
| 36934 | + sqlite3_mutex_enter(pShmNode->pShmMutex); |
| 36693 | 36935 | p->pNext = pShmNode->pFirst; |
| 36694 | 36936 | pShmNode->pFirst = p; |
| 36695 | | - sqlite3_mutex_leave(pShmNode->mutex); |
| 36937 | + sqlite3_mutex_leave(pShmNode->pShmMutex); |
| 36696 | 36938 | return rc; |
| 36697 | 36939 | |
| 36698 | 36940 | /* Jump here on any error */ |
| 36699 | 36941 | shm_open_err: |
| 36700 | 36942 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
| | @@ -36742,20 +36984,20 @@ |
| 36742 | 36984 | if( rc!=SQLITE_OK ) return rc; |
| 36743 | 36985 | } |
| 36744 | 36986 | |
| 36745 | 36987 | p = pDbFd->pShm; |
| 36746 | 36988 | pShmNode = p->pShmNode; |
| 36747 | | - sqlite3_mutex_enter(pShmNode->mutex); |
| 36989 | + sqlite3_mutex_enter(pShmNode->pShmMutex); |
| 36748 | 36990 | if( pShmNode->isUnlocked ){ |
| 36749 | 36991 | rc = unixLockSharedMemory(pDbFd, pShmNode); |
| 36750 | 36992 | if( rc!=SQLITE_OK ) goto shmpage_out; |
| 36751 | 36993 | pShmNode->isUnlocked = 0; |
| 36752 | 36994 | } |
| 36753 | 36995 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
| 36754 | 36996 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 36755 | | - assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 36756 | | - assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
| 36997 | + assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 36998 | + assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); |
| 36757 | 36999 | |
| 36758 | 37000 | /* Minimum number of regions required to be mapped. */ |
| 36759 | 37001 | nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap; |
| 36760 | 37002 | |
| 36761 | 37003 | if( pShmNode->nRegion<nReqRegion ){ |
| | @@ -36763,16 +37005,16 @@ |
| 36763 | 37005 | int nByte = nReqRegion*szRegion; /* Minimum required file size */ |
| 36764 | 37006 | struct stat sStat; /* Used by fstat() */ |
| 36765 | 37007 | |
| 36766 | 37008 | pShmNode->szRegion = szRegion; |
| 36767 | 37009 | |
| 36768 | | - if( pShmNode->h>=0 ){ |
| 37010 | + if( pShmNode->hShm>=0 ){ |
| 36769 | 37011 | /* The requested region is not mapped into this processes address space. |
| 36770 | 37012 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 36771 | 37013 | ** large enough to contain the requested region). |
| 36772 | 37014 | */ |
| 36773 | | - if( osFstat(pShmNode->h, &sStat) ){ |
| 37015 | + if( osFstat(pShmNode->hShm, &sStat) ){ |
| 36774 | 37016 | rc = SQLITE_IOERR_SHMSIZE; |
| 36775 | 37017 | goto shmpage_out; |
| 36776 | 37018 | } |
| 36777 | 37019 | |
| 36778 | 37020 | if( sStat.st_size<nByte ){ |
| | @@ -36796,11 +37038,11 @@ |
| 36796 | 37038 | |
| 36797 | 37039 | /* Write to the last byte of each newly allocated or extended page */ |
| 36798 | 37040 | assert( (nByte % pgsz)==0 ); |
| 36799 | 37041 | for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){ |
| 36800 | 37042 | int x = 0; |
| 36801 | | - if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){ |
| 37043 | + if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){ |
| 36802 | 37044 | const char *zFile = pShmNode->zFilename; |
| 36803 | 37045 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile); |
| 36804 | 37046 | goto shmpage_out; |
| 36805 | 37047 | } |
| 36806 | 37048 | } |
| | @@ -36819,26 +37061,26 @@ |
| 36819 | 37061 | pShmNode->apRegion = apNew; |
| 36820 | 37062 | while( pShmNode->nRegion<nReqRegion ){ |
| 36821 | 37063 | int nMap = szRegion*nShmPerMap; |
| 36822 | 37064 | int i; |
| 36823 | 37065 | void *pMem; |
| 36824 | | - if( pShmNode->h>=0 ){ |
| 37066 | + if( pShmNode->hShm>=0 ){ |
| 36825 | 37067 | pMem = osMmap(0, nMap, |
| 36826 | 37068 | pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, |
| 36827 | | - MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion |
| 37069 | + MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion |
| 36828 | 37070 | ); |
| 36829 | 37071 | if( pMem==MAP_FAILED ){ |
| 36830 | 37072 | rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
| 36831 | 37073 | goto shmpage_out; |
| 36832 | 37074 | } |
| 36833 | 37075 | }else{ |
| 36834 | | - pMem = sqlite3_malloc64(szRegion); |
| 37076 | + pMem = sqlite3_malloc64(nMap); |
| 36835 | 37077 | if( pMem==0 ){ |
| 36836 | 37078 | rc = SQLITE_NOMEM_BKPT; |
| 36837 | 37079 | goto shmpage_out; |
| 36838 | 37080 | } |
| 36839 | | - memset(pMem, 0, szRegion); |
| 37081 | + memset(pMem, 0, nMap); |
| 36840 | 37082 | } |
| 36841 | 37083 | |
| 36842 | 37084 | for(i=0; i<nShmPerMap; i++){ |
| 36843 | 37085 | pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i]; |
| 36844 | 37086 | } |
| | @@ -36851,11 +37093,11 @@ |
| 36851 | 37093 | *pp = pShmNode->apRegion[iRegion]; |
| 36852 | 37094 | }else{ |
| 36853 | 37095 | *pp = 0; |
| 36854 | 37096 | } |
| 36855 | 37097 | if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; |
| 36856 | | - sqlite3_mutex_leave(pShmNode->mutex); |
| 37098 | + sqlite3_mutex_leave(pShmNode->pShmMutex); |
| 36857 | 37099 | return rc; |
| 36858 | 37100 | } |
| 36859 | 37101 | |
| 36860 | 37102 | /* |
| 36861 | 37103 | ** Change the lock state for a shared-memory segment. |
| | @@ -36885,16 +37127,16 @@ |
| 36885 | 37127 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 36886 | 37128 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 36887 | 37129 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 36888 | 37130 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 36889 | 37131 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
| 36890 | | - assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 36891 | | - assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
| 37132 | + assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 37133 | + assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); |
| 36892 | 37134 | |
| 36893 | 37135 | mask = (1<<(ofst+n)) - (1<<ofst); |
| 36894 | 37136 | assert( n>1 || mask==(1<<ofst) ); |
| 36895 | | - sqlite3_mutex_enter(pShmNode->mutex); |
| 37137 | + sqlite3_mutex_enter(pShmNode->pShmMutex); |
| 36896 | 37138 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 36897 | 37139 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 36898 | 37140 | |
| 36899 | 37141 | /* See if any siblings hold this same lock */ |
| 36900 | 37142 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| | @@ -36963,11 +37205,11 @@ |
| 36963 | 37205 | assert( (p->sharedMask & mask)==0 ); |
| 36964 | 37206 | p->exclMask |= mask; |
| 36965 | 37207 | } |
| 36966 | 37208 | } |
| 36967 | 37209 | } |
| 36968 | | - sqlite3_mutex_leave(pShmNode->mutex); |
| 37210 | + sqlite3_mutex_leave(pShmNode->pShmMutex); |
| 36969 | 37211 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
| 36970 | 37212 | p->id, osGetpid(0), p->sharedMask, p->exclMask)); |
| 36971 | 37213 | return rc; |
| 36972 | 37214 | } |
| 36973 | 37215 | |
| | @@ -37013,27 +37255,27 @@ |
| 37013 | 37255 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 37014 | 37256 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 37015 | 37257 | |
| 37016 | 37258 | /* Remove connection p from the set of connections associated |
| 37017 | 37259 | ** with pShmNode */ |
| 37018 | | - sqlite3_mutex_enter(pShmNode->mutex); |
| 37260 | + sqlite3_mutex_enter(pShmNode->pShmMutex); |
| 37019 | 37261 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 37020 | 37262 | *pp = p->pNext; |
| 37021 | 37263 | |
| 37022 | 37264 | /* Free the connection p */ |
| 37023 | 37265 | sqlite3_free(p); |
| 37024 | 37266 | pDbFd->pShm = 0; |
| 37025 | | - sqlite3_mutex_leave(pShmNode->mutex); |
| 37267 | + sqlite3_mutex_leave(pShmNode->pShmMutex); |
| 37026 | 37268 | |
| 37027 | 37269 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 37028 | 37270 | ** shared-memory file, too */ |
| 37029 | 37271 | assert( unixFileMutexNotheld(pDbFd) ); |
| 37030 | 37272 | unixEnterMutex(); |
| 37031 | 37273 | assert( pShmNode->nRef>0 ); |
| 37032 | 37274 | pShmNode->nRef--; |
| 37033 | 37275 | if( pShmNode->nRef==0 ){ |
| 37034 | | - if( deleteFlag && pShmNode->h>=0 ){ |
| 37276 | + if( deleteFlag && pShmNode->hShm>=0 ){ |
| 37035 | 37277 | osUnlink(pShmNode->zFilename); |
| 37036 | 37278 | } |
| 37037 | 37279 | unixShmPurge(pDbFd); |
| 37038 | 37280 | } |
| 37039 | 37281 | unixLeaveMutex(); |
| | @@ -40447,12 +40689,11 @@ |
| 40447 | 40689 | #endif |
| 40448 | 40690 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 40449 | 40691 | int nFetchOut; /* Number of outstanding xFetch references */ |
| 40450 | 40692 | HANDLE hMap; /* Handle for accessing memory mapping */ |
| 40451 | 40693 | void *pMapRegion; /* Area memory mapped */ |
| 40452 | | - sqlite3_int64 mmapSize; /* Usable size of mapped region */ |
| 40453 | | - sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */ |
| 40694 | + sqlite3_int64 mmapSize; /* Size of mapped region */ |
| 40454 | 40695 | sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ |
| 40455 | 40696 | #endif |
| 40456 | 40697 | }; |
| 40457 | 40698 | |
| 40458 | 40699 | /* |
| | @@ -43069,10 +43310,30 @@ |
| 43069 | 43310 | winFile *pFile = (winFile*)id; /* File handle object */ |
| 43070 | 43311 | int rc = SQLITE_OK; /* Return code for this function */ |
| 43071 | 43312 | DWORD lastErrno; |
| 43072 | 43313 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 43073 | 43314 | sqlite3_int64 oldMmapSize; |
| 43315 | + if( pFile->nFetchOut>0 ){ |
| 43316 | + /* File truncation is a no-op if there are outstanding memory mapped |
| 43317 | + ** pages. This is because truncating the file means temporarily unmapping |
| 43318 | + ** the file, and that might delete memory out from under existing cursors. |
| 43319 | + ** |
| 43320 | + ** This can result in incremental vacuum not truncating the file, |
| 43321 | + ** if there is an active read cursor when the incremental vacuum occurs. |
| 43322 | + ** No real harm comes of this - the database file is not corrupted, |
| 43323 | + ** though some folks might complain that the file is bigger than it |
| 43324 | + ** needs to be. |
| 43325 | + ** |
| 43326 | + ** The only feasible work-around is to defer the truncation until after |
| 43327 | + ** all references to memory-mapped content are closed. That is doable, |
| 43328 | + ** but involves adding a few branches in the common write code path which |
| 43329 | + ** could slow down normal operations slightly. Hence, we have decided for |
| 43330 | + ** now to simply make trancations a no-op if there are pending reads. We |
| 43331 | + ** can maybe revisit this decision in the future. |
| 43332 | + */ |
| 43333 | + return SQLITE_OK; |
| 43334 | + } |
| 43074 | 43335 | #endif |
| 43075 | 43336 | |
| 43076 | 43337 | assert( pFile ); |
| 43077 | 43338 | SimulateIOError(return SQLITE_IOERR_TRUNCATE); |
| 43078 | 43339 | OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n", |
| | @@ -44497,13 +44758,13 @@ |
| 44497 | 44758 | */ |
| 44498 | 44759 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 44499 | 44760 | static int winUnmapfile(winFile *pFile){ |
| 44500 | 44761 | assert( pFile!=0 ); |
| 44501 | 44762 | OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, " |
| 44502 | | - "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n", |
| 44763 | + "mmapSize=%lld, mmapSizeMax=%lld\n", |
| 44503 | 44764 | osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion, |
| 44504 | | - pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax)); |
| 44765 | + pFile->mmapSize, pFile->mmapSizeMax)); |
| 44505 | 44766 | if( pFile->pMapRegion ){ |
| 44506 | 44767 | if( !osUnmapViewOfFile(pFile->pMapRegion) ){ |
| 44507 | 44768 | pFile->lastErrno = osGetLastError(); |
| 44508 | 44769 | OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, " |
| 44509 | 44770 | "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile, |
| | @@ -44511,11 +44772,10 @@ |
| 44511 | 44772 | return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno, |
| 44512 | 44773 | "winUnmapfile1", pFile->zPath); |
| 44513 | 44774 | } |
| 44514 | 44775 | pFile->pMapRegion = 0; |
| 44515 | 44776 | pFile->mmapSize = 0; |
| 44516 | | - pFile->mmapSizeActual = 0; |
| 44517 | 44777 | } |
| 44518 | 44778 | if( pFile->hMap!=NULL ){ |
| 44519 | 44779 | if( !osCloseHandle(pFile->hMap) ){ |
| 44520 | 44780 | pFile->lastErrno = osGetLastError(); |
| 44521 | 44781 | OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n", |
| | @@ -44622,11 +44882,10 @@ |
| 44622 | 44882 | osGetCurrentProcessId(), pFd, sqlite3ErrName(rc))); |
| 44623 | 44883 | return SQLITE_OK; |
| 44624 | 44884 | } |
| 44625 | 44885 | pFd->pMapRegion = pNew; |
| 44626 | 44886 | pFd->mmapSize = nMap; |
| 44627 | | - pFd->mmapSizeActual = nMap; |
| 44628 | 44887 | } |
| 44629 | 44888 | |
| 44630 | 44889 | OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n", |
| 44631 | 44890 | osGetCurrentProcessId(), pFd)); |
| 44632 | 44891 | return SQLITE_OK; |
| | @@ -45424,11 +45683,10 @@ |
| 45424 | 45683 | pFile->zPath = zName; |
| 45425 | 45684 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 45426 | 45685 | pFile->hMap = NULL; |
| 45427 | 45686 | pFile->pMapRegion = 0; |
| 45428 | 45687 | pFile->mmapSize = 0; |
| 45429 | | - pFile->mmapSizeActual = 0; |
| 45430 | 45688 | pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
| 45431 | 45689 | #endif |
| 45432 | 45690 | |
| 45433 | 45691 | OpenCounter(+1); |
| 45434 | 45692 | return rc; |
| | @@ -47321,11 +47579,11 @@ |
| 47321 | 47579 | ** pDirtyTail to the last (oldest). |
| 47322 | 47580 | ** |
| 47323 | 47581 | ** The PCache.pSynced variable is used to optimize searching for a dirty |
| 47324 | 47582 | ** page to eject from the cache mid-transaction. It is better to eject |
| 47325 | 47583 | ** a page that does not require a journal sync than one that does. |
| 47326 | | -** Therefore, pSynced is maintained to that it *almost* always points |
| 47584 | +** Therefore, pSynced is maintained so that it *almost* always points |
| 47327 | 47585 | ** to either the oldest page in the pDirty/pDirtyTail list that has a |
| 47328 | 47586 | ** clear PGHDR_NEED_SYNC flag or to a page that is older than this one |
| 47329 | 47587 | ** (so that the right page to eject can be found by following pDirtyPrev |
| 47330 | 47588 | ** pointers). |
| 47331 | 47589 | */ |
| | @@ -48144,10 +48402,19 @@ |
| 48144 | 48402 | int nDirty = 0; |
| 48145 | 48403 | int nCache = numberOfCachePages(pCache); |
| 48146 | 48404 | for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++; |
| 48147 | 48405 | return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0; |
| 48148 | 48406 | } |
| 48407 | + |
| 48408 | +#ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 48409 | +/* |
| 48410 | +** Return true if there are one or more dirty pages in the cache. Else false. |
| 48411 | +*/ |
| 48412 | +SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache){ |
| 48413 | + return (pCache->pDirty!=0); |
| 48414 | +} |
| 48415 | +#endif |
| 48149 | 48416 | |
| 48150 | 48417 | #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) |
| 48151 | 48418 | /* |
| 48152 | 48419 | ** For all dirty pages currently in the cache, invoke the specified |
| 48153 | 48420 | ** callback. This is only used if the SQLITE_CHECK_PAGES macro is |
| | @@ -48268,11 +48535,12 @@ |
| 48268 | 48535 | PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */ |
| 48269 | 48536 | PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */ |
| 48270 | 48537 | }; |
| 48271 | 48538 | |
| 48272 | 48539 | /* |
| 48273 | | -** A page is pinned if it is no on the LRU list |
| 48540 | +** A page is pinned if it is not on the LRU list. To be "pinned" means |
| 48541 | +** that the page is in active use and must not be deallocated. |
| 48274 | 48542 | */ |
| 48275 | 48543 | #define PAGE_IS_PINNED(p) ((p)->pLruNext==0) |
| 48276 | 48544 | #define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0) |
| 48277 | 48545 | |
| 48278 | 48546 | /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set |
| | @@ -50908,23 +51176,34 @@ |
| 50908 | 51176 | ** |
| 50909 | 51177 | ** if( pPager->jfd->pMethods ){ ... |
| 50910 | 51178 | */ |
| 50911 | 51179 | #define isOpen(pFd) ((pFd)->pMethods!=0) |
| 50912 | 51180 | |
| 51181 | +#ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 50913 | 51182 | /* |
| 50914 | | -** Return true if this pager uses a write-ahead log to read page pgno. |
| 50915 | | -** Return false if the pager reads pgno directly from the database. |
| 51183 | +** Return true if page pgno can be read directly from the database file |
| 51184 | +** by the b-tree layer. This is the case if: |
| 51185 | +** |
| 51186 | +** * the database file is open, |
| 51187 | +** * there are no dirty pages in the cache, and |
| 51188 | +** * the desired page is not currently in the wal file. |
| 50916 | 51189 | */ |
| 50917 | | -#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_DIRECT_OVERFLOW_READ) |
| 50918 | | -SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno pgno){ |
| 50919 | | - u32 iRead = 0; |
| 50920 | | - int rc; |
| 50921 | | - if( pPager->pWal==0 ) return 0; |
| 50922 | | - rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead); |
| 50923 | | - return rc || iRead; |
| 51190 | +SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){ |
| 51191 | + if( pPager->fd->pMethods==0 ) return 0; |
| 51192 | + if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0; |
| 51193 | +#ifndef SQLITE_OMIT_WAL |
| 51194 | + if( pPager->pWal ){ |
| 51195 | + u32 iRead = 0; |
| 51196 | + int rc; |
| 51197 | + rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead); |
| 51198 | + return (rc==SQLITE_OK && iRead==0); |
| 51199 | + } |
| 51200 | +#endif |
| 51201 | + return 1; |
| 50924 | 51202 | } |
| 50925 | 51203 | #endif |
| 51204 | + |
| 50926 | 51205 | #ifndef SQLITE_OMIT_WAL |
| 50927 | 51206 | # define pagerUseWal(x) ((x)->pWal!=0) |
| 50928 | 51207 | #else |
| 50929 | 51208 | # define pagerUseWal(x) 0 |
| 50930 | 51209 | # define pagerRollbackWal(x) 0 |
| | @@ -57104,11 +57383,15 @@ |
| 57104 | 57383 | void *(*xCodec)(void*,void*,Pgno,int), |
| 57105 | 57384 | void (*xCodecSizeChng)(void*,int,int), |
| 57106 | 57385 | void (*xCodecFree)(void*), |
| 57107 | 57386 | void *pCodec |
| 57108 | 57387 | ){ |
| 57109 | | - if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec); |
| 57388 | + if( pPager->xCodecFree ){ |
| 57389 | + pPager->xCodecFree(pPager->pCodec); |
| 57390 | + }else{ |
| 57391 | + pager_reset(pPager); |
| 57392 | + } |
| 57110 | 57393 | pPager->xCodec = pPager->memDb ? 0 : xCodec; |
| 57111 | 57394 | pPager->xCodecSizeChng = xCodecSizeChng; |
| 57112 | 57395 | pPager->xCodecFree = xCodecFree; |
| 57113 | 57396 | pPager->pCodec = pCodec; |
| 57114 | 57397 | setGetterMethod(pPager); |
| | @@ -65764,11 +66047,11 @@ |
| 65764 | 66047 | freeTempSpace(pBt); |
| 65765 | 66048 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, |
| 65766 | 66049 | pageSize-usableSize); |
| 65767 | 66050 | return rc; |
| 65768 | 66051 | } |
| 65769 | | - if( (pBt->db->flags & SQLITE_WriteSchema)==0 && nPage>nPageFile ){ |
| 66052 | + if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){ |
| 65770 | 66053 | rc = SQLITE_CORRUPT_BKPT; |
| 65771 | 66054 | goto page1_init_failed; |
| 65772 | 66055 | } |
| 65773 | 66056 | /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to |
| 65774 | 66057 | ** be less than 480. In other words, if the page size is 512, then the |
| | @@ -66238,10 +66521,11 @@ |
| 66238 | 66521 | |
| 66239 | 66522 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 66240 | 66523 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
| 66241 | 66524 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 66242 | 66525 | assert( pDbPage->pBt==pBt ); |
| 66526 | + if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT; |
| 66243 | 66527 | |
| 66244 | 66528 | /* Move page iDbPage from its current location to page number iFreePage */ |
| 66245 | 66529 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 66246 | 66530 | iDbPage, iFreePage, iPtrPage, eType)); |
| 66247 | 66531 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); |
| | @@ -67409,13 +67693,10 @@ |
| 67409 | 67693 | offset -= ovflSize; |
| 67410 | 67694 | }else{ |
| 67411 | 67695 | /* Need to read this page properly. It contains some of the |
| 67412 | 67696 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
| 67413 | 67697 | */ |
| 67414 | | -#ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 67415 | | - sqlite3_file *fd; /* File from which to do direct overflow read */ |
| 67416 | | -#endif |
| 67417 | 67698 | int a = amt; |
| 67418 | 67699 | if( a + offset > ovflSize ){ |
| 67419 | 67700 | a = ovflSize - offset; |
| 67420 | 67701 | } |
| 67421 | 67702 | |
| | @@ -67422,11 +67703,11 @@ |
| 67422 | 67703 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 67423 | 67704 | /* If all the following are true: |
| 67424 | 67705 | ** |
| 67425 | 67706 | ** 1) this is a read operation, and |
| 67426 | 67707 | ** 2) data is required from the start of this overflow page, and |
| 67427 | | - ** 3) there is no open write-transaction, and |
| 67708 | + ** 3) there are no dirty pages in the page-cache |
| 67428 | 67709 | ** 4) the database is file-backed, and |
| 67429 | 67710 | ** 5) the page is not in the WAL file |
| 67430 | 67711 | ** 6) at least 4 bytes have already been read into the output buffer |
| 67431 | 67712 | ** |
| 67432 | 67713 | ** then data can be read directly from the database file into the |
| | @@ -67433,15 +67714,14 @@ |
| 67433 | 67714 | ** output buffer, bypassing the page-cache altogether. This speeds |
| 67434 | 67715 | ** up loading large records that span many overflow pages. |
| 67435 | 67716 | */ |
| 67436 | 67717 | if( eOp==0 /* (1) */ |
| 67437 | 67718 | && offset==0 /* (2) */ |
| 67438 | | - && pBt->inTransaction==TRANS_READ /* (3) */ |
| 67439 | | - && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (4) */ |
| 67440 | | - && 0==sqlite3PagerUseWal(pBt->pPager, nextPage) /* (5) */ |
| 67719 | + && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */ |
| 67441 | 67720 | && &pBuf[-4]>=pBufStart /* (6) */ |
| 67442 | 67721 | ){ |
| 67722 | + sqlite3_file *fd = sqlite3PagerFile(pBt->pPager); |
| 67443 | 67723 | u8 aSave[4]; |
| 67444 | 67724 | u8 *aWrite = &pBuf[-4]; |
| 67445 | 67725 | assert( aWrite>=pBufStart ); /* due to (6) */ |
| 67446 | 67726 | memcpy(aSave, aWrite, 4); |
| 67447 | 67727 | rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1)); |
| | @@ -74033,11 +74313,12 @@ |
| 74033 | 74313 | sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i); |
| 74034 | 74314 | }else{ |
| 74035 | 74315 | assert( fg & MEM_Real ); |
| 74036 | 74316 | sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r); |
| 74037 | 74317 | } |
| 74038 | | - pMem->n = sqlite3Strlen30(pMem->z); |
| 74318 | + assert( pMem->z!=0 ); |
| 74319 | + pMem->n = sqlite3Strlen30NN(pMem->z); |
| 74039 | 74320 | pMem->enc = SQLITE_UTF8; |
| 74040 | 74321 | pMem->flags |= MEM_Str|MEM_Term; |
| 74041 | 74322 | if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real); |
| 74042 | 74323 | sqlite3VdbeChangeEncoding(pMem, enc); |
| 74043 | 74324 | return SQLITE_OK; |
| | @@ -75608,10 +75889,17 @@ |
| 75608 | 75889 | if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){ |
| 75609 | 75890 | p->expmask = 0; |
| 75610 | 75891 | } |
| 75611 | 75892 | assert( p->zSql==0 ); |
| 75612 | 75893 | p->zSql = sqlite3DbStrNDup(p->db, z, n); |
| 75894 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 75895 | + assert( p->zNormSql==0 ); |
| 75896 | + if( p->zSql && (prepFlags & SQLITE_PREPARE_NORMALIZE)!=0 ){ |
| 75897 | + sqlite3Normalize(p, p->zSql, n, prepFlags); |
| 75898 | + assert( p->zNormSql!=0 || p->db->mallocFailed ); |
| 75899 | + } |
| 75900 | +#endif |
| 75613 | 75901 | } |
| 75614 | 75902 | |
| 75615 | 75903 | /* |
| 75616 | 75904 | ** Swap all content between two VDBE structures. |
| 75617 | 75905 | */ |
| | @@ -75629,10 +75917,15 @@ |
| 75629 | 75917 | pA->pPrev = pB->pPrev; |
| 75630 | 75918 | pB->pPrev = pTmp; |
| 75631 | 75919 | zTmp = pA->zSql; |
| 75632 | 75920 | pA->zSql = pB->zSql; |
| 75633 | 75921 | pB->zSql = zTmp; |
| 75922 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 75923 | + zTmp = pA->zNormSql; |
| 75924 | + pA->zNormSql = pB->zNormSql; |
| 75925 | + pB->zNormSql = zTmp; |
| 75926 | +#endif |
| 75634 | 75927 | pB->expmask = pA->expmask; |
| 75635 | 75928 | pB->prepFlags = pA->prepFlags; |
| 75636 | 75929 | memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter)); |
| 75637 | 75930 | pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++; |
| 75638 | 75931 | } |
| | @@ -78700,10 +78993,13 @@ |
| 78700 | 78993 | sqlite3DbFree(db, p->pFree); |
| 78701 | 78994 | } |
| 78702 | 78995 | vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 78703 | 78996 | sqlite3DbFree(db, p->aColName); |
| 78704 | 78997 | sqlite3DbFree(db, p->zSql); |
| 78998 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 78999 | + sqlite3DbFree(db, p->zNormSql); |
| 79000 | +#endif |
| 78705 | 79001 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 78706 | 79002 | { |
| 78707 | 79003 | int i; |
| 78708 | 79004 | for(i=0; i<p->nScan; i++){ |
| 78709 | 79005 | sqlite3DbFree(db, p->aScan[i].zName); |
| | @@ -80101,11 +80397,13 @@ |
| 80101 | 80397 | |
| 80102 | 80398 | /* The index entry must begin with a header size */ |
| 80103 | 80399 | (void)getVarint32((u8*)m.z, szHdr); |
| 80104 | 80400 | testcase( szHdr==3 ); |
| 80105 | 80401 | testcase( szHdr==m.n ); |
| 80106 | | - if( unlikely(szHdr<3 || (int)szHdr>m.n) ){ |
| 80402 | + testcase( szHdr>0x7fffffff ); |
| 80403 | + assert( m.n>=0 ); |
| 80404 | + if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){ |
| 80107 | 80405 | goto idx_rowid_corruption; |
| 80108 | 80406 | } |
| 80109 | 80407 | |
| 80110 | 80408 | /* The last field of the index should be an integer - the ROWID. |
| 80111 | 80409 | ** Verify that the last entry really is an integer. */ |
| | @@ -82112,10 +82410,20 @@ |
| 82112 | 82410 | } |
| 82113 | 82411 | return z; |
| 82114 | 82412 | #endif |
| 82115 | 82413 | } |
| 82116 | 82414 | |
| 82415 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 82416 | +/* |
| 82417 | +** Return the normalized SQL associated with a prepared statement. |
| 82418 | +*/ |
| 82419 | +SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){ |
| 82420 | + Vdbe *p = (Vdbe *)pStmt; |
| 82421 | + return p ? p->zNormSql : 0; |
| 82422 | +} |
| 82423 | +#endif /* SQLITE_ENABLE_NORMALIZE */ |
| 82424 | + |
| 82117 | 82425 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 82118 | 82426 | /* |
| 82119 | 82427 | ** Allocate and populate an UnpackedRecord structure based on the serialized |
| 82120 | 82428 | ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure |
| 82121 | 82429 | ** if successful, or a NULL pointer if an OOM error is encountered. |
| | @@ -85551,21 +85859,29 @@ |
| 85551 | 85859 | nVarint = sqlite3VarintLen(nHdr); |
| 85552 | 85860 | nHdr += nVarint; |
| 85553 | 85861 | if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++; |
| 85554 | 85862 | } |
| 85555 | 85863 | nByte = nHdr+nData; |
| 85556 | | - if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 85557 | | - goto too_big; |
| 85558 | | - } |
| 85559 | 85864 | |
| 85560 | 85865 | /* Make sure the output register has a buffer large enough to store |
| 85561 | 85866 | ** the new record. The output register (pOp->p3) is not allowed to |
| 85562 | 85867 | ** be one of the input registers (because the following call to |
| 85563 | 85868 | ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). |
| 85564 | 85869 | */ |
| 85565 | | - if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ |
| 85566 | | - goto no_mem; |
| 85870 | + if( nByte+nZero<=pOut->szMalloc ){ |
| 85871 | + /* The output register is already large enough to hold the record. |
| 85872 | + ** No error checks or buffer enlargement is required */ |
| 85873 | + pOut->z = pOut->zMalloc; |
| 85874 | + }else{ |
| 85875 | + /* Need to make sure that the output is not too big and then enlarge |
| 85876 | + ** the output register to hold the full result */ |
| 85877 | + if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 85878 | + goto too_big; |
| 85879 | + } |
| 85880 | + if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ |
| 85881 | + goto no_mem; |
| 85882 | + } |
| 85567 | 85883 | } |
| 85568 | 85884 | zNewRecord = (u8 *)pOut->z; |
| 85569 | 85885 | |
| 85570 | 85886 | /* Write the record */ |
| 85571 | 85887 | i = putVarint32(zNewRecord, nHdr); |
| | @@ -88417,11 +88733,11 @@ |
| 88417 | 88733 | }else |
| 88418 | 88734 | #endif |
| 88419 | 88735 | { |
| 88420 | 88736 | zMaster = MASTER_NAME; |
| 88421 | 88737 | initData.db = db; |
| 88422 | | - initData.iDb = pOp->p1; |
| 88738 | + initData.iDb = iDb; |
| 88423 | 88739 | initData.pzErrMsg = &p->zErrMsg; |
| 88424 | 88740 | initData.mInitFlags = 0; |
| 88425 | 88741 | zSql = sqlite3MPrintf(db, |
| 88426 | 88742 | "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid", |
| 88427 | 88743 | db->aDb[iDb].zDbSName, zMaster, pOp->p4.z); |
| | @@ -89614,14 +89930,15 @@ |
| 89614 | 89930 | ** Store in register P3 the value of the P2-th column of |
| 89615 | 89931 | ** the current row of the virtual-table of cursor P1. |
| 89616 | 89932 | ** |
| 89617 | 89933 | ** If the VColumn opcode is being used to fetch the value of |
| 89618 | 89934 | ** an unchanging column during an UPDATE operation, then the P5 |
| 89619 | | -** value is 1. Otherwise, P5 is 0. The P5 value is returned |
| 89620 | | -** by sqlite3_vtab_nochange() routine and can be used |
| 89621 | | -** by virtual table implementations to return special "no-change" |
| 89622 | | -** marks which can be more efficient, depending on the virtual table. |
| 89935 | +** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange() |
| 89936 | +** function to return true inside the xColumn method of the virtual |
| 89937 | +** table implementation. The P5 column might also contain other |
| 89938 | +** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are |
| 89939 | +** unused by OP_VColumn. |
| 89623 | 89940 | */ |
| 89624 | 89941 | case OP_VColumn: { |
| 89625 | 89942 | sqlite3_vtab *pVtab; |
| 89626 | 89943 | const sqlite3_module *pModule; |
| 89627 | 89944 | Mem *pDest; |
| | @@ -89639,11 +89956,12 @@ |
| 89639 | 89956 | pVtab = pCur->uc.pVCur->pVtab; |
| 89640 | 89957 | pModule = pVtab->pModule; |
| 89641 | 89958 | assert( pModule->xColumn ); |
| 89642 | 89959 | memset(&sContext, 0, sizeof(sContext)); |
| 89643 | 89960 | sContext.pOut = pDest; |
| 89644 | | - if( pOp->p5 ){ |
| 89961 | + testcase( (pOp->p5 & OPFLAG_NOCHNG)==0 && pOp->p5!=0 ); |
| 89962 | + if( pOp->p5 & OPFLAG_NOCHNG ){ |
| 89645 | 89963 | sqlite3VdbeMemSetNull(pDest); |
| 89646 | 89964 | pDest->flags = MEM_Null|MEM_Zero; |
| 89647 | 89965 | pDest->u.nZero = 0; |
| 89648 | 89966 | }else{ |
| 89649 | 89967 | MemSetTypeFlag(pDest, MEM_Null); |
| | @@ -93998,12 +94316,12 @@ |
| 93998 | 94316 | if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort; |
| 93999 | 94317 | }else if( pExpr->x.pList ){ |
| 94000 | 94318 | if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; |
| 94001 | 94319 | } |
| 94002 | 94320 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 94003 | | - if( !ExprHasProperty(pExpr, EP_Reduced) && pExpr->pWin ){ |
| 94004 | | - Window *pWin = pExpr->pWin; |
| 94321 | + if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 94322 | + Window *pWin = pExpr->y.pWin; |
| 94005 | 94323 | if( sqlite3WalkExprList(pWalker, pWin->pPartition) ) return WRC_Abort; |
| 94006 | 94324 | if( sqlite3WalkExprList(pWalker, pWin->pOrderBy) ) return WRC_Abort; |
| 94007 | 94325 | if( sqlite3WalkExpr(pWalker, pWin->pFilter) ) return WRC_Abort; |
| 94008 | 94326 | } |
| 94009 | 94327 | #endif |
| | @@ -94272,11 +94590,11 @@ |
| 94272 | 94590 | ** |
| 94273 | 94591 | ** pExpr->iDb Set the index in db->aDb[] of the database X |
| 94274 | 94592 | ** (even if X is implied). |
| 94275 | 94593 | ** pExpr->iTable Set to the cursor number for the table obtained |
| 94276 | 94594 | ** from pSrcList. |
| 94277 | | -** pExpr->pTab Points to the Table structure of X.Y (even if |
| 94595 | +** pExpr->y.pTab Points to the Table structure of X.Y (even if |
| 94278 | 94596 | ** X and/or Y are implied.) |
| 94279 | 94597 | ** pExpr->iColumn Set to the column number within the table. |
| 94280 | 94598 | ** pExpr->op Set to TK_COLUMN. |
| 94281 | 94599 | ** pExpr->pLeft Any expression this points to is deleted |
| 94282 | 94600 | ** pExpr->pRight Any expression this points to is deleted. |
| | @@ -94316,11 +94634,10 @@ |
| 94316 | 94634 | assert( zCol ); /* The Z in X.Y.Z cannot be NULL */ |
| 94317 | 94635 | assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); |
| 94318 | 94636 | |
| 94319 | 94637 | /* Initialize the node to no-match */ |
| 94320 | 94638 | pExpr->iTable = -1; |
| 94321 | | - pExpr->pTab = 0; |
| 94322 | 94639 | ExprSetVVAProperty(pExpr, EP_NoReduce); |
| 94323 | 94640 | |
| 94324 | 94641 | /* Translate the schema name in zDb into a pointer to the corresponding |
| 94325 | 94642 | ** schema. If not found, pSchema will remain NULL and nothing will match |
| 94326 | 94643 | ** resulting in an appropriate error message toward the end of this routine |
| | @@ -94378,11 +94695,11 @@ |
| 94378 | 94695 | assert( zTabName!=0 ); |
| 94379 | 94696 | if( sqlite3StrICmp(zTabName, zTab)!=0 ){ |
| 94380 | 94697 | continue; |
| 94381 | 94698 | } |
| 94382 | 94699 | if( IN_RENAME_OBJECT && pItem->zAlias ){ |
| 94383 | | - sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->pTab); |
| 94700 | + sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab); |
| 94384 | 94701 | } |
| 94385 | 94702 | } |
| 94386 | 94703 | if( 0==(cntTab++) ){ |
| 94387 | 94704 | pMatch = pItem; |
| 94388 | 94705 | } |
| | @@ -94404,17 +94721,17 @@ |
| 94404 | 94721 | } |
| 94405 | 94722 | } |
| 94406 | 94723 | } |
| 94407 | 94724 | if( pMatch ){ |
| 94408 | 94725 | pExpr->iTable = pMatch->iCursor; |
| 94409 | | - pExpr->pTab = pMatch->pTab; |
| 94726 | + pExpr->y.pTab = pMatch->pTab; |
| 94410 | 94727 | /* RIGHT JOIN not (yet) supported */ |
| 94411 | 94728 | assert( (pMatch->fg.jointype & JT_RIGHT)==0 ); |
| 94412 | 94729 | if( (pMatch->fg.jointype & JT_LEFT)!=0 ){ |
| 94413 | 94730 | ExprSetProperty(pExpr, EP_CanBeNull); |
| 94414 | 94731 | } |
| 94415 | | - pSchema = pExpr->pTab->pSchema; |
| 94732 | + pSchema = pExpr->y.pTab->pSchema; |
| 94416 | 94733 | } |
| 94417 | 94734 | } /* if( pSrcList ) */ |
| 94418 | 94735 | |
| 94419 | 94736 | #if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) |
| 94420 | 94737 | /* If we have not already resolved the name, then maybe |
| | @@ -94467,11 +94784,11 @@ |
| 94467 | 94784 | #ifndef SQLITE_OMIT_UPSERT |
| 94468 | 94785 | if( pExpr->iTable==2 ){ |
| 94469 | 94786 | testcase( iCol==(-1) ); |
| 94470 | 94787 | if( IN_RENAME_OBJECT ){ |
| 94471 | 94788 | pExpr->iColumn = iCol; |
| 94472 | | - pExpr->pTab = pTab; |
| 94789 | + pExpr->y.pTab = pTab; |
| 94473 | 94790 | eNewExprOp = TK_COLUMN; |
| 94474 | 94791 | }else{ |
| 94475 | 94792 | pExpr->iTable = pNC->uNC.pUpsert->regData + iCol; |
| 94476 | 94793 | eNewExprOp = TK_REGISTER; |
| 94477 | 94794 | ExprSetProperty(pExpr, EP_Alias); |
| | @@ -94489,11 +94806,11 @@ |
| 94489 | 94806 | }else{ |
| 94490 | 94807 | testcase( iCol==31 ); |
| 94491 | 94808 | testcase( iCol==32 ); |
| 94492 | 94809 | pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol)); |
| 94493 | 94810 | } |
| 94494 | | - pExpr->pTab = pTab; |
| 94811 | + pExpr->y.pTab = pTab; |
| 94495 | 94812 | pExpr->iColumn = (i16)iCol; |
| 94496 | 94813 | eNewExprOp = TK_TRIGGER; |
| 94497 | 94814 | #endif /* SQLITE_OMIT_TRIGGER */ |
| 94498 | 94815 | } |
| 94499 | 94816 | } |
| | @@ -94589,11 +94906,11 @@ |
| 94589 | 94906 | */ |
| 94590 | 94907 | if( cnt==0 && zTab==0 ){ |
| 94591 | 94908 | assert( pExpr->op==TK_ID ); |
| 94592 | 94909 | if( ExprHasProperty(pExpr,EP_DblQuoted) ){ |
| 94593 | 94910 | pExpr->op = TK_STRING; |
| 94594 | | - pExpr->pTab = 0; |
| 94911 | + pExpr->y.pTab = 0; |
| 94595 | 94912 | return WRC_Prune; |
| 94596 | 94913 | } |
| 94597 | 94914 | if( sqlite3ExprIdToTrueFalse(pExpr) ){ |
| 94598 | 94915 | return WRC_Prune; |
| 94599 | 94916 | } |
| | @@ -94667,13 +94984,13 @@ |
| 94667 | 94984 | */ |
| 94668 | 94985 | SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){ |
| 94669 | 94986 | Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0); |
| 94670 | 94987 | if( p ){ |
| 94671 | 94988 | struct SrcList_item *pItem = &pSrc->a[iSrc]; |
| 94672 | | - p->pTab = pItem->pTab; |
| 94989 | + p->y.pTab = pItem->pTab; |
| 94673 | 94990 | p->iTable = pItem->iCursor; |
| 94674 | | - if( p->pTab->iPKey==iCol ){ |
| 94991 | + if( p->y.pTab->iPKey==iCol ){ |
| 94675 | 94992 | p->iColumn = -1; |
| 94676 | 94993 | }else{ |
| 94677 | 94994 | p->iColumn = (ynVar)iCol; |
| 94678 | 94995 | testcase( iCol==BMS ); |
| 94679 | 94996 | testcase( iCol==BMS-1 ); |
| | @@ -94759,11 +95076,11 @@ |
| 94759 | 95076 | struct SrcList_item *pItem; |
| 94760 | 95077 | assert( pSrcList && pSrcList->nSrc==1 ); |
| 94761 | 95078 | pItem = pSrcList->a; |
| 94762 | 95079 | assert( HasRowid(pItem->pTab) && pItem->pTab->pSelect==0 ); |
| 94763 | 95080 | pExpr->op = TK_COLUMN; |
| 94764 | | - pExpr->pTab = pItem->pTab; |
| 95081 | + pExpr->y.pTab = pItem->pTab; |
| 94765 | 95082 | pExpr->iTable = pItem->iCursor; |
| 94766 | 95083 | pExpr->iColumn = -1; |
| 94767 | 95084 | pExpr->affinity = SQLITE_AFF_INTEGER; |
| 94768 | 95085 | break; |
| 94769 | 95086 | } |
| | @@ -94803,13 +95120,11 @@ |
| 94803 | 95120 | } |
| 94804 | 95121 | zTable = pLeft->u.zToken; |
| 94805 | 95122 | zColumn = pRight->u.zToken; |
| 94806 | 95123 | if( IN_RENAME_OBJECT ){ |
| 94807 | 95124 | sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight); |
| 94808 | | - } |
| 94809 | | - if( IN_RENAME_OBJECT ){ |
| 94810 | | - sqlite3RenameTokenRemap(pParse, (void*)&pExpr->pTab, (void*)pLeft); |
| 95125 | + sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft); |
| 94811 | 95126 | } |
| 94812 | 95127 | } |
| 94813 | 95128 | return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr); |
| 94814 | 95129 | } |
| 94815 | 95130 | |
| | @@ -94887,30 +95202,39 @@ |
| 94887 | 95202 | ** sqlite_version() that might change over time cannot be used |
| 94888 | 95203 | ** in an index. */ |
| 94889 | 95204 | notValid(pParse, pNC, "non-deterministic functions", |
| 94890 | 95205 | NC_IdxExpr|NC_PartIdx); |
| 94891 | 95206 | } |
| 95207 | + if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0 |
| 95208 | + && pParse->nested==0 |
| 95209 | + && sqlite3Config.bInternalFunctions==0 |
| 95210 | + ){ |
| 95211 | + /* Internal-use-only functions are disallowed unless the |
| 95212 | + ** SQL is being compiled using sqlite3NestedParse() */ |
| 95213 | + no_such_func = 1; |
| 95214 | + pDef = 0; |
| 95215 | + } |
| 94892 | 95216 | } |
| 94893 | 95217 | |
| 94894 | 95218 | if( 0==IN_RENAME_OBJECT ){ |
| 94895 | 95219 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 94896 | 95220 | assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX) |
| 94897 | 95221 | || (pDef->xValue==0 && pDef->xInverse==0) |
| 94898 | 95222 | || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize) |
| 94899 | 95223 | ); |
| 94900 | | - if( pDef && pDef->xValue==0 && pExpr->pWin ){ |
| 95224 | + if( pDef && pDef->xValue==0 && ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 94901 | 95225 | sqlite3ErrorMsg(pParse, |
| 94902 | 95226 | "%.*s() may not be used as a window function", nId, zId |
| 94903 | 95227 | ); |
| 94904 | 95228 | pNC->nErr++; |
| 94905 | 95229 | }else if( |
| 94906 | 95230 | (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) |
| 94907 | | - || (is_agg && (pDef->funcFlags & SQLITE_FUNC_WINDOW) && !pExpr->pWin) |
| 94908 | | - || (is_agg && pExpr->pWin && (pNC->ncFlags & NC_AllowWin)==0) |
| 95231 | + || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pExpr->y.pWin) |
| 95232 | + || (is_agg && pExpr->y.pWin && (pNC->ncFlags & NC_AllowWin)==0) |
| 94909 | 95233 | ){ |
| 94910 | 95234 | const char *zType; |
| 94911 | | - if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pExpr->pWin ){ |
| 95235 | + if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pExpr->y.pWin ){ |
| 94912 | 95236 | zType = "window"; |
| 94913 | 95237 | }else{ |
| 94914 | 95238 | zType = "aggregate"; |
| 94915 | 95239 | } |
| 94916 | 95240 | sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId); |
| | @@ -94936,30 +95260,30 @@ |
| 94936 | 95260 | nId, zId); |
| 94937 | 95261 | pNC->nErr++; |
| 94938 | 95262 | } |
| 94939 | 95263 | if( is_agg ){ |
| 94940 | 95264 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 94941 | | - pNC->ncFlags &= ~(pExpr->pWin ? NC_AllowWin : NC_AllowAgg); |
| 95265 | + pNC->ncFlags &= ~(pExpr->y.pWin ? NC_AllowWin : NC_AllowAgg); |
| 94942 | 95266 | #else |
| 94943 | 95267 | pNC->ncFlags &= ~NC_AllowAgg; |
| 94944 | 95268 | #endif |
| 94945 | 95269 | } |
| 94946 | 95270 | } |
| 94947 | 95271 | sqlite3WalkExprList(pWalker, pList); |
| 94948 | 95272 | if( is_agg ){ |
| 94949 | 95273 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 94950 | | - if( pExpr->pWin ){ |
| 95274 | + if( pExpr->y.pWin ){ |
| 94951 | 95275 | Select *pSel = pNC->pWinSelect; |
| 94952 | | - sqlite3WalkExprList(pWalker, pExpr->pWin->pPartition); |
| 94953 | | - sqlite3WalkExprList(pWalker, pExpr->pWin->pOrderBy); |
| 94954 | | - sqlite3WalkExpr(pWalker, pExpr->pWin->pFilter); |
| 94955 | | - sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->pWin, pDef); |
| 95276 | + sqlite3WalkExprList(pWalker, pExpr->y.pWin->pPartition); |
| 95277 | + sqlite3WalkExprList(pWalker, pExpr->y.pWin->pOrderBy); |
| 95278 | + sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter); |
| 95279 | + sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef); |
| 94956 | 95280 | if( 0==pSel->pWin |
| 94957 | | - || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->pWin) |
| 95281 | + || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin) |
| 94958 | 95282 | ){ |
| 94959 | | - pExpr->pWin->pNextWin = pSel->pWin; |
| 94960 | | - pSel->pWin = pExpr->pWin; |
| 95283 | + pExpr->y.pWin->pNextWin = pSel->pWin; |
| 95284 | + pSel->pWin = pExpr->y.pWin; |
| 94961 | 95285 | } |
| 94962 | 95286 | pNC->ncFlags |= NC_AllowWin; |
| 94963 | 95287 | }else |
| 94964 | 95288 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 94965 | 95289 | { |
| | @@ -95378,17 +95702,17 @@ |
| 95378 | 95702 | return 1; |
| 95379 | 95703 | } |
| 95380 | 95704 | for(j=0; j<pSelect->pEList->nExpr; j++){ |
| 95381 | 95705 | if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){ |
| 95382 | 95706 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 95383 | | - if( pE->pWin ){ |
| 95707 | + if( ExprHasProperty(pE, EP_WinFunc) ){ |
| 95384 | 95708 | /* Since this window function is being changed into a reference |
| 95385 | 95709 | ** to the same window function the result set, remove the instance |
| 95386 | 95710 | ** of this window function from the Select.pWin list. */ |
| 95387 | 95711 | Window **pp; |
| 95388 | 95712 | for(pp=&pSelect->pWin; *pp; pp=&(*pp)->pNextWin){ |
| 95389 | | - if( *pp==pE->pWin ){ |
| 95713 | + if( *pp==pE->y.pWin ){ |
| 95390 | 95714 | *pp = (*pp)->pNextWin; |
| 95391 | 95715 | } |
| 95392 | 95716 | } |
| 95393 | 95717 | } |
| 95394 | 95718 | #endif |
| | @@ -95847,12 +96171,12 @@ |
| 95847 | 96171 | if( op==TK_CAST ){ |
| 95848 | 96172 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 95849 | 96173 | return sqlite3AffinityType(pExpr->u.zToken, 0); |
| 95850 | 96174 | } |
| 95851 | 96175 | #endif |
| 95852 | | - if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->pTab ){ |
| 95853 | | - return sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn); |
| 96176 | + if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->y.pTab ){ |
| 96177 | + return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn); |
| 95854 | 96178 | } |
| 95855 | 96179 | if( op==TK_SELECT_COLUMN ){ |
| 95856 | 96180 | assert( pExpr->pLeft->flags&EP_xIsSelect ); |
| 95857 | 96181 | return sqlite3ExprAffinity( |
| 95858 | 96182 | pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr |
| | @@ -95932,17 +96256,17 @@ |
| 95932 | 96256 | while( p ){ |
| 95933 | 96257 | int op = p->op; |
| 95934 | 96258 | if( p->flags & EP_Generic ) break; |
| 95935 | 96259 | if( (op==TK_AGG_COLUMN || op==TK_COLUMN |
| 95936 | 96260 | || op==TK_REGISTER || op==TK_TRIGGER) |
| 95937 | | - && p->pTab!=0 |
| 96261 | + && p->y.pTab!=0 |
| 95938 | 96262 | ){ |
| 95939 | | - /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally |
| 96263 | + /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally |
| 95940 | 96264 | ** a TK_COLUMN but was previously evaluated and cached in a register */ |
| 95941 | 96265 | int j = p->iColumn; |
| 95942 | 96266 | if( j>=0 ){ |
| 95943 | | - const char *zColl = p->pTab->aCol[j].zColl; |
| 96267 | + const char *zColl = p->y.pTab->aCol[j].zColl; |
| 95944 | 96268 | pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); |
| 95945 | 96269 | } |
| 95946 | 96270 | break; |
| 95947 | 96271 | } |
| 95948 | 96272 | if( op==TK_CAST || op==TK_UPLUS ){ |
| | @@ -96841,10 +97165,14 @@ |
| 96841 | 97165 | */ |
| 96842 | 97166 | static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ |
| 96843 | 97167 | assert( p!=0 ); |
| 96844 | 97168 | /* Sanity check: Assert that the IntValue is non-negative if it exists */ |
| 96845 | 97169 | assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 ); |
| 97170 | + |
| 97171 | + assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed ); |
| 97172 | + assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced) |
| 97173 | + || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) ); |
| 96846 | 97174 | #ifdef SQLITE_DEBUG |
| 96847 | 97175 | if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){ |
| 96848 | 97176 | assert( p->pLeft==0 ); |
| 96849 | 97177 | assert( p->pRight==0 ); |
| 96850 | 97178 | assert( p->x.pSelect==0 ); |
| | @@ -96859,12 +97187,13 @@ |
| 96859 | 97187 | }else if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 96860 | 97188 | sqlite3SelectDelete(db, p->x.pSelect); |
| 96861 | 97189 | }else{ |
| 96862 | 97190 | sqlite3ExprListDelete(db, p->x.pList); |
| 96863 | 97191 | } |
| 96864 | | - if( !ExprHasProperty(p, EP_Reduced) ){ |
| 96865 | | - sqlite3WindowDelete(db, p->pWin); |
| 97192 | + if( ExprHasProperty(p, EP_WinFunc) ){ |
| 97193 | + assert( p->op==TK_FUNCTION ); |
| 97194 | + sqlite3WindowDelete(db, p->y.pWin); |
| 96866 | 97195 | } |
| 96867 | 97196 | } |
| 96868 | 97197 | if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken); |
| 96869 | 97198 | if( !ExprHasProperty(p, EP_Static) ){ |
| 96870 | 97199 | sqlite3DbFreeNN(db, p); |
| | @@ -96924,11 +97253,11 @@ |
| 96924 | 97253 | assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ |
| 96925 | 97254 | assert( EXPR_FULLSIZE<=0xfff ); |
| 96926 | 97255 | assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 ); |
| 96927 | 97256 | if( 0==flags || p->op==TK_SELECT_COLUMN |
| 96928 | 97257 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 96929 | | - || p->pWin |
| 97258 | + || ExprHasProperty(p, EP_WinFunc) |
| 96930 | 97259 | #endif |
| 96931 | 97260 | ){ |
| 96932 | 97261 | nSize = EXPR_FULLSIZE; |
| 96933 | 97262 | }else{ |
| 96934 | 97263 | assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); |
| | @@ -96951,11 +97280,11 @@ |
| 96951 | 97280 | ** string is defined.) |
| 96952 | 97281 | */ |
| 96953 | 97282 | static int dupedExprNodeSize(Expr *p, int flags){ |
| 96954 | 97283 | int nByte = dupedExprStructSize(p, flags) & 0xfff; |
| 96955 | 97284 | if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ |
| 96956 | | - nByte += sqlite3Strlen30(p->u.zToken)+1; |
| 97285 | + nByte += sqlite3Strlen30NN(p->u.zToken)+1; |
| 96957 | 97286 | } |
| 96958 | 97287 | return ROUND8(nByte); |
| 96959 | 97288 | } |
| 96960 | 97289 | |
| 96961 | 97290 | /* |
| | @@ -97054,26 +97383,28 @@ |
| 97054 | 97383 | pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags); |
| 97055 | 97384 | } |
| 97056 | 97385 | } |
| 97057 | 97386 | |
| 97058 | 97387 | /* Fill in pNew->pLeft and pNew->pRight. */ |
| 97059 | | - zAlloc += dupedExprNodeSize(p, dupFlags); |
| 97060 | | - if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){ |
| 97388 | + if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){ |
| 97389 | + zAlloc += dupedExprNodeSize(p, dupFlags); |
| 97061 | 97390 | if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){ |
| 97062 | 97391 | pNew->pLeft = p->pLeft ? |
| 97063 | 97392 | exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0; |
| 97064 | 97393 | pNew->pRight = p->pRight ? |
| 97065 | 97394 | exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0; |
| 97066 | 97395 | } |
| 97067 | | - }else{ |
| 97068 | 97396 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 97069 | | - if( ExprHasProperty(p, EP_Reduced|EP_TokenOnly) ){ |
| 97070 | | - pNew->pWin = 0; |
| 97071 | | - }else{ |
| 97072 | | - pNew->pWin = sqlite3WindowDup(db, pNew, p->pWin); |
| 97397 | + if( ExprHasProperty(p, EP_WinFunc) ){ |
| 97398 | + pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin); |
| 97399 | + assert( ExprHasProperty(pNew, EP_WinFunc) ); |
| 97073 | 97400 | } |
| 97074 | 97401 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 97402 | + if( pzBuffer ){ |
| 97403 | + *pzBuffer = zAlloc; |
| 97404 | + } |
| 97405 | + }else{ |
| 97075 | 97406 | if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){ |
| 97076 | 97407 | if( pNew->op==TK_SELECT_COLUMN ){ |
| 97077 | 97408 | pNew->pLeft = p->pLeft; |
| 97078 | 97409 | assert( p->iColumn==0 || p->pRight==0 ); |
| 97079 | 97410 | assert( p->pRight==0 || p->pRight==p->pLeft ); |
| | @@ -97081,13 +97412,10 @@ |
| 97081 | 97412 | pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); |
| 97082 | 97413 | } |
| 97083 | 97414 | pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); |
| 97084 | 97415 | } |
| 97085 | 97416 | } |
| 97086 | | - if( pzBuffer ){ |
| 97087 | | - *pzBuffer = zAlloc; |
| 97088 | | - } |
| 97089 | 97417 | } |
| 97090 | 97418 | return pNew; |
| 97091 | 97419 | } |
| 97092 | 97420 | |
| 97093 | 97421 | /* |
| | @@ -97878,12 +98206,12 @@ |
| 97878 | 98206 | case TK_FLOAT: |
| 97879 | 98207 | case TK_BLOB: |
| 97880 | 98208 | return 0; |
| 97881 | 98209 | case TK_COLUMN: |
| 97882 | 98210 | return ExprHasProperty(p, EP_CanBeNull) || |
| 97883 | | - p->pTab==0 || /* Reference to column of index on expression */ |
| 97884 | | - (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0); |
| 98211 | + p->y.pTab==0 || /* Reference to column of index on expression */ |
| 98212 | + (p->iColumn>=0 && p->y.pTab->aCol[p->iColumn].notNull==0); |
| 97885 | 98213 | default: |
| 97886 | 98214 | return 1; |
| 97887 | 98215 | } |
| 97888 | 98216 | } |
| 97889 | 98217 | |
| | @@ -97934,10 +98262,18 @@ |
| 97934 | 98262 | if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; |
| 97935 | 98263 | if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; |
| 97936 | 98264 | if( sqlite3StrICmp(z, "OID")==0 ) return 1; |
| 97937 | 98265 | return 0; |
| 97938 | 98266 | } |
| 98267 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 98268 | +SQLITE_PRIVATE int sqlite3IsRowidN(const char *z, int n){ |
| 98269 | + if( sqlite3StrNICmp(z, "_ROWID_", n)==0 ) return 1; |
| 98270 | + if( sqlite3StrNICmp(z, "ROWID", n)==0 ) return 1; |
| 98271 | + if( sqlite3StrNICmp(z, "OID", n)==0 ) return 1; |
| 98272 | + return 0; |
| 98273 | +} |
| 98274 | +#endif |
| 97939 | 98275 | |
| 97940 | 98276 | /* |
| 97941 | 98277 | ** pX is the RHS of an IN operator. If pX is a SELECT statement |
| 97942 | 98278 | ** that can be simplified to a direct table access, then return |
| 97943 | 98279 | ** a pointer to the SELECT statement. If pX is not a SELECT statement, |
| | @@ -99167,11 +99503,11 @@ |
| 99167 | 99503 | ** expresssion. However, make sure the constant has the correct |
| 99168 | 99504 | ** datatype by applying the Affinity of the table column to the |
| 99169 | 99505 | ** constant. |
| 99170 | 99506 | */ |
| 99171 | 99507 | int iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target); |
| 99172 | | - int aff = sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn); |
| 99508 | + int aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn); |
| 99173 | 99509 | if( aff!=SQLITE_AFF_BLOB ){ |
| 99174 | 99510 | static const char zAff[] = "B\000C\000D\000E"; |
| 99175 | 99511 | assert( SQLITE_AFF_BLOB=='A' ); |
| 99176 | 99512 | assert( SQLITE_AFF_TEXT=='B' ); |
| 99177 | 99513 | if( iReg!=target ){ |
| | @@ -99191,11 +99527,11 @@ |
| 99191 | 99527 | /* Coding an expression that is part of an index where column names |
| 99192 | 99528 | ** in the index refer to the table to which the index belongs */ |
| 99193 | 99529 | iTab = pParse->iSelfTab - 1; |
| 99194 | 99530 | } |
| 99195 | 99531 | } |
| 99196 | | - return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, |
| 99532 | + return sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab, |
| 99197 | 99533 | pExpr->iColumn, iTab, target, |
| 99198 | 99534 | pExpr->op2); |
| 99199 | 99535 | } |
| 99200 | 99536 | case TK_INTEGER: { |
| 99201 | 99537 | codeInteger(pParse, pExpr, 0, target); |
| | @@ -99405,12 +99741,12 @@ |
| 99405 | 99741 | sqlite3 *db = pParse->db; /* The database connection */ |
| 99406 | 99742 | u8 enc = ENC(db); /* The text encoding used by this database */ |
| 99407 | 99743 | CollSeq *pColl = 0; /* A collating sequence */ |
| 99408 | 99744 | |
| 99409 | 99745 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 99410 | | - if( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) && pExpr->pWin ){ |
| 99411 | | - return pExpr->pWin->regResult; |
| 99746 | + if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 99747 | + return pExpr->y.pWin->regResult; |
| 99412 | 99748 | } |
| 99413 | 99749 | #endif |
| 99414 | 99750 | |
| 99415 | 99751 | if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){ |
| 99416 | 99752 | /* SQL functions can be expensive. So try to move constant functions |
| | @@ -99649,11 +99985,11 @@ |
| 99649 | 99985 | ** |
| 99650 | 99986 | ** p1==0 -> old.rowid p1==3 -> new.rowid |
| 99651 | 99987 | ** p1==1 -> old.a p1==4 -> new.a |
| 99652 | 99988 | ** p1==2 -> old.b p1==5 -> new.b |
| 99653 | 99989 | */ |
| 99654 | | - Table *pTab = pExpr->pTab; |
| 99990 | + Table *pTab = pExpr->y.pTab; |
| 99655 | 99991 | int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn; |
| 99656 | 99992 | |
| 99657 | 99993 | assert( pExpr->iTable==0 || pExpr->iTable==1 ); |
| 99658 | 99994 | assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol ); |
| 99659 | 99995 | assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey ); |
| | @@ -99660,11 +99996,11 @@ |
| 99660 | 99996 | assert( p1>=0 && p1<(pTab->nCol*2+2) ); |
| 99661 | 99997 | |
| 99662 | 99998 | sqlite3VdbeAddOp2(v, OP_Param, p1, target); |
| 99663 | 99999 | VdbeComment((v, "r[%d]=%s.%s", target, |
| 99664 | 100000 | (pExpr->iTable ? "new" : "old"), |
| 99665 | | - (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName) |
| 100001 | + (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[pExpr->iColumn].zName) |
| 99666 | 100002 | )); |
| 99667 | 100003 | |
| 99668 | 100004 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 99669 | 100005 | /* If the column has REAL affinity, it may currently be stored as an |
| 99670 | 100006 | ** integer. Use OP_RealAffinity to make sure it is really real. |
| | @@ -100511,10 +100847,24 @@ |
| 100511 | 100847 | return 2; |
| 100512 | 100848 | } |
| 100513 | 100849 | if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){ |
| 100514 | 100850 | if( pA->op==TK_FUNCTION ){ |
| 100515 | 100851 | if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; |
| 100852 | +#ifndef SQLITE_OMIT_WINDOWFUNC |
| 100853 | + /* Justification for the assert(): |
| 100854 | + ** window functions have p->op==TK_FUNCTION but aggregate functions |
| 100855 | + ** have p->op==TK_AGG_FUNCTION. So any comparison between an aggregate |
| 100856 | + ** function and a window function should have failed before reaching |
| 100857 | + ** this point. And, it is not possible to have a window function and |
| 100858 | + ** a scalar function with the same name and number of arguments. So |
| 100859 | + ** if we reach this point, either A and B both window functions or |
| 100860 | + ** neither are a window functions. */ |
| 100861 | + assert( ExprHasProperty(pA,EP_WinFunc)==ExprHasProperty(pB,EP_WinFunc) ); |
| 100862 | + if( ExprHasProperty(pA,EP_WinFunc) ){ |
| 100863 | + if( sqlite3WindowCompare(pParse,pA->y.pWin,pB->y.pWin)!=0 ) return 2; |
| 100864 | + } |
| 100865 | +#endif |
| 100516 | 100866 | }else if( pA->op==TK_COLLATE ){ |
| 100517 | 100867 | if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; |
| 100518 | 100868 | }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ |
| 100519 | 100869 | return 2; |
| 100520 | 100870 | } |
| | @@ -100530,25 +100880,10 @@ |
| 100530 | 100880 | if( pA->op!=TK_STRING && pA->op!=TK_TRUEFALSE ){ |
| 100531 | 100881 | if( pA->iColumn!=pB->iColumn ) return 2; |
| 100532 | 100882 | if( pA->iTable!=pB->iTable |
| 100533 | 100883 | && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; |
| 100534 | 100884 | } |
| 100535 | | -#ifndef SQLITE_OMIT_WINDOWFUNC |
| 100536 | | - /* Justification for the assert(): |
| 100537 | | - ** window functions have p->op==TK_FUNCTION but aggregate functions |
| 100538 | | - ** have p->op==TK_AGG_FUNCTION. So any comparison between an aggregate |
| 100539 | | - ** function and a window function should have failed before reaching |
| 100540 | | - ** this point. And, it is not possible to have a window function and |
| 100541 | | - ** a scalar function with the same name and number of arguments. So |
| 100542 | | - ** if we reach this point, either A and B both window functions or |
| 100543 | | - ** neither are a window functions. */ |
| 100544 | | - assert( (pA->pWin==0)==(pB->pWin==0) ); |
| 100545 | | - |
| 100546 | | - if( pA->pWin!=0 ){ |
| 100547 | | - if( sqlite3WindowCompare(pParse,pA->pWin,pB->pWin)!=0 ) return 2; |
| 100548 | | - } |
| 100549 | | -#endif |
| 100550 | 100885 | } |
| 100551 | 100886 | return 0; |
| 100552 | 100887 | } |
| 100553 | 100888 | |
| 100554 | 100889 | /* |
| | @@ -100685,12 +101020,12 @@ |
| 100685 | 101020 | testcase( pExpr->op==TK_NE ); |
| 100686 | 101021 | testcase( pExpr->op==TK_LT ); |
| 100687 | 101022 | testcase( pExpr->op==TK_LE ); |
| 100688 | 101023 | testcase( pExpr->op==TK_GT ); |
| 100689 | 101024 | testcase( pExpr->op==TK_GE ); |
| 100690 | | - if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->pTab)) |
| 100691 | | - || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->pTab)) |
| 101025 | + if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->y.pTab)) |
| 101026 | + || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->y.pTab)) |
| 100692 | 101027 | ){ |
| 100693 | 101028 | return WRC_Prune; |
| 100694 | 101029 | } |
| 100695 | 101030 | default: |
| 100696 | 101031 | return WRC_Continue; |
| | @@ -100917,11 +101252,11 @@ |
| 100917 | 101252 | } |
| 100918 | 101253 | if( (k>=pAggInfo->nColumn) |
| 100919 | 101254 | && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 |
| 100920 | 101255 | ){ |
| 100921 | 101256 | pCol = &pAggInfo->aCol[k]; |
| 100922 | | - pCol->pTab = pExpr->pTab; |
| 101257 | + pCol->pTab = pExpr->y.pTab; |
| 100923 | 101258 | pCol->iTable = pExpr->iTable; |
| 100924 | 101259 | pCol->iColumn = pExpr->iColumn; |
| 100925 | 101260 | pCol->iMem = ++pParse->nMem; |
| 100926 | 101261 | pCol->iSorterColumn = -1; |
| 100927 | 101262 | pCol->pExpr = pExpr; |
| | @@ -101800,14 +102135,20 @@ |
| 101800 | 102135 | #else |
| 101801 | 102136 | # define renameTokenCheckAll(x,y) |
| 101802 | 102137 | #endif |
| 101803 | 102138 | |
| 101804 | 102139 | /* |
| 101805 | | -** Add a new RenameToken object mapping parse tree element pPtr into |
| 101806 | | -** token *pToken to the Parse object currently under construction. |
| 102140 | +** Remember that the parser tree element pPtr was created using |
| 102141 | +** the token pToken. |
| 101807 | 102142 | ** |
| 101808 | | -** Return a copy of pPtr. |
| 102143 | +** In other words, construct a new RenameToken object and add it |
| 102144 | +** to the list of RenameToken objects currently being built up |
| 102145 | +** in pParse->pRename. |
| 102146 | +** |
| 102147 | +** The pPtr argument is returned so that this routine can be used |
| 102148 | +** with tail recursion in tokenExpr() routine, for a small performance |
| 102149 | +** improvement. |
| 101809 | 102150 | */ |
| 101810 | 102151 | SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){ |
| 101811 | 102152 | RenameToken *pNew; |
| 101812 | 102153 | assert( pPtr || pParse->db->mallocFailed ); |
| 101813 | 102154 | renameTokenCheckAll(pParse, pPtr); |
| | @@ -101936,11 +102277,11 @@ |
| 101936 | 102277 | && pWalker->pParse->pTriggerTab==p->pTab |
| 101937 | 102278 | ){ |
| 101938 | 102279 | renameTokenFind(pWalker->pParse, p, (void*)pExpr); |
| 101939 | 102280 | }else if( pExpr->op==TK_COLUMN |
| 101940 | 102281 | && pExpr->iColumn==p->iCol |
| 101941 | | - && p->pTab==pExpr->pTab |
| 102282 | + && p->pTab==pExpr->y.pTab |
| 101942 | 102283 | ){ |
| 101943 | 102284 | renameTokenFind(pWalker->pParse, p, (void*)pExpr); |
| 101944 | 102285 | } |
| 101945 | 102286 | return WRC_Continue; |
| 101946 | 102287 | } |
| | @@ -102194,13 +102535,18 @@ |
| 102194 | 102535 | assert( pNew->pTabSchema ); |
| 102195 | 102536 | pParse->pTriggerTab = sqlite3FindTable(db, pNew->table, |
| 102196 | 102537 | db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName |
| 102197 | 102538 | ); |
| 102198 | 102539 | pParse->eTriggerOp = pNew->op; |
| 102540 | + /* ALWAYS() because if the table of the trigger does not exist, the |
| 102541 | + ** error would have been hit before this point */ |
| 102542 | + if( ALWAYS(pParse->pTriggerTab) ){ |
| 102543 | + rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab); |
| 102544 | + } |
| 102199 | 102545 | |
| 102200 | 102546 | /* Resolve symbols in WHEN clause */ |
| 102201 | | - if( pNew->pWhen ){ |
| 102547 | + if( rc==SQLITE_OK && pNew->pWhen ){ |
| 102202 | 102548 | rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen); |
| 102203 | 102549 | } |
| 102204 | 102550 | |
| 102205 | 102551 | for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){ |
| 102206 | 102552 | if( pStep->pSelect ){ |
| | @@ -102310,19 +102656,12 @@ |
| 102310 | 102656 | ** Do a column rename operation on the CREATE statement given in zSql. |
| 102311 | 102657 | ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol |
| 102312 | 102658 | ** into zNew. The name should be quoted if bQuote is true. |
| 102313 | 102659 | ** |
| 102314 | 102660 | ** This function is used internally by the ALTER TABLE RENAME COLUMN command. |
| 102315 | | -** Though accessible to application code, it is not intended for use by |
| 102316 | | -** applications. The existance of this function, and the way it works, |
| 102317 | | -** is subject to change without notice. |
| 102318 | | -** |
| 102319 | | -** If any of the parameters are out-of-bounds, then simply return NULL. |
| 102320 | | -** An out-of-bounds parameter can only occur when the application calls |
| 102321 | | -** this function directly. The parameters will always be well-formed when |
| 102322 | | -** this routine is invoked by the bytecode for a legitimate ALTER TABLE |
| 102323 | | -** statement. |
| 102661 | +** It is only accessible to SQL created using sqlite3NestedParse(). It is |
| 102662 | +** not reachable from ordinary SQL passed into sqlite3_prepare(). |
| 102324 | 102663 | */ |
| 102325 | 102664 | static void renameColumnFunc( |
| 102326 | 102665 | sqlite3_context *context, |
| 102327 | 102666 | int NotUsed, |
| 102328 | 102667 | sqlite3_value **argv |
| | @@ -102474,12 +102813,12 @@ |
| 102474 | 102813 | /* |
| 102475 | 102814 | ** Walker expression callback used by "RENAME TABLE". |
| 102476 | 102815 | */ |
| 102477 | 102816 | static int renameTableExprCb(Walker *pWalker, Expr *pExpr){ |
| 102478 | 102817 | RenameCtx *p = pWalker->u.pRename; |
| 102479 | | - if( pExpr->op==TK_COLUMN && p->pTab==pExpr->pTab ){ |
| 102480 | | - renameTokenFind(pWalker->pParse, p, (void*)&pExpr->pTab); |
| 102818 | + if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){ |
| 102819 | + renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab); |
| 102481 | 102820 | } |
| 102482 | 102821 | return WRC_Continue; |
| 102483 | 102822 | } |
| 102484 | 102823 | |
| 102485 | 102824 | /* |
| | @@ -102572,11 +102911,11 @@ |
| 102572 | 102911 | sqlite3WalkSelect(&sWalker, pTab->pSelect); |
| 102573 | 102912 | } |
| 102574 | 102913 | }else{ |
| 102575 | 102914 | /* Modify any FK definitions to point to the new table. */ |
| 102576 | 102915 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 102577 | | - if( db->flags & SQLITE_ForeignKeys ){ |
| 102916 | + if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){ |
| 102578 | 102917 | FKey *pFKey; |
| 102579 | 102918 | for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
| 102580 | 102919 | if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){ |
| 102581 | 102920 | renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo); |
| 102582 | 102921 | } |
| | @@ -102726,13 +103065,13 @@ |
| 102726 | 103065 | /* |
| 102727 | 103066 | ** Register built-in functions used to help implement ALTER TABLE |
| 102728 | 103067 | */ |
| 102729 | 103068 | SQLITE_PRIVATE void sqlite3AlterFunctions(void){ |
| 102730 | 103069 | static FuncDef aAlterTableFuncs[] = { |
| 102731 | | - FUNCTION(sqlite_rename_column, 9, 0, 0, renameColumnFunc), |
| 102732 | | - FUNCTION(sqlite_rename_table, 7, 0, 0, renameTableFunc), |
| 102733 | | - FUNCTION(sqlite_rename_test, 5, 0, 0, renameTableTest), |
| 103070 | + INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc), |
| 103071 | + INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc), |
| 103072 | + INTERNAL_FUNCTION(sqlite_rename_test, 5, renameTableTest), |
| 102734 | 103073 | }; |
| 102735 | 103074 | sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs)); |
| 102736 | 103075 | } |
| 102737 | 103076 | #endif /* SQLITE_ALTER_TABLE */ |
| 102738 | 103077 | |
| | @@ -104777,11 +105116,11 @@ |
| 104777 | 105116 | if( pVfs==0 ) return; |
| 104778 | 105117 | pNew = &db->aDb[db->init.iDb]; |
| 104779 | 105118 | if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt); |
| 104780 | 105119 | pNew->pBt = 0; |
| 104781 | 105120 | pNew->pSchema = 0; |
| 104782 | | - rc = sqlite3BtreeOpen(pVfs, "x", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB); |
| 105121 | + rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB); |
| 104783 | 105122 | }else{ |
| 104784 | 105123 | /* This is a real ATTACH |
| 104785 | 105124 | ** |
| 104786 | 105125 | ** Check for the following errors: |
| 104787 | 105126 | ** |
| | @@ -105457,10 +105796,11 @@ |
| 105457 | 105796 | int iSrc; /* Index in pTabList->a[] of table being read */ |
| 105458 | 105797 | int iDb; /* The index of the database the expression refers to */ |
| 105459 | 105798 | int iCol; /* Index of column in table */ |
| 105460 | 105799 | |
| 105461 | 105800 | assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER ); |
| 105801 | + assert( !IN_RENAME_OBJECT || db->xAuth==0 ); |
| 105462 | 105802 | if( db->xAuth==0 ) return; |
| 105463 | 105803 | iDb = sqlite3SchemaToIndex(pParse->db, pSchema); |
| 105464 | 105804 | if( iDb<0 ){ |
| 105465 | 105805 | /* An attempt to read a column out of a subquery or other |
| 105466 | 105806 | ** temporary table. */ |
| | @@ -105513,10 +105853,11 @@ |
| 105513 | 105853 | int rc; |
| 105514 | 105854 | |
| 105515 | 105855 | /* Don't do any authorization checks if the database is initialising |
| 105516 | 105856 | ** or if the parser is being invoked from within sqlite3_declare_vtab. |
| 105517 | 105857 | */ |
| 105858 | + assert( !IN_RENAME_OBJECT || db->xAuth==0 ); |
| 105518 | 105859 | if( db->init.busy || IN_SPECIAL_PARSE ){ |
| 105519 | 105860 | return SQLITE_OK; |
| 105520 | 105861 | } |
| 105521 | 105862 | |
| 105522 | 105863 | if( db->xAuth==0 ){ |
| | @@ -105936,21 +106277,19 @@ |
| 105936 | 106277 | |
| 105937 | 106278 | p = sqlite3FindTable(db, zName, zDbase); |
| 105938 | 106279 | if( p==0 ){ |
| 105939 | 106280 | const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table"; |
| 105940 | 106281 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 105941 | | - if( sqlite3FindDbName(db, zDbase)<1 ){ |
| 105942 | | - /* If zName is the not the name of a table in the schema created using |
| 105943 | | - ** CREATE, then check to see if it is the name of an virtual table that |
| 105944 | | - ** can be an eponymous virtual table. */ |
| 105945 | | - Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName); |
| 105946 | | - if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){ |
| 105947 | | - pMod = sqlite3PragmaVtabRegister(db, zName); |
| 105948 | | - } |
| 105949 | | - if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ |
| 105950 | | - return pMod->pEpoTab; |
| 105951 | | - } |
| 106282 | + /* If zName is the not the name of a table in the schema created using |
| 106283 | + ** CREATE, then check to see if it is the name of an virtual table that |
| 106284 | + ** can be an eponymous virtual table. */ |
| 106285 | + Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName); |
| 106286 | + if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){ |
| 106287 | + pMod = sqlite3PragmaVtabRegister(db, zName); |
| 106288 | + } |
| 106289 | + if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ |
| 106290 | + return pMod->pEpoTab; |
| 105952 | 106291 | } |
| 105953 | 106292 | #endif |
| 105954 | 106293 | if( (flags & LOCATE_NOERR)==0 ){ |
| 105955 | 106294 | if( zDbase ){ |
| 105956 | 106295 | sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); |
| | @@ -106126,21 +106465,26 @@ |
| 106126 | 106465 | ** "main" and "temp") for a single database connection. |
| 106127 | 106466 | */ |
| 106128 | 106467 | SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){ |
| 106129 | 106468 | int i; |
| 106130 | 106469 | sqlite3BtreeEnterAll(db); |
| 106131 | | - assert( db->nSchemaLock==0 ); |
| 106132 | 106470 | for(i=0; i<db->nDb; i++){ |
| 106133 | 106471 | Db *pDb = &db->aDb[i]; |
| 106134 | 106472 | if( pDb->pSchema ){ |
| 106135 | | - sqlite3SchemaClear(pDb->pSchema); |
| 106473 | + if( db->nSchemaLock==0 ){ |
| 106474 | + sqlite3SchemaClear(pDb->pSchema); |
| 106475 | + }else{ |
| 106476 | + DbSetProperty(db, i, DB_ResetWanted); |
| 106477 | + } |
| 106136 | 106478 | } |
| 106137 | 106479 | } |
| 106138 | 106480 | db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk); |
| 106139 | 106481 | sqlite3VtabUnlockList(db); |
| 106140 | 106482 | sqlite3BtreeLeaveAll(db); |
| 106141 | | - sqlite3CollapseDatabaseArray(db); |
| 106483 | + if( db->nSchemaLock==0 ){ |
| 106484 | + sqlite3CollapseDatabaseArray(db); |
| 106485 | + } |
| 106142 | 106486 | } |
| 106143 | 106487 | |
| 106144 | 106488 | /* |
| 106145 | 106489 | ** This routine is called when a commit occurs. |
| 106146 | 106490 | */ |
| | @@ -106213,10 +106557,16 @@ |
| 106213 | 106557 | /* Delete any foreign keys attached to this table. */ |
| 106214 | 106558 | sqlite3FkDelete(db, pTable); |
| 106215 | 106559 | |
| 106216 | 106560 | /* Delete the Table structure itself. |
| 106217 | 106561 | */ |
| 106562 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 106563 | + if( pTable->pColHash ){ |
| 106564 | + sqlite3HashClear(pTable->pColHash); |
| 106565 | + sqlite3_free(pTable->pColHash); |
| 106566 | + } |
| 106567 | +#endif |
| 106218 | 106568 | sqlite3DeleteColumnNames(db, pTable); |
| 106219 | 106569 | sqlite3DbFree(db, pTable->zName); |
| 106220 | 106570 | sqlite3DbFree(db, pTable->zColAff); |
| 106221 | 106571 | sqlite3SelectDelete(db, pTable->pSelect); |
| 106222 | 106572 | sqlite3ExprListDelete(db, pTable->pCheck); |
| | @@ -106370,10 +106720,24 @@ |
| 106370 | 106720 | iDb = db->init.iDb; |
| 106371 | 106721 | *pUnqual = pName1; |
| 106372 | 106722 | } |
| 106373 | 106723 | return iDb; |
| 106374 | 106724 | } |
| 106725 | + |
| 106726 | +/* |
| 106727 | +** True if PRAGMA writable_schema is ON |
| 106728 | +*/ |
| 106729 | +SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3 *db){ |
| 106730 | + testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 ); |
| 106731 | + testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 106732 | + SQLITE_WriteSchema ); |
| 106733 | + testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 106734 | + SQLITE_Defensive ); |
| 106735 | + testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 106736 | + (SQLITE_WriteSchema|SQLITE_Defensive) ); |
| 106737 | + return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema; |
| 106738 | +} |
| 106375 | 106739 | |
| 106376 | 106740 | /* |
| 106377 | 106741 | ** This routine is used to check if the UTF-8 string zName is a legal |
| 106378 | 106742 | ** unqualified name for a new schema object (table, index, view or |
| 106379 | 106743 | ** trigger). All names are legal except those that begin with the string |
| | @@ -106380,11 +106744,11 @@ |
| 106380 | 106744 | ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace |
| 106381 | 106745 | ** is reserved for internal use. |
| 106382 | 106746 | */ |
| 106383 | 106747 | SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){ |
| 106384 | 106748 | if( !pParse->db->init.busy && pParse->nested==0 |
| 106385 | | - && (pParse->db->flags & SQLITE_WriteSchema)==0 |
| 106749 | + && sqlite3WritableSchema(pParse->db)==0 |
| 106386 | 106750 | && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ |
| 106387 | 106751 | sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName); |
| 106388 | 106752 | return SQLITE_ERROR; |
| 106389 | 106753 | } |
| 106390 | 106754 | return SQLITE_OK; |
| | @@ -107456,10 +107820,40 @@ |
| 107456 | 107820 | pPk->nColumn = pTab->nCol; |
| 107457 | 107821 | } |
| 107458 | 107822 | recomputeColumnsNotIndexed(pPk); |
| 107459 | 107823 | } |
| 107460 | 107824 | |
| 107825 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 107826 | +/* |
| 107827 | +** Return true if zName is a shadow table name in the current database |
| 107828 | +** connection. |
| 107829 | +** |
| 107830 | +** zName is temporarily modified while this routine is running, but is |
| 107831 | +** restored to its original value prior to this routine returning. |
| 107832 | +*/ |
| 107833 | +static int isShadowTableName(sqlite3 *db, char *zName){ |
| 107834 | + char *zTail; /* Pointer to the last "_" in zName */ |
| 107835 | + Table *pTab; /* Table that zName is a shadow of */ |
| 107836 | + Module *pMod; /* Module for the virtual table */ |
| 107837 | + |
| 107838 | + zTail = strrchr(zName, '_'); |
| 107839 | + if( zTail==0 ) return 0; |
| 107840 | + *zTail = 0; |
| 107841 | + pTab = sqlite3FindTable(db, zName, 0); |
| 107842 | + *zTail = '_'; |
| 107843 | + if( pTab==0 ) return 0; |
| 107844 | + if( !IsVirtual(pTab) ) return 0; |
| 107845 | + pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]); |
| 107846 | + if( pMod==0 ) return 0; |
| 107847 | + if( pMod->pModule->iVersion<3 ) return 0; |
| 107848 | + if( pMod->pModule->xShadowName==0 ) return 0; |
| 107849 | + return pMod->pModule->xShadowName(zTail+1); |
| 107850 | +} |
| 107851 | +#else |
| 107852 | +# define isShadowTableName(x,y) 0 |
| 107853 | +#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 107854 | + |
| 107461 | 107855 | /* |
| 107462 | 107856 | ** This routine is called to report the final ")" that terminates |
| 107463 | 107857 | ** a CREATE TABLE statement. |
| 107464 | 107858 | ** |
| 107465 | 107859 | ** The table structure that other action routines have been building |
| | @@ -107494,10 +107888,14 @@ |
| 107494 | 107888 | return; |
| 107495 | 107889 | } |
| 107496 | 107890 | assert( !db->mallocFailed ); |
| 107497 | 107891 | p = pParse->pNewTable; |
| 107498 | 107892 | if( p==0 ) return; |
| 107893 | + |
| 107894 | + if( pSelect==0 && isShadowTableName(db, p->zName) ){ |
| 107895 | + p->tabFlags |= TF_Shadow; |
| 107896 | + } |
| 107499 | 107897 | |
| 107500 | 107898 | /* If the db->init.busy is 1 it means we are reading the SQL off the |
| 107501 | 107899 | ** "sqlite_master" or "sqlite_temp_master" table on the disk. |
| 107502 | 107900 | ** So do not write to the disk again. Extract the root page number |
| 107503 | 107901 | ** for the table from the db->init.newTnum field. (The page number |
| | @@ -108002,11 +108400,11 @@ |
| 108002 | 108400 | ** erasing iTable (this can happen with an auto-vacuum database). |
| 108003 | 108401 | */ |
| 108004 | 108402 | static void destroyRootPage(Parse *pParse, int iTable, int iDb){ |
| 108005 | 108403 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 108006 | 108404 | int r1 = sqlite3GetTempReg(pParse); |
| 108007 | | - assert( iTable>1 ); |
| 108405 | + if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema"); |
| 108008 | 108406 | sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb); |
| 108009 | 108407 | sqlite3MayAbort(pParse); |
| 108010 | 108408 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 108011 | 108409 | /* OP_Destroy stores an in integer r1. If this integer |
| 108012 | 108410 | ** is non-zero, then it is the root page number of a table moved to |
| | @@ -110457,10 +110855,25 @@ |
| 110457 | 110855 | return p; |
| 110458 | 110856 | } |
| 110459 | 110857 | } |
| 110460 | 110858 | return 0; |
| 110461 | 110859 | } |
| 110860 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 110861 | +SQLITE_PRIVATE FuncDef *sqlite3FunctionSearchN( |
| 110862 | + int h, /* Hash of the name */ |
| 110863 | + const char *zFunc, /* Name of function */ |
| 110864 | + int nFunc /* Length of the name */ |
| 110865 | +){ |
| 110866 | + FuncDef *p; |
| 110867 | + for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){ |
| 110868 | + if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 ){ |
| 110869 | + return p; |
| 110870 | + } |
| 110871 | + } |
| 110872 | + return 0; |
| 110873 | +} |
| 110874 | +#endif /* SQLITE_ENABLE_NORMALIZE */ |
| 110462 | 110875 | |
| 110463 | 110876 | /* |
| 110464 | 110877 | ** Insert a new FuncDef into a FuncDefHash hash table. |
| 110465 | 110878 | */ |
| 110466 | 110879 | SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs( |
| | @@ -110470,11 +110883,11 @@ |
| 110470 | 110883 | int i; |
| 110471 | 110884 | for(i=0; i<nDef; i++){ |
| 110472 | 110885 | FuncDef *pOther; |
| 110473 | 110886 | const char *zName = aDef[i].zName; |
| 110474 | 110887 | int nName = sqlite3Strlen30(zName); |
| 110475 | | - int h = (zName[0] + nName) % SQLITE_FUNC_HASH_SZ; |
| 110888 | + int h = SQLITE_FUNC_HASH(zName[0], nName); |
| 110476 | 110889 | assert( zName[0]>='a' && zName[0]<='z' ); |
| 110477 | 110890 | pOther = functionSearch(h, zName); |
| 110478 | 110891 | if( pOther ){ |
| 110479 | 110892 | assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] ); |
| 110480 | 110893 | aDef[i].pNext = pOther->pNext; |
| | @@ -110549,11 +110962,11 @@ |
| 110549 | 110962 | ** new function. But the FuncDefs for built-in functions are read-only. |
| 110550 | 110963 | ** So we must not search for built-ins when creating a new function. |
| 110551 | 110964 | */ |
| 110552 | 110965 | if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){ |
| 110553 | 110966 | bestScore = 0; |
| 110554 | | - h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % SQLITE_FUNC_HASH_SZ; |
| 110967 | + h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName); |
| 110555 | 110968 | p = functionSearch(h, zName); |
| 110556 | 110969 | while( p ){ |
| 110557 | 110970 | int score = matchQuality(p, nArg, enc); |
| 110558 | 110971 | if( score>bestScore ){ |
| 110559 | 110972 | pBest = p; |
| | @@ -110696,37 +111109,54 @@ |
| 110696 | 111109 | if( sqlite3IndexedByLookup(pParse, pItem) ){ |
| 110697 | 111110 | pTab = 0; |
| 110698 | 111111 | } |
| 110699 | 111112 | return pTab; |
| 110700 | 111113 | } |
| 111114 | + |
| 111115 | +/* Return true if table pTab is read-only. |
| 111116 | +** |
| 111117 | +** A table is read-only if any of the following are true: |
| 111118 | +** |
| 111119 | +** 1) It is a virtual table and no implementation of the xUpdate method |
| 111120 | +** has been provided |
| 111121 | +** |
| 111122 | +** 2) It is a system table (i.e. sqlite_master), this call is not |
| 111123 | +** part of a nested parse and writable_schema pragma has not |
| 111124 | +** been specified |
| 111125 | +** |
| 111126 | +** 3) The table is a shadow table, the database connection is in |
| 111127 | +** defensive mode, and the current sqlite3_prepare() |
| 111128 | +** is for a top-level SQL statement. |
| 111129 | +*/ |
| 111130 | +static int tabIsReadOnly(Parse *pParse, Table *pTab){ |
| 111131 | + sqlite3 *db; |
| 111132 | + if( IsVirtual(pTab) ){ |
| 111133 | + return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0; |
| 111134 | + } |
| 111135 | + if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0; |
| 111136 | + db = pParse->db; |
| 111137 | + if( (pTab->tabFlags & TF_Readonly)!=0 ){ |
| 111138 | + return sqlite3WritableSchema(db)==0 && pParse->nested==0; |
| 111139 | + } |
| 111140 | + assert( pTab->tabFlags & TF_Shadow ); |
| 111141 | + return (db->flags & SQLITE_Defensive)!=0 |
| 111142 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 111143 | + && db->pVtabCtx==0 |
| 111144 | +#endif |
| 111145 | + && db->nVdbeExec==0; |
| 111146 | +} |
| 110701 | 111147 | |
| 110702 | 111148 | /* |
| 110703 | 111149 | ** Check to make sure the given table is writable. If it is not |
| 110704 | 111150 | ** writable, generate an error message and return 1. If it is |
| 110705 | 111151 | ** writable return 0; |
| 110706 | 111152 | */ |
| 110707 | 111153 | SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){ |
| 110708 | | - /* A table is not writable under the following circumstances: |
| 110709 | | - ** |
| 110710 | | - ** 1) It is a virtual table and no implementation of the xUpdate method |
| 110711 | | - ** has been provided, or |
| 110712 | | - ** 2) It is a system table (i.e. sqlite_master), this call is not |
| 110713 | | - ** part of a nested parse and writable_schema pragma has not |
| 110714 | | - ** been specified. |
| 110715 | | - ** |
| 110716 | | - ** In either case leave an error message in pParse and return non-zero. |
| 110717 | | - */ |
| 110718 | | - if( ( IsVirtual(pTab) |
| 110719 | | - && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ) |
| 110720 | | - || ( (pTab->tabFlags & TF_Readonly)!=0 |
| 110721 | | - && (pParse->db->flags & SQLITE_WriteSchema)==0 |
| 110722 | | - && pParse->nested==0 ) |
| 110723 | | - ){ |
| 111154 | + if( tabIsReadOnly(pParse, pTab) ){ |
| 110724 | 111155 | sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName); |
| 110725 | 111156 | return 1; |
| 110726 | 111157 | } |
| 110727 | | - |
| 110728 | 111158 | #ifndef SQLITE_OMIT_VIEW |
| 110729 | 111159 | if( !viewOk && pTab->pSelect ){ |
| 110730 | 111160 | sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName); |
| 110731 | 111161 | return 1; |
| 110732 | 111162 | } |
| | @@ -114126,11 +114556,11 @@ |
| 114126 | 114556 | int iCursor, /* The open cursor on the table */ |
| 114127 | 114557 | i16 iCol /* The column that is wanted */ |
| 114128 | 114558 | ){ |
| 114129 | 114559 | Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0); |
| 114130 | 114560 | if( pExpr ){ |
| 114131 | | - pExpr->pTab = pTab; |
| 114561 | + pExpr->y.pTab = pTab; |
| 114132 | 114562 | pExpr->iTable = iCursor; |
| 114133 | 114563 | pExpr->iColumn = iCol; |
| 114134 | 114564 | } |
| 114135 | 114565 | return pExpr; |
| 114136 | 114566 | } |
| | @@ -115202,11 +115632,12 @@ |
| 115202 | 115632 | do{ |
| 115203 | 115633 | zColAff[i--] = 0; |
| 115204 | 115634 | }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB ); |
| 115205 | 115635 | pTab->zColAff = zColAff; |
| 115206 | 115636 | } |
| 115207 | | - i = sqlite3Strlen30(zColAff); |
| 115637 | + assert( zColAff!=0 ); |
| 115638 | + i = sqlite3Strlen30NN(zColAff); |
| 115208 | 115639 | if( i ){ |
| 115209 | 115640 | if( iReg ){ |
| 115210 | 115641 | sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i); |
| 115211 | 115642 | }else{ |
| 115212 | 115643 | sqlite3VdbeChangeP4(v, -1, zColAff, i); |
| | @@ -116182,18 +116613,19 @@ |
| 116182 | 116613 | #ifdef tmask |
| 116183 | 116614 | #undef tmask |
| 116184 | 116615 | #endif |
| 116185 | 116616 | |
| 116186 | 116617 | /* |
| 116187 | | -** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged() |
| 116618 | +** Meanings of bits in of pWalker->eCode for |
| 116619 | +** sqlite3ExprReferencesUpdatedColumn() |
| 116188 | 116620 | */ |
| 116189 | 116621 | #define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */ |
| 116190 | 116622 | #define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */ |
| 116191 | 116623 | |
| 116192 | | -/* This is the Walker callback from checkConstraintUnchanged(). Set |
| 116193 | | -** bit 0x01 of pWalker->eCode if |
| 116194 | | -** pWalker->eCode to 0 if this expression node references any of the |
| 116624 | +/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn(). |
| 116625 | +* Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this |
| 116626 | +** expression node references any of the |
| 116195 | 116627 | ** columns that are being modifed by an UPDATE statement. |
| 116196 | 116628 | */ |
| 116197 | 116629 | static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){ |
| 116198 | 116630 | if( pExpr->op==TK_COLUMN ){ |
| 116199 | 116631 | assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 ); |
| | @@ -116211,16 +116643,25 @@ |
| 116211 | 116643 | /* |
| 116212 | 116644 | ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The |
| 116213 | 116645 | ** only columns that are modified by the UPDATE are those for which |
| 116214 | 116646 | ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true. |
| 116215 | 116647 | ** |
| 116216 | | -** Return true if CHECK constraint pExpr does not use any of the |
| 116648 | +** Return true if CHECK constraint pExpr uses any of the |
| 116217 | 116649 | ** changing columns (or the rowid if it is changing). In other words, |
| 116218 | | -** return true if this CHECK constraint can be skipped when validating |
| 116650 | +** return true if this CHECK constraint must be validated for |
| 116219 | 116651 | ** the new row in the UPDATE statement. |
| 116652 | +** |
| 116653 | +** 2018-09-15: pExpr might also be an expression for an index-on-expressions. |
| 116654 | +** The operation of this routine is the same - return true if an only if |
| 116655 | +** the expression uses one or more of columns identified by the second and |
| 116656 | +** third arguments. |
| 116220 | 116657 | */ |
| 116221 | | -static int checkConstraintUnchanged(Expr *pExpr, int *aiChng, int chngRowid){ |
| 116658 | +SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn( |
| 116659 | + Expr *pExpr, /* The expression to be checked */ |
| 116660 | + int *aiChng, /* aiChng[x]>=0 if column x changed by the UPDATE */ |
| 116661 | + int chngRowid /* True if UPDATE changes the rowid */ |
| 116662 | +){ |
| 116222 | 116663 | Walker w; |
| 116223 | 116664 | memset(&w, 0, sizeof(w)); |
| 116224 | 116665 | w.eCode = 0; |
| 116225 | 116666 | w.xExprCallback = checkConstraintExprNode; |
| 116226 | 116667 | w.u.aiCol = aiChng; |
| | @@ -116231,11 +116672,11 @@ |
| 116231 | 116672 | } |
| 116232 | 116673 | testcase( w.eCode==0 ); |
| 116233 | 116674 | testcase( w.eCode==CKCNSTRNT_COLUMN ); |
| 116234 | 116675 | testcase( w.eCode==CKCNSTRNT_ROWID ); |
| 116235 | 116676 | testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) ); |
| 116236 | | - return !w.eCode; |
| 116677 | + return w.eCode!=0; |
| 116237 | 116678 | } |
| 116238 | 116679 | |
| 116239 | 116680 | /* |
| 116240 | 116681 | ** Generate code to do constraint checks prior to an INSERT or an UPDATE |
| 116241 | 116682 | ** on table pTab. |
| | @@ -116437,11 +116878,17 @@ |
| 116437 | 116878 | pParse->iSelfTab = -(regNewData+1); |
| 116438 | 116879 | onError = overrideError!=OE_Default ? overrideError : OE_Abort; |
| 116439 | 116880 | for(i=0; i<pCheck->nExpr; i++){ |
| 116440 | 116881 | int allOk; |
| 116441 | 116882 | Expr *pExpr = pCheck->a[i].pExpr; |
| 116442 | | - if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue; |
| 116883 | + if( aiChng |
| 116884 | + && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng) |
| 116885 | + ){ |
| 116886 | + /* The check constraints do not reference any of the columns being |
| 116887 | + ** updated so there is no point it verifying the check constraint */ |
| 116888 | + continue; |
| 116889 | + } |
| 116443 | 116890 | allOk = sqlite3VdbeMakeLabel(v); |
| 116444 | 116891 | sqlite3VdbeVerifyAbortable(v, onError); |
| 116445 | 116892 | sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL); |
| 116446 | 116893 | if( onError==OE_Ignore ){ |
| 116447 | 116894 | sqlite3VdbeGoto(v, ignoreDest); |
| | @@ -117938,16 +118385,19 @@ |
| 117938 | 118385 | void (*str_appendchar)(sqlite3_str*, int N, char C); |
| 117939 | 118386 | void (*str_reset)(sqlite3_str*); |
| 117940 | 118387 | int (*str_errcode)(sqlite3_str*); |
| 117941 | 118388 | int (*str_length)(sqlite3_str*); |
| 117942 | 118389 | char *(*str_value)(sqlite3_str*); |
| 118390 | + /* Version 3.25.0 and later */ |
| 117943 | 118391 | int (*create_window_function)(sqlite3*,const char*,int,int,void*, |
| 117944 | 118392 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 117945 | 118393 | void (*xFinal)(sqlite3_context*), |
| 117946 | 118394 | void (*xValue)(sqlite3_context*), |
| 117947 | 118395 | void (*xInv)(sqlite3_context*,int,sqlite3_value**), |
| 117948 | 118396 | void(*xDestroy)(void*)); |
| 118397 | + /* Version 3.26.0 and later */ |
| 118398 | + const char *(*normalized_sql)(sqlite3_stmt*); |
| 117949 | 118399 | }; |
| 117950 | 118400 | |
| 117951 | 118401 | /* |
| 117952 | 118402 | ** This is the function signature used for all extension entry points. It |
| 117953 | 118403 | ** is also defined in the file "loadext.c". |
| | @@ -118231,10 +118681,12 @@ |
| 118231 | 118681 | #define sqlite3_str_errcode sqlite3_api->str_errcode |
| 118232 | 118682 | #define sqlite3_str_length sqlite3_api->str_length |
| 118233 | 118683 | #define sqlite3_str_value sqlite3_api->str_value |
| 118234 | 118684 | /* Version 3.25.0 and later */ |
| 118235 | 118685 | #define sqlite3_create_window_function sqlite3_api->create_window_function |
| 118686 | +/* Version 3.26.0 and later */ |
| 118687 | +#define sqlite3_normalized_sql sqlite3_api->normalized_sql |
| 118236 | 118688 | #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ |
| 118237 | 118689 | |
| 118238 | 118690 | #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 118239 | 118691 | /* This case when the file really is being compiled as a loadable |
| 118240 | 118692 | ** extension */ |
| | @@ -118319,10 +118771,11 @@ |
| 118319 | 118771 | # define sqlite3_create_module 0 |
| 118320 | 118772 | # define sqlite3_create_module_v2 0 |
| 118321 | 118773 | # define sqlite3_declare_vtab 0 |
| 118322 | 118774 | # define sqlite3_vtab_config 0 |
| 118323 | 118775 | # define sqlite3_vtab_on_conflict 0 |
| 118776 | +# define sqlite3_vtab_collation 0 |
| 118324 | 118777 | #endif |
| 118325 | 118778 | |
| 118326 | 118779 | #ifdef SQLITE_OMIT_SHARED_CACHE |
| 118327 | 118780 | # define sqlite3_enable_shared_cache 0 |
| 118328 | 118781 | #endif |
| | @@ -118686,11 +119139,17 @@ |
| 118686 | 119139 | sqlite3_str_reset, |
| 118687 | 119140 | sqlite3_str_errcode, |
| 118688 | 119141 | sqlite3_str_length, |
| 118689 | 119142 | sqlite3_str_value, |
| 118690 | 119143 | /* Version 3.25.0 and later */ |
| 118691 | | - sqlite3_create_window_function |
| 119144 | + sqlite3_create_window_function, |
| 119145 | + /* Version 3.26.0 and later */ |
| 119146 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 119147 | + sqlite3_normalized_sql |
| 119148 | +#else |
| 119149 | + 0 |
| 119150 | +#endif |
| 118692 | 119151 | }; |
| 118693 | 119152 | |
| 118694 | 119153 | /* |
| 118695 | 119154 | ** Attempt to load an SQLite extension library contained in the file |
| 118696 | 119155 | ** zFile. The entry point is zProc. zProc may be 0 in which case a |
| | @@ -119136,14 +119595,13 @@ |
| 119136 | 119595 | #define PragTyp_WAL_AUTOCHECKPOINT 38 |
| 119137 | 119596 | #define PragTyp_WAL_CHECKPOINT 39 |
| 119138 | 119597 | #define PragTyp_ACTIVATE_EXTENSIONS 40 |
| 119139 | 119598 | #define PragTyp_HEXKEY 41 |
| 119140 | 119599 | #define PragTyp_KEY 42 |
| 119141 | | -#define PragTyp_REKEY 43 |
| 119142 | | -#define PragTyp_LOCK_STATUS 44 |
| 119143 | | -#define PragTyp_PARSER_TRACE 45 |
| 119144 | | -#define PragTyp_STATS 46 |
| 119600 | +#define PragTyp_LOCK_STATUS 43 |
| 119601 | +#define PragTyp_PARSER_TRACE 44 |
| 119602 | +#define PragTyp_STATS 45 |
| 119145 | 119603 | |
| 119146 | 119604 | /* Property flags associated with various pragma. */ |
| 119147 | 119605 | #define PragFlg_NeedSchema 0x01 /* Force schema load before running */ |
| 119148 | 119606 | #define PragFlg_NoColumns 0x02 /* OP_ResultRow called with zero columns */ |
| 119149 | 119607 | #define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */ |
| | @@ -119156,72 +119614,71 @@ |
| 119156 | 119614 | /* Names of columns for pragmas that return multi-column result |
| 119157 | 119615 | ** or that return single-column results where the name of the |
| 119158 | 119616 | ** result column is different from the name of the pragma |
| 119159 | 119617 | */ |
| 119160 | 119618 | static const char *const pragCName[] = { |
| 119161 | | - /* 0 */ "cache_size", /* Used by: default_cache_size */ |
| 119162 | | - /* 1 */ "cid", /* Used by: table_info */ |
| 119163 | | - /* 2 */ "name", |
| 119164 | | - /* 3 */ "type", |
| 119165 | | - /* 4 */ "notnull", |
| 119166 | | - /* 5 */ "dflt_value", |
| 119167 | | - /* 6 */ "pk", |
| 119168 | | - /* 7 */ "tbl", /* Used by: stats */ |
| 119169 | | - /* 8 */ "idx", |
| 119170 | | - /* 9 */ "wdth", |
| 119171 | | - /* 10 */ "hght", |
| 119172 | | - /* 11 */ "flgs", |
| 119173 | | - /* 12 */ "seqno", /* Used by: index_info */ |
| 119174 | | - /* 13 */ "cid", |
| 119175 | | - /* 14 */ "name", |
| 119619 | + /* 0 */ "id", /* Used by: foreign_key_list */ |
| 119620 | + /* 1 */ "seq", |
| 119621 | + /* 2 */ "table", |
| 119622 | + /* 3 */ "from", |
| 119623 | + /* 4 */ "to", |
| 119624 | + /* 5 */ "on_update", |
| 119625 | + /* 6 */ "on_delete", |
| 119626 | + /* 7 */ "match", |
| 119627 | + /* 8 */ "cid", /* Used by: table_xinfo */ |
| 119628 | + /* 9 */ "name", |
| 119629 | + /* 10 */ "type", |
| 119630 | + /* 11 */ "notnull", |
| 119631 | + /* 12 */ "dflt_value", |
| 119632 | + /* 13 */ "pk", |
| 119633 | + /* 14 */ "hidden", |
| 119634 | + /* table_info reuses 8 */ |
| 119176 | 119635 | /* 15 */ "seqno", /* Used by: index_xinfo */ |
| 119177 | 119636 | /* 16 */ "cid", |
| 119178 | 119637 | /* 17 */ "name", |
| 119179 | 119638 | /* 18 */ "desc", |
| 119180 | 119639 | /* 19 */ "coll", |
| 119181 | 119640 | /* 20 */ "key", |
| 119182 | | - /* 21 */ "seq", /* Used by: index_list */ |
| 119183 | | - /* 22 */ "name", |
| 119184 | | - /* 23 */ "unique", |
| 119185 | | - /* 24 */ "origin", |
| 119186 | | - /* 25 */ "partial", |
| 119187 | | - /* 26 */ "seq", /* Used by: database_list */ |
| 119641 | + /* 21 */ "tbl", /* Used by: stats */ |
| 119642 | + /* 22 */ "idx", |
| 119643 | + /* 23 */ "wdth", |
| 119644 | + /* 24 */ "hght", |
| 119645 | + /* 25 */ "flgs", |
| 119646 | + /* 26 */ "seq", /* Used by: index_list */ |
| 119188 | 119647 | /* 27 */ "name", |
| 119189 | | - /* 28 */ "file", |
| 119190 | | - /* 29 */ "name", /* Used by: function_list */ |
| 119191 | | - /* 30 */ "builtin", |
| 119192 | | - /* 31 */ "name", /* Used by: module_list pragma_list */ |
| 119193 | | - /* 32 */ "seq", /* Used by: collation_list */ |
| 119194 | | - /* 33 */ "name", |
| 119195 | | - /* 34 */ "id", /* Used by: foreign_key_list */ |
| 119196 | | - /* 35 */ "seq", |
| 119197 | | - /* 36 */ "table", |
| 119198 | | - /* 37 */ "from", |
| 119199 | | - /* 38 */ "to", |
| 119200 | | - /* 39 */ "on_update", |
| 119201 | | - /* 40 */ "on_delete", |
| 119202 | | - /* 41 */ "match", |
| 119203 | | - /* 42 */ "table", /* Used by: foreign_key_check */ |
| 119204 | | - /* 43 */ "rowid", |
| 119205 | | - /* 44 */ "parent", |
| 119206 | | - /* 45 */ "fkid", |
| 119207 | | - /* 46 */ "busy", /* Used by: wal_checkpoint */ |
| 119208 | | - /* 47 */ "log", |
| 119209 | | - /* 48 */ "checkpointed", |
| 119210 | | - /* 49 */ "timeout", /* Used by: busy_timeout */ |
| 119211 | | - /* 50 */ "database", /* Used by: lock_status */ |
| 119212 | | - /* 51 */ "status", |
| 119648 | + /* 28 */ "unique", |
| 119649 | + /* 29 */ "origin", |
| 119650 | + /* 30 */ "partial", |
| 119651 | + /* 31 */ "table", /* Used by: foreign_key_check */ |
| 119652 | + /* 32 */ "rowid", |
| 119653 | + /* 33 */ "parent", |
| 119654 | + /* 34 */ "fkid", |
| 119655 | + /* index_info reuses 15 */ |
| 119656 | + /* 35 */ "seq", /* Used by: database_list */ |
| 119657 | + /* 36 */ "name", |
| 119658 | + /* 37 */ "file", |
| 119659 | + /* 38 */ "busy", /* Used by: wal_checkpoint */ |
| 119660 | + /* 39 */ "log", |
| 119661 | + /* 40 */ "checkpointed", |
| 119662 | + /* 41 */ "name", /* Used by: function_list */ |
| 119663 | + /* 42 */ "builtin", |
| 119664 | + /* collation_list reuses 26 */ |
| 119665 | + /* 43 */ "database", /* Used by: lock_status */ |
| 119666 | + /* 44 */ "status", |
| 119667 | + /* 45 */ "cache_size", /* Used by: default_cache_size */ |
| 119668 | + /* module_list pragma_list reuses 9 */ |
| 119669 | + /* 46 */ "timeout", /* Used by: busy_timeout */ |
| 119213 | 119670 | }; |
| 119214 | 119671 | |
| 119215 | 119672 | /* Definitions of all built-in pragmas */ |
| 119216 | 119673 | typedef struct PragmaName { |
| 119217 | 119674 | const char *const zName; /* Name of pragma */ |
| 119218 | 119675 | u8 ePragTyp; /* PragTyp_XXX value */ |
| 119219 | 119676 | u8 mPragFlg; /* Zero or more PragFlg_XXX values */ |
| 119220 | 119677 | u8 iPragCName; /* Start of column names in pragCName[] */ |
| 119221 | 119678 | u8 nPragCName; /* Num of col names. 0 means use pragma name */ |
| 119222 | | - u32 iArg; /* Extra argument */ |
| 119679 | + u64 iArg; /* Extra argument */ |
| 119223 | 119680 | } PragmaName; |
| 119224 | 119681 | static const PragmaName aPragmaName[] = { |
| 119225 | 119682 | #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD) |
| 119226 | 119683 | {/* zName: */ "activate_extensions", |
| 119227 | 119684 | /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS, |
| | @@ -119253,11 +119710,11 @@ |
| 119253 | 119710 | #endif |
| 119254 | 119711 | #endif |
| 119255 | 119712 | {/* zName: */ "busy_timeout", |
| 119256 | 119713 | /* ePragTyp: */ PragTyp_BUSY_TIMEOUT, |
| 119257 | 119714 | /* ePragFlg: */ PragFlg_Result0, |
| 119258 | | - /* ColNames: */ 49, 1, |
| 119715 | + /* ColNames: */ 46, 1, |
| 119259 | 119716 | /* iArg: */ 0 }, |
| 119260 | 119717 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) |
| 119261 | 119718 | {/* zName: */ "cache_size", |
| 119262 | 119719 | /* ePragTyp: */ PragTyp_CACHE_SIZE, |
| 119263 | 119720 | /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1, |
| | @@ -119290,11 +119747,11 @@ |
| 119290 | 119747 | #endif |
| 119291 | 119748 | #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) |
| 119292 | 119749 | {/* zName: */ "collation_list", |
| 119293 | 119750 | /* ePragTyp: */ PragTyp_COLLATION_LIST, |
| 119294 | 119751 | /* ePragFlg: */ PragFlg_Result0, |
| 119295 | | - /* ColNames: */ 32, 2, |
| 119752 | + /* ColNames: */ 26, 2, |
| 119296 | 119753 | /* iArg: */ 0 }, |
| 119297 | 119754 | #endif |
| 119298 | 119755 | #if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS) |
| 119299 | 119756 | {/* zName: */ "compile_options", |
| 119300 | 119757 | /* ePragTyp: */ PragTyp_COMPILE_OPTIONS, |
| | @@ -119325,18 +119782,18 @@ |
| 119325 | 119782 | #endif |
| 119326 | 119783 | #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) |
| 119327 | 119784 | {/* zName: */ "database_list", |
| 119328 | 119785 | /* ePragTyp: */ PragTyp_DATABASE_LIST, |
| 119329 | 119786 | /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0, |
| 119330 | | - /* ColNames: */ 26, 3, |
| 119787 | + /* ColNames: */ 35, 3, |
| 119331 | 119788 | /* iArg: */ 0 }, |
| 119332 | 119789 | #endif |
| 119333 | 119790 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED) |
| 119334 | 119791 | {/* zName: */ "default_cache_size", |
| 119335 | 119792 | /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE, |
| 119336 | 119793 | /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1, |
| 119337 | | - /* ColNames: */ 0, 1, |
| 119794 | + /* ColNames: */ 45, 1, |
| 119338 | 119795 | /* iArg: */ 0 }, |
| 119339 | 119796 | #endif |
| 119340 | 119797 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 119341 | 119798 | #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
| 119342 | 119799 | {/* zName: */ "defer_foreign_keys", |
| | @@ -119362,18 +119819,18 @@ |
| 119362 | 119819 | #endif |
| 119363 | 119820 | #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
| 119364 | 119821 | {/* zName: */ "foreign_key_check", |
| 119365 | 119822 | /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK, |
| 119366 | 119823 | /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0, |
| 119367 | | - /* ColNames: */ 42, 4, |
| 119824 | + /* ColNames: */ 31, 4, |
| 119368 | 119825 | /* iArg: */ 0 }, |
| 119369 | 119826 | #endif |
| 119370 | 119827 | #if !defined(SQLITE_OMIT_FOREIGN_KEY) |
| 119371 | 119828 | {/* zName: */ "foreign_key_list", |
| 119372 | 119829 | /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST, |
| 119373 | 119830 | /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt, |
| 119374 | | - /* ColNames: */ 34, 8, |
| 119831 | + /* ColNames: */ 0, 8, |
| 119375 | 119832 | /* iArg: */ 0 }, |
| 119376 | 119833 | #endif |
| 119377 | 119834 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 119378 | 119835 | #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
| 119379 | 119836 | {/* zName: */ "foreign_keys", |
| | @@ -119405,25 +119862,25 @@ |
| 119405 | 119862 | #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) |
| 119406 | 119863 | #if defined(SQLITE_INTROSPECTION_PRAGMAS) |
| 119407 | 119864 | {/* zName: */ "function_list", |
| 119408 | 119865 | /* ePragTyp: */ PragTyp_FUNCTION_LIST, |
| 119409 | 119866 | /* ePragFlg: */ PragFlg_Result0, |
| 119410 | | - /* ColNames: */ 29, 2, |
| 119867 | + /* ColNames: */ 41, 2, |
| 119411 | 119868 | /* iArg: */ 0 }, |
| 119412 | 119869 | #endif |
| 119413 | 119870 | #endif |
| 119414 | 119871 | #if defined(SQLITE_HAS_CODEC) |
| 119415 | 119872 | {/* zName: */ "hexkey", |
| 119416 | 119873 | /* ePragTyp: */ PragTyp_HEXKEY, |
| 119417 | 119874 | /* ePragFlg: */ 0, |
| 119418 | 119875 | /* ColNames: */ 0, 0, |
| 119419 | | - /* iArg: */ 0 }, |
| 119876 | + /* iArg: */ 2 }, |
| 119420 | 119877 | {/* zName: */ "hexrekey", |
| 119421 | 119878 | /* ePragTyp: */ PragTyp_HEXKEY, |
| 119422 | 119879 | /* ePragFlg: */ 0, |
| 119423 | 119880 | /* ColNames: */ 0, 0, |
| 119424 | | - /* iArg: */ 0 }, |
| 119881 | + /* iArg: */ 3 }, |
| 119425 | 119882 | #endif |
| 119426 | 119883 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 119427 | 119884 | #if !defined(SQLITE_OMIT_CHECK) |
| 119428 | 119885 | {/* zName: */ "ignore_check_constraints", |
| 119429 | 119886 | /* ePragTyp: */ PragTyp_FLAG, |
| | @@ -119441,16 +119898,16 @@ |
| 119441 | 119898 | #endif |
| 119442 | 119899 | #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) |
| 119443 | 119900 | {/* zName: */ "index_info", |
| 119444 | 119901 | /* ePragTyp: */ PragTyp_INDEX_INFO, |
| 119445 | 119902 | /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt, |
| 119446 | | - /* ColNames: */ 12, 3, |
| 119903 | + /* ColNames: */ 15, 3, |
| 119447 | 119904 | /* iArg: */ 0 }, |
| 119448 | 119905 | {/* zName: */ "index_list", |
| 119449 | 119906 | /* ePragTyp: */ PragTyp_INDEX_LIST, |
| 119450 | 119907 | /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt, |
| 119451 | | - /* ColNames: */ 21, 5, |
| 119908 | + /* ColNames: */ 26, 5, |
| 119452 | 119909 | /* iArg: */ 0 }, |
| 119453 | 119910 | {/* zName: */ "index_xinfo", |
| 119454 | 119911 | /* ePragTyp: */ PragTyp_INDEX_INFO, |
| 119455 | 119912 | /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt, |
| 119456 | 119913 | /* ColNames: */ 15, 6, |
| | @@ -119503,11 +119960,11 @@ |
| 119503 | 119960 | #endif |
| 119504 | 119961 | #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) |
| 119505 | 119962 | {/* zName: */ "lock_status", |
| 119506 | 119963 | /* ePragTyp: */ PragTyp_LOCK_STATUS, |
| 119507 | 119964 | /* ePragFlg: */ PragFlg_Result0, |
| 119508 | | - /* ColNames: */ 50, 2, |
| 119965 | + /* ColNames: */ 43, 2, |
| 119509 | 119966 | /* iArg: */ 0 }, |
| 119510 | 119967 | #endif |
| 119511 | 119968 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) |
| 119512 | 119969 | {/* zName: */ "locking_mode", |
| 119513 | 119970 | /* ePragTyp: */ PragTyp_LOCKING_MODE, |
| | @@ -119529,11 +119986,11 @@ |
| 119529 | 119986 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 119530 | 119987 | #if defined(SQLITE_INTROSPECTION_PRAGMAS) |
| 119531 | 119988 | {/* zName: */ "module_list", |
| 119532 | 119989 | /* ePragTyp: */ PragTyp_MODULE_LIST, |
| 119533 | 119990 | /* ePragFlg: */ PragFlg_Result0, |
| 119534 | | - /* ColNames: */ 31, 1, |
| 119991 | + /* ColNames: */ 9, 1, |
| 119535 | 119992 | /* iArg: */ 0 }, |
| 119536 | 119993 | #endif |
| 119537 | 119994 | #endif |
| 119538 | 119995 | #endif |
| 119539 | 119996 | {/* zName: */ "optimize", |
| | @@ -119562,11 +120019,11 @@ |
| 119562 | 120019 | #endif |
| 119563 | 120020 | #if defined(SQLITE_INTROSPECTION_PRAGMAS) |
| 119564 | 120021 | {/* zName: */ "pragma_list", |
| 119565 | 120022 | /* ePragTyp: */ PragTyp_PRAGMA_LIST, |
| 119566 | 120023 | /* ePragFlg: */ PragFlg_Result0, |
| 119567 | | - /* ColNames: */ 31, 1, |
| 120024 | + /* ColNames: */ 9, 1, |
| 119568 | 120025 | /* iArg: */ 0 }, |
| 119569 | 120026 | #endif |
| 119570 | 120027 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 119571 | 120028 | {/* zName: */ "query_only", |
| 119572 | 120029 | /* ePragTyp: */ PragTyp_FLAG, |
| | @@ -119593,14 +120050,14 @@ |
| 119593 | 120050 | /* ColNames: */ 0, 0, |
| 119594 | 120051 | /* iArg: */ SQLITE_RecTriggers }, |
| 119595 | 120052 | #endif |
| 119596 | 120053 | #if defined(SQLITE_HAS_CODEC) |
| 119597 | 120054 | {/* zName: */ "rekey", |
| 119598 | | - /* ePragTyp: */ PragTyp_REKEY, |
| 120055 | + /* ePragTyp: */ PragTyp_KEY, |
| 119599 | 120056 | /* ePragFlg: */ 0, |
| 119600 | 120057 | /* ColNames: */ 0, 0, |
| 119601 | | - /* iArg: */ 0 }, |
| 120058 | + /* iArg: */ 1 }, |
| 119602 | 120059 | #endif |
| 119603 | 120060 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 119604 | 120061 | {/* zName: */ "reverse_unordered_selects", |
| 119605 | 120062 | /* ePragTyp: */ PragTyp_FLAG, |
| 119606 | 120063 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| | @@ -119649,11 +120106,11 @@ |
| 119649 | 120106 | #endif |
| 119650 | 120107 | #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG) |
| 119651 | 120108 | {/* zName: */ "stats", |
| 119652 | 120109 | /* ePragTyp: */ PragTyp_STATS, |
| 119653 | 120110 | /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq, |
| 119654 | | - /* ColNames: */ 7, 5, |
| 120111 | + /* ColNames: */ 21, 5, |
| 119655 | 120112 | /* iArg: */ 0 }, |
| 119656 | 120113 | #endif |
| 119657 | 120114 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) |
| 119658 | 120115 | {/* zName: */ "synchronous", |
| 119659 | 120116 | /* ePragTyp: */ PragTyp_SYNCHRONOUS, |
| | @@ -119663,12 +120120,17 @@ |
| 119663 | 120120 | #endif |
| 119664 | 120121 | #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) |
| 119665 | 120122 | {/* zName: */ "table_info", |
| 119666 | 120123 | /* ePragTyp: */ PragTyp_TABLE_INFO, |
| 119667 | 120124 | /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt, |
| 119668 | | - /* ColNames: */ 1, 6, |
| 120125 | + /* ColNames: */ 8, 6, |
| 119669 | 120126 | /* iArg: */ 0 }, |
| 120127 | + {/* zName: */ "table_xinfo", |
| 120128 | + /* ePragTyp: */ PragTyp_TABLE_INFO, |
| 120129 | + /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt, |
| 120130 | + /* ColNames: */ 8, 7, |
| 120131 | + /* iArg: */ 1 }, |
| 119670 | 120132 | #endif |
| 119671 | 120133 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) |
| 119672 | 120134 | {/* zName: */ "temp_store", |
| 119673 | 120135 | /* ePragTyp: */ PragTyp_TEMP_STORE, |
| 119674 | 120136 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| | @@ -119677,10 +120139,22 @@ |
| 119677 | 120139 | {/* zName: */ "temp_store_directory", |
| 119678 | 120140 | /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY, |
| 119679 | 120141 | /* ePragFlg: */ PragFlg_NoColumns1, |
| 119680 | 120142 | /* ColNames: */ 0, 0, |
| 119681 | 120143 | /* iArg: */ 0 }, |
| 120144 | +#endif |
| 120145 | +#if defined(SQLITE_HAS_CODEC) |
| 120146 | + {/* zName: */ "textkey", |
| 120147 | + /* ePragTyp: */ PragTyp_KEY, |
| 120148 | + /* ePragFlg: */ 0, |
| 120149 | + /* ColNames: */ 0, 0, |
| 120150 | + /* iArg: */ 4 }, |
| 120151 | + {/* zName: */ "textrekey", |
| 120152 | + /* ePragTyp: */ PragTyp_KEY, |
| 120153 | + /* ePragFlg: */ 0, |
| 120154 | + /* ColNames: */ 0, 0, |
| 120155 | + /* iArg: */ 5 }, |
| 119682 | 120156 | #endif |
| 119683 | 120157 | {/* zName: */ "threads", |
| 119684 | 120158 | /* ePragTyp: */ PragTyp_THREADS, |
| 119685 | 120159 | /* ePragFlg: */ PragFlg_Result0, |
| 119686 | 120160 | /* ColNames: */ 0, 0, |
| | @@ -119728,22 +120202,22 @@ |
| 119728 | 120202 | /* ColNames: */ 0, 0, |
| 119729 | 120203 | /* iArg: */ 0 }, |
| 119730 | 120204 | {/* zName: */ "wal_checkpoint", |
| 119731 | 120205 | /* ePragTyp: */ PragTyp_WAL_CHECKPOINT, |
| 119732 | 120206 | /* ePragFlg: */ PragFlg_NeedSchema, |
| 119733 | | - /* ColNames: */ 46, 3, |
| 120207 | + /* ColNames: */ 38, 3, |
| 119734 | 120208 | /* iArg: */ 0 }, |
| 119735 | 120209 | #endif |
| 119736 | 120210 | #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) |
| 119737 | 120211 | {/* zName: */ "writable_schema", |
| 119738 | 120212 | /* ePragTyp: */ PragTyp_FLAG, |
| 119739 | 120213 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 119740 | 120214 | /* ColNames: */ 0, 0, |
| 119741 | | - /* iArg: */ SQLITE_WriteSchema }, |
| 120215 | + /* iArg: */ SQLITE_WriteSchema|SQLITE_NoSchemaError }, |
| 119742 | 120216 | #endif |
| 119743 | 120217 | }; |
| 119744 | | -/* Number of pragmas: 61 on by default, 78 total. */ |
| 120218 | +/* Number of pragmas: 62 on by default, 81 total. */ |
| 119745 | 120219 | |
| 119746 | 120220 | /************** End of pragma.h **********************************************/ |
| 119747 | 120221 | /************** Continuing where we left off in pragma.c *********************/ |
| 119748 | 120222 | |
| 119749 | 120223 | /* |
| | @@ -120751,11 +121225,11 @@ |
| 120751 | 121225 | case PragTyp_FLAG: { |
| 120752 | 121226 | if( zRight==0 ){ |
| 120753 | 121227 | setPragmaResultColumnNames(v, pPragma); |
| 120754 | 121228 | returnSingleInt(v, (db->flags & pPragma->iArg)!=0 ); |
| 120755 | 121229 | }else{ |
| 120756 | | - int mask = pPragma->iArg; /* Mask of bits to set or clear. */ |
| 121230 | + u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */ |
| 120757 | 121231 | if( db->autoCommit==0 ){ |
| 120758 | 121232 | /* Foreign key support may not be enabled or disabled while not |
| 120759 | 121233 | ** in auto-commit mode. */ |
| 120760 | 121234 | mask &= ~(SQLITE_ForeignKeys); |
| 120761 | 121235 | } |
| | @@ -120800,19 +121274,21 @@ |
| 120800 | 121274 | */ |
| 120801 | 121275 | case PragTyp_TABLE_INFO: if( zRight ){ |
| 120802 | 121276 | Table *pTab; |
| 120803 | 121277 | pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb); |
| 120804 | 121278 | if( pTab ){ |
| 121279 | + int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 120805 | 121280 | int i, k; |
| 120806 | 121281 | int nHidden = 0; |
| 120807 | 121282 | Column *pCol; |
| 120808 | 121283 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 120809 | | - pParse->nMem = 6; |
| 120810 | | - sqlite3CodeVerifySchema(pParse, iDb); |
| 121284 | + pParse->nMem = 7; |
| 121285 | + sqlite3CodeVerifySchema(pParse, iTabDb); |
| 120811 | 121286 | sqlite3ViewGetColumnNames(pParse, pTab); |
| 120812 | 121287 | for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){ |
| 120813 | | - if( IsHiddenColumn(pCol) ){ |
| 121288 | + int isHidden = IsHiddenColumn(pCol); |
| 121289 | + if( isHidden && pPragma->iArg==0 ){ |
| 120814 | 121290 | nHidden++; |
| 120815 | 121291 | continue; |
| 120816 | 121292 | } |
| 120817 | 121293 | if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){ |
| 120818 | 121294 | k = 0; |
| | @@ -120820,17 +121296,18 @@ |
| 120820 | 121296 | k = 1; |
| 120821 | 121297 | }else{ |
| 120822 | 121298 | for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){} |
| 120823 | 121299 | } |
| 120824 | 121300 | assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN ); |
| 120825 | | - sqlite3VdbeMultiLoad(v, 1, "issisi", |
| 121301 | + sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi", |
| 120826 | 121302 | i-nHidden, |
| 120827 | 121303 | pCol->zName, |
| 120828 | 121304 | sqlite3ColumnType(pCol,""), |
| 120829 | 121305 | pCol->notNull ? 1 : 0, |
| 120830 | 121306 | pCol->pDflt ? pCol->pDflt->u.zToken : 0, |
| 120831 | | - k); |
| 121307 | + k, |
| 121308 | + isHidden); |
| 120832 | 121309 | } |
| 120833 | 121310 | } |
| 120834 | 121311 | } |
| 120835 | 121312 | break; |
| 120836 | 121313 | |
| | @@ -120864,10 +121341,11 @@ |
| 120864 | 121341 | case PragTyp_INDEX_INFO: if( zRight ){ |
| 120865 | 121342 | Index *pIdx; |
| 120866 | 121343 | Table *pTab; |
| 120867 | 121344 | pIdx = sqlite3FindIndex(db, zRight, zDb); |
| 120868 | 121345 | if( pIdx ){ |
| 121346 | + int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema); |
| 120869 | 121347 | int i; |
| 120870 | 121348 | int mx; |
| 120871 | 121349 | if( pPragma->iArg ){ |
| 120872 | 121350 | /* PRAGMA index_xinfo (newer version with more rows and columns) */ |
| 120873 | 121351 | mx = pIdx->nColumn; |
| | @@ -120876,11 +121354,11 @@ |
| 120876 | 121354 | /* PRAGMA index_info (legacy version) */ |
| 120877 | 121355 | mx = pIdx->nKeyCol; |
| 120878 | 121356 | pParse->nMem = 3; |
| 120879 | 121357 | } |
| 120880 | 121358 | pTab = pIdx->pTable; |
| 120881 | | - sqlite3CodeVerifySchema(pParse, iDb); |
| 121359 | + sqlite3CodeVerifySchema(pParse, iIdxDb); |
| 120882 | 121360 | assert( pParse->nMem<=pPragma->nPragCName ); |
| 120883 | 121361 | for(i=0; i<mx; i++){ |
| 120884 | 121362 | i16 cnum = pIdx->aiColumn[i]; |
| 120885 | 121363 | sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum, |
| 120886 | 121364 | cnum<0 ? 0 : pTab->aCol[cnum].zName); |
| | @@ -120900,12 +121378,13 @@ |
| 120900 | 121378 | Index *pIdx; |
| 120901 | 121379 | Table *pTab; |
| 120902 | 121380 | int i; |
| 120903 | 121381 | pTab = sqlite3FindTable(db, zRight, zDb); |
| 120904 | 121382 | if( pTab ){ |
| 121383 | + int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 120905 | 121384 | pParse->nMem = 5; |
| 120906 | | - sqlite3CodeVerifySchema(pParse, iDb); |
| 121385 | + sqlite3CodeVerifySchema(pParse, iTabDb); |
| 120907 | 121386 | for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){ |
| 120908 | 121387 | const char *azOrigin[] = { "c", "u", "pk" }; |
| 120909 | 121388 | sqlite3VdbeMultiLoad(v, 1, "isisi", |
| 120910 | 121389 | i, |
| 120911 | 121390 | pIdx->zName, |
| | @@ -120948,10 +121427,11 @@ |
| 120948 | 121427 | HashElem *j; |
| 120949 | 121428 | FuncDef *p; |
| 120950 | 121429 | pParse->nMem = 2; |
| 120951 | 121430 | for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){ |
| 120952 | 121431 | for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){ |
| 121432 | + if( p->funcFlags & SQLITE_FUNC_INTERNAL ) continue; |
| 120953 | 121433 | sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 1); |
| 120954 | 121434 | } |
| 120955 | 121435 | } |
| 120956 | 121436 | for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){ |
| 120957 | 121437 | p = (FuncDef*)sqliteHashData(j); |
| | @@ -120989,13 +121469,14 @@ |
| 120989 | 121469 | Table *pTab; |
| 120990 | 121470 | pTab = sqlite3FindTable(db, zRight, zDb); |
| 120991 | 121471 | if( pTab ){ |
| 120992 | 121472 | pFK = pTab->pFKey; |
| 120993 | 121473 | if( pFK ){ |
| 121474 | + int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 120994 | 121475 | int i = 0; |
| 120995 | 121476 | pParse->nMem = 8; |
| 120996 | | - sqlite3CodeVerifySchema(pParse, iDb); |
| 121477 | + sqlite3CodeVerifySchema(pParse, iTabDb); |
| 120997 | 121478 | while(pFK){ |
| 120998 | 121479 | int j; |
| 120999 | 121480 | for(j=0; j<pFK->nCol; j++){ |
| 121000 | 121481 | sqlite3VdbeMultiLoad(v, 1, "iissssss", |
| 121001 | 121482 | i, |
| | @@ -121036,36 +121517,38 @@ |
| 121036 | 121517 | |
| 121037 | 121518 | regResult = pParse->nMem+1; |
| 121038 | 121519 | pParse->nMem += 4; |
| 121039 | 121520 | regKey = ++pParse->nMem; |
| 121040 | 121521 | regRow = ++pParse->nMem; |
| 121041 | | - sqlite3CodeVerifySchema(pParse, iDb); |
| 121042 | 121522 | k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash); |
| 121043 | 121523 | while( k ){ |
| 121524 | + int iTabDb; |
| 121044 | 121525 | if( zRight ){ |
| 121045 | 121526 | pTab = sqlite3LocateTable(pParse, 0, zRight, zDb); |
| 121046 | 121527 | k = 0; |
| 121047 | 121528 | }else{ |
| 121048 | 121529 | pTab = (Table*)sqliteHashData(k); |
| 121049 | 121530 | k = sqliteHashNext(k); |
| 121050 | 121531 | } |
| 121051 | 121532 | if( pTab==0 || pTab->pFKey==0 ) continue; |
| 121052 | | - sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 121533 | + iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 121534 | + sqlite3CodeVerifySchema(pParse, iTabDb); |
| 121535 | + sqlite3TableLock(pParse, iTabDb, pTab->tnum, 0, pTab->zName); |
| 121053 | 121536 | if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow; |
| 121054 | | - sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead); |
| 121537 | + sqlite3OpenTable(pParse, 0, iTabDb, pTab, OP_OpenRead); |
| 121055 | 121538 | sqlite3VdbeLoadString(v, regResult, pTab->zName); |
| 121056 | 121539 | for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){ |
| 121057 | 121540 | pParent = sqlite3FindTable(db, pFK->zTo, zDb); |
| 121058 | 121541 | if( pParent==0 ) continue; |
| 121059 | 121542 | pIdx = 0; |
| 121060 | | - sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName); |
| 121543 | + sqlite3TableLock(pParse, iTabDb, pParent->tnum, 0, pParent->zName); |
| 121061 | 121544 | x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0); |
| 121062 | 121545 | if( x==0 ){ |
| 121063 | 121546 | if( pIdx==0 ){ |
| 121064 | | - sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead); |
| 121547 | + sqlite3OpenTable(pParse, i, iTabDb, pParent, OP_OpenRead); |
| 121065 | 121548 | }else{ |
| 121066 | | - sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb); |
| 121549 | + sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iTabDb); |
| 121067 | 121550 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 121068 | 121551 | } |
| 121069 | 121552 | }else{ |
| 121070 | 121553 | k = 0; |
| 121071 | 121554 | break; |
| | @@ -121830,16 +122313,28 @@ |
| 121830 | 122313 | break; |
| 121831 | 122314 | } |
| 121832 | 122315 | #endif |
| 121833 | 122316 | |
| 121834 | 122317 | #ifdef SQLITE_HAS_CODEC |
| 122318 | + /* Pragma iArg |
| 122319 | + ** ---------- ------ |
| 122320 | + ** key 0 |
| 122321 | + ** rekey 1 |
| 122322 | + ** hexkey 2 |
| 122323 | + ** hexrekey 3 |
| 122324 | + ** textkey 4 |
| 122325 | + ** textrekey 5 |
| 122326 | + */ |
| 121835 | 122327 | case PragTyp_KEY: { |
| 121836 | | - if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight)); |
| 121837 | | - break; |
| 121838 | | - } |
| 121839 | | - case PragTyp_REKEY: { |
| 121840 | | - if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight)); |
| 122328 | + if( zRight ){ |
| 122329 | + int n = pPragma->iArg<4 ? sqlite3Strlen30(zRight) : -1; |
| 122330 | + if( (pPragma->iArg & 1)==0 ){ |
| 122331 | + sqlite3_key_v2(db, zDb, zRight, n); |
| 122332 | + }else{ |
| 122333 | + sqlite3_rekey_v2(db, zDb, zRight, n); |
| 122334 | + } |
| 122335 | + } |
| 121841 | 122336 | break; |
| 121842 | 122337 | } |
| 121843 | 122338 | case PragTyp_HEXKEY: { |
| 121844 | 122339 | if( zRight ){ |
| 121845 | 122340 | u8 iByte; |
| | @@ -121847,11 +122342,11 @@ |
| 121847 | 122342 | char zKey[40]; |
| 121848 | 122343 | for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){ |
| 121849 | 122344 | iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]); |
| 121850 | 122345 | if( (i&1)!=0 ) zKey[i/2] = iByte; |
| 121851 | 122346 | } |
| 121852 | | - if( (zLeft[3] & 0xf)==0xb ){ |
| 122347 | + if( (pPragma->iArg & 1)==0 ){ |
| 121853 | 122348 | sqlite3_key_v2(db, zDb, zKey, i/2); |
| 121854 | 122349 | }else{ |
| 121855 | 122350 | sqlite3_rekey_v2(db, zDb, zKey, i/2); |
| 121856 | 122351 | } |
| 121857 | 122352 | } |
| | @@ -122177,11 +122672,12 @@ |
| 122177 | 122672 | 0, /* xRollback - rollback transaction */ |
| 122178 | 122673 | 0, /* xFindFunction - function overloading */ |
| 122179 | 122674 | 0, /* xRename - rename the table */ |
| 122180 | 122675 | 0, /* xSavepoint */ |
| 122181 | 122676 | 0, /* xRelease */ |
| 122182 | | - 0 /* xRollbackTo */ |
| 122677 | + 0, /* xRollbackTo */ |
| 122678 | + 0 /* xShadowName */ |
| 122183 | 122679 | }; |
| 122184 | 122680 | |
| 122185 | 122681 | /* |
| 122186 | 122682 | ** Check to see if zTabName is really the name of a pragma. If it is, |
| 122187 | 122683 | ** then register an eponymous virtual table for that pragma and return |
| | @@ -122530,12 +123026,12 @@ |
| 122530 | 123026 | } |
| 122531 | 123027 | if( db->mallocFailed ){ |
| 122532 | 123028 | rc = SQLITE_NOMEM_BKPT; |
| 122533 | 123029 | sqlite3ResetAllSchemasOfConnection(db); |
| 122534 | 123030 | } |
| 122535 | | - if( rc==SQLITE_OK || (db->flags&SQLITE_WriteSchema)){ |
| 122536 | | - /* Black magic: If the SQLITE_WriteSchema flag is set, then consider |
| 123031 | + if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){ |
| 123032 | + /* Black magic: If the SQLITE_NoSchemaError flag is set, then consider |
| 122537 | 123033 | ** the schema loaded, even if errors occurred. In this situation the |
| 122538 | 123034 | ** current sqlite3_prepare() operation will fail, but the following one |
| 122539 | 123035 | ** will attempt to compile the supplied statement against whatever subset |
| 122540 | 123036 | ** of the schema was loaded before the error occurred. The primary |
| 122541 | 123037 | ** purpose of this is to allow access to the sqlite_master table |
| | @@ -122912,10 +123408,298 @@ |
| 122912 | 123408 | assert( (rc&db->errMask)==rc ); |
| 122913 | 123409 | sqlite3_mutex_leave(db->mutex); |
| 122914 | 123410 | return rc; |
| 122915 | 123411 | } |
| 122916 | 123412 | |
| 123413 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 123414 | +/* |
| 123415 | +** Checks if the specified token is a table, column, or function name, |
| 123416 | +** based on the databases associated with the statement being prepared. |
| 123417 | +** If the function fails, zero is returned and pRc is filled with the |
| 123418 | +** error code. |
| 123419 | +*/ |
| 123420 | +static int shouldTreatAsIdentifier( |
| 123421 | + sqlite3 *db, /* Database handle. */ |
| 123422 | + const char *zToken, /* Pointer to start of token to be checked */ |
| 123423 | + int nToken, /* Length of token to be checked */ |
| 123424 | + int *pRc /* Pointer to error code upon failure */ |
| 123425 | +){ |
| 123426 | + int bFound = 0; /* Non-zero if token is an identifier name. */ |
| 123427 | + int i, j; /* Database and column loop indexes. */ |
| 123428 | + Schema *pSchema; /* Schema for current database. */ |
| 123429 | + Hash *pHash; /* Hash table of tables for current database. */ |
| 123430 | + HashElem *e; /* Hash element for hash table iteration. */ |
| 123431 | + Table *pTab; /* Database table for columns being checked. */ |
| 123432 | + |
| 123433 | + if( sqlite3IsRowidN(zToken, nToken) ){ |
| 123434 | + return 1; |
| 123435 | + } |
| 123436 | + if( nToken>0 ){ |
| 123437 | + int hash = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zToken[0]], nToken); |
| 123438 | + if( sqlite3FunctionSearchN(hash, zToken, nToken) ) return 1; |
| 123439 | + } |
| 123440 | + assert( db!=0 ); |
| 123441 | + sqlite3_mutex_enter(db->mutex); |
| 123442 | + sqlite3BtreeEnterAll(db); |
| 123443 | + for(i=0; i<db->nDb; i++){ |
| 123444 | + pHash = &db->aFunc; |
| 123445 | + if( sqlite3HashFindN(pHash, zToken, nToken) ){ |
| 123446 | + bFound = 1; |
| 123447 | + break; |
| 123448 | + } |
| 123449 | + pSchema = db->aDb[i].pSchema; |
| 123450 | + if( pSchema==0 ) continue; |
| 123451 | + pHash = &pSchema->tblHash; |
| 123452 | + if( sqlite3HashFindN(pHash, zToken, nToken) ){ |
| 123453 | + bFound = 1; |
| 123454 | + break; |
| 123455 | + } |
| 123456 | + for(e=sqliteHashFirst(pHash); e; e=sqliteHashNext(e)){ |
| 123457 | + pTab = sqliteHashData(e); |
| 123458 | + if( pTab==0 ) continue; |
| 123459 | + pHash = pTab->pColHash; |
| 123460 | + if( pHash==0 ){ |
| 123461 | + pTab->pColHash = pHash = sqlite3_malloc(sizeof(Hash)); |
| 123462 | + if( pHash ){ |
| 123463 | + sqlite3HashInit(pHash); |
| 123464 | + for(j=0; j<pTab->nCol; j++){ |
| 123465 | + Column *pCol = &pTab->aCol[j]; |
| 123466 | + sqlite3HashInsert(pHash, pCol->zName, pCol); |
| 123467 | + } |
| 123468 | + }else{ |
| 123469 | + *pRc = SQLITE_NOMEM_BKPT; |
| 123470 | + bFound = 0; |
| 123471 | + goto done; |
| 123472 | + } |
| 123473 | + } |
| 123474 | + if( pHash && sqlite3HashFindN(pHash, zToken, nToken) ){ |
| 123475 | + bFound = 1; |
| 123476 | + goto done; |
| 123477 | + } |
| 123478 | + } |
| 123479 | + } |
| 123480 | +done: |
| 123481 | + sqlite3BtreeLeaveAll(db); |
| 123482 | + sqlite3_mutex_leave(db->mutex); |
| 123483 | + return bFound; |
| 123484 | +} |
| 123485 | + |
| 123486 | +/* |
| 123487 | +** Attempt to estimate the final output buffer size needed for the fully |
| 123488 | +** normalized version of the specified SQL string. This should take into |
| 123489 | +** account any potential expansion that could occur (e.g. via IN clauses |
| 123490 | +** being expanded, etc). This size returned is the total number of bytes |
| 123491 | +** including the NUL terminator. |
| 123492 | +*/ |
| 123493 | +static int estimateNormalizedSize( |
| 123494 | + const char *zSql, /* The original SQL string */ |
| 123495 | + int nSql, /* Length of original SQL string */ |
| 123496 | + u8 prepFlags /* The flags passed to sqlite3_prepare_v3() */ |
| 123497 | +){ |
| 123498 | + int nOut = nSql + 4; |
| 123499 | + const char *z = zSql; |
| 123500 | + while( nOut<nSql*5 ){ |
| 123501 | + while( z[0]!=0 && z[0]!='I' && z[0]!='i' ){ z++; } |
| 123502 | + if( z[0]==0 ) break; |
| 123503 | + z++; |
| 123504 | + if( z[0]!='N' && z[0]!='n' ) break; |
| 123505 | + z++; |
| 123506 | + while( sqlite3Isspace(z[0]) ){ z++; } |
| 123507 | + if( z[0]!='(' ) break; |
| 123508 | + z++; |
| 123509 | + nOut += 5; /* ?,?,? */ |
| 123510 | + } |
| 123511 | + return nOut; |
| 123512 | +} |
| 123513 | + |
| 123514 | +/* |
| 123515 | +** Copy the current token into the output buffer while dealing with quoted |
| 123516 | +** identifiers. By default, all letters will be converted into lowercase. |
| 123517 | +** If the bUpper flag is set, uppercase will be used. The piOut argument |
| 123518 | +** will be used to update the target index into the output string. |
| 123519 | +*/ |
| 123520 | +static void copyNormalizedToken( |
| 123521 | + const char *zSql, /* The original SQL string */ |
| 123522 | + int iIn, /* Current index into the original SQL string */ |
| 123523 | + int nToken, /* Number of bytes in the current token */ |
| 123524 | + int tokenFlags, /* Flags returned by the tokenizer */ |
| 123525 | + char *zOut, /* The output string */ |
| 123526 | + int *piOut /* Pointer to target index into the output string */ |
| 123527 | +){ |
| 123528 | + int bQuoted = tokenFlags & SQLITE_TOKEN_QUOTED; |
| 123529 | + int bKeyword = tokenFlags & SQLITE_TOKEN_KEYWORD; |
| 123530 | + int j = *piOut, k = 0; |
| 123531 | + for(; k<nToken; k++){ |
| 123532 | + if( bQuoted ){ |
| 123533 | + if( k==0 && iIn>0 ){ |
| 123534 | + zOut[j++] = '"'; |
| 123535 | + continue; |
| 123536 | + }else if( k==nToken-1 ){ |
| 123537 | + zOut[j++] = '"'; |
| 123538 | + continue; |
| 123539 | + } |
| 123540 | + } |
| 123541 | + if( bKeyword ){ |
| 123542 | + zOut[j++] = sqlite3Toupper(zSql[iIn+k]); |
| 123543 | + }else{ |
| 123544 | + zOut[j++] = sqlite3Tolower(zSql[iIn+k]); |
| 123545 | + } |
| 123546 | + } |
| 123547 | + *piOut = j; |
| 123548 | +} |
| 123549 | + |
| 123550 | +/* |
| 123551 | +** Perform normalization of the SQL contained in the prepared statement and |
| 123552 | +** store the result in the zNormSql field. The schema for the associated |
| 123553 | +** databases are consulted while performing the normalization in order to |
| 123554 | +** determine if a token appears to be an identifier. All identifiers are |
| 123555 | +** left intact in the normalized SQL and all literals are replaced with a |
| 123556 | +** single '?'. |
| 123557 | +*/ |
| 123558 | +SQLITE_PRIVATE void sqlite3Normalize( |
| 123559 | + Vdbe *pVdbe, /* VM being reprepared */ |
| 123560 | + const char *zSql, /* The original SQL string */ |
| 123561 | + int nSql, /* Size of the input string in bytes */ |
| 123562 | + u8 prepFlags /* The flags passed to sqlite3_prepare_v3() */ |
| 123563 | +){ |
| 123564 | + sqlite3 *db; /* Database handle. */ |
| 123565 | + char *z; /* The output string */ |
| 123566 | + int nZ; /* Size of the output string in bytes */ |
| 123567 | + int i; /* Next character to read from zSql[] */ |
| 123568 | + int j; /* Next character to fill in on z[] */ |
| 123569 | + int tokenType = 0; /* Type of the next token */ |
| 123570 | + int prevTokenType = 0; /* Type of the previous token, except spaces */ |
| 123571 | + int n; /* Size of the next token */ |
| 123572 | + int nParen = 0; /* Nesting level of parenthesis */ |
| 123573 | + Hash inHash; /* Table of parenthesis levels to output index. */ |
| 123574 | + |
| 123575 | + db = sqlite3VdbeDb(pVdbe); |
| 123576 | + assert( db!=0 ); |
| 123577 | + assert( pVdbe->zNormSql==0 ); |
| 123578 | + if( zSql==0 ) return; |
| 123579 | + nZ = estimateNormalizedSize(zSql, nSql, prepFlags); |
| 123580 | + z = sqlite3DbMallocRawNN(db, nZ); |
| 123581 | + if( z==0 ) return; |
| 123582 | + sqlite3HashInit(&inHash); |
| 123583 | + for(i=j=0; i<nSql && zSql[i]; i+=n){ |
| 123584 | + int flags = 0; |
| 123585 | + if( tokenType!=TK_SPACE ) prevTokenType = tokenType; |
| 123586 | + n = sqlite3GetTokenNormalized((unsigned char*)zSql+i, &tokenType, &flags); |
| 123587 | + switch( tokenType ){ |
| 123588 | + case TK_SPACE: { |
| 123589 | + break; |
| 123590 | + } |
| 123591 | + case TK_ILLEGAL: { |
| 123592 | + sqlite3DbFree(db, z); |
| 123593 | + sqlite3HashClear(&inHash); |
| 123594 | + return; |
| 123595 | + } |
| 123596 | + case TK_STRING: |
| 123597 | + case TK_INTEGER: |
| 123598 | + case TK_FLOAT: |
| 123599 | + case TK_VARIABLE: |
| 123600 | + case TK_BLOB: { |
| 123601 | + z[j++] = '?'; |
| 123602 | + break; |
| 123603 | + } |
| 123604 | + case TK_LP: |
| 123605 | + case TK_RP: { |
| 123606 | + if( tokenType==TK_LP ){ |
| 123607 | + nParen++; |
| 123608 | + if( prevTokenType==TK_IN ){ |
| 123609 | + assert( nParen<nSql ); |
| 123610 | + sqlite3HashInsert(&inHash, zSql+nParen, SQLITE_INT_TO_PTR(j)); |
| 123611 | + } |
| 123612 | + }else{ |
| 123613 | + int jj; |
| 123614 | + assert( nParen<nSql ); |
| 123615 | + jj = SQLITE_PTR_TO_INT(sqlite3HashFind(&inHash, zSql+nParen)); |
| 123616 | + if( jj>0 ){ |
| 123617 | + sqlite3HashInsert(&inHash, zSql+nParen, 0); |
| 123618 | + assert( jj+6<nZ ); |
| 123619 | + memcpy(z+jj+1, "?,?,?", 5); |
| 123620 | + j = jj+6; |
| 123621 | + assert( nZ-1-j>=0 ); |
| 123622 | + assert( nZ-1-j<nZ ); |
| 123623 | + memset(z+j, 0, nZ-1-j); |
| 123624 | + } |
| 123625 | + nParen--; |
| 123626 | + } |
| 123627 | + assert( nParen>=0 ); |
| 123628 | + /* Fall through */ |
| 123629 | + } |
| 123630 | + case TK_MINUS: |
| 123631 | + case TK_SEMI: |
| 123632 | + case TK_PLUS: |
| 123633 | + case TK_STAR: |
| 123634 | + case TK_SLASH: |
| 123635 | + case TK_REM: |
| 123636 | + case TK_EQ: |
| 123637 | + case TK_LE: |
| 123638 | + case TK_NE: |
| 123639 | + case TK_LSHIFT: |
| 123640 | + case TK_LT: |
| 123641 | + case TK_RSHIFT: |
| 123642 | + case TK_GT: |
| 123643 | + case TK_GE: |
| 123644 | + case TK_BITOR: |
| 123645 | + case TK_CONCAT: |
| 123646 | + case TK_COMMA: |
| 123647 | + case TK_BITAND: |
| 123648 | + case TK_BITNOT: |
| 123649 | + case TK_DOT: |
| 123650 | + case TK_IN: |
| 123651 | + case TK_IS: |
| 123652 | + case TK_NOT: |
| 123653 | + case TK_NULL: |
| 123654 | + case TK_ID: { |
| 123655 | + if( tokenType==TK_NULL ){ |
| 123656 | + if( prevTokenType==TK_IS || prevTokenType==TK_NOT ){ |
| 123657 | + /* NULL is a keyword in this case, not a literal value */ |
| 123658 | + }else{ |
| 123659 | + /* Here the NULL is a literal value */ |
| 123660 | + z[j++] = '?'; |
| 123661 | + break; |
| 123662 | + } |
| 123663 | + } |
| 123664 | + if( j>0 && sqlite3IsIdChar(z[j-1]) && sqlite3IsIdChar(zSql[i]) ){ |
| 123665 | + z[j++] = ' '; |
| 123666 | + } |
| 123667 | + if( tokenType==TK_ID ){ |
| 123668 | + int i2 = i, n2 = n, rc = SQLITE_OK; |
| 123669 | + if( nParen>0 ){ |
| 123670 | + assert( nParen<nSql ); |
| 123671 | + sqlite3HashInsert(&inHash, zSql+nParen, 0); |
| 123672 | + } |
| 123673 | + if( flags&SQLITE_TOKEN_QUOTED ){ i2++; n2-=2; } |
| 123674 | + if( shouldTreatAsIdentifier(db, zSql+i2, n2, &rc)==0 ){ |
| 123675 | + if( rc!=SQLITE_OK ){ |
| 123676 | + sqlite3DbFree(db, z); |
| 123677 | + sqlite3HashClear(&inHash); |
| 123678 | + return; |
| 123679 | + } |
| 123680 | + if( sqlite3_keyword_check(zSql+i2, n2)==0 ){ |
| 123681 | + z[j++] = '?'; |
| 123682 | + break; |
| 123683 | + } |
| 123684 | + } |
| 123685 | + } |
| 123686 | + copyNormalizedToken(zSql, i, n, flags, z, &j); |
| 123687 | + break; |
| 123688 | + } |
| 123689 | + } |
| 123690 | + } |
| 123691 | + assert( j<nZ && "one" ); |
| 123692 | + while( j>0 && z[j-1]==' ' ){ j--; } |
| 123693 | + if( j>0 && z[j-1]!=';' ){ z[j++] = ';'; } |
| 123694 | + z[j] = 0; |
| 123695 | + assert( j<nZ && "two" ); |
| 123696 | + pVdbe->zNormSql = z; |
| 123697 | + sqlite3HashClear(&inHash); |
| 123698 | +} |
| 123699 | +#endif /* SQLITE_ENABLE_NORMALIZE */ |
| 123700 | + |
| 122917 | 123701 | /* |
| 122918 | 123702 | ** Rerun the compilation of a statement after a schema change. |
| 122919 | 123703 | ** |
| 122920 | 123704 | ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise, |
| 122921 | 123705 | ** if the statement cannot be recompiled because another connection has |
| | @@ -123924,11 +124708,11 @@ |
| 123924 | 124708 | ExprList *pExtra = 0; |
| 123925 | 124709 | for(i=0; i<pEList->nExpr; i++){ |
| 123926 | 124710 | struct ExprList_item *pItem = &pEList->a[i]; |
| 123927 | 124711 | if( pItem->u.x.iOrderByCol==0 ){ |
| 123928 | 124712 | Expr *pExpr = pItem->pExpr; |
| 123929 | | - Table *pTab = pExpr->pTab; |
| 124713 | + Table *pTab = pExpr->y.pTab; |
| 123930 | 124714 | if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab) |
| 123931 | 124715 | && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF) |
| 123932 | 124716 | ){ |
| 123933 | 124717 | int j; |
| 123934 | 124718 | for(j=0; j<nDefer; j++){ |
| | @@ -123947,16 +124731,16 @@ |
| 123947 | 124731 | } |
| 123948 | 124732 | for(k=0; k<nKey; k++){ |
| 123949 | 124733 | Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0); |
| 123950 | 124734 | if( pNew ){ |
| 123951 | 124735 | pNew->iTable = pExpr->iTable; |
| 123952 | | - pNew->pTab = pExpr->pTab; |
| 124736 | + pNew->y.pTab = pExpr->y.pTab; |
| 123953 | 124737 | pNew->iColumn = pPk ? pPk->aiColumn[k] : -1; |
| 123954 | 124738 | pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew); |
| 123955 | 124739 | } |
| 123956 | 124740 | } |
| 123957 | | - pSort->aDefer[nDefer].pTab = pExpr->pTab; |
| 124741 | + pSort->aDefer[nDefer].pTab = pExpr->y.pTab; |
| 123958 | 124742 | pSort->aDefer[nDefer].iCsr = pExpr->iTable; |
| 123959 | 124743 | pSort->aDefer[nDefer].nKey = nKey; |
| 123960 | 124744 | nDefer++; |
| 123961 | 124745 | } |
| 123962 | 124746 | } |
| | @@ -124801,11 +125585,11 @@ |
| 124801 | 125585 | ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT |
| 124802 | 125586 | ** branch below. */ |
| 124803 | 125587 | break; |
| 124804 | 125588 | } |
| 124805 | 125589 | |
| 124806 | | - assert( pTab && pExpr->pTab==pTab ); |
| 125590 | + assert( pTab && pExpr->y.pTab==pTab ); |
| 124807 | 125591 | if( pS ){ |
| 124808 | 125592 | /* The "table" is actually a sub-select or a view in the FROM clause |
| 124809 | 125593 | ** of the SELECT statement. Return the declaration type and origin |
| 124810 | 125594 | ** data for the result-set column of the sub-select. |
| 124811 | 125595 | */ |
| | @@ -124986,19 +125770,19 @@ |
| 124986 | 125770 | for(i=0; i<pEList->nExpr; i++){ |
| 124987 | 125771 | Expr *p = pEList->a[i].pExpr; |
| 124988 | 125772 | |
| 124989 | 125773 | assert( p!=0 ); |
| 124990 | 125774 | assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */ |
| 124991 | | - assert( p->op!=TK_COLUMN || p->pTab!=0 ); /* Covering idx not yet coded */ |
| 125775 | + assert( p->op!=TK_COLUMN || p->y.pTab!=0 ); /* Covering idx not yet coded */ |
| 124992 | 125776 | if( pEList->a[i].zName ){ |
| 124993 | 125777 | /* An AS clause always takes first priority */ |
| 124994 | 125778 | char *zName = pEList->a[i].zName; |
| 124995 | 125779 | sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT); |
| 124996 | 125780 | }else if( srcName && p->op==TK_COLUMN ){ |
| 124997 | 125781 | char *zCol; |
| 124998 | 125782 | int iCol = p->iColumn; |
| 124999 | | - pTab = p->pTab; |
| 125783 | + pTab = p->y.pTab; |
| 125000 | 125784 | assert( pTab!=0 ); |
| 125001 | 125785 | if( iCol<0 ) iCol = pTab->iPKey; |
| 125002 | 125786 | assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); |
| 125003 | 125787 | if( iCol<0 ){ |
| 125004 | 125788 | zCol = "rowid"; |
| | @@ -125085,11 +125869,11 @@ |
| 125085 | 125869 | } |
| 125086 | 125870 | assert( pColExpr->op!=TK_AGG_COLUMN ); |
| 125087 | 125871 | if( pColExpr->op==TK_COLUMN ){ |
| 125088 | 125872 | /* For columns use the column name name */ |
| 125089 | 125873 | int iCol = pColExpr->iColumn; |
| 125090 | | - Table *pTab = pColExpr->pTab; |
| 125874 | + Table *pTab = pColExpr->y.pTab; |
| 125091 | 125875 | assert( pTab!=0 ); |
| 125092 | 125876 | if( iCol<0 ) iCol = pTab->iPKey; |
| 125093 | 125877 | zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid"; |
| 125094 | 125878 | }else if( pColExpr->op==TK_ID ){ |
| 125095 | 125879 | assert( !ExprHasProperty(pColExpr, EP_IntValue) ); |
| | @@ -125438,10 +126222,17 @@ |
| 125438 | 126222 | int i; /* Loop counter */ |
| 125439 | 126223 | int rc; /* Result code */ |
| 125440 | 126224 | ExprList *pOrderBy; /* The ORDER BY clause */ |
| 125441 | 126225 | Expr *pLimit; /* Saved LIMIT and OFFSET */ |
| 125442 | 126226 | int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */ |
| 126227 | + |
| 126228 | +#ifndef SQLITE_OMIT_WINDOWFUNC |
| 126229 | + if( p->pWin ){ |
| 126230 | + sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries"); |
| 126231 | + return; |
| 126232 | + } |
| 126233 | +#endif |
| 125443 | 126234 | |
| 125444 | 126235 | /* Obtain authorization to do a recursive query */ |
| 125445 | 126236 | if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return; |
| 125446 | 126237 | |
| 125447 | 126238 | /* Process the LIMIT and OFFSET clauses, if they exist */ |
| | @@ -127188,11 +127979,11 @@ |
| 127188 | 127979 | return 1; |
| 127189 | 127980 | } |
| 127190 | 127981 | #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 127191 | 127982 | |
| 127192 | 127983 | /* |
| 127193 | | -** A structure to keep track of all of the column values that fixed to |
| 127984 | +** A structure to keep track of all of the column values that are fixed to |
| 127194 | 127985 | ** a known value due to WHERE clause constraints of the form COLUMN=VALUE. |
| 127195 | 127986 | */ |
| 127196 | 127987 | typedef struct WhereConst WhereConst; |
| 127197 | 127988 | struct WhereConst { |
| 127198 | 127989 | Parse *pParse; /* Parsing context */ |
| | @@ -127200,17 +127991,32 @@ |
| 127200 | 127991 | int nChng; /* Number of times a constant is propagated */ |
| 127201 | 127992 | Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */ |
| 127202 | 127993 | }; |
| 127203 | 127994 | |
| 127204 | 127995 | /* |
| 127205 | | -** Add a new entry to the pConst object |
| 127996 | +** Add a new entry to the pConst object. Except, do not add duplicate |
| 127997 | +** pColumn entires. |
| 127206 | 127998 | */ |
| 127207 | 127999 | static void constInsert( |
| 127208 | | - WhereConst *pConst, |
| 127209 | | - Expr *pColumn, |
| 127210 | | - Expr *pValue |
| 128000 | + WhereConst *pConst, /* The WhereConst into which we are inserting */ |
| 128001 | + Expr *pColumn, /* The COLUMN part of the constraint */ |
| 128002 | + Expr *pValue /* The VALUE part of the constraint */ |
| 127211 | 128003 | ){ |
| 128004 | + int i; |
| 128005 | + assert( pColumn->op==TK_COLUMN ); |
| 128006 | + |
| 128007 | + /* 2018-10-25 ticket [cf5ed20f] |
| 128008 | + ** Make sure the same pColumn is not inserted more than once */ |
| 128009 | + for(i=0; i<pConst->nConst; i++){ |
| 128010 | + const Expr *pExpr = pConst->apExpr[i*2]; |
| 128011 | + assert( pExpr->op==TK_COLUMN ); |
| 128012 | + if( pExpr->iTable==pColumn->iTable |
| 128013 | + && pExpr->iColumn==pColumn->iColumn |
| 128014 | + ){ |
| 128015 | + return; /* Already present. Return without doing anything. */ |
| 128016 | + } |
| 128017 | + } |
| 127212 | 128018 | |
| 127213 | 128019 | pConst->nConst++; |
| 127214 | 128020 | pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr, |
| 127215 | 128021 | pConst->nConst*2*sizeof(Expr*)); |
| 127216 | 128022 | if( pConst->apExpr==0 ){ |
| | @@ -131187,10 +131993,61 @@ |
| 131187 | 131993 | if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){ |
| 131188 | 131994 | sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); |
| 131189 | 131995 | } |
| 131190 | 131996 | #endif |
| 131191 | 131997 | } |
| 131998 | + |
| 131999 | +/* |
| 132000 | +** Check to see if column iCol of index pIdx references any of the |
| 132001 | +** columns defined by aXRef and chngRowid. Return true if it does |
| 132002 | +** and false if not. This is an optimization. False-positives are a |
| 132003 | +** performance degradation, but false-negatives can result in a corrupt |
| 132004 | +** index and incorrect answers. |
| 132005 | +** |
| 132006 | +** aXRef[j] will be non-negative if column j of the original table is |
| 132007 | +** being updated. chngRowid will be true if the rowid of the table is |
| 132008 | +** being updated. |
| 132009 | +*/ |
| 132010 | +static int indexColumnIsBeingUpdated( |
| 132011 | + Index *pIdx, /* The index to check */ |
| 132012 | + int iCol, /* Which column of the index to check */ |
| 132013 | + int *aXRef, /* aXRef[j]>=0 if column j is being updated */ |
| 132014 | + int chngRowid /* true if the rowid is being updated */ |
| 132015 | +){ |
| 132016 | + i16 iIdxCol = pIdx->aiColumn[iCol]; |
| 132017 | + assert( iIdxCol!=XN_ROWID ); /* Cannot index rowid */ |
| 132018 | + if( iIdxCol>=0 ){ |
| 132019 | + return aXRef[iIdxCol]>=0; |
| 132020 | + } |
| 132021 | + assert( iIdxCol==XN_EXPR ); |
| 132022 | + assert( pIdx->aColExpr!=0 ); |
| 132023 | + assert( pIdx->aColExpr->a[iCol].pExpr!=0 ); |
| 132024 | + return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr, |
| 132025 | + aXRef,chngRowid); |
| 132026 | +} |
| 132027 | + |
| 132028 | +/* |
| 132029 | +** Check to see if index pIdx is a partial index whose conditional |
| 132030 | +** expression might change values due to an UPDATE. Return true if |
| 132031 | +** the index is subject to change and false if the index is guaranteed |
| 132032 | +** to be unchanged. This is an optimization. False-positives are a |
| 132033 | +** performance degradation, but false-negatives can result in a corrupt |
| 132034 | +** index and incorrect answers. |
| 132035 | +** |
| 132036 | +** aXRef[j] will be non-negative if column j of the original table is |
| 132037 | +** being updated. chngRowid will be true if the rowid of the table is |
| 132038 | +** being updated. |
| 132039 | +*/ |
| 132040 | +static int indexWhereClauseMightChange( |
| 132041 | + Index *pIdx, /* The index to check */ |
| 132042 | + int *aXRef, /* aXRef[j]>=0 if column j is being updated */ |
| 132043 | + int chngRowid /* true if the rowid is being updated */ |
| 132044 | +){ |
| 132045 | + if( pIdx->pPartIdxWhere==0 ) return 0; |
| 132046 | + return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere, |
| 132047 | + aXRef, chngRowid); |
| 132048 | +} |
| 131192 | 132049 | |
| 131193 | 132050 | /* |
| 131194 | 132051 | ** Process an UPDATE statement. |
| 131195 | 132052 | ** |
| 131196 | 132053 | ** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL; |
| | @@ -131411,23 +132268,22 @@ |
| 131411 | 132268 | hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey); |
| 131412 | 132269 | |
| 131413 | 132270 | /* There is one entry in the aRegIdx[] array for each index on the table |
| 131414 | 132271 | ** being updated. Fill in aRegIdx[] with a register number that will hold |
| 131415 | 132272 | ** the key for accessing each index. |
| 131416 | | - ** |
| 131417 | | - ** FIXME: Be smarter about omitting indexes that use expressions. |
| 131418 | 132273 | */ |
| 131419 | 132274 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 131420 | 132275 | int reg; |
| 131421 | | - if( chngKey || hasFK>1 || pIdx->pPartIdxWhere || pIdx==pPk ){ |
| 132276 | + if( chngKey || hasFK>1 || pIdx==pPk |
| 132277 | + || indexWhereClauseMightChange(pIdx,aXRef,chngRowid) |
| 132278 | + ){ |
| 131422 | 132279 | reg = ++pParse->nMem; |
| 131423 | 132280 | pParse->nMem += pIdx->nColumn; |
| 131424 | 132281 | }else{ |
| 131425 | 132282 | reg = 0; |
| 131426 | 132283 | for(i=0; i<pIdx->nKeyCol; i++){ |
| 131427 | | - i16 iIdxCol = pIdx->aiColumn[i]; |
| 131428 | | - if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){ |
| 132284 | + if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){ |
| 131429 | 132285 | reg = ++pParse->nMem; |
| 131430 | 132286 | pParse->nMem += pIdx->nColumn; |
| 131431 | 132287 | if( (onError==OE_Replace) |
| 131432 | 132288 | || (onError==OE_Default && pIdx->onError==OE_Replace) |
| 131433 | 132289 | ){ |
| | @@ -131972,11 +132828,11 @@ |
| 131972 | 132828 | for(i=0; i<pTab->nCol; i++){ |
| 131973 | 132829 | if( aXRef[i]>=0 ){ |
| 131974 | 132830 | sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i); |
| 131975 | 132831 | }else{ |
| 131976 | 132832 | sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i); |
| 131977 | | - sqlite3VdbeChangeP5(v, 1); /* Enable sqlite3_vtab_nochange() */ |
| 132833 | + sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG);/* Enable sqlite3_vtab_nochange() */ |
| 131978 | 132834 | } |
| 131979 | 132835 | } |
| 131980 | 132836 | if( HasRowid(pTab) ){ |
| 131981 | 132837 | sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg); |
| 131982 | 132838 | if( pRowid ){ |
| | @@ -132473,11 +133329,12 @@ |
| 132473 | 133329 | saved_nChange = db->nChange; |
| 132474 | 133330 | saved_nTotalChange = db->nTotalChange; |
| 132475 | 133331 | saved_mTrace = db->mTrace; |
| 132476 | 133332 | db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks; |
| 132477 | 133333 | db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum; |
| 132478 | | - db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_CountRows); |
| 133334 | + db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder |
| 133335 | + | SQLITE_Defensive | SQLITE_CountRows); |
| 132479 | 133336 | db->mTrace = 0; |
| 132480 | 133337 | |
| 132481 | 133338 | zDbMain = db->aDb[iDb].zDbSName; |
| 132482 | 133339 | pMain = db->aDb[iDb].pBt; |
| 132483 | 133340 | isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain)); |
| | @@ -133015,22 +133872,19 @@ |
| 133015 | 133872 | Token *pName1, /* Name of new table, or database name */ |
| 133016 | 133873 | Token *pName2, /* Name of new table or NULL */ |
| 133017 | 133874 | Token *pModuleName, /* Name of the module for the virtual table */ |
| 133018 | 133875 | int ifNotExists /* No error if the table already exists */ |
| 133019 | 133876 | ){ |
| 133020 | | - int iDb; /* The database the table is being created in */ |
| 133021 | 133877 | Table *pTable; /* The new virtual table */ |
| 133022 | 133878 | sqlite3 *db; /* Database connection */ |
| 133023 | 133879 | |
| 133024 | 133880 | sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists); |
| 133025 | 133881 | pTable = pParse->pNewTable; |
| 133026 | 133882 | if( pTable==0 ) return; |
| 133027 | 133883 | assert( 0==pTable->pIndex ); |
| 133028 | 133884 | |
| 133029 | 133885 | db = pParse->db; |
| 133030 | | - iDb = sqlite3SchemaToIndex(db, pTable->pSchema); |
| 133031 | | - assert( iDb>=0 ); |
| 133032 | 133886 | |
| 133033 | 133887 | assert( pTable->nModuleArg==0 ); |
| 133034 | 133888 | addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); |
| 133035 | 133889 | addModuleArgument(db, pTable, 0); |
| 133036 | 133890 | addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); |
| | @@ -133046,10 +133900,12 @@ |
| 133046 | 133900 | ** The first invocation, to obtain permission to INSERT a row into the |
| 133047 | 133901 | ** sqlite_master table, has already been made by sqlite3StartTable(). |
| 133048 | 133902 | ** The second call, to obtain permission to create the table, is made now. |
| 133049 | 133903 | */ |
| 133050 | 133904 | if( pTable->azModuleArg ){ |
| 133905 | + int iDb = sqlite3SchemaToIndex(db, pTable->pSchema); |
| 133906 | + assert( iDb>=0 ); /* The database the table is being created in */ |
| 133051 | 133907 | sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, |
| 133052 | 133908 | pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName); |
| 133053 | 133909 | } |
| 133054 | 133910 | #endif |
| 133055 | 133911 | } |
| | @@ -133740,11 +134596,11 @@ |
| 133740 | 134596 | int rc = 0; |
| 133741 | 134597 | |
| 133742 | 134598 | /* Check to see the left operand is a column in a virtual table */ |
| 133743 | 134599 | if( NEVER(pExpr==0) ) return pDef; |
| 133744 | 134600 | if( pExpr->op!=TK_COLUMN ) return pDef; |
| 133745 | | - pTab = pExpr->pTab; |
| 134601 | + pTab = pExpr->y.pTab; |
| 133746 | 134602 | if( pTab==0 ) return pDef; |
| 133747 | 134603 | if( !IsVirtual(pTab) ) return pDef; |
| 133748 | 134604 | pVtab = sqlite3GetVTable(db, pTab)->pVtab; |
| 133749 | 134605 | assert( pVtab!=0 ); |
| 133750 | 134606 | assert( pVtab->pModule!=0 ); |
| | @@ -134360,15 +135216,36 @@ |
| 134360 | 135216 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 134361 | 135217 | UnpackedRecord *pRec; /* Probe for stat4 (if required) */ |
| 134362 | 135218 | int nRecValid; /* Number of valid fields currently in pRec */ |
| 134363 | 135219 | #endif |
| 134364 | 135220 | unsigned int bldFlags; /* SQLITE_BLDF_* flags */ |
| 135221 | + unsigned int iPlanLimit; /* Search limiter */ |
| 134365 | 135222 | }; |
| 134366 | 135223 | |
| 134367 | 135224 | /* Allowed values for WhereLoopBuider.bldFlags */ |
| 134368 | 135225 | #define SQLITE_BLDF_INDEXED 0x0001 /* An index is used */ |
| 134369 | 135226 | #define SQLITE_BLDF_UNIQUE 0x0002 /* All keys of a UNIQUE index used */ |
| 135227 | + |
| 135228 | +/* The WhereLoopBuilder.iPlanLimit is used to limit the number of |
| 135229 | +** index+constraint combinations the query planner will consider for a |
| 135230 | +** particular query. If this parameter is unlimited, then certain |
| 135231 | +** pathological queries can spend excess time in the sqlite3WhereBegin() |
| 135232 | +** routine. The limit is high enough that is should not impact real-world |
| 135233 | +** queries. |
| 135234 | +** |
| 135235 | +** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit. The limit is |
| 135236 | +** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM |
| 135237 | +** clause is processed, so that every table in a join is guaranteed to be |
| 135238 | +** able to propose a some index+constraint combinations even if the initial |
| 135239 | +** baseline limit was exhausted by prior tables of the join. |
| 135240 | +*/ |
| 135241 | +#ifndef SQLITE_QUERY_PLANNER_LIMIT |
| 135242 | +# define SQLITE_QUERY_PLANNER_LIMIT 20000 |
| 135243 | +#endif |
| 135244 | +#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR |
| 135245 | +# define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000 |
| 135246 | +#endif |
| 134370 | 135247 | |
| 134371 | 135248 | /* |
| 134372 | 135249 | ** The WHERE clause processing routine has two halves. The |
| 134373 | 135250 | ** first part does the start of the WHERE loop and the second |
| 134374 | 135251 | ** half does the tail of the WHERE loop. An instance of |
| | @@ -134927,11 +135804,11 @@ |
| 134927 | 135804 | Select *pSelect; /* Pointer to the SELECT on the RHS */ |
| 134928 | 135805 | |
| 134929 | 135806 | for(i=iEq; i<pLoop->nLTerm; i++){ |
| 134930 | 135807 | if( pLoop->aLTerm[i]->pExpr==pX ){ |
| 134931 | 135808 | int iField = pLoop->aLTerm[i]->iField - 1; |
| 134932 | | - assert( pOrigRhs->a[iField].pExpr!=0 ); |
| 135809 | + if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */ |
| 134933 | 135810 | pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr); |
| 134934 | 135811 | pOrigRhs->a[iField].pExpr = 0; |
| 134935 | 135812 | assert( pOrigLhs->a[iField].pExpr!=0 ); |
| 134936 | 135813 | pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr); |
| 134937 | 135814 | pOrigLhs->a[iField].pExpr = 0; |
| | @@ -135619,11 +136496,11 @@ |
| 135619 | 136496 | IdxExprTrans *pX = p->u.pIdxTrans; |
| 135620 | 136497 | if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){ |
| 135621 | 136498 | pExpr->op = TK_COLUMN; |
| 135622 | 136499 | pExpr->iTable = pX->iIdxCur; |
| 135623 | 136500 | pExpr->iColumn = pX->iIdxCol; |
| 135624 | | - pExpr->pTab = 0; |
| 136501 | + pExpr->y.pTab = 0; |
| 135625 | 136502 | return WRC_Prune; |
| 135626 | 136503 | }else{ |
| 135627 | 136504 | return WRC_Continue; |
| 135628 | 136505 | } |
| 135629 | 136506 | } |
| | @@ -137019,11 +137896,11 @@ |
| 137019 | 137896 | || zNew[0]=='-' |
| 137020 | 137897 | || (zNew[0]+1=='0' && iTo==1) |
| 137021 | 137898 | ){ |
| 137022 | 137899 | if( pLeft->op!=TK_COLUMN |
| 137023 | 137900 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 137024 | | - || IsVirtual(pLeft->pTab) /* Value might be numeric */ |
| 137901 | + || IsVirtual(pLeft->y.pTab) /* Value might be numeric */ |
| 137025 | 137902 | ){ |
| 137026 | 137903 | sqlite3ExprDelete(db, pPrefix); |
| 137027 | 137904 | sqlite3ValueFree(pVal); |
| 137028 | 137905 | return 0; |
| 137029 | 137906 | } |
| | @@ -137120,11 +137997,11 @@ |
| 137120 | 137997 | ** |
| 137121 | 137998 | ** vtab_column MATCH expression |
| 137122 | 137999 | ** MATCH(expression,vtab_column) |
| 137123 | 138000 | */ |
| 137124 | 138001 | pCol = pList->a[1].pExpr; |
| 137125 | | - if( pCol->op==TK_COLUMN && IsVirtual(pCol->pTab) ){ |
| 138002 | + if( pCol->op==TK_COLUMN && IsVirtual(pCol->y.pTab) ){ |
| 137126 | 138003 | for(i=0; i<ArraySize(aOp); i++){ |
| 137127 | 138004 | if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){ |
| 137128 | 138005 | *peOp2 = aOp[i].eOp2; |
| 137129 | 138006 | *ppRight = pList->a[0].pExpr; |
| 137130 | 138007 | *ppLeft = pCol; |
| | @@ -137142,16 +138019,16 @@ |
| 137142 | 138019 | ** Historically, xFindFunction expected to see lower-case function |
| 137143 | 138020 | ** names. But for this use case, xFindFunction is expected to deal |
| 137144 | 138021 | ** with function names in an arbitrary case. |
| 137145 | 138022 | */ |
| 137146 | 138023 | pCol = pList->a[0].pExpr; |
| 137147 | | - if( pCol->op==TK_COLUMN && IsVirtual(pCol->pTab) ){ |
| 138024 | + if( pCol->op==TK_COLUMN && IsVirtual(pCol->y.pTab) ){ |
| 137148 | 138025 | sqlite3_vtab *pVtab; |
| 137149 | 138026 | sqlite3_module *pMod; |
| 137150 | 138027 | void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**); |
| 137151 | 138028 | void *pNotUsed; |
| 137152 | | - pVtab = sqlite3GetVTable(db, pCol->pTab)->pVtab; |
| 138029 | + pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab; |
| 137153 | 138030 | assert( pVtab!=0 ); |
| 137154 | 138031 | assert( pVtab->pModule!=0 ); |
| 137155 | 138032 | pMod = (sqlite3_module *)pVtab->pModule; |
| 137156 | 138033 | if( pMod->xFindFunction!=0 ){ |
| 137157 | 138034 | i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed); |
| | @@ -137165,14 +138042,14 @@ |
| 137165 | 138042 | } |
| 137166 | 138043 | }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){ |
| 137167 | 138044 | int res = 0; |
| 137168 | 138045 | Expr *pLeft = pExpr->pLeft; |
| 137169 | 138046 | Expr *pRight = pExpr->pRight; |
| 137170 | | - if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->pTab) ){ |
| 138047 | + if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->y.pTab) ){ |
| 137171 | 138048 | res++; |
| 137172 | 138049 | } |
| 137173 | | - if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->pTab) ){ |
| 138050 | + if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->y.pTab) ){ |
| 137174 | 138051 | res++; |
| 137175 | 138052 | SWAP(Expr*, pLeft, pRight); |
| 137176 | 138053 | } |
| 137177 | 138054 | *ppLeft = pLeft; |
| 137178 | 138055 | *ppRight = pRight; |
| | @@ -138120,10 +138997,11 @@ |
| 138120 | 138997 | ** Note that the virtual term must be tagged with TERM_VNULL. |
| 138121 | 138998 | */ |
| 138122 | 138999 | if( pExpr->op==TK_NOTNULL |
| 138123 | 139000 | && pExpr->pLeft->op==TK_COLUMN |
| 138124 | 139001 | && pExpr->pLeft->iColumn>=0 |
| 139002 | + && !ExprHasProperty(pExpr, EP_FromJoin) |
| 138125 | 139003 | && OptimizationEnabled(db, SQLITE_Stat34) |
| 138126 | 139004 | ){ |
| 138127 | 139005 | Expr *pNewExpr; |
| 138128 | 139006 | Expr *pLeft = pExpr->pLeft; |
| 138129 | 139007 | int idxNew; |
| | @@ -138311,10 +139189,11 @@ |
| 138311 | 139189 | pTab = pItem->pTab; |
| 138312 | 139190 | assert( pTab!=0 ); |
| 138313 | 139191 | pArgs = pItem->u1.pFuncArg; |
| 138314 | 139192 | if( pArgs==0 ) return; |
| 138315 | 139193 | for(j=k=0; j<pArgs->nExpr; j++){ |
| 139194 | + Expr *pRhs; |
| 138316 | 139195 | while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;} |
| 138317 | 139196 | if( k>=pTab->nCol ){ |
| 138318 | 139197 | sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d", |
| 138319 | 139198 | pTab->zName, j); |
| 138320 | 139199 | return; |
| | @@ -138321,13 +139200,14 @@ |
| 138321 | 139200 | } |
| 138322 | 139201 | pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0); |
| 138323 | 139202 | if( pColRef==0 ) return; |
| 138324 | 139203 | pColRef->iTable = pItem->iCursor; |
| 138325 | 139204 | pColRef->iColumn = k++; |
| 138326 | | - pColRef->pTab = pTab; |
| 138327 | | - pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, |
| 138328 | | - sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0)); |
| 139205 | + pColRef->y.pTab = pTab; |
| 139206 | + pRhs = sqlite3PExpr(pParse, TK_UPLUS, |
| 139207 | + sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0); |
| 139208 | + pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs); |
| 138329 | 139209 | whereClauseInsert(pWC, pTerm, TERM_DYNAMIC); |
| 138330 | 139210 | } |
| 138331 | 139211 | } |
| 138332 | 139212 | |
| 138333 | 139213 | /************** End of whereexpr.c *******************************************/ |
| | @@ -139186,11 +140066,10 @@ |
| 139186 | 140066 | sqlite3VdbeChangeP2(v, addrCounter, regBase+n); |
| 139187 | 140067 | testcase( pParse->db->mallocFailed ); |
| 139188 | 140068 | translateColumnToCopy(pParse, addrTop, pLevel->iTabCur, |
| 139189 | 140069 | pTabItem->regResult, 1); |
| 139190 | 140070 | sqlite3VdbeGoto(v, addrTop); |
| 139191 | | - pTabItem->fg.viaCoroutine = 0; |
| 139192 | 140071 | }else{ |
| 139193 | 140072 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); |
| 139194 | 140073 | } |
| 139195 | 140074 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
| 139196 | 140075 | sqlite3VdbeJumpHere(v, addrTop); |
| | @@ -139364,13 +140243,15 @@ |
| 139364 | 140243 | ** The table object reference passed as the second argument to this function |
| 139365 | 140244 | ** must represent a virtual table. This function invokes the xBestIndex() |
| 139366 | 140245 | ** method of the virtual table with the sqlite3_index_info object that |
| 139367 | 140246 | ** comes in as the 3rd argument to this function. |
| 139368 | 140247 | ** |
| 139369 | | -** If an error occurs, pParse is populated with an error message and a |
| 139370 | | -** non-zero value is returned. Otherwise, 0 is returned and the output |
| 139371 | | -** part of the sqlite3_index_info structure is left populated. |
| 140248 | +** If an error occurs, pParse is populated with an error message and an |
| 140249 | +** appropriate error code is returned. A return of SQLITE_CONSTRAINT from |
| 140250 | +** xBestIndex is not considered an error. SQLITE_CONSTRAINT indicates that |
| 140251 | +** the current configuration of "unusable" flags in sqlite3_index_info can |
| 140252 | +** not result in a valid plan. |
| 139372 | 140253 | ** |
| 139373 | 140254 | ** Whether or not an error is returned, it is the responsibility of the |
| 139374 | 140255 | ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates |
| 139375 | 140256 | ** that this is required. |
| 139376 | 140257 | */ |
| | @@ -139380,11 +140261,11 @@ |
| 139380 | 140261 | |
| 139381 | 140262 | TRACE_IDX_INPUTS(p); |
| 139382 | 140263 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 139383 | 140264 | TRACE_IDX_OUTPUTS(p); |
| 139384 | 140265 | |
| 139385 | | - if( rc!=SQLITE_OK ){ |
| 140266 | + if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){ |
| 139386 | 140267 | if( rc==SQLITE_NOMEM ){ |
| 139387 | 140268 | sqlite3OomFault(pParse->db); |
| 139388 | 140269 | }else if( !pVtab->zErrMsg ){ |
| 139389 | 140270 | sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 139390 | 140271 | }else{ |
| | @@ -139391,23 +140272,11 @@ |
| 139391 | 140272 | sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 139392 | 140273 | } |
| 139393 | 140274 | } |
| 139394 | 140275 | sqlite3_free(pVtab->zErrMsg); |
| 139395 | 140276 | pVtab->zErrMsg = 0; |
| 139396 | | - |
| 139397 | | -#if 0 |
| 139398 | | - /* This error is now caught by the caller. |
| 139399 | | - ** Search for "xBestIndex malfunction" below */ |
| 139400 | | - for(i=0; i<p->nConstraint; i++){ |
| 139401 | | - if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 139402 | | - sqlite3ErrorMsg(pParse, |
| 139403 | | - "table %s: xBestIndex returned an invalid plan", pTab->zName); |
| 139404 | | - } |
| 139405 | | - } |
| 139406 | | -#endif |
| 139407 | | - |
| 139408 | | - return pParse->nErr; |
| 140277 | + return rc; |
| 139409 | 140278 | } |
| 139410 | 140279 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
| 139411 | 140280 | |
| 139412 | 140281 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 139413 | 140282 | /* |
| | @@ -140457,10 +141326,18 @@ |
| 140457 | 141326 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
| 140458 | 141327 | WhereLoop **ppPrev, *p; |
| 140459 | 141328 | WhereInfo *pWInfo = pBuilder->pWInfo; |
| 140460 | 141329 | sqlite3 *db = pWInfo->pParse->db; |
| 140461 | 141330 | int rc; |
| 141331 | + |
| 141332 | + /* Stop the search once we hit the query planner search limit */ |
| 141333 | + if( pBuilder->iPlanLimit==0 ){ |
| 141334 | + WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n")); |
| 141335 | + if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0; |
| 141336 | + return SQLITE_DONE; |
| 141337 | + } |
| 141338 | + pBuilder->iPlanLimit--; |
| 140462 | 141339 | |
| 140463 | 141340 | /* If pBuilder->pOrSet is defined, then only keep track of the costs |
| 140464 | 141341 | ** and prereqs. |
| 140465 | 141342 | */ |
| 140466 | 141343 | if( pBuilder->pOrSet!=0 ){ |
| | @@ -141468,11 +142345,21 @@ |
| 141468 | 142345 | pIdxInfo->idxFlags = 0; |
| 141469 | 142346 | pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed; |
| 141470 | 142347 | |
| 141471 | 142348 | /* Invoke the virtual table xBestIndex() method */ |
| 141472 | 142349 | rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo); |
| 141473 | | - if( rc ) return rc; |
| 142350 | + if( rc ){ |
| 142351 | + if( rc==SQLITE_CONSTRAINT ){ |
| 142352 | + /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means |
| 142353 | + ** that the particular combination of parameters provided is unusable. |
| 142354 | + ** Make no entries in the loop table. |
| 142355 | + */ |
| 142356 | + WHERETRACE(0xffff, (" ^^^^--- non-viable plan rejected!\n")); |
| 142357 | + return SQLITE_OK; |
| 142358 | + } |
| 142359 | + return rc; |
| 142360 | + } |
| 141474 | 142361 | |
| 141475 | 142362 | mxTerm = -1; |
| 141476 | 142363 | assert( pNew->nLSlot>=nConstraint ); |
| 141477 | 142364 | for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
| 141478 | 142365 | pNew->u.vtab.omitMask = 0; |
| | @@ -141864,13 +142751,15 @@ |
| 141864 | 142751 | u8 priorJointype = 0; |
| 141865 | 142752 | |
| 141866 | 142753 | /* Loop over the tables in the join, from left to right */ |
| 141867 | 142754 | pNew = pBuilder->pNew; |
| 141868 | 142755 | whereLoopInit(pNew); |
| 142756 | + pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT; |
| 141869 | 142757 | for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){ |
| 141870 | 142758 | Bitmask mUnusable = 0; |
| 141871 | 142759 | pNew->iTab = iTab; |
| 142760 | + pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR; |
| 141872 | 142761 | pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor); |
| 141873 | 142762 | if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){ |
| 141874 | 142763 | /* This condition is true when pItem is the FROM clause term on the |
| 141875 | 142764 | ** right-hand-side of a LEFT or CROSS JOIN. */ |
| 141876 | 142765 | mPrereq = mPrior; |
| | @@ -141892,11 +142781,19 @@ |
| 141892 | 142781 | } |
| 141893 | 142782 | if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){ |
| 141894 | 142783 | rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable); |
| 141895 | 142784 | } |
| 141896 | 142785 | mPrior |= pNew->maskSelf; |
| 141897 | | - if( rc || db->mallocFailed ) break; |
| 142786 | + if( rc || db->mallocFailed ){ |
| 142787 | + if( rc==SQLITE_DONE ){ |
| 142788 | + /* We hit the query planner search limit set by iPlanLimit */ |
| 142789 | + sqlite3_log(SQLITE_WARNING, "abbreviated query algorithm search"); |
| 142790 | + rc = SQLITE_OK; |
| 142791 | + }else{ |
| 142792 | + break; |
| 142793 | + } |
| 142794 | + } |
| 141898 | 142795 | } |
| 141899 | 142796 | |
| 141900 | 142797 | whereLoopClear(db, pNew); |
| 141901 | 142798 | return rc; |
| 141902 | 142799 | } |
| | @@ -144274,16 +145171,16 @@ |
| 144274 | 145171 | } |
| 144275 | 145172 | |
| 144276 | 145173 | switch( pExpr->op ){ |
| 144277 | 145174 | |
| 144278 | 145175 | case TK_FUNCTION: |
| 144279 | | - if( pExpr->pWin==0 ){ |
| 145176 | + if( !ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 144280 | 145177 | break; |
| 144281 | 145178 | }else{ |
| 144282 | 145179 | Window *pWin; |
| 144283 | 145180 | for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){ |
| 144284 | | - if( pExpr->pWin==pWin ){ |
| 145181 | + if( pExpr->y.pWin==pWin ){ |
| 144285 | 145182 | assert( pWin->pOwner==pExpr ); |
| 144286 | 145183 | return WRC_Prune; |
| 144287 | 145184 | } |
| 144288 | 145185 | } |
| 144289 | 145186 | } |
| | @@ -144396,11 +145293,11 @@ |
| 144396 | 145293 | ** are invoked in the correct order as described under "SELECT REWRITING" |
| 144397 | 145294 | ** at the top of this file. |
| 144398 | 145295 | */ |
| 144399 | 145296 | SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){ |
| 144400 | 145297 | int rc = SQLITE_OK; |
| 144401 | | - if( p->pWin ){ |
| 145298 | + if( p->pWin && p->pPrior==0 ){ |
| 144402 | 145299 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 144403 | 145300 | sqlite3 *db = pParse->db; |
| 144404 | 145301 | Select *pSub = 0; /* The subquery */ |
| 144405 | 145302 | SrcList *pSrc = p->pSrc; |
| 144406 | 145303 | Expr *pWhere = p->pWhere; |
| | @@ -144609,15 +145506,17 @@ |
| 144609 | 145506 | /* |
| 144610 | 145507 | ** Attach window object pWin to expression p. |
| 144611 | 145508 | */ |
| 144612 | 145509 | SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){ |
| 144613 | 145510 | if( p ){ |
| 145511 | + assert( p->op==TK_FUNCTION ); |
| 144614 | 145512 | /* This routine is only called for the parser. If pWin was not |
| 144615 | 145513 | ** allocated due to an OOM, then the parser would fail before ever |
| 144616 | 145514 | ** invoking this routine */ |
| 144617 | 145515 | if( ALWAYS(pWin) ){ |
| 144618 | | - p->pWin = pWin; |
| 145516 | + p->y.pWin = pWin; |
| 145517 | + ExprSetProperty(p, EP_WinFunc); |
| 144619 | 145518 | pWin->pOwner = p; |
| 144620 | 145519 | if( p->flags & EP_Distinct ){ |
| 144621 | 145520 | sqlite3ErrorMsg(pParse, |
| 144622 | 145521 | "DISTINCT is not supported for window functions"); |
| 144623 | 145522 | } |
| | @@ -145776,11 +146675,11 @@ |
| 145776 | 146675 | ** third argument. Set the Window.pOwner field of the new object to |
| 145777 | 146676 | ** pOwner. |
| 145778 | 146677 | */ |
| 145779 | 146678 | SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){ |
| 145780 | 146679 | Window *pNew = 0; |
| 145781 | | - if( p ){ |
| 146680 | + if( ALWAYS(p) ){ |
| 145782 | 146681 | pNew = sqlite3DbMallocZero(db, sizeof(Window)); |
| 145783 | 146682 | if( pNew ){ |
| 145784 | 146683 | pNew->zName = sqlite3DbStrDup(db, p->zName); |
| 145785 | 146684 | pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0); |
| 145786 | 146685 | pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0); |
| | @@ -145928,10 +146827,11 @@ |
| 145928 | 146827 | ** |
| 145929 | 146828 | ** The following is the concatenation of all %include directives from the |
| 145930 | 146829 | ** input grammar file: |
| 145931 | 146830 | */ |
| 145932 | 146831 | /* #include <stdio.h> */ |
| 146832 | +/* #include <assert.h> */ |
| 145933 | 146833 | /************ Begin %include sections from the grammar ************************/ |
| 145934 | 146834 | |
| 145935 | 146835 | /* #include "sqliteInt.h" */ |
| 145936 | 146836 | |
| 145937 | 146837 | /* |
| | @@ -146029,17 +146929,14 @@ |
| 146029 | 146929 | p->flags = EP_Leaf; |
| 146030 | 146930 | p->iAgg = -1; |
| 146031 | 146931 | p->pLeft = p->pRight = 0; |
| 146032 | 146932 | p->x.pList = 0; |
| 146033 | 146933 | p->pAggInfo = 0; |
| 146034 | | - p->pTab = 0; |
| 146934 | + p->y.pTab = 0; |
| 146035 | 146935 | p->op2 = 0; |
| 146036 | 146936 | p->iTable = 0; |
| 146037 | 146937 | p->iColumn = 0; |
| 146038 | | -#ifndef SQLITE_OMIT_WINDOWFUNC |
| 146039 | | - p->pWin = 0; |
| 146040 | | -#endif |
| 146041 | 146938 | p->u.zToken = (char*)&p[1]; |
| 146042 | 146939 | memcpy(p->u.zToken, t.z, t.n); |
| 146043 | 146940 | p->u.zToken[t.n] = 0; |
| 146044 | 146941 | if( sqlite3Isquote(p->u.zToken[0]) ){ |
| 146045 | 146942 | if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted; |
| | @@ -150227,14 +151124,13 @@ |
| 150227 | 151124 | #endif |
| 150228 | 151125 | yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion); |
| 150229 | 151126 | yymajor = YYNOCODE; |
| 150230 | 151127 | }else{ |
| 150231 | 151128 | while( yypParser->yytos >= yypParser->yystack |
| 150232 | | - && yymx != YYERRORSYMBOL |
| 150233 | 151129 | && (yyact = yy_find_reduce_action( |
| 150234 | 151130 | yypParser->yytos->stateno, |
| 150235 | | - YYERRORSYMBOL)) >= YY_MIN_REDUCE |
| 151131 | + YYERRORSYMBOL)) > YY_MAX_SHIFTREDUCE |
| 150236 | 151132 | ){ |
| 150237 | 151133 | yy_pop_parser_stack(yypParser); |
| 150238 | 151134 | } |
| 150239 | 151135 | if( yypParser->yytos < yypParser->yystack || yymajor==0 ){ |
| 150240 | 151136 | yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); |
| | @@ -151197,10 +152093,77 @@ |
| 151197 | 152093 | while( IdChar(z[i]) ){ i++; } |
| 151198 | 152094 | *tokenType = TK_ID; |
| 151199 | 152095 | return i; |
| 151200 | 152096 | } |
| 151201 | 152097 | |
| 152098 | +#ifdef SQLITE_ENABLE_NORMALIZE |
| 152099 | +/* |
| 152100 | +** Return the length (in bytes) of the token that begins at z[0]. |
| 152101 | +** Store the token type in *tokenType before returning. If flags has |
| 152102 | +** SQLITE_TOKEN_NORMALIZE flag enabled, use the identifier token type |
| 152103 | +** for keywords. Add SQLITE_TOKEN_QUOTED to flags if the token was |
| 152104 | +** actually a quoted identifier. Add SQLITE_TOKEN_KEYWORD to flags |
| 152105 | +** if the token was recognized as a keyword; this is useful when the |
| 152106 | +** SQLITE_TOKEN_NORMALIZE flag is used, because it enables the caller |
| 152107 | +** to differentiate between a keyword being treated as an identifier |
| 152108 | +** (for normalization purposes) and an actual identifier. |
| 152109 | +*/ |
| 152110 | +SQLITE_PRIVATE int sqlite3GetTokenNormalized( |
| 152111 | + const unsigned char *z, |
| 152112 | + int *tokenType, |
| 152113 | + int *flags |
| 152114 | +){ |
| 152115 | + int n; |
| 152116 | + unsigned char iClass = aiClass[*z]; |
| 152117 | + if( iClass==CC_KYWD ){ |
| 152118 | + int i; |
| 152119 | + for(i=1; aiClass[z[i]]<=CC_KYWD; i++){} |
| 152120 | + if( IdChar(z[i]) ){ |
| 152121 | + /* This token started out using characters that can appear in keywords, |
| 152122 | + ** but z[i] is a character not allowed within keywords, so this must |
| 152123 | + ** be an identifier instead */ |
| 152124 | + i++; |
| 152125 | + while( IdChar(z[i]) ){ i++; } |
| 152126 | + *tokenType = TK_ID; |
| 152127 | + return i; |
| 152128 | + } |
| 152129 | + *tokenType = TK_ID; |
| 152130 | + n = keywordCode((char*)z, i, tokenType); |
| 152131 | + /* If the token is no longer considered to be an identifier, then it is a |
| 152132 | + ** keyword of some kind. Make the token back into an identifier and then |
| 152133 | + ** set the SQLITE_TOKEN_KEYWORD flag. Several non-identifier tokens are |
| 152134 | + ** used verbatim, including IN, IS, NOT, and NULL. */ |
| 152135 | + switch( *tokenType ){ |
| 152136 | + case TK_ID: { |
| 152137 | + /* do nothing, handled by caller */ |
| 152138 | + break; |
| 152139 | + } |
| 152140 | + case TK_IN: |
| 152141 | + case TK_IS: |
| 152142 | + case TK_NOT: |
| 152143 | + case TK_NULL: { |
| 152144 | + *flags |= SQLITE_TOKEN_KEYWORD; |
| 152145 | + break; |
| 152146 | + } |
| 152147 | + default: { |
| 152148 | + *tokenType = TK_ID; |
| 152149 | + *flags |= SQLITE_TOKEN_KEYWORD; |
| 152150 | + break; |
| 152151 | + } |
| 152152 | + } |
| 152153 | + }else{ |
| 152154 | + n = sqlite3GetToken(z, tokenType); |
| 152155 | + /* If the token is considered to be an identifier and the character class |
| 152156 | + ** of the first character is a quote, set the SQLITE_TOKEN_QUOTED flag. */ |
| 152157 | + if( *tokenType==TK_ID && (iClass==CC_QUOTE || iClass==CC_QUOTE2) ){ |
| 152158 | + *flags |= SQLITE_TOKEN_QUOTED; |
| 152159 | + } |
| 152160 | + } |
| 152161 | + return n; |
| 152162 | +} |
| 152163 | +#endif /* SQLITE_ENABLE_NORMALIZE */ |
| 152164 | + |
| 151202 | 152165 | /* |
| 151203 | 152166 | ** Run the parser on the given SQL string. The parser structure is |
| 151204 | 152167 | ** passed in. An SQLITE_ status code is returned. If an error occurs |
| 151205 | 152168 | ** then an and attempt is made to write an error message into |
| 151206 | 152169 | ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that |
| | @@ -152594,10 +153557,11 @@ |
| 152594 | 153557 | { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension }, |
| 152595 | 153558 | { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, SQLITE_NoCkptOnClose }, |
| 152596 | 153559 | { SQLITE_DBCONFIG_ENABLE_QPSG, SQLITE_EnableQPSG }, |
| 152597 | 153560 | { SQLITE_DBCONFIG_TRIGGER_EQP, SQLITE_TriggerEQP }, |
| 152598 | 153561 | { SQLITE_DBCONFIG_RESET_DATABASE, SQLITE_ResetDatabase }, |
| 153562 | + { SQLITE_DBCONFIG_DEFENSIVE, SQLITE_Defensive }, |
| 152599 | 153563 | }; |
| 152600 | 153564 | unsigned int i; |
| 152601 | 153565 | rc = SQLITE_ERROR; /* IMP: R-42790-23372 */ |
| 152602 | 153566 | for(i=0; i<ArraySize(aFlagOp); i++){ |
| 152603 | 153567 | if( aFlagOp[i].op==op ){ |
| | @@ -154821,10 +155785,13 @@ |
| 154821 | 155785 | | SQLITE_Fts3Tokenizer |
| 154822 | 155786 | #endif |
| 154823 | 155787 | #if defined(SQLITE_ENABLE_QPSG) |
| 154824 | 155788 | | SQLITE_EnableQPSG |
| 154825 | 155789 | #endif |
| 155790 | +#if defined(SQLITE_DEFAULT_DEFENSIVE) |
| 155791 | + | SQLITE_Defensive |
| 155792 | +#endif |
| 154826 | 155793 | ; |
| 154827 | 155794 | sqlite3HashInit(&db->aCollSeq); |
| 154828 | 155795 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 154829 | 155796 | sqlite3HashInit(&db->aModule); |
| 154830 | 155797 | #endif |
| | @@ -155708,18 +156675,29 @@ |
| 155708 | 156675 | break; |
| 155709 | 156676 | } |
| 155710 | 156677 | |
| 155711 | 156678 | /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff); |
| 155712 | 156679 | ** |
| 155713 | | - ** If parameter onoff is non-zero, configure the wrappers so that all |
| 155714 | | - ** subsequent calls to localtime() and variants fail. If onoff is zero, |
| 155715 | | - ** undo this setting. |
| 156680 | + ** If parameter onoff is non-zero, subsequent calls to localtime() |
| 156681 | + ** and its variants fail. If onoff is zero, undo this setting. |
| 155716 | 156682 | */ |
| 155717 | 156683 | case SQLITE_TESTCTRL_LOCALTIME_FAULT: { |
| 155718 | 156684 | sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int); |
| 155719 | 156685 | break; |
| 155720 | 156686 | } |
| 156687 | + |
| 156688 | + /* sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCS, int onoff); |
| 156689 | + ** |
| 156690 | + ** If parameter onoff is non-zero, internal-use-only SQL functions |
| 156691 | + ** are visible to ordinary SQL. This is useful for testing but is |
| 156692 | + ** unsafe because invalid parameters to those internal-use-only functions |
| 156693 | + ** can result in crashes or segfaults. |
| 156694 | + */ |
| 156695 | + case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: { |
| 156696 | + sqlite3GlobalConfig.bInternalFunctions = va_arg(ap, int); |
| 156697 | + break; |
| 156698 | + } |
| 155721 | 156699 | |
| 155722 | 156700 | /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int); |
| 155723 | 156701 | ** |
| 155724 | 156702 | ** Set or clear a flag that indicates that the database file is always well- |
| 155725 | 156703 | ** formed and never corrupt. This flag is clear by default, indicating that |
| | @@ -159159,11 +160137,11 @@ |
| 159159 | 160137 | ){ |
| 159160 | 160138 | int rc = SQLITE_OK; /* Return code */ |
| 159161 | 160139 | const char *zCsr = zNode; /* Cursor to iterate through node */ |
| 159162 | 160140 | const char *zEnd = &zCsr[nNode];/* End of interior node buffer */ |
| 159163 | 160141 | char *zBuffer = 0; /* Buffer to load terms into */ |
| 159164 | | - int nAlloc = 0; /* Size of allocated buffer */ |
| 160142 | + i64 nAlloc = 0; /* Size of allocated buffer */ |
| 159165 | 160143 | int isFirstTerm = 1; /* True when processing first term on page */ |
| 159166 | 160144 | sqlite3_int64 iChild; /* Block id of child node to descend to */ |
| 159167 | 160145 | |
| 159168 | 160146 | /* Skip over the 'height' varint that occurs at the start of every |
| 159169 | 160147 | ** interior node. Then load the blockid of the left-child of the b-tree |
| | @@ -159197,18 +160175,18 @@ |
| 159197 | 160175 | } |
| 159198 | 160176 | isFirstTerm = 0; |
| 159199 | 160177 | zCsr += fts3GetVarint32(zCsr, &nSuffix); |
| 159200 | 160178 | |
| 159201 | 160179 | assert( nPrefix>=0 && nSuffix>=0 ); |
| 159202 | | - if( &zCsr[nSuffix]>zEnd ){ |
| 160180 | + if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr ){ |
| 159203 | 160181 | rc = FTS_CORRUPT_VTAB; |
| 159204 | 160182 | goto finish_scan; |
| 159205 | 160183 | } |
| 159206 | | - if( nPrefix+nSuffix>nAlloc ){ |
| 160184 | + if( (i64)nPrefix+nSuffix>nAlloc ){ |
| 159207 | 160185 | char *zNew; |
| 159208 | | - nAlloc = (nPrefix+nSuffix) * 2; |
| 159209 | | - zNew = (char *)sqlite3_realloc(zBuffer, nAlloc); |
| 160186 | + nAlloc = ((i64)nPrefix+nSuffix) * 2; |
| 160187 | + zNew = (char *)sqlite3_realloc64(zBuffer, nAlloc); |
| 159210 | 160188 | if( !zNew ){ |
| 159211 | 160189 | rc = SQLITE_NOMEM; |
| 159212 | 160190 | goto finish_scan; |
| 159213 | 160191 | } |
| 159214 | 160192 | zBuffer = zNew; |
| | @@ -161183,13 +162161,28 @@ |
| 161183 | 162161 | assert( p->mxSavepoint >= iSavepoint ); |
| 161184 | 162162 | TESTONLY( p->mxSavepoint = iSavepoint ); |
| 161185 | 162163 | sqlite3Fts3PendingTermsClear(p); |
| 161186 | 162164 | return SQLITE_OK; |
| 161187 | 162165 | } |
| 162166 | + |
| 162167 | +/* |
| 162168 | +** Return true if zName is the extension on one of the shadow tables used |
| 162169 | +** by this module. |
| 162170 | +*/ |
| 162171 | +static int fts3ShadowName(const char *zName){ |
| 162172 | + static const char *azName[] = { |
| 162173 | + "content", "docsize", "segdir", "segments", "stat", |
| 162174 | + }; |
| 162175 | + unsigned int i; |
| 162176 | + for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){ |
| 162177 | + if( sqlite3_stricmp(zName, azName[i])==0 ) return 1; |
| 162178 | + } |
| 162179 | + return 0; |
| 162180 | +} |
| 161188 | 162181 | |
| 161189 | 162182 | static const sqlite3_module fts3Module = { |
| 161190 | | - /* iVersion */ 2, |
| 162183 | + /* iVersion */ 3, |
| 161191 | 162184 | /* xCreate */ fts3CreateMethod, |
| 161192 | 162185 | /* xConnect */ fts3ConnectMethod, |
| 161193 | 162186 | /* xBestIndex */ fts3BestIndexMethod, |
| 161194 | 162187 | /* xDisconnect */ fts3DisconnectMethod, |
| 161195 | 162188 | /* xDestroy */ fts3DestroyMethod, |
| | @@ -161208,10 +162201,11 @@ |
| 161208 | 162201 | /* xFindFunction */ fts3FindFunctionMethod, |
| 161209 | 162202 | /* xRename */ fts3RenameMethod, |
| 161210 | 162203 | /* xSavepoint */ fts3SavepointMethod, |
| 161211 | 162204 | /* xRelease */ fts3ReleaseMethod, |
| 161212 | 162205 | /* xRollbackTo */ fts3RollbackToMethod, |
| 162206 | + /* xShadowName */ fts3ShadowName, |
| 161213 | 162207 | }; |
| 161214 | 162208 | |
| 161215 | 162209 | /* |
| 161216 | 162210 | ** This function is registered as the module destructor (called when an |
| 161217 | 162211 | ** FTS3 enabled database connection is closed). It frees the memory |
| | @@ -161488,10 +162482,11 @@ |
| 161488 | 162482 | } |
| 161489 | 162483 | |
| 161490 | 162484 | return rc; |
| 161491 | 162485 | } |
| 161492 | 162486 | |
| 162487 | +#ifndef SQLITE_DISABLE_FTS4_DEFERRED |
| 161493 | 162488 | /* |
| 161494 | 162489 | ** This function is called on each phrase after the position lists for |
| 161495 | 162490 | ** any deferred tokens have been loaded into memory. It updates the phrases |
| 161496 | 162491 | ** current position list to include only those positions that are really |
| 161497 | 162492 | ** instances of the phrase (after considering deferred tokens). If this |
| | @@ -161591,10 +162586,11 @@ |
| 161591 | 162586 | } |
| 161592 | 162587 | } |
| 161593 | 162588 | |
| 161594 | 162589 | return SQLITE_OK; |
| 161595 | 162590 | } |
| 162591 | +#endif /* SQLITE_DISABLE_FTS4_DEFERRED */ |
| 161596 | 162592 | |
| 161597 | 162593 | /* |
| 161598 | 162594 | ** Maximum number of tokens a phrase may have to be considered for the |
| 161599 | 162595 | ** incremental doclists strategy. |
| 161600 | 162596 | */ |
| | @@ -163839,11 +164835,12 @@ |
| 163839 | 164835 | 0, /* xRollback */ |
| 163840 | 164836 | 0, /* xFindFunction */ |
| 163841 | 164837 | 0, /* xRename */ |
| 163842 | 164838 | 0, /* xSavepoint */ |
| 163843 | 164839 | 0, /* xRelease */ |
| 163844 | | - 0 /* xRollbackTo */ |
| 164840 | + 0, /* xRollbackTo */ |
| 164841 | + 0 /* xShadowName */ |
| 163845 | 164842 | }; |
| 163846 | 164843 | int rc; /* Return code */ |
| 163847 | 164844 | |
| 163848 | 164845 | rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0); |
| 163849 | 164846 | return rc; |
| | @@ -167398,11 +168395,12 @@ |
| 167398 | 168395 | 0, /* xRollback */ |
| 167399 | 168396 | 0, /* xFindFunction */ |
| 167400 | 168397 | 0, /* xRename */ |
| 167401 | 168398 | 0, /* xSavepoint */ |
| 167402 | 168399 | 0, /* xRelease */ |
| 167403 | | - 0 /* xRollbackTo */ |
| 168400 | + 0, /* xRollbackTo */ |
| 168401 | + 0 /* xShadowName */ |
| 167404 | 168402 | }; |
| 167405 | 168403 | int rc; /* Return code */ |
| 167406 | 168404 | |
| 167407 | 168405 | rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash); |
| 167408 | 168406 | return rc; |
| | @@ -168786,19 +169784,23 @@ |
| 168786 | 169784 | |
| 168787 | 169785 | /* Because of the FTS3_NODE_PADDING bytes of padding, the following is |
| 168788 | 169786 | ** safe (no risk of overread) even if the node data is corrupted. */ |
| 168789 | 169787 | pNext += fts3GetVarint32(pNext, &nPrefix); |
| 168790 | 169788 | pNext += fts3GetVarint32(pNext, &nSuffix); |
| 168791 | | - if( nPrefix<0 || nSuffix<=0 |
| 168792 | | - || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] |
| 169789 | + if( nSuffix<=0 |
| 169790 | + || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix |
| 169791 | + || nPrefix>pReader->nTermAlloc |
| 168793 | 169792 | ){ |
| 168794 | 169793 | return FTS_CORRUPT_VTAB; |
| 168795 | 169794 | } |
| 168796 | 169795 | |
| 168797 | | - if( nPrefix+nSuffix>pReader->nTermAlloc ){ |
| 168798 | | - int nNew = (nPrefix+nSuffix)*2; |
| 168799 | | - char *zNew = sqlite3_realloc(pReader->zTerm, nNew); |
| 169796 | + /* Both nPrefix and nSuffix were read by fts3GetVarint32() and so are |
| 169797 | + ** between 0 and 0x7FFFFFFF. But the sum of the two may cause integer |
| 169798 | + ** overflow - hence the (i64) casts. */ |
| 169799 | + if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){ |
| 169800 | + i64 nNew = ((i64)nPrefix+nSuffix)*2; |
| 169801 | + char *zNew = sqlite3_realloc64(pReader->zTerm, nNew); |
| 168800 | 169802 | if( !zNew ){ |
| 168801 | 169803 | return SQLITE_NOMEM; |
| 168802 | 169804 | } |
| 168803 | 169805 | pReader->zTerm = zNew; |
| 168804 | 169806 | pReader->nTermAlloc = nNew; |
| | @@ -168816,11 +169818,11 @@ |
| 168816 | 169818 | |
| 168817 | 169819 | /* Check that the doclist does not appear to extend past the end of the |
| 168818 | 169820 | ** b-tree node. And that the final byte of the doclist is 0x00. If either |
| 168819 | 169821 | ** of these statements is untrue, then the data structure is corrupt. |
| 168820 | 169822 | */ |
| 168821 | | - if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] |
| 169823 | + if( (&pReader->aNode[pReader->nNode] - pReader->aDoclist)<pReader->nDoclist |
| 168822 | 169824 | || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1]) |
| 168823 | 169825 | ){ |
| 168824 | 169826 | return FTS_CORRUPT_VTAB; |
| 168825 | 169827 | } |
| 168826 | 169828 | return SQLITE_OK; |
| | @@ -171142,25 +172144,30 @@ |
| 171142 | 172144 | if( bFirst==0 ){ |
| 171143 | 172145 | p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix); |
| 171144 | 172146 | } |
| 171145 | 172147 | p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix); |
| 171146 | 172148 | |
| 172149 | + if( nPrefix>p->iOff || nSuffix>p->nNode-p->iOff ){ |
| 172150 | + return SQLITE_CORRUPT_VTAB; |
| 172151 | + } |
| 171147 | 172152 | blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc); |
| 171148 | 172153 | if( rc==SQLITE_OK ){ |
| 171149 | 172154 | memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix); |
| 171150 | 172155 | p->term.n = nPrefix+nSuffix; |
| 171151 | 172156 | p->iOff += nSuffix; |
| 171152 | 172157 | if( p->iChild==0 ){ |
| 171153 | 172158 | p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist); |
| 172159 | + if( (p->nNode-p->iOff)<p->nDoclist ){ |
| 172160 | + return SQLITE_CORRUPT_VTAB; |
| 172161 | + } |
| 171154 | 172162 | p->aDoclist = &p->aNode[p->iOff]; |
| 171155 | 172163 | p->iOff += p->nDoclist; |
| 171156 | 172164 | } |
| 171157 | 172165 | } |
| 171158 | 172166 | } |
| 171159 | 172167 | |
| 171160 | 172168 | assert( p->iOff<=p->nNode ); |
| 171161 | | - |
| 171162 | 172169 | return rc; |
| 171163 | 172170 | } |
| 171164 | 172171 | |
| 171165 | 172172 | /* |
| 171166 | 172173 | ** Release all dynamic resources held by node-reader object *p. |
| | @@ -177566,10 +178573,13 @@ |
| 177566 | 178573 | #define JEACH_ATOM 3 |
| 177567 | 178574 | #define JEACH_ID 4 |
| 177568 | 178575 | #define JEACH_PARENT 5 |
| 177569 | 178576 | #define JEACH_FULLKEY 6 |
| 177570 | 178577 | #define JEACH_PATH 7 |
| 178578 | +/* The xBestIndex method assumes that the JSON and ROOT columns are |
| 178579 | +** the last two columns in the table. Should this ever changes, be |
| 178580 | +** sure to update the xBestIndex method. */ |
| 177571 | 178581 | #define JEACH_JSON 8 |
| 177572 | 178582 | #define JEACH_ROOT 9 |
| 177573 | 178583 | |
| 177574 | 178584 | UNUSED_PARAM(pzErr); |
| 177575 | 178585 | UNUSED_PARAM(argv); |
| | @@ -177823,39 +178833,58 @@ |
| 177823 | 178833 | */ |
| 177824 | 178834 | static int jsonEachBestIndex( |
| 177825 | 178835 | sqlite3_vtab *tab, |
| 177826 | 178836 | sqlite3_index_info *pIdxInfo |
| 177827 | 178837 | ){ |
| 177828 | | - int i; |
| 177829 | | - int jsonIdx = -1; |
| 177830 | | - int rootIdx = -1; |
| 178838 | + int i; /* Loop counter or computed array index */ |
| 178839 | + int aIdx[2]; /* Index of constraints for JSON and ROOT */ |
| 178840 | + int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */ |
| 178841 | + int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */ |
| 177831 | 178842 | const struct sqlite3_index_constraint *pConstraint; |
| 177832 | 178843 | |
| 178844 | + /* This implementation assumes that JSON and ROOT are the last two |
| 178845 | + ** columns in the table */ |
| 178846 | + assert( JEACH_ROOT == JEACH_JSON+1 ); |
| 177833 | 178847 | UNUSED_PARAM(tab); |
| 178848 | + aIdx[0] = aIdx[1] = -1; |
| 177834 | 178849 | pConstraint = pIdxInfo->aConstraint; |
| 177835 | 178850 | for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
| 177836 | | - if( pConstraint->usable==0 ) continue; |
| 177837 | | - if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 177838 | | - switch( pConstraint->iColumn ){ |
| 177839 | | - case JEACH_JSON: jsonIdx = i; break; |
| 177840 | | - case JEACH_ROOT: rootIdx = i; break; |
| 177841 | | - default: /* no-op */ break; |
| 178851 | + int iCol; |
| 178852 | + int iMask; |
| 178853 | + if( pConstraint->iColumn < JEACH_JSON ) continue; |
| 178854 | + iCol = pConstraint->iColumn - JEACH_JSON; |
| 178855 | + assert( iCol==0 || iCol==1 ); |
| 178856 | + iMask = 1 << iCol; |
| 178857 | + if( pConstraint->usable==0 ){ |
| 178858 | + unusableMask |= iMask; |
| 178859 | + }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ |
| 178860 | + aIdx[iCol] = i; |
| 178861 | + idxMask |= iMask; |
| 177842 | 178862 | } |
| 177843 | 178863 | } |
| 177844 | | - if( jsonIdx<0 ){ |
| 178864 | + if( (unusableMask & ~idxMask)!=0 ){ |
| 178865 | + /* If there are any unusable constraints on JSON or ROOT, then reject |
| 178866 | + ** this entire plan */ |
| 178867 | + return SQLITE_CONSTRAINT; |
| 178868 | + } |
| 178869 | + if( aIdx[0]<0 ){ |
| 178870 | + /* No JSON input. Leave estimatedCost at the huge value that it was |
| 178871 | + ** initialized to to discourage the query planner from selecting this |
| 178872 | + ** plan. */ |
| 177845 | 178873 | pIdxInfo->idxNum = 0; |
| 177846 | | - pIdxInfo->estimatedCost = 1e99; |
| 177847 | 178874 | }else{ |
| 177848 | 178875 | pIdxInfo->estimatedCost = 1.0; |
| 177849 | | - pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1; |
| 177850 | | - pIdxInfo->aConstraintUsage[jsonIdx].omit = 1; |
| 177851 | | - if( rootIdx<0 ){ |
| 177852 | | - pIdxInfo->idxNum = 1; |
| 178876 | + i = aIdx[0]; |
| 178877 | + pIdxInfo->aConstraintUsage[i].argvIndex = 1; |
| 178878 | + pIdxInfo->aConstraintUsage[i].omit = 1; |
| 178879 | + if( aIdx[1]<0 ){ |
| 178880 | + pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */ |
| 177853 | 178881 | }else{ |
| 177854 | | - pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2; |
| 177855 | | - pIdxInfo->aConstraintUsage[rootIdx].omit = 1; |
| 177856 | | - pIdxInfo->idxNum = 3; |
| 178882 | + i = aIdx[1]; |
| 178883 | + pIdxInfo->aConstraintUsage[i].argvIndex = 2; |
| 178884 | + pIdxInfo->aConstraintUsage[i].omit = 1; |
| 178885 | + pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */ |
| 177857 | 178886 | } |
| 177858 | 178887 | } |
| 177859 | 178888 | return SQLITE_OK; |
| 177860 | 178889 | } |
| 177861 | 178890 | |
| | @@ -177960,11 +178989,12 @@ |
| 177960 | 178989 | 0, /* xRollback */ |
| 177961 | 178990 | 0, /* xFindMethod */ |
| 177962 | 178991 | 0, /* xRename */ |
| 177963 | 178992 | 0, /* xSavepoint */ |
| 177964 | 178993 | 0, /* xRelease */ |
| 177965 | | - 0 /* xRollbackTo */ |
| 178994 | + 0, /* xRollbackTo */ |
| 178995 | + 0 /* xShadowName */ |
| 177966 | 178996 | }; |
| 177967 | 178997 | |
| 177968 | 178998 | /* The methods of the json_tree virtual table. */ |
| 177969 | 178999 | static sqlite3_module jsonTreeModule = { |
| 177970 | 179000 | 0, /* iVersion */ |
| | @@ -177987,11 +179017,12 @@ |
| 177987 | 179017 | 0, /* xRollback */ |
| 177988 | 179018 | 0, /* xFindMethod */ |
| 177989 | 179019 | 0, /* xRename */ |
| 177990 | 179020 | 0, /* xSavepoint */ |
| 177991 | 179021 | 0, /* xRelease */ |
| 177992 | | - 0 /* xRollbackTo */ |
| 179022 | + 0, /* xRollbackTo */ |
| 179023 | + 0 /* xShadowName */ |
| 177993 | 179024 | }; |
| 177994 | 179025 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 177995 | 179026 | |
| 177996 | 179027 | /**************************************************************************** |
| 177997 | 179028 | ** The following routines are the only publically visible identifiers in this |
| | @@ -181417,12 +182448,28 @@ |
| 181417 | 182448 | } |
| 181418 | 182449 | |
| 181419 | 182450 | return rc; |
| 181420 | 182451 | } |
| 181421 | 182452 | |
| 182453 | + |
| 182454 | +/* |
| 182455 | +** Return true if zName is the extension on one of the shadow tables used |
| 182456 | +** by this module. |
| 182457 | +*/ |
| 182458 | +static int rtreeShadowName(const char *zName){ |
| 182459 | + static const char *azName[] = { |
| 182460 | + "node", "parent", "rowid" |
| 182461 | + }; |
| 182462 | + unsigned int i; |
| 182463 | + for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){ |
| 182464 | + if( sqlite3_stricmp(zName, azName[i])==0 ) return 1; |
| 182465 | + } |
| 182466 | + return 0; |
| 182467 | +} |
| 182468 | + |
| 181422 | 182469 | static sqlite3_module rtreeModule = { |
| 181423 | | - 2, /* iVersion */ |
| 182470 | + 3, /* iVersion */ |
| 181424 | 182471 | rtreeCreate, /* xCreate - create a table */ |
| 181425 | 182472 | rtreeConnect, /* xConnect - connect to an existing table */ |
| 181426 | 182473 | rtreeBestIndex, /* xBestIndex - Determine search strategy */ |
| 181427 | 182474 | rtreeDisconnect, /* xDisconnect - Disconnect from a table */ |
| 181428 | 182475 | rtreeDestroy, /* xDestroy - Drop a table */ |
| | @@ -181441,10 +182488,11 @@ |
| 181441 | 182488 | 0, /* xFindFunction - function overloading */ |
| 181442 | 182489 | rtreeRename, /* xRename - rename the table */ |
| 181443 | 182490 | rtreeSavepoint, /* xSavepoint */ |
| 181444 | 182491 | 0, /* xRelease */ |
| 181445 | 182492 | 0, /* xRollbackTo */ |
| 182493 | + rtreeShadowName /* xShadowName */ |
| 181446 | 182494 | }; |
| 181447 | 182495 | |
| 181448 | 182496 | static int rtreeSqlInit( |
| 181449 | 182497 | Rtree *pRtree, |
| 181450 | 182498 | sqlite3 *db, |
| | @@ -182431,17 +183479,27 @@ |
| 182431 | 183479 | ** The on-disk representation consists of a 4-byte header followed by |
| 182432 | 183480 | ** the values. The 4-byte header is: |
| 182433 | 183481 | ** |
| 182434 | 183482 | ** encoding (1 byte) 0=big-endian, 1=little-endian |
| 182435 | 183483 | ** nvertex (3 bytes) Number of vertexes as a big-endian integer |
| 183484 | +** |
| 183485 | +** Enough space is allocated for 4 coordinates, to work around over-zealous |
| 183486 | +** warnings coming from some compiler (notably, clang). In reality, the size |
| 183487 | +** of each GeoPoly memory allocate is adjusted as necessary so that the |
| 183488 | +** GeoPoly.a[] array at the end is the appropriate size. |
| 182436 | 183489 | */ |
| 182437 | 183490 | typedef struct GeoPoly GeoPoly; |
| 182438 | 183491 | struct GeoPoly { |
| 182439 | 183492 | int nVertex; /* Number of vertexes */ |
| 182440 | 183493 | unsigned char hdr[4]; /* Header for on-disk representation */ |
| 182441 | | - GeoCoord a[2]; /* 2*nVertex values. X (longitude) first, then Y */ |
| 183494 | + GeoCoord a[8]; /* 2*nVertex values. X (longitude) first, then Y */ |
| 182442 | 183495 | }; |
| 183496 | + |
| 183497 | +/* The size of a memory allocation needed for a GeoPoly object sufficient |
| 183498 | +** to hold N coordinate pairs. |
| 183499 | +*/ |
| 183500 | +#define GEOPOLY_SZ(N) (sizeof(GeoPoly) + sizeof(GeoCoord)*2*((N)-4)) |
| 182443 | 183501 | |
| 182444 | 183502 | /* |
| 182445 | 183503 | ** State of a parse of a GeoJSON input. |
| 182446 | 183504 | */ |
| 182447 | 183505 | typedef struct GeoParse GeoParse; |
| | @@ -182463,11 +183521,11 @@ |
| 182463 | 183521 | a[2] = t; |
| 182464 | 183522 | } |
| 182465 | 183523 | |
| 182466 | 183524 | /* Skip whitespace. Return the next non-whitespace character. */ |
| 182467 | 183525 | static char geopolySkipSpace(GeoParse *p){ |
| 182468 | | - while( p->z[0] && safe_isspace(p->z[0]) ) p->z++; |
| 183526 | + while( safe_isspace(p->z[0]) ) p->z++; |
| 182469 | 183527 | return p->z[0]; |
| 182470 | 183528 | } |
| 182471 | 183529 | |
| 182472 | 183530 | /* Parse out a number. Write the value into *pVal if pVal!=0. |
| 182473 | 183531 | ** return non-zero on success and zero if the next token is not a number. |
| | @@ -182483,11 +183541,11 @@ |
| 182483 | 183541 | c = z[j]; |
| 182484 | 183542 | } |
| 182485 | 183543 | if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0; |
| 182486 | 183544 | for(;; j++){ |
| 182487 | 183545 | c = z[j]; |
| 182488 | | - if( c>='0' && c<='9' ) continue; |
| 183546 | + if( safe_isdigit(c) ) continue; |
| 182489 | 183547 | if( c=='.' ){ |
| 182490 | 183548 | if( z[j-1]=='-' ) return 0; |
| 182491 | 183549 | if( seenDP ) return 0; |
| 182492 | 183550 | seenDP = 1; |
| 182493 | 183551 | continue; |
| | @@ -182505,11 +183563,21 @@ |
| 182505 | 183563 | continue; |
| 182506 | 183564 | } |
| 182507 | 183565 | break; |
| 182508 | 183566 | } |
| 182509 | 183567 | if( z[j-1]<'0' ) return 0; |
| 182510 | | - if( pVal ) *pVal = (GeoCoord)atof((const char*)p->z); |
| 183568 | + if( pVal ){ |
| 183569 | +#ifdef SQLITE_AMALGAMATION |
| 183570 | + /* The sqlite3AtoF() routine is much much faster than atof(), if it |
| 183571 | + ** is available */ |
| 183572 | + double r; |
| 183573 | + (void)sqlite3AtoF((const char*)p->z, &r, j, SQLITE_UTF8); |
| 183574 | + *pVal = r; |
| 183575 | +#else |
| 183576 | + *pVal = (GeoCoord)atof((const char*)p->z); |
| 183577 | +#endif |
| 183578 | + } |
| 182511 | 183579 | p->z += j; |
| 182512 | 183580 | return 1; |
| 182513 | 183581 | } |
| 182514 | 183582 | |
| 182515 | 183583 | /* |
| | @@ -182563,16 +183631,14 @@ |
| 182563 | 183631 | && s.nVertex>=4 |
| 182564 | 183632 | && s.a[0]==s.a[s.nVertex*2-2] |
| 182565 | 183633 | && s.a[1]==s.a[s.nVertex*2-1] |
| 182566 | 183634 | && (s.z++, geopolySkipSpace(&s)==0) |
| 182567 | 183635 | ){ |
| 182568 | | - int nByte; |
| 182569 | 183636 | GeoPoly *pOut; |
| 182570 | 183637 | int x = 1; |
| 182571 | 183638 | s.nVertex--; /* Remove the redundant vertex at the end */ |
| 182572 | | - nByte = sizeof(GeoPoly) * s.nVertex*2*sizeof(GeoCoord); |
| 182573 | | - pOut = sqlite3_malloc64( nByte ); |
| 183639 | + pOut = sqlite3_malloc64( GEOPOLY_SZ(s.nVertex) ); |
| 182574 | 183640 | x = 1; |
| 182575 | 183641 | if( pOut==0 ) goto parse_json_err; |
| 182576 | 183642 | pOut->nVertex = s.nVertex; |
| 182577 | 183643 | memcpy(pOut->a, s.a, s.nVertex*2*sizeof(GeoCoord)); |
| 182578 | 183644 | pOut->hdr[0] = *(unsigned char*)&x; |
| | @@ -182770,10 +183836,31 @@ |
| 182770 | 183836 | sqlite3_result_blob(context, p->hdr, |
| 182771 | 183837 | 4+8*p->nVertex, SQLITE_TRANSIENT); |
| 182772 | 183838 | sqlite3_free(p); |
| 182773 | 183839 | } |
| 182774 | 183840 | } |
| 183841 | + |
| 183842 | +/* |
| 183843 | +** Compute the area enclosed by the polygon. |
| 183844 | +** |
| 183845 | +** This routine can also be used to detect polygons that rotate in |
| 183846 | +** the wrong direction. Polygons are suppose to be counter-clockwise (CCW). |
| 183847 | +** This routine returns a negative value for clockwise (CW) polygons. |
| 183848 | +*/ |
| 183849 | +static double geopolyArea(GeoPoly *p){ |
| 183850 | + double rArea = 0.0; |
| 183851 | + int ii; |
| 183852 | + for(ii=0; ii<p->nVertex-1; ii++){ |
| 183853 | + rArea += (p->a[ii*2] - p->a[ii*2+2]) /* (x0 - x1) */ |
| 183854 | + * (p->a[ii*2+1] + p->a[ii*2+3]) /* (y0 + y1) */ |
| 183855 | + * 0.5; |
| 183856 | + } |
| 183857 | + rArea += (p->a[ii*2] - p->a[0]) /* (xN - x0) */ |
| 183858 | + * (p->a[ii*2+1] + p->a[1]) /* (yN + y0) */ |
| 183859 | + * 0.5; |
| 183860 | + return rArea; |
| 183861 | +} |
| 182775 | 183862 | |
| 182776 | 183863 | /* |
| 182777 | 183864 | ** Implementation of the geopoly_area(X) function. |
| 182778 | 183865 | ** |
| 182779 | 183866 | ** If the input is a well-formed Geopoly BLOB then return the area |
| | @@ -182786,24 +183873,109 @@ |
| 182786 | 183873 | int argc, |
| 182787 | 183874 | sqlite3_value **argv |
| 182788 | 183875 | ){ |
| 182789 | 183876 | GeoPoly *p = geopolyFuncParam(context, argv[0], 0); |
| 182790 | 183877 | if( p ){ |
| 182791 | | - double rArea = 0.0; |
| 182792 | | - int ii; |
| 182793 | | - for(ii=0; ii<p->nVertex-1; ii++){ |
| 182794 | | - rArea += (p->a[ii*2] - p->a[ii*2+2]) /* (x0 - x1) */ |
| 182795 | | - * (p->a[ii*2+1] + p->a[ii*2+3]) /* (y0 + y1) */ |
| 182796 | | - * 0.5; |
| 182797 | | - } |
| 182798 | | - rArea += (p->a[ii*2] - p->a[0]) /* (xN - x0) */ |
| 182799 | | - * (p->a[ii*2+1] + p->a[1]) /* (yN + y0) */ |
| 182800 | | - * 0.5; |
| 182801 | | - sqlite3_result_double(context, rArea); |
| 183878 | + sqlite3_result_double(context, geopolyArea(p)); |
| 183879 | + sqlite3_free(p); |
| 183880 | + } |
| 183881 | +} |
| 183882 | + |
| 183883 | +/* |
| 183884 | +** Implementation of the geopoly_ccw(X) function. |
| 183885 | +** |
| 183886 | +** If the rotation of polygon X is clockwise (incorrect) instead of |
| 183887 | +** counter-clockwise (the correct winding order according to RFC7946) |
| 183888 | +** then reverse the order of the vertexes in polygon X. |
| 183889 | +** |
| 183890 | +** In other words, this routine returns a CCW polygon regardless of the |
| 183891 | +** winding order of its input. |
| 183892 | +** |
| 183893 | +** Use this routine to sanitize historical inputs that that sometimes |
| 183894 | +** contain polygons that wind in the wrong direction. |
| 183895 | +*/ |
| 183896 | +static void geopolyCcwFunc( |
| 183897 | + sqlite3_context *context, |
| 183898 | + int argc, |
| 183899 | + sqlite3_value **argv |
| 183900 | +){ |
| 183901 | + GeoPoly *p = geopolyFuncParam(context, argv[0], 0); |
| 183902 | + if( p ){ |
| 183903 | + if( geopolyArea(p)<0.0 ){ |
| 183904 | + int ii, jj; |
| 183905 | + for(ii=2, jj=p->nVertex*2 - 2; ii<jj; ii+=2, jj-=2){ |
| 183906 | + GeoCoord t = p->a[ii]; |
| 183907 | + p->a[ii] = p->a[jj]; |
| 183908 | + p->a[jj] = t; |
| 183909 | + t = p->a[ii+1]; |
| 183910 | + p->a[ii+1] = p->a[jj+1]; |
| 183911 | + p->a[jj+1] = t; |
| 183912 | + } |
| 183913 | + } |
| 183914 | + sqlite3_result_blob(context, p->hdr, |
| 183915 | + 4+8*p->nVertex, SQLITE_TRANSIENT); |
| 182802 | 183916 | sqlite3_free(p); |
| 182803 | 183917 | } |
| 182804 | 183918 | } |
| 183919 | + |
| 183920 | +#define GEOPOLY_PI 3.1415926535897932385 |
| 183921 | + |
| 183922 | +/* Fast approximation for sine(X) for X between -0.5*pi and 2*pi |
| 183923 | +*/ |
| 183924 | +static double geopolySine(double r){ |
| 183925 | + assert( r>=-0.5*GEOPOLY_PI && r<=2.0*GEOPOLY_PI ); |
| 183926 | + if( r>=1.5*GEOPOLY_PI ){ |
| 183927 | + r -= 2.0*GEOPOLY_PI; |
| 183928 | + } |
| 183929 | + if( r>=0.5*GEOPOLY_PI ){ |
| 183930 | + return -geopolySine(r-GEOPOLY_PI); |
| 183931 | + }else{ |
| 183932 | + double r2 = r*r; |
| 183933 | + double r3 = r2*r; |
| 183934 | + double r5 = r3*r2; |
| 183935 | + return 0.9996949*r - 0.1656700*r3 + 0.0075134*r5; |
| 183936 | + } |
| 183937 | +} |
| 183938 | + |
| 183939 | +/* |
| 183940 | +** Function: geopoly_regular(X,Y,R,N) |
| 183941 | +** |
| 183942 | +** Construct a simple, convex, regular polygon centered at X, Y |
| 183943 | +** with circumradius R and with N sides. |
| 183944 | +*/ |
| 183945 | +static void geopolyRegularFunc( |
| 183946 | + sqlite3_context *context, |
| 183947 | + int argc, |
| 183948 | + sqlite3_value **argv |
| 183949 | +){ |
| 183950 | + double x = sqlite3_value_double(argv[0]); |
| 183951 | + double y = sqlite3_value_double(argv[1]); |
| 183952 | + double r = sqlite3_value_double(argv[2]); |
| 183953 | + int n = sqlite3_value_int(argv[3]); |
| 183954 | + int i; |
| 183955 | + GeoPoly *p; |
| 183956 | + |
| 183957 | + if( n<3 || r<=0.0 ) return; |
| 183958 | + if( n>1000 ) n = 1000; |
| 183959 | + p = sqlite3_malloc64( sizeof(*p) + (n-1)*2*sizeof(GeoCoord) ); |
| 183960 | + if( p==0 ){ |
| 183961 | + sqlite3_result_error_nomem(context); |
| 183962 | + return; |
| 183963 | + } |
| 183964 | + i = 1; |
| 183965 | + p->hdr[0] = *(unsigned char*)&i; |
| 183966 | + p->hdr[1] = 0; |
| 183967 | + p->hdr[2] = (n>>8)&0xff; |
| 183968 | + p->hdr[3] = n&0xff; |
| 183969 | + for(i=0; i<n; i++){ |
| 183970 | + double rAngle = 2.0*GEOPOLY_PI*i/n; |
| 183971 | + p->a[i*2] = x - r*geopolySine(rAngle-0.5*GEOPOLY_PI); |
| 183972 | + p->a[i*2+1] = y + r*geopolySine(rAngle); |
| 183973 | + } |
| 183974 | + sqlite3_result_blob(context, p->hdr, 4+8*n, SQLITE_TRANSIENT); |
| 183975 | + sqlite3_free(p); |
| 183976 | +} |
| 182805 | 183977 | |
| 182806 | 183978 | /* |
| 182807 | 183979 | ** If pPoly is a polygon, compute its bounding box. Then: |
| 182808 | 183980 | ** |
| 182809 | 183981 | ** (1) if aCoord!=0 store the bounding box in aCoord, returning NULL |
| | @@ -182845,11 +184017,11 @@ |
| 182845 | 184017 | else if( r>mxY ) mxY = (float)r; |
| 182846 | 184018 | } |
| 182847 | 184019 | if( pRc ) *pRc = SQLITE_OK; |
| 182848 | 184020 | if( aCoord==0 ){ |
| 182849 | 184021 | geopolyBboxFill: |
| 182850 | | - pOut = sqlite3_realloc(p, sizeof(GeoPoly)+sizeof(GeoCoord)*6); |
| 184022 | + pOut = sqlite3_realloc(p, GEOPOLY_SZ(4)); |
| 182851 | 184023 | if( pOut==0 ){ |
| 182852 | 184024 | sqlite3_free(p); |
| 182853 | 184025 | if( context ) sqlite3_result_error_nomem(context); |
| 182854 | 184026 | if( pRc ) *pRc = SQLITE_NOMEM; |
| 182855 | 184027 | return 0; |
| | @@ -183873,11 +185045,20 @@ |
| 183873 | 185045 | sqlite3_bind_int64(pUp, 1, cell.iRowid); |
| 183874 | 185046 | assert( pRtree->nAux>=1 ); |
| 183875 | 185047 | if( sqlite3_value_nochange(aData[2]) ){ |
| 183876 | 185048 | sqlite3_bind_null(pUp, 2); |
| 183877 | 185049 | }else{ |
| 183878 | | - sqlite3_bind_value(pUp, 2, aData[2]); |
| 185050 | + GeoPoly *p = 0; |
| 185051 | + if( sqlite3_value_type(aData[2])==SQLITE_TEXT |
| 185052 | + && (p = geopolyFuncParam(0, aData[2], &rc))!=0 |
| 185053 | + && rc==SQLITE_OK |
| 185054 | + ){ |
| 185055 | + sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT); |
| 185056 | + }else{ |
| 185057 | + sqlite3_bind_value(pUp, 2, aData[2]); |
| 185058 | + } |
| 185059 | + sqlite3_free(p); |
| 183879 | 185060 | nChange = 1; |
| 183880 | 185061 | } |
| 183881 | 185062 | for(jj=1; jj<pRtree->nAux; jj++){ |
| 183882 | 185063 | nChange++; |
| 183883 | 185064 | sqlite3_bind_value(pUp, jj+2, aData[jj+2]); |
| | @@ -183917,11 +185098,11 @@ |
| 183917 | 185098 | return 0; |
| 183918 | 185099 | } |
| 183919 | 185100 | |
| 183920 | 185101 | |
| 183921 | 185102 | static sqlite3_module geopolyModule = { |
| 183922 | | - 2, /* iVersion */ |
| 185103 | + 3, /* iVersion */ |
| 183923 | 185104 | geopolyCreate, /* xCreate - create a table */ |
| 183924 | 185105 | geopolyConnect, /* xConnect - connect to an existing table */ |
| 183925 | 185106 | geopolyBestIndex, /* xBestIndex - Determine search strategy */ |
| 183926 | 185107 | rtreeDisconnect, /* xDisconnect - Disconnect from a table */ |
| 183927 | 185108 | rtreeDestroy, /* xDestroy - Drop a table */ |
| | @@ -183940,29 +185121,33 @@ |
| 183940 | 185121 | geopolyFindFunction, /* xFindFunction - function overloading */ |
| 183941 | 185122 | rtreeRename, /* xRename - rename the table */ |
| 183942 | 185123 | rtreeSavepoint, /* xSavepoint */ |
| 183943 | 185124 | 0, /* xRelease */ |
| 183944 | 185125 | 0, /* xRollbackTo */ |
| 185126 | + rtreeShadowName /* xShadowName */ |
| 183945 | 185127 | }; |
| 183946 | 185128 | |
| 183947 | 185129 | static int sqlite3_geopoly_init(sqlite3 *db){ |
| 183948 | 185130 | int rc = SQLITE_OK; |
| 183949 | 185131 | static const struct { |
| 183950 | 185132 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
| 183951 | | - int nArg; |
| 185133 | + signed char nArg; |
| 185134 | + unsigned char bPure; |
| 183952 | 185135 | const char *zName; |
| 183953 | 185136 | } aFunc[] = { |
| 183954 | | - { geopolyAreaFunc, 1, "geopoly_area" }, |
| 183955 | | - { geopolyBlobFunc, 1, "geopoly_blob" }, |
| 183956 | | - { geopolyJsonFunc, 1, "geopoly_json" }, |
| 183957 | | - { geopolySvgFunc, -1, "geopoly_svg" }, |
| 183958 | | - { geopolyWithinFunc, 2, "geopoly_within" }, |
| 183959 | | - { geopolyContainsPointFunc, 3, "geopoly_contains_point" }, |
| 183960 | | - { geopolyOverlapFunc, 2, "geopoly_overlap" }, |
| 183961 | | - { geopolyDebugFunc, 1, "geopoly_debug" }, |
| 183962 | | - { geopolyBBoxFunc, 1, "geopoly_bbox" }, |
| 183963 | | - { geopolyXformFunc, 7, "geopoly_xform" }, |
| 185137 | + { geopolyAreaFunc, 1, 1, "geopoly_area" }, |
| 185138 | + { geopolyBlobFunc, 1, 1, "geopoly_blob" }, |
| 185139 | + { geopolyJsonFunc, 1, 1, "geopoly_json" }, |
| 185140 | + { geopolySvgFunc, -1, 1, "geopoly_svg" }, |
| 185141 | + { geopolyWithinFunc, 2, 1, "geopoly_within" }, |
| 185142 | + { geopolyContainsPointFunc, 3, 1, "geopoly_contains_point" }, |
| 185143 | + { geopolyOverlapFunc, 2, 1, "geopoly_overlap" }, |
| 185144 | + { geopolyDebugFunc, 1, 0, "geopoly_debug" }, |
| 185145 | + { geopolyBBoxFunc, 1, 1, "geopoly_bbox" }, |
| 185146 | + { geopolyXformFunc, 7, 1, "geopoly_xform" }, |
| 185147 | + { geopolyRegularFunc, 4, 1, "geopoly_regular" }, |
| 185148 | + { geopolyCcwFunc, 1, 1, "geopoly_ccw" }, |
| 183964 | 185149 | }; |
| 183965 | 185150 | static const struct { |
| 183966 | 185151 | void (*xStep)(sqlite3_context*,int,sqlite3_value**); |
| 183967 | 185152 | void (*xFinal)(sqlite3_context*); |
| 183968 | 185153 | const char *zName; |
| | @@ -183969,12 +185154,13 @@ |
| 183969 | 185154 | } aAgg[] = { |
| 183970 | 185155 | { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox" }, |
| 183971 | 185156 | }; |
| 183972 | 185157 | int i; |
| 183973 | 185158 | for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){ |
| 185159 | + int enc = aFunc[i].bPure ? SQLITE_UTF8|SQLITE_DETERMINISTIC : SQLITE_UTF8; |
| 183974 | 185160 | rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg, |
| 183975 | | - SQLITE_UTF8, 0, |
| 185161 | + enc, 0, |
| 183976 | 185162 | aFunc[i].xFunc, 0, 0); |
| 183977 | 185163 | } |
| 183978 | 185164 | for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){ |
| 183979 | 185165 | rc = sqlite3_create_function(db, aAgg[i].zName, 1, SQLITE_UTF8, 0, |
| 183980 | 185166 | 0, aAgg[i].xStep, aAgg[i].xFinal); |
| | @@ -185997,11 +187183,12 @@ |
| 185997 | 187183 | struct rbu_vfs { |
| 185998 | 187184 | sqlite3_vfs base; /* rbu VFS shim methods */ |
| 185999 | 187185 | sqlite3_vfs *pRealVfs; /* Underlying VFS */ |
| 186000 | 187186 | sqlite3_mutex *mutex; /* Mutex to protect pMain */ |
| 186001 | 187187 | sqlite3rbu *pRbu; /* Owner RBU object */ |
| 186002 | | - rbu_file *pMain; /* Linked list of main db files */ |
| 187188 | + rbu_file *pMain; /* List of main db files */ |
| 187189 | + rbu_file *pMainRbu; /* List of main db files with pRbu!=0 */ |
| 186003 | 187190 | }; |
| 186004 | 187191 | |
| 186005 | 187192 | /* |
| 186006 | 187193 | ** Each file opened by an rbu VFS is represented by an instance of |
| 186007 | 187194 | ** the following structure. |
| | @@ -186026,10 +187213,11 @@ |
| 186026 | 187213 | char *zDel; /* Delete this when closing file */ |
| 186027 | 187214 | |
| 186028 | 187215 | const char *zWal; /* Wal filename for this main db file */ |
| 186029 | 187216 | rbu_file *pWalFd; /* Wal file descriptor for this main db */ |
| 186030 | 187217 | rbu_file *pMainNext; /* Next MAIN_DB file */ |
| 187218 | + rbu_file *pMainRbuNext; /* Next MAIN_DB file with pRbu!=0 */ |
| 186031 | 187219 | }; |
| 186032 | 187220 | |
| 186033 | 187221 | /* |
| 186034 | 187222 | ** True for an RBU vacuum handle, or false otherwise. |
| 186035 | 187223 | */ |
| | @@ -189621,10 +190809,73 @@ |
| 189621 | 190809 | pFd->sz = nNew; |
| 189622 | 190810 | assert( pRbu->szTemp>=0 ); |
| 189623 | 190811 | if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL; |
| 189624 | 190812 | return SQLITE_OK; |
| 189625 | 190813 | } |
| 190814 | + |
| 190815 | +/* |
| 190816 | +** Add an item to the main-db lists, if it is not already present. |
| 190817 | +** |
| 190818 | +** There are two main-db lists. One for all file descriptors, and one |
| 190819 | +** for all file descriptors with rbu_file.pDb!=0. If the argument has |
| 190820 | +** rbu_file.pDb!=0, then it is assumed to already be present on the |
| 190821 | +** main list and is only added to the pDb!=0 list. |
| 190822 | +*/ |
| 190823 | +static void rbuMainlistAdd(rbu_file *p){ |
| 190824 | + rbu_vfs *pRbuVfs = p->pRbuVfs; |
| 190825 | + rbu_file *pIter; |
| 190826 | + assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) ); |
| 190827 | + sqlite3_mutex_enter(pRbuVfs->mutex); |
| 190828 | + if( p->pRbu==0 ){ |
| 190829 | + for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext); |
| 190830 | + p->pMainNext = pRbuVfs->pMain; |
| 190831 | + pRbuVfs->pMain = p; |
| 190832 | + }else{ |
| 190833 | + for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){} |
| 190834 | + if( pIter==0 ){ |
| 190835 | + p->pMainRbuNext = pRbuVfs->pMainRbu; |
| 190836 | + pRbuVfs->pMainRbu = p; |
| 190837 | + } |
| 190838 | + } |
| 190839 | + sqlite3_mutex_leave(pRbuVfs->mutex); |
| 190840 | +} |
| 190841 | + |
| 190842 | +/* |
| 190843 | +** Remove an item from the main-db lists. |
| 190844 | +*/ |
| 190845 | +static void rbuMainlistRemove(rbu_file *p){ |
| 190846 | + rbu_file **pp; |
| 190847 | + sqlite3_mutex_enter(p->pRbuVfs->mutex); |
| 190848 | + for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){} |
| 190849 | + if( *pp ) *pp = p->pMainNext; |
| 190850 | + p->pMainNext = 0; |
| 190851 | + for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){} |
| 190852 | + if( *pp ) *pp = p->pMainRbuNext; |
| 190853 | + p->pMainRbuNext = 0; |
| 190854 | + sqlite3_mutex_leave(p->pRbuVfs->mutex); |
| 190855 | +} |
| 190856 | + |
| 190857 | +/* |
| 190858 | +** Given that zWal points to a buffer containing a wal file name passed to |
| 190859 | +** either the xOpen() or xAccess() VFS method, search the main-db list for |
| 190860 | +** a file-handle opened by the same database connection on the corresponding |
| 190861 | +** database file. |
| 190862 | +** |
| 190863 | +** If parameter bRbu is true, only search for file-descriptors with |
| 190864 | +** rbu_file.pDb!=0. |
| 190865 | +*/ |
| 190866 | +static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){ |
| 190867 | + rbu_file *pDb; |
| 190868 | + sqlite3_mutex_enter(pRbuVfs->mutex); |
| 190869 | + if( bRbu ){ |
| 190870 | + for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){} |
| 190871 | + }else{ |
| 190872 | + for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){} |
| 190873 | + } |
| 190874 | + sqlite3_mutex_leave(pRbuVfs->mutex); |
| 190875 | + return pDb; |
| 190876 | +} |
| 189626 | 190877 | |
| 189627 | 190878 | /* |
| 189628 | 190879 | ** Close an rbu file. |
| 189629 | 190880 | */ |
| 189630 | 190881 | static int rbuVfsClose(sqlite3_file *pFile){ |
| | @@ -189639,21 +190890,18 @@ |
| 189639 | 190890 | sqlite3_free(p->apShm); |
| 189640 | 190891 | p->apShm = 0; |
| 189641 | 190892 | sqlite3_free(p->zDel); |
| 189642 | 190893 | |
| 189643 | 190894 | if( p->openFlags & SQLITE_OPEN_MAIN_DB ){ |
| 189644 | | - rbu_file **pp; |
| 189645 | | - sqlite3_mutex_enter(p->pRbuVfs->mutex); |
| 189646 | | - for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext)); |
| 189647 | | - *pp = p->pMainNext; |
| 189648 | | - sqlite3_mutex_leave(p->pRbuVfs->mutex); |
| 190895 | + rbuMainlistRemove(p); |
| 189649 | 190896 | rbuUnlockShm(p); |
| 189650 | 190897 | p->pReal->pMethods->xShmUnmap(p->pReal, 0); |
| 189651 | 190898 | } |
| 189652 | 190899 | else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){ |
| 189653 | 190900 | rbuUpdateTempSize(p, 0); |
| 189654 | 190901 | } |
| 190902 | + assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p ); |
| 189655 | 190903 | |
| 189656 | 190904 | /* Close the underlying file handle */ |
| 189657 | 190905 | rc = p->pReal->pMethods->xClose(p->pReal); |
| 189658 | 190906 | return rc; |
| 189659 | 190907 | } |
| | @@ -189908,10 +191156,13 @@ |
| 189908 | 191156 | rc = SQLITE_ERROR; |
| 189909 | 191157 | pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error"); |
| 189910 | 191158 | }else if( rc==SQLITE_NOTFOUND ){ |
| 189911 | 191159 | pRbu->pTargetFd = p; |
| 189912 | 191160 | p->pRbu = pRbu; |
| 191161 | + if( p->openFlags & SQLITE_OPEN_MAIN_DB ){ |
| 191162 | + rbuMainlistAdd(p); |
| 191163 | + } |
| 189913 | 191164 | if( p->pWalFd ) p->pWalFd->pRbu = pRbu; |
| 189914 | 191165 | rc = SQLITE_OK; |
| 189915 | 191166 | } |
| 189916 | 191167 | } |
| 189917 | 191168 | return rc; |
| | @@ -190069,24 +191320,10 @@ |
| 190069 | 191320 | rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag); |
| 190070 | 191321 | } |
| 190071 | 191322 | return rc; |
| 190072 | 191323 | } |
| 190073 | 191324 | |
| 190074 | | -/* |
| 190075 | | -** Given that zWal points to a buffer containing a wal file name passed to |
| 190076 | | -** either the xOpen() or xAccess() VFS method, return a pointer to the |
| 190077 | | -** file-handle opened by the same database connection on the corresponding |
| 190078 | | -** database file. |
| 190079 | | -*/ |
| 190080 | | -static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal){ |
| 190081 | | - rbu_file *pDb; |
| 190082 | | - sqlite3_mutex_enter(pRbuVfs->mutex); |
| 190083 | | - for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){} |
| 190084 | | - sqlite3_mutex_leave(pRbuVfs->mutex); |
| 190085 | | - return pDb; |
| 190086 | | -} |
| 190087 | | - |
| 190088 | 191325 | /* |
| 190089 | 191326 | ** A main database named zName has just been opened. The following |
| 190090 | 191327 | ** function returns a pointer to a buffer owned by SQLite that contains |
| 190091 | 191328 | ** the name of the *-wal file this db connection will use. SQLite |
| 190092 | 191329 | ** happens to pass a pointer to this buffer when using xAccess() |
| | @@ -190161,11 +191398,11 @@ |
| 190161 | 191398 | ** happens to pass a pointer to this buffer when using xAccess() |
| 190162 | 191399 | ** or xOpen() to operate on the *-wal file. */ |
| 190163 | 191400 | pFd->zWal = rbuMainToWal(zName, flags); |
| 190164 | 191401 | } |
| 190165 | 191402 | else if( flags & SQLITE_OPEN_WAL ){ |
| 190166 | | - rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName); |
| 191403 | + rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0); |
| 190167 | 191404 | if( pDb ){ |
| 190168 | 191405 | if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){ |
| 190169 | 191406 | /* This call is to open a *-wal file. Intead, open the *-oal. This |
| 190170 | 191407 | ** code ensures that the string passed to xOpen() is terminated by a |
| 190171 | 191408 | ** pair of '\0' bytes in case the VFS attempts to extract a URI |
| | @@ -190213,14 +191450,11 @@ |
| 190213 | 191450 | /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods |
| 190214 | 191451 | ** pointer and, if the file is a main database file, link it into the |
| 190215 | 191452 | ** mutex protected linked list of all such files. */ |
| 190216 | 191453 | pFile->pMethods = &rbuvfs_io_methods; |
| 190217 | 191454 | if( flags & SQLITE_OPEN_MAIN_DB ){ |
| 190218 | | - sqlite3_mutex_enter(pRbuVfs->mutex); |
| 190219 | | - pFd->pMainNext = pRbuVfs->pMain; |
| 190220 | | - pRbuVfs->pMain = pFd; |
| 190221 | | - sqlite3_mutex_leave(pRbuVfs->mutex); |
| 191455 | + rbuMainlistAdd(pFd); |
| 190222 | 191456 | } |
| 190223 | 191457 | }else{ |
| 190224 | 191458 | sqlite3_free(pFd->zDel); |
| 190225 | 191459 | } |
| 190226 | 191460 | |
| | @@ -190264,11 +191498,11 @@ |
| 190264 | 191498 | ** causing SQLite to call xOpen() to open it. This call will also |
| 190265 | 191499 | ** be intercepted (see the rbuVfsOpen() function) and the *-oal |
| 190266 | 191500 | ** file opened instead. |
| 190267 | 191501 | */ |
| 190268 | 191502 | if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){ |
| 190269 | | - rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath); |
| 191503 | + rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1); |
| 190270 | 191504 | if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){ |
| 190271 | 191505 | if( *pResOut ){ |
| 190272 | 191506 | rc = SQLITE_CANTOPEN; |
| 190273 | 191507 | }else{ |
| 190274 | 191508 | sqlite3_int64 sz = 0; |
| | @@ -190677,21 +191911,19 @@ |
| 190677 | 191911 | ** idxNum is normally 0, but will be 1 if a schema=? constraint exists. |
| 190678 | 191912 | */ |
| 190679 | 191913 | static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
| 190680 | 191914 | int i; |
| 190681 | 191915 | |
| 190682 | | - pIdxInfo->estimatedCost = 1.0e6; /* Initial cost estimate */ |
| 190683 | | - |
| 190684 | 191916 | /* Look for a valid schema=? constraint. If found, change the idxNum to |
| 190685 | 191917 | ** 1 and request the value of that constraint be sent to xFilter. And |
| 190686 | 191918 | ** lower the cost estimate to encourage the constrained version to be |
| 190687 | 191919 | ** used. |
| 190688 | 191920 | */ |
| 190689 | 191921 | for(i=0; i<pIdxInfo->nConstraint; i++){ |
| 190690 | | - if( pIdxInfo->aConstraint[i].usable==0 ) continue; |
| 190691 | | - if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 190692 | 191922 | if( pIdxInfo->aConstraint[i].iColumn!=10 ) continue; |
| 191923 | + if( pIdxInfo->aConstraint[i].usable==0 ) return SQLITE_CONSTRAINT; |
| 191924 | + if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 190693 | 191925 | pIdxInfo->idxNum = 1; |
| 190694 | 191926 | pIdxInfo->estimatedCost = 1.0; |
| 190695 | 191927 | pIdxInfo->aConstraintUsage[i].argvIndex = 1; |
| 190696 | 191928 | pIdxInfo->aConstraintUsage[i].omit = 1; |
| 190697 | 191929 | break; |
| | @@ -190737,18 +191969,24 @@ |
| 190737 | 191969 | |
| 190738 | 191970 | *ppCursor = (sqlite3_vtab_cursor *)pCsr; |
| 190739 | 191971 | return SQLITE_OK; |
| 190740 | 191972 | } |
| 190741 | 191973 | |
| 190742 | | -static void statClearPage(StatPage *p){ |
| 191974 | +static void statClearCells(StatPage *p){ |
| 190743 | 191975 | int i; |
| 190744 | 191976 | if( p->aCell ){ |
| 190745 | 191977 | for(i=0; i<p->nCell; i++){ |
| 190746 | 191978 | sqlite3_free(p->aCell[i].aOvfl); |
| 190747 | 191979 | } |
| 190748 | 191980 | sqlite3_free(p->aCell); |
| 190749 | 191981 | } |
| 191982 | + p->nCell = 0; |
| 191983 | + p->aCell = 0; |
| 191984 | +} |
| 191985 | + |
| 191986 | +static void statClearPage(StatPage *p){ |
| 191987 | + statClearCells(p); |
| 190750 | 191988 | sqlite3PagerUnref(p->pPg); |
| 190751 | 191989 | sqlite3_free(p->zPath); |
| 190752 | 191990 | memset(p, 0, sizeof(StatPage)); |
| 190753 | 191991 | } |
| 190754 | 191992 | |
| | @@ -190807,26 +192045,37 @@ |
| 190807 | 192045 | |
| 190808 | 192046 | u8 *aData = sqlite3PagerGetData(p->pPg); |
| 190809 | 192047 | u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0]; |
| 190810 | 192048 | |
| 190811 | 192049 | p->flags = aHdr[0]; |
| 192050 | + if( p->flags==0x0A || p->flags==0x0D ){ |
| 192051 | + isLeaf = 1; |
| 192052 | + nHdr = 8; |
| 192053 | + }else if( p->flags==0x05 || p->flags==0x02 ){ |
| 192054 | + isLeaf = 0; |
| 192055 | + nHdr = 12; |
| 192056 | + }else{ |
| 192057 | + goto statPageIsCorrupt; |
| 192058 | + } |
| 192059 | + if( p->iPgno==1 ) nHdr += 100; |
| 190812 | 192060 | p->nCell = get2byte(&aHdr[3]); |
| 190813 | 192061 | p->nMxPayload = 0; |
| 190814 | | - |
| 190815 | | - isLeaf = (p->flags==0x0A || p->flags==0x0D); |
| 190816 | | - nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100; |
| 192062 | + szPage = sqlite3BtreeGetPageSize(pBt); |
| 190817 | 192063 | |
| 190818 | 192064 | nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell; |
| 190819 | 192065 | nUnused += (int)aHdr[7]; |
| 190820 | 192066 | iOff = get2byte(&aHdr[1]); |
| 190821 | 192067 | while( iOff ){ |
| 192068 | + int iNext; |
| 192069 | + if( iOff>=szPage ) goto statPageIsCorrupt; |
| 190822 | 192070 | nUnused += get2byte(&aData[iOff+2]); |
| 190823 | | - iOff = get2byte(&aData[iOff]); |
| 192071 | + iNext = get2byte(&aData[iOff]); |
| 192072 | + if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt; |
| 192073 | + iOff = iNext; |
| 190824 | 192074 | } |
| 190825 | 192075 | p->nUnused = nUnused; |
| 190826 | 192076 | p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]); |
| 190827 | | - szPage = sqlite3BtreeGetPageSize(pBt); |
| 190828 | 192077 | |
| 190829 | 192078 | if( p->nCell ){ |
| 190830 | 192079 | int i; /* Used to iterate through cells */ |
| 190831 | 192080 | int nUsable; /* Usable bytes per page */ |
| 190832 | 192081 | |
| | @@ -190839,10 +192088,11 @@ |
| 190839 | 192088 | |
| 190840 | 192089 | for(i=0; i<p->nCell; i++){ |
| 190841 | 192090 | StatCell *pCell = &p->aCell[i]; |
| 190842 | 192091 | |
| 190843 | 192092 | iOff = get2byte(&aData[nHdr+i*2]); |
| 192093 | + if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt; |
| 190844 | 192094 | if( !isLeaf ){ |
| 190845 | 192095 | pCell->iChildPg = sqlite3Get4byte(&aData[iOff]); |
| 190846 | 192096 | iOff += 4; |
| 190847 | 192097 | } |
| 190848 | 192098 | if( p->flags==0x05 ){ |
| | @@ -190855,17 +192105,18 @@ |
| 190855 | 192105 | u64 dummy; |
| 190856 | 192106 | iOff += sqlite3GetVarint(&aData[iOff], &dummy); |
| 190857 | 192107 | } |
| 190858 | 192108 | if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload; |
| 190859 | 192109 | getLocalPayload(nUsable, p->flags, nPayload, &nLocal); |
| 192110 | + if( nLocal<0 ) goto statPageIsCorrupt; |
| 190860 | 192111 | pCell->nLocal = nLocal; |
| 190861 | | - assert( nLocal>=0 ); |
| 190862 | 192112 | assert( nPayload>=(u32)nLocal ); |
| 190863 | 192113 | assert( nLocal<=(nUsable-35) ); |
| 190864 | 192114 | if( nPayload>(u32)nLocal ){ |
| 190865 | 192115 | int j; |
| 190866 | 192116 | int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4); |
| 192117 | + if( iOff+nLocal>nUsable ) goto statPageIsCorrupt; |
| 190867 | 192118 | pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4); |
| 190868 | 192119 | pCell->nOvfl = nOvfl; |
| 190869 | 192120 | pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl); |
| 190870 | 192121 | if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT; |
| 190871 | 192122 | pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]); |
| | @@ -190885,10 +192136,15 @@ |
| 190885 | 192136 | } |
| 190886 | 192137 | } |
| 190887 | 192138 | } |
| 190888 | 192139 | |
| 190889 | 192140 | return SQLITE_OK; |
| 192141 | + |
| 192142 | +statPageIsCorrupt: |
| 192143 | + p->flags = 0; |
| 192144 | + statClearCells(p); |
| 192145 | + return SQLITE_OK; |
| 190890 | 192146 | } |
| 190891 | 192147 | |
| 190892 | 192148 | /* |
| 190893 | 192149 | ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on |
| 190894 | 192150 | ** the current value of pCsr->iPageno. |
| | @@ -191180,10 +192436,11 @@ |
| 191180 | 192436 | 0, /* xFindMethod */ |
| 191181 | 192437 | 0, /* xRename */ |
| 191182 | 192438 | 0, /* xSavepoint */ |
| 191183 | 192439 | 0, /* xRelease */ |
| 191184 | 192440 | 0, /* xRollbackTo */ |
| 192441 | + 0 /* xShadowName */ |
| 191185 | 192442 | }; |
| 191186 | 192443 | return sqlite3_create_module(db, "dbstat", &dbstat_module, 0); |
| 191187 | 192444 | } |
| 191188 | 192445 | #elif defined(SQLITE_ENABLE_DBSTAT_VTAB) |
| 191189 | 192446 | SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; } |
| | @@ -191310,13 +192567,12 @@ |
| 191310 | 192567 | for(i=0; i<pIdxInfo->nConstraint; i++){ |
| 191311 | 192568 | struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i]; |
| 191312 | 192569 | if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue; |
| 191313 | 192570 | if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 191314 | 192571 | if( !p->usable ){ |
| 191315 | | - /* No solution. Use the default SQLITE_BIG_DBL cost */ |
| 191316 | | - pIdxInfo->estimatedRows = 0x7fffffff; |
| 191317 | | - return SQLITE_OK; |
| 192572 | + /* No solution. */ |
| 192573 | + return SQLITE_CONSTRAINT; |
| 191318 | 192574 | } |
| 191319 | 192575 | iPlan = 2; |
| 191320 | 192576 | pIdxInfo->aConstraintUsage[i].argvIndex = 1; |
| 191321 | 192577 | pIdxInfo->aConstraintUsage[i].omit = 1; |
| 191322 | 192578 | break; |
| | @@ -191504,10 +192760,14 @@ |
| 191504 | 192760 | int iDb; |
| 191505 | 192761 | Btree *pBt; |
| 191506 | 192762 | Pager *pPager; |
| 191507 | 192763 | int szPage; |
| 191508 | 192764 | |
| 192765 | + if( pTab->db->flags & SQLITE_Defensive ){ |
| 192766 | + zErr = "read-only"; |
| 192767 | + goto update_fail; |
| 192768 | + } |
| 191509 | 192769 | if( argc==1 ){ |
| 191510 | 192770 | zErr = "cannot delete"; |
| 191511 | 192771 | goto update_fail; |
| 191512 | 192772 | } |
| 191513 | 192773 | pgno = sqlite3_value_int(argv[0]); |
| | @@ -191594,10 +192854,11 @@ |
| 191594 | 192854 | 0, /* xFindMethod */ |
| 191595 | 192855 | 0, /* xRename */ |
| 191596 | 192856 | 0, /* xSavepoint */ |
| 191597 | 192857 | 0, /* xRelease */ |
| 191598 | 192858 | 0, /* xRollbackTo */ |
| 192859 | + 0 /* xShadowName */ |
| 191599 | 192860 | }; |
| 191600 | 192861 | return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0); |
| 191601 | 192862 | } |
| 191602 | 192863 | #elif defined(SQLITE_ENABLE_DBPAGE_VTAB) |
| 191603 | 192864 | SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK; } |
| | @@ -191629,10 +192890,12 @@ |
| 191629 | 192890 | # define SESSIONS_STRM_CHUNK_SIZE 64 |
| 191630 | 192891 | # else |
| 191631 | 192892 | # define SESSIONS_STRM_CHUNK_SIZE 1024 |
| 191632 | 192893 | # endif |
| 191633 | 192894 | #endif |
| 192895 | + |
| 192896 | +static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE; |
| 191634 | 192897 | |
| 191635 | 192898 | typedef struct SessionHook SessionHook; |
| 191636 | 192899 | struct SessionHook { |
| 191637 | 192900 | void *pCtx; |
| 191638 | 192901 | int (*xOld)(void*,int,sqlite3_value**); |
| | @@ -191692,10 +192955,11 @@ |
| 191692 | 192955 | */ |
| 191693 | 192956 | struct sqlite3_changeset_iter { |
| 191694 | 192957 | SessionInput in; /* Input buffer or stream */ |
| 191695 | 192958 | SessionBuffer tblhdr; /* Buffer to hold apValue/zTab/abPK/ */ |
| 191696 | 192959 | int bPatchset; /* True if this is a patchset */ |
| 192960 | + int bInvert; /* True to invert changeset */ |
| 191697 | 192961 | int rc; /* Iterator error code */ |
| 191698 | 192962 | sqlite3_stmt *pConflict; /* Points to conflicting row, if any */ |
| 191699 | 192963 | char *zTab; /* Current table */ |
| 191700 | 192964 | int nCol; /* Number of columns in zTab */ |
| 191701 | 192965 | int op; /* Current operation */ |
| | @@ -191848,10 +193112,46 @@ |
| 191848 | 193112 | ** and fields associated with modified columns contain the new column values. |
| 191849 | 193113 | ** |
| 191850 | 193114 | ** The records associated with INSERT changes are in the same format as for |
| 191851 | 193115 | ** changesets. It is not possible for a record associated with an INSERT |
| 191852 | 193116 | ** change to contain a field set to "undefined". |
| 193117 | +** |
| 193118 | +** REBASE BLOB FORMAT: |
| 193119 | +** |
| 193120 | +** A rebase blob may be output by sqlite3changeset_apply_v2() and its |
| 193121 | +** streaming equivalent for use with the sqlite3_rebaser APIs to rebase |
| 193122 | +** existing changesets. A rebase blob contains one entry for each conflict |
| 193123 | +** resolved using either the OMIT or REPLACE strategies within the apply_v2() |
| 193124 | +** call. |
| 193125 | +** |
| 193126 | +** The format used for a rebase blob is very similar to that used for |
| 193127 | +** changesets. All entries related to a single table are grouped together. |
| 193128 | +** |
| 193129 | +** Each group of entries begins with a table header in changeset format: |
| 193130 | +** |
| 193131 | +** 1 byte: Constant 0x54 (capital 'T') |
| 193132 | +** Varint: Number of columns in the table. |
| 193133 | +** nCol bytes: 0x01 for PK columns, 0x00 otherwise. |
| 193134 | +** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated. |
| 193135 | +** |
| 193136 | +** Followed by one or more entries associated with the table. |
| 193137 | +** |
| 193138 | +** 1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09). |
| 193139 | +** 1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT. |
| 193140 | +** record: (in the record format defined above). |
| 193141 | +** |
| 193142 | +** In a rebase blob, the first field is set to SQLITE_INSERT if the change |
| 193143 | +** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if |
| 193144 | +** it was a DELETE. The second field is set to 0x01 if the conflict |
| 193145 | +** resolution strategy was REPLACE, or 0x00 if it was OMIT. |
| 193146 | +** |
| 193147 | +** If the change that caused the conflict was a DELETE, then the single |
| 193148 | +** record is a copy of the old.* record from the original changeset. If it |
| 193149 | +** was an INSERT, then the single record is a copy of the new.* record. If |
| 193150 | +** the conflicting change was an UPDATE, then the single record is a copy |
| 193151 | +** of the new.* record with the PK fields filled in based on the original |
| 193152 | +** old.* record. |
| 191853 | 193153 | */ |
| 191854 | 193154 | |
| 191855 | 193155 | /* |
| 191856 | 193156 | ** For each row modified during a session, there exists a single instance of |
| 191857 | 193157 | ** this structure stored in a SessionTable.aChange[] hash table. |
| | @@ -193398,16 +194698,16 @@ |
| 193398 | 194698 | ** set *pRc to SQLITE_NOMEM and return non-zero. |
| 193399 | 194699 | */ |
| 193400 | 194700 | static int sessionBufferGrow(SessionBuffer *p, int nByte, int *pRc){ |
| 193401 | 194701 | if( *pRc==SQLITE_OK && p->nAlloc-p->nBuf<nByte ){ |
| 193402 | 194702 | u8 *aNew; |
| 193403 | | - int nNew = p->nAlloc ? p->nAlloc : 128; |
| 194703 | + i64 nNew = p->nAlloc ? p->nAlloc : 128; |
| 193404 | 194704 | do { |
| 193405 | 194705 | nNew = nNew*2; |
| 193406 | | - }while( nNew<(p->nBuf+nByte) ); |
| 194706 | + }while( (nNew-p->nBuf)<nByte ); |
| 193407 | 194707 | |
| 193408 | | - aNew = (u8 *)sqlite3_realloc(p->aBuf, nNew); |
| 194708 | + aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew); |
| 193409 | 194709 | if( 0==aNew ){ |
| 193410 | 194710 | *pRc = SQLITE_NOMEM; |
| 193411 | 194711 | }else{ |
| 193412 | 194712 | p->aBuf = aNew; |
| 193413 | 194713 | p->nAlloc = nNew; |
| | @@ -194001,16 +195301,16 @@ |
| 194001 | 195301 | } |
| 194002 | 195302 | if( rc==SQLITE_OK ){ |
| 194003 | 195303 | rc = sqlite3_reset(pSel); |
| 194004 | 195304 | } |
| 194005 | 195305 | |
| 194006 | | - /* If the buffer is now larger than SESSIONS_STRM_CHUNK_SIZE, pass |
| 195306 | + /* If the buffer is now larger than sessions_strm_chunk_size, pass |
| 194007 | 195307 | ** its contents to the xOutput() callback. */ |
| 194008 | 195308 | if( xOutput |
| 194009 | 195309 | && rc==SQLITE_OK |
| 194010 | 195310 | && buf.nBuf>nNoop |
| 194011 | | - && buf.nBuf>SESSIONS_STRM_CHUNK_SIZE |
| 195311 | + && buf.nBuf>sessions_strm_chunk_size |
| 194012 | 195312 | ){ |
| 194013 | 195313 | rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf); |
| 194014 | 195314 | nNoop = -1; |
| 194015 | 195315 | buf.nBuf = 0; |
| 194016 | 195316 | } |
| | @@ -194145,11 +195445,12 @@ |
| 194145 | 195445 | static int sessionChangesetStart( |
| 194146 | 195446 | sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */ |
| 194147 | 195447 | int (*xInput)(void *pIn, void *pData, int *pnData), |
| 194148 | 195448 | void *pIn, |
| 194149 | 195449 | int nChangeset, /* Size of buffer pChangeset in bytes */ |
| 194150 | | - void *pChangeset /* Pointer to buffer containing changeset */ |
| 195450 | + void *pChangeset, /* Pointer to buffer containing changeset */ |
| 195451 | + int bInvert /* True to invert changeset */ |
| 194151 | 195452 | ){ |
| 194152 | 195453 | sqlite3_changeset_iter *pRet; /* Iterator to return */ |
| 194153 | 195454 | int nByte; /* Number of bytes to allocate for iterator */ |
| 194154 | 195455 | |
| 194155 | 195456 | assert( xInput==0 || (pChangeset==0 && nChangeset==0) ); |
| | @@ -194165,10 +195466,11 @@ |
| 194165 | 195466 | pRet->in.aData = (u8 *)pChangeset; |
| 194166 | 195467 | pRet->in.nData = nChangeset; |
| 194167 | 195468 | pRet->in.xInput = xInput; |
| 194168 | 195469 | pRet->in.pIn = pIn; |
| 194169 | 195470 | pRet->in.bEof = (xInput ? 0 : 1); |
| 195471 | + pRet->bInvert = bInvert; |
| 194170 | 195472 | |
| 194171 | 195473 | /* Populate the output variable and return success. */ |
| 194172 | 195474 | *pp = pRet; |
| 194173 | 195475 | return SQLITE_OK; |
| 194174 | 195476 | } |
| | @@ -194179,11 +195481,20 @@ |
| 194179 | 195481 | SQLITE_API int sqlite3changeset_start( |
| 194180 | 195482 | sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */ |
| 194181 | 195483 | int nChangeset, /* Size of buffer pChangeset in bytes */ |
| 194182 | 195484 | void *pChangeset /* Pointer to buffer containing changeset */ |
| 194183 | 195485 | ){ |
| 194184 | | - return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset); |
| 195486 | + return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0); |
| 195487 | +} |
| 195488 | +SQLITE_API int sqlite3changeset_start_v2( |
| 195489 | + sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */ |
| 195490 | + int nChangeset, /* Size of buffer pChangeset in bytes */ |
| 195491 | + void *pChangeset, /* Pointer to buffer containing changeset */ |
| 195492 | + int flags |
| 195493 | +){ |
| 195494 | + int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT); |
| 195495 | + return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert); |
| 194185 | 195496 | } |
| 194186 | 195497 | |
| 194187 | 195498 | /* |
| 194188 | 195499 | ** Streaming version of sqlite3changeset_start(). |
| 194189 | 195500 | */ |
| | @@ -194190,19 +195501,28 @@ |
| 194190 | 195501 | SQLITE_API int sqlite3changeset_start_strm( |
| 194191 | 195502 | sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */ |
| 194192 | 195503 | int (*xInput)(void *pIn, void *pData, int *pnData), |
| 194193 | 195504 | void *pIn |
| 194194 | 195505 | ){ |
| 194195 | | - return sessionChangesetStart(pp, xInput, pIn, 0, 0); |
| 195506 | + return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0); |
| 195507 | +} |
| 195508 | +SQLITE_API int sqlite3changeset_start_v2_strm( |
| 195509 | + sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */ |
| 195510 | + int (*xInput)(void *pIn, void *pData, int *pnData), |
| 195511 | + void *pIn, |
| 195512 | + int flags |
| 195513 | +){ |
| 195514 | + int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT); |
| 195515 | + return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert); |
| 194196 | 195516 | } |
| 194197 | 195517 | |
| 194198 | 195518 | /* |
| 194199 | 195519 | ** If the SessionInput object passed as the only argument is a streaming |
| 194200 | 195520 | ** object and the buffer is full, discard some data to free up space. |
| 194201 | 195521 | */ |
| 194202 | 195522 | static void sessionDiscardData(SessionInput *pIn){ |
| 194203 | | - if( pIn->xInput && pIn->iNext>=SESSIONS_STRM_CHUNK_SIZE ){ |
| 195523 | + if( pIn->xInput && pIn->iNext>=sessions_strm_chunk_size ){ |
| 194204 | 195524 | int nMove = pIn->buf.nBuf - pIn->iNext; |
| 194205 | 195525 | assert( nMove>=0 ); |
| 194206 | 195526 | if( nMove>0 ){ |
| 194207 | 195527 | memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove); |
| 194208 | 195528 | } |
| | @@ -194221,11 +195541,11 @@ |
| 194221 | 195541 | */ |
| 194222 | 195542 | static int sessionInputBuffer(SessionInput *pIn, int nByte){ |
| 194223 | 195543 | int rc = SQLITE_OK; |
| 194224 | 195544 | if( pIn->xInput ){ |
| 194225 | 195545 | while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){ |
| 194226 | | - int nNew = SESSIONS_STRM_CHUNK_SIZE; |
| 195546 | + int nNew = sessions_strm_chunk_size; |
| 194227 | 195547 | |
| 194228 | 195548 | if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn); |
| 194229 | 195549 | if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){ |
| 194230 | 195550 | rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew); |
| 194231 | 195551 | if( nNew==0 ){ |
| | @@ -194569,14 +195889,14 @@ |
| 194569 | 195889 | p->in.iCurrent = p->in.iNext; |
| 194570 | 195890 | if( p->in.iNext>=p->in.nData ) return SQLITE_DONE; |
| 194571 | 195891 | op = p->in.aData[p->in.iNext++]; |
| 194572 | 195892 | } |
| 194573 | 195893 | |
| 194574 | | - if( p->zTab==0 ){ |
| 195894 | + if( p->zTab==0 || (p->bPatchset && p->bInvert) ){ |
| 194575 | 195895 | /* The first record in the changeset is not a table header. Must be a |
| 194576 | 195896 | ** corrupt changeset. */ |
| 194577 | | - assert( p->in.iNext==1 ); |
| 195897 | + assert( p->in.iNext==1 || p->zTab ); |
| 194578 | 195898 | return (p->rc = SQLITE_CORRUPT_BKPT); |
| 194579 | 195899 | } |
| 194580 | 195900 | |
| 194581 | 195901 | p->op = op; |
| 194582 | 195902 | p->bIndirect = p->in.aData[p->in.iNext++]; |
| | @@ -194597,37 +195917,43 @@ |
| 194597 | 195917 | p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec); |
| 194598 | 195918 | if( p->rc!=SQLITE_OK ) return p->rc; |
| 194599 | 195919 | *paRec = &p->in.aData[p->in.iNext]; |
| 194600 | 195920 | p->in.iNext += *pnRec; |
| 194601 | 195921 | }else{ |
| 195922 | + sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue); |
| 195923 | + sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]); |
| 194602 | 195924 | |
| 194603 | 195925 | /* If this is an UPDATE or DELETE, read the old.* record. */ |
| 194604 | 195926 | if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){ |
| 194605 | 195927 | u8 *abPK = p->bPatchset ? p->abPK : 0; |
| 194606 | | - p->rc = sessionReadRecord(&p->in, p->nCol, abPK, p->apValue); |
| 195928 | + p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld); |
| 194607 | 195929 | if( p->rc!=SQLITE_OK ) return p->rc; |
| 194608 | 195930 | } |
| 194609 | 195931 | |
| 194610 | 195932 | /* If this is an INSERT or UPDATE, read the new.* record. */ |
| 194611 | 195933 | if( p->op!=SQLITE_DELETE ){ |
| 194612 | | - p->rc = sessionReadRecord(&p->in, p->nCol, 0, &p->apValue[p->nCol]); |
| 195934 | + p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew); |
| 194613 | 195935 | if( p->rc!=SQLITE_OK ) return p->rc; |
| 194614 | 195936 | } |
| 194615 | 195937 | |
| 194616 | | - if( p->bPatchset && p->op==SQLITE_UPDATE ){ |
| 195938 | + if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE ){ |
| 194617 | 195939 | /* If this is an UPDATE that is part of a patchset, then all PK and |
| 194618 | 195940 | ** modified fields are present in the new.* record. The old.* record |
| 194619 | 195941 | ** is currently completely empty. This block shifts the PK fields from |
| 194620 | 195942 | ** new.* to old.*, to accommodate the code that reads these arrays. */ |
| 194621 | 195943 | for(i=0; i<p->nCol; i++){ |
| 194622 | | - assert( p->apValue[i]==0 ); |
| 195944 | + assert( p->bPatchset==0 || p->apValue[i]==0 ); |
| 194623 | 195945 | if( p->abPK[i] ){ |
| 195946 | + assert( p->apValue[i]==0 ); |
| 194624 | 195947 | p->apValue[i] = p->apValue[i+p->nCol]; |
| 194625 | 195948 | if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT); |
| 194626 | 195949 | p->apValue[i+p->nCol] = 0; |
| 194627 | 195950 | } |
| 194628 | 195951 | } |
| 195952 | + }else if( p->bInvert ){ |
| 195953 | + if( p->op==SQLITE_INSERT ) p->op = SQLITE_DELETE; |
| 195954 | + else if( p->op==SQLITE_DELETE ) p->op = SQLITE_INSERT; |
| 194629 | 195955 | } |
| 194630 | 195956 | } |
| 194631 | 195957 | |
| 194632 | 195958 | return SQLITE_ROW; |
| 194633 | 195959 | } |
| | @@ -194940,11 +196266,11 @@ |
| 194940 | 196266 | rc = SQLITE_CORRUPT_BKPT; |
| 194941 | 196267 | goto finished_invert; |
| 194942 | 196268 | } |
| 194943 | 196269 | |
| 194944 | 196270 | assert( rc==SQLITE_OK ); |
| 194945 | | - if( xOutput && sOut.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){ |
| 196271 | + if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){ |
| 194946 | 196272 | rc = xOutput(pOut, sOut.aBuf, sOut.nBuf); |
| 194947 | 196273 | sOut.nBuf = 0; |
| 194948 | 196274 | if( rc!=SQLITE_OK ) goto finished_invert; |
| 194949 | 196275 | } |
| 194950 | 196276 | } |
| | @@ -195019,11 +196345,12 @@ |
| 195019 | 196345 | u8 *abPK; /* Boolean array - true if column is in PK */ |
| 195020 | 196346 | int bStat1; /* True if table is sqlite_stat1 */ |
| 195021 | 196347 | int bDeferConstraints; /* True to defer constraints */ |
| 195022 | 196348 | SessionBuffer constraints; /* Deferred constraints are stored here */ |
| 195023 | 196349 | SessionBuffer rebase; /* Rebase information (if any) here */ |
| 195024 | | - int bRebaseStarted; /* If table header is already in rebase */ |
| 196350 | + u8 bRebaseStarted; /* If table header is already in rebase */ |
| 196351 | + u8 bRebase; /* True to collect rebase information */ |
| 195025 | 196352 | }; |
| 195026 | 196353 | |
| 195027 | 196354 | /* |
| 195028 | 196355 | ** Formulate a statement to DELETE a row from database db. Assuming a table |
| 195029 | 196356 | ** structure like this: |
| | @@ -195416,39 +196743,40 @@ |
| 195416 | 196743 | SessionApplyCtx *p, /* Apply context */ |
| 195417 | 196744 | int eType, /* Conflict resolution (OMIT or REPLACE) */ |
| 195418 | 196745 | sqlite3_changeset_iter *pIter /* Iterator pointing at current change */ |
| 195419 | 196746 | ){ |
| 195420 | 196747 | int rc = SQLITE_OK; |
| 195421 | | - int i; |
| 195422 | | - int eOp = pIter->op; |
| 195423 | | - if( p->bRebaseStarted==0 ){ |
| 195424 | | - /* Append a table-header to the rebase buffer */ |
| 195425 | | - const char *zTab = pIter->zTab; |
| 195426 | | - sessionAppendByte(&p->rebase, 'T', &rc); |
| 195427 | | - sessionAppendVarint(&p->rebase, p->nCol, &rc); |
| 195428 | | - sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc); |
| 195429 | | - sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc); |
| 195430 | | - p->bRebaseStarted = 1; |
| 195431 | | - } |
| 195432 | | - |
| 195433 | | - assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT ); |
| 195434 | | - assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE ); |
| 195435 | | - |
| 195436 | | - sessionAppendByte(&p->rebase, |
| 195437 | | - (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc |
| 195438 | | - ); |
| 195439 | | - sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc); |
| 195440 | | - for(i=0; i<p->nCol; i++){ |
| 195441 | | - sqlite3_value *pVal = 0; |
| 195442 | | - if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){ |
| 195443 | | - sqlite3changeset_old(pIter, i, &pVal); |
| 195444 | | - }else{ |
| 195445 | | - sqlite3changeset_new(pIter, i, &pVal); |
| 195446 | | - } |
| 195447 | | - sessionAppendValue(&p->rebase, pVal, &rc); |
| 195448 | | - } |
| 195449 | | - |
| 196748 | + if( p->bRebase ){ |
| 196749 | + int i; |
| 196750 | + int eOp = pIter->op; |
| 196751 | + if( p->bRebaseStarted==0 ){ |
| 196752 | + /* Append a table-header to the rebase buffer */ |
| 196753 | + const char *zTab = pIter->zTab; |
| 196754 | + sessionAppendByte(&p->rebase, 'T', &rc); |
| 196755 | + sessionAppendVarint(&p->rebase, p->nCol, &rc); |
| 196756 | + sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc); |
| 196757 | + sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc); |
| 196758 | + p->bRebaseStarted = 1; |
| 196759 | + } |
| 196760 | + |
| 196761 | + assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT ); |
| 196762 | + assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE ); |
| 196763 | + |
| 196764 | + sessionAppendByte(&p->rebase, |
| 196765 | + (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc |
| 196766 | + ); |
| 196767 | + sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc); |
| 196768 | + for(i=0; i<p->nCol; i++){ |
| 196769 | + sqlite3_value *pVal = 0; |
| 196770 | + if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){ |
| 196771 | + sqlite3changeset_old(pIter, i, &pVal); |
| 196772 | + }else{ |
| 196773 | + sqlite3changeset_new(pIter, i, &pVal); |
| 196774 | + } |
| 196775 | + sessionAppendValue(&p->rebase, pVal, &rc); |
| 196776 | + } |
| 196777 | + } |
| 195450 | 196778 | return rc; |
| 195451 | 196779 | } |
| 195452 | 196780 | |
| 195453 | 196781 | /* |
| 195454 | 196782 | ** Invoke the conflict handler for the change that the changeset iterator |
| | @@ -195787,11 +197115,11 @@ |
| 195787 | 197115 | while( pApply->constraints.nBuf ){ |
| 195788 | 197116 | sqlite3_changeset_iter *pIter2 = 0; |
| 195789 | 197117 | SessionBuffer cons = pApply->constraints; |
| 195790 | 197118 | memset(&pApply->constraints, 0, sizeof(SessionBuffer)); |
| 195791 | 197119 | |
| 195792 | | - rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf); |
| 197120 | + rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf, 0); |
| 195793 | 197121 | if( rc==SQLITE_OK ){ |
| 195794 | 197122 | int nByte = 2*pApply->nCol*sizeof(sqlite3_value*); |
| 195795 | 197123 | int rc2; |
| 195796 | 197124 | pIter2->bPatchset = bPatchset; |
| 195797 | 197125 | pIter2->zTab = (char*)zTab; |
| | @@ -195853,10 +197181,11 @@ |
| 195853 | 197181 | |
| 195854 | 197182 | assert( xConflict!=0 ); |
| 195855 | 197183 | |
| 195856 | 197184 | pIter->in.bNoDiscard = 1; |
| 195857 | 197185 | memset(&sApply, 0, sizeof(sApply)); |
| 197186 | + sApply.bRebase = (ppRebase && pnRebase); |
| 195858 | 197187 | sqlite3_mutex_enter(sqlite3_db_mutex(db)); |
| 195859 | 197188 | if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){ |
| 195860 | 197189 | rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0); |
| 195861 | 197190 | } |
| 195862 | 197191 | if( rc==SQLITE_OK ){ |
| | @@ -196003,11 +197332,12 @@ |
| 196003 | 197332 | sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0); |
| 196004 | 197333 | sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0); |
| 196005 | 197334 | } |
| 196006 | 197335 | } |
| 196007 | 197336 | |
| 196008 | | - if( rc==SQLITE_OK && bPatchset==0 && ppRebase && pnRebase ){ |
| 197337 | + assert( sApply.bRebase || sApply.rebase.nBuf==0 ); |
| 197338 | + if( rc==SQLITE_OK && bPatchset==0 && sApply.bRebase ){ |
| 196009 | 197339 | *ppRebase = (void*)sApply.rebase.aBuf; |
| 196010 | 197340 | *pnRebase = sApply.rebase.nBuf; |
| 196011 | 197341 | sApply.rebase.aBuf = 0; |
| 196012 | 197342 | } |
| 196013 | 197343 | sqlite3_finalize(sApply.pInsert); |
| | @@ -196041,11 +197371,12 @@ |
| 196041 | 197371 | void *pCtx, /* First argument passed to xConflict */ |
| 196042 | 197372 | void **ppRebase, int *pnRebase, |
| 196043 | 197373 | int flags |
| 196044 | 197374 | ){ |
| 196045 | 197375 | sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */ |
| 196046 | | - int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset); |
| 197376 | + int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT); |
| 197377 | + int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset,bInverse); |
| 196047 | 197378 | if( rc==SQLITE_OK ){ |
| 196048 | 197379 | rc = sessionChangesetApply( |
| 196049 | 197380 | db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags |
| 196050 | 197381 | ); |
| 196051 | 197382 | } |
| | @@ -196098,11 +197429,12 @@ |
| 196098 | 197429 | void *pCtx, /* First argument passed to xConflict */ |
| 196099 | 197430 | void **ppRebase, int *pnRebase, |
| 196100 | 197431 | int flags |
| 196101 | 197432 | ){ |
| 196102 | 197433 | sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */ |
| 196103 | | - int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn); |
| 197434 | + int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT); |
| 197435 | + int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse); |
| 196104 | 197436 | if( rc==SQLITE_OK ){ |
| 196105 | 197437 | rc = sessionChangesetApply( |
| 196106 | 197438 | db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags |
| 196107 | 197439 | ); |
| 196108 | 197440 | } |
| | @@ -196471,17 +197803,16 @@ |
| 196471 | 197803 | SessionChange *p; |
| 196472 | 197804 | for(p=pTab->apChange[i]; p; p=p->pNext){ |
| 196473 | 197805 | sessionAppendByte(&buf, p->op, &rc); |
| 196474 | 197806 | sessionAppendByte(&buf, p->bIndirect, &rc); |
| 196475 | 197807 | sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc); |
| 197808 | + if( rc==SQLITE_OK && xOutput && buf.nBuf>=sessions_strm_chunk_size ){ |
| 197809 | + rc = xOutput(pOut, buf.aBuf, buf.nBuf); |
| 197810 | + buf.nBuf = 0; |
| 197811 | + } |
| 196476 | 197812 | } |
| 196477 | 197813 | } |
| 196478 | | - |
| 196479 | | - if( rc==SQLITE_OK && xOutput && buf.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){ |
| 196480 | | - rc = xOutput(pOut, buf.aBuf, buf.nBuf); |
| 196481 | | - buf.nBuf = 0; |
| 196482 | | - } |
| 196483 | 197814 | } |
| 196484 | 197815 | |
| 196485 | 197816 | if( rc==SQLITE_OK ){ |
| 196486 | 197817 | if( xOutput ){ |
| 196487 | 197818 | if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf); |
| | @@ -196868,11 +198199,11 @@ |
| 196868 | 198199 | if( bDone==0 ){ |
| 196869 | 198200 | sessionAppendByte(&sOut, pIter->op, &rc); |
| 196870 | 198201 | sessionAppendByte(&sOut, pIter->bIndirect, &rc); |
| 196871 | 198202 | sessionAppendBlob(&sOut, aRec, nRec, &rc); |
| 196872 | 198203 | } |
| 196873 | | - if( rc==SQLITE_OK && xOutput && sOut.nBuf>SESSIONS_STRM_CHUNK_SIZE ){ |
| 198204 | + if( rc==SQLITE_OK && xOutput && sOut.nBuf>sessions_strm_chunk_size ){ |
| 196874 | 198205 | rc = xOutput(pOut, sOut.aBuf, sOut.nBuf); |
| 196875 | 198206 | sOut.nBuf = 0; |
| 196876 | 198207 | } |
| 196877 | 198208 | if( rc ) break; |
| 196878 | 198209 | } |
| | @@ -196978,10 +198309,31 @@ |
| 196978 | 198309 | if( p ){ |
| 196979 | 198310 | sessionDeleteTable(p->grp.pList); |
| 196980 | 198311 | sqlite3_free(p); |
| 196981 | 198312 | } |
| 196982 | 198313 | } |
| 198314 | + |
| 198315 | +/* |
| 198316 | +** Global configuration |
| 198317 | +*/ |
| 198318 | +SQLITE_API int sqlite3session_config(int op, void *pArg){ |
| 198319 | + int rc = SQLITE_OK; |
| 198320 | + switch( op ){ |
| 198321 | + case SQLITE_SESSION_CONFIG_STRMSIZE: { |
| 198322 | + int *pInt = (int*)pArg; |
| 198323 | + if( *pInt>0 ){ |
| 198324 | + sessions_strm_chunk_size = *pInt; |
| 198325 | + } |
| 198326 | + *pInt = sessions_strm_chunk_size; |
| 198327 | + break; |
| 198328 | + } |
| 198329 | + default: |
| 198330 | + rc = SQLITE_MISUSE; |
| 198331 | + break; |
| 198332 | + } |
| 198333 | + return rc; |
| 198334 | +} |
| 196983 | 198335 | |
| 196984 | 198336 | #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */ |
| 196985 | 198337 | |
| 196986 | 198338 | /************** End of sqlite3session.c **************************************/ |
| 196987 | 198339 | /************** Begin file fts5.c ********************************************/ |
| | @@ -198413,10 +199765,11 @@ |
| 198413 | 199765 | ** |
| 198414 | 199766 | ** The following is the concatenation of all %include directives from the |
| 198415 | 199767 | ** input grammar file: |
| 198416 | 199768 | */ |
| 198417 | 199769 | /* #include <stdio.h> */ |
| 199770 | +/* #include <assert.h> */ |
| 198418 | 199771 | /************ Begin %include sections from the grammar ************************/ |
| 198419 | 199772 | |
| 198420 | 199773 | /* #include "fts5Int.h" */ |
| 198421 | 199774 | /* #include "fts5parse.h" */ |
| 198422 | 199775 | |
| | @@ -199740,14 +201093,13 @@ |
| 199740 | 201093 | #endif |
| 199741 | 201094 | fts5yy_destructor(fts5yypParser, (fts5YYCODETYPE)fts5yymajor, &fts5yyminorunion); |
| 199742 | 201095 | fts5yymajor = fts5YYNOCODE; |
| 199743 | 201096 | }else{ |
| 199744 | 201097 | while( fts5yypParser->fts5yytos >= fts5yypParser->fts5yystack |
| 199745 | | - && fts5yymx != fts5YYERRORSYMBOL |
| 199746 | 201098 | && (fts5yyact = fts5yy_find_reduce_action( |
| 199747 | 201099 | fts5yypParser->fts5yytos->stateno, |
| 199748 | | - fts5YYERRORSYMBOL)) >= fts5YY_MIN_REDUCE |
| 201100 | + fts5YYERRORSYMBOL)) > fts5YY_MAX_SHIFTREDUCE |
| 199749 | 201101 | ){ |
| 199750 | 201102 | fts5yy_pop_parser_stack(fts5yypParser); |
| 199751 | 201103 | } |
| 199752 | 201104 | if( fts5yypParser->fts5yytos < fts5yypParser->fts5yystack || fts5yymajor==0 ){ |
| 199753 | 201105 | fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion); |
| | @@ -210693,11 +212045,11 @@ |
| 210693 | 212045 | sqlite3Fts5IterClose((Fts5IndexIter*)pRet); |
| 210694 | 212046 | pRet = 0; |
| 210695 | 212047 | fts5CloseReader(p); |
| 210696 | 212048 | } |
| 210697 | 212049 | |
| 210698 | | - *ppIter = &pRet->base; |
| 212050 | + *ppIter = (Fts5IndexIter*)pRet; |
| 210699 | 212051 | sqlite3Fts5BufferFree(&buf); |
| 210700 | 212052 | } |
| 210701 | 212053 | return fts5IndexReturn(p); |
| 210702 | 212054 | } |
| 210703 | 212055 | |
| | @@ -214442,16 +215794,31 @@ |
| 214442 | 215794 | int nArg, /* Number of args */ |
| 214443 | 215795 | sqlite3_value **apUnused /* Function arguments */ |
| 214444 | 215796 | ){ |
| 214445 | 215797 | assert( nArg==0 ); |
| 214446 | 215798 | UNUSED_PARAM2(nArg, apUnused); |
| 214447 | | - sqlite3_result_text(pCtx, "fts5: 2018-09-25 19:08:10 fb90e7189ae6d62e77ba3a308ca5d683f90bbe633cf681865365b8e92792d1c7", -1, SQLITE_TRANSIENT); |
| 215799 | + sqlite3_result_text(pCtx, "fts5: 2018-12-01 12:34:55 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238b4f9", -1, SQLITE_TRANSIENT); |
| 215800 | +} |
| 215801 | + |
| 215802 | +/* |
| 215803 | +** Return true if zName is the extension on one of the shadow tables used |
| 215804 | +** by this module. |
| 215805 | +*/ |
| 215806 | +static int fts5ShadowName(const char *zName){ |
| 215807 | + static const char *azName[] = { |
| 215808 | + "config", "content", "data", "docsize", "idx" |
| 215809 | + }; |
| 215810 | + unsigned int i; |
| 215811 | + for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){ |
| 215812 | + if( sqlite3_stricmp(zName, azName[i])==0 ) return 1; |
| 215813 | + } |
| 215814 | + return 0; |
| 214448 | 215815 | } |
| 214449 | 215816 | |
| 214450 | 215817 | static int fts5Init(sqlite3 *db){ |
| 214451 | 215818 | static const sqlite3_module fts5Mod = { |
| 214452 | | - /* iVersion */ 2, |
| 215819 | + /* iVersion */ 3, |
| 214453 | 215820 | /* xCreate */ fts5CreateMethod, |
| 214454 | 215821 | /* xConnect */ fts5ConnectMethod, |
| 214455 | 215822 | /* xBestIndex */ fts5BestIndexMethod, |
| 214456 | 215823 | /* xDisconnect */ fts5DisconnectMethod, |
| 214457 | 215824 | /* xDestroy */ fts5DestroyMethod, |
| | @@ -214470,10 +215837,11 @@ |
| 214470 | 215837 | /* xFindFunction */ fts5FindFunctionMethod, |
| 214471 | 215838 | /* xRename */ fts5RenameMethod, |
| 214472 | 215839 | /* xSavepoint */ fts5SavepointMethod, |
| 214473 | 215840 | /* xRelease */ fts5ReleaseMethod, |
| 214474 | 215841 | /* xRollbackTo */ fts5RollbackToMethod, |
| 215842 | + /* xShadowName */ fts5ShadowName |
| 214475 | 215843 | }; |
| 214476 | 215844 | |
| 214477 | 215845 | int rc; |
| 214478 | 215846 | Fts5Global *pGlobal = 0; |
| 214479 | 215847 | |
| | @@ -218514,20 +219882,22 @@ |
| 218514 | 219882 | int rc = SQLITE_OK; |
| 218515 | 219883 | Fts5IndexIter *pIter = pCsr->pIter; |
| 218516 | 219884 | i64 *pp = &pCsr->iInstPos; |
| 218517 | 219885 | int *po = &pCsr->iInstOff; |
| 218518 | 219886 | |
| 219887 | + assert( sqlite3Fts5IterEof(pIter)==0 ); |
| 219888 | + assert( pCsr->bEof==0 ); |
| 218519 | 219889 | while( eDetail==FTS5_DETAIL_NONE |
| 218520 | 219890 | || sqlite3Fts5PoslistNext64(pIter->pData, pIter->nData, po, pp) |
| 218521 | 219891 | ){ |
| 218522 | 219892 | pCsr->iInstPos = 0; |
| 218523 | 219893 | pCsr->iInstOff = 0; |
| 218524 | 219894 | |
| 218525 | 219895 | rc = sqlite3Fts5IterNextScan(pCsr->pIter); |
| 218526 | 219896 | if( rc==SQLITE_OK ){ |
| 218527 | 219897 | rc = fts5VocabInstanceNewTerm(pCsr); |
| 218528 | | - if( eDetail==FTS5_DETAIL_NONE ) break; |
| 219898 | + if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE ) break; |
| 218529 | 219899 | } |
| 218530 | 219900 | if( rc ){ |
| 218531 | 219901 | pCsr->bEof = 1; |
| 218532 | 219902 | break; |
| 218533 | 219903 | } |
| | @@ -218838,17 +220208,16 @@ |
| 218838 | 220208 | /* xFindFunction */ 0, |
| 218839 | 220209 | /* xRename */ 0, |
| 218840 | 220210 | /* xSavepoint */ 0, |
| 218841 | 220211 | /* xRelease */ 0, |
| 218842 | 220212 | /* xRollbackTo */ 0, |
| 220213 | + /* xShadowName */ 0 |
| 218843 | 220214 | }; |
| 218844 | 220215 | void *p = (void*)pGlobal; |
| 218845 | 220216 | |
| 218846 | 220217 | return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0); |
| 218847 | 220218 | } |
| 218848 | | - |
| 218849 | | - |
| 218850 | 220219 | |
| 218851 | 220220 | |
| 218852 | 220221 | |
| 218853 | 220222 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */ |
| 218854 | 220223 | |
| | @@ -219120,10 +220489,11 @@ |
| 219120 | 220489 | 0, /* xFindMethod */ |
| 219121 | 220490 | 0, /* xRename */ |
| 219122 | 220491 | 0, /* xSavepoint */ |
| 219123 | 220492 | 0, /* xRelease */ |
| 219124 | 220493 | 0, /* xRollbackTo */ |
| 220494 | + 0, /* xShadowName */ |
| 219125 | 220495 | }; |
| 219126 | 220496 | |
| 219127 | 220497 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 219128 | 220498 | |
| 219129 | 220499 | SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3 *db){ |
| | @@ -219152,12 +220522,12 @@ |
| 219152 | 220522 | } |
| 219153 | 220523 | #endif /* SQLITE_CORE */ |
| 219154 | 220524 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */ |
| 219155 | 220525 | |
| 219156 | 220526 | /************** End of stmt.c ************************************************/ |
| 219157 | | -#if __LINE__!=219157 |
| 220527 | +#if __LINE__!=220527 |
| 219158 | 220528 | #undef SQLITE_SOURCE_ID |
| 219159 | | -#define SQLITE_SOURCE_ID "2018-09-25 19:08:10 fb90e7189ae6d62e77ba3a308ca5d683f90bbe633cf681865365b8e92792alt2" |
| 220529 | +#define SQLITE_SOURCE_ID "2018-12-01 12:34:55 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238alt2" |
| 219160 | 220530 | #endif |
| 219161 | 220531 | /* Return the source-id for this library */ |
| 219162 | 220532 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 219163 | 220533 | /************************** End of sqlite3.c ******************************/ |
| 219164 | 220534 | |