sqllogictest
Check-in [7ecd9f43d7]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Update the built-in SQLite to the latest 3.26.0 beta.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:7ecd9f43d7ed057b7d59999753b37ce819575331
User & Date: drh 2018-11-27 22:13:02
Context
2019-02-06
16:28
Update the built-in SQLite to the latest 3.27.0 beta. check-in: 40b4bc94f8 user: drh tags: trunk
2018-11-27
22:13
Update the built-in SQLite to the latest 3.26.0 beta. check-in: 7ecd9f43d7 user: drh tags: trunk
2018-09-12
14:31
Update the built-in SQLite to the first 3.25.0 release candidate. check-in: 0756e950c1 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.25.0.  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.
     9      9   **
    10     10   ** This file is all you need to compile SQLite.  To use SQLite in other
................................................................................
   255    255     "ENABLE_FTS3_TOKENIZER",
   256    256   #endif
   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
   267    270     "ENABLE_ICU",
   268    271   #endif
................................................................................
   285    288     "ENABLE_MEMSYS3",
   286    289   #endif
   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
   297    303     "ENABLE_OVERSIZE_CELL_CHECK",
   298    304   #endif
................................................................................
  1152   1158   ** been edited in any way since it was last checked in, then the last
  1153   1159   ** four hexadecimal digits of the hash may be modified.
  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.0"
  1160         -#define SQLITE_VERSION_NUMBER 3025000
  1161         -#define SQLITE_SOURCE_ID      "2018-09-12 08:51:48 572de7e4e33562c72cd90790b267ba389370f21ddcaebc4db609fd76ae9b7ada"
         1165  +#define SQLITE_VERSION        "3.26.0"
         1166  +#define SQLITE_VERSION_NUMBER 3026000
         1167  +#define SQLITE_SOURCE_ID      "2018-11-27 19:47:55 0ea049f342d11c676e148239e45d252164081362e921a4beb735d6899eb77344"
  1162   1168   
  1163   1169   /*
  1164   1170   ** CAPI3REF: Run-Time Library Version Numbers
  1165   1171   ** KEYWORDS: sqlite3_version sqlite3_sourceid
  1166   1172   **
  1167   1173   ** These interfaces provide the same information as the [SQLITE_VERSION],
  1168   1174   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  2111   2117   **
  2112   2118   ** <li>[[SQLITE_FCNTL_DATA_VERSION]]
  2113   2119   ** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
  2114   2120   ** a database file.  The argument is a pointer to a 32-bit unsigned integer.
  2115   2121   ** The "data version" for the pager is written into the pointer.  The
  2116   2122   ** "data version" changes whenever any change occurs to the corresponding
  2117   2123   ** database file, either through SQL statements on the same database
  2118         -** connection, or through transactions committed by separate database
         2124  +** connection or through transactions committed by separate database
  2119   2125   ** connections possibly in other processes. The [sqlite3_total_changes()]
  2120   2126   ** interface can be used to find if any database on the connection has changed,
  2121         -** but that interface response to changes on TEMP as well as MAIN and does
         2127  +** but that interface responds to changes on TEMP as well as MAIN and does
  2122   2128   ** not provide a mechanism to detect changes to MAIN only.  Also, the
  2123         -** [sqlite3_total_changes()] interface response to internal changes only and
         2129  +** [sqlite3_total_changes()] interface responds to internal changes only and
  2124   2130   ** omits changes made by other database connections.  The
  2125   2131   ** [PRAGMA data_version] command provide a mechanism to detect changes to
  2126   2132   ** a single attached database that occur due to other database connections,
  2127         -** but omits changes implemented by the database connection for which it is
         2133  +** but omits changes implemented by the database connection on which it is
  2128   2134   ** called.  This file control is the only mechanism to detect changes that
  2129         -** happen either internally or externally on a single database.
         2135  +** happen either internally or externally and that are associated with
         2136  +** a particular attached database.
  2130   2137   ** </ul>
  2131   2138   */
  2132   2139   #define SQLITE_FCNTL_LOCKSTATE               1
  2133   2140   #define SQLITE_FCNTL_GET_LOCKPROXYFILE       2
  2134   2141   #define SQLITE_FCNTL_SET_LOCKPROXYFILE       3
  2135   2142   #define SQLITE_FCNTL_LAST_ERRNO              4
  2136   2143   #define SQLITE_FCNTL_SIZE_HINT               5
................................................................................
  3045   3052   ** Existing configuration options might be discontinued.  Applications
  3046   3053   ** should check the return code from [sqlite3_db_config()] to make sure that
  3047   3054   ** the call worked.  ^The [sqlite3_db_config()] interface will return a
  3048   3055   ** non-zero [error code] if a discontinued or unsupported configuration option
  3049   3056   ** is invoked.
  3050   3057   **
  3051   3058   ** <dl>
         3059  +** [[SQLITE_DBCONFIG_LOOKASIDE]]
  3052   3060   ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
  3053   3061   ** <dd> ^This option takes three additional arguments that determine the 
  3054   3062   ** [lookaside memory allocator] configuration for the [database connection].
  3055   3063   ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
  3056   3064   ** pointer to a memory buffer to use for lookaside memory.
  3057   3065   ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
  3058   3066   ** may be NULL in which case SQLite will allocate the
................................................................................
  3067   3075   ** connection is not currently using lookaside memory, or in other words
  3068   3076   ** when the "current value" returned by
  3069   3077   ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
  3070   3078   ** Any attempt to change the lookaside memory configuration when lookaside
  3071   3079   ** memory is in use leaves the configuration unchanged and returns 
  3072   3080   ** [SQLITE_BUSY].)^</dd>
  3073   3081   **
         3082  +** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
  3074   3083   ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
  3075   3084   ** <dd> ^This option is used to enable or disable the enforcement of
  3076   3085   ** [foreign key constraints].  There should be two additional arguments.
  3077   3086   ** The first argument is an integer which is 0 to disable FK enforcement,
  3078   3087   ** positive to enable FK enforcement or negative to leave FK enforcement
  3079   3088   ** unchanged.  The second parameter is a pointer to an integer into which
  3080   3089   ** is written 0 or 1 to indicate whether FK enforcement is off or on
  3081   3090   ** following this call.  The second parameter may be a NULL pointer, in
  3082   3091   ** which case the FK enforcement setting is not reported back. </dd>
  3083   3092   **
         3093  +** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
  3084   3094   ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
  3085   3095   ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
  3086   3096   ** There should be two additional arguments.
  3087   3097   ** The first argument is an integer which is 0 to disable triggers,
  3088   3098   ** positive to enable triggers or negative to leave the setting unchanged.
  3089   3099   ** The second parameter is a pointer to an integer into which
  3090   3100   ** is written 0 or 1 to indicate whether triggers are disabled or enabled
  3091   3101   ** following this call.  The second parameter may be a NULL pointer, in
  3092   3102   ** which case the trigger setting is not reported back. </dd>
  3093   3103   **
         3104  +** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
  3094   3105   ** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
  3095   3106   ** <dd> ^This option is used to enable or disable the two-argument
  3096   3107   ** version of the [fts3_tokenizer()] function which is part of the
  3097   3108   ** [FTS3] full-text search engine extension.
  3098   3109   ** There should be two additional arguments.
  3099   3110   ** The first argument is an integer which is 0 to disable fts3_tokenizer() or
  3100   3111   ** positive to enable fts3_tokenizer() or negative to leave the setting
  3101   3112   ** unchanged.
  3102   3113   ** The second parameter is a pointer to an integer into which
  3103   3114   ** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
  3104   3115   ** following this call.  The second parameter may be a NULL pointer, in
  3105   3116   ** which case the new setting is not reported back. </dd>
  3106   3117   **
         3118  +** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
  3107   3119   ** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
  3108   3120   ** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
  3109   3121   ** interface independently of the [load_extension()] SQL function.
  3110   3122   ** The [sqlite3_enable_load_extension()] API enables or disables both the
  3111   3123   ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
  3112   3124   ** There should be two additional arguments.
  3113   3125   ** When the first argument to this interface is 1, then only the C-API is
................................................................................
  3117   3129   ** C-API or the SQL function.
  3118   3130   ** The second parameter is a pointer to an integer into which
  3119   3131   ** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
  3120   3132   ** is disabled or enabled following this call.  The second parameter may
  3121   3133   ** be a NULL pointer, in which case the new setting is not reported back.
  3122   3134   ** </dd>
  3123   3135   **
  3124         -** <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
         3136  +** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
  3125   3137   ** <dd> ^This option is used to change the name of the "main" database
  3126   3138   ** schema.  ^The sole argument is a pointer to a constant UTF8 string
  3127   3139   ** which will become the new schema name in place of "main".  ^SQLite
  3128   3140   ** does not make a copy of the new main schema name string, so the application
  3129   3141   ** must ensure that the argument passed into this DBCONFIG option is unchanged
  3130   3142   ** until after the database connection closes.
  3131   3143   ** </dd>
  3132   3144   **
         3145  +** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]] 
  3133   3146   ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
  3134   3147   ** <dd> Usually, when a database in wal mode is closed or detached from a 
  3135   3148   ** database handle, SQLite checks if this will mean that there are now no 
  3136   3149   ** connections at all to the database. If so, it performs a checkpoint 
  3137   3150   ** operation before closing the connection. This option may be used to
  3138   3151   ** override this behaviour. The first parameter passed to this operation
  3139   3152   ** is an integer - positive to disable checkpoints-on-close, or zero (the
  3140   3153   ** default) to enable them, and negative to leave the setting unchanged.
  3141   3154   ** The second parameter is a pointer to an integer
  3142   3155   ** into which is written 0 or 1 to indicate whether checkpoints-on-close
  3143   3156   ** have been disabled - 0 if they are not disabled, 1 if they are.
  3144   3157   ** </dd>
  3145   3158   **
  3146         -** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
         3159  +** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
  3147   3160   ** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
  3148   3161   ** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
  3149   3162   ** a single SQL query statement will always use the same algorithm regardless
  3150   3163   ** of values of [bound parameters].)^ The QPSG disables some query optimizations
  3151   3164   ** that look at the values of bound parameters, which can make some queries
  3152   3165   ** slower.  But the QPSG has the advantage of more predictable behavior.  With
  3153   3166   ** the QPSG active, SQLite will always use the same query plan in the field as
................................................................................
  3155   3168   ** The first argument to this setting is an integer which is 0 to disable 
  3156   3169   ** the QPSG, positive to enable QPSG, or negative to leave the setting
  3157   3170   ** unchanged. The second parameter is a pointer to an integer into which
  3158   3171   ** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
  3159   3172   ** following this call.
  3160   3173   ** </dd>
  3161   3174   **
  3162         -** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
         3175  +** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
  3163   3176   ** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
  3164   3177   ** include output for any operations performed by trigger programs. This
  3165   3178   ** option is used to set or clear (the default) a flag that governs this
  3166   3179   ** behavior. The first parameter passed to this operation is an integer -
  3167   3180   ** positive to enable output for trigger programs, or zero to disable it,
  3168   3181   ** or negative to leave the setting unchanged.
  3169   3182   ** The second parameter is a pointer to an integer into which is written 
  3170   3183   ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
  3171   3184   ** it is not disabled, 1 if it is.  
  3172   3185   ** </dd>
  3173   3186   **
  3174         -** <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
         3187  +** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
  3175   3188   ** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
  3176   3189   ** [VACUUM] in order to reset a database back to an empty database
  3177   3190   ** with no schema and no content. The following process works even for
  3178   3191   ** a badly corrupted database file:
  3179   3192   ** <ol>
  3180   3193   ** <li> If the database connection is newly opened, make sure it has read the
  3181   3194   **      database schema by preparing then discarding some query against the
................................................................................
  3186   3199   ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
  3187   3200   ** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
  3188   3201   ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
  3189   3202   ** </ol>
  3190   3203   ** Because resetting a database is destructive and irreversible, the
  3191   3204   ** process requires the use of this obscure API and multiple steps to help
  3192   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>
  3193   3218   ** </dd>
  3194   3219   ** </dl>
  3195   3220   */
  3196   3221   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
  3197   3222   #define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
  3198   3223   #define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
  3199   3224   #define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
  3200   3225   #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
  3201   3226   #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
  3202   3227   #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
  3203   3228   #define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
  3204   3229   #define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
  3205   3230   #define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
  3206         -#define SQLITE_DBCONFIG_MAX                   1009 /* Largest DBCONFIG */
         3231  +#define SQLITE_DBCONFIG_DEFENSIVE             1010 /* int int* */
         3232  +#define SQLITE_DBCONFIG_MAX                   1010 /* Largest DBCONFIG */
  3207   3233   
  3208   3234   /*
  3209   3235   ** CAPI3REF: Enable Or Disable Extended Result Codes
  3210   3236   ** METHOD: sqlite3
  3211   3237   **
  3212   3238   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  3213   3239   ** [extended result codes] feature of SQLite. ^The extended result
................................................................................
  4637   4663   ** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
  4638   4664   ** and [sqlite3_prepare16_v3()] assume that the prepared statement will 
  4639   4665   ** be used just once or at most a few times and then destroyed using
  4640   4666   ** [sqlite3_finalize()] relatively soon. The current implementation acts
  4641   4667   ** on this hint by avoiding the use of [lookaside memory] so as not to
  4642   4668   ** deplete the limited store of lookaside memory. Future versions of
  4643   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.
  4644   4679   ** </dl>
  4645   4680   */
  4646   4681   #define SQLITE_PREPARE_PERSISTENT              0x01
         4682  +#define SQLITE_PREPARE_NORMALIZE               0x02
  4647   4683   
  4648   4684   /*
  4649   4685   ** CAPI3REF: Compiling An SQL Statement
  4650   4686   ** KEYWORDS: {SQL statement compiler}
  4651   4687   ** METHOD: sqlite3
  4652   4688   ** CONSTRUCTOR: sqlite3_stmt
  4653   4689   **
................................................................................
  4797   4833   ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
  4798   4834   ** SQL text used to create [prepared statement] P if P was
  4799   4835   ** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
  4800   4836   ** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
  4801   4837   ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
  4802   4838   ** string containing the SQL text of prepared statement P with
  4803   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.
  4804   4845   **
  4805   4846   ** ^(For example, if a prepared statement is created using the SQL
  4806   4847   ** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
  4807   4848   ** and parameter :xyz is unbound, then sqlite3_sql() will return
  4808   4849   ** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
  4809   4850   ** will return "SELECT 2345,NULL".)^
  4810   4851   **
................................................................................
  4812   4853   ** is available to hold the result, or if the result would exceed the
  4813   4854   ** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
  4814   4855   **
  4815   4856   ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
  4816   4857   ** bound parameter expansions.  ^The [SQLITE_OMIT_TRACE] compile-time
  4817   4858   ** option causes sqlite3_expanded_sql() to always return NULL.
  4818   4859   **
  4819         -** ^The string returned by sqlite3_sql(P) is managed by SQLite and is
  4820         -** 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.
  4821   4863   ** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
  4822   4864   ** is obtained from [sqlite3_malloc()] and must be free by the application
  4823   4865   ** by passing it to [sqlite3_free()].
  4824   4866   */
  4825   4867   SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
  4826   4868   SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
         4869  +SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
  4827   4870   
  4828   4871   /*
  4829   4872   ** CAPI3REF: Determine If An SQL Statement Writes The Database
  4830   4873   ** METHOD: sqlite3_stmt
  4831   4874   **
  4832   4875   ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  4833   4876   ** and only if the [prepared statement] X makes no direct changes to
................................................................................
  5760   5803   ** parameters. ^An aggregate SQL function requires an implementation of xStep
  5761   5804   ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
  5762   5805   ** SQL function or aggregate, pass NULL pointers for all three function
  5763   5806   ** callbacks.
  5764   5807   **
  5765   5808   ** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue 
  5766   5809   ** and xInverse) passed to sqlite3_create_window_function are pointers to
  5767         -** C-lanugage callbacks that implement the new function. xStep and xFinal
         5810  +** C-language callbacks that implement the new function. xStep and xFinal
  5768   5811   ** must both be non-NULL. xValue and xInverse may either both be NULL, in
  5769   5812   ** which case a regular aggregate function is created, or must both be 
  5770   5813   ** non-NULL, in which case the new function may be used as either an aggregate
  5771   5814   ** or aggregate window function. More details regarding the implementation
  5772   5815   ** of aggregate window functions are 
  5773   5816   ** [user-defined window functions|available here].
  5774   5817   **
................................................................................
  7309   7352                          void **ppArg);
  7310   7353     int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
  7311   7354     /* The methods above are in version 1 of the sqlite_module object. Those 
  7312   7355     ** below are for version 2 and greater. */
  7313   7356     int (*xSavepoint)(sqlite3_vtab *pVTab, int);
  7314   7357     int (*xRelease)(sqlite3_vtab *pVTab, int);
  7315   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*);
  7316   7362   };
  7317   7363   
  7318   7364   /*
  7319   7365   ** CAPI3REF: Virtual Table Indexing Information
  7320   7366   ** KEYWORDS: sqlite3_index_info
  7321   7367   **
  7322   7368   ** The sqlite3_index_info structure and its substructures is used as part
................................................................................
  8231   8277   #define SQLITE_TESTCTRL_PENDING_BYTE            11
  8232   8278   #define SQLITE_TESTCTRL_ASSERT                  12
  8233   8279   #define SQLITE_TESTCTRL_ALWAYS                  13
  8234   8280   #define SQLITE_TESTCTRL_RESERVE                 14
  8235   8281   #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  8236   8282   #define SQLITE_TESTCTRL_ISKEYWORD               16  /* NOT USED */
  8237   8283   #define SQLITE_TESTCTRL_SCRATCHMALLOC           17  /* NOT USED */
         8284  +#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS      17
  8238   8285   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  8239   8286   #define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
  8240   8287   #define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
  8241   8288   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  8242   8289   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  8243   8290   #define SQLITE_TESTCTRL_BYTEORDER               22
  8244   8291   #define SQLITE_TESTCTRL_ISINIT                  23
................................................................................
  9643   9690   ** CAPI3REF: Virtual Table Configuration Options
  9644   9691   **
  9645   9692   ** These macros define the various options to the
  9646   9693   ** [sqlite3_vtab_config()] interface that [virtual table] implementations
  9647   9694   ** can use to customize and optimize their behavior.
  9648   9695   **
  9649   9696   ** <dl>
         9697  +** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
  9650   9698   ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
  9651   9699   ** <dd>Calls of the form
  9652   9700   ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
  9653   9701   ** where X is an integer.  If X is zero, then the [virtual table] whose
  9654   9702   ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
  9655   9703   ** support constraints.  In this configuration (which is the default) if
  9656   9704   ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
................................................................................
 10286  10334   ** The following are allowed values for 6th argument (the F argument) to
 10287  10335   ** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
 10288  10336   **
 10289  10337   ** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
 10290  10338   ** in the P argument is held in memory obtained from [sqlite3_malloc64()]
 10291  10339   ** and that SQLite should take ownership of this memory and automatically
 10292  10340   ** free it when it has finished using it.  Without this flag, the caller
 10293         -** is resposible for freeing any dynamically allocated memory.
        10341  +** is responsible for freeing any dynamically allocated memory.
 10294  10342   **
 10295  10343   ** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
 10296  10344   ** grow the size of the database using calls to [sqlite3_realloc64()].  This
 10297  10345   ** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
 10298  10346   ** Without this flag, the deserialized database cannot increase in size beyond
 10299  10347   ** the number of bytes specified by the M parameter.
 10300  10348   **
................................................................................
 10908  10956   ** [sqlite3changeset_invert()] functions, all changes within the changeset 
 10909  10957   ** that apply to a single table are grouped together. This means that when 
 10910  10958   ** an application iterates through a changeset using an iterator created by 
 10911  10959   ** this function, all changes that relate to a single table are visited 
 10912  10960   ** consecutively. There is no chance that the iterator will visit a change 
 10913  10961   ** the applies to table X, then one for table Y, and then later on visit 
 10914  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.
 10915  10970   */
 10916  10971   SQLITE_API int sqlite3changeset_start(
 10917  10972     sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
 10918  10973     int nChangeset,                 /* Size of changeset blob in bytes */
 10919  10974     void *pChangeset                /* Pointer to blob containing changeset */
 10920  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
 10921  10995   
 10922  10996   
 10923  10997   /*
 10924  10998   ** CAPI3REF: Advance A Changeset Iterator
 10925  10999   ** METHOD: sqlite3_changeset_iter
 10926  11000   **
 10927  11001   ** This function may only be used with iterators created by function
................................................................................
 11568  11642     int(*xConflict)(
 11569  11643       void *pCtx,                   /* Copy of sixth arg to _apply() */
 11570  11644       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 11571  11645       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 11572  11646     ),
 11573  11647     void *pCtx,                     /* First argument passed to xConflict */
 11574  11648     void **ppRebase, int *pnRebase, /* OUT: Rebase data */
 11575         -  int flags                       /* Combination of SESSION_APPLY_* flags */
        11649  +  int flags                       /* SESSION_CHANGESETAPPLY_* flags */
 11576  11650   );
 11577  11651   
 11578  11652   /*
 11579  11653   ** CAPI3REF: Flags for sqlite3changeset_apply_v2
 11580  11654   **
 11581  11655   ** The following flags may passed via the 9th parameter to
 11582  11656   ** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
................................................................................
 11586  11660   **   Usually, the sessions module encloses all operations performed by
 11587  11661   **   a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
 11588  11662   **   SAVEPOINT is committed if the changeset or patchset is successfully
 11589  11663   **   applied, or rolled back if an error occurs. Specifying this flag
 11590  11664   **   causes the sessions module to omit this savepoint. In this case, if the
 11591  11665   **   caller has an open transaction or savepoint when apply_v2() is called, 
 11592  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.
 11593  11672   */
 11594  11673   #define SQLITE_CHANGESETAPPLY_NOSAVEPOINT   0x0001
        11674  +#define SQLITE_CHANGESETAPPLY_INVERT        0x0002
 11595  11675   
 11596  11676   /* 
 11597  11677   ** CAPI3REF: Constants Passed To The Conflict Handler
 11598  11678   **
 11599  11679   ** Values that may be passed as the second argument to a conflict-handler.
 11600  11680   **
 11601  11681   ** <dl>
................................................................................
 11980  12060     int (*xOutput)(void *pOut, const void *pData, int nData),
 11981  12061     void *pOut
 11982  12062   );
 11983  12063   SQLITE_API int sqlite3changeset_start_strm(
 11984  12064     sqlite3_changeset_iter **pp,
 11985  12065     int (*xInput)(void *pIn, void *pData, int *pnData),
 11986  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
 11987  12073   );
 11988  12074   SQLITE_API int sqlite3session_changeset_strm(
 11989  12075     sqlite3_session *pSession,
 11990  12076     int (*xOutput)(void *pOut, const void *pData, int nData),
 11991  12077     void *pOut
 11992  12078   );
 11993  12079   SQLITE_API int sqlite3session_patchset_strm(
................................................................................
 12007  12093     sqlite3_rebaser *pRebaser,
 12008  12094     int (*xInput)(void *pIn, void *pData, int *pnData),
 12009  12095     void *pIn,
 12010  12096     int (*xOutput)(void *pOut, const void *pData, int nData),
 12011  12097     void *pOut
 12012  12098   );
 12013  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
 12014  12139   
 12015  12140   /*
 12016  12141   ** Make sure we can call this stuff from C++.
 12017  12142   */
 12018  12143   #if 0
 12019  12144   }
 12020  12145   #endif
................................................................................
 12464  12589   **            document such as "I won first place" is tokenized, entries are
 12465  12590   **            added to the FTS index for "i", "won", "first", "1st" and
 12466  12591   **            "place".
 12467  12592   **
 12468  12593   **            This way, even if the tokenizer does not provide synonyms
 12469  12594   **            when tokenizing query text (it should not - to do would be
 12470  12595   **            inefficient), it doesn't matter if the user queries for 
 12471         -**            'first + place' or '1st + place', as there are entires in the
        12596  +**            'first + place' or '1st + place', as there are entries in the
 12472  12597   **            FTS index corresponding to both forms of the first token.
 12473  12598   **   </ol>
 12474  12599   **
 12475  12600   **   Whether it is parsing document or query text, any call to xToken that
 12476  12601   **   specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
 12477  12602   **   is considered to supply a synonym for the previous token. For example,
 12478  12603   **   when parsing the document "I won first place", a tokenizer that supports
................................................................................
 12492  12617   **   There is no limit to the number of synonyms that may be provided for a
 12493  12618   **   single token.
 12494  12619   **
 12495  12620   **   In many cases, method (1) above is the best approach. It does not add 
 12496  12621   **   extra data to the FTS index or require FTS5 to query for multiple terms,
 12497  12622   **   so it is efficient in terms of disk space and query speed. However, it
 12498  12623   **   does not support prefix queries very well. If, as suggested above, the
 12499         -**   token "first" is subsituted for "1st" by the tokenizer, then the query:
        12624  +**   token "first" is substituted for "1st" by the tokenizer, then the query:
 12500  12625   **
 12501  12626   **   <codeblock>
 12502  12627   **     ... MATCH '1s*'</codeblock>
 12503  12628   **
 12504  12629   **   will not match documents that contain the token "1st" (as the tokenizer
 12505  12630   **   will probably not map "1s" to any prefix of "first").
 12506  12631   **
................................................................................
 15272  15397   
 15273  15398   #ifndef SQLITE_OMIT_WAL
 15274  15399   SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
 15275  15400   SQLITE_PRIVATE   int sqlite3PagerWalSupported(Pager *pPager);
 15276  15401   SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
 15277  15402   SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
 15278  15403   SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
 15279         -# ifdef SQLITE_DIRECT_OVERFLOW_READ
 15280         -SQLITE_PRIVATE   int sqlite3PagerUseWal(Pager *pPager, Pgno);
 15281         -# endif
 15282  15404   # ifdef SQLITE_ENABLE_SNAPSHOT
 15283  15405   SQLITE_PRIVATE   int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
 15284  15406   SQLITE_PRIVATE   int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
 15285  15407   SQLITE_PRIVATE   int sqlite3PagerSnapshotRecover(Pager *pPager);
 15286  15408   SQLITE_PRIVATE   int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
 15287  15409   SQLITE_PRIVATE   void sqlite3PagerSnapshotUnlock(Pager *pPager);
 15288  15410   # endif
 15289         -#else
 15290         -# define sqlite3PagerUseWal(x,y) 0
        15411  +#endif
        15412  +
        15413  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
        15414  +SQLITE_PRIVATE   int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
 15291  15415   #endif
 15292  15416   
 15293  15417   #ifdef SQLITE_ENABLE_ZIPVFS
 15294  15418   SQLITE_PRIVATE   int sqlite3PagerWalFramesize(Pager *pPager);
 15295  15419   #endif
 15296  15420   
 15297  15421   /* Functions used to query pager state and configuration. */
................................................................................
 15527  15651   
 15528  15652   /* Return the header size */
 15529  15653   SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
 15530  15654   SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
 15531  15655   
 15532  15656   /* Number of dirty pages as a percentage of the configured cache size */
 15533  15657   SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*);
        15658  +
        15659  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
        15660  +SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache);
        15661  +#endif
 15534  15662   
 15535  15663   #endif /* _PCACHE_H_ */
 15536  15664   
 15537  15665   /************** End of pcache.h **********************************************/
 15538  15666   /************** Continuing where we left off in sqliteInt.h ******************/
 15539  15667   /************** Include os.h in the middle of sqliteInt.h ********************/
 15540  15668   /************** Begin file os.h **********************************************/
................................................................................
 16033  16161   };
 16034  16162   
 16035  16163   /*
 16036  16164   ** A hash table for built-in function definitions.  (Application-defined
 16037  16165   ** functions use a regular table table from hash.h.)
 16038  16166   **
 16039  16167   ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
 16040         -** 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.
 16041  16170   */
 16042  16171   #define SQLITE_FUNC_HASH_SZ 23
 16043  16172   struct FuncDefHash {
 16044  16173     FuncDef *a[SQLITE_FUNC_HASH_SZ];       /* Hash table for functions */
 16045  16174   };
        16175  +#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
 16046  16176   
 16047  16177   #ifdef SQLITE_USER_AUTHENTICATION
 16048  16178   /*
 16049  16179   ** Information held in the "sqlite3" database connection object and used
 16050  16180   ** to manage user authentication.
 16051  16181   */
 16052  16182   typedef struct sqlite3_userauth sqlite3_userauth;
................................................................................
 16099  16229     sqlite3_vfs *pVfs;            /* OS Interface */
 16100  16230     struct Vdbe *pVdbe;           /* List of active virtual machines */
 16101  16231     CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
 16102  16232     sqlite3_mutex *mutex;         /* Connection mutex */
 16103  16233     Db *aDb;                      /* All backends */
 16104  16234     int nDb;                      /* Number of backends currently in use */
 16105  16235     u32 mDbFlags;                 /* flags recording internal state */
 16106         -  u32 flags;                    /* flags settable by pragmas. See below */
        16236  +  u64 flags;                    /* flags settable by pragmas. See below */
 16107  16237     i64 lastRowid;                /* ROWID of most recent insert (see above) */
 16108  16238     i64 szMmap;                   /* Default mmap_size setting */
 16109  16239     u32 nSchemaLock;              /* Do not reset the schema when non-zero */
 16110  16240     unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
 16111  16241     int errCode;                  /* Most recent error code (SQLITE_*) */
 16112  16242     int errMask;                  /* & result codes with this before returning */
 16113  16243     int iSysErrno;                /* Errno value from last system error */
................................................................................
 16264  16394   #define SQLITE_DeferFKs       0x00080000  /* Defer all FK constraints */
 16265  16395   #define SQLITE_QueryOnly      0x00100000  /* Disable database changes */
 16266  16396   #define SQLITE_CellSizeCk     0x00200000  /* Check btree cell sizes on load */
 16267  16397   #define SQLITE_Fts3Tokenizer  0x00400000  /* Enable fts3_tokenizer(2) */
 16268  16398   #define SQLITE_EnableQPSG     0x00800000  /* Query Planner Stability Guarantee*/
 16269  16399   #define SQLITE_TriggerEQP     0x01000000  /* Show trigger EXPLAIN QUERY PLAN */
 16270  16400   #define SQLITE_ResetDatabase  0x02000000  /* Reset the database */
        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 */
 16271  16404   
 16272  16405   /* Flags used only if debugging */
        16406  +#define HI(X)  ((u64)(X)<<32)
 16273  16407   #ifdef SQLITE_DEBUG
 16274         -#define SQLITE_SqlTrace       0x08000000  /* Debug print SQL as it executes */
 16275         -#define SQLITE_VdbeListing    0x10000000  /* Debug listings of VDBE programs */
 16276         -#define SQLITE_VdbeTrace      0x20000000  /* True to trace VDBE execution */
 16277         -#define SQLITE_VdbeAddopTrace 0x40000000  /* Trace sqlite3VdbeAddOp() calls */
 16278         -#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 */
 16279  16413   #endif
 16280  16414   
 16281  16415   /*
 16282  16416   ** Allowed values for sqlite3.mDbFlags
 16283  16417   */
 16284  16418   #define DBFLAG_SchemaChange   0x0001  /* Uncommitted Hash table changes */
 16285  16419   #define DBFLAG_PreferBuiltin  0x0002  /* Preference to built-in funcs */
................................................................................
 16405  16539   #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
 16406  16540   #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
 16407  16541   #define SQLITE_FUNC_MINMAX   0x1000 /* True for min() and max() aggregates */
 16408  16542   #define SQLITE_FUNC_SLOCHNG  0x2000 /* "Slow Change". Value constant during a
 16409  16543                                       ** single query - might change over time */
 16410  16544   #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
 16411  16545   #define SQLITE_FUNC_OFFSET   0x8000 /* Built-in sqlite_offset() function */
 16412         -#define SQLITE_FUNC_WINDOW  0x10000 /* Built-in window-only function */
 16413         -#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 */
 16414  16549   
 16415  16550   /*
 16416  16551   ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
 16417  16552   ** used to create the initializers for the FuncDef structures.
 16418  16553   **
 16419  16554   **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
 16420  16555   **     Used to create a scalar function definition of a function zName
................................................................................
 16482  16617      (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
 16483  16618   #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue) \
 16484  16619     {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
 16485  16620      SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,0,#zName, {0}}
 16486  16621   #define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \
 16487  16622     {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
 16488  16623      SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xFinal,0,#zName, {0}}
 16489         -
 16490  16624   #define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \
 16491  16625     {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \
 16492  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  +
 16493  16631   
 16494  16632   /*
 16495  16633   ** All current savepoints are stored in a linked list starting at
 16496  16634   ** sqlite3.pSavepoint. The first element in the list is the most recently
 16497  16635   ** opened savepoint. Savepoints are added to the list by the vdbe
 16498  16636   ** OP_Savepoint instruction.
 16499  16637   */
................................................................................
 16670  16808   /*
 16671  16809   ** The schema for each SQL table and view is represented in memory
 16672  16810   ** by an instance of the following structure.
 16673  16811   */
 16674  16812   struct Table {
 16675  16813     char *zName;         /* Name of the table or view */
 16676  16814     Column *aCol;        /* Information about each column */
        16815  +#ifdef SQLITE_ENABLE_NORMALIZE
        16816  +  Hash *pColHash;      /* All columns indexed by name */
        16817  +#endif
 16677  16818     Index *pIndex;       /* List of SQL indexes on this table. */
 16678  16819     Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
 16679  16820     FKey *pFKey;         /* Linked list of all foreign keys in this table */
 16680  16821     char *zColAff;       /* String defining the affinity of each column */
 16681  16822     ExprList *pCheck;    /* All CHECK constraints */
 16682  16823                          /*   ... also used as column name list in a VIEW */
 16683  16824     int tnum;            /* Root BTree page for this table */
................................................................................
 16720  16861   #define TF_HasStat1        0x0010    /* nRowLogEst set from sqlite_stat1 */
 16721  16862   #define TF_WithoutRowid    0x0020    /* No rowid.  PRIMARY KEY is the key */
 16722  16863   #define TF_NoVisibleRowid  0x0040    /* No user-visible "rowid" column */
 16723  16864   #define TF_OOOHidden       0x0080    /* Out-of-Order hidden columns */
 16724  16865   #define TF_StatsUsed       0x0100    /* Query planner decisions affected by
 16725  16866                                        ** Index.aiRowLogEst[] values */
 16726  16867   #define TF_HasNotNull      0x0200    /* Contains NOT NULL constraints */
        16868  +#define TF_Shadow          0x0400    /* True for a shadow table */
 16727  16869   
 16728  16870   /*
 16729  16871   ** Test to see whether or not a table is a virtual table.  This is
 16730  16872   ** done as a macro so that it will be optimized out when virtual
 16731  16873   ** table support is omitted from the build.
 16732  16874   */
 16733  16875   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
 17006  17148     void *p;          /* Pointer to sampled record */
 17007  17149     int n;            /* Size of record in bytes */
 17008  17150     tRowcnt *anEq;    /* Est. number of rows where the key equals this sample */
 17009  17151     tRowcnt *anLt;    /* Est. number of rows where key is less than this sample */
 17010  17152     tRowcnt *anDLt;   /* Est. number of distinct keys less than this sample */
 17011  17153   };
 17012  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  +
 17013  17161   /*
 17014  17162   ** Each token coming out of the lexer is an instance of
 17015  17163   ** this structure.  Tokens are also used as part of an expression.
 17016  17164   **
 17017  17165   ** The memory that "z" points to is owned by other objects.  Take care
 17018  17166   ** that the owner of the "z" string does not deallocate the string before
 17019  17167   ** the Token goes out of scope!  Very often, the "z" points to some place
................................................................................
 17187  17335                            ** TK_SELECT_COLUMN: column of the result vector */
 17188  17336     i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
 17189  17337     i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
 17190  17338     u8 op2;                /* TK_REGISTER: original value of Expr.op
 17191  17339                            ** TK_COLUMN: the value of p5 for OP_Column
 17192  17340                            ** TK_AGG_FUNCTION: nesting depth */
 17193  17341     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
 17194         -  Table *pTab;           /* Table for TK_COLUMN expressions.  Can be NULL
 17195         -                         ** for a column of an index on an expression */
 17196         -#ifndef SQLITE_OMIT_WINDOWFUNC
 17197         -  Window *pWin;          /* Window definition for window functions */
 17198         -#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;
 17199  17347   };
 17200  17348   
 17201  17349   /*
 17202  17350   ** The following are the meanings of bits in the Expr.flags field.
 17203  17351   */
 17204  17352   #define EP_FromJoin  0x000001 /* Originates in ON/USING clause of outer join */
 17205  17353   #define EP_Agg       0x000002 /* Contains one or more aggregate functions */
................................................................................
 17221  17369   #define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
 17222  17370   #define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
 17223  17371   #define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
 17224  17372   #define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
 17225  17373   #define EP_Subquery  0x200000 /* Tree contains a TK_SELECT operator */
 17226  17374   #define EP_Alias     0x400000 /* Is an alias for a result set column */
 17227  17375   #define EP_Leaf      0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
        17376  +#define EP_WinFunc  0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
 17228  17377   
 17229  17378   /*
 17230  17379   ** The EP_Propagate mask is a set of properties that automatically propagate
 17231  17380   ** upwards into parent nodes.
 17232  17381   */
 17233  17382   #define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
 17234  17383   
................................................................................
 17906  18055   **    OPFLAG_SEEKEQ       == BTREE_SEEK_EQ
 17907  18056   **    OPFLAG_FORDELETE    == BTREE_FORDELETE
 17908  18057   **    OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
 17909  18058   **    OPFLAG_AUXDELETE    == BTREE_AUXDELETE
 17910  18059   */
 17911  18060   #define OPFLAG_NCHANGE       0x01    /* OP_Insert: Set to update db->nChange */
 17912  18061                                        /* Also used in P2 (not P5) of OP_Delete */
        18062  +#define OPFLAG_NOCHNG        0x01    /* OP_VColumn nochange for UPDATE */
 17913  18063   #define OPFLAG_EPHEM         0x01    /* OP_Column: Ephemeral output is ok */
 17914  18064   #define OPFLAG_LASTROWID     0x20    /* Set to update db->lastRowid */
 17915  18065   #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
 17916  18066   #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
 17917  18067   #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
 17918  18068   #define OPFLAG_ISNOOP        0x40    /* OP_Delete does pre-update-hook only */
 17919  18069   #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
................................................................................
 18124  18274     void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx);  /* Callback */
 18125  18275     void *pVdbeBranchArg;                                     /* 1st argument */
 18126  18276   #endif
 18127  18277   #ifndef SQLITE_UNTESTABLE
 18128  18278     int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
 18129  18279   #endif
 18130  18280     int bLocaltimeFault;              /* True to fail localtime() calls */
        18281  +  int bInternalFunctions;           /* Internal SQL functions are visible */
 18131  18282     int iOnceResetThreshold;          /* When to reset OP_Once counters */
 18132  18283     u32 szSorterRef;                  /* Min size in bytes to use sorter-refs */
 18133  18284   };
 18134  18285   
 18135  18286   /*
 18136  18287   ** This macro is used inside of assert() statements to indicate that
 18137  18288   ** the assert is only valid on a well-formed database.  Instead of:
................................................................................
 18377  18528   SQLITE_PRIVATE int sqlite3IsIdChar(u8);
 18378  18529   
 18379  18530   /*
 18380  18531   ** Internal function prototypes
 18381  18532   */
 18382  18533   SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*);
 18383  18534   SQLITE_PRIVATE int sqlite3Strlen30(const char*);
        18535  +#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff)
 18384  18536   SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*);
 18385  18537   #define sqlite3StrNICmp sqlite3_strnicmp
 18386  18538   
 18387  18539   SQLITE_PRIVATE int sqlite3MallocInit(void);
 18388  18540   SQLITE_PRIVATE void sqlite3MallocEnd(void);
 18389  18541   SQLITE_PRIVATE void *sqlite3Malloc(u64);
 18390  18542   SQLITE_PRIVATE void *sqlite3MallocZero(u64);
................................................................................
 18493  18645   SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
 18494  18646   #endif
 18495  18647   
 18496  18648   #if defined(SQLITE_DEBUG)
 18497  18649   SQLITE_PRIVATE   void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
 18498  18650   SQLITE_PRIVATE   void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
 18499  18651   SQLITE_PRIVATE   void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
        18652  +SQLITE_PRIVATE   void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
 18500  18653   SQLITE_PRIVATE   void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
 18501  18654   SQLITE_PRIVATE   void sqlite3TreeViewWith(TreeView*, const With*, u8);
 18502  18655   #ifndef SQLITE_OMIT_WINDOWFUNC
 18503  18656   SQLITE_PRIVATE   void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
 18504  18657   SQLITE_PRIVATE   void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
 18505  18658   #endif
 18506  18659   #endif
................................................................................
 18725  18878   #ifdef SQLITE_ENABLE_CURSOR_HINTS
 18726  18879   SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
 18727  18880   #endif
 18728  18881   SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
 18729  18882   SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
 18730  18883   SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
 18731  18884   SQLITE_PRIVATE int sqlite3IsRowid(const char*);
        18885  +#ifdef SQLITE_ENABLE_NORMALIZE
        18886  +SQLITE_PRIVATE int sqlite3IsRowidN(const char*, int);
        18887  +#endif
 18732  18888   SQLITE_PRIVATE void sqlite3GenerateRowDelete(
 18733  18889       Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
 18734  18890   SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
 18735  18891   SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
 18736  18892   SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
        18893  +SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
 18737  18894   SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
 18738  18895                                        u8,u8,int,int*,int*,Upsert*);
 18739  18896   #ifdef SQLITE_ENABLE_NULL_TRIM
 18740  18897   SQLITE_PRIVATE   void sqlite3SetMakeRecordP5(Vdbe*,Table*);
 18741  18898   #else
 18742  18899   # define sqlite3SetMakeRecordP5(A,B)
 18743  18900   #endif
................................................................................
 18750  18907   SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
 18751  18908   SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*);
 18752  18909   SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
 18753  18910   SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
 18754  18911   SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
 18755  18912   SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
 18756  18913   SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
        18914  +#ifdef SQLITE_ENABLE_NORMALIZE
        18915  +SQLITE_PRIVATE FuncDef *sqlite3FunctionSearchN(int,const char*,int);
        18916  +#endif
 18757  18917   SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
 18758  18918   SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
 18759  18919   SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
 18760  18920   SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
 18761  18921   SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
 18762  18922   SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
 18763  18923   SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
................................................................................
 18907  19067   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
 18908  19068   SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
 18909  19069   SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*);
 18910  19070   SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
 18911  19071   SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
 18912  19072   SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
 18913  19073   SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
        19074  +SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3*);
 18914  19075   SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
 18915  19076   SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
 18916  19077   SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
 18917  19078   SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
 18918  19079   SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
 18919  19080   SQLITE_PRIVATE int sqlite3AbsInt32(int);
 18920  19081   #ifdef SQLITE_ENABLE_8_3_NAMES
................................................................................
 18953  19114   #endif
 18954  19115   SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
 18955  19116   SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 18956  19117   SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 18957  19118   SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 18958  19119   SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
 18959  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
 18960  19124   SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 18961  19125   SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
 18962  19126   SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr *, int, int);
 18963  19127   SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
 18964  19128   SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
 18965  19129   SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
 18966  19130   SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
................................................................................
 19110  19274   SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
 19111  19275   SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
 19112  19276   SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
 19113  19277   SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
 19114  19278   SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
 19115  19279   SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
 19116  19280   SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
        19281  +#ifdef SQLITE_ENABLE_NORMALIZE
        19282  +SQLITE_PRIVATE void sqlite3Normalize(Vdbe*, const char*, int, u8);
        19283  +#endif
 19117  19284   SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
 19118  19285   SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
 19119  19286   SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
 19120  19287   SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
 19121  19288   SQLITE_PRIVATE const char *sqlite3JournalModename(int);
 19122  19289   #ifndef SQLITE_OMIT_WAL
 19123  19290   SQLITE_PRIVATE   int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
................................................................................
 19571  19738      0,                         /* xVdbeBranch */
 19572  19739      0,                         /* pVbeBranchArg */
 19573  19740   #endif
 19574  19741   #ifndef SQLITE_UNTESTABLE
 19575  19742      0,                         /* xTestCallback */
 19576  19743   #endif
 19577  19744      0,                         /* bLocaltimeFault */
        19745  +   0,                         /* bInternalFunctions */
 19578  19746      0x7ffffffe,                /* iOnceResetThreshold */
 19579  19747      SQLITE_DEFAULT_SORTERREF_SIZE   /* szSorterRef */
 19580  19748   };
 19581  19749   
 19582  19750   /*
 19583  19751   ** Hash table for global functions - functions common to all
 19584  19752   ** database connections.  After initialization, this table is
................................................................................
 20062  20230     bft usesStmtJournal:1;  /* True if uses a statement journal */
 20063  20231     bft readOnly:1;         /* True for statements that do not write */
 20064  20232     bft bIsReader:1;        /* True for statements that read */
 20065  20233     yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
 20066  20234     yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
 20067  20235     u32 aCounter[7];        /* Counters used by sqlite3_stmt_status() */
 20068  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
 20069  20240     void *pFree;            /* Free this when deleting the vdbe */
 20070  20241     VdbeFrame *pFrame;      /* Parent frame */
 20071  20242     VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
 20072  20243     int nFrame;             /* Number of frames in pFrame list */
 20073  20244     u32 expmask;            /* Binding to these vars invalidates VM */
 20074  20245     SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
 20075  20246     AuxData *pAuxData;      /* Linked list of auxdata allocations */
................................................................................
 20124  20295   SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
 20125  20296   SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
 20126  20297   
 20127  20298   int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
 20128  20299   SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
 20129  20300   SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
 20130  20301   SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
        20302  +#ifndef SQLITE_OMIT_EXPLAIN
 20131  20303   SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
        20304  +#endif
 20132  20305   SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
 20133  20306   SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
 20134  20307   SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
 20135  20308   SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
 20136  20309   SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
 20137  20310   SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
 20138  20311   SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
................................................................................
 20163  20336   SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
 20164  20337   SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
 20165  20338   SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
 20166  20339   SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
 20167  20340   #ifndef SQLITE_OMIT_WINDOWFUNC
 20168  20341   SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
 20169  20342   #endif
        20343  +#ifndef SQLITE_OMIT_EXPLAIN
 20170  20344   SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
        20345  +#endif
 20171  20346   SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
 20172  20347   SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
 20173  20348   SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
 20174  20349   #ifdef SQLITE_DEBUG
 20175  20350   SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame*);
 20176  20351   #endif
 20177  20352   SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void*);      /* Destructor on Mem */
................................................................................
 27605  27780             xtype = etSTRING;
 27606  27781           }else{
 27607  27782             bufpt = va_arg(ap,char*);
 27608  27783           }
 27609  27784           if( bufpt==0 ){
 27610  27785             bufpt = "";
 27611  27786           }else if( xtype==etDYNSTRING ){
 27612         -          if( pAccum->nChar==0 && pAccum->mxAlloc && width==0 && precision<0 ){
        27787  +          if( pAccum->nChar==0
        27788  +           && pAccum->mxAlloc
        27789  +           && width==0
        27790  +           && precision<0
        27791  +           && pAccum->accError==0
        27792  +          ){
 27613  27793               /* Special optimization for sqlite3_mprintf("%z..."):
 27614  27794               ** Extend an existing memory allocation rather than creating
 27615  27795               ** a new one. */
 27616  27796               assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
 27617  27797               pAccum->zText = bufpt;
 27618  27798               pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
 27619  27799               pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
................................................................................
 28285  28465         sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
 28286  28466         sqlite3TreeViewPop(pView);
 28287  28467       }
 28288  28468       sqlite3TreeViewPop(pView);
 28289  28469     }
 28290  28470   }
 28291  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  +}
 28292  28508   
 28293  28509   /*
 28294  28510   ** Generate a human-readable description of a Select object.
 28295  28511   */
 28296  28512   SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
 28297  28513     int n = 0;
 28298  28514     int cnt = 0;
................................................................................
 28339  28555         for(pX=p->pWin; pX; pX=pX->pNextWin){
 28340  28556           sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
 28341  28557         }
 28342  28558         sqlite3TreeViewPop(pView);
 28343  28559       }
 28344  28560   #endif
 28345  28561       if( p->pSrc && p->pSrc->nSrc ){
 28346         -      int i;
 28347  28562         pView = sqlite3TreeViewPush(pView, (n--)>0);
 28348  28563         sqlite3TreeViewLine(pView, "FROM");
 28349         -      for(i=0; i<p->pSrc->nSrc; i++){
 28350         -        struct SrcList_item *pItem = &p->pSrc->a[i];
 28351         -        StrAccum x;
 28352         -        char zLine[100];
 28353         -        sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
 28354         -        sqlite3_str_appendf(&x, "{%d,*}", pItem->iCursor);
 28355         -        if( pItem->zDatabase ){
 28356         -          sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
 28357         -        }else if( pItem->zName ){
 28358         -          sqlite3_str_appendf(&x, " %s", pItem->zName);
 28359         -        }
 28360         -        if( pItem->pTab ){
 28361         -          sqlite3_str_appendf(&x, " tabname=%Q", pItem->pTab->zName);
 28362         -        }
 28363         -        if( pItem->zAlias ){
 28364         -          sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
 28365         -        }
 28366         -        if( pItem->fg.jointype & JT_LEFT ){
 28367         -          sqlite3_str_appendf(&x, " LEFT-JOIN");
 28368         -        }
 28369         -        sqlite3StrAccumFinish(&x);
 28370         -        sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); 
 28371         -        if( pItem->pSelect ){
 28372         -          sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
 28373         -        }
 28374         -        if( pItem->fg.isTabFunc ){
 28375         -          sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
 28376         -        }
 28377         -        sqlite3TreeViewPop(pView);
 28378         -      }
        28564  +      sqlite3TreeViewSrcList(pView, p->pSrc);
 28379  28565         sqlite3TreeViewPop(pView);
 28380  28566       }
 28381  28567       if( p->pWhere ){
 28382  28568         sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
 28383  28569         sqlite3TreeViewExpr(pView, p->pWhere, 0);
 28384  28570         sqlite3TreeViewPop(pView);
 28385  28571       }
................................................................................
 28661  28847         Window *pWin;
 28662  28848         if( ExprHasProperty(pExpr, EP_TokenOnly) ){
 28663  28849           pFarg = 0;
 28664  28850           pWin = 0;
 28665  28851         }else{
 28666  28852           pFarg = pExpr->x.pList;
 28667  28853   #ifndef SQLITE_OMIT_WINDOWFUNC
 28668         -        pWin = pExpr->pWin;
        28854  +        pWin = pExpr->y.pWin;
 28669  28855   #else
 28670  28856           pWin = 0;
 28671  28857   #endif 
 28672  28858         }
 28673  28859         if( pExpr->op==TK_AGG_FUNCTION ){
 28674  28860           sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
 28675  28861                                pExpr->op2, pExpr->u.zToken);
................................................................................
 31489  31675       ** 0x9e3779b1 is 2654435761 which is the closest prime number to
 31490  31676       ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
 31491  31677       h += sqlite3UpperToLower[c];
 31492  31678       h *= 0x9e3779b1;
 31493  31679     }
 31494  31680     return h;
 31495  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 */
 31496  31696   
 31497  31697   
 31498  31698   /* Link pNew element into the hash table pH.  If pEntry!=0 then also
 31499  31699   ** insert pNew into the pEntry hash bucket.
 31500  31700   */
 31501  31701   static void insertElement(
 31502  31702     Hash *pH,              /* The complete hash table */
................................................................................
 31600  31800       if( sqlite3StrICmp(elem->pKey,pKey)==0 ){ 
 31601  31801         return elem;
 31602  31802       }
 31603  31803       elem = elem->next;
 31604  31804     }
 31605  31805     return &nullElement;
 31606  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 */
 31607  31841   
 31608  31842   /* Remove a single entry from the hash table given a pointer to that
 31609  31843   ** element and a hash on the element's key.
 31610  31844   */
 31611  31845   static void removeElementGivenHash(
 31612  31846     Hash *pH,         /* The pH containing "elem" */
 31613  31847     HashElem* elem,   /* The element to be removed from the pH */
................................................................................
 31644  31878   ** found, or NULL if there is no match.
 31645  31879   */
 31646  31880   SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){
 31647  31881     assert( pH!=0 );
 31648  31882     assert( pKey!=0 );
 31649  31883     return findElementWithHash(pH, pKey, 0)->data;
 31650  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 */
 31651  31893   
 31652  31894   /* Insert an element into the hash table pH.  The key is pKey
 31653  31895   ** and the data is "data".
 31654  31896   **
 31655  31897   ** If no element exists with a matching key, then a new
 31656  31898   ** element is created and NULL is returned.
 31657  31899   **
................................................................................
 32026  32268   
 32027  32269   /*
 32028  32270   ** Allowed values of unixFile.fsFlags
 32029  32271   */
 32030  32272   #define SQLITE_FSFLAGS_IS_MSDOS     0x1
 32031  32273   
 32032  32274   /*
 32033         -** If we are to be thread-safe, include the pthreads header and define
 32034         -** the SQLITE_UNIX_THREADS macro.
        32275  +** If we are to be thread-safe, include the pthreads header.
 32035  32276   */
 32036  32277   #if SQLITE_THREADSAFE
 32037  32278   /* # include <pthread.h> */
 32038         -# define SQLITE_UNIX_THREADS 1
 32039  32279   #endif
 32040  32280   
 32041  32281   /*
 32042  32282   ** Default permissions when creating a new file
 32043  32283   */
 32044  32284   #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
 32045  32285   # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
................................................................................
 32609  32849     { "lstat",         (sqlite3_syscall_ptr)lstat,          0 },
 32610  32850   #else
 32611  32851     { "lstat",         (sqlite3_syscall_ptr)0,              0 },
 32612  32852   #endif
 32613  32853   #define osLstat      ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
 32614  32854   
 32615  32855   #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
        32856  +# ifdef __ANDROID__
        32857  +  { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
        32858  +# else
 32616  32859     { "ioctl",         (sqlite3_syscall_ptr)ioctl,          0 },
        32860  +# endif
 32617  32861   #else
 32618  32862     { "ioctl",         (sqlite3_syscall_ptr)0,              0 },
 32619  32863   #endif
 32620  32864   #define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
 32621  32865   
 32622  32866   }; /* End of the overrideable system calls */
 32623  32867   
................................................................................
 33203  33447     /* WAS:  ino_t ino;   */
 33204  33448     u64 ino;                   /* Inode number */
 33205  33449   #endif
 33206  33450   };
 33207  33451   
 33208  33452   /*
 33209  33453   ** An instance of the following structure is allocated for each open
 33210         -** inode.  Or, on LinuxThreads, there is one of these structures for
 33211         -** each inode opened by each thread.
        33454  +** inode.
 33212  33455   **
 33213  33456   ** A single inode can have multiple file descriptors, so each unixFile
 33214  33457   ** structure contains a pointer to an instance of this object and this
 33215  33458   ** object keeps a count of the number of unixFile pointing to it.
 33216  33459   **
 33217  33460   ** Mutex rules:
 33218  33461   **
................................................................................
 33250  33493     sem_t *pSem;                    /* Named POSIX semaphore */
 33251  33494     char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
 33252  33495   #endif
 33253  33496   };
 33254  33497   
 33255  33498   /*
 33256  33499   ** A lists of all unixInodeInfo objects.
        33500  +**
        33501  +** Must hold unixBigLock in order to read or write this variable.
 33257  33502   */
 33258  33503   static unixInodeInfo *inodeList = 0;  /* All unixInodeInfo objects */
 33259  33504   
 33260  33505   #ifdef SQLITE_DEBUG
 33261  33506   /*
 33262         -** True if the inode mutex is held, or not.  Used only within assert()
 33263         -** 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.
 33264  33510   */
 33265  33511   int unixFileMutexHeld(unixFile *pFile){
 33266  33512     assert( pFile->pInode );
 33267  33513     return sqlite3_mutex_held(pFile->pInode->pLockMutex);
 33268  33514   }
 33269  33515   int unixFileMutexNotheld(unixFile *pFile){
 33270  33516     assert( pFile->pInode );
................................................................................
 33384  33630     }
 33385  33631     pInode->pUnused = 0;
 33386  33632   }
 33387  33633   
 33388  33634   /*
 33389  33635   ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
 33390  33636   **
 33391         -** The mutex entered using the unixEnterMutex() function must be held
 33392         -** 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.
 33393  33639   */
 33394  33640   static void releaseInodeInfo(unixFile *pFile){
 33395  33641     unixInodeInfo *pInode = pFile->pInode;
 33396  33642     assert( unixMutexHeld() );
 33397  33643     assert( unixFileMutexNotheld(pFile) );
 33398  33644     if( ALWAYS(pInode) ){
 33399  33645       pInode->nRef--;
................................................................................
 33420  33666   }
 33421  33667   
 33422  33668   /*
 33423  33669   ** Given a file descriptor, locate the unixInodeInfo object that
 33424  33670   ** describes that file descriptor.  Create a new one if necessary.  The
 33425  33671   ** return value might be uninitialized if an error occurs.
 33426  33672   **
 33427         -** The mutex entered using the unixEnterMutex() function must be held
 33428         -** when this function is called.
        33673  +** The global mutex must held when calling this routine.
 33429  33674   **
 33430  33675   ** Return an appropriate error code.
 33431  33676   */
 33432  33677   static int findInodeInfo(
 33433  33678     unixFile *pFile,               /* Unix file with file desc used in the key */
 33434  33679     unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
 33435  33680   ){
................................................................................
 33482  33727     memset(&fileId, 0, sizeof(fileId));
 33483  33728     fileId.dev = statbuf.st_dev;
 33484  33729   #if OS_VXWORKS
 33485  33730     fileId.pId = pFile->pId;
 33486  33731   #else
 33487  33732     fileId.ino = (u64)statbuf.st_ino;
 33488  33733   #endif
        33734  +  assert( unixMutexHeld() );
 33489  33735     pInode = inodeList;
 33490  33736     while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
 33491  33737       pInode = pInode->pNext;
 33492  33738     }
 33493  33739     if( pInode==0 ){
 33494  33740       pInode = sqlite3_malloc64( sizeof(*pInode) );
 33495  33741       if( pInode==0 ){
................................................................................
 33501  33747         pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 33502  33748         if( pInode->pLockMutex==0 ){
 33503  33749           sqlite3_free(pInode);
 33504  33750           return SQLITE_NOMEM_BKPT;
 33505  33751         }
 33506  33752       }
 33507  33753       pInode->nRef = 1;
        33754  +    assert( unixMutexHeld() );
 33508  33755       pInode->pNext = inodeList;
 33509  33756       pInode->pPrev = 0;
 33510  33757       if( inodeList ) inodeList->pPrev = pInode;
 33511  33758       inodeList = pInode;
 33512  33759     }else{
 33513  33760       pInode->nRef++;
 33514  33761     }
................................................................................
 36298  36545   ** unixMutexHeld() must be true when creating or destroying
 36299  36546   ** this object or while reading or writing the following fields:
 36300  36547   **
 36301  36548   **      nRef
 36302  36549   **
 36303  36550   ** The following fields are read-only after the object is created:
 36304  36551   ** 
 36305         -**      fid
        36552  +**      hShm
 36306  36553   **      zFilename
 36307  36554   **
 36308         -** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
        36555  +** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and
 36309  36556   ** unixMutexHeld() is true when reading or writing any other field
 36310  36557   ** in this structure.
 36311  36558   */
 36312  36559   struct unixShmNode {
 36313  36560     unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
 36314         -  sqlite3_mutex *mutex;      /* Mutex to access this object */
        36561  +  sqlite3_mutex *pShmMutex;  /* Mutex to access this object */
 36315  36562     char *zFilename;           /* Name of the mmapped file */
 36316         -  int h;                     /* Open file descriptor */
        36563  +  int hShm;                  /* Open file descriptor */
 36317  36564     int szRegion;              /* Size of shared-memory regions */
 36318  36565     u16 nRegion;               /* Size of array apRegion */
 36319  36566     u8 isReadonly;             /* True if read-only */
 36320  36567     u8 isUnlocked;             /* True if no DMS lock held */
 36321  36568     char **apRegion;           /* Array of mapped shared-memory regions */
 36322  36569     int nRef;                  /* Number of unixShm objects pointing to this */
 36323  36570     unixShm *pFirst;           /* All unixShm objects pointing to this */
................................................................................
 36331  36578   /*
 36332  36579   ** Structure used internally by this VFS to record the state of an
 36333  36580   ** open shared memory connection.
 36334  36581   **
 36335  36582   ** The following fields are initialized when this object is created and
 36336  36583   ** are read-only thereafter:
 36337  36584   **
 36338         -**    unixShm.pFile
        36585  +**    unixShm.pShmNode
 36339  36586   **    unixShm.id
 36340  36587   **
 36341         -** All other fields are read/write.  The unixShm.pFile->mutex must be held
 36342         -** 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.
 36343  36590   */
 36344  36591   struct unixShm {
 36345  36592     unixShmNode *pShmNode;     /* The underlying unixShmNode object */
 36346  36593     unixShm *pNext;            /* Next unixShm with the same unixShmNode */
 36347         -  u8 hasMutex;               /* True if holding the unixShmNode mutex */
        36594  +  u8 hasMutex;               /* True if holding the unixShmNode->pShmMutex */
 36348  36595     u8 id;                     /* Id of this connection within its unixShmNode */
 36349  36596     u16 sharedMask;            /* Mask of shared locks held */
 36350  36597     u16 exclMask;              /* Mask of exclusive locks held */
 36351  36598   };
 36352  36599   
 36353  36600   /*
 36354  36601   ** Constants used for locking
................................................................................
 36370  36617   ){
 36371  36618     unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
 36372  36619     struct flock f;        /* The posix advisory locking structure */
 36373  36620     int rc = SQLITE_OK;    /* Result code form fcntl() */
 36374  36621   
 36375  36622     /* Access to the unixShmNode object is serialized by the caller */
 36376  36623     pShmNode = pFile->pInode->pShmNode;
 36377         -  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() );
 36378  36626   
 36379  36627     /* Shared locks never span more than one byte */
 36380  36628     assert( n==1 || lockType!=F_RDLCK );
 36381  36629   
 36382  36630     /* Locks are within range */
 36383  36631     assert( n>=1 && n<=SQLITE_SHM_NLOCK );
 36384  36632   
 36385         -  if( pShmNode->h>=0 ){
        36633  +  if( pShmNode->hShm>=0 ){
 36386  36634       /* Initialize the locking parameters */
 36387  36635       f.l_type = lockType;
 36388  36636       f.l_whence = SEEK_SET;
 36389  36637       f.l_start = ofst;
 36390  36638       f.l_len = n;
 36391         -    rc = osSetPosixAdvisoryLock(pShmNode->h, &f, pFile);
        36639  +    rc = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
 36392  36640       rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
 36393  36641     }
 36394  36642   
 36395  36643     /* Update the global lock state and do debug tracing */
 36396  36644   #ifdef SQLITE_DEBUG
 36397  36645     { u16 mask;
 36398  36646     OSTRACE(("SHM-LOCK "));
................................................................................
 36456  36704   static void unixShmPurge(unixFile *pFd){
 36457  36705     unixShmNode *p = pFd->pInode->pShmNode;
 36458  36706     assert( unixMutexHeld() );
 36459  36707     if( p && ALWAYS(p->nRef==0) ){
 36460  36708       int nShmPerMap = unixShmRegionPerMap();
 36461  36709       int i;
 36462  36710       assert( p->pInode==pFd->pInode );
 36463         -    sqlite3_mutex_free(p->mutex);
        36711  +    sqlite3_mutex_free(p->pShmMutex);
 36464  36712       for(i=0; i<p->nRegion; i+=nShmPerMap){
 36465         -      if( p->h>=0 ){
        36713  +      if( p->hShm>=0 ){
 36466  36714           osMunmap(p->apRegion[i], p->szRegion);
 36467  36715         }else{
 36468  36716           sqlite3_free(p->apRegion[i]);
 36469  36717         }
 36470  36718       }
 36471  36719       sqlite3_free(p->apRegion);
 36472         -    if( p->h>=0 ){
 36473         -      robust_close(pFd, p->h, __LINE__);
 36474         -      p->h = -1;
        36720  +    if( p->hShm>=0 ){
        36721  +      robust_close(pFd, p->hShm, __LINE__);
        36722  +      p->hShm = -1;
 36475  36723       }
 36476  36724       p->pInode->pShmNode = 0;
 36477  36725       sqlite3_free(p);
 36478  36726     }
 36479  36727   }
 36480  36728   
 36481  36729   /*
................................................................................
 36509  36757     ** process might open and use the *-shm file without truncating it.
 36510  36758     ** And if the *-shm file has been corrupted by a power failure or
 36511  36759     ** system crash, the database itself may also become corrupt.  */
 36512  36760     lock.l_whence = SEEK_SET;
 36513  36761     lock.l_start = UNIX_SHM_DMS;
 36514  36762     lock.l_len = 1;
 36515  36763     lock.l_type = F_WRLCK;
 36516         -  if( osFcntl(pShmNode->h, F_GETLK, &lock)!=0 ) {
        36764  +  if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
 36517  36765       rc = SQLITE_IOERR_LOCK;
 36518  36766     }else if( lock.l_type==F_UNLCK ){
 36519  36767       if( pShmNode->isReadonly ){
 36520  36768         pShmNode->isUnlocked = 1;
 36521  36769         rc = SQLITE_READONLY_CANTINIT;
 36522  36770       }else{
 36523  36771         rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1);
 36524         -      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) ){
 36525  36778           rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
 36526  36779         }
 36527  36780       }
 36528  36781     }else if( lock.l_type==F_WRLCK ){
 36529  36782       rc = SQLITE_BUSY;
 36530  36783     }
 36531  36784   
................................................................................
 36623  36876       sqlite3_snprintf(nShmFilename, zShm, 
 36624  36877                        SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
 36625  36878                        (u32)sStat.st_ino, (u32)sStat.st_dev);
 36626  36879   #else
 36627  36880       sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
 36628  36881       sqlite3FileSuffix3(pDbFd->zPath, zShm);
 36629  36882   #endif
 36630         -    pShmNode->h = -1;
        36883  +    pShmNode->hShm = -1;
 36631  36884       pDbFd->pInode->pShmNode = pShmNode;
 36632  36885       pShmNode->pInode = pDbFd->pInode;
 36633  36886       if( sqlite3GlobalConfig.bCoreMutex ){
 36634         -      pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 36635         -      if( pShmNode->mutex==0 ){
        36887  +      pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
        36888  +      if( pShmNode->pShmMutex==0 ){
 36636  36889           rc = SQLITE_NOMEM_BKPT;
 36637  36890           goto shm_open_err;
 36638  36891         }
 36639  36892       }
 36640  36893   
 36641  36894       if( pInode->bProcessLock==0 ){
 36642  36895         if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
 36643         -        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));
 36644  36897         }
 36645         -      if( pShmNode->h<0 ){
 36646         -        pShmNode->h = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777));
 36647         -        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 ){
 36648  36901             rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm);
 36649  36902             goto shm_open_err;
 36650  36903           }
 36651  36904           pShmNode->isReadonly = 1;
 36652  36905         }
 36653  36906   
 36654  36907         /* If this process is running as root, make sure that the SHM file
 36655  36908         ** is owned by the same user that owns the original database.  Otherwise,
 36656  36909         ** the original owner will not be able to connect.
 36657  36910         */
 36658         -      robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
        36911  +      robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
 36659  36912   
 36660  36913         rc = unixLockSharedMemory(pDbFd, pShmNode);
 36661  36914         if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
 36662  36915       }
 36663  36916     }
 36664  36917   
 36665  36918     /* Make the new connection a child of the unixShmNode */
................................................................................
 36671  36924     pDbFd->pShm = p;
 36672  36925     unixLeaveMutex();
 36673  36926   
 36674  36927     /* The reference count on pShmNode has already been incremented under
 36675  36928     ** the cover of the unixEnterMutex() mutex and the pointer from the
 36676  36929     ** new (struct unixShm) object to the pShmNode has been set. All that is
 36677  36930     ** left to do is to link the new object into the linked list starting
 36678         -  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
 36679         -  ** mutex.
        36931  +  ** at pShmNode->pFirst. This must be done while holding the
        36932  +  ** pShmNode->pShmMutex.
 36680  36933     */
 36681         -  sqlite3_mutex_enter(pShmNode->mutex);
        36934  +  sqlite3_mutex_enter(pShmNode->pShmMutex);
 36682  36935     p->pNext = pShmNode->pFirst;
 36683  36936     pShmNode->pFirst = p;
 36684         -  sqlite3_mutex_leave(pShmNode->mutex);
        36937  +  sqlite3_mutex_leave(pShmNode->pShmMutex);
 36685  36938     return rc;
 36686  36939   
 36687  36940     /* Jump here on any error */
 36688  36941   shm_open_err:
 36689  36942     unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
 36690  36943     sqlite3_free(p);
 36691  36944     unixLeaveMutex();
................................................................................
 36729  36982     if( pDbFd->pShm==0 ){
 36730  36983       rc = unixOpenSharedMemory(pDbFd);
 36731  36984       if( rc!=SQLITE_OK ) return rc;
 36732  36985     }
 36733  36986   
 36734  36987     p = pDbFd->pShm;
 36735  36988     pShmNode = p->pShmNode;
 36736         -  sqlite3_mutex_enter(pShmNode->mutex);
        36989  +  sqlite3_mutex_enter(pShmNode->pShmMutex);
 36737  36990     if( pShmNode->isUnlocked ){
 36738  36991       rc = unixLockSharedMemory(pDbFd, pShmNode);
 36739  36992       if( rc!=SQLITE_OK ) goto shmpage_out;
 36740  36993       pShmNode->isUnlocked = 0;
 36741  36994     }
 36742  36995     assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
 36743  36996     assert( pShmNode->pInode==pDbFd->pInode );
 36744         -  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
 36745         -  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 );
 36746  36999   
 36747  37000     /* Minimum number of regions required to be mapped. */
 36748  37001     nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
 36749  37002   
 36750  37003     if( pShmNode->nRegion<nReqRegion ){
 36751  37004       char **apNew;                      /* New apRegion[] array */
 36752  37005       int nByte = nReqRegion*szRegion;   /* Minimum required file size */
 36753  37006       struct stat sStat;                 /* Used by fstat() */
 36754  37007   
 36755  37008       pShmNode->szRegion = szRegion;
 36756  37009   
 36757         -    if( pShmNode->h>=0 ){
        37010  +    if( pShmNode->hShm>=0 ){
 36758  37011         /* The requested region is not mapped into this processes address space.
 36759  37012         ** Check to see if it has been allocated (i.e. if the wal-index file is
 36760  37013         ** large enough to contain the requested region).
 36761  37014         */
 36762         -      if( osFstat(pShmNode->h, &sStat) ){
        37015  +      if( osFstat(pShmNode->hShm, &sStat) ){
 36763  37016           rc = SQLITE_IOERR_SHMSIZE;
 36764  37017           goto shmpage_out;
 36765  37018         }
 36766  37019     
 36767  37020         if( sStat.st_size<nByte ){
 36768  37021           /* The requested memory region does not exist. If bExtend is set to
 36769  37022           ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
................................................................................
 36783  37036             static const int pgsz = 4096;
 36784  37037             int iPg;
 36785  37038   
 36786  37039             /* Write to the last byte of each newly allocated or extended page */
 36787  37040             assert( (nByte % pgsz)==0 );
 36788  37041             for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
 36789  37042               int x = 0;
 36790         -            if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){
        37043  +            if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
 36791  37044                 const char *zFile = pShmNode->zFilename;
 36792  37045                 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
 36793  37046                 goto shmpage_out;
 36794  37047               }
 36795  37048             }
 36796  37049           }
 36797  37050         }
................................................................................
 36806  37059         goto shmpage_out;
 36807  37060       }
 36808  37061       pShmNode->apRegion = apNew;
 36809  37062       while( pShmNode->nRegion<nReqRegion ){
 36810  37063         int nMap = szRegion*nShmPerMap;
 36811  37064         int i;
 36812  37065         void *pMem;
 36813         -      if( pShmNode->h>=0 ){
        37066  +      if( pShmNode->hShm>=0 ){
 36814  37067           pMem = osMmap(0, nMap,
 36815  37068               pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
 36816         -            MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
        37069  +            MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
 36817  37070           );
 36818  37071           if( pMem==MAP_FAILED ){
 36819  37072             rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
 36820  37073             goto shmpage_out;
 36821  37074           }
 36822  37075         }else{
 36823         -        pMem = sqlite3_malloc64(szRegion);
        37076  +        pMem = sqlite3_malloc64(nMap);
 36824  37077           if( pMem==0 ){
 36825  37078             rc = SQLITE_NOMEM_BKPT;
 36826  37079             goto shmpage_out;
 36827  37080           }
 36828         -        memset(pMem, 0, szRegion);
        37081  +        memset(pMem, 0, nMap);
 36829  37082         }
 36830  37083   
 36831  37084         for(i=0; i<nShmPerMap; i++){
 36832  37085           pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
 36833  37086         }
 36834  37087         pShmNode->nRegion += nShmPerMap;
 36835  37088       }
................................................................................
 36838  37091   shmpage_out:
 36839  37092     if( pShmNode->nRegion>iRegion ){
 36840  37093       *pp = pShmNode->apRegion[iRegion];
 36841  37094     }else{
 36842  37095       *pp = 0;
 36843  37096     }
 36844  37097     if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
 36845         -  sqlite3_mutex_leave(pShmNode->mutex);
        37098  +  sqlite3_mutex_leave(pShmNode->pShmMutex);
 36846  37099     return rc;
 36847  37100   }
 36848  37101   
 36849  37102   /*
 36850  37103   ** Change the lock state for a shared-memory segment.
 36851  37104   **
 36852  37105   ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
................................................................................
 36872  37125     assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
 36873  37126     assert( n>=1 );
 36874  37127     assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
 36875  37128          || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
 36876  37129          || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
 36877  37130          || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
 36878  37131     assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
 36879         -  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
 36880         -  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 );
 36881  37134   
 36882  37135     mask = (1<<(ofst+n)) - (1<<ofst);
 36883  37136     assert( n>1 || mask==(1<<ofst) );
 36884         -  sqlite3_mutex_enter(pShmNode->mutex);
        37137  +  sqlite3_mutex_enter(pShmNode->pShmMutex);
 36885  37138     if( flags & SQLITE_SHM_UNLOCK ){
 36886  37139       u16 allMask = 0; /* Mask of locks held by siblings */
 36887  37140   
 36888  37141       /* See if any siblings hold this same lock */
 36889  37142       for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 36890  37143         if( pX==p ) continue;
 36891  37144         assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
................................................................................
 36950  37203         rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
 36951  37204         if( rc==SQLITE_OK ){
 36952  37205           assert( (p->sharedMask & mask)==0 );
 36953  37206           p->exclMask |= mask;
 36954  37207         }
 36955  37208       }
 36956  37209     }
 36957         -  sqlite3_mutex_leave(pShmNode->mutex);
        37210  +  sqlite3_mutex_leave(pShmNode->pShmMutex);
 36958  37211     OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
 36959  37212              p->id, osGetpid(0), p->sharedMask, p->exclMask));
 36960  37213     return rc;
 36961  37214   }
 36962  37215   
 36963  37216   /*
 36964  37217   ** Implement a memory barrier or memory fence on shared memory.  
................................................................................
 36967  37220   ** any load or store begun after the barrier.
 36968  37221   */
 36969  37222   static void unixShmBarrier(
 36970  37223     sqlite3_file *fd                /* Database file holding the shared memory */
 36971  37224   ){
 36972  37225     UNUSED_PARAMETER(fd);
 36973  37226     sqlite3MemoryBarrier();         /* compiler-defined memory barrier */
 36974         -  assert( unixFileMutexNotheld((unixFile*)fd) );
        37227  +  assert( fd->pMethods->xLock==nolockLock 
        37228  +       || unixFileMutexNotheld((unixFile*)fd) 
        37229  +  );
 36975  37230     unixEnterMutex();               /* Also mutex, for redundancy */
 36976  37231     unixLeaveMutex();
 36977  37232   }
 36978  37233   
 36979  37234   /*
 36980  37235   ** Close a connection to shared-memory.  Delete the underlying 
 36981  37236   ** storage if deleteFlag is true.
................................................................................
 36998  37253     pShmNode = p->pShmNode;
 36999  37254   
 37000  37255     assert( pShmNode==pDbFd->pInode->pShmNode );
 37001  37256     assert( pShmNode->pInode==pDbFd->pInode );
 37002  37257   
 37003  37258     /* Remove connection p from the set of connections associated
 37004  37259     ** with pShmNode */
 37005         -  sqlite3_mutex_enter(pShmNode->mutex);
        37260  +  sqlite3_mutex_enter(pShmNode->pShmMutex);
 37006  37261     for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
 37007  37262     *pp = p->pNext;
 37008  37263   
 37009  37264     /* Free the connection p */
 37010  37265     sqlite3_free(p);
 37011  37266     pDbFd->pShm = 0;
 37012         -  sqlite3_mutex_leave(pShmNode->mutex);
        37267  +  sqlite3_mutex_leave(pShmNode->pShmMutex);
 37013  37268   
 37014  37269     /* If pShmNode->nRef has reached 0, then close the underlying
 37015  37270     ** shared-memory file, too */
 37016  37271     assert( unixFileMutexNotheld(pDbFd) );
 37017  37272     unixEnterMutex();
 37018  37273     assert( pShmNode->nRef>0 );
 37019  37274     pShmNode->nRef--;
 37020  37275     if( pShmNode->nRef==0 ){
 37021         -    if( deleteFlag && pShmNode->h>=0 ){
        37276  +    if( deleteFlag && pShmNode->hShm>=0 ){
 37022  37277         osUnlink(pShmNode->zFilename);
 37023  37278       }
 37024  37279       unixShmPurge(pDbFd);
 37025  37280     }
 37026  37281     unixLeaveMutex();
 37027  37282   
 37028  37283     return SQLITE_OK;
................................................................................
 40432  40687     winceLock local;        /* Locks obtained by this instance of winFile */
 40433  40688     winceLock *shared;      /* Global shared lock memory for the file  */
 40434  40689   #endif
 40435  40690   #if SQLITE_MAX_MMAP_SIZE>0
 40436  40691     int nFetchOut;                /* Number of outstanding xFetch references */
 40437  40692     HANDLE hMap;                  /* Handle for accessing memory mapping */
 40438  40693     void *pMapRegion;             /* Area memory mapped */
 40439         -  sqlite3_int64 mmapSize;       /* Usable size of mapped region */
 40440         -  sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
        40694  +  sqlite3_int64 mmapSize;       /* Size of mapped region */
 40441  40695     sqlite3_int64 mmapSizeMax;    /* Configured FCNTL_MMAP_SIZE value */
 40442  40696   #endif
 40443  40697   };
 40444  40698   
 40445  40699   /*
 40446  40700   ** The winVfsAppData structure is used for the pAppData member for all of the
 40447  40701   ** Win32 VFS variants.
................................................................................
 43054  43308   */
 43055  43309   static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
 43056  43310     winFile *pFile = (winFile*)id;  /* File handle object */
 43057  43311     int rc = SQLITE_OK;             /* Return code for this function */
 43058  43312     DWORD lastErrno;
 43059  43313   #if SQLITE_MAX_MMAP_SIZE>0
 43060  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  +  }
 43061  43335   #endif
 43062  43336   
 43063  43337     assert( pFile );
 43064  43338     SimulateIOError(return SQLITE_IOERR_TRUNCATE);
 43065  43339     OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
 43066  43340              osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
 43067  43341   
................................................................................
 44482  44756   /*
 44483  44757   ** Cleans up the mapped region of the specified file, if any.
 44484  44758   */
 44485  44759   #if SQLITE_MAX_MMAP_SIZE>0
 44486  44760   static int winUnmapfile(winFile *pFile){
 44487  44761     assert( pFile!=0 );
 44488  44762     OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
 44489         -           "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n",
        44763  +           "mmapSize=%lld, mmapSizeMax=%lld\n",
 44490  44764              osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
 44491         -           pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax));
        44765  +           pFile->mmapSize, pFile->mmapSizeMax));
 44492  44766     if( pFile->pMapRegion ){
 44493  44767       if( !osUnmapViewOfFile(pFile->pMapRegion) ){
 44494  44768         pFile->lastErrno = osGetLastError();
 44495  44769         OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
 44496  44770                  "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
 44497  44771                  pFile->pMapRegion));
 44498  44772         return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
 44499  44773                            "winUnmapfile1", pFile->zPath);
 44500  44774       }
 44501  44775       pFile->pMapRegion = 0;
 44502  44776       pFile->mmapSize = 0;
 44503         -    pFile->mmapSizeActual = 0;
 44504  44777     }
 44505  44778     if( pFile->hMap!=NULL ){
 44506  44779       if( !osCloseHandle(pFile->hMap) ){
 44507  44780         pFile->lastErrno = osGetLastError();
 44508  44781         OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
 44509  44782                  osGetCurrentProcessId(), pFile, pFile->hMap));
 44510  44783         return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
................................................................................
 44607  44880         /* Log the error, but continue normal operation using xRead/xWrite */
 44608  44881         OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
 44609  44882                  osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
 44610  44883         return SQLITE_OK;
 44611  44884       }
 44612  44885       pFd->pMapRegion = pNew;
 44613  44886       pFd->mmapSize = nMap;
 44614         -    pFd->mmapSizeActual = nMap;
 44615  44887     }
 44616  44888   
 44617  44889     OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
 44618  44890              osGetCurrentProcessId(), pFd));
 44619  44891     return SQLITE_OK;
 44620  44892   }
 44621  44893   #endif /* SQLITE_MAX_MMAP_SIZE>0 */
................................................................................
 45409  45681     }
 45410  45682     pFile->lastErrno = NO_ERROR;
 45411  45683     pFile->zPath = zName;
 45412  45684   #if SQLITE_MAX_MMAP_SIZE>0
 45413  45685     pFile->hMap = NULL;
 45414  45686     pFile->pMapRegion = 0;
 45415  45687     pFile->mmapSize = 0;
 45416         -  pFile->mmapSizeActual = 0;
 45417  45688     pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
 45418  45689   #endif
 45419  45690   
 45420  45691     OpenCounter(+1);
 45421  45692     return rc;
 45422  45693   }
 45423  45694   
................................................................................
 47306  47577   **   such that p was added to the list more recently than p->pDirtyNext.
 47307  47578   **   PCache.pDirty points to the first (newest) element in the list and
 47308  47579   **   pDirtyTail to the last (oldest).
 47309  47580   **
 47310  47581   **   The PCache.pSynced variable is used to optimize searching for a dirty
 47311  47582   **   page to eject from the cache mid-transaction. It is better to eject
 47312  47583   **   a page that does not require a journal sync than one that does. 
 47313         -**   Therefore, pSynced is maintained to that it *almost* always points
        47584  +**   Therefore, pSynced is maintained so that it *almost* always points
 47314  47585   **   to either the oldest page in the pDirty/pDirtyTail list that has a
 47315  47586   **   clear PGHDR_NEED_SYNC flag or to a page that is older than this one
 47316  47587   **   (so that the right page to eject can be found by following pDirtyPrev
 47317  47588   **   pointers).
 47318  47589   */
 47319  47590   struct PCache {
 47320  47591     PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
................................................................................
 48129  48400   SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
 48130  48401     PgHdr *pDirty;
 48131  48402     int nDirty = 0;
 48132  48403     int nCache = numberOfCachePages(pCache);
 48133  48404     for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
 48134  48405     return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
 48135  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
 48136  48416   
 48137  48417   #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
 48138  48418   /*
 48139  48419   ** For all dirty pages currently in the cache, invoke the specified
 48140  48420   ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
 48141  48421   ** defined.
 48142  48422   */
................................................................................
 50893  51173   **
 50894  51174   ** instead of
 50895  51175   **
 50896  51176   **   if( pPager->jfd->pMethods ){ ...
 50897  51177   */
 50898  51178   #define isOpen(pFd) ((pFd)->pMethods!=0)
 50899  51179   
        51180  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
 50900  51181   /*
 50901         -** Return true if this pager uses a write-ahead log to read page pgno.
 50902         -** Return false if the pager reads pgno directly from the database.
        51182  +** Return true if page pgno can be read directly from the database file
        51183  +** by the b-tree layer. This is the case if:
        51184  +**
        51185  +**   * the database file is open,
        51186  +**   * there are no dirty pages in the cache, and
        51187  +**   * the desired page is not currently in the wal file.
 50903  51188   */
 50904         -#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_DIRECT_OVERFLOW_READ)
 50905         -SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno pgno){
 50906         -  u32 iRead = 0;
 50907         -  int rc;
 50908         -  if( pPager->pWal==0 ) return 0;
 50909         -  rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
 50910         -  return rc || iRead;
        51189  +SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
        51190  +  if( pPager->fd->pMethods==0 ) return 0;
        51191  +  if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
        51192  +#ifndef SQLITE_OMIT_WAL
        51193  +  if( pPager->pWal ){
        51194  +    u32 iRead = 0;
        51195  +    int rc;
        51196  +    rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
        51197  +    return (rc==SQLITE_OK && iRead==0);
        51198  +  }
        51199  +#endif
        51200  +  return 1;
 50911  51201   }
 50912  51202   #endif
        51203  +
 50913  51204   #ifndef SQLITE_OMIT_WAL
 50914  51205   # define pagerUseWal(x) ((x)->pWal!=0)
 50915  51206   #else
 50916  51207   # define pagerUseWal(x) 0
 50917  51208   # define pagerRollbackWal(x) 0
 50918  51209   # define pagerWalFrames(v,w,x,y) 0
 50919  51210   # define pagerOpenWalIfPresent(z) SQLITE_OK
................................................................................
 57089  57380   SQLITE_PRIVATE void sqlite3PagerSetCodec(
 57090  57381     Pager *pPager,
 57091  57382     void *(*xCodec)(void*,void*,Pgno,int),
 57092  57383     void (*xCodecSizeChng)(void*,int,int),
 57093  57384     void (*xCodecFree)(void*),
 57094  57385     void *pCodec
 57095  57386   ){
        57387  +  pager_reset(pPager);
 57096  57388     if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
 57097  57389     pPager->xCodec = pPager->memDb ? 0 : xCodec;
 57098  57390     pPager->xCodecSizeChng = xCodecSizeChng;
 57099  57391     pPager->xCodecFree = xCodecFree;
 57100  57392     pPager->pCodec = pCodec;
 57101  57393     setGetterMethod(pPager);
 57102  57394     pagerReportSize(pPager);
................................................................................
 65749  66041         pBt->usableSize = usableSize;
 65750  66042         pBt->pageSize = pageSize;
 65751  66043         freeTempSpace(pBt);
 65752  66044         rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
 65753  66045                                      pageSize-usableSize);
 65754  66046         return rc;
 65755  66047       }
 65756         -    if( (pBt->db->flags & SQLITE_WriteSchema)==0 && nPage>nPageFile ){
        66048  +    if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){
 65757  66049         rc = SQLITE_CORRUPT_BKPT;
 65758  66050         goto page1_init_failed;
 65759  66051       }
 65760  66052       /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
 65761  66053       ** be less than 480. In other words, if the page size is 512, then the
 65762  66054       ** reserved space size cannot exceed 32. */
 65763  66055       if( usableSize<480 ){
................................................................................
 66223  66515     Pager *pPager = pBt->pPager;
 66224  66516     int rc;
 66225  66517   
 66226  66518     assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || 
 66227  66519         eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
 66228  66520     assert( sqlite3_mutex_held(pBt->mutex) );
 66229  66521     assert( pDbPage->pBt==pBt );
        66522  +  if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT;
 66230  66523   
 66231  66524     /* Move page iDbPage from its current location to page number iFreePage */
 66232  66525     TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", 
 66233  66526         iDbPage, iFreePage, iPtrPage, eType));
 66234  66527     rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
 66235  66528     if( rc!=SQLITE_OK ){
 66236  66529       return rc;
................................................................................
 67394  67687             rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
 67395  67688           }
 67396  67689           offset -= ovflSize;
 67397  67690         }else{
 67398  67691           /* Need to read this page properly. It contains some of the
 67399  67692           ** range of data that is being read (eOp==0) or written (eOp!=0).
 67400  67693           */
 67401         -#ifdef SQLITE_DIRECT_OVERFLOW_READ
 67402         -        sqlite3_file *fd;      /* File from which to do direct overflow read */
 67403         -#endif
 67404  67694           int a = amt;
 67405  67695           if( a + offset > ovflSize ){
 67406  67696             a = ovflSize - offset;
 67407  67697           }
 67408  67698   
 67409  67699   #ifdef SQLITE_DIRECT_OVERFLOW_READ
 67410  67700           /* If all the following are true:
 67411  67701           **
 67412  67702           **   1) this is a read operation, and 
 67413  67703           **   2) data is required from the start of this overflow page, and
 67414         -        **   3) there is no open write-transaction, and
        67704  +        **   3) there are no dirty pages in the page-cache
 67415  67705           **   4) the database is file-backed, and
 67416  67706           **   5) the page is not in the WAL file
 67417  67707           **   6) at least 4 bytes have already been read into the output buffer 
 67418  67708           **
 67419  67709           ** then data can be read directly from the database file into the
 67420  67710           ** output buffer, bypassing the page-cache altogether. This speeds
 67421  67711           ** up loading large records that span many overflow pages.
 67422  67712           */
 67423  67713           if( eOp==0                                             /* (1) */
 67424  67714            && offset==0                                          /* (2) */
 67425         -         && pBt->inTransaction==TRANS_READ                     /* (3) */
 67426         -         && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (4) */
 67427         -         && 0==sqlite3PagerUseWal(pBt->pPager, nextPage)       /* (5) */
        67715  +         && sqlite3PagerDirectReadOk(pBt->pPager, nextPage)    /* (3,4,5) */
 67428  67716            && &pBuf[-4]>=pBufStart                               /* (6) */
 67429  67717           ){
        67718  +          sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
 67430  67719             u8 aSave[4];
 67431  67720             u8 *aWrite = &pBuf[-4];
 67432  67721             assert( aWrite>=pBufStart );                         /* due to (6) */
 67433  67722             memcpy(aSave, aWrite, 4);
 67434  67723             rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
 67435  67724             nextPage = get4byte(aWrite);
 67436  67725             memcpy(aWrite, aSave, 4);
................................................................................
 74018  74307     */
 74019  74308     if( fg & MEM_Int ){
 74020  74309       sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
 74021  74310     }else{
 74022  74311       assert( fg & MEM_Real );
 74023  74312       sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
 74024  74313     }
 74025         -  pMem->n = sqlite3Strlen30(pMem->z);
        74314  +  assert( pMem->z!=0 );
        74315  +  pMem->n = sqlite3Strlen30NN(pMem->z);
 74026  74316     pMem->enc = SQLITE_UTF8;
 74027  74317     pMem->flags |= MEM_Str|MEM_Term;
 74028  74318     if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
 74029  74319     sqlite3VdbeChangeEncoding(pMem, enc);
 74030  74320     return SQLITE_OK;
 74031  74321   }
 74032  74322   
................................................................................
 75593  75883     if( p==0 ) return;
 75594  75884     p->prepFlags = prepFlags;
 75595  75885     if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
 75596  75886       p->expmask = 0;
 75597  75887     }
 75598  75888     assert( p->zSql==0 );
 75599  75889     p->zSql = sqlite3DbStrNDup(p->db, z, n);
        75890  +#ifdef SQLITE_ENABLE_NORMALIZE
        75891  +  assert( p->zNormSql==0 );
        75892  +  if( p->zSql && (prepFlags & SQLITE_PREPARE_NORMALIZE)!=0 ){
        75893  +    sqlite3Normalize(p, p->zSql, n, prepFlags);
        75894  +    assert( p->zNormSql!=0 || p->db->mallocFailed );
        75895  +  }
        75896  +#endif
 75600  75897   }
 75601  75898   
 75602  75899   /*
 75603  75900   ** Swap all content between two VDBE structures.
 75604  75901   */
 75605  75902   SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
 75606  75903     Vdbe tmp, *pTmp;
................................................................................
 75614  75911     pB->pNext = pTmp;
 75615  75912     pTmp = pA->pPrev;
 75616  75913     pA->pPrev = pB->pPrev;
 75617  75914     pB->pPrev = pTmp;
 75618  75915     zTmp = pA->zSql;
 75619  75916     pA->zSql = pB->zSql;
 75620  75917     pB->zSql = zTmp;
        75918  +#ifdef SQLITE_ENABLE_NORMALIZE
        75919  +  zTmp = pA->zNormSql;
        75920  +  pA->zNormSql = pB->zNormSql;
        75921  +  pB->zNormSql = zTmp;
        75922  +#endif
 75621  75923     pB->expmask = pA->expmask;
 75622  75924     pB->prepFlags = pA->prepFlags;
 75623  75925     memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
 75624  75926     pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
 75625  75927   }
 75626  75928   
 75627  75929   /*
................................................................................
 78685  78987       releaseMemArray(p->aVar, p->nVar);
 78686  78988       sqlite3DbFree(db, p->pVList);
 78687  78989       sqlite3DbFree(db, p->pFree);
 78688  78990     }
 78689  78991     vdbeFreeOpArray(db, p->aOp, p->nOp);
 78690  78992     sqlite3DbFree(db, p->aColName);
 78691  78993     sqlite3DbFree(db, p->zSql);
        78994  +#ifdef SQLITE_ENABLE_NORMALIZE
        78995  +  sqlite3DbFree(db, p->zNormSql);
        78996  +#endif
 78692  78997   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 78693  78998     {
 78694  78999       int i;
 78695  79000       for(i=0; i<p->nScan; i++){
 78696  79001         sqlite3DbFree(db, p->aScan[i].zName);
 78697  79002       }
 78698  79003       sqlite3DbFree(db, p->aScan);
................................................................................
 80086  80391       return rc;
 80087  80392     }
 80088  80393   
 80089  80394     /* The index entry must begin with a header size */
 80090  80395     (void)getVarint32((u8*)m.z, szHdr);
 80091  80396     testcase( szHdr==3 );
 80092  80397     testcase( szHdr==m.n );
 80093         -  if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
        80398  +  testcase( szHdr>0x7fffffff );
        80399  +  assert( m.n>=0 );
        80400  +  if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){
 80094  80401       goto idx_rowid_corruption;
 80095  80402     }
 80096  80403   
 80097  80404     /* The last field of the index should be an integer - the ROWID.
 80098  80405     ** Verify that the last entry really is an integer. */
 80099  80406     (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
 80100  80407     testcase( typeRowid==1 );
................................................................................
 82097  82404       z = sqlite3VdbeExpandSql(p, zSql);
 82098  82405       sqlite3_mutex_leave(p->db->mutex);
 82099  82406     }
 82100  82407     return z;
 82101  82408   #endif
 82102  82409   }
 82103  82410   
        82411  +#ifdef SQLITE_ENABLE_NORMALIZE
        82412  +/*
        82413  +** Return the normalized SQL associated with a prepared statement.
        82414  +*/
        82415  +SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
        82416  +  Vdbe *p = (Vdbe *)pStmt;
        82417  +  return p ? p->zNormSql : 0;
        82418  +}
        82419  +#endif /* SQLITE_ENABLE_NORMALIZE */
        82420  +
 82104  82421   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 82105  82422   /*
 82106  82423   ** Allocate and populate an UnpackedRecord structure based on the serialized
 82107  82424   ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
 82108  82425   ** if successful, or a NULL pointer if an OOM error is encountered.
 82109  82426   */
 82110  82427   static UnpackedRecord *vdbeUnpackRecord(
................................................................................
 85536  85853     }else{
 85537  85854       /* Rare case of a really large header */
 85538  85855       nVarint = sqlite3VarintLen(nHdr);
 85539  85856       nHdr += nVarint;
 85540  85857       if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
 85541  85858     }
 85542  85859     nByte = nHdr+nData;
 85543         -  if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 85544         -    goto too_big;
 85545         -  }
 85546  85860   
 85547  85861     /* Make sure the output register has a buffer large enough to store 
 85548  85862     ** the new record. The output register (pOp->p3) is not allowed to
 85549  85863     ** be one of the input registers (because the following call to
 85550  85864     ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
 85551  85865     */
 85552         -  if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
 85553         -    goto no_mem;
        85866  +  if( nByte+nZero<=pOut->szMalloc ){
        85867  +    /* The output register is already large enough to hold the record.
        85868  +    ** No error checks or buffer enlargement is required */
        85869  +    pOut->z = pOut->zMalloc;
        85870  +  }else{
        85871  +    /* Need to make sure that the output is not too big and then enlarge
        85872  +    ** the output register to hold the full result */
        85873  +    if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
        85874  +      goto too_big;
        85875  +    }
        85876  +    if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
        85877  +      goto no_mem;
        85878  +    }
 85554  85879     }
 85555  85880     zNewRecord = (u8 *)pOut->z;
 85556  85881   
 85557  85882     /* Write the record */
 85558  85883     i = putVarint32(zNewRecord, nHdr);
 85559  85884     j = nHdr;
 85560  85885     assert( pData0<=pLast );
................................................................................
 88402  88727       db->mDbFlags |= DBFLAG_SchemaChange;
 88403  88728       p->expired = 0;
 88404  88729     }else
 88405  88730   #endif
 88406  88731     {
 88407  88732       zMaster = MASTER_NAME;
 88408  88733       initData.db = db;
 88409         -    initData.iDb = pOp->p1;
        88734  +    initData.iDb = iDb;
 88410  88735       initData.pzErrMsg = &p->zErrMsg;
 88411  88736       initData.mInitFlags = 0;
 88412  88737       zSql = sqlite3MPrintf(db,
 88413  88738          "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
 88414  88739          db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
 88415  88740       if( zSql==0 ){
 88416  88741         rc = SQLITE_NOMEM_BKPT;
................................................................................
 89599  89924   ** Synopsis: r[P3]=vcolumn(P2)
 89600  89925   **
 89601  89926   ** Store in register P3 the value of the P2-th column of
 89602  89927   ** the current row of the virtual-table of cursor P1.
 89603  89928   **
 89604  89929   ** If the VColumn opcode is being used to fetch the value of
 89605  89930   ** an unchanging column during an UPDATE operation, then the P5
 89606         -** value is 1.  Otherwise, P5 is 0.  The P5 value is returned
 89607         -** by sqlite3_vtab_nochange() routine and can be used
 89608         -** by virtual table implementations to return special "no-change"
 89609         -** marks which can be more efficient, depending on the virtual table.
        89931  +** value is OPFLAG_NOCHNG.  This will cause the sqlite3_vtab_nochange()
        89932  +** function to return true inside the xColumn method of the virtual
        89933  +** table implementation.  The P5 column might also contain other
        89934  +** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
        89935  +** unused by OP_VColumn.
 89610  89936   */
 89611  89937   case OP_VColumn: {
 89612  89938     sqlite3_vtab *pVtab;
 89613  89939     const sqlite3_module *pModule;
 89614  89940     Mem *pDest;
 89615  89941     sqlite3_context sContext;
 89616  89942   
................................................................................
 89624  89950       break;
 89625  89951     }
 89626  89952     pVtab = pCur->uc.pVCur->pVtab;
 89627  89953     pModule = pVtab->pModule;
 89628  89954     assert( pModule->xColumn );
 89629  89955     memset(&sContext, 0, sizeof(sContext));
 89630  89956     sContext.pOut = pDest;
 89631         -  if( pOp->p5 ){
        89957  +  testcase( (pOp->p5 & OPFLAG_NOCHNG)==0 && pOp->p5!=0 );
        89958  +  if( pOp->p5 & OPFLAG_NOCHNG ){
 89632  89959       sqlite3VdbeMemSetNull(pDest);
 89633  89960       pDest->flags = MEM_Null|MEM_Zero;
 89634  89961       pDest->u.nZero = 0;
 89635  89962     }else{
 89636  89963       MemSetTypeFlag(pDest, MEM_Null);
 89637  89964     }
 89638  89965     rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
................................................................................
 89701  90028   ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 89702  90029   ** This opcode invokes the corresponding xRename method. The value
 89703  90030   ** in register P1 is passed as the zName argument to the xRename method.
 89704  90031   */
 89705  90032   case OP_VRename: {
 89706  90033     sqlite3_vtab *pVtab;
 89707  90034     Mem *pName;
 89708         -
        90035  +  int isLegacy;
        90036  +  
        90037  +  isLegacy = (db->flags & SQLITE_LegacyAlter);
        90038  +  db->flags |= SQLITE_LegacyAlter;
 89709  90039     pVtab = pOp->p4.pVtab->pVtab;
 89710  90040     pName = &aMem[pOp->p1];
 89711  90041     assert( pVtab->pModule->xRename );
 89712  90042     assert( memIsValid(pName) );
 89713  90043     assert( p->readOnly==0 );
 89714  90044     REGISTER_TRACE(pOp->p1, pName);
 89715  90045     assert( pName->flags & MEM_Str );
 89716  90046     testcase( pName->enc==SQLITE_UTF8 );
 89717  90047     testcase( pName->enc==SQLITE_UTF16BE );
 89718  90048     testcase( pName->enc==SQLITE_UTF16LE );
 89719  90049     rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
 89720  90050     if( rc ) goto abort_due_to_error;
 89721  90051     rc = pVtab->pModule->xRename(pVtab, pName->z);
        90052  +  if( isLegacy==0 ) db->flags &= ~SQLITE_LegacyAlter;
 89722  90053     sqlite3VtabImportErrmsg(p, pVtab);
 89723  90054     p->expired = 0;
 89724  90055     if( rc ) goto abort_due_to_error;
 89725  90056     break;
 89726  90057   }
 89727  90058   #endif
 89728  90059   
................................................................................
 93979  94310           continue;
 93980  94311         }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 93981  94312           if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
 93982  94313         }else if( pExpr->x.pList ){
 93983  94314           if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
 93984  94315         }
 93985  94316   #ifndef SQLITE_OMIT_WINDOWFUNC
 93986         -      if( !ExprHasProperty(pExpr, EP_Reduced) && pExpr->pWin ){
 93987         -        Window *pWin = pExpr->pWin;
        94317  +      if( ExprHasProperty(pExpr, EP_WinFunc) ){
        94318  +        Window *pWin = pExpr->y.pWin;
 93988  94319           if( sqlite3WalkExprList(pWalker, pWin->pPartition) ) return WRC_Abort;
 93989  94320           if( sqlite3WalkExprList(pWalker, pWin->pOrderBy) ) return WRC_Abort;
 93990  94321           if( sqlite3WalkExpr(pWalker, pWin->pFilter) ) return WRC_Abort;
 93991  94322         }
 93992  94323   #endif
 93993  94324       }
 93994  94325       break;
................................................................................
 94253  94584   ** expression node refer back to that source column.  The following changes
 94254  94585   ** are made to pExpr:
 94255  94586   **
 94256  94587   **    pExpr->iDb           Set the index in db->aDb[] of the database X
 94257  94588   **                         (even if X is implied).
 94258  94589   **    pExpr->iTable        Set to the cursor number for the table obtained
 94259  94590   **                         from pSrcList.
 94260         -**    pExpr->pTab          Points to the Table structure of X.Y (even if
        94591  +**    pExpr->y.pTab        Points to the Table structure of X.Y (even if
 94261  94592   **                         X and/or Y are implied.)
 94262  94593   **    pExpr->iColumn       Set to the column number within the table.
 94263  94594   **    pExpr->op            Set to TK_COLUMN.
 94264  94595   **    pExpr->pLeft         Any expression this points to is deleted
 94265  94596   **    pExpr->pRight        Any expression this points to is deleted.
 94266  94597   **
 94267  94598   ** The zDb variable is the name of the database (the "X").  This value may be
................................................................................
 94297  94628   
 94298  94629     assert( pNC );     /* the name context cannot be NULL. */
 94299  94630     assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
 94300  94631     assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 94301  94632   
 94302  94633     /* Initialize the node to no-match */
 94303  94634     pExpr->iTable = -1;
 94304         -  pExpr->pTab = 0;
 94305  94635     ExprSetVVAProperty(pExpr, EP_NoReduce);
 94306  94636   
 94307  94637     /* Translate the schema name in zDb into a pointer to the corresponding
 94308  94638     ** schema.  If not found, pSchema will remain NULL and nothing will match
 94309  94639     ** resulting in an appropriate error message toward the end of this routine
 94310  94640     */
 94311  94641     if( zDb ){
................................................................................
 94359  94689           if( zTab ){
 94360  94690             const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
 94361  94691             assert( zTabName!=0 );
 94362  94692             if( sqlite3StrICmp(zTabName, zTab)!=0 ){
 94363  94693               continue;
 94364  94694             }
 94365  94695             if( IN_RENAME_OBJECT && pItem->zAlias ){
 94366         -            sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->pTab);
        94696  +            sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
 94367  94697             }
 94368  94698           }
 94369  94699           if( 0==(cntTab++) ){
 94370  94700             pMatch = pItem;
 94371  94701           }
 94372  94702           for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
 94373  94703             if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
................................................................................
 94385  94715               pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
 94386  94716               break;
 94387  94717             }
 94388  94718           }
 94389  94719         }
 94390  94720         if( pMatch ){
 94391  94721           pExpr->iTable = pMatch->iCursor;
 94392         -        pExpr->pTab = pMatch->pTab;
        94722  +        pExpr->y.pTab = pMatch->pTab;
 94393  94723           /* RIGHT JOIN not (yet) supported */
 94394  94724           assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
 94395  94725           if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
 94396  94726             ExprSetProperty(pExpr, EP_CanBeNull);
 94397  94727           }
 94398         -        pSchema = pExpr->pTab->pSchema;
        94728  +        pSchema = pExpr->y.pTab->pSchema;
 94399  94729         }
 94400  94730       } /* if( pSrcList ) */
 94401  94731   
 94402  94732   #if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
 94403  94733       /* If we have not already resolved the name, then maybe 
 94404  94734       ** it is a new.* or old.* trigger argument reference.  Or
 94405  94735       ** maybe it is an excluded.* from an upsert.
................................................................................
 94448  94778           if( iCol<pTab->nCol ){
 94449  94779             cnt++;
 94450  94780   #ifndef SQLITE_OMIT_UPSERT
 94451  94781             if( pExpr->iTable==2 ){
 94452  94782               testcase( iCol==(-1) );
 94453  94783               if( IN_RENAME_OBJECT ){
 94454  94784                 pExpr->iColumn = iCol;
 94455         -              pExpr->pTab = pTab;
        94785  +              pExpr->y.pTab = pTab;
 94456  94786                 eNewExprOp = TK_COLUMN;
 94457  94787               }else{
 94458  94788                 pExpr->iTable = pNC->uNC.pUpsert->regData + iCol;
 94459  94789                 eNewExprOp = TK_REGISTER;
 94460  94790                 ExprSetProperty(pExpr, EP_Alias);
 94461  94791               }
 94462  94792             }else
................................................................................
 94470  94800                 testcase( iCol==32 );
 94471  94801                 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 94472  94802               }else{
 94473  94803                 testcase( iCol==31 );
 94474  94804                 testcase( iCol==32 );
 94475  94805                 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 94476  94806               }
 94477         -            pExpr->pTab = pTab;
        94807  +            pExpr->y.pTab = pTab;
 94478  94808               pExpr->iColumn = (i16)iCol;
 94479  94809               eNewExprOp = TK_TRIGGER;
 94480  94810   #endif /* SQLITE_OMIT_TRIGGER */
 94481  94811             }
 94482  94812           }
 94483  94813         }
 94484  94814       }
................................................................................
 94570  94900     ** Because no reference was made to outer contexts, the pNC->nRef
 94571  94901     ** fields are not changed in any context.
 94572  94902     */
 94573  94903     if( cnt==0 && zTab==0 ){
 94574  94904       assert( pExpr->op==TK_ID );
 94575  94905       if( ExprHasProperty(pExpr,EP_DblQuoted) ){
 94576  94906         pExpr->op = TK_STRING;
 94577         -      pExpr->pTab = 0;
        94907  +      pExpr->y.pTab = 0;
 94578  94908         return WRC_Prune;
 94579  94909       }
 94580  94910       if( sqlite3ExprIdToTrueFalse(pExpr) ){
 94581  94911         return WRC_Prune;
 94582  94912       }
 94583  94913     }
 94584  94914   
................................................................................
 94648  94978   ** Allocate and return a pointer to an expression to load the column iCol
 94649  94979   ** from datasource iSrc in SrcList pSrc.
 94650  94980   */
 94651  94981   SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
 94652  94982     Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
 94653  94983     if( p ){
 94654  94984       struct SrcList_item *pItem = &pSrc->a[iSrc];
 94655         -    p->pTab = pItem->pTab;
        94985  +    p->y.pTab = pItem->pTab;
 94656  94986       p->iTable = pItem->iCursor;
 94657         -    if( p->pTab->iPKey==iCol ){
        94987  +    if( p->y.pTab->iPKey==iCol ){
 94658  94988         p->iColumn = -1;
 94659  94989       }else{
 94660  94990         p->iColumn = (ynVar)iCol;
 94661  94991         testcase( iCol==BMS );
 94662  94992         testcase( iCol==BMS-1 );
 94663  94993         pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
 94664  94994       }
................................................................................
 94740  95070       case TK_ROW: {
 94741  95071         SrcList *pSrcList = pNC->pSrcList;
 94742  95072         struct SrcList_item *pItem;
 94743  95073         assert( pSrcList && pSrcList->nSrc==1 );
 94744  95074         pItem = pSrcList->a;
 94745  95075         assert( HasRowid(pItem->pTab) && pItem->pTab->pSelect==0 );
 94746  95076         pExpr->op = TK_COLUMN;
 94747         -      pExpr->pTab = pItem->pTab;
        95077  +      pExpr->y.pTab = pItem->pTab;
 94748  95078         pExpr->iTable = pItem->iCursor;
 94749  95079         pExpr->iColumn = -1;
 94750  95080         pExpr->affinity = SQLITE_AFF_INTEGER;
 94751  95081         break;
 94752  95082       }
 94753  95083   #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
 94754  95084             && !defined(SQLITE_OMIT_SUBQUERY) */
................................................................................
 94784  95114             pLeft = pRight->pLeft;
 94785  95115             pRight = pRight->pRight;
 94786  95116           }
 94787  95117           zTable = pLeft->u.zToken;
 94788  95118           zColumn = pRight->u.zToken;
 94789  95119           if( IN_RENAME_OBJECT ){
 94790  95120             sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
 94791         -        }
 94792         -        if( IN_RENAME_OBJECT ){
 94793         -          sqlite3RenameTokenRemap(pParse, (void*)&pExpr->pTab, (void*)pLeft);
        95121  +          sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
 94794  95122           }
 94795  95123         }
 94796  95124         return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
 94797  95125       }
 94798  95126   
 94799  95127       /* Resolve function names
 94800  95128       */
................................................................................
 94868  95196           if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
 94869  95197             /* Date/time functions that use 'now', and other functions like
 94870  95198             ** sqlite_version() that might change over time cannot be used
 94871  95199             ** in an index. */
 94872  95200             notValid(pParse, pNC, "non-deterministic functions",
 94873  95201                      NC_IdxExpr|NC_PartIdx);
 94874  95202           }
        95203  +        if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
        95204  +         && pParse->nested==0
        95205  +         && sqlite3Config.bInternalFunctions==0
        95206  +        ){
        95207  +          /* Internal-use-only functions are disallowed unless the
        95208  +          ** SQL is being compiled using sqlite3NestedParse() */
        95209  +          no_such_func = 1;
        95210  +          pDef = 0;
        95211  +        }
 94875  95212         }
 94876  95213   
 94877  95214         if( 0==IN_RENAME_OBJECT ){
 94878  95215   #ifndef SQLITE_OMIT_WINDOWFUNC
 94879  95216           assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
 94880  95217             || (pDef->xValue==0 && pDef->xInverse==0)
 94881  95218             || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
 94882  95219           );
 94883         -        if( pDef && pDef->xValue==0 && pExpr->pWin ){
        95220  +        if( pDef && pDef->xValue==0 && ExprHasProperty(pExpr, EP_WinFunc) ){
 94884  95221             sqlite3ErrorMsg(pParse, 
 94885  95222                 "%.*s() may not be used as a window function", nId, zId
 94886  95223             );
 94887  95224             pNC->nErr++;
 94888  95225           }else if( 
 94889  95226                 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
 94890         -           || (is_agg && (pDef->funcFlags & SQLITE_FUNC_WINDOW) && !pExpr->pWin)
 94891         -           || (is_agg && pExpr->pWin && (pNC->ncFlags & NC_AllowWin)==0)
        95227  +           || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pExpr->y.pWin)
        95228  +           || (is_agg && pExpr->y.pWin && (pNC->ncFlags & NC_AllowWin)==0)
 94892  95229           ){
 94893  95230             const char *zType;
 94894         -          if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pExpr->pWin ){
        95231  +          if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pExpr->y.pWin ){
 94895  95232               zType = "window";
 94896  95233             }else{
 94897  95234               zType = "aggregate";
 94898  95235             }
 94899  95236             sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId);
 94900  95237             pNC->nErr++;
 94901  95238             is_agg = 0;
................................................................................
 94917  95254           }else if( wrong_num_args ){
 94918  95255             sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
 94919  95256                  nId, zId);
 94920  95257             pNC->nErr++;
 94921  95258           }
 94922  95259           if( is_agg ){
 94923  95260   #ifndef SQLITE_OMIT_WINDOWFUNC
 94924         -          pNC->ncFlags &= ~(pExpr->pWin ? NC_AllowWin : NC_AllowAgg);
        95261  +          pNC->ncFlags &= ~(pExpr->y.pWin ? NC_AllowWin : NC_AllowAgg);
 94925  95262   #else
 94926  95263             pNC->ncFlags &= ~NC_AllowAgg;
 94927  95264   #endif
 94928  95265           }
 94929  95266         }
 94930  95267         sqlite3WalkExprList(pWalker, pList);
 94931  95268         if( is_agg ){
 94932  95269   #ifndef SQLITE_OMIT_WINDOWFUNC
 94933         -        if( pExpr->pWin ){
        95270  +        if( pExpr->y.pWin ){
 94934  95271             Select *pSel = pNC->pWinSelect;
 94935         -          sqlite3WalkExprList(pWalker, pExpr->pWin->pPartition);
 94936         -          sqlite3WalkExprList(pWalker, pExpr->pWin->pOrderBy);
 94937         -          sqlite3WalkExpr(pWalker, pExpr->pWin->pFilter);
 94938         -          sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->pWin, pDef);
        95272  +          sqlite3WalkExprList(pWalker, pExpr->y.pWin->pPartition);
        95273  +          sqlite3WalkExprList(pWalker, pExpr->y.pWin->pOrderBy);
        95274  +          sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
        95275  +          sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef);
 94939  95276             if( 0==pSel->pWin 
 94940         -           || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->pWin) 
        95277  +           || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin) 
 94941  95278             ){
 94942         -            pExpr->pWin->pNextWin = pSel->pWin;
 94943         -            pSel->pWin = pExpr->pWin;
        95279  +            pExpr->y.pWin->pNextWin = pSel->pWin;
        95280  +            pSel->pWin = pExpr->y.pWin;
 94944  95281             }
 94945  95282             pNC->ncFlags |= NC_AllowWin;
 94946  95283           }else
 94947  95284   #endif /* SQLITE_OMIT_WINDOWFUNC */
 94948  95285           {
 94949  95286             NameContext *pNC2 = pNC;
 94950  95287             pExpr->op = TK_AGG_FUNCTION;
................................................................................
 95359  95696       pItem->u.x.iOrderByCol = 0;
 95360  95697       if( sqlite3ResolveExprNames(pNC, pE) ){
 95361  95698         return 1;
 95362  95699       }
 95363  95700       for(j=0; j<pSelect->pEList->nExpr; j++){
 95364  95701         if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
 95365  95702   #ifndef SQLITE_OMIT_WINDOWFUNC
 95366         -        if( pE->pWin ){
        95703  +        if( ExprHasProperty(pE, EP_WinFunc) ){
 95367  95704             /* Since this window function is being changed into a reference
 95368  95705             ** to the same window function the result set, remove the instance
 95369  95706             ** of this window function from the Select.pWin list. */
 95370  95707             Window **pp;
 95371  95708             for(pp=&pSelect->pWin; *pp; pp=&(*pp)->pNextWin){
 95372         -            if( *pp==pE->pWin ){
        95709  +            if( *pp==pE->y.pWin ){
 95373  95710                 *pp = (*pp)->pNextWin;
 95374  95711               }    
 95375  95712             }
 95376  95713           }
 95377  95714   #endif
 95378  95715           pItem->u.x.iOrderByCol = j+1;
 95379  95716         }
................................................................................
 95828  96165     if( op==TK_REGISTER ) op = pExpr->op2;
 95829  96166   #ifndef SQLITE_OMIT_CAST
 95830  96167     if( op==TK_CAST ){
 95831  96168       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 95832  96169       return sqlite3AffinityType(pExpr->u.zToken, 0);
 95833  96170     }
 95834  96171   #endif
 95835         -  if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->pTab ){
 95836         -    return sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
        96172  +  if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->y.pTab ){
        96173  +    return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
 95837  96174     }
 95838  96175     if( op==TK_SELECT_COLUMN ){
 95839  96176       assert( pExpr->pLeft->flags&EP_xIsSelect );
 95840  96177       return sqlite3ExprAffinity(
 95841  96178           pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
 95842  96179       );
 95843  96180     }
................................................................................
 95913  96250     CollSeq *pColl = 0;
 95914  96251     Expr *p = pExpr;
 95915  96252     while( p ){
 95916  96253       int op = p->op;
 95917  96254       if( p->flags & EP_Generic ) break;
 95918  96255       if( (op==TK_AGG_COLUMN || op==TK_COLUMN
 95919  96256             || op==TK_REGISTER || op==TK_TRIGGER)
 95920         -     && p->pTab!=0
        96257  +     && p->y.pTab!=0
 95921  96258       ){
 95922         -      /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
        96259  +      /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
 95923  96260         ** a TK_COLUMN but was previously evaluated and cached in a register */
 95924  96261         int j = p->iColumn;
 95925  96262         if( j>=0 ){
 95926         -        const char *zColl = p->pTab->aCol[j].zColl;
        96263  +        const char *zColl = p->y.pTab->aCol[j].zColl;
 95927  96264           pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
 95928  96265         }
 95929  96266         break;
 95930  96267       }
 95931  96268       if( op==TK_CAST || op==TK_UPLUS ){
 95932  96269         p = p->pLeft;
 95933  96270         continue;
................................................................................
 96822  97159   /*
 96823  97160   ** Recursively delete an expression tree.
 96824  97161   */
 96825  97162   static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
 96826  97163     assert( p!=0 );
 96827  97164     /* Sanity check: Assert that the IntValue is non-negative if it exists */
 96828  97165     assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
        97166  +
        97167  +  assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed );
        97168  +  assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced)
        97169  +          || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) );
 96829  97170   #ifdef SQLITE_DEBUG
 96830  97171     if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
 96831  97172       assert( p->pLeft==0 );
 96832  97173       assert( p->pRight==0 );
 96833  97174       assert( p->x.pSelect==0 );
 96834  97175     }
 96835  97176   #endif
................................................................................
 96840  97181       if( p->pRight ){
 96841  97182         sqlite3ExprDeleteNN(db, p->pRight);
 96842  97183       }else if( ExprHasProperty(p, EP_xIsSelect) ){
 96843  97184         sqlite3SelectDelete(db, p->x.pSelect);
 96844  97185       }else{
 96845  97186         sqlite3ExprListDelete(db, p->x.pList);
 96846  97187       }
 96847         -    if( !ExprHasProperty(p, EP_Reduced) ){
 96848         -      sqlite3WindowDelete(db, p->pWin);
        97188  +    if( ExprHasProperty(p, EP_WinFunc) ){
        97189  +      assert( p->op==TK_FUNCTION );
        97190  +      sqlite3WindowDelete(db, p->y.pWin);
 96849  97191       }
 96850  97192     }
 96851  97193     if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
 96852  97194     if( !ExprHasProperty(p, EP_Static) ){
 96853  97195       sqlite3DbFreeNN(db, p);
 96854  97196     }
 96855  97197   }
................................................................................
 96905  97247   static int dupedExprStructSize(Expr *p, int flags){
 96906  97248     int nSize;
 96907  97249     assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
 96908  97250     assert( EXPR_FULLSIZE<=0xfff );
 96909  97251     assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
 96910  97252     if( 0==flags || p->op==TK_SELECT_COLUMN 
 96911  97253   #ifndef SQLITE_OMIT_WINDOWFUNC
 96912         -   || p->pWin 
        97254  +   || ExprHasProperty(p, EP_WinFunc)
 96913  97255   #endif
 96914  97256     ){
 96915  97257       nSize = EXPR_FULLSIZE;
 96916  97258     }else{
 96917  97259       assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
 96918  97260       assert( !ExprHasProperty(p, EP_FromJoin) ); 
 96919  97261       assert( !ExprHasProperty(p, EP_MemToken) );
................................................................................
 96932  97274   ** This function returns the space in bytes required to store the copy 
 96933  97275   ** of the Expr structure and a copy of the Expr.u.zToken string (if that
 96934  97276   ** string is defined.)
 96935  97277   */
 96936  97278   static int dupedExprNodeSize(Expr *p, int flags){
 96937  97279     int nByte = dupedExprStructSize(p, flags) & 0xfff;
 96938  97280     if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
 96939         -    nByte += sqlite3Strlen30(p->u.zToken)+1;
        97281  +    nByte += sqlite3Strlen30NN(p->u.zToken)+1;
 96940  97282     }
 96941  97283     return ROUND8(nByte);
 96942  97284   }
 96943  97285   
 96944  97286   /*
 96945  97287   ** Return the number of bytes required to create a duplicate of the 
 96946  97288   ** expression passed as the first argument. The second argument is a
................................................................................
 97035  97377           pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
 97036  97378         }else{
 97037  97379           pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
 97038  97380         }
 97039  97381       }
 97040  97382   
 97041  97383       /* Fill in pNew->pLeft and pNew->pRight. */
 97042         -    if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
        97384  +    if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){
 97043  97385         zAlloc += dupedExprNodeSize(p, dupFlags);
 97044  97386         if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
 97045  97387           pNew->pLeft = p->pLeft ?
 97046  97388                         exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
 97047  97389           pNew->pRight = p->pRight ?
 97048  97390                          exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
 97049  97391         }
        97392  +#ifndef SQLITE_OMIT_WINDOWFUNC
        97393  +      if( ExprHasProperty(p, EP_WinFunc) ){
        97394  +        pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
        97395  +        assert( ExprHasProperty(pNew, EP_WinFunc) );
        97396  +      }
        97397  +#endif /* SQLITE_OMIT_WINDOWFUNC */
 97050  97398         if( pzBuffer ){
 97051  97399           *pzBuffer = zAlloc;
 97052  97400         }
 97053  97401       }else{
 97054         -#ifndef SQLITE_OMIT_WINDOWFUNC
 97055         -      if( ExprHasProperty(p, EP_Reduced|EP_TokenOnly) ){
 97056         -        pNew->pWin = 0;
 97057         -      }else{
 97058         -        pNew->pWin = sqlite3WindowDup(db, pNew, p->pWin);
 97059         -      }
 97060         -#endif /* SQLITE_OMIT_WINDOWFUNC */
 97061  97402         if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
 97062  97403           if( pNew->op==TK_SELECT_COLUMN ){
 97063  97404             pNew->pLeft = p->pLeft;
 97064  97405             assert( p->iColumn==0 || p->pRight==0 );
 97065  97406             assert( p->pRight==0  || p->pRight==p->pLeft );
 97066  97407           }else{
 97067  97408             pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
................................................................................
 97859  98200       case TK_INTEGER:
 97860  98201       case TK_STRING:
 97861  98202       case TK_FLOAT:
 97862  98203       case TK_BLOB:
 97863  98204         return 0;
 97864  98205       case TK_COLUMN:
 97865  98206         return ExprHasProperty(p, EP_CanBeNull) ||
 97866         -             p->pTab==0 ||  /* Reference to column of index on expression */
 97867         -             (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
        98207  +             p->y.pTab==0 ||  /* Reference to column of index on expression */
        98208  +             (p->iColumn>=0 && p->y.pTab->aCol[p->iColumn].notNull==0);
 97868  98209       default:
 97869  98210         return 1;
 97870  98211     }
 97871  98212   }
 97872  98213   
 97873  98214   /*
 97874  98215   ** Return TRUE if the given expression is a constant which would be
................................................................................
 97915  98256   */
 97916  98257   SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
 97917  98258     if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
 97918  98259     if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
 97919  98260     if( sqlite3StrICmp(z, "OID")==0 ) return 1;
 97920  98261     return 0;
 97921  98262   }
        98263  +#ifdef SQLITE_ENABLE_NORMALIZE
        98264  +SQLITE_PRIVATE int sqlite3IsRowidN(const char *z, int n){
        98265  +  if( sqlite3StrNICmp(z, "_ROWID_", n)==0 ) return 1;
        98266  +  if( sqlite3StrNICmp(z, "ROWID", n)==0 ) return 1;
        98267  +  if( sqlite3StrNICmp(z, "OID", n)==0 ) return 1;
        98268  +  return 0;
        98269  +}
        98270  +#endif
 97922  98271   
 97923  98272   /*
 97924  98273   ** pX is the RHS of an IN operator.  If pX is a SELECT statement 
 97925  98274   ** that can be simplified to a direct table access, then return
 97926  98275   ** a pointer to the SELECT statement.  If pX is not a SELECT statement,
 97927  98276   ** or if the SELECT statement needs to be manifested into a transient
 97928  98277   ** table, then return NULL.
................................................................................
 99148  99497           /* This COLUMN expression is really a constant due to WHERE clause
 99149  99498           ** constraints, and that constant is coded by the pExpr->pLeft
 99150  99499           ** expresssion.  However, make sure the constant has the correct
 99151  99500           ** datatype by applying the Affinity of the table column to the
 99152  99501           ** constant.
 99153  99502           */
 99154  99503           int iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
 99155         -        int aff = sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
        99504  +        int aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
 99156  99505           if( aff!=SQLITE_AFF_BLOB ){
 99157  99506             static const char zAff[] = "B\000C\000D\000E";
 99158  99507             assert( SQLITE_AFF_BLOB=='A' );
 99159  99508             assert( SQLITE_AFF_TEXT=='B' );
 99160  99509             if( iReg!=target ){
 99161  99510               sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target);
 99162  99511               iReg = target;
................................................................................
 99172  99521             return pExpr->iColumn - pParse->iSelfTab;
 99173  99522           }else{
 99174  99523             /* Coding an expression that is part of an index where column names
 99175  99524             ** in the index refer to the table to which the index belongs */
 99176  99525             iTab = pParse->iSelfTab - 1;
 99177  99526           }
 99178  99527         }
 99179         -      return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
        99528  +      return sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
 99180  99529                                  pExpr->iColumn, iTab, target,
 99181  99530                                  pExpr->op2);
 99182  99531       }
 99183  99532       case TK_INTEGER: {
 99184  99533         codeInteger(pParse, pExpr, 0, target);
 99185  99534         return target;
 99186  99535       }
................................................................................
 99386  99735         u32 constMask = 0;     /* Mask of function arguments that are constant */
 99387  99736         int i;                 /* Loop counter */
 99388  99737         sqlite3 *db = pParse->db;  /* The database connection */
 99389  99738         u8 enc = ENC(db);      /* The text encoding used by this database */
 99390  99739         CollSeq *pColl = 0;    /* A collating sequence */
 99391  99740   
 99392  99741   #ifndef SQLITE_OMIT_WINDOWFUNC
 99393         -      if( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) && pExpr->pWin ){
 99394         -        return pExpr->pWin->regResult;
        99742  +      if( ExprHasProperty(pExpr, EP_WinFunc) ){
        99743  +        return pExpr->y.pWin->regResult;
 99395  99744         }
 99396  99745   #endif
 99397  99746   
 99398  99747         if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
 99399  99748           /* SQL functions can be expensive. So try to move constant functions
 99400  99749           ** out of the inner loop, even if that means an extra OP_Copy. */
 99401  99750           return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
................................................................................
 99630  99979         **
 99631  99980         ** Then p1 is interpreted as follows:
 99632  99981         **
 99633  99982         **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
 99634  99983         **   p1==1   ->    old.a         p1==4   ->    new.a
 99635  99984         **   p1==2   ->    old.b         p1==5   ->    new.b       
 99636  99985         */
 99637         -      Table *pTab = pExpr->pTab;
        99986  +      Table *pTab = pExpr->y.pTab;
 99638  99987         int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
 99639  99988   
 99640  99989         assert( pExpr->iTable==0 || pExpr->iTable==1 );
 99641  99990         assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
 99642  99991         assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
 99643  99992         assert( p1>=0 && p1<(pTab->nCol*2+2) );
 99644  99993   
 99645  99994         sqlite3VdbeAddOp2(v, OP_Param, p1, target);
 99646  99995         VdbeComment((v, "r[%d]=%s.%s", target,
 99647  99996           (pExpr->iTable ? "new" : "old"),
 99648         -        (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName)
        99997  +        (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[pExpr->iColumn].zName)
 99649  99998         ));
 99650  99999   
 99651 100000   #ifndef SQLITE_OMIT_FLOATING_POINT
 99652 100001         /* If the column has REAL affinity, it may currently be stored as an
 99653 100002         ** integer. Use OP_RealAffinity to make sure it is really real.
 99654 100003         **
 99655 100004         ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
................................................................................
100492 100841         return 1;
100493 100842       }
100494 100843       return 2;
100495 100844     }
100496 100845     if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
100497 100846       if( pA->op==TK_FUNCTION ){
100498 100847         if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
       100848  +#ifndef SQLITE_OMIT_WINDOWFUNC
       100849  +      /* Justification for the assert():
       100850  +      ** window functions have p->op==TK_FUNCTION but aggregate functions
       100851  +      ** have p->op==TK_AGG_FUNCTION.  So any comparison between an aggregate
       100852  +      ** function and a window function should have failed before reaching
       100853  +      ** this point.  And, it is not possible to have a window function and
       100854  +      ** a scalar function with the same name and number of arguments.  So
       100855  +      ** if we reach this point, either A and B both window functions or
       100856  +      ** neither are a window functions. */
       100857  +      assert( ExprHasProperty(pA,EP_WinFunc)==ExprHasProperty(pB,EP_WinFunc) );
       100858  +      if( ExprHasProperty(pA,EP_WinFunc) ){
       100859  +        if( sqlite3WindowCompare(pParse,pA->y.pWin,pB->y.pWin)!=0 ) return 2;
       100860  +      }
       100861  +#endif
100499 100862       }else if( pA->op==TK_COLLATE ){
100500 100863         if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
100501 100864       }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
100502 100865         return 2;
100503 100866       }
100504 100867     }
100505 100868     if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
................................................................................
100511 100874       if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
100512 100875       assert( (combinedFlags & EP_Reduced)==0 );
100513 100876       if( pA->op!=TK_STRING && pA->op!=TK_TRUEFALSE ){
100514 100877         if( pA->iColumn!=pB->iColumn ) return 2;
100515 100878         if( pA->iTable!=pB->iTable 
100516 100879          && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
100517 100880       }
100518         -#ifndef SQLITE_OMIT_WINDOWFUNC
100519         -    /* Justification for the assert():
100520         -    ** window functions have p->op==TK_FUNCTION but aggregate functions
100521         -    ** have p->op==TK_AGG_FUNCTION.  So any comparison between an aggregate
100522         -    ** function and a window function should have failed before reaching
100523         -    ** this point.  And, it is not possible to have a window function and
100524         -    ** a scalar function with the same name and number of arguments.  So
100525         -    ** if we reach this point, either A and B both window functions or
100526         -    ** neither are a window functions. */
100527         -    assert( (pA->pWin==0)==(pB->pWin==0) );
100528         -
100529         -    if( pA->pWin!=0 ){
100530         -      if( sqlite3WindowCompare(pParse,pA->pWin,pB->pWin)!=0 ) return 2;
100531         -    }
100532         -#endif
100533 100881     }
100534 100882     return 0;
100535 100883   }
100536 100884   
100537 100885   /*
100538 100886   ** Compare two ExprList objects.  Return 0 if they are identical and 
100539 100887   ** non-zero if they differ in any way.
................................................................................
100616 100964     }
100617 100965     return 0;
100618 100966   }
100619 100967   
100620 100968   /*
100621 100969   ** This is the Expr node callback for sqlite3ExprImpliesNotNullRow().
100622 100970   ** If the expression node requires that the table at pWalker->iCur
100623         -** have a non-NULL column, then set pWalker->eCode to 1 and abort.
       100971  +** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
       100972  +**
       100973  +** This routine controls an optimization.  False positives (setting
       100974  +** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
       100975  +** (never setting pWalker->eCode) is a harmless missed optimization.
100624 100976   */
100625 100977   static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
100626         -  /* This routine is only called for WHERE clause expressions and so it
100627         -  ** cannot have any TK_AGG_COLUMN entries because those are only found
100628         -  ** in HAVING clauses.  We can get a TK_AGG_FUNCTION in a WHERE clause,
100629         -  ** but that is an illegal construct and the query will be rejected at
100630         -  ** a later stage of processing, so the TK_AGG_FUNCTION case does not
100631         -  ** need to be considered here. */
100632         -  assert( pExpr->op!=TK_AGG_COLUMN );
       100978  +  testcase( pExpr->op==TK_AGG_COLUMN );
100633 100979     testcase( pExpr->op==TK_AGG_FUNCTION );
100634         -
100635 100980     if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
100636 100981     switch( pExpr->op ){
100637 100982       case TK_ISNOT:
100638 100983       case TK_NOT:
100639 100984       case TK_ISNULL:
100640 100985       case TK_IS:
100641 100986       case TK_OR:
................................................................................
100669 101014       case TK_GE:
100670 101015         testcase( pExpr->op==TK_EQ );
100671 101016         testcase( pExpr->op==TK_NE );
100672 101017         testcase( pExpr->op==TK_LT );
100673 101018         testcase( pExpr->op==TK_LE );
100674 101019         testcase( pExpr->op==TK_GT );
100675 101020         testcase( pExpr->op==TK_GE );
100676         -      if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->pTab))
100677         -       || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->pTab))
       101021  +      if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->y.pTab))
       101022  +       || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->y.pTab))
100678 101023         ){
100679 101024          return WRC_Prune;
100680 101025         }
100681 101026       default:
100682 101027         return WRC_Continue;
100683 101028     }
100684 101029   }
................................................................................
100901 101246                   break;
100902 101247                 }
100903 101248               }
100904 101249               if( (k>=pAggInfo->nColumn)
100905 101250                && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 
100906 101251               ){
100907 101252                 pCol = &pAggInfo->aCol[k];
100908         -              pCol->pTab = pExpr->pTab;
       101253  +              pCol->pTab = pExpr->y.pTab;
100909 101254                 pCol->iTable = pExpr->iTable;
100910 101255                 pCol->iColumn = pExpr->iColumn;
100911 101256                 pCol->iMem = ++pParse->nMem;
100912 101257                 pCol->iSorterColumn = -1;
100913 101258                 pCol->pExpr = pExpr;
100914 101259                 if( pAggInfo->pGroupBy ){
100915 101260                   int j, n;
................................................................................
101160 101505   /*
101161 101506   ** Generate code to verify that the schemas of database zDb and, if
101162 101507   ** bTemp is not true, database "temp", can still be parsed. This is
101163 101508   ** called at the end of the generation of an ALTER TABLE ... RENAME ...
101164 101509   ** statement to ensure that the operation has not rendered any schema
101165 101510   ** objects unusable.
101166 101511   */
101167         -void renameTestSchema(Parse *pParse, const char *zDb, int bTemp){
       101512  +static void renameTestSchema(Parse *pParse, const char *zDb, int bTemp){
101168 101513     sqlite3NestedParse(pParse, 
101169 101514         "SELECT 1 "
101170 101515         "FROM \"%w\".%s "
101171 101516         "WHERE name NOT LIKE 'sqlite_%%'"
101172 101517         " AND sql NOT LIKE 'create virtual%%'"
101173 101518         " AND sqlite_rename_test(%Q, sql, type, name, %d)=NULL ",
101174 101519         zDb, MASTER_NAME, 
................................................................................
101187 101532     }
101188 101533   }
101189 101534   
101190 101535   /*
101191 101536   ** Generate code to reload the schema for database iDb. And, if iDb!=1, for
101192 101537   ** the temp database as well.
101193 101538   */
101194         -void renameReloadSchema(Parse *pParse, int iDb){
       101539  +static void renameReloadSchema(Parse *pParse, int iDb){
101195 101540     Vdbe *v = pParse->pVdbe;
101196 101541     if( v ){
101197 101542       sqlite3ChangeCookie(pParse, iDb);
101198 101543       sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0);
101199 101544       if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0);
101200 101545     }
101201 101546   }
................................................................................
101286 101631     ** table.
101287 101632     */
101288 101633     v = sqlite3GetVdbe(pParse);
101289 101634     if( v==0 ){
101290 101635       goto exit_rename_table;
101291 101636     }
101292 101637   
101293         -  /* If this is a virtual table, invoke the xRename() function if
101294         -  ** one is defined. The xRename() callback will modify the names
101295         -  ** of any resources used by the v-table implementation (including other
101296         -  ** SQLite tables) that are identified by the name of the virtual table.
101297         -  */
101298         -#ifndef SQLITE_OMIT_VIRTUALTABLE
101299         -  if( pVTab ){
101300         -    int i = ++pParse->nMem;
101301         -    sqlite3VdbeLoadString(v, i, zName);
101302         -    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
101303         -    sqlite3MayAbort(pParse);
101304         -  }
101305         -#endif
101306         -
101307 101638     /* figure out how many UTF-8 characters are in zName */
101308 101639     zTabName = pTab->zName;
101309 101640     nTabName = sqlite3Utf8CharLen(zTabName, -1);
101310 101641   
101311 101642     /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
101312 101643     ** the schema to use the new table name.  */
101313 101644     sqlite3NestedParse(pParse, 
................................................................................
101356 101687               "tbl_name = "
101357 101688                 "CASE WHEN tbl_name=%Q COLLATE nocase AND "
101358 101689                 "          sqlite_rename_test(%Q, sql, type, name, 1) "
101359 101690                 "THEN %Q ELSE tbl_name END "
101360 101691               "WHERE type IN ('view', 'trigger')"
101361 101692           , zDb, zTabName, zName, zTabName, zDb, zName);
101362 101693     }
       101694  +
       101695  +  /* If this is a virtual table, invoke the xRename() function if
       101696  +  ** one is defined. The xRename() callback will modify the names
       101697  +  ** of any resources used by the v-table implementation (including other
       101698  +  ** SQLite tables) that are identified by the name of the virtual table.
       101699  +  */
       101700  +#ifndef SQLITE_OMIT_VIRTUALTABLE
       101701  +  if( pVTab ){
       101702  +    int i = ++pParse->nMem;
       101703  +    sqlite3VdbeLoadString(v, i, zName);
       101704  +    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
       101705  +    sqlite3MayAbort(pParse);
       101706  +  }
       101707  +#endif
101363 101708   
101364 101709     renameReloadSchema(pParse, iDb);
101365 101710     renameTestSchema(pParse, zDb, iDb==1);
101366 101711   
101367 101712   exit_rename_table:
101368 101713     sqlite3SrcListDelete(db, pSrc);
101369 101714     sqlite3DbFree(db, zName);
................................................................................
101767 102112   **
101768 102113   **     sqlite3_free(x);
101769 102114   **     if( x==y ) ...
101770 102115   **
101771 102116   ** Technically, as x no longer points into a valid object or to the byte
101772 102117   ** following a valid object, it may not be used in comparison operations.
101773 102118   */
101774         -void renameTokenCheckAll(Parse *pParse, void *pPtr){
       102119  +static void renameTokenCheckAll(Parse *pParse, void *pPtr){
101775 102120     if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){
101776 102121       RenameToken *p;
101777 102122       u8 i = 0;
101778 102123       for(p=pParse->pRename; p; p=p->pNext){
101779 102124         if( p->p ){
101780 102125           assert( p->p!=pPtr );
101781 102126           i += *(u8*)(p->p);
................................................................................
101784 102129     }
101785 102130   }
101786 102131   #else
101787 102132   # define renameTokenCheckAll(x,y)
101788 102133   #endif
101789 102134   
101790 102135   /*
101791         -** Add a new RenameToken object mapping parse tree element pPtr into
101792         -** token *pToken to the Parse object currently under construction.
       102136  +** Remember that the parser tree element pPtr was created using
       102137  +** the token pToken.
101793 102138   **
101794         -** Return a copy of pPtr.
       102139  +** In other words, construct a new RenameToken object and add it
       102140  +** to the list of RenameToken objects currently being built up
       102141  +** in pParse->pRename.
       102142  +**
       102143  +** The pPtr argument is returned so that this routine can be used
       102144  +** with tail recursion in tokenExpr() routine, for a small performance
       102145  +** improvement.
101795 102146   */
101796 102147   SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
101797 102148     RenameToken *pNew;
101798 102149     assert( pPtr || pParse->db->mallocFailed );
101799 102150     renameTokenCheckAll(pParse, pPtr);
101800 102151     pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
101801 102152     if( pNew ){
................................................................................
101920 102271     if( pExpr->op==TK_TRIGGER 
101921 102272      && pExpr->iColumn==p->iCol 
101922 102273      && pWalker->pParse->pTriggerTab==p->pTab
101923 102274     ){
101924 102275       renameTokenFind(pWalker->pParse, p, (void*)pExpr);
101925 102276     }else if( pExpr->op==TK_COLUMN 
101926 102277      && pExpr->iColumn==p->iCol 
101927         -   && p->pTab==pExpr->pTab
       102278  +   && p->pTab==pExpr->y.pTab
101928 102279     ){
101929 102280       renameTokenFind(pWalker->pParse, p, (void*)pExpr);
101930 102281     }
101931 102282     return WRC_Continue;
101932 102283   }
101933 102284   
101934 102285   /*
................................................................................
102178 102529     memset(&sNC, 0, sizeof(sNC));
102179 102530     sNC.pParse = pParse;
102180 102531     assert( pNew->pTabSchema );
102181 102532     pParse->pTriggerTab = sqlite3FindTable(db, pNew->table, 
102182 102533         db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
102183 102534     );
102184 102535     pParse->eTriggerOp = pNew->op;
       102536  +  /* ALWAYS() because if the table of the trigger does not exist, the
       102537  +  ** error would have been hit before this point */
       102538  +  if( ALWAYS(pParse->pTriggerTab) ){
       102539  +    rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
       102540  +  }
102185 102541   
102186 102542     /* Resolve symbols in WHEN clause */
102187         -  if( pNew->pWhen ){
       102543  +  if( rc==SQLITE_OK && pNew->pWhen ){
102188 102544       rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
102189 102545     }
102190 102546   
102191 102547     for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
102192 102548       if( pStep->pSelect ){
102193 102549         sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
102194 102550         if( pParse->nErr ) rc = pParse->rc;
102195 102551       }
102196 102552       if( rc==SQLITE_OK && pStep->zTarget ){
102197 102553         Table *pTarget = sqlite3LocateTable(pParse, 0, pStep->zTarget, zDb);
102198 102554         if( pTarget==0 ){
102199 102555           rc = SQLITE_ERROR;
102200         -      }else{
       102556  +      }else if( SQLITE_OK==(rc = sqlite3ViewGetColumnNames(pParse, pTarget)) ){
102201 102557           SrcList sSrc;
102202 102558           memset(&sSrc, 0, sizeof(sSrc));
102203 102559           sSrc.nSrc = 1;
102204 102560           sSrc.a[0].zName = pStep->zTarget;
102205 102561           sSrc.a[0].pTab = pTarget;
102206 102562           sNC.pSrcList = &sSrc;
102207 102563           if( pStep->pWhere ){
................................................................................
102294 102650   **   8. bTemp:    True if zSql comes from temp schema
102295 102651   **
102296 102652   ** Do a column rename operation on the CREATE statement given in zSql.
102297 102653   ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
102298 102654   ** into zNew.  The name should be quoted if bQuote is true.
102299 102655   **
102300 102656   ** This function is used internally by the ALTER TABLE RENAME COLUMN command.
102301         -** Though accessible to application code, it is not intended for use by
102302         -** applications.  The existance of this function, and the way it works,
102303         -** is subject to change without notice.
102304         -**
102305         -** If any of the parameters are out-of-bounds, then simply return NULL.
102306         -** An out-of-bounds parameter can only occur when the application calls
102307         -** this function directly.  The parameters will always be well-formed when
102308         -** this routine is invoked by the bytecode for a legitimate ALTER TABLE
102309         -** statement.
       102657  +** It is only accessible to SQL created using sqlite3NestedParse().  It is
       102658  +** not reachable from ordinary SQL passed into sqlite3_prepare().
102310 102659   */
102311 102660   static void renameColumnFunc(
102312 102661     sqlite3_context *context,
102313 102662     int NotUsed,
102314 102663     sqlite3_value **argv
102315 102664   ){
102316 102665     sqlite3 *db = sqlite3_context_db_handle(context);
................................................................................
102458 102807   }
102459 102808   
102460 102809   /*
102461 102810   ** Walker expression callback used by "RENAME TABLE". 
102462 102811   */
102463 102812   static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
102464 102813     RenameCtx *p = pWalker->u.pRename;
102465         -  if( pExpr->op==TK_COLUMN && p->pTab==pExpr->pTab ){
102466         -    renameTokenFind(pWalker->pParse, p, (void*)&pExpr->pTab);
       102814  +  if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){
       102815  +    renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
102467 102816     }
102468 102817     return WRC_Continue;
102469 102818   }
102470 102819   
102471 102820   /*
102472 102821   ** Walker select callback used by "RENAME TABLE". 
102473 102822   */
................................................................................
102539 102888       sWalker.xExprCallback = renameTableExprCb;
102540 102889       sWalker.xSelectCallback = renameTableSelectCb;
102541 102890       sWalker.u.pRename = &sCtx;
102542 102891   
102543 102892       rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
102544 102893   
102545 102894       if( rc==SQLITE_OK ){
       102895  +      int isLegacy = (db->flags & SQLITE_LegacyAlter);
102546 102896         if( sParse.pNewTable ){
102547 102897           Table *pTab = sParse.pNewTable;
102548 102898   
102549 102899           if( pTab->pSelect ){
102550         -          NameContext sNC;
102551         -          memset(&sNC, 0, sizeof(sNC));
102552         -          sNC.pParse = &sParse;
       102900  +          if( isLegacy==0 ){
       102901  +            NameContext sNC;
       102902  +            memset(&sNC, 0, sizeof(sNC));
       102903  +            sNC.pParse = &sParse;
102553 102904   
102554         -          sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
102555         -          if( sParse.nErr ) rc = sParse.rc;
102556         -          sqlite3WalkSelect(&sWalker, pTab->pSelect);
       102905  +            sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
       102906  +            if( sParse.nErr ) rc = sParse.rc;
       102907  +            sqlite3WalkSelect(&sWalker, pTab->pSelect);
       102908  +          }
102557 102909           }else{
102558 102910             /* Modify any FK definitions to point to the new table. */
102559 102911   #ifndef SQLITE_OMIT_FOREIGN_KEY
102560         -          if( db->flags & SQLITE_ForeignKeys ){
       102912  +          if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){
102561 102913               FKey *pFKey;
102562 102914               for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
102563 102915                 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
102564 102916                   renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
102565 102917                 }
102566 102918               }
102567 102919             }
................................................................................
102568 102920   #endif
102569 102921   
102570 102922             /* If this is the table being altered, fix any table refs in CHECK
102571 102923             ** expressions. Also update the name that appears right after the
102572 102924             ** "CREATE [VIRTUAL] TABLE" bit. */
102573 102925             if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
102574 102926               sCtx.pTab = pTab;
102575         -            sqlite3WalkExprList(&sWalker, pTab->pCheck);
       102927  +            if( isLegacy==0 ){
       102928  +              sqlite3WalkExprList(&sWalker, pTab->pCheck);
       102929  +            }
102576 102930               renameTokenFind(&sParse, &sCtx, pTab->zName);
102577 102931             }
102578 102932           }
102579 102933         }
102580 102934   
102581 102935         else if( sParse.pNewIndex ){
102582 102936           renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
102583         -        sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
       102937  +        if( isLegacy==0 ){
       102938  +          sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
       102939  +        }
102584 102940         }
102585 102941   
102586 102942   #ifndef SQLITE_OMIT_TRIGGER
102587 102943         else{
102588 102944           Trigger *pTrigger = sParse.pNewTrigger;
102589 102945           TriggerStep *pStep;
102590 102946           if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld) 
102591 102947               && sCtx.pTab->pSchema==pTrigger->pTabSchema
102592 102948             ){
102593 102949             renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
102594 102950           }
102595 102951   
102596         -        rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
102597         -        if( rc==SQLITE_OK ){
102598         -          renameWalkTrigger(&sWalker, pTrigger);
102599         -          for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
102600         -            if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
102601         -              renameTokenFind(&sParse, &sCtx, pStep->zTarget);
       102952  +        if( isLegacy==0 ){
       102953  +          rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
       102954  +          if( rc==SQLITE_OK ){
       102955  +            renameWalkTrigger(&sWalker, pTrigger);
       102956  +            for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
       102957  +              if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
       102958  +                renameTokenFind(&sParse, &sCtx, pStep->zTarget);
       102959  +              }
102602 102960               }
102603 102961             }
102604 102962           }
102605 102963         }
102606 102964   #endif
102607 102965       }
102608 102966   
................................................................................
102652 103010     int NotUsed,
102653 103011     sqlite3_value **argv
102654 103012   ){
102655 103013     sqlite3 *db = sqlite3_context_db_handle(context);
102656 103014     char const *zDb = (const char*)sqlite3_value_text(argv[0]);
102657 103015     char const *zInput = (const char*)sqlite3_value_text(argv[1]);
102658 103016     int bTemp = sqlite3_value_int(argv[4]);
       103017  +  int isLegacy = (db->flags & SQLITE_LegacyAlter);
102659 103018   
102660 103019   #ifndef SQLITE_OMIT_AUTHORIZATION
102661 103020     sqlite3_xauth xAuth = db->xAuth;
102662 103021     db->xAuth = 0;
102663 103022   #endif
102664 103023   
102665 103024     UNUSED_PARAMETER(NotUsed);
102666 103025     if( zDb && zInput ){
102667 103026       int rc;
102668 103027       Parse sParse;
102669 103028       rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
102670 103029       if( rc==SQLITE_OK ){
102671         -      if( sParse.pNewTable && sParse.pNewTable->pSelect ){
       103030  +      if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
102672 103031           NameContext sNC;
102673 103032           memset(&sNC, 0, sizeof(sNC));
102674 103033           sNC.pParse = &sParse;
102675 103034           sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
102676 103035           if( sParse.nErr ) rc = sParse.rc;
102677 103036         }
102678 103037   
102679 103038         else if( sParse.pNewTrigger ){
102680         -        rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
       103039  +        if( isLegacy==0 ){
       103040  +          rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
       103041  +        }
102681 103042           if( rc==SQLITE_OK ){
102682 103043             int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
102683 103044             int i2 = sqlite3FindDbName(db, zDb);
102684 103045             if( i1==i2 ) sqlite3_result_int(context, 1);
102685 103046           }
102686 103047         }
102687 103048       }
................................................................................
102698 103059   }
102699 103060   
102700 103061   /*
102701 103062   ** Register built-in functions used to help implement ALTER TABLE
102702 103063   */
102703 103064   SQLITE_PRIVATE void sqlite3AlterFunctions(void){
102704 103065     static FuncDef aAlterTableFuncs[] = {
102705         -    FUNCTION(sqlite_rename_column,  9, 0, 0, renameColumnFunc),
102706         -    FUNCTION(sqlite_rename_table,  7, 0, 0, renameTableFunc),
102707         -    FUNCTION(sqlite_rename_test,  5, 0, 0, renameTableTest),
       103066  +    INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc),
       103067  +    INTERNAL_FUNCTION(sqlite_rename_table,  7, renameTableFunc),
       103068  +    INTERNAL_FUNCTION(sqlite_rename_test,   5, renameTableTest),
102708 103069     };
102709 103070     sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
102710 103071   }
102711 103072   #endif  /* SQLITE_ALTER_TABLE */
102712 103073   
102713 103074   /************** End of alter.c ***********************************************/
102714 103075   /************** Begin file analyze.c *****************************************/
................................................................................
104749 105110       ** reopen it as a MemDB */
104750 105111       pVfs = sqlite3_vfs_find("memdb");
104751 105112       if( pVfs==0 ) return;
104752 105113       pNew = &db->aDb[db->init.iDb];
104753 105114       if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
104754 105115       pNew->pBt = 0;
104755 105116       pNew->pSchema = 0;
104756         -    rc = sqlite3BtreeOpen(pVfs, "x", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
       105117  +    rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
104757 105118     }else{
104758 105119       /* This is a real ATTACH
104759 105120       **
104760 105121       ** Check for the following errors:
104761 105122       **
104762 105123       **     * Too many attached databases,
104763 105124       **     * Transaction currently open
................................................................................
105429 105790     Table *pTab = 0;      /* The table being read */
105430 105791     const char *zCol;     /* Name of the column of the table */
105431 105792     int iSrc;             /* Index in pTabList->a[] of table being read */
105432 105793     int iDb;              /* The index of the database the expression refers to */
105433 105794     int iCol;             /* Index of column in table */
105434 105795   
105435 105796     assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
       105797  +  assert( !IN_RENAME_OBJECT || db->xAuth==0 );
105436 105798     if( db->xAuth==0 ) return;
105437 105799     iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
105438 105800     if( iDb<0 ){
105439 105801       /* An attempt to read a column out of a subquery or other
105440 105802       ** temporary table. */
105441 105803       return;
105442 105804     }
................................................................................
105485 105847   ){
105486 105848     sqlite3 *db = pParse->db;
105487 105849     int rc;
105488 105850   
105489 105851     /* Don't do any authorization checks if the database is initialising
105490 105852     ** or if the parser is being invoked from within sqlite3_declare_vtab.
105491 105853     */
       105854  +  assert( !IN_RENAME_OBJECT || db->xAuth==0 );
105492 105855     if( db->init.busy || IN_SPECIAL_PARSE ){
105493 105856       return SQLITE_OK;
105494 105857     }
105495 105858   
105496 105859     if( db->xAuth==0 ){
105497 105860       return SQLITE_OK;
105498 105861     }
................................................................................
105908 106271       return 0;
105909 106272     }
105910 106273   
105911 106274     p = sqlite3FindTable(db, zName, zDbase);
105912 106275     if( p==0 ){
105913 106276       const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
105914 106277   #ifndef SQLITE_OMIT_VIRTUALTABLE
105915         -    if( sqlite3FindDbName(db, zDbase)<1 ){
105916         -      /* If zName is the not the name of a table in the schema created using
105917         -      ** CREATE, then check to see if it is the name of an virtual table that
105918         -      ** can be an eponymous virtual table. */
105919         -      Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
105920         -      if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
105921         -        pMod = sqlite3PragmaVtabRegister(db, zName);
105922         -      }
105923         -      if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
105924         -        return pMod->pEpoTab;
105925         -      }
       106278  +    /* If zName is the not the name of a table in the schema created using
       106279  +    ** CREATE, then check to see if it is the name of an virtual table that
       106280  +    ** can be an eponymous virtual table. */
       106281  +    Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
       106282  +    if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
       106283  +      pMod = sqlite3PragmaVtabRegister(db, zName);
       106284  +    }
       106285  +    if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
       106286  +      return pMod->pEpoTab;
105926 106287       }
105927 106288   #endif
105928 106289       if( (flags & LOCATE_NOERR)==0 ){
105929 106290         if( zDbase ){
105930 106291           sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
105931 106292         }else{
105932 106293           sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
................................................................................
106098 106459   /*
106099 106460   ** Erase all schema information from all attached databases (including
106100 106461   ** "main" and "temp") for a single database connection.
106101 106462   */
106102 106463   SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
106103 106464     int i;
106104 106465     sqlite3BtreeEnterAll(db);
106105         -  assert( db->nSchemaLock==0 );
106106 106466     for(i=0; i<db->nDb; i++){
106107 106467       Db *pDb = &db->aDb[i];
106108 106468       if( pDb->pSchema ){
106109         -      sqlite3SchemaClear(pDb->pSchema);
       106469  +      if( db->nSchemaLock==0 ){
       106470  +        sqlite3SchemaClear(pDb->pSchema);
       106471  +      }else{
       106472  +        DbSetProperty(db, i, DB_ResetWanted);
       106473  +      }
106110 106474       }
106111 106475     }
106112 106476     db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
106113 106477     sqlite3VtabUnlockList(db);
106114 106478     sqlite3BtreeLeaveAll(db);
106115         -  sqlite3CollapseDatabaseArray(db);
       106479  +  if( db->nSchemaLock==0 ){
       106480  +    sqlite3CollapseDatabaseArray(db);
       106481  +  }
106116 106482   }
106117 106483   
106118 106484   /*
106119 106485   ** This routine is called when a commit occurs.
106120 106486   */
106121 106487   SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
106122 106488     db->mDbFlags &= ~DBFLAG_SchemaChange;
................................................................................
106185 106551     }
106186 106552   
106187 106553     /* Delete any foreign keys attached to this table. */
106188 106554     sqlite3FkDelete(db, pTable);
106189 106555   
106190 106556     /* Delete the Table structure itself.
106191 106557     */
       106558  +#ifdef SQLITE_ENABLE_NORMALIZE
       106559  +  if( pTable->pColHash ){
       106560  +    sqlite3HashClear(pTable->pColHash);
       106561  +    sqlite3_free(pTable->pColHash);
       106562  +  }
       106563  +#endif
106192 106564     sqlite3DeleteColumnNames(db, pTable);
106193 106565     sqlite3DbFree(db, pTable->zName);
106194 106566     sqlite3DbFree(db, pTable->zColAff);
106195 106567     sqlite3SelectDelete(db, pTable->pSelect);
106196 106568     sqlite3ExprListDelete(db, pTable->pCheck);
106197 106569   #ifndef SQLITE_OMIT_VIRTUALTABLE
106198 106570     sqlite3VtabClear(db, pTable);
................................................................................
106342 106714       assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT
106343 106715                || (db->mDbFlags & DBFLAG_Vacuum)!=0);
106344 106716       iDb = db->init.iDb;
106345 106717       *pUnqual = pName1;
106346 106718     }
106347 106719     return iDb;
106348 106720   }
       106721  +
       106722  +/*
       106723  +** True if PRAGMA writable_schema is ON
       106724  +*/
       106725  +SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3 *db){
       106726  +  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
       106727  +  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
       106728  +               SQLITE_WriteSchema );
       106729  +  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
       106730  +               SQLITE_Defensive );
       106731  +  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
       106732  +               (SQLITE_WriteSchema|SQLITE_Defensive) );
       106733  +  return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
       106734  +}
106349 106735   
106350 106736   /*
106351 106737   ** This routine is used to check if the UTF-8 string zName is a legal
106352 106738   ** unqualified name for a new schema object (table, index, view or
106353 106739   ** trigger). All names are legal except those that begin with the string
106354 106740   ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
106355 106741   ** is reserved for internal use.
106356 106742   */
106357 106743   SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
106358 106744     if( !pParse->db->init.busy && pParse->nested==0 
106359         -          && (pParse->db->flags & SQLITE_WriteSchema)==0
       106745  +          && sqlite3WritableSchema(pParse->db)==0
106360 106746             && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
106361 106747       sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
106362 106748       return SQLITE_ERROR;
106363 106749     }
106364 106750     return SQLITE_OK;
106365 106751   }
106366 106752   
................................................................................
107323 107709       for(i=0; i<pTab->nCol; i++){
107324 107710         if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
107325 107711           pTab->aCol[i].notNull = OE_Abort;
107326 107712         }
107327 107713       }
107328 107714     }
107329 107715   
107330         -  /* The remaining transformations only apply to b-tree tables, not to
107331         -  ** virtual tables */
107332         -  if( IN_DECLARE_VTAB ) return;
107333         -
107334 107716     /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
107335 107717     ** into BTREE_BLOBKEY.
107336 107718     */
107337 107719     if( pParse->addrCrTab ){
107338 107720       assert( v );
107339 107721       sqlite3VdbeChangeP3(v, pParse->addrCrTab, BTREE_BLOBKEY);
107340 107722     }
................................................................................
107431 107813       assert( pPk->nColumn==j );
107432 107814       assert( pTab->nCol==j );
107433 107815     }else{
107434 107816       pPk->nColumn = pTab->nCol;
107435 107817     }
107436 107818     recomputeColumnsNotIndexed(pPk);
107437 107819   }
       107820  +
       107821  +/*
       107822  +** Return true if zName is a shadow table name in the current database
       107823  +** connection.
       107824  +**
       107825  +** zName is temporarily modified while this routine is running, but is
       107826  +** restored to its original value prior to this routine returning.
       107827  +*/
       107828  +static int isShadowTableName(sqlite3 *db, char *zName){
       107829  +  char *zTail;                  /* Pointer to the last "_" in zName */
       107830  +  Table *pTab;                  /* Table that zName is a shadow of */
       107831  +  Module *pMod;                 /* Module for the virtual table */
       107832  +
       107833  +  zTail = strrchr(zName, '_');
       107834  +  if( zTail==0 ) return 0;
       107835  +  *zTail = 0;
       107836  +  pTab = sqlite3FindTable(db, zName, 0);
       107837  +  *zTail = '_';
       107838  +  if( pTab==0 ) return 0;
       107839  +  if( !IsVirtual(pTab) ) return 0;
       107840  +  pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]);
       107841  +  if( pMod==0 ) return 0;
       107842  +  if( pMod->pModule->iVersion<3 ) return 0;
       107843  +  if( pMod->pModule->xShadowName==0 ) return 0;
       107844  +  return pMod->pModule->xShadowName(zTail+1);
       107845  +}
107438 107846   
107439 107847   /*
107440 107848   ** This routine is called to report the final ")" that terminates
107441 107849   ** a CREATE TABLE statement.
107442 107850   **
107443 107851   ** The table structure that other action routines have been building
107444 107852   ** is added to the internal hash tables, assuming no errors have
................................................................................
107470 107878   
107471 107879     if( pEnd==0 && pSelect==0 ){
107472 107880       return;
107473 107881     }
107474 107882     assert( !db->mallocFailed );
107475 107883     p = pParse->pNewTable;
107476 107884     if( p==0 ) return;
       107885  +
       107886  +  if( pSelect==0 && isShadowTableName(db, p->zName) ){
       107887  +    p->tabFlags |= TF_Shadow;
       107888  +  }
107477 107889   
107478 107890     /* If the db->init.busy is 1 it means we are reading the SQL off the
107479 107891     ** "sqlite_master" or "sqlite_temp_master" table on the disk.
107480 107892     ** So do not write to the disk again.  Extract the root page number
107481 107893     ** for the table from the db->init.newTnum field.  (The page number
107482 107894     ** should have been put there by the sqliteOpenCb routine.)
107483 107895     **
................................................................................
107839 108251     ** to the elements of the FROM clause.  But we do not want these changes
107840 108252     ** to be permanent.  So the computation is done on a copy of the SELECT
107841 108253     ** statement that defines the view.
107842 108254     */
107843 108255     assert( pTable->pSelect );
107844 108256     pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
107845 108257     if( pSel ){
       108258  +#ifndef SQLITE_OMIT_ALTERTABLE
       108259  +    u8 eParseMode = pParse->eParseMode;
       108260  +    pParse->eParseMode = PARSE_MODE_NORMAL;
       108261  +#endif
107846 108262       n = pParse->nTab;
107847 108263       sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
107848 108264       pTable->nCol = -1;
107849 108265       db->lookaside.bDisable++;
107850 108266   #ifndef SQLITE_OMIT_AUTHORIZATION
107851 108267       xAuth = db->xAuth;
107852 108268       db->xAuth = 0;
................................................................................
107884 108300       }else{
107885 108301         pTable->nCol = 0;
107886 108302         nErr++;
107887 108303       }
107888 108304       sqlite3DeleteTable(db, pSelTab);
107889 108305       sqlite3SelectDelete(db, pSel);
107890 108306       db->lookaside.bDisable--;
       108307  +#ifndef SQLITE_OMIT_ALTERTABLE
       108308  +    pParse->eParseMode = eParseMode;
       108309  +#endif
107891 108310     } else {
107892 108311       nErr++;
107893 108312     }
107894 108313     pTable->pSchema->schemaFlags |= DB_UnresetViews;
107895 108314     if( db->mallocFailed ){
107896 108315       sqlite3DeleteColumnNames(db, pTable);
107897 108316       pTable->aCol = 0;
................................................................................
107971 108390   ** Also write code to modify the sqlite_master table and internal schema
107972 108391   ** if a root-page of another table is moved by the btree-layer whilst
107973 108392   ** erasing iTable (this can happen with an auto-vacuum database).
107974 108393   */ 
107975 108394   static void destroyRootPage(Parse *pParse, int iTable, int iDb){
107976 108395     Vdbe *v = sqlite3GetVdbe(pParse);
107977 108396     int r1 = sqlite3GetTempReg(pParse);
107978         -  assert( iTable>1 );
       108397  +  if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema");
107979 108398     sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
107980 108399     sqlite3MayAbort(pParse);
107981 108400   #ifndef SQLITE_OMIT_AUTOVACUUM
107982 108401     /* OP_Destroy stores an in integer r1. If this integer
107983 108402     ** is non-zero, then it is the root page number of a table moved to
107984 108403     ** location iTable. The following code modifies the sqlite_master table to
107985 108404     ** reflect this.
................................................................................
110426 110845     for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
110427 110846       if( sqlite3StrICmp(p->zName, zFunc)==0 ){
110428 110847         return p;
110429 110848       }
110430 110849     }
110431 110850     return 0;
110432 110851   }
       110852  +#ifdef SQLITE_ENABLE_NORMALIZE
       110853  +SQLITE_PRIVATE FuncDef *sqlite3FunctionSearchN(
       110854  +  int h,               /* Hash of the name */
       110855  +  const char *zFunc,   /* Name of function */
       110856  +  int nFunc            /* Length of the name */
       110857  +){
       110858  +  FuncDef *p;
       110859  +  for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
       110860  +    if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 ){
       110861  +      return p;
       110862  +    }
       110863  +  }
       110864  +  return 0;
       110865  +}
       110866  +#endif /* SQLITE_ENABLE_NORMALIZE */
110433 110867   
110434 110868   /*
110435 110869   ** Insert a new FuncDef into a FuncDefHash hash table.
110436 110870   */
110437 110871   SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(
110438 110872     FuncDef *aDef,      /* List of global functions to be inserted */
110439 110873     int nDef            /* Length of the apDef[] list */
110440 110874   ){
110441 110875     int i;
110442 110876     for(i=0; i<nDef; i++){
110443 110877       FuncDef *pOther;
110444 110878       const char *zName = aDef[i].zName;
110445 110879       int nName = sqlite3Strlen30(zName);
110446         -    int h = (zName[0] + nName) % SQLITE_FUNC_HASH_SZ;
       110880  +    int h = SQLITE_FUNC_HASH(zName[0], nName);
110447 110881       assert( zName[0]>='a' && zName[0]<='z' );
110448 110882       pOther = functionSearch(h, zName);
110449 110883       if( pOther ){
110450 110884         assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
110451 110885         aDef[i].pNext = pOther->pNext;
110452 110886         pOther->pNext = &aDef[i];
110453 110887       }else{
................................................................................
110518 110952     ** install a new function.  Whatever FuncDef structure is returned it will
110519 110953     ** have fields overwritten with new information appropriate for the
110520 110954     ** new function.  But the FuncDefs for built-in functions are read-only.
110521 110955     ** So we must not search for built-ins when creating a new function.
110522 110956     */ 
110523 110957     if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
110524 110958       bestScore = 0;
110525         -    h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % SQLITE_FUNC_HASH_SZ;
       110959  +    h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
110526 110960       p = functionSearch(h, zName);
110527 110961       while( p ){
110528 110962         int score = matchQuality(p, nArg, enc);
110529 110963         if( score>bestScore ){
110530 110964           pBest = p;
110531 110965           bestScore = score;
110532 110966         }
................................................................................
110665 111099       pTab->nTabRef++;
110666 111100     }
110667 111101     if( sqlite3IndexedByLookup(pParse, pItem) ){
110668 111102       pTab = 0;
110669 111103     }
110670 111104     return pTab;
110671 111105   }
       111106  +
       111107  +/* Return true if table pTab is read-only.
       111108  +**
       111109  +** A table is read-only if any of the following are true:
       111110  +**
       111111  +**   1) It is a virtual table and no implementation of the xUpdate method
       111112  +**      has been provided
       111113  +**
       111114  +**   2) It is a system table (i.e. sqlite_master), this call is not
       111115  +**      part of a nested parse and writable_schema pragma has not 
       111116  +**      been specified
       111117  +**
       111118  +**   3) The table is a shadow table, the database connection is in
       111119  +**      defensive mode, and the current sqlite3_prepare()
       111120  +**      is for a top-level SQL statement.
       111121  +*/
       111122  +static int tabIsReadOnly(Parse *pParse, Table *pTab){
       111123  +  sqlite3 *db;
       111124  +  if( IsVirtual(pTab) ){
       111125  +    return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
       111126  +  }
       111127  +  if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
       111128  +  db = pParse->db;
       111129  +  if( (pTab->tabFlags & TF_Readonly)!=0 ){
       111130  +    return sqlite3WritableSchema(db)==0 && pParse->nested==0;
       111131  +  }
       111132  +  assert( pTab->tabFlags & TF_Shadow );
       111133  +  return (db->flags & SQLITE_Defensive)!=0
       111134  +           && db->nVdbeExec==0
       111135  +           && db->pVtabCtx==0;
       111136  +}
110672 111137   
110673 111138   /*
110674 111139   ** Check to make sure the given table is writable.  If it is not
110675 111140   ** writable, generate an error message and return 1.  If it is
110676 111141   ** writable return 0;
110677 111142   */
110678 111143   SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
110679         -  /* A table is not writable under the following circumstances:
110680         -  **
110681         -  **   1) It is a virtual table and no implementation of the xUpdate method
110682         -  **      has been provided, or
110683         -  **   2) It is a system table (i.e. sqlite_master), this call is not
110684         -  **      part of a nested parse and writable_schema pragma has not 
110685         -  **      been specified.
110686         -  **
110687         -  ** In either case leave an error message in pParse and return non-zero.
110688         -  */
110689         -  if( ( IsVirtual(pTab) 
110690         -     && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
110691         -   || ( (pTab->tabFlags & TF_Readonly)!=0
110692         -     && (pParse->db->flags & SQLITE_WriteSchema)==0
110693         -     && pParse->nested==0 )
110694         -  ){
       111144  +  if( tabIsReadOnly(pParse, pTab) ){
110695 111145       sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
110696 111146       return 1;
110697 111147     }
110698         -
110699 111148   #ifndef SQLITE_OMIT_VIEW
110700 111149     if( !viewOk && pTab->pSelect ){
110701 111150       sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
110702 111151       return 1;
110703 111152     }
110704 111153   #endif
110705 111154     return 0;
................................................................................
114095 114544     sqlite3 *db,      /* The database connection */
114096 114545     Table *pTab,      /* The table whose column is desired */
114097 114546     int iCursor,      /* The open cursor on the table */
114098 114547     i16 iCol          /* The column that is wanted */
114099 114548   ){
114100 114549     Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
114101 114550     if( pExpr ){
114102         -    pExpr->pTab = pTab;
       114551  +    pExpr->y.pTab = pTab;
114103 114552       pExpr->iTable = iCursor;
114104 114553       pExpr->iColumn = iCol;
114105 114554     }
114106 114555     return pExpr;
114107 114556   }
114108 114557   
114109 114558   /*
................................................................................
115171 115620         zColAff[i] = pTab->aCol[i].affinity;
115172 115621       }
115173 115622       do{
115174 115623         zColAff[i--] = 0;
115175 115624       }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
115176 115625       pTab->zColAff = zColAff;
115177 115626     }
115178         -  i = sqlite3Strlen30(zColAff);
       115627  +  assert( zColAff!=0 );
       115628  +  i = sqlite3Strlen30NN(zColAff);
115179 115629     if( i ){
115180 115630       if( iReg ){
115181 115631         sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
115182 115632       }else{
115183 115633         sqlite3VdbeChangeP4(v, -1, zColAff, i);
115184 115634       }
115185 115635     }
................................................................................
116151 116601    #undef pTrigger
116152 116602   #endif
116153 116603   #ifdef tmask
116154 116604    #undef tmask
116155 116605   #endif
116156 116606   
116157 116607   /*
116158         -** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged()
       116608  +** Meanings of bits in of pWalker->eCode for 
       116609  +** sqlite3ExprReferencesUpdatedColumn()
116159 116610   */
116160 116611   #define CKCNSTRNT_COLUMN   0x01    /* CHECK constraint uses a changing column */
116161 116612   #define CKCNSTRNT_ROWID    0x02    /* CHECK constraint references the ROWID */
116162 116613   
116163         -/* This is the Walker callback from checkConstraintUnchanged().  Set
116164         -** bit 0x01 of pWalker->eCode if
116165         -** pWalker->eCode to 0 if this expression node references any of the
       116614  +/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
       116615  +*  Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
       116616  +** expression node references any of the
116166 116617   ** columns that are being modifed by an UPDATE statement.
116167 116618   */
116168 116619   static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
116169 116620     if( pExpr->op==TK_COLUMN ){
116170 116621       assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
116171 116622       if( pExpr->iColumn>=0 ){
116172 116623         if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
................................................................................
116180 116631   }
116181 116632   
116182 116633   /*
116183 116634   ** pExpr is a CHECK constraint on a row that is being UPDATE-ed.  The
116184 116635   ** only columns that are modified by the UPDATE are those for which
116185 116636   ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
116186 116637   **
116187         -** Return true if CHECK constraint pExpr does not use any of the
       116638  +** Return true if CHECK constraint pExpr uses any of the
116188 116639   ** changing columns (or the rowid if it is changing).  In other words,
116189         -** return true if this CHECK constraint can be skipped when validating
       116640  +** return true if this CHECK constraint must be validated for
116190 116641   ** the new row in the UPDATE statement.
       116642  +**
       116643  +** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
       116644  +** The operation of this routine is the same - return true if an only if
       116645  +** the expression uses one or more of columns identified by the second and
       116646  +** third arguments.
116191 116647   */
116192         -static int checkConstraintUnchanged(Expr *pExpr, int *aiChng, int chngRowid){
       116648  +SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(
       116649  +  Expr *pExpr,    /* The expression to be checked */
       116650  +  int *aiChng,    /* aiChng[x]>=0 if column x changed by the UPDATE */
       116651  +  int chngRowid   /* True if UPDATE changes the rowid */
       116652  +){
116193 116653     Walker w;
116194 116654     memset(&w, 0, sizeof(w));
116195 116655     w.eCode = 0;
116196 116656     w.xExprCallback = checkConstraintExprNode;
116197 116657     w.u.aiCol = aiChng;
116198 116658     sqlite3WalkExpr(&w, pExpr);
116199 116659     if( !chngRowid ){
................................................................................
116200 116660       testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
116201 116661       w.eCode &= ~CKCNSTRNT_ROWID;
116202 116662     }
116203 116663     testcase( w.eCode==0 );
116204 116664     testcase( w.eCode==CKCNSTRNT_COLUMN );
116205 116665     testcase( w.eCode==CKCNSTRNT_ROWID );
116206 116666     testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
116207         -  return !w.eCode;
       116667  +  return w.eCode!=0;
116208 116668   }
116209 116669   
116210 116670   /*
116211 116671   ** Generate code to do constraint checks prior to an INSERT or an UPDATE
116212 116672   ** on table pTab.
116213 116673   **
116214 116674   ** The regNewData parameter is the first register in a range that contains
................................................................................
116406 116866     if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
116407 116867       ExprList *pCheck = pTab->pCheck;
116408 116868       pParse->iSelfTab = -(regNewData+1);
116409 116869       onError = overrideError!=OE_Default ? overrideError : OE_Abort;
116410 116870       for(i=0; i<pCheck->nExpr; i++){
116411 116871         int allOk;
116412 116872         Expr *pExpr = pCheck->a[i].pExpr;
116413         -      if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue;
       116873  +      if( aiChng
       116874  +       && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
       116875  +      ){
       116876  +        /* The check constraints do not reference any of the columns being
       116877  +        ** updated so there is no point it verifying the check constraint */
       116878  +        continue;
       116879  +      }
116414 116880         allOk = sqlite3VdbeMakeLabel(v);
116415 116881         sqlite3VdbeVerifyAbortable(v, onError);
116416 116882         sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
116417 116883         if( onError==OE_Ignore ){
116418 116884           sqlite3VdbeGoto(v, ignoreDest);
116419 116885         }else{
116420 116886           char *zName = pCheck->a[i].zName;
................................................................................
117907 118373     void (*str_append)(sqlite3_str*, const char *zIn, int N);
117908 118374     void (*str_appendall)(sqlite3_str*, const char *zIn);
117909 118375     void (*str_appendchar)(sqlite3_str*, int N, char C);
117910 118376     void (*str_reset)(sqlite3_str*);
117911 118377     int (*str_errcode)(sqlite3_str*);
117912 118378     int (*str_length)(sqlite3_str*);
117913 118379     char *(*str_value)(sqlite3_str*);
       118380  +  /* Version 3.25.0 and later */
117914 118381     int (*create_window_function)(sqlite3*,const char*,int,int,void*,
117915 118382                               void (*xStep)(sqlite3_context*,int,sqlite3_value**),
117916 118383                               void (*xFinal)(sqlite3_context*),
117917 118384                               void (*xValue)(sqlite3_context*),
117918 118385                               void (*xInv)(sqlite3_context*,int,sqlite3_value**),
117919 118386                               void(*xDestroy)(void*));
       118387  +  /* Version 3.26.0 and later */
       118388  +  const char *(*normalized_sql)(sqlite3_stmt*);
117920 118389   };
117921 118390   
117922 118391   /*
117923 118392   ** This is the function signature used for all extension entry points.  It
117924 118393   ** is also defined in the file "loadext.c".
117925 118394   */
117926 118395   typedef int (*sqlite3_loadext_entry)(
................................................................................
118200 118669   #define sqlite3_str_appendchar         sqlite3_api->str_appendchar
118201 118670   #define sqlite3_str_reset              sqlite3_api->str_reset
118202 118671   #define sqlite3_str_errcode            sqlite3_api->str_errcode
118203 118672   #define sqlite3_str_length             sqlite3_api->str_length
118204 118673   #define sqlite3_str_value              sqlite3_api->str_value
118205 118674   /* Version 3.25.0 and later */
118206 118675   #define sqlite3_create_window_function sqlite3_api->create_window_function
       118676  +/* Version 3.26.0 and later */
       118677  +#define sqlite3_normalized_sql         sqlite3_api->normalized_sql
118207 118678   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
118208 118679   
118209 118680   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
118210 118681     /* This case when the file really is being compiled as a loadable 
118211 118682     ** extension */
118212 118683   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
118213 118684   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
................................................................................
118655 119126     sqlite3_str_appendall,
118656 119127     sqlite3_str_appendchar,
118657 119128     sqlite3_str_reset,
118658 119129     sqlite3_str_errcode,
118659 119130     sqlite3_str_length,
118660 119131     sqlite3_str_value,
118661 119132     /* Version 3.25.0 and later */
118662         -  sqlite3_create_window_function
       119133  +  sqlite3_create_window_function,
       119134  +  /* Version 3.26.0 and later */
       119135  +#ifdef SQLITE_ENABLE_NORMALIZE
       119136  +  sqlite3_normalized_sql
       119137  +#else
       119138  +  0
       119139  +#endif
118663 119140   };
118664 119141   
118665 119142   /*
118666 119143   ** Attempt to load an SQLite extension library contained in the file
118667 119144   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
118668 119145   ** default entry point name (sqlite3_extension_init) is used.  Use
118669 119146   ** of the default name is recommended.
................................................................................
119105 119582   #define PragTyp_TEMP_STORE_DIRECTORY          36
119106 119583   #define PragTyp_THREADS                       37
119107 119584   #define PragTyp_WAL_AUTOCHECKPOINT            38
119108 119585   #define PragTyp_WAL_CHECKPOINT                39
119109 119586   #define PragTyp_ACTIVATE_EXTENSIONS           40
119110 119587   #define PragTyp_HEXKEY                        41
119111 119588   #define PragTyp_KEY                           42
119112         -#define PragTyp_REKEY                         43
119113         -#define PragTyp_LOCK_STATUS                   44
119114         -#define PragTyp_PARSER_TRACE                  45
119115         -#define PragTyp_STATS                         46
       119589  +#define PragTyp_LOCK_STATUS                   43
       119590  +#define PragTyp_PARSER_TRACE                  44
       119591  +#define PragTyp_STATS                         45
119116 119592   
119117 119593   /* Property flags associated with various pragma. */
119118 119594   #define PragFlg_NeedSchema 0x01 /* Force schema load before running */
119119 119595   #define PragFlg_NoColumns  0x02 /* OP_ResultRow called with zero columns */
119120 119596   #define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */
119121 119597   #define PragFlg_ReadOnly   0x08 /* Read-only HEADER_VALUE */
119122 119598   #define PragFlg_Result0    0x10 /* Acts as query when no argument */
................................................................................
119125 119601   #define PragFlg_SchemaReq  0x80 /* Schema required - "main" is default */
119126 119602   
119127 119603   /* Names of columns for pragmas that return multi-column result
119128 119604   ** or that return single-column results where the name of the
119129 119605   ** result column is different from the name of the pragma
119130 119606   */
119131 119607   static const char *const pragCName[] = {
119132         -  /*   0 */ "cache_size",  /* Used by: default_cache_size */
119133         -  /*   1 */ "cid",         /* Used by: table_info */
119134         -  /*   2 */ "name",       
119135         -  /*   3 */ "type",       
119136         -  /*   4 */ "notnull",    
119137         -  /*   5 */ "dflt_value", 
119138         -  /*   6 */ "pk",         
119139         -  /*   7 */ "tbl",         /* Used by: stats */
119140         -  /*   8 */ "idx",        
119141         -  /*   9 */ "wdth",       
119142         -  /*  10 */ "hght",       
119143         -  /*  11 */ "flgs",       
119144         -  /*  12 */ "seqno",       /* Used by: index_info */
119145         -  /*  13 */ "cid",        
119146         -  /*  14 */ "name",       
       119608  +  /*   0 */ "id",          /* Used by: foreign_key_list */
       119609  +  /*   1 */ "seq",        
       119610  +  /*   2 */ "table",      
       119611  +  /*   3 */ "from",       
       119612  +  /*   4 */ "to",         
       119613  +  /*   5 */ "on_update",  
       119614  +  /*   6 */ "on_delete",  
       119615  +  /*   7 */ "match",      
       119616  +  /*   8 */ "cid",         /* Used by: table_xinfo */
       119617  +  /*   9 */ "name",       
       119618  +  /*  10 */ "type",       
       119619  +  /*  11 */ "notnull",    
       119620  +  /*  12 */ "dflt_value", 
       119621  +  /*  13 */ "pk",         
       119622  +  /*  14 */ "hidden",     
       119623  +                           /* table_info reuses 8 */
119147 119624     /*  15 */ "seqno",       /* Used by: index_xinfo */
119148 119625     /*  16 */ "cid",        
119149 119626     /*  17 */ "name",       
119150 119627     /*  18 */ "desc",       
119151 119628     /*  19 */ "coll",       
119152 119629     /*  20 */ "key",        
119153         -  /*  21 */ "seq",         /* Used by: index_list */
119154         -  /*  22 */ "name",       
119155         -  /*  23 */ "unique",     
119156         -  /*  24 */ "origin",     
119157         -  /*  25 */ "partial",    
119158         -  /*  26 */ "seq",         /* Used by: database_list */
       119630  +  /*  21 */ "tbl",         /* Used by: stats */
       119631  +  /*  22 */ "idx",        
       119632  +  /*  23 */ "wdth",       
       119633  +  /*  24 */ "hght",       
       119634  +  /*  25 */ "flgs",       
       119635  +  /*  26 */ "seq",         /* Used by: index_list */
119159 119636     /*  27 */ "name",       
119160         -  /*  28 */ "file",       
119161         -  /*  29 */ "name",        /* Used by: function_list */
119162         -  /*  30 */ "builtin",    
119163         -  /*  31 */ "name",        /* Used by: module_list pragma_list */
119164         -  /*  32 */ "seq",         /* Used by: collation_list */
119165         -  /*  33 */ "name",       
119166         -  /*  34 */ "id",          /* Used by: foreign_key_list */
119167         -  /*  35 */ "seq",        
119168         -  /*  36 */ "table",      
119169         -  /*  37 */ "from",       
119170         -  /*  38 */ "to",         
119171         -  /*  39 */ "on_update",  
119172         -  /*  40 */ "on_delete",  
119173         -  /*  41 */ "match",      
119174         -  /*  42 */ "table",       /* Used by: foreign_key_check */
119175         -  /*  43 */ "rowid",      
119176         -  /*  44 */ "parent",     
119177         -  /*  45 */ "fkid",       
119178         -  /*  46 */ "busy",        /* Used by: wal_checkpoint */
119179         -  /*  47 */ "log",        
119180         -  /*  48 */ "checkpointed",
119181         -  /*  49 */ "timeout",     /* Used by: busy_timeout */
119182         -  /*  50 */ "database",    /* Used by: lock_status */
119183         -  /*  51 */ "status",     
       119637  +  /*  28 */ "unique",     
       119638  +  /*  29 */ "origin",     
       119639  +  /*  30 */ "partial",    
       119640  +  /*  31 */ "table",       /* Used by: foreign_key_check */
       119641  +  /*  32 */ "rowid",      
       119642  +  /*  33 */ "parent",     
       119643  +  /*  34 */ "fkid",       
       119644  +                           /* index_info reuses 15 */
       119645  +  /*  35 */ "seq",         /* Used by: database_list */
       119646  +  /*  36 */ "name",       
       119647  +  /*  37 */ "file",       
       119648  +  /*  38 */ "busy",        /* Used by: wal_checkpoint */
       119649  +  /*  39 */ "log",        
       119650  +  /*  40 */ "checkpointed",
       119651  +  /*  41 */ "name",        /* Used by: function_list */
       119652  +  /*  42 */ "builtin",    
       119653  +                           /* collation_list reuses 26 */
       119654  +  /*  43 */ "database",    /* Used by: lock_status */
       119655  +  /*  44 */ "status",     
       119656  +  /*  45 */ "cache_size",  /* Used by: default_cache_size */
       119657  +                           /* module_list pragma_list reuses 9 */
       119658  +  /*  46 */ "timeout",     /* Used by: busy_timeout */
119184 119659   };
119185 119660   
119186 119661   /* Definitions of all built-in pragmas */
119187 119662   typedef struct PragmaName {
119188 119663     const char *const zName; /* Name of pragma */
119189 119664     u8 ePragTyp;             /* PragTyp_XXX value */
119190 119665     u8 mPragFlg;             /* Zero or more PragFlg_XXX values */
119191 119666     u8 iPragCName;           /* Start of column names in pragCName[] */
119192 119667     u8 nPragCName;           /* Num of col names. 0 means use pragma name */
119193         -  u32 iArg;                /* Extra argument */
       119668  +  u64 iArg;                /* Extra argument */
119194 119669   } PragmaName;
119195 119670   static const PragmaName aPragmaName[] = {
119196 119671   #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
119197 119672    {/* zName:     */ "activate_extensions",
119198 119673     /* ePragTyp:  */ PragTyp_ACTIVATE_EXTENSIONS,
119199 119674     /* ePragFlg:  */ 0,
119200 119675     /* ColNames:  */ 0, 0,
................................................................................
119222 119697     /* ColNames:  */ 0, 0,
119223 119698     /* iArg:      */ SQLITE_AutoIndex },
119224 119699   #endif
119225 119700   #endif
119226 119701    {/* zName:     */ "busy_timeout",
119227 119702     /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
119228 119703     /* ePragFlg:  */ PragFlg_Result0,
119229         -  /* ColNames:  */ 49, 1,
       119704  +  /* ColNames:  */ 46, 1,
119230 119705     /* iArg:      */ 0 },
119231 119706   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119232 119707    {/* zName:     */ "cache_size",
119233 119708     /* ePragTyp:  */ PragTyp_CACHE_SIZE,
119234 119709     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119235 119710     /* ColNames:  */ 0, 0,
119236 119711     /* iArg:      */ 0 },
................................................................................
119259 119734     /* ColNames:  */ 0, 0,
119260 119735     /* iArg:      */ SQLITE_CkptFullFSync },
119261 119736   #endif
119262 119737   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119263 119738    {/* zName:     */ "collation_list",
119264 119739     /* ePragTyp:  */ PragTyp_COLLATION_LIST,
119265 119740     /* ePragFlg:  */ PragFlg_Result0,
119266         -  /* ColNames:  */ 32, 2,
       119741  +  /* ColNames:  */ 26, 2,
119267 119742     /* iArg:      */ 0 },
119268 119743   #endif
119269 119744   #if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
119270 119745    {/* zName:     */ "compile_options",
119271 119746     /* ePragTyp:  */ PragTyp_COMPILE_OPTIONS,
119272 119747     /* ePragFlg:  */ PragFlg_Result0,
119273 119748     /* ColNames:  */ 0, 0,
................................................................................
119294 119769     /* ColNames:  */ 0, 0,
119295 119770     /* iArg:      */ BTREE_DATA_VERSION },
119296 119771   #endif
119297 119772   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119298 119773    {/* zName:     */ "database_list",
119299 119774     /* ePragTyp:  */ PragTyp_DATABASE_LIST,
119300 119775     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0,
119301         -  /* ColNames:  */ 26, 3,
       119776  +  /* ColNames:  */ 35, 3,
119302 119777     /* iArg:      */ 0 },
119303 119778   #endif
119304 119779   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
119305 119780    {/* zName:     */ "default_cache_size",
119306 119781     /* ePragTyp:  */ PragTyp_DEFAULT_CACHE_SIZE,
119307 119782     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119308         -  /* ColNames:  */ 0, 1,
       119783  +  /* ColNames:  */ 45, 1,
119309 119784     /* iArg:      */ 0 },
119310 119785   #endif
119311 119786   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119312 119787   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119313 119788    {/* zName:     */ "defer_foreign_keys",
119314 119789     /* ePragTyp:  */ PragTyp_FLAG,
119315 119790     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
................................................................................
119331 119806     /* ColNames:  */ 0, 0,
119332 119807     /* iArg:      */ 0 },
119333 119808   #endif
119334 119809   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119335 119810    {/* zName:     */ "foreign_key_check",
119336 119811     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_CHECK,
119337 119812     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0,
119338         -  /* ColNames:  */ 42, 4,
       119813  +  /* ColNames:  */ 31, 4,
119339 119814     /* iArg:      */ 0 },
119340 119815   #endif
119341 119816   #if !defined(SQLITE_OMIT_FOREIGN_KEY)
119342 119817    {/* zName:     */ "foreign_key_list",
119343 119818     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_LIST,
119344 119819     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119345         -  /* ColNames:  */ 34, 8,
       119820  +  /* ColNames:  */ 0, 8,
119346 119821     /* iArg:      */ 0 },
119347 119822   #endif
119348 119823   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119349 119824   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119350 119825    {/* zName:     */ "foreign_keys",
119351 119826     /* ePragTyp:  */ PragTyp_FLAG,
119352 119827     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
................................................................................
119374 119849     /* iArg:      */ SQLITE_FullFSync },
119375 119850   #endif
119376 119851   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119377 119852   #if defined(SQLITE_INTROSPECTION_PRAGMAS)
119378 119853    {/* zName:     */ "function_list",
119379 119854     /* ePragTyp:  */ PragTyp_FUNCTION_LIST,
119380 119855     /* ePragFlg:  */ PragFlg_Result0,
119381         -  /* ColNames:  */ 29, 2,
       119856  +  /* ColNames:  */ 41, 2,
119382 119857     /* iArg:      */ 0 },
119383 119858   #endif
119384 119859   #endif
119385 119860   #if defined(SQLITE_HAS_CODEC)
119386 119861    {/* zName:     */ "hexkey",
119387 119862     /* ePragTyp:  */ PragTyp_HEXKEY,
119388 119863     /* ePragFlg:  */ 0,
119389 119864     /* ColNames:  */ 0, 0,
119390         -  /* iArg:      */ 0 },
       119865  +  /* iArg:      */ 2 },
119391 119866    {/* zName:     */ "hexrekey",
119392 119867     /* ePragTyp:  */ PragTyp_HEXKEY,
119393 119868     /* ePragFlg:  */ 0,
119394 119869     /* ColNames:  */ 0, 0,
119395         -  /* iArg:      */ 0 },
       119870  +  /* iArg:      */ 3 },
119396 119871   #endif
119397 119872   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119398 119873   #if !defined(SQLITE_OMIT_CHECK)
119399 119874    {/* zName:     */ "ignore_check_constraints",
119400 119875     /* ePragTyp:  */ PragTyp_FLAG,
119401 119876     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119402 119877     /* ColNames:  */ 0, 0,
................................................................................
119410 119885     /* ColNames:  */ 0, 0,
119411 119886     /* iArg:      */ 0 },
119412 119887   #endif
119413 119888   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119414 119889    {/* zName:     */ "index_info",
119415 119890     /* ePragTyp:  */ PragTyp_INDEX_INFO,
119416 119891     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119417         -  /* ColNames:  */ 12, 3,
       119892  +  /* ColNames:  */ 15, 3,
119418 119893     /* iArg:      */ 0 },
119419 119894    {/* zName:     */ "index_list",
119420 119895     /* ePragTyp:  */ PragTyp_INDEX_LIST,
119421 119896     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119422         -  /* ColNames:  */ 21, 5,
       119897  +  /* ColNames:  */ 26, 5,
119423 119898     /* iArg:      */ 0 },
119424 119899    {/* zName:     */ "index_xinfo",
119425 119900     /* ePragTyp:  */ PragTyp_INDEX_INFO,
119426 119901     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119427 119902     /* ColNames:  */ 15, 6,
119428 119903     /* iArg:      */ 1 },
119429 119904   #endif
................................................................................
119450 119925    {/* zName:     */ "key",
119451 119926     /* ePragTyp:  */ PragTyp_KEY,
119452 119927     /* ePragFlg:  */ 0,
119453 119928     /* ColNames:  */ 0, 0,
119454 119929     /* iArg:      */ 0 },
119455 119930   #endif
119456 119931   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
       119932  + {/* zName:     */ "legacy_alter_table",
       119933  +  /* ePragTyp:  */ PragTyp_FLAG,
       119934  +  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
       119935  +  /* ColNames:  */ 0, 0,
       119936  +  /* iArg:      */ SQLITE_LegacyAlter },
119457 119937    {/* zName:     */ "legacy_file_format",
119458 119938     /* ePragTyp:  */ PragTyp_FLAG,
119459 119939     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119460 119940     /* ColNames:  */ 0, 0,
119461 119941     /* iArg:      */ SQLITE_LegacyFileFmt },
119462 119942   #endif
119463 119943   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
................................................................................
119467 119947     /* ColNames:  */ 0, 0,
119468 119948     /* iArg:      */ 0 },
119469 119949   #endif
119470 119950   #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
119471 119951    {/* zName:     */ "lock_status",
119472 119952     /* ePragTyp:  */ PragTyp_LOCK_STATUS,
119473 119953     /* ePragFlg:  */ PragFlg_Result0,
119474         -  /* ColNames:  */ 50, 2,
       119954  +  /* ColNames:  */ 43, 2,
119475 119955     /* iArg:      */ 0 },
119476 119956   #endif
119477 119957   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119478 119958    {/* zName:     */ "locking_mode",
119479 119959     /* ePragTyp:  */ PragTyp_LOCKING_MODE,
119480 119960     /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq,
119481 119961     /* ColNames:  */ 0, 0,
................................................................................
119493 119973   #endif
119494 119974   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119495 119975   #if !defined(SQLITE_OMIT_VIRTUALTABLE)
119496 119976   #if defined(SQLITE_INTROSPECTION_PRAGMAS)
119497 119977    {/* zName:     */ "module_list",
119498 119978     /* ePragTyp:  */ PragTyp_MODULE_LIST,
119499 119979     /* ePragFlg:  */ PragFlg_Result0,
119500         -  /* ColNames:  */ 31, 1,
       119980  +  /* ColNames:  */ 9, 1,
119501 119981     /* iArg:      */ 0 },
119502 119982   #endif
119503 119983   #endif
119504 119984   #endif
119505 119985    {/* zName:     */ "optimize",
119506 119986     /* ePragTyp:  */ PragTyp_OPTIMIZE,
119507 119987     /* ePragFlg:  */ PragFlg_Result1|PragFlg_NeedSchema,
................................................................................
119526 120006     /* ColNames:  */ 0, 0,
119527 120007     /* iArg:      */ 0 },
119528 120008   #endif
119529 120009   #if defined(SQLITE_INTROSPECTION_PRAGMAS)
119530 120010    {/* zName:     */ "pragma_list",
119531 120011     /* ePragTyp:  */ PragTyp_PRAGMA_LIST,
119532 120012     /* ePragFlg:  */ PragFlg_Result0,
119533         -  /* ColNames:  */ 31, 1,
       120013  +  /* ColNames:  */ 9, 1,
119534 120014     /* iArg:      */ 0 },
119535 120015   #endif
119536 120016   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119537 120017    {/* zName:     */ "query_only",
119538 120018     /* ePragTyp:  */ PragTyp_FLAG,
119539 120019     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119540 120020     /* ColNames:  */ 0, 0,
................................................................................
119557 120037     /* ePragTyp:  */ PragTyp_FLAG,
119558 120038     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119559 120039     /* ColNames:  */ 0, 0,
119560 120040     /* iArg:      */ SQLITE_RecTriggers },
119561 120041   #endif
119562 120042   #if defined(SQLITE_HAS_CODEC)
119563 120043    {/* zName:     */ "rekey",
119564         -  /* ePragTyp:  */ PragTyp_REKEY,
       120044  +  /* ePragTyp:  */ PragTyp_KEY,
119565 120045     /* ePragFlg:  */ 0,
119566 120046     /* ColNames:  */ 0, 0,
119567         -  /* iArg:      */ 0 },
       120047  +  /* iArg:      */ 1 },
119568 120048   #endif
119569 120049   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119570 120050    {/* zName:     */ "reverse_unordered_selects",
119571 120051     /* ePragTyp:  */ PragTyp_FLAG,
119572 120052     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119573 120053     /* ColNames:  */ 0, 0,
119574 120054     /* iArg:      */ SQLITE_ReverseOrder },
................................................................................
119613 120093     /* iArg:      */ SQLITE_SqlTrace },
119614 120094   #endif
119615 120095   #endif
119616 120096   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
119617 120097    {/* zName:     */ "stats",
119618 120098     /* ePragTyp:  */ PragTyp_STATS,
119619 120099     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
119620         -  /* ColNames:  */ 7, 5,
       120100  +  /* ColNames:  */ 21, 5,
119621 120101     /* iArg:      */ 0 },
119622 120102   #endif
119623 120103   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119624 120104    {/* zName:     */ "synchronous",
119625 120105     /* ePragTyp:  */ PragTyp_SYNCHRONOUS,
119626 120106     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119627 120107     /* ColNames:  */ 0, 0,
119628 120108     /* iArg:      */ 0 },
119629 120109   #endif
119630 120110   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119631 120111    {/* zName:     */ "table_info",
119632 120112     /* ePragTyp:  */ PragTyp_TABLE_INFO,
119633 120113     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119634         -  /* ColNames:  */ 1, 6,
       120114  +  /* ColNames:  */ 8, 6,
119635 120115     /* iArg:      */ 0 },
       120116  + {/* zName:     */ "table_xinfo",
       120117  +  /* ePragTyp:  */ PragTyp_TABLE_INFO,
       120118  +  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
       120119  +  /* ColNames:  */ 8, 7,
       120120  +  /* iArg:      */ 1 },
119636 120121   #endif
119637 120122   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119638 120123    {/* zName:     */ "temp_store",
119639 120124     /* ePragTyp:  */ PragTyp_TEMP_STORE,
119640 120125     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119641 120126     /* ColNames:  */ 0, 0,
119642 120127     /* iArg:      */ 0 },
119643 120128    {/* zName:     */ "temp_store_directory",
119644 120129     /* ePragTyp:  */ PragTyp_TEMP_STORE_DIRECTORY,
119645 120130     /* ePragFlg:  */ PragFlg_NoColumns1,
119646 120131     /* ColNames:  */ 0, 0,
119647 120132     /* iArg:      */ 0 },
       120133  +#endif
       120134  +#if defined(SQLITE_HAS_CODEC)
       120135  + {/* zName:     */ "textkey",
       120136  +  /* ePragTyp:  */ PragTyp_KEY,
       120137  +  /* ePragFlg:  */ 0,
       120138  +  /* ColNames:  */ 0, 0,
       120139  +  /* iArg:      */ 4 },
       120140  + {/* zName:     */ "textrekey",
       120141  +  /* ePragTyp:  */ PragTyp_KEY,
       120142  +  /* ePragFlg:  */ 0,
       120143  +  /* ColNames:  */ 0, 0,
       120144  +  /* iArg:      */ 5 },
119648 120145   #endif
119649 120146    {/* zName:     */ "threads",
119650 120147     /* ePragTyp:  */ PragTyp_THREADS,
119651 120148     /* ePragFlg:  */ PragFlg_Result0,
119652 120149     /* ColNames:  */ 0, 0,
119653 120150     /* iArg:      */ 0 },
119654 120151   #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
................................................................................
119692 120189     /* ePragTyp:  */ PragTyp_WAL_AUTOCHECKPOINT,
119693 120190     /* ePragFlg:  */ 0,
119694 120191     /* ColNames:  */ 0, 0,
119695 120192     /* iArg:      */ 0 },
119696 120193    {/* zName:     */ "wal_checkpoint",
119697 120194     /* ePragTyp:  */ PragTyp_WAL_CHECKPOINT,
119698 120195     /* ePragFlg:  */ PragFlg_NeedSchema,
119699         -  /* ColNames:  */ 46, 3,
       120196  +  /* ColNames:  */ 38, 3,
119700 120197     /* iArg:      */ 0 },
119701 120198   #endif
119702 120199   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119703 120200    {/* zName:     */ "writable_schema",
119704 120201     /* ePragTyp:  */ PragTyp_FLAG,
119705 120202     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119706 120203     /* ColNames:  */ 0, 0,
119707         -  /* iArg:      */ SQLITE_WriteSchema },
       120204  +  /* iArg:      */ SQLITE_WriteSchema|SQLITE_NoSchemaError },
119708 120205   #endif
119709 120206   };
119710         -/* Number of pragmas: 60 on by default, 77 total. */
       120207  +/* Number of pragmas: 62 on by default, 81 total. */
119711 120208   
119712 120209   /************** End of pragma.h **********************************************/
119713 120210   /************** Continuing where we left off in pragma.c *********************/
119714 120211   
119715 120212   /*
119716 120213   ** Interpret the given string as a safety level.  Return 0 for OFF,
119717 120214   ** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA.  Return 1 for an empty or 
................................................................................
120715 121212   
120716 121213   #ifndef SQLITE_OMIT_FLAG_PRAGMAS
120717 121214     case PragTyp_FLAG: {
120718 121215       if( zRight==0 ){
120719 121216         setPragmaResultColumnNames(v, pPragma);
120720 121217         returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
120721 121218       }else{
120722         -      int mask = pPragma->iArg;    /* Mask of bits to set or clear. */
       121219  +      u64 mask = pPragma->iArg;    /* Mask of bits to set or clear. */
120723 121220         if( db->autoCommit==0 ){
120724 121221           /* Foreign key support may not be enabled or disabled while not
120725 121222           ** in auto-commit mode.  */
120726 121223           mask &= ~(SQLITE_ForeignKeys);
120727 121224         }
120728 121225   #if SQLITE_USER_AUTHENTICATION
120729 121226         if( db->auth.authLevel==UAUTH_User ){
................................................................................
120764 121261     ** dflt_value: The default value for the column, if any.
120765 121262     ** pk:         Non-zero for PK fields.
120766 121263     */
120767 121264     case PragTyp_TABLE_INFO: if( zRight ){
120768 121265       Table *pTab;
120769 121266       pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
120770 121267       if( pTab ){
       121268  +      int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
120771 121269         int i, k;
120772 121270         int nHidden = 0;
120773 121271         Column *pCol;
120774 121272         Index *pPk = sqlite3PrimaryKeyIndex(pTab);
120775         -      pParse->nMem = 6;
120776         -      sqlite3CodeVerifySchema(pParse, iDb);
       121273  +      pParse->nMem = 7;
       121274  +      sqlite3CodeVerifySchema(pParse, iTabDb);
120777 121275         sqlite3ViewGetColumnNames(pParse, pTab);
120778 121276         for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
120779         -        if( IsHiddenColumn(pCol) ){
       121277  +        int isHidden = IsHiddenColumn(pCol);
       121278  +        if( isHidden && pPragma->iArg==0 ){
120780 121279             nHidden++;
120781 121280             continue;
120782 121281           }
120783 121282           if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
120784 121283             k = 0;
120785 121284           }else if( pPk==0 ){
120786 121285             k = 1;
120787 121286           }else{
120788 121287             for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
120789 121288           }
120790 121289           assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN );
120791         -        sqlite3VdbeMultiLoad(v, 1, "issisi",
       121290  +        sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
120792 121291                  i-nHidden,
120793 121292                  pCol->zName,
120794 121293                  sqlite3ColumnType(pCol,""),
120795 121294                  pCol->notNull ? 1 : 0,
120796 121295                  pCol->pDflt ? pCol->pDflt->u.zToken : 0,
120797         -               k);
       121296  +               k,
       121297  +               isHidden);
120798 121298         }
120799 121299       }
120800 121300     }
120801 121301     break;
120802 121302   
120803 121303   #ifdef SQLITE_DEBUG
120804 121304     case PragTyp_STATS: {
................................................................................
120828 121328   #endif
120829 121329   
120830 121330     case PragTyp_INDEX_INFO: if( zRight ){
120831 121331       Index *pIdx;
120832 121332       Table *pTab;
120833 121333       pIdx = sqlite3FindIndex(db, zRight, zDb);
120834 121334       if( pIdx ){
       121335  +      int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
120835 121336         int i;
120836 121337         int mx;
120837 121338         if( pPragma->iArg ){
120838 121339           /* PRAGMA index_xinfo (newer version with more rows and columns) */
120839 121340           mx = pIdx->nColumn;
120840 121341           pParse->nMem = 6;
120841 121342         }else{
120842 121343           /* PRAGMA index_info (legacy version) */
120843 121344           mx = pIdx->nKeyCol;
120844 121345           pParse->nMem = 3;
120845 121346         }
120846 121347         pTab = pIdx->pTable;
120847         -      sqlite3CodeVerifySchema(pParse, iDb);
       121348  +      sqlite3CodeVerifySchema(pParse, iIdxDb);
120848 121349         assert( pParse->nMem<=pPragma->nPragCName );
120849 121350         for(i=0; i<mx; i++){
120850 121351           i16 cnum = pIdx->aiColumn[i];
120851 121352           sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
120852 121353                                cnum<0 ? 0 : pTab->aCol[cnum].zName);
120853 121354           if( pPragma->iArg ){
120854 121355             sqlite3VdbeMultiLoad(v, 4, "isiX",
................................................................................
120864 121365   
120865 121366     case PragTyp_INDEX_LIST: if( zRight ){
120866 121367       Index *pIdx;
120867 121368       Table *pTab;
120868 121369       int i;
120869 121370       pTab = sqlite3FindTable(db, zRight, zDb);
120870 121371       if( pTab ){
       121372  +      int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
120871 121373         pParse->nMem = 5;
120872         -      sqlite3CodeVerifySchema(pParse, iDb);
       121374  +      sqlite3CodeVerifySchema(pParse, iTabDb);
120873 121375         for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
120874 121376           const char *azOrigin[] = { "c", "u", "pk" };
120875 121377           sqlite3VdbeMultiLoad(v, 1, "isisi",
120876 121378              i,
120877 121379              pIdx->zName,
120878 121380              IsUniqueIndex(pIdx),
120879 121381              azOrigin[pIdx->idxType],
................................................................................
120912 121414     case PragTyp_FUNCTION_LIST: {
120913 121415       int i;
120914 121416       HashElem *j;
120915 121417       FuncDef *p;
120916 121418       pParse->nMem = 2;
120917 121419       for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
120918 121420         for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
       121421  +        if( p->funcFlags & SQLITE_FUNC_INTERNAL ) continue;
120919 121422           sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 1);
120920 121423         }
120921 121424       }
120922 121425       for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
120923 121426         p = (FuncDef*)sqliteHashData(j);
120924 121427         sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 0);
120925 121428       }
................................................................................
120953 121456     case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
120954 121457       FKey *pFK;
120955 121458       Table *pTab;
120956 121459       pTab = sqlite3FindTable(db, zRight, zDb);
120957 121460       if( pTab ){
120958 121461         pFK = pTab->pFKey;
120959 121462         if( pFK ){
       121463  +        int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
120960 121464           int i = 0; 
120961 121465           pParse->nMem = 8;
120962         -        sqlite3CodeVerifySchema(pParse, iDb);
       121466  +        sqlite3CodeVerifySchema(pParse, iTabDb);
120963 121467           while(pFK){
120964 121468             int j;
120965 121469             for(j=0; j<pFK->nCol; j++){
120966 121470               sqlite3VdbeMultiLoad(v, 1, "iissssss",
120967 121471                      i,
120968 121472                      j,
120969 121473                      pFK->zTo,
................................................................................
121000 121504       int addrOk;            /* Jump here if the key is OK */
121001 121505       int *aiCols;           /* child to parent column mapping */
121002 121506   
121003 121507       regResult = pParse->nMem+1;
121004 121508       pParse->nMem += 4;
121005 121509       regKey = ++pParse->nMem;
121006 121510       regRow = ++pParse->nMem;
121007         -    sqlite3CodeVerifySchema(pParse, iDb);
121008 121511       k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
121009 121512       while( k ){
       121513  +      int iTabDb;
121010 121514         if( zRight ){
121011 121515           pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
121012 121516           k = 0;
121013 121517         }else{
121014 121518           pTab = (Table*)sqliteHashData(k);
121015 121519           k = sqliteHashNext(k);
121016 121520         }
121017 121521         if( pTab==0 || pTab->pFKey==0 ) continue;
121018         -      sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
       121522  +      iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       121523  +      sqlite3CodeVerifySchema(pParse, iTabDb);
       121524  +      sqlite3TableLock(pParse, iTabDb, pTab->tnum, 0, pTab->zName);
121019 121525         if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
121020         -      sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
       121526  +      sqlite3OpenTable(pParse, 0, iTabDb, pTab, OP_OpenRead);
121021 121527         sqlite3VdbeLoadString(v, regResult, pTab->zName);
121022 121528         for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
121023 121529           pParent = sqlite3FindTable(db, pFK->zTo, zDb);
121024 121530           if( pParent==0 ) continue;
121025 121531           pIdx = 0;
121026         -        sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
       121532  +        sqlite3TableLock(pParse, iTabDb, pParent->tnum, 0, pParent->zName);
121027 121533           x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
121028 121534           if( x==0 ){
121029 121535             if( pIdx==0 ){
121030         -            sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
       121536  +            sqlite3OpenTable(pParse, i, iTabDb, pParent, OP_OpenRead);
121031 121537             }else{
121032         -            sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
       121538  +            sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iTabDb);
121033 121539               sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
121034 121540             }
121035 121541           }else{
121036 121542             k = 0;
121037 121543             break;
121038 121544           }
121039 121545         }
................................................................................
121794 122300         sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
121795 122301       }
121796 122302       break;
121797 122303     }
121798 122304   #endif
121799 122305   
121800 122306   #ifdef SQLITE_HAS_CODEC
       122307  +  /* Pragma        iArg
       122308  +  ** ----------   ------
       122309  +  **  key           0
       122310  +  **  rekey         1
       122311  +  **  hexkey        2
       122312  +  **  hexrekey      3
       122313  +  **  textkey       4
       122314  +  **  textrekey     5
       122315  +  */
121801 122316     case PragTyp_KEY: {
121802         -    if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
121803         -    break;
121804         -  }
121805         -  case PragTyp_REKEY: {
121806         -    if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
       122317  +    if( zRight ){
       122318  +      int n = pPragma->iArg<4 ? sqlite3Strlen30(zRight) : -1;
       122319  +      if( (pPragma->iArg & 1)==0 ){
       122320  +        sqlite3_key_v2(db, zDb, zRight, n);
       122321  +      }else{
       122322  +        sqlite3_rekey_v2(db, zDb, zRight, n);
       122323  +      }
       122324  +    }
121807 122325       break;
121808 122326     }
121809 122327     case PragTyp_HEXKEY: {
121810 122328       if( zRight ){
121811 122329         u8 iByte;
121812 122330         int i;
121813 122331         char zKey[40];
121814 122332         for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
121815 122333           iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
121816 122334           if( (i&1)!=0 ) zKey[i/2] = iByte;
121817 122335         }
121818         -      if( (zLeft[3] & 0xf)==0xb ){
       122336  +      if( (pPragma->iArg & 1)==0 ){
121819 122337           sqlite3_key_v2(db, zDb, zKey, i/2);
121820 122338         }else{
121821 122339           sqlite3_rekey_v2(db, zDb, zKey, i/2);
121822 122340         }
121823 122341       }
121824 122342       break;
121825 122343     }
................................................................................
122141 122659     0,                           /* xSync - sync transaction */
122142 122660     0,                           /* xCommit - commit transaction */
122143 122661     0,                           /* xRollback - rollback transaction */
122144 122662     0,                           /* xFindFunction - function overloading */
122145 122663     0,                           /* xRename - rename the table */
122146 122664     0,                           /* xSavepoint */
122147 122665     0,                           /* xRelease */
122148         -  0                            /* xRollbackTo */
       122666  +  0,                           /* xRollbackTo */
       122667  +  0                            /* xShadowName */
122149 122668   };
122150 122669   
122151 122670   /*
122152 122671   ** Check to see if zTabName is really the name of a pragma.  If it is,
122153 122672   ** then register an eponymous virtual table for that pragma and return
122154 122673   ** a pointer to the Module object for the new virtual table.
122155 122674   */
................................................................................
122494 123013       }
122495 123014   #endif
122496 123015     }
122497 123016     if( db->mallocFailed ){
122498 123017       rc = SQLITE_NOMEM_BKPT;
122499 123018       sqlite3ResetAllSchemasOfConnection(db);
122500 123019     }
122501         -  if( rc==SQLITE_OK || (db->flags&SQLITE_WriteSchema)){
122502         -    /* Black magic: If the SQLITE_WriteSchema flag is set, then consider
       123020  +  if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){
       123021  +    /* Black magic: If the SQLITE_NoSchemaError flag is set, then consider
122503 123022       ** the schema loaded, even if errors occurred. In this situation the 
122504 123023       ** current sqlite3_prepare() operation will fail, but the following one
122505 123024       ** will attempt to compile the supplied statement against whatever subset
122506 123025       ** of the schema was loaded before the error occurred. The primary
122507 123026       ** purpose of this is to allow access to the sqlite_master table
122508 123027       ** even when its contents have been corrupted.
122509 123028       */
................................................................................
122876 123395     sqlite3BtreeLeaveAll(db);
122877 123396     rc = sqlite3ApiExit(db, rc);
122878 123397     assert( (rc&db->errMask)==rc );
122879 123398     sqlite3_mutex_leave(db->mutex);
122880 123399     return rc;
122881 123400   }
122882 123401   
       123402  +#ifdef SQLITE_ENABLE_NORMALIZE
       123403  +/*
       123404  +** Checks if the specified token is a table, column, or function name,
       123405  +** based on the databases associated with the statement being prepared.
       123406  +** If the function fails, zero is returned and pRc is filled with the
       123407  +** error code.
       123408  +*/
       123409  +static int shouldTreatAsIdentifier(
       123410  +  sqlite3 *db,        /* Database handle. */
       123411  +  const char *zToken, /* Pointer to start of token to be checked */
       123412  +  int nToken,         /* Length of token to be checked */
       123413  +  int *pRc            /* Pointer to error code upon failure */
       123414  +){
       123415  +  int bFound = 0;     /* Non-zero if token is an identifier name. */
       123416  +  int i, j;           /* Database and column loop indexes. */
       123417  +  Schema *pSchema;    /* Schema for current database. */
       123418  +  Hash *pHash;        /* Hash table of tables for current database. */
       123419  +  HashElem *e;        /* Hash element for hash table iteration. */
       123420  +  Table *pTab;        /* Database table for columns being checked. */
       123421  +
       123422  +  if( sqlite3IsRowidN(zToken, nToken) ){
       123423  +    return 1;
       123424  +  }
       123425  +  if( nToken>0 ){
       123426  +    int hash = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zToken[0]], nToken);
       123427  +    if( sqlite3FunctionSearchN(hash, zToken, nToken) ) return 1;
       123428  +  }
       123429  +  assert( db!=0 );
       123430  +  sqlite3_mutex_enter(db->mutex);
       123431  +  sqlite3BtreeEnterAll(db);
       123432  +  for(i=0; i<db->nDb; i++){
       123433  +    pHash = &db->aFunc;
       123434  +    if( sqlite3HashFindN(pHash, zToken, nToken) ){
       123435  +      bFound = 1;
       123436  +      break;
       123437  +    }
       123438  +    pSchema = db->aDb[i].pSchema;
       123439  +    if( pSchema==0 ) continue;
       123440  +    pHash = &pSchema->tblHash;
       123441  +    if( sqlite3HashFindN(pHash, zToken, nToken) ){
       123442  +      bFound = 1;
       123443  +      break;
       123444  +    }
       123445  +    for(e=sqliteHashFirst(pHash); e; e=sqliteHashNext(e)){
       123446  +      pTab = sqliteHashData(e);
       123447  +      if( pTab==0 ) continue;
       123448  +      pHash = pTab->pColHash;
       123449  +      if( pHash==0 ){
       123450  +        pTab->pColHash = pHash = sqlite3_malloc(sizeof(Hash));
       123451  +        if( pHash ){
       123452  +          sqlite3HashInit(pHash);
       123453  +          for(j=0; j<pTab->nCol; j++){
       123454  +            Column *pCol = &pTab->aCol[j];
       123455  +            sqlite3HashInsert(pHash, pCol->zName, pCol);
       123456  +          }
       123457  +        }else{
       123458  +          *pRc = SQLITE_NOMEM_BKPT;
       123459  +          bFound = 0;
       123460  +          goto done;
       123461  +        }
       123462  +      }
       123463  +      if( pHash && sqlite3HashFindN(pHash, zToken, nToken) ){
       123464  +        bFound = 1;
       123465  +        goto done;
       123466  +      }
       123467  +    }
       123468  +  }
       123469  +done:
       123470  +  sqlite3BtreeLeaveAll(db);
       123471  +  sqlite3_mutex_leave(db->mutex);
       123472  +  return bFound;
       123473  +}
       123474  +
       123475  +/*
       123476  +** Attempt to estimate the final output buffer size needed for the fully
       123477  +** normalized version of the specified SQL string.  This should take into
       123478  +** account any potential expansion that could occur (e.g. via IN clauses
       123479  +** being expanded, etc).  This size returned is the total number of bytes
       123480  +** including the NUL terminator.
       123481  +*/
       123482  +static int estimateNormalizedSize(
       123483  +  const char *zSql, /* The original SQL string */
       123484  +  int nSql,         /* Length of original SQL string */
       123485  +  u8 prepFlags      /* The flags passed to sqlite3_prepare_v3() */
       123486  +){
       123487  +  int nOut = nSql + 4;
       123488  +  const char *z = zSql;
       123489  +  while( nOut<nSql*5 ){
       123490  +    while( z[0]!=0 && z[0]!='I' && z[0]!='i' ){ z++; }
       123491  +    if( z[0]==0 ) break;
       123492  +    z++;
       123493  +    if( z[0]!='N' && z[0]!='n' ) break;
       123494  +    z++;
       123495  +    while( sqlite3Isspace(z[0]) ){ z++; }
       123496  +    if( z[0]!='(' ) break;
       123497  +    z++;
       123498  +    nOut += 5; /* ?,?,? */
       123499  +  }
       123500  +  return nOut;
       123501  +}
       123502  +
       123503  +/*
       123504  +** Copy the current token into the output buffer while dealing with quoted
       123505  +** identifiers.  By default, all letters will be converted into lowercase.
       123506  +** If the bUpper flag is set, uppercase will be used.  The piOut argument
       123507  +** will be used to update the target index into the output string.
       123508  +*/
       123509  +static void copyNormalizedToken(
       123510  +  const char *zSql, /* The original SQL string */
       123511  +  int iIn,          /* Current index into the original SQL string */
       123512  +  int nToken,       /* Number of bytes in the current token */
       123513  +  int tokenFlags,   /* Flags returned by the tokenizer */
       123514  +  char *zOut,       /* The output string */
       123515  +  int *piOut        /* Pointer to target index into the output string */
       123516  +){
       123517  +  int bQuoted = tokenFlags & SQLITE_TOKEN_QUOTED;
       123518  +  int bKeyword = tokenFlags & SQLITE_TOKEN_KEYWORD;
       123519  +  int j = *piOut, k = 0;
       123520  +  for(; k<nToken; k++){
       123521  +    if( bQuoted ){
       123522  +      if( k==0 && iIn>0 ){
       123523  +        zOut[j++] = '"';
       123524  +        continue;
       123525  +      }else if( k==nToken-1 ){
       123526  +        zOut[j++] = '"';
       123527  +        continue;
       123528  +      }
       123529  +    }
       123530  +    if( bKeyword ){
       123531  +      zOut[j++] = sqlite3Toupper(zSql[iIn+k]);
       123532  +    }else{
       123533  +      zOut[j++] = sqlite3Tolower(zSql[iIn+k]);
       123534  +    }
       123535  +  }
       123536  +  *piOut = j;
       123537  +}
       123538  +
       123539  +/*
       123540  +** Perform normalization of the SQL contained in the prepared statement and
       123541  +** store the result in the zNormSql field.  The schema for the associated
       123542  +** databases are consulted while performing the normalization in order to
       123543  +** determine if a token appears to be an identifier.  All identifiers are
       123544  +** left intact in the normalized SQL and all literals are replaced with a
       123545  +** single '?'.
       123546  +*/
       123547  +SQLITE_PRIVATE void sqlite3Normalize(
       123548  +  Vdbe *pVdbe,      /* VM being reprepared */
       123549  +  const char *zSql, /* The original SQL string */
       123550  +  int nSql,         /* Size of the input string in bytes */
       123551  +  u8 prepFlags      /* The flags passed to sqlite3_prepare_v3() */
       123552  +){
       123553  +  sqlite3 *db;           /* Database handle. */
       123554  +  char *z;               /* The output string */
       123555  +  int nZ;                /* Size of the output string in bytes */
       123556  +  int i;                 /* Next character to read from zSql[] */
       123557  +  int j;                 /* Next character to fill in on z[] */
       123558  +  int tokenType = 0;     /* Type of the next token */
       123559  +  int prevTokenType = 0; /* Type of the previous token, except spaces */
       123560  +  int n;                 /* Size of the next token */
       123561  +  int nParen = 0;        /* Nesting level of parenthesis */
       123562  +  Hash inHash;           /* Table of parenthesis levels to output index. */
       123563  +
       123564  +  db = sqlite3VdbeDb(pVdbe);
       123565  +  assert( db!=0 );
       123566  +  assert( pVdbe->zNormSql==0 );
       123567  +  if( zSql==0 ) return;
       123568  +  nZ = estimateNormalizedSize(zSql, nSql, prepFlags);
       123569  +  z = sqlite3DbMallocRawNN(db, nZ);
       123570  +  if( z==0 ) return;
       123571  +  sqlite3HashInit(&inHash);
       123572  +  for(i=j=0; i<nSql && zSql[i]; i+=n){
       123573  +    int flags = 0;
       123574  +    if( tokenType!=TK_SPACE ) prevTokenType = tokenType;
       123575  +    n = sqlite3GetTokenNormalized((unsigned char*)zSql+i, &tokenType, &flags);
       123576  +    switch( tokenType ){
       123577  +      case TK_SPACE: {
       123578  +        break;
       123579  +      }
       123580  +      case TK_ILLEGAL: {
       123581  +        sqlite3DbFree(db, z);
       123582  +        sqlite3HashClear(&inHash);
       123583  +        return;
       123584  +      }
       123585  +      case TK_STRING:
       123586  +      case TK_INTEGER:
       123587  +      case TK_FLOAT:
       123588  +      case TK_VARIABLE:
       123589  +      case TK_BLOB: {
       123590  +        z[j++] = '?';
       123591  +        break;
       123592  +      }
       123593  +      case TK_LP:
       123594  +      case TK_RP: {
       123595  +        if( tokenType==TK_LP ){
       123596  +          nParen++;
       123597  +          if( prevTokenType==TK_IN ){
       123598  +            assert( nParen<nSql );
       123599  +            sqlite3HashInsert(&inHash, zSql+nParen, SQLITE_INT_TO_PTR(j));
       123600  +          }
       123601  +        }else{
       123602  +          int jj;
       123603  +          assert( nParen<nSql );
       123604  +          jj = SQLITE_PTR_TO_INT(sqlite3HashFind(&inHash, zSql+nParen));
       123605  +          if( jj>0 ){
       123606  +            sqlite3HashInsert(&inHash, zSql+nParen, 0);
       123607  +            assert( jj+6<nZ );
       123608  +            memcpy(z+jj+1, "?,?,?", 5);
       123609  +            j = jj+6;
       123610  +            assert( nZ-1-j>=0 );
       123611  +            assert( nZ-1-j<nZ );
       123612  +            memset(z+j, 0, nZ-1-j);
       123613  +          }
       123614  +          nParen--;
       123615  +        }
       123616  +        assert( nParen>=0 );
       123617  +        /* Fall through */
       123618  +      }
       123619  +      case TK_MINUS:
       123620  +      case TK_SEMI:
       123621  +      case TK_PLUS:
       123622  +      case TK_STAR:
       123623  +      case TK_SLASH:
       123624  +      case TK_REM:
       123625  +      case TK_EQ:
       123626  +      case TK_LE:
       123627  +      case TK_NE:
       123628  +      case TK_LSHIFT:
       123629  +      case TK_LT:
       123630  +      case TK_RSHIFT:
       123631  +      case TK_GT:
       123632  +      case TK_GE:
       123633  +      case TK_BITOR:
       123634  +      case TK_CONCAT:
       123635  +      case TK_COMMA:
       123636  +      case TK_BITAND:
       123637  +      case TK_BITNOT:
       123638  +      case TK_DOT:
       123639  +      case TK_IN:
       123640  +      case TK_IS:
       123641  +      case TK_NOT:
       123642  +      case TK_NULL:
       123643  +      case TK_ID: {
       123644  +        if( tokenType==TK_NULL ){
       123645  +          if( prevTokenType==TK_IS || prevTokenType==TK_NOT ){
       123646  +            /* NULL is a keyword in this case, not a literal value */
       123647  +          }else{
       123648  +            /* Here the NULL is a literal value */
       123649  +            z[j++] = '?';
       123650  +            break;
       123651  +          }
       123652  +        }
       123653  +        if( j>0 && sqlite3IsIdChar(z[j-1]) && sqlite3IsIdChar(zSql[i]) ){
       123654  +          z[j++] = ' ';
       123655  +        }
       123656  +        if( tokenType==TK_ID ){
       123657  +          int i2 = i, n2 = n, rc = SQLITE_OK;
       123658  +          if( nParen>0 ){
       123659  +            assert( nParen<nSql );
       123660  +            sqlite3HashInsert(&inHash, zSql+nParen, 0);
       123661  +          }
       123662  +          if( flags&SQLITE_TOKEN_QUOTED ){ i2++; n2-=2; }
       123663  +          if( shouldTreatAsIdentifier(db, zSql+i2, n2, &rc)==0 ){
       123664  +            if( rc!=SQLITE_OK ){
       123665  +              sqlite3DbFree(db, z);
       123666  +              sqlite3HashClear(&inHash);
       123667  +              return;
       123668  +            }
       123669  +            if( sqlite3_keyword_check(zSql+i2, n2)==0 ){
       123670  +              z[j++] = '?';
       123671  +              break;
       123672  +            }
       123673  +          }
       123674  +        }
       123675  +        copyNormalizedToken(zSql, i, n, flags, z, &j);
       123676  +        break;
       123677  +      }
       123678  +    }
       123679  +  }
       123680  +  assert( j<nZ && "one" );
       123681  +  while( j>0 && z[j-1]==' ' ){ j--; }
       123682  +  if( j>0 && z[j-1]!=';' ){ z[j++] = ';'; }
       123683  +  z[j] = 0;
       123684  +  assert( j<nZ && "two" );
       123685  +  pVdbe->zNormSql = z;
       123686  +  sqlite3HashClear(&inHash);
       123687  +}
       123688  +#endif /* SQLITE_ENABLE_NORMALIZE */
       123689  +
122883 123690   /*
122884 123691   ** Rerun the compilation of a statement after a schema change.
122885 123692   **
122886 123693   ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
122887 123694   ** if the statement cannot be recompiled because another connection has
122888 123695   ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
122889 123696   ** occurs, return SQLITE_SCHEMA.
................................................................................
123888 124695     int i;
123889 124696     int nDefer = 0;
123890 124697     ExprList *pExtra = 0;
123891 124698     for(i=0; i<pEList->nExpr; i++){
123892 124699       struct ExprList_item *pItem = &pEList->a[i];
123893 124700       if( pItem->u.x.iOrderByCol==0 ){
123894 124701         Expr *pExpr = pItem->pExpr;
123895         -      Table *pTab = pExpr->pTab;
       124702  +      Table *pTab = pExpr->y.pTab;
123896 124703         if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)
123897 124704          && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
123898 124705         ){
123899 124706           int j;
123900 124707           for(j=0; j<nDefer; j++){
123901 124708             if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
123902 124709           }
................................................................................
123911 124718                 pPk = sqlite3PrimaryKeyIndex(pTab);
123912 124719                 nKey = pPk->nKeyCol;
123913 124720               }
123914 124721               for(k=0; k<nKey; k++){
123915 124722                 Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
123916 124723                 if( pNew ){
123917 124724                   pNew->iTable = pExpr->iTable;
123918         -                pNew->pTab = pExpr->pTab;
       124725  +                pNew->y.pTab = pExpr->y.pTab;
123919 124726                   pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
123920 124727                   pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
123921 124728                 }
123922 124729               }
123923         -            pSort->aDefer[nDefer].pTab = pExpr->pTab;
       124730  +            pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
123924 124731               pSort->aDefer[nDefer].iCsr = pExpr->iTable;
123925 124732               pSort->aDefer[nDefer].nKey = nKey;
123926 124733               nDefer++;
123927 124734             }
123928 124735           }
123929 124736           pItem->bSorterRef = 1;
123930 124737         }
................................................................................
124765 125572           ** This is not a problem, as the column type of "t1.col" is never
124766 125573           ** used. When columnType() is called on the expression 
124767 125574           ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
124768 125575           ** branch below.  */
124769 125576           break;
124770 125577         }
124771 125578   
124772         -      assert( pTab && pExpr->pTab==pTab );
       125579  +      assert( pTab && pExpr->y.pTab==pTab );
124773 125580         if( pS ){
124774 125581           /* The "table" is actually a sub-select or a view in the FROM clause
124775 125582           ** of the SELECT statement. Return the declaration type and origin
124776 125583           ** data for the result-set column of the sub-select.
124777 125584           */
124778 125585           if( iCol>=0 && iCol<pS->pEList->nExpr ){
124779 125586             /* If iCol is less than zero, then the expression requests the
................................................................................
124950 125757     srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
124951 125758     sqlite3VdbeSetNumCols(v, pEList->nExpr);
124952 125759     for(i=0; i<pEList->nExpr; i++){
124953 125760       Expr *p = pEList->a[i].pExpr;
124954 125761   
124955 125762       assert( p!=0 );
124956 125763       assert( p->op!=TK_AGG_COLUMN );  /* Agg processing has not run yet */
124957         -    assert( p->op!=TK_COLUMN || p->pTab!=0 ); /* Covering idx not yet coded */
       125764  +    assert( p->op!=TK_COLUMN || p->y.pTab!=0 ); /* Covering idx not yet coded */
124958 125765       if( pEList->a[i].zName ){
124959 125766         /* An AS clause always takes first priority */
124960 125767         char *zName = pEList->a[i].zName;
124961 125768         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
124962 125769       }else if( srcName && p->op==TK_COLUMN ){
124963 125770         char *zCol;
124964 125771         int iCol = p->iColumn;
124965         -      pTab = p->pTab;
       125772  +      pTab = p->y.pTab;
124966 125773         assert( pTab!=0 );
124967 125774         if( iCol<0 ) iCol = pTab->iPKey;
124968 125775         assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
124969 125776         if( iCol<0 ){
124970 125777           zCol = "rowid";
124971 125778         }else{
124972 125779           zCol = pTab->aCol[iCol].zName;
................................................................................
125049 125856           pColExpr = pColExpr->pRight;
125050 125857           assert( pColExpr!=0 );
125051 125858         }
125052 125859         assert( pColExpr->op!=TK_AGG_COLUMN );
125053 125860         if( pColExpr->op==TK_COLUMN ){
125054 125861           /* For columns use the column name name */
125055 125862           int iCol = pColExpr->iColumn;
125056         -        Table *pTab = pColExpr->pTab;
       125863  +        Table *pTab = pColExpr->y.pTab;
125057 125864           assert( pTab!=0 );
125058 125865           if( iCol<0 ) iCol = pTab->iPKey;
125059 125866           zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
125060 125867         }else if( pColExpr->op==TK_ID ){
125061 125868           assert( !ExprHasProperty(pColExpr, EP_IntValue) );
125062 125869           zName = pColExpr->u.zToken;
125063 125870         }else{
................................................................................
125402 126209     int eDest = SRT_Fifo;         /* How to write to Queue */
125403 126210     SelectDest destQueue;         /* SelectDest targetting the Queue table */
125404 126211     int i;                        /* Loop counter */
125405 126212     int rc;                       /* Result code */
125406 126213     ExprList *pOrderBy;           /* The ORDER BY clause */
125407 126214     Expr *pLimit;                 /* Saved LIMIT and OFFSET */
125408 126215     int regLimit, regOffset;      /* Registers used by LIMIT and OFFSET */
       126216  +
       126217  +#ifndef SQLITE_OMIT_WINDOWFUNC
       126218  +  if( p->pWin ){
       126219  +    sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries");
       126220  +    return;
       126221  +  }
       126222  +#endif
125409 126223   
125410 126224     /* Obtain authorization to do a recursive query */
125411 126225     if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
125412 126226   
125413 126227     /* Process the LIMIT and OFFSET clauses, if they exist */
125414 126228     addrBreak = sqlite3VdbeMakeLabel(v);
125415 126229     p->nSelectRow = 320;  /* 4 billion rows */
................................................................................
127152 127966   #endif
127153 127967   
127154 127968     return 1;
127155 127969   }
127156 127970   #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
127157 127971   
127158 127972   /*
127159         -** A structure to keep track of all of the column values that fixed to
       127973  +** A structure to keep track of all of the column values that are fixed to
127160 127974   ** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
127161 127975   */
127162 127976   typedef struct WhereConst WhereConst;
127163 127977   struct WhereConst {
127164 127978     Parse *pParse;   /* Parsing context */
127165 127979     int nConst;      /* Number for COLUMN=CONSTANT terms */
127166 127980     int nChng;       /* Number of times a constant is propagated */
127167 127981     Expr **apExpr;   /* [i*2] is COLUMN and [i*2+1] is VALUE */
127168 127982   };
127169 127983   
127170 127984   /*
127171         -** Add a new entry to the pConst object
       127985  +** Add a new entry to the pConst object.  Except, do not add duplicate
       127986  +** pColumn entires.
127172 127987   */
127173 127988   static void constInsert(
127174         -  WhereConst *pConst,
127175         -  Expr *pColumn,
127176         -  Expr *pValue
       127989  +  WhereConst *pConst,      /* The WhereConst into which we are inserting */
       127990  +  Expr *pColumn,           /* The COLUMN part of the constraint */
       127991  +  Expr *pValue             /* The VALUE part of the constraint */
127177 127992   ){
       127993  +  int i;
       127994  +  assert( pColumn->op==TK_COLUMN );
       127995  +
       127996  +  /* 2018-10-25 ticket [cf5ed20f]
       127997  +  ** Make sure the same pColumn is not inserted more than once */
       127998  +  for(i=0; i<pConst->nConst; i++){
       127999  +    const Expr *pExpr = pConst->apExpr[i*2];
       128000  +    assert( pExpr->op==TK_COLUMN );
       128001  +    if( pExpr->iTable==pColumn->iTable
       128002  +     && pExpr->iColumn==pColumn->iColumn
       128003  +    ){
       128004  +      return;  /* Already present.  Return without doing anything. */
       128005  +    }
       128006  +  }
127178 128007   
127179 128008     pConst->nConst++;
127180 128009     pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
127181 128010                            pConst->nConst*2*sizeof(Expr*));
127182 128011     if( pConst->apExpr==0 ){
127183 128012       pConst->nConst = 0;
127184 128013     }else{
................................................................................
129171 130000         int regGosub = ++pParse->nMem;
129172 130001   
129173 130002         sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub);
129174 130003   
129175 130004         sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
129176 130005         sqlite3VdbeResolveLabel(v, addrGosub);
129177 130006         VdbeNoopComment((v, "inner-loop subroutine"));
       130007  +      sSort.labelOBLopt = 0;
129178 130008         selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
129179 130009         sqlite3VdbeResolveLabel(v, iCont);
129180 130010         sqlite3VdbeAddOp1(v, OP_Return, regGosub);
129181 130011         VdbeComment((v, "end inner-loop subroutine"));
129182 130012         sqlite3VdbeResolveLabel(v, iBreak);
129183 130013       }else
129184 130014   #endif /* SQLITE_OMIT_WINDOWFUNC */
................................................................................
131150 131980     }
131151 131981   #ifndef SQLITE_OMIT_FLOATING_POINT
131152 131982     if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
131153 131983       sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
131154 131984     }
131155 131985   #endif
131156 131986   }
       131987  +
       131988  +/*
       131989  +** Check to see if column iCol of index pIdx references any of the
       131990  +** columns defined by aXRef and chngRowid.  Return true if it does
       131991  +** and false if not.  This is an optimization.  False-positives are a
       131992  +** performance degradation, but false-negatives can result in a corrupt
       131993  +** index and incorrect answers.
       131994  +**
       131995  +** aXRef[j] will be non-negative if column j of the original table is
       131996  +** being updated.  chngRowid will be true if the rowid of the table is
       131997  +** being updated.
       131998  +*/
       131999  +static int indexColumnIsBeingUpdated(
       132000  +  Index *pIdx,      /* The index to check */
       132001  +  int iCol,         /* Which column of the index to check */
       132002  +  int *aXRef,       /* aXRef[j]>=0 if column j is being updated */
       132003  +  int chngRowid     /* true if the rowid is being updated */
       132004  +){
       132005  +  i16 iIdxCol = pIdx->aiColumn[iCol];
       132006  +  assert( iIdxCol!=XN_ROWID ); /* Cannot index rowid */
       132007  +  if( iIdxCol>=0 ){
       132008  +    return aXRef[iIdxCol]>=0;
       132009  +  }
       132010  +  assert( iIdxCol==XN_EXPR );
       132011  +  assert( pIdx->aColExpr!=0 );
       132012  +  assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
       132013  +  return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
       132014  +                                            aXRef,chngRowid);
       132015  +}
       132016  +
       132017  +/*
       132018  +** Check to see if index pIdx is a partial index whose conditional
       132019  +** expression might change values due to an UPDATE.  Return true if
       132020  +** the index is subject to change and false if the index is guaranteed
       132021  +** to be unchanged.  This is an optimization.  False-positives are a
       132022  +** performance degradation, but false-negatives can result in a corrupt
       132023  +** index and incorrect answers.
       132024  +**
       132025  +** aXRef[j] will be non-negative if column j of the original table is
       132026  +** being updated.  chngRowid will be true if the rowid of the table is
       132027  +** being updated.
       132028  +*/
       132029  +static int indexWhereClauseMightChange(
       132030  +  Index *pIdx,      /* The index to check */
       132031  +  int *aXRef,       /* aXRef[j]>=0 if column j is being updated */
       132032  +  int chngRowid     /* true if the rowid is being updated */
       132033  +){
       132034  +  if( pIdx->pPartIdxWhere==0 ) return 0;
       132035  +  return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
       132036  +                                            aXRef, chngRowid);
       132037  +}
131157 132038   
131158 132039   /*
131159 132040   ** Process an UPDATE statement.
131160 132041   **
131161 132042   **   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
131162 132043   **          \_______/ \________/     \______/       \________________/
131163 132044   *            onError   pTabList      pChanges             pWhere
................................................................................
131374 132255     pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
131375 132256   
131376 132257     hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
131377 132258   
131378 132259     /* There is one entry in the aRegIdx[] array for each index on the table
131379 132260     ** being updated.  Fill in aRegIdx[] with a register number that will hold
131380 132261     ** the key for accessing each index.
131381         -  **
131382         -  ** FIXME:  Be smarter about omitting indexes that use expressions.
131383 132262     */
131384 132263     for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
131385 132264       int reg;
131386         -    if( chngKey || hasFK>1 || pIdx->pPartIdxWhere || pIdx==pPk ){
       132265  +    if( chngKey || hasFK>1 || pIdx==pPk
       132266  +     || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
       132267  +    ){
131387 132268         reg = ++pParse->nMem;
131388 132269         pParse->nMem += pIdx->nColumn;
131389 132270       }else{
131390 132271         reg = 0;
131391 132272         for(i=0; i<pIdx->nKeyCol; i++){
131392         -        i16 iIdxCol = pIdx->aiColumn[i];
131393         -        if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){
       132273  +        if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){
131394 132274             reg = ++pParse->nMem;
131395 132275             pParse->nMem += pIdx->nColumn;
131396 132276             if( (onError==OE_Replace)
131397 132277              || (onError==OE_Default && pIdx->onError==OE_Replace) 
131398 132278             ){
131399 132279               bReplace = 1;
131400 132280             }
................................................................................
131935 132815   
131936 132816     /* Populate the argument registers. */
131937 132817     for(i=0; i<pTab->nCol; i++){
131938 132818       if( aXRef[i]>=0 ){
131939 132819         sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
131940 132820       }else{
131941 132821         sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i);
131942         -      sqlite3VdbeChangeP5(v, 1); /* Enable sqlite3_vtab_nochange() */
       132822  +      sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG);/* Enable sqlite3_vtab_nochange() */
131943 132823       }
131944 132824     }
131945 132825     if( HasRowid(pTab) ){
131946 132826       sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg);
131947 132827       if( pRowid ){
131948 132828         sqlite3ExprCode(pParse, pRowid, regArg+1);
131949 132829       }else{
................................................................................
132436 133316     saved_flags = db->flags;
132437 133317     saved_mDbFlags = db->mDbFlags;
132438 133318     saved_nChange = db->nChange;
132439 133319     saved_nTotalChange = db->nTotalChange;
132440 133320     saved_mTrace = db->mTrace;
132441 133321     db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
132442 133322     db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
132443         -  db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_CountRows);
       133323  +  db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder
       133324  +                   | SQLITE_Defensive | SQLITE_CountRows);
132444 133325     db->mTrace = 0;
132445 133326   
132446 133327     zDbMain = db->aDb[iDb].zDbSName;
132447 133328     pMain = db->aDb[iDb].pBt;
132448 133329     isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
132449 133330   
132450 133331     /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
................................................................................
132978 133859   SQLITE_PRIVATE void sqlite3VtabBeginParse(
132979 133860     Parse *pParse,        /* Parsing context */
132980 133861     Token *pName1,        /* Name of new table, or database name */
132981 133862     Token *pName2,        /* Name of new table or NULL */
132982 133863     Token *pModuleName,   /* Name of the module for the virtual table */
132983 133864     int ifNotExists       /* No error if the table already exists */
132984 133865   ){
132985         -  int iDb;              /* The database the table is being created in */
132986 133866     Table *pTable;        /* The new virtual table */
132987 133867     sqlite3 *db;          /* Database connection */
132988 133868   
132989 133869     sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
132990 133870     pTable = pParse->pNewTable;
132991 133871     if( pTable==0 ) return;
132992 133872     assert( 0==pTable->pIndex );
132993 133873   
132994 133874     db = pParse->db;
132995         -  iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
132996         -  assert( iDb>=0 );
132997 133875   
132998 133876     assert( pTable->nModuleArg==0 );
132999 133877     addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
133000 133878     addModuleArgument(db, pTable, 0);
133001 133879     addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
133002 133880     assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
133003 133881          || (pParse->sNameToken.z==pName1->z && pName2->z==0)
................................................................................
133009 133887   #ifndef SQLITE_OMIT_AUTHORIZATION
133010 133888     /* Creating a virtual table invokes the authorization callback twice.
133011 133889     ** The first invocation, to obtain permission to INSERT a row into the
133012 133890     ** sqlite_master table, has already been made by sqlite3StartTable().
133013 133891     ** The second call, to obtain permission to create the table, is made now.
133014 133892     */
133015 133893     if( pTable->azModuleArg ){
       133894  +    int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
       133895  +    assert( iDb>=0 ); /* The database the table is being created in */
133016 133896       sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 
133017 133897               pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName);
133018 133898     }
133019 133899   #endif
133020 133900   }
133021 133901   
133022 133902   /*
................................................................................
133703 134583     void *pArg = 0;
133704 134584     FuncDef *pNew;
133705 134585     int rc = 0;
133706 134586   
133707 134587     /* Check to see the left operand is a column in a virtual table */
133708 134588     if( NEVER(pExpr==0) ) return pDef;
133709 134589     if( pExpr->op!=TK_COLUMN ) return pDef;
133710         -  pTab = pExpr->pTab;
       134590  +  pTab = pExpr->y.pTab;
133711 134591     if( pTab==0 ) return pDef;
133712 134592     if( !IsVirtual(pTab) ) return pDef;
133713 134593     pVtab = sqlite3GetVTable(db, pTab)->pVtab;
133714 134594     assert( pVtab!=0 );
133715 134595     assert( pVtab->pModule!=0 );
133716 134596     pMod = (sqlite3_module *)pVtab->pModule;
133717 134597     if( pMod->xFindFunction==0 ) return pDef;
................................................................................
134323 135203     WhereLoop *pNew;          /* Template WhereLoop */
134324 135204     WhereOrSet *pOrSet;       /* Record best loops here, if not NULL */
134325 135205   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
134326 135206     UnpackedRecord *pRec;     /* Probe for stat4 (if required) */
134327 135207     int nRecValid;            /* Number of valid fields currently in pRec */
134328 135208   #endif
134329 135209     unsigned int bldFlags;    /* SQLITE_BLDF_* flags */
       135210  +  unsigned int iPlanLimit;  /* Search limiter */
134330 135211   };
134331 135212   
134332 135213   /* Allowed values for WhereLoopBuider.bldFlags */
134333 135214   #define SQLITE_BLDF_INDEXED  0x0001   /* An index is used */
134334 135215   #define SQLITE_BLDF_UNIQUE   0x0002   /* All keys of a UNIQUE index used */
       135216  +
       135217  +/* The WhereLoopBuilder.iPlanLimit is used to limit the number of
       135218  +** index+constraint combinations the query planner will consider for a
       135219  +** particular query.  If this parameter is unlimited, then certain
       135220  +** pathological queries can spend excess time in the sqlite3WhereBegin()
       135221  +** routine.  The limit is high enough that is should not impact real-world
       135222  +** queries.
       135223  +**
       135224  +** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit.  The limit is
       135225  +** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM
       135226  +** clause is processed, so that every table in a join is guaranteed to be
       135227  +** able to propose a some index+constraint combinations even if the initial
       135228  +** baseline limit was exhausted by prior tables of the join.
       135229  +*/
       135230  +#ifndef SQLITE_QUERY_PLANNER_LIMIT
       135231  +# define SQLITE_QUERY_PLANNER_LIMIT 20000
       135232  +#endif
       135233  +#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
       135234  +# define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
       135235  +#endif
134335 135236   
134336 135237   /*
134337 135238   ** The WHERE clause processing routine has two halves.  The
134338 135239   ** first part does the start of the WHERE loop and the second
134339 135240   ** half does the tail of the WHERE loop.  An instance of
134340 135241   ** this structure is returned by the first half and passed
134341 135242   ** into the second half to give some continuity.
................................................................................
134890 135791       ExprList *pLhs = 0;         /* New LHS after mods */
134891 135792       int i;                      /* Loop counter */
134892 135793       Select *pSelect;            /* Pointer to the SELECT on the RHS */
134893 135794   
134894 135795       for(i=iEq; i<pLoop->nLTerm; i++){
134895 135796         if( pLoop->aLTerm[i]->pExpr==pX ){
134896 135797           int iField = pLoop->aLTerm[i]->iField - 1;
134897         -        assert( pOrigRhs->a[iField].pExpr!=0 );
       135798  +        if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
134898 135799           pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
134899 135800           pOrigRhs->a[iField].pExpr = 0;
134900 135801           assert( pOrigLhs->a[iField].pExpr!=0 );
134901 135802           pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
134902 135803           pOrigLhs->a[iField].pExpr = 0;
134903 135804         }
134904 135805       }
................................................................................
135582 136483   */
135583 136484   static int whereIndexExprTransNode(Walker *p, Expr *pExpr){
135584 136485     IdxExprTrans *pX = p->u.pIdxTrans;
135585 136486     if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
135586 136487       pExpr->op = TK_COLUMN;
135587 136488       pExpr->iTable = pX->iIdxCur;
135588 136489       pExpr->iColumn = pX->iIdxCol;
135589         -    pExpr->pTab = 0;
       136490  +    pExpr->y.pTab = 0;
135590 136491       return WRC_Prune;
135591 136492     }else{
135592 136493       return WRC_Continue;
135593 136494     }
135594 136495   }
135595 136496   
135596 136497   /*
................................................................................
136982 137883           */
136983 137884           if( sqlite3Isdigit(zNew[0])
136984 137885            || zNew[0]=='-'
136985 137886            || (zNew[0]+1=='0' && iTo==1)
136986 137887           ){
136987 137888             if( pLeft->op!=TK_COLUMN 
136988 137889              || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
136989         -           || IsVirtual(pLeft->pTab)  /* Value might be numeric */
       137890  +           || IsVirtual(pLeft->y.pTab)  /* Value might be numeric */
136990 137891             ){
136991 137892               sqlite3ExprDelete(db, pPrefix);
136992 137893               sqlite3ValueFree(pVal);
136993 137894               return 0;
136994 137895             }
136995 137896           }
136996 137897         }
................................................................................
137083 137984       ** virtual table on their second argument, which is the same as
137084 137985       ** the left-hand side operand in their in-fix form.
137085 137986       **
137086 137987       **       vtab_column MATCH expression
137087 137988       **       MATCH(expression,vtab_column)
137088 137989       */
137089 137990       pCol = pList->a[1].pExpr;
137090         -    if( pCol->op==TK_COLUMN && IsVirtual(pCol->pTab) ){
       137991  +    if( pCol->op==TK_COLUMN && IsVirtual(pCol->y.pTab) ){
137091 137992         for(i=0; i<ArraySize(aOp); i++){
137092 137993           if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
137093 137994             *peOp2 = aOp[i].eOp2;
137094 137995             *ppRight = pList->a[0].pExpr;
137095 137996             *ppLeft = pCol;
137096 137997             return 1;
137097 137998           }
................................................................................
137105 138006       **      OVERLOADED(vtab_column,expression)
137106 138007       **
137107 138008       ** Historically, xFindFunction expected to see lower-case function
137108 138009       ** names.  But for this use case, xFindFunction is expected to deal
137109 138010       ** with function names in an arbitrary case.
137110 138011       */
137111 138012       pCol = pList->a[0].pExpr;
137112         -    if( pCol->op==TK_COLUMN && IsVirtual(pCol->pTab) ){
       138013  +    if( pCol->op==TK_COLUMN && IsVirtual(pCol->y.pTab) ){
137113 138014         sqlite3_vtab *pVtab;
137114 138015         sqlite3_module *pMod;
137115 138016         void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
137116 138017         void *pNotUsed;
137117         -      pVtab = sqlite3GetVTable(db, pCol->pTab)->pVtab;
       138018  +      pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
137118 138019         assert( pVtab!=0 );
137119 138020         assert( pVtab->pModule!=0 );
137120 138021         pMod = (sqlite3_module *)pVtab->pModule;
137121 138022         if( pMod->xFindFunction!=0 ){
137122 138023           i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
137123 138024           if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
137124 138025             *peOp2 = i;
................................................................................
137128 138029           }
137129 138030         }
137130 138031       }
137131 138032     }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
137132 138033       int res = 0;
137133 138034       Expr *pLeft = pExpr->pLeft;
137134 138035       Expr *pRight = pExpr->pRight;
137135         -    if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->pTab) ){
       138036  +    if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->y.pTab) ){
137136 138037         res++;
137137 138038       }
137138         -    if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->pTab) ){
       138039  +    if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->y.pTab) ){
137139 138040         res++;
137140 138041         SWAP(Expr*, pLeft, pRight);
137141 138042       }
137142 138043       *ppLeft = pLeft;
137143 138044       *ppRight = pRight;
137144 138045       if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
137145 138046       if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
................................................................................
138083 138984     ** virtual term of that form.
138084 138985     **
138085 138986     ** Note that the virtual term must be tagged with TERM_VNULL.
138086 138987     */
138087 138988     if( pExpr->op==TK_NOTNULL
138088 138989      && pExpr->pLeft->op==TK_COLUMN
138089 138990      && pExpr->pLeft->iColumn>=0
       138991  +   && !ExprHasProperty(pExpr, EP_FromJoin)
138090 138992      && OptimizationEnabled(db, SQLITE_Stat34)
138091 138993     ){
138092 138994       Expr *pNewExpr;
138093 138995       Expr *pLeft = pExpr->pLeft;
138094 138996       int idxNew;
138095 138997       WhereTerm *pNewTerm;
138096 138998   
................................................................................
138274 139176     Expr *pTerm;
138275 139177     if( pItem->fg.isTabFunc==0 ) return;
138276 139178     pTab = pItem->pTab;
138277 139179     assert( pTab!=0 );
138278 139180     pArgs = pItem->u1.pFuncArg;
138279 139181     if( pArgs==0 ) return;
138280 139182     for(j=k=0; j<pArgs->nExpr; j++){
       139183  +    Expr *pRhs;
138281 139184       while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
138282 139185       if( k>=pTab->nCol ){
138283 139186         sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
138284 139187                         pTab->zName, j);
138285 139188         return;
138286 139189       }
138287 139190       pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
138288 139191       if( pColRef==0 ) return;
138289 139192       pColRef->iTable = pItem->iCursor;
138290 139193       pColRef->iColumn = k++;
138291         -    pColRef->pTab = pTab;
138292         -    pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef,
138293         -                         sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0));
       139194  +    pColRef->y.pTab = pTab;
       139195  +    pRhs = sqlite3PExpr(pParse, TK_UPLUS, 
       139196  +        sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
       139197  +    pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs);
138294 139198       whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
138295 139199     }
138296 139200   }
138297 139201   
138298 139202   /************** End of whereexpr.c *******************************************/
138299 139203   /************** Begin file where.c *******************************************/
138300 139204   /*
................................................................................
139149 140053     if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
139150 140054     if( pTabItem->fg.viaCoroutine ){
139151 140055       sqlite3VdbeChangeP2(v, addrCounter, regBase+n);
139152 140056       testcase( pParse->db->mallocFailed );
139153 140057       translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
139154 140058                             pTabItem->regResult, 1);
139155 140059       sqlite3VdbeGoto(v, addrTop);
139156         -    pTabItem->fg.viaCoroutine = 0;
139157 140060     }else{
139158 140061       sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
139159 140062     }
139160 140063     sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
139161 140064     sqlite3VdbeJumpHere(v, addrTop);
139162 140065     sqlite3ReleaseTempReg(pParse, regRecord);
139163 140066     
................................................................................
139327 140230   
139328 140231   /*
139329 140232   ** The table object reference passed as the second argument to this function
139330 140233   ** must represent a virtual table. This function invokes the xBestIndex()
139331 140234   ** method of the virtual table with the sqlite3_index_info object that
139332 140235   ** comes in as the 3rd argument to this function.
139333 140236   **
139334         -** If an error occurs, pParse is populated with an error message and a
139335         -** non-zero value is returned. Otherwise, 0 is returned and the output
139336         -** part of the sqlite3_index_info structure is left populated.
       140237  +** If an error occurs, pParse is populated with an error message and an
       140238  +** appropriate error code is returned.  A return of SQLITE_CONSTRAINT from
       140239  +** xBestIndex is not considered an error.  SQLITE_CONSTRAINT indicates that
       140240  +** the current configuration of "unusable" flags in sqlite3_index_info can
       140241  +** not result in a valid plan.
139337 140242   **
139338 140243   ** Whether or not an error is returned, it is the responsibility of the
139339 140244   ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
139340 140245   ** that this is required.
139341 140246   */
139342 140247   static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
139343 140248     sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
139344 140249     int rc;
139345 140250   
139346 140251     TRACE_IDX_INPUTS(p);
139347 140252     rc = pVtab->pModule->xBestIndex(pVtab, p);
139348 140253     TRACE_IDX_OUTPUTS(p);
139349 140254   
139350         -  if( rc!=SQLITE_OK ){
       140255  +  if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){
139351 140256       if( rc==SQLITE_NOMEM ){
139352 140257         sqlite3OomFault(pParse->db);
139353 140258       }else if( !pVtab->zErrMsg ){
139354 140259         sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
139355 140260       }else{
139356 140261         sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
139357 140262       }
139358 140263     }
139359 140264     sqlite3_free(pVtab->zErrMsg);
139360 140265     pVtab->zErrMsg = 0;
139361         -
139362         -#if 0
139363         -  /* This error is now caught by the caller.
139364         -  ** Search for "xBestIndex malfunction" below */
139365         -  for(i=0; i<p->nConstraint; i++){
139366         -    if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
139367         -      sqlite3ErrorMsg(pParse, 
139368         -          "table %s: xBestIndex returned an invalid plan", pTab->zName);
139369         -    }
139370         -  }
139371         -#endif
139372         -
139373         -  return pParse->nErr;
       140266  +  return rc;
139374 140267   }
139375 140268   #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
139376 140269   
139377 140270   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
139378 140271   /*
139379 140272   ** Estimate the location of a particular key among all keys in an
139380 140273   ** index.  Store the results in aStat as follows:
................................................................................
140420 141313   **    (4)  The template has the same or lower cost than the current loop
140421 141314   */
140422 141315   static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
140423 141316     WhereLoop **ppPrev, *p;
140424 141317     WhereInfo *pWInfo = pBuilder->pWInfo;
140425 141318     sqlite3 *db = pWInfo->pParse->db;
140426 141319     int rc;
       141320  +
       141321  +  /* Stop the search once we hit the query planner search limit */
       141322  +  if( pBuilder->iPlanLimit==0 ){
       141323  +    WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n"));
       141324  +    if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
       141325  +    return SQLITE_DONE;
       141326  +  }
       141327  +  pBuilder->iPlanLimit--;
140427 141328   
140428 141329     /* If pBuilder->pOrSet is defined, then only keep track of the costs
140429 141330     ** and prereqs.
140430 141331     */
140431 141332     if( pBuilder->pOrSet!=0 ){
140432 141333       if( pTemplate->nLTerm ){
140433 141334   #if WHERETRACE_ENABLED
................................................................................
141431 142332     pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
141432 142333     pIdxInfo->estimatedRows = 25;
141433 142334     pIdxInfo->idxFlags = 0;
141434 142335     pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
141435 142336   
141436 142337     /* Invoke the virtual table xBestIndex() method */
141437 142338     rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
141438         -  if( rc ) return rc;
       142339  +  if( rc ){
       142340  +    if( rc==SQLITE_CONSTRAINT ){
       142341  +      /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
       142342  +      ** that the particular combination of parameters provided is unusable.
       142343  +      ** Make no entries in the loop table.
       142344  +      */
       142345  +      WHERETRACE(0xffff, ("  ^^^^--- non-viable plan rejected!\n"));
       142346  +      return SQLITE_OK;
       142347  +    }
       142348  +    return rc;
       142349  +  }
141439 142350   
141440 142351     mxTerm = -1;
141441 142352     assert( pNew->nLSlot>=nConstraint );
141442 142353     for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
141443 142354     pNew->u.vtab.omitMask = 0;
141444 142355     pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
141445 142356     for(i=0; i<nConstraint; i++, pIdxCons++){
................................................................................
141827 142738     int rc = SQLITE_OK;
141828 142739     WhereLoop *pNew;
141829 142740     u8 priorJointype = 0;
141830 142741   
141831 142742     /* Loop over the tables in the join, from left to right */
141832 142743     pNew = pBuilder->pNew;
141833 142744     whereLoopInit(pNew);
       142745  +  pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
141834 142746     for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
141835 142747       Bitmask mUnusable = 0;
141836 142748       pNew->iTab = iTab;
       142749  +    pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
141837 142750       pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
141838 142751       if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
141839 142752         /* This condition is true when pItem is the FROM clause term on the
141840 142753         ** right-hand-side of a LEFT or CROSS JOIN.  */
141841 142754         mPrereq = mPrior;
141842 142755       }
141843 142756       priorJointype = pItem->fg.jointype;
................................................................................
141855 142768       {
141856 142769         rc = whereLoopAddBtree(pBuilder, mPrereq);
141857 142770       }
141858 142771       if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
141859 142772         rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
141860 142773       }
141861 142774       mPrior |= pNew->maskSelf;
141862         -    if( rc || db->mallocFailed ) break;
       142775  +    if( rc || db->mallocFailed ){
       142776  +      if( rc==SQLITE_DONE ){
       142777  +        /* We hit the query planner search limit set by iPlanLimit */
       142778  +        sqlite3_log(SQLITE_WARNING, "abbreviated query algorithm search");
       142779  +        rc = SQLITE_OK;
       142780  +      }else{
       142781  +        break;
       142782  +      }
       142783  +    }
141863 142784     }
141864 142785   
141865 142786     whereLoopClear(db, pNew);
141866 142787     return rc;
141867 142788   }
141868 142789   
141869 142790   /*
................................................................................
144237 145158         if( i==nSrc ) return WRC_Continue;
144238 145159       }
144239 145160     }
144240 145161   
144241 145162     switch( pExpr->op ){
144242 145163   
144243 145164       case TK_FUNCTION:
144244         -      if( pExpr->pWin==0 ){
       145165  +      if( !ExprHasProperty(pExpr, EP_WinFunc) ){
144245 145166           break;
144246 145167         }else{
144247 145168           Window *pWin;
144248 145169           for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
144249         -          if( pExpr->pWin==pWin ){
       145170  +          if( pExpr->y.pWin==pWin ){
144250 145171               assert( pWin->pOwner==pExpr );
144251 145172               return WRC_Prune;
144252 145173             }
144253 145174           }
144254 145175         }
144255 145176         /* Fall through.  */
144256 145177   
................................................................................
144359 145280   ** any SQL window functions, this function is a no-op. Otherwise, it 
144360 145281   ** rewrites the SELECT statement so that window function xStep functions
144361 145282   ** are invoked in the correct order as described under "SELECT REWRITING"
144362 145283   ** at the top of this file.
144363 145284   */
144364 145285   SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
144365 145286     int rc = SQLITE_OK;
144366         -  if( p->pWin ){
       145287  +  if( p->pWin && p->pPrior==0 ){
144367 145288       Vdbe *v = sqlite3GetVdbe(pParse);
144368 145289       sqlite3 *db = pParse->db;
144369 145290       Select *pSub = 0;             /* The subquery */
144370 145291       SrcList *pSrc = p->pSrc;
144371 145292       Expr *pWhere = p->pWhere;
144372 145293       ExprList *pGroupBy = p->pGroupBy;
144373 145294       Expr *pHaving = p->pHaving;
................................................................................
144572 145493   }
144573 145494   
144574 145495   /*
144575 145496   ** Attach window object pWin to expression p.
144576 145497   */
144577 145498   SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
144578 145499     if( p ){
       145500  +    assert( p->op==TK_FUNCTION );
144579 145501       /* This routine is only called for the parser.  If pWin was not
144580 145502       ** allocated due to an OOM, then the parser would fail before ever
144581 145503       ** invoking this routine */
144582 145504       if( ALWAYS(pWin) ){
144583         -      p->pWin = pWin;
       145505  +      p->y.pWin = pWin;
       145506  +      ExprSetProperty(p, EP_WinFunc);
144584 145507         pWin->pOwner = p;
144585 145508         if( p->flags & EP_Distinct ){
144586 145509           sqlite3ErrorMsg(pParse,
144587 145510              "DISTINCT is not supported for window functions");
144588 145511         }
144589 145512       }
144590 145513     }else{
................................................................................
145739 146662   /*
145740 146663   ** Allocate and return a duplicate of the Window object indicated by the
145741 146664   ** third argument. Set the Window.pOwner field of the new object to
145742 146665   ** pOwner.
145743 146666   */
145744 146667   SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
145745 146668     Window *pNew = 0;
145746         -  if( p ){
       146669  +  if( ALWAYS(p) ){
145747 146670       pNew = sqlite3DbMallocZero(db, sizeof(Window));
145748 146671       if( pNew ){
145749 146672         pNew->zName = sqlite3DbStrDup(db, p->zName);
145750 146673         pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
145751 146674         pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
145752 146675         pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
145753 146676         pNew->eType = p->eType;
................................................................................
145891 146814   ** of this template is copied straight through into the generate parser
145892 146815   ** source file.
145893 146816   **
145894 146817   ** The following is the concatenation of all %include directives from the
145895 146818   ** input grammar file:
145896 146819   */
145897 146820   /* #include <stdio.h> */
       146821  +/* #include <assert.h> */
145898 146822   /************ Begin %include sections from the grammar ************************/
145899 146823   
145900 146824   /* #include "sqliteInt.h" */
145901 146825   
145902 146826   /*
145903 146827   ** Disable all error recovery processing in the parser push-down
145904 146828   ** automaton.
................................................................................
145992 146916         p->op = (u8)op;
145993 146917         p->affinity = 0;
145994 146918         p->flags = EP_Leaf;
145995 146919         p->iAgg = -1;
145996 146920         p->pLeft = p->pRight = 0;
145997 146921         p->x.pList = 0;
145998 146922         p->pAggInfo = 0;
145999         -      p->pTab = 0;
       146923  +      p->y.pTab = 0;
146000 146924         p->op2 = 0;
146001 146925         p->iTable = 0;
146002 146926         p->iColumn = 0;
146003         -#ifndef SQLITE_OMIT_WINDOWFUNC
146004         -      p->pWin = 0;
146005         -#endif
146006 146927         p->u.zToken = (char*)&p[1];
146007 146928         memcpy(p->u.zToken, t.z, t.n);
146008 146929         p->u.zToken[t.n] = 0;
146009 146930         if( sqlite3Isquote(p->u.zToken[0]) ){
146010 146931           if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
146011 146932           sqlite3Dequote(p->u.zToken);
146012 146933         }
................................................................................
150190 151111                yyTracePrompt,yyTokenName[yymajor]);
150191 151112           }
150192 151113   #endif
150193 151114           yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
150194 151115           yymajor = YYNOCODE;
150195 151116         }else{
150196 151117           while( yypParser->yytos >= yypParser->yystack
150197         -            && yymx != YYERRORSYMBOL
150198 151118               && (yyact = yy_find_reduce_action(
150199 151119                           yypParser->yytos->stateno,
150200         -                        YYERRORSYMBOL)) >= YY_MIN_REDUCE
       151120  +                        YYERRORSYMBOL)) > YY_MAX_SHIFTREDUCE
150201 151121           ){
150202 151122             yy_pop_parser_stack(yypParser);
150203 151123           }
150204 151124           if( yypParser->yytos < yypParser->yystack || yymajor==0 ){
150205 151125             yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
150206 151126             yy_parse_failed(yypParser);
150207 151127   #ifndef YYNOERRORRECOVERY
................................................................................
151160 152080       }
151161 152081     }
151162 152082     while( IdChar(z[i]) ){ i++; }
151163 152083     *tokenType = TK_ID;
151164 152084     return i;
151165 152085   }
151166 152086   
       152087  +#ifdef SQLITE_ENABLE_NORMALIZE
       152088  +/*
       152089  +** Return the length (in bytes) of the token that begins at z[0].
       152090  +** Store the token type in *tokenType before returning.  If flags has
       152091  +** SQLITE_TOKEN_NORMALIZE flag enabled, use the identifier token type
       152092  +** for keywords.  Add SQLITE_TOKEN_QUOTED to flags if the token was
       152093  +** actually a quoted identifier.  Add SQLITE_TOKEN_KEYWORD to flags
       152094  +** if the token was recognized as a keyword; this is useful when the
       152095  +** SQLITE_TOKEN_NORMALIZE flag is used, because it enables the caller
       152096  +** to differentiate between a keyword being treated as an identifier
       152097  +** (for normalization purposes) and an actual identifier.
       152098  +*/
       152099  +SQLITE_PRIVATE int sqlite3GetTokenNormalized(
       152100  +  const unsigned char *z,
       152101  +  int *tokenType,
       152102  +  int *flags
       152103  +){
       152104  +  int n;
       152105  +  unsigned char iClass = aiClass[*z];
       152106  +  if( iClass==CC_KYWD ){
       152107  +    int i;
       152108  +    for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
       152109  +    if( IdChar(z[i]) ){
       152110  +      /* This token started out using characters that can appear in keywords,
       152111  +      ** but z[i] is a character not allowed within keywords, so this must
       152112  +      ** be an identifier instead */
       152113  +      i++;
       152114  +      while( IdChar(z[i]) ){ i++; }
       152115  +      *tokenType = TK_ID;
       152116  +      return i;
       152117  +    }
       152118  +    *tokenType = TK_ID;
       152119  +    n = keywordCode((char*)z, i, tokenType);
       152120  +    /* If the token is no longer considered to be an identifier, then it is a
       152121  +    ** keyword of some kind.  Make the token back into an identifier and then
       152122  +    ** set the SQLITE_TOKEN_KEYWORD flag.  Several non-identifier tokens are
       152123  +    ** used verbatim, including IN, IS, NOT, and NULL. */
       152124  +    switch( *tokenType ){
       152125  +      case TK_ID: {
       152126  +        /* do nothing, handled by caller */
       152127  +        break;
       152128  +      }
       152129  +      case TK_IN:
       152130  +      case TK_IS:
       152131  +      case TK_NOT:
       152132  +      case TK_NULL: {
       152133  +        *flags |= SQLITE_TOKEN_KEYWORD;
       152134  +        break;
       152135  +      }
       152136  +      default: {
       152137  +        *tokenType = TK_ID;
       152138  +        *flags |= SQLITE_TOKEN_KEYWORD;
       152139  +        break;
       152140  +      }
       152141  +    }
       152142  +  }else{
       152143  +    n = sqlite3GetToken(z, tokenType);
       152144  +    /* If the token is considered to be an identifier and the character class
       152145  +    ** of the first character is a quote, set the SQLITE_TOKEN_QUOTED flag. */
       152146  +    if( *tokenType==TK_ID && (iClass==CC_QUOTE || iClass==CC_QUOTE2) ){
       152147  +      *flags |= SQLITE_TOKEN_QUOTED;
       152148  +    }
       152149  +  }
       152150  +  return n;
       152151  +}
       152152  +#endif /* SQLITE_ENABLE_NORMALIZE */
       152153  +
151167 152154   /*
151168 152155   ** Run the parser on the given SQL string.  The parser structure is
151169 152156   ** passed in.  An SQLITE_ status code is returned.  If an error occurs
151170 152157   ** then an and attempt is made to write an error message into 
151171 152158   ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
151172 152159   ** error message.
151173 152160   */
................................................................................
152557 153544           { SQLITE_DBCONFIG_ENABLE_TRIGGER,        SQLITE_EnableTrigger  },
152558 153545           { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer  },
152559 153546           { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension  },
152560 153547           { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE,      SQLITE_NoCkptOnClose  },
152561 153548           { SQLITE_DBCONFIG_ENABLE_QPSG,           SQLITE_EnableQPSG     },
152562 153549           { SQLITE_DBCONFIG_TRIGGER_EQP,           SQLITE_TriggerEQP     },
152563 153550           { SQLITE_DBCONFIG_RESET_DATABASE,        SQLITE_ResetDatabase  },
       153551  +        { SQLITE_DBCONFIG_DEFENSIVE,             SQLITE_Defensive      },
152564 153552         };
152565 153553         unsigned int i;
152566 153554         rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
152567 153555         for(i=0; i<ArraySize(aFlagOp); i++){
152568 153556           if( aFlagOp[i].op==op ){
152569 153557             int onoff = va_arg(ap, int);
152570 153558             int *pRes = va_arg(ap, int*);
................................................................................
154743 155731       }
154744 155732     }
154745 155733     sqlite3_mutex_enter(db->mutex);
154746 155734     db->errMask = 0xff;
154747 155735     db->nDb = 2;
154748 155736     db->magic = SQLITE_MAGIC_BUSY;
154749 155737     db->aDb = db->aDbStatic;
       155738  +  db->lookaside.bDisable = 1;
154750 155739   
154751 155740     assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
154752 155741     memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
154753 155742     db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
154754 155743     db->autoCommit = 1;
154755 155744     db->nextAutovac = -1;
154756 155745     db->szMmap = sqlite3GlobalConfig.szMmap;
................................................................................
154782 155771                    | SQLITE_CellSizeCk
154783 155772   #endif
154784 155773   #if defined(SQLITE_ENABLE_FTS3_TOKENIZER)
154785 155774                    | SQLITE_Fts3Tokenizer
154786 155775   #endif
154787 155776   #if defined(SQLITE_ENABLE_QPSG)
154788 155777                    | SQLITE_EnableQPSG
       155778  +#endif
       155779  +#if defined(SQLITE_DEFAULT_DEFENSIVE)
       155780  +                 | SQLITE_Defensive
154789 155781   #endif
154790 155782         ;
154791 155783     sqlite3HashInit(&db->aCollSeq);
154792 155784   #ifndef SQLITE_OMIT_VIRTUALTABLE
154793 155785     sqlite3HashInit(&db->aModule);
154794 155786   #endif
154795 155787   
................................................................................
155670 156662         sqlite3 *db = va_arg(ap, sqlite3*);
155671 156663         db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
155672 156664         break;
155673 156665       }
155674 156666   
155675 156667       /*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
155676 156668       **
155677         -    ** If parameter onoff is non-zero, configure the wrappers so that all
155678         -    ** subsequent calls to localtime() and variants fail. If onoff is zero,
155679         -    ** undo this setting.
       156669  +    ** If parameter onoff is non-zero, subsequent calls to localtime()
       156670  +    ** and its variants fail. If onoff is zero, undo this setting.
155680 156671       */
155681 156672       case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
155682 156673         sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
155683 156674         break;
155684 156675       }
       156676  +
       156677  +    /*   sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCS, int onoff);
       156678  +    **
       156679  +    ** If parameter onoff is non-zero, internal-use-only SQL functions
       156680  +    ** are visible to ordinary SQL.  This is useful for testing but is
       156681  +    ** unsafe because invalid parameters to those internal-use-only functions
       156682  +    ** can result in crashes or segfaults.
       156683  +    */
       156684  +    case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: {
       156685  +      sqlite3GlobalConfig.bInternalFunctions = va_arg(ap, int);
       156686  +      break;
       156687  +    }
155685 156688   
155686 156689       /*   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
155687 156690       **
155688 156691       ** Set or clear a flag that indicates that the database file is always well-
155689 156692       ** formed and never corrupt.  This flag is clear by default, indicating that
155690 156693       ** database files might have arbitrary corruption.  Setting the flag during
155691 156694       ** testing causes certain assert() statements in the code to be activated
................................................................................
159121 160124     sqlite3_int64 *piFirst,         /* OUT: Selected child node */
159122 160125     sqlite3_int64 *piLast           /* OUT: Selected child node */
159123 160126   ){
159124 160127     int rc = SQLITE_OK;             /* Return code */
159125 160128     const char *zCsr = zNode;       /* Cursor to iterate through node */
159126 160129     const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
159127 160130     char *zBuffer = 0;              /* Buffer to load terms into */
159128         -  int nAlloc = 0;                 /* Size of allocated buffer */
       160131  +  i64 nAlloc = 0;                 /* Size of allocated buffer */
159129 160132     int isFirstTerm = 1;            /* True when processing first term on page */
159130 160133     sqlite3_int64 iChild;           /* Block id of child node to descend to */
159131 160134   
159132 160135     /* Skip over the 'height' varint that occurs at the start of every 
159133 160136     ** interior node. Then load the blockid of the left-child of the b-tree
159134 160137     ** node into variable iChild.  
159135 160138     **
................................................................................
159159 160162       if( !isFirstTerm ){
159160 160163         zCsr += fts3GetVarint32(zCsr, &nPrefix);
159161 160164       }
159162 160165       isFirstTerm = 0;
159163 160166       zCsr += fts3GetVarint32(zCsr, &nSuffix);
159164 160167       
159165 160168       assert( nPrefix>=0 && nSuffix>=0 );
159166         -    if( &zCsr[nSuffix]>zEnd ){
       160169  +    if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr ){
159167 160170         rc = FTS_CORRUPT_VTAB;
159168 160171         goto finish_scan;
159169 160172       }
159170         -    if( nPrefix+nSuffix>nAlloc ){
       160173  +    if( (i64)nPrefix+nSuffix>nAlloc ){
159171 160174         char *zNew;
159172         -      nAlloc = (nPrefix+nSuffix) * 2;
159173         -      zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
       160175  +      nAlloc = ((i64)nPrefix+nSuffix) * 2;
       160176  +      zNew = (char *)sqlite3_realloc64(zBuffer, nAlloc);
159174 160177         if( !zNew ){
159175 160178           rc = SQLITE_NOMEM;
159176 160179           goto finish_scan;
159177 160180         }
159178 160181         zBuffer = zNew;
159179 160182       }
159180 160183       assert( zBuffer );
................................................................................
161145 162148     UNUSED_PARAMETER(iSavepoint);
161146 162149     assert( p->inTransaction );
161147 162150     assert( p->mxSavepoint >= iSavepoint );
161148 162151     TESTONLY( p->mxSavepoint = iSavepoint );
161149 162152     sqlite3Fts3PendingTermsClear(p);
161150 162153     return SQLITE_OK;
161151 162154   }
       162155  +
       162156  +/*
       162157  +** Return true if zName is the extension on one of the shadow tables used
       162158  +** by this module.
       162159  +*/
       162160  +static int fts3ShadowName(const char *zName){
       162161  +  static const char *azName[] = {
       162162  +    "content", "docsize", "segdir", "segments", "stat", 
       162163  +  };
       162164  +  unsigned int i;
       162165  +  for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
       162166  +    if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
       162167  +  }
       162168  +  return 0;
       162169  +}
161152 162170   
161153 162171   static const sqlite3_module fts3Module = {
161154         -  /* iVersion      */ 2,
       162172  +  /* iVersion      */ 3,
161155 162173     /* xCreate       */ fts3CreateMethod,
161156 162174     /* xConnect      */ fts3ConnectMethod,
161157 162175     /* xBestIndex    */ fts3BestIndexMethod,
161158 162176     /* xDisconnect   */ fts3DisconnectMethod,
161159 162177     /* xDestroy      */ fts3DestroyMethod,
161160 162178     /* xOpen         */ fts3OpenMethod,
161161 162179     /* xClose        */ fts3CloseMethod,
................................................................................
161170 162188     /* xCommit       */ fts3CommitMethod,
161171 162189     /* xRollback     */ fts3RollbackMethod,
161172 162190     /* xFindFunction */ fts3FindFunctionMethod,
161173 162191     /* xRename */       fts3RenameMethod,
161174 162192     /* xSavepoint    */ fts3SavepointMethod,
161175 162193     /* xRelease      */ fts3ReleaseMethod,
161176 162194     /* xRollbackTo   */ fts3RollbackToMethod,
       162195  +  /* xShadowName   */ fts3ShadowName,
161177 162196   };
161178 162197   
161179 162198   /*
161180 162199   ** This function is registered as the module destructor (called when an
161181 162200   ** FTS3 enabled database connection is closed). It frees the memory
161182 162201   ** allocated for the tokenizer hash table.
161183 162202   */
................................................................................
161450 162469       }
161451 162470       assert( pToken->pSegcsr==0 );
161452 162471     }
161453 162472   
161454 162473     return rc;
161455 162474   }
161456 162475   
       162476  +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
161457 162477   /*
161458 162478   ** This function is called on each phrase after the position lists for
161459 162479   ** any deferred tokens have been loaded into memory. It updates the phrases
161460 162480   ** current position list to include only those positions that are really
161461 162481   ** instances of the phrase (after considering deferred tokens). If this
161462 162482   ** means that the phrase does not appear in the current row, doclist.pList
161463 162483   ** and doclist.nList are both zeroed.
................................................................................
161553 162573         }
161554 162574         sqlite3_free(aPoslist);
161555 162575       }
161556 162576     }
161557 162577   
161558 162578     return SQLITE_OK;
161559 162579   }
       162580  +#endif /* SQLITE_DISABLE_FTS4_DEFERRED */
161560 162581   
161561 162582   /*
161562 162583   ** Maximum number of tokens a phrase may have to be considered for the
161563 162584   ** incremental doclists strategy.
161564 162585   */
161565 162586   #define MAX_INCR_PHRASE_TOKENS 4
161566 162587   
................................................................................
163801 164822        0,                           /* xSync         */
163802 164823        0,                           /* xCommit       */
163803 164824        0,                           /* xRollback     */
163804 164825        0,                           /* xFindFunction */
163805 164826        0,                           /* xRename       */
163806 164827        0,                           /* xSavepoint    */
163807 164828        0,                           /* xRelease      */
163808         -     0                            /* xRollbackTo   */
       164829  +     0,                           /* xRollbackTo   */
       164830  +     0                            /* xShadowName   */
163809 164831     };
163810 164832     int rc;                         /* Return code */
163811 164833   
163812 164834     rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
163813 164835     return rc;
163814 164836   }
163815 164837   
................................................................................
167360 168382        0,                           /* xSync         */
167361 168383        0,                           /* xCommit       */
167362 168384        0,                           /* xRollback     */
167363 168385        0,                           /* xFindFunction */
167364 168386        0,                           /* xRename       */
167365 168387        0,                           /* xSavepoint    */
167366 168388        0,                           /* xRelease      */
167367         -     0                            /* xRollbackTo   */
       168389  +     0,                           /* xRollbackTo   */
       168390  +     0                            /* xShadowName   */
167368 168391     };
167369 168392     int rc;                         /* Return code */
167370 168393   
167371 168394     rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
167372 168395     return rc;
167373 168396   }
167374 168397   
................................................................................
168748 169771     rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
168749 169772     if( rc!=SQLITE_OK ) return rc;
168750 169773     
168751 169774     /* Because of the FTS3_NODE_PADDING bytes of padding, the following is 
168752 169775     ** safe (no risk of overread) even if the node data is corrupted. */
168753 169776     pNext += fts3GetVarint32(pNext, &nPrefix);
168754 169777     pNext += fts3GetVarint32(pNext, &nSuffix);
168755         -  if( nPrefix<0 || nSuffix<=0 
168756         -   || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] 
       169778  +  if( nSuffix<=0 
       169779  +   || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
       169780  +   || nPrefix>pReader->nTermAlloc
168757 169781     ){
168758 169782       return FTS_CORRUPT_VTAB;
168759 169783     }
168760 169784   
168761         -  if( nPrefix+nSuffix>pReader->nTermAlloc ){
168762         -    int nNew = (nPrefix+nSuffix)*2;
168763         -    char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
       169785  +  /* Both nPrefix and nSuffix were read by fts3GetVarint32() and so are
       169786  +  ** between 0 and 0x7FFFFFFF. But the sum of the two may cause integer
       169787  +  ** overflow - hence the (i64) casts.  */
       169788  +  if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){
       169789  +    i64 nNew = ((i64)nPrefix+nSuffix)*2;
       169790  +    char *zNew = sqlite3_realloc64(pReader->zTerm, nNew);
168764 169791       if( !zNew ){
168765 169792         return SQLITE_NOMEM;
168766 169793       }
168767 169794       pReader->zTerm = zNew;
168768 169795       pReader->nTermAlloc = nNew;
168769 169796     }
168770 169797   
................................................................................
168778 169805     pReader->aDoclist = pNext;
168779 169806     pReader->pOffsetList = 0;
168780 169807   
168781 169808     /* Check that the doclist does not appear to extend past the end of the
168782 169809     ** b-tree node. And that the final byte of the doclist is 0x00. If either 
168783 169810     ** of these statements is untrue, then the data structure is corrupt.
168784 169811     */
168785         -  if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] 
       169812  +  if( (&pReader->aNode[pReader->nNode] - pReader->aDoclist)<pReader->nDoclist
168786 169813      || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
168787 169814     ){
168788 169815       return FTS_CORRUPT_VTAB;
168789 169816     }
168790 169817     return SQLITE_OK;
168791 169818   }
168792 169819   
................................................................................
171104 172131       p->aNode = 0;
171105 172132     }else{
171106 172133       if( bFirst==0 ){
171107 172134         p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
171108 172135       }
171109 172136       p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
171110 172137   
       172138  +    if( nPrefix>p->iOff || nSuffix>p->nNode-p->iOff ){
       172139  +      return SQLITE_CORRUPT_VTAB;
       172140  +    }
171111 172141       blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
171112 172142       if( rc==SQLITE_OK ){
171113 172143         memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
171114 172144         p->term.n = nPrefix+nSuffix;
171115 172145         p->iOff += nSuffix;
171116 172146         if( p->iChild==0 ){
171117 172147           p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
       172148  +        if( (p->nNode-p->iOff)<p->nDoclist ){
       172149  +          return SQLITE_CORRUPT_VTAB;
       172150  +        }
171118 172151           p->aDoclist = &p->aNode[p->iOff];
171119 172152           p->iOff += p->nDoclist;
171120 172153         }
171121 172154       }
171122 172155     }
171123 172156   
171124 172157     assert( p->iOff<=p->nNode );
171125         -
171126 172158     return rc;
171127 172159   }
171128 172160   
171129 172161   /*
171130 172162   ** Release all dynamic resources held by node-reader object *p.
171131 172163   */
171132 172164   static void nodeReaderRelease(NodeReader *p){
................................................................................
177528 178560   #define JEACH_VALUE   1
177529 178561   #define JEACH_TYPE    2
177530 178562   #define JEACH_ATOM    3
177531 178563   #define JEACH_ID      4
177532 178564   #define JEACH_PARENT  5
177533 178565   #define JEACH_FULLKEY 6
177534 178566   #define JEACH_PATH    7
       178567  +/* The xBestIndex method assumes that the JSON and ROOT columns are
       178568  +** the last two columns in the table.  Should this ever changes, be
       178569  +** sure to update the xBestIndex method. */
177535 178570   #define JEACH_JSON    8
177536 178571   #define JEACH_ROOT    9
177537 178572   
177538 178573     UNUSED_PARAM(pzErr);
177539 178574     UNUSED_PARAM(argv);
177540 178575     UNUSED_PARAM(argc);
177541 178576     UNUSED_PARAM(pAux);
................................................................................
177785 178820   ** 1 if the constraint is found, 3 if the constraint and zRoot are found,
177786 178821   ** and 0 otherwise.
177787 178822   */
177788 178823   static int jsonEachBestIndex(
177789 178824     sqlite3_vtab *tab,
177790 178825     sqlite3_index_info *pIdxInfo
177791 178826   ){
177792         -  int i;
177793         -  int jsonIdx = -1;
177794         -  int rootIdx = -1;
       178827  +  int i;                     /* Loop counter or computed array index */
       178828  +  int aIdx[2];               /* Index of constraints for JSON and ROOT */
       178829  +  int unusableMask = 0;      /* Mask of unusable JSON and ROOT constraints */
       178830  +  int idxMask = 0;           /* Mask of usable == constraints JSON and ROOT */
177795 178831     const struct sqlite3_index_constraint *pConstraint;
177796 178832   
       178833  +  /* This implementation assumes that JSON and ROOT are the last two
       178834  +  ** columns in the table */
       178835  +  assert( JEACH_ROOT == JEACH_JSON+1 );
177797 178836     UNUSED_PARAM(tab);
       178837  +  aIdx[0] = aIdx[1] = -1;
177798 178838     pConstraint = pIdxInfo->aConstraint;
177799 178839     for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
177800         -    if( pConstraint->usable==0 ) continue;
177801         -    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
177802         -    switch( pConstraint->iColumn ){
177803         -      case JEACH_JSON:   jsonIdx = i;    break;
177804         -      case JEACH_ROOT:   rootIdx = i;    break;
177805         -      default:           /* no-op */     break;
       178840  +    int iCol;
       178841  +    int iMask;
       178842  +    if( pConstraint->iColumn < JEACH_JSON ) continue;
       178843  +    iCol = pConstraint->iColumn - JEACH_JSON;
       178844  +    assert( iCol==0 || iCol==1 );
       178845  +    iMask = 1 << iCol;
       178846  +    if( pConstraint->usable==0 ){
       178847  +      unusableMask |= iMask;
       178848  +    }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
       178849  +      aIdx[iCol] = i;
       178850  +      idxMask |= iMask;
177806 178851       }
177807 178852     }
177808         -  if( jsonIdx<0 ){
       178853  +  if( (unusableMask & ~idxMask)!=0 ){
       178854  +    /* If there are any unusable constraints on JSON or ROOT, then reject
       178855  +    ** this entire plan */
       178856  +    return SQLITE_CONSTRAINT;
       178857  +  }
       178858  +  if( aIdx[0]<0 ){
       178859  +    /* No JSON input.  Leave estimatedCost at the huge value that it was
       178860  +    ** initialized to to discourage the query planner from selecting this
       178861  +    ** plan. */
177809 178862       pIdxInfo->idxNum = 0;
177810         -    pIdxInfo->estimatedCost = 1e99;
177811 178863     }else{
177812 178864       pIdxInfo->estimatedCost = 1.0;
177813         -    pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
177814         -    pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
177815         -    if( rootIdx<0 ){
177816         -      pIdxInfo->idxNum = 1;
       178865  +    i = aIdx[0];
       178866  +    pIdxInfo->aConstraintUsage[i].argvIndex = 1;
       178867  +    pIdxInfo->aConstraintUsage[i].omit = 1;
       178868  +    if( aIdx[1]<0 ){
       178869  +      pIdxInfo->idxNum = 1;  /* Only JSON supplied.  Plan 1 */
177817 178870       }else{
177818         -      pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2;
177819         -      pIdxInfo->aConstraintUsage[rootIdx].omit = 1;
177820         -      pIdxInfo->idxNum = 3;
       178871  +      i = aIdx[1];
       178872  +      pIdxInfo->aConstraintUsage[i].argvIndex = 2;
       178873  +      pIdxInfo->aConstraintUsage[i].omit = 1;
       178874  +      pIdxInfo->idxNum = 3;  /* Both JSON and ROOT are supplied.  Plan 3 */
177821 178875       }
177822 178876     }
177823 178877     return SQLITE_OK;
177824 178878   }
177825 178879   
177826 178880   /* Start a search on a new JSON string */
177827 178881   static int jsonEachFilter(
................................................................................
177922 178976     0,                         /* xSync */
177923 178977     0,                         /* xCommit */
177924 178978     0,                         /* xRollback */
177925 178979     0,                         /* xFindMethod */
177926 178980     0,                         /* xRename */
177927 178981     0,                         /* xSavepoint */
177928 178982     0,                         /* xRelease */
177929         -  0                          /* xRollbackTo */
       178983  +  0,                         /* xRollbackTo */
       178984  +  0                          /* xShadowName */
177930 178985   };
177931 178986   
177932 178987   /* The methods of the json_tree virtual table. */
177933 178988   static sqlite3_module jsonTreeModule = {
177934 178989     0,                         /* iVersion */
177935 178990     0,                         /* xCreate */
177936 178991     jsonEachConnect,           /* xConnect */
................................................................................
177949 179004     0,                         /* xSync */
177950 179005     0,                         /* xCommit */
177951 179006     0,                         /* xRollback */
177952 179007     0,                         /* xFindMethod */
177953 179008     0,                         /* xRename */
177954 179009     0,                         /* xSavepoint */
177955 179010     0,                         /* xRelease */
177956         -  0                          /* xRollbackTo */
       179011  +  0,                         /* xRollbackTo */
       179012  +  0                          /* xShadowName */
177957 179013   };
177958 179014   #endif /* SQLITE_OMIT_VIRTUALTABLE */
177959 179015   
177960 179016   /****************************************************************************
177961 179017   ** The following routines are the only publically visible identifiers in this
177962 179018   ** file.  Call the following routines in order to register the various SQL
177963 179019   ** functions and the virtual table implemented by this file.
................................................................................
181379 182435       }
181380 182436       sqlite3_free(zSql);
181381 182437     }
181382 182438   
181383 182439     return rc;
181384 182440   }
181385 182441   
       182442  +
       182443  +/*
       182444  +** Return true if zName is the extension on one of the shadow tables used
       182445  +** by this module.
       182446  +*/
       182447  +static int rtreeShadowName(const char *zName){
       182448  +  static const char *azName[] = {
       182449  +    "node", "parent", "rowid"
       182450  +  };
       182451  +  unsigned int i;
       182452  +  for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
       182453  +    if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
       182454  +  }
       182455  +  return 0;
       182456  +}
       182457  +
181386 182458   static sqlite3_module rtreeModule = {
181387         -  2,                          /* iVersion */
       182459  +  3,                          /* iVersion */
181388 182460     rtreeCreate,                /* xCreate - create a table */
181389 182461     rtreeConnect,               /* xConnect - connect to an existing table */
181390 182462     rtreeBestIndex,             /* xBestIndex - Determine search strategy */
181391 182463     rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
181392 182464     rtreeDestroy,               /* xDestroy - Drop a table */
181393 182465     rtreeOpen,                  /* xOpen - open a cursor */
181394 182466     rtreeClose,                 /* xClose - close a cursor */
................................................................................
181403 182475     rtreeEndTransaction,        /* xCommit - commit transaction */
181404 182476     rtreeEndTransaction,        /* xRollback - rollback transaction */
181405 182477     0,                          /* xFindFunction - function overloading */
181406 182478     rtreeRename,                /* xRename - rename the table */
181407 182479     rtreeSavepoint,             /* xSavepoint */
181408 182480     0,                          /* xRelease */
181409 182481     0,                          /* xRollbackTo */
       182482  +  rtreeShadowName             /* xShadowName */
181410 182483   };
181411 182484   
181412 182485   static int rtreeSqlInit(
181413 182486     Rtree *pRtree, 
181414 182487     sqlite3 *db, 
181415 182488     const char *zDb, 
181416 182489     const char *zPrefix, 
................................................................................
182393 183466   ** each segment is "outside" and the area to the left is "inside".
182394 183467   **
182395 183468   ** The on-disk representation consists of a 4-byte header followed by
182396 183469   ** the values.  The 4-byte header is:
182397 183470   **
182398 183471   **      encoding    (1 byte)   0=big-endian, 1=little-endian
182399 183472   **      nvertex     (3 bytes)  Number of vertexes as a big-endian integer
       183473  +**
       183474  +** Enough space is allocated for 4 coordinates, to work around over-zealous
       183475  +** warnings coming from some compiler (notably, clang). In reality, the size
       183476  +** of each GeoPoly memory allocate is adjusted as necessary so that the
       183477  +** GeoPoly.a[] array at the end is the appropriate size.
182400 183478   */
182401 183479   typedef struct GeoPoly GeoPoly;
182402 183480   struct GeoPoly {
182403 183481     int nVertex;          /* Number of vertexes */
182404 183482     unsigned char hdr[4]; /* Header for on-disk representation */
182405         -  GeoCoord a[2];    /* 2*nVertex values. X (longitude) first, then Y */
       183483  +  GeoCoord a[8];        /* 2*nVertex values. X (longitude) first, then Y */
182406 183484   };
       183485  +
       183486  +/* The size of a memory allocation needed for a GeoPoly object sufficient
       183487  +** to hold N coordinate pairs.
       183488  +*/
       183489  +#define GEOPOLY_SZ(N)  (sizeof(GeoPoly) + sizeof(GeoCoord)*2*((N)-4))
182407 183490   
182408 183491   /*
182409 183492   ** State of a parse of a GeoJSON input.
182410 183493   */
182411 183494   typedef struct GeoParse GeoParse;
182412 183495   struct GeoParse {
182413 183496     const unsigned char *z;   /* Unparsed input */
................................................................................
182425 183508     t = a[1];
182426 183509     a[1] = a[2];
182427 183510     a[2] = t;
182428 183511   }
182429 183512   
182430 183513   /* Skip whitespace.  Return the next non-whitespace character. */
182431 183514   static char geopolySkipSpace(GeoParse *p){
182432         -  while( p->z[0] && safe_isspace(p->z[0]) ) p->z++;
       183515  +  while( safe_isspace(p->z[0]) ) p->z++;
182433 183516     return p->z[0];
182434 183517   }
182435 183518   
182436 183519   /* Parse out a number.  Write the value into *pVal if pVal!=0.
182437 183520   ** return non-zero on success and zero if the next token is not a number.
182438 183521   */
182439 183522   static int geopolyParseNumber(GeoParse *p, GeoCoord *pVal){
................................................................................
182445 183528     if( c=='-' ){
182446 183529       j = 1;
182447 183530       c = z[j];
182448 183531     }
182449 183532     if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0;
182450 183533     for(;; j++){
182451 183534       c = z[j];
182452         -    if( c>='0' && c<='9' ) continue;
       183535  +    if( safe_isdigit(c) ) continue;
182453 183536       if( c=='.' ){
182454 183537         if( z[j-1]=='-' ) return 0;
182455 183538         if( seenDP ) return 0;
182456 183539         seenDP = 1;
182457 183540         continue;
182458 183541       }
182459 183542       if( c=='e' || c=='E' ){
................................................................................
182467 183550         }
182468 183551         if( c<'0' || c>'9' ) return 0;
182469 183552         continue;
182470 183553       }
182471 183554       break;
182472 183555     }
182473 183556     if( z[j-1]<'0' ) return 0;
182474         -  if( pVal ) *pVal = (GeoCoord)atof((const char*)p->z);
       183557  +  if( pVal ){
       183558  +#ifdef SQLITE_AMALGAMATION
       183559  +     /* The sqlite3AtoF() routine is much much faster than atof(), if it
       183560  +     ** is available */
       183561  +     double r;
       183562  +     (void)sqlite3AtoF((const char*)p->z, &r, j, SQLITE_UTF8);
       183563  +     *pVal = r;
       183564  +#else
       183565  +     *pVal = (GeoCoord)atof((const char*)p->z);
       183566  +#endif
       183567  +  }
182475 183568     p->z += j;
182476 183569     return 1;
182477 183570   }
182478 183571   
182479 183572   /*
182480 183573   ** If the input is a well-formed JSON array of coordinates with at least
182481 183574   ** four coordinates and where each coordinate is itself a two-value array,
................................................................................
182525 183618       }
182526 183619       if( geopolySkipSpace(&s)==']'
182527 183620        && s.nVertex>=4
182528 183621        && s.a[0]==s.a[s.nVertex*2-2]
182529 183622        && s.a[1]==s.a[s.nVertex*2-1]
182530 183623        && (s.z++, geopolySkipSpace(&s)==0)
182531 183624       ){
182532         -      int nByte;
182533 183625         GeoPoly *pOut;
182534 183626         int x = 1;
182535 183627         s.nVertex--;  /* Remove the redundant vertex at the end */
182536         -      nByte = sizeof(GeoPoly) * s.nVertex*2*sizeof(GeoCoord);
182537         -      pOut = sqlite3_malloc64( nByte );
       183628  +      pOut = sqlite3_malloc64( GEOPOLY_SZ(s.nVertex) );
182538 183629         x = 1;
182539 183630         if( pOut==0 ) goto parse_json_err;
182540 183631         pOut->nVertex = s.nVertex;
182541 183632         memcpy(pOut->a, s.a, s.nVertex*2*sizeof(GeoCoord));
182542 183633         pOut->hdr[0] = *(unsigned char*)&x;
182543 183634         pOut->hdr[1] = (s.nVertex>>16)&0xff;
182544 183635         pOut->hdr[2] = (s.nVertex>>8)&0xff;
................................................................................
182732 183823         p->a[ii*2+1] = y1;
182733 183824       }
182734 183825       sqlite3_result_blob(context, p->hdr, 
182735 183826          4+8*p->nVertex, SQLITE_TRANSIENT);
182736 183827       sqlite3_free(p);
182737 183828     }
182738 183829   }
       183830  +
       183831  +/*
       183832  +** Compute the area enclosed by the polygon.
       183833  +**
       183834  +** This routine can also be used to detect polygons that rotate in
       183835  +** the wrong direction.  Polygons are suppose to be counter-clockwise (CCW).
       183836  +** This routine returns a negative value for clockwise (CW) polygons.
       183837  +*/
       183838  +static double geopolyArea(GeoPoly *p){
       183839  +  double rArea = 0.0;
       183840  +  int ii;
       183841  +  for(ii=0; ii<p->nVertex-1; ii++){
       183842  +    rArea += (p->a[ii*2] - p->a[ii*2+2])           /* (x0 - x1) */
       183843  +              * (p->a[ii*2+1] + p->a[ii*2+3])      /* (y0 + y1) */
       183844  +              * 0.5;
       183845  +  }
       183846  +  rArea += (p->a[ii*2] - p->a[0])                  /* (xN - x0) */
       183847  +           * (p->a[ii*2+1] + p->a[1])              /* (yN + y0) */
       183848  +           * 0.5;
       183849  +  return rArea;
       183850  +}
182739 183851   
182740 183852   /*
182741 183853   ** Implementation of the geopoly_area(X) function.
182742 183854   **
182743 183855   ** If the input is a well-formed Geopoly BLOB then return the area
182744 183856   ** enclosed by the polygon.  If the polygon circulates clockwise instead
182745 183857   ** of counterclockwise (as it should) then return the negative of the
................................................................................
182748 183860   static void geopolyAreaFunc(
182749 183861     sqlite3_context *context,
182750 183862     int argc,
182751 183863     sqlite3_value **argv
182752 183864   ){
182753 183865     GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
182754 183866     if( p ){
182755         -    double rArea = 0.0;
182756         -    int ii;
182757         -    for(ii=0; ii<p->nVertex-1; ii++){
182758         -      rArea += (p->a[ii*2] - p->a[ii*2+2])           /* (x0 - x1) */
182759         -                * (p->a[ii*2+1] + p->a[ii*2+3])      /* (y0 + y1) */
182760         -                * 0.5;
182761         -    }
182762         -    rArea += (p->a[ii*2] - p->a[0])                  /* (xN - x0) */
182763         -             * (p->a[ii*2+1] + p->a[1])              /* (yN + y0) */
182764         -             * 0.5;
182765         -    sqlite3_result_double(context, rArea);
       183867  +    sqlite3_result_double(context, geopolyArea(p));
       183868  +    sqlite3_free(p);
       183869  +  }            
       183870  +}
       183871  +
       183872  +/*
       183873  +** Implementation of the geopoly_ccw(X) function.
       183874  +**
       183875  +** If the rotation of polygon X is clockwise (incorrect) instead of
       183876  +** counter-clockwise (the correct winding order according to RFC7946)
       183877  +** then reverse the order of the vertexes in polygon X.  
       183878  +**
       183879  +** In other words, this routine returns a CCW polygon regardless of the
       183880  +** winding order of its input.
       183881  +**
       183882  +** Use this routine to sanitize historical inputs that that sometimes
       183883  +** contain polygons that wind in the wrong direction.
       183884  +*/
       183885  +static void geopolyCcwFunc(
       183886  +  sqlite3_context *context,
       183887  +  int argc,
       183888  +  sqlite3_value **argv
       183889  +){
       183890  +  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
       183891  +  if( p ){
       183892  +    if( geopolyArea(p)<0.0 ){
       183893  +      int ii, jj;
       183894  +      for(ii=2, jj=p->nVertex*2 - 2; ii<jj; ii+=2, jj-=2){
       183895  +        GeoCoord t = p->a[ii];
       183896  +        p->a[ii] = p->a[jj];
       183897  +        p->a[jj] = t;
       183898  +        t = p->a[ii+1];
       183899  +        p->a[ii+1] = p->a[jj+1];
       183900  +        p->a[jj+1] = t;
       183901  +      }
       183902  +    }
       183903  +    sqlite3_result_blob(context, p->hdr, 
       183904  +       4+8*p->nVertex, SQLITE_TRANSIENT);
182766 183905       sqlite3_free(p);
182767 183906     }            
182768 183907   }
       183908  +
       183909  +#define GEOPOLY_PI 3.1415926535897932385
       183910  +
       183911  +/* Fast approximation for cosine(X) for X between -0.5*pi and 2*pi
       183912  +*/
       183913  +static double geopolyCosine(double r){
       183914  +  assert( r>=-0.5*GEOPOLY_PI && r<=2.0*GEOPOLY_PI );
       183915  +  if( r>=1.5*GEOPOLY_PI ){
       183916  +    r -= 2.0*GEOPOLY_PI;
       183917  +  }
       183918  +  if( r>=0.5*GEOPOLY_PI ){
       183919  +    return -geopolyCosine(r-GEOPOLY_PI);
       183920  +  }else{
       183921  +    double r2 = r*r;
       183922  +    double r3 = r2*r;
       183923  +    double r5 = r3*r2;
       183924  +    return 0.9996949*r - 0.1656700*r3 + 0.0075134*r5;
       183925  +  }
       183926  +}
       183927  +
       183928  +/*
       183929  +** Function:   geopoly_regular(X,Y,R,N)
       183930  +**
       183931  +** Construct a simple, convex, regular polygon centered at X, Y
       183932  +** with circumradius R and with N sides.
       183933  +*/
       183934  +static void geopolyRegularFunc(
       183935  +  sqlite3_context *context,
       183936  +  int argc,
       183937  +  sqlite3_value **argv
       183938  +){
       183939  +  double x = sqlite3_value_double(argv[0]);
       183940  +  double y = sqlite3_value_double(argv[1]);
       183941  +  double r = sqlite3_value_double(argv[2]);
       183942  +  int n = sqlite3_value_int(argv[3]);
       183943  +  int i;
       183944  +  GeoPoly *p;
       183945  +
       183946  +  if( n<3 || r<=0.0 ) return;
       183947  +  if( n>1000 ) n = 1000;
       183948  +  p = sqlite3_malloc64( sizeof(*p) + (n-1)*2*sizeof(GeoCoord) );
       183949  +  if( p==0 ){
       183950  +    sqlite3_result_error_nomem(context);
       183951  +    return;
       183952  +  }
       183953  +  i = 1;
       183954  +  p->hdr[0] = *(unsigned char*)&i;
       183955  +  p->hdr[1] = 0;
       183956  +  p->hdr[2] = (n>>8)&0xff;
       183957  +  p->hdr[3] = n&0xff;
       183958  +  for(i=0; i<n; i++){
       183959  +    double rAngle = 2.0*GEOPOLY_PI*i/n;
       183960  +    p->a[i*2] = x - r*geopolyCosine(rAngle-0.5*GEOPOLY_PI);
       183961  +    p->a[i*2+1] = y + r*geopolyCosine(rAngle);
       183962  +  }
       183963  +  sqlite3_result_blob(context, p->hdr, 4+8*n, SQLITE_TRANSIENT);
       183964  +  sqlite3_free(p);
       183965  +}
182769 183966   
182770 183967   /*
182771 183968   ** If pPoly is a polygon, compute its bounding box. Then:
182772 183969   **
182773 183970   **    (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
182774 183971   **    (2) otherwise, compute a GeoPoly for the bounding box and return the
182775 183972   **        new GeoPoly
................................................................................
182807 184004         r = p->a[ii*2+1];
182808 184005         if( r<mnY ) mnY = (float)r;
182809 184006         else if( r>mxY ) mxY = (float)r;
182810 184007       }
182811 184008       if( pRc ) *pRc = SQLITE_OK;
182812 184009       if( aCoord==0 ){
182813 184010         geopolyBboxFill:
182814         -      pOut = sqlite3_realloc(p, sizeof(GeoPoly)+sizeof(GeoCoord)*6);
       184011  +      pOut = sqlite3_realloc(p, GEOPOLY_SZ(4));
182815 184012         if( pOut==0 ){
182816 184013           sqlite3_free(p);
182817 184014           if( context ) sqlite3_result_error_nomem(context);
182818 184015           if( pRc ) *pRc = SQLITE_NOMEM;
182819 184016           return 0;
182820 184017         }
182821 184018         pOut->nVertex = 4;
................................................................................
183835 185032       int jj;
183836 185033       int nChange = 0;
183837 185034       sqlite3_bind_int64(pUp, 1, cell.iRowid);
183838 185035       assert( pRtree->nAux>=1 );
183839 185036       if( sqlite3_value_nochange(aData[2]) ){
183840 185037         sqlite3_bind_null(pUp, 2);
183841 185038       }else{
183842         -      sqlite3_bind_value(pUp, 2, aData[2]);
       185039  +      GeoPoly *p = 0;
       185040  +      if( sqlite3_value_type(aData[2])==SQLITE_TEXT
       185041  +       && (p = geopolyFuncParam(0, aData[2], &rc))!=0
       185042  +       && rc==SQLITE_OK
       185043  +      ){
       185044  +        sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT);
       185045  +      }else{
       185046  +        sqlite3_bind_value(pUp, 2, aData[2]);
       185047  +      }
       185048  +      sqlite3_free(p);
183843 185049         nChange = 1;
183844 185050       }
183845 185051       for(jj=1; jj<pRtree->nAux; jj++){
183846 185052         nChange++;
183847 185053         sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
183848 185054       }
183849 185055       if( nChange ){
................................................................................
183879 185085       return SQLITE_INDEX_CONSTRAINT_FUNCTION+1;
183880 185086     }
183881 185087     return 0;
183882 185088   }
183883 185089   
183884 185090   
183885 185091   static sqlite3_module geopolyModule = {
183886         -  2,                          /* iVersion */
       185092  +  3,                          /* iVersion */
183887 185093     geopolyCreate,              /* xCreate - create a table */
183888 185094     geopolyConnect,             /* xConnect - connect to an existing table */
183889 185095     geopolyBestIndex,           /* xBestIndex - Determine search strategy */
183890 185096     rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
183891 185097     rtreeDestroy,               /* xDestroy - Drop a table */
183892 185098     rtreeOpen,                  /* xOpen - open a cursor */
183893 185099     rtreeClose,                 /* xClose - close a cursor */
................................................................................
183902 185108     rtreeEndTransaction,        /* xCommit - commit transaction */
183903 185109     rtreeEndTransaction,        /* xRollback - rollback transaction */
183904 185110     geopolyFindFunction,        /* xFindFunction - function overloading */
183905 185111     rtreeRename,                /* xRename - rename the table */
183906 185112     rtreeSavepoint,             /* xSavepoint */
183907 185113     0,                          /* xRelease */
183908 185114     0,                          /* xRollbackTo */
       185115  +  rtreeShadowName             /* xShadowName */
183909 185116   };
183910 185117   
183911 185118   static int sqlite3_geopoly_init(sqlite3 *db){
183912 185119     int rc = SQLITE_OK;
183913 185120     static const struct {
183914 185121       void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
183915         -    int nArg;
       185122  +    signed char nArg;
       185123  +    unsigned char bPure;
183916 185124       const char *zName;
183917 185125     } aFunc[] = {
183918         -     { geopolyAreaFunc,          1,    "geopoly_area"             },
183919         -     { geopolyBlobFunc,          1,    "geopoly_blob"             },
183920         -     { geopolyJsonFunc,          1,    "geopoly_json"             },
183921         -     { geopolySvgFunc,          -1,    "geopoly_svg"              },
183922         -     { geopolyWithinFunc,        2,    "geopoly_within"           },
183923         -     { geopolyContainsPointFunc, 3,    "geopoly_contains_point"   },
183924         -     { geopolyOverlapFunc,       2,    "geopoly_overlap"          },
183925         -     { geopolyDebugFunc,         1,    "geopoly_debug"            },
183926         -     { geopolyBBoxFunc,          1,    "geopoly_bbox"             },
183927         -     { geopolyXformFunc,         7,    "geopoly_xform"            },
       185126  +     { geopolyAreaFunc,          1, 1,    "geopoly_area"             },
       185127  +     { geopolyBlobFunc,          1, 1,    "geopoly_blob"             },
       185128  +     { geopolyJsonFunc,          1, 1,    "geopoly_json"             },
       185129  +     { geopolySvgFunc,          -1, 1,    "geopoly_svg"              },
       185130  +     { geopolyWithinFunc,        2, 1,    "geopoly_within"           },
       185131  +     { geopolyContainsPointFunc, 3, 1,    "geopoly_contains_point"   },
       185132  +     { geopolyOverlapFunc,       2, 1,    "geopoly_overlap"          },
       185133  +     { geopolyDebugFunc,         1, 0,    "geopoly_debug"            },
       185134  +     { geopolyBBoxFunc,          1, 1,    "geopoly_bbox"             },
       185135  +     { geopolyXformFunc,         7, 1,    "geopoly_xform"            },
       185136  +     { geopolyRegularFunc,       4, 1,    "geopoly_regular"          },
       185137  +     { geopolyCcwFunc,           1, 1,    "geopoly_ccw"              },
183928 185138     };
183929 185139     static const struct {
183930 185140       void (*xStep)(sqlite3_context*,int,sqlite3_value**);
183931 185141       void (*xFinal)(sqlite3_context*);
183932 185142       const char *zName;
183933 185143     } aAgg[] = {
183934 185144        { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox"    },
183935 185145     };
183936 185146     int i;
183937 185147     for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
       185148  +    int enc = aFunc[i].bPure ? SQLITE_UTF8|SQLITE_DETERMINISTIC : SQLITE_UTF8;
183938 185149       rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
183939         -                                 SQLITE_UTF8, 0,
       185150  +                                 enc, 0,
183940 185151                                    aFunc[i].xFunc, 0, 0);
183941 185152     }
183942 185153     for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
183943 185154       rc = sqlite3_create_function(db, aAgg[i].zName, 1, SQLITE_UTF8, 0,
183944 185155                                    0, aAgg[i].xStep, aAgg[i].xFinal);
183945 185156     }
183946 185157     if( rc==SQLITE_OK ){
................................................................................
185959 187170   ** space used by the RBU handle.
185960 187171   */
185961 187172   struct rbu_vfs {
185962 187173     sqlite3_vfs base;               /* rbu VFS shim methods */
185963 187174     sqlite3_vfs *pRealVfs;          /* Underlying VFS */
185964 187175     sqlite3_mutex *mutex;           /* Mutex to protect pMain */
185965 187176     sqlite3rbu *pRbu;               /* Owner RBU object */
185966         -  rbu_file *pMain;                /* Linked list of main db files */
       187177  +  rbu_file *pMain;                /* List of main db files */
       187178  +  rbu_file *pMainRbu;             /* List of main db files with pRbu!=0 */
185967 187179   };
185968 187180   
185969 187181   /*
185970 187182   ** Each file opened by an rbu VFS is represented by an instance of
185971 187183   ** the following structure.
185972 187184   **
185973 187185   ** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
................................................................................
185988 187200     int nShm;                       /* Number of entries in apShm[] array */
185989 187201     char **apShm;                   /* Array of mmap'd *-shm regions */
185990 187202     char *zDel;                     /* Delete this when closing file */
185991 187203   
185992 187204     const char *zWal;               /* Wal filename for this main db file */
185993 187205     rbu_file *pWalFd;               /* Wal file descriptor for this main db */
185994 187206     rbu_file *pMainNext;            /* Next MAIN_DB file */
       187207  +  rbu_file *pMainRbuNext;         /* Next MAIN_DB file with pRbu!=0 */
185995 187208   };
185996 187209   
185997 187210   /*
185998 187211   ** True for an RBU vacuum handle, or false otherwise.
185999 187212   */
186000 187213   #define rbuIsVacuum(p) ((p)->zTarget==0)
186001 187214   
................................................................................
189583 190796     i64 nDiff = nNew - pFd->sz;
189584 190797     pRbu->szTemp += nDiff;
189585 190798     pFd->sz = nNew;
189586 190799     assert( pRbu->szTemp>=0 );
189587 190800     if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL;
189588 190801     return SQLITE_OK;
189589 190802   }
       190803  +
       190804  +/*
       190805  +** Add an item to the main-db lists, if it is not already present.
       190806  +**
       190807  +** There are two main-db lists. One for all file descriptors, and one
       190808  +** for all file descriptors with rbu_file.pDb!=0. If the argument has
       190809  +** rbu_file.pDb!=0, then it is assumed to already be present on the
       190810  +** main list and is only added to the pDb!=0 list.
       190811  +*/
       190812  +static void rbuMainlistAdd(rbu_file *p){
       190813  +  rbu_vfs *pRbuVfs = p->pRbuVfs;
       190814  +  rbu_file *pIter;
       190815  +  assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) );
       190816  +  sqlite3_mutex_enter(pRbuVfs->mutex);
       190817  +  if( p->pRbu==0 ){
       190818  +    for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
       190819  +    p->pMainNext = pRbuVfs->pMain;
       190820  +    pRbuVfs->pMain = p;
       190821  +  }else{
       190822  +    for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
       190823  +    if( pIter==0 ){
       190824  +      p->pMainRbuNext = pRbuVfs->pMainRbu;
       190825  +      pRbuVfs->pMainRbu = p;
       190826  +    }
       190827  +  }
       190828  +  sqlite3_mutex_leave(pRbuVfs->mutex);
       190829  +}
       190830  +
       190831  +/*
       190832  +** Remove an item from the main-db lists.
       190833  +*/
       190834  +static void rbuMainlistRemove(rbu_file *p){
       190835  +  rbu_file **pp;
       190836  +  sqlite3_mutex_enter(p->pRbuVfs->mutex);
       190837  +  for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
       190838  +  if( *pp ) *pp = p->pMainNext;
       190839  +  p->pMainNext = 0;
       190840  +  for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
       190841  +  if( *pp ) *pp = p->pMainRbuNext;
       190842  +  p->pMainRbuNext = 0;
       190843  +  sqlite3_mutex_leave(p->pRbuVfs->mutex);
       190844  +}
       190845  +
       190846  +/*
       190847  +** Given that zWal points to a buffer containing a wal file name passed to 
       190848  +** either the xOpen() or xAccess() VFS method, search the main-db list for
       190849  +** a file-handle opened by the same database connection on the corresponding
       190850  +** database file.
       190851  +**
       190852  +** If parameter bRbu is true, only search for file-descriptors with
       190853  +** rbu_file.pDb!=0.
       190854  +*/
       190855  +static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){
       190856  +  rbu_file *pDb;
       190857  +  sqlite3_mutex_enter(pRbuVfs->mutex);
       190858  +  if( bRbu ){
       190859  +    for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
       190860  +  }else{
       190861  +    for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
       190862  +  }
       190863  +  sqlite3_mutex_leave(pRbuVfs->mutex);
       190864  +  return pDb;
       190865  +}
189590 190866   
189591 190867   /*
189592 190868   ** Close an rbu file.
189593 190869   */
189594 190870   static int rbuVfsClose(sqlite3_file *pFile){
189595 190871     rbu_file *p = (rbu_file*)pFile;
189596 190872     int rc;
................................................................................
189601 190877       sqlite3_free(p->apShm[i]);
189602 190878     }
189603 190879     sqlite3_free(p->apShm);
189604 190880     p->apShm = 0;
189605 190881     sqlite3_free(p->zDel);
189606 190882   
189607 190883     if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
189608         -    rbu_file **pp;
189609         -    sqlite3_mutex_enter(p->pRbuVfs->mutex);
189610         -    for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
189611         -    *pp = p->pMainNext;
189612         -    sqlite3_mutex_leave(p->pRbuVfs->mutex);
       190884  +    rbuMainlistRemove(p);
189613 190885       rbuUnlockShm(p);
189614 190886       p->pReal->pMethods->xShmUnmap(p->pReal, 0);
189615 190887     }
189616 190888     else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
189617 190889       rbuUpdateTempSize(p, 0);
189618 190890     }
       190891  +  assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p );
189619 190892   
189620 190893     /* Close the underlying file handle */
189621 190894     rc = p->pReal->pMethods->xClose(p->pReal);
189622 190895     return rc;
189623 190896   }
189624 190897   
189625 190898   
................................................................................
189870 191143         rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
189871 191144         if( rc==SQLITE_OK ){
189872 191145           rc = SQLITE_ERROR;
189873 191146           pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
189874 191147         }else if( rc==SQLITE_NOTFOUND ){
189875 191148           pRbu->pTargetFd = p;
189876 191149           p->pRbu = pRbu;
       191150  +        if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
       191151  +          rbuMainlistAdd(p);
       191152  +        }
189877 191153           if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
189878 191154           rc = SQLITE_OK;
189879 191155         }
189880 191156       }
189881 191157       return rc;
189882 191158     }
189883 191159     else if( op==SQLITE_FCNTL_RBUCNT ){
................................................................................
190031 191307       /* Release the checkpointer and writer locks */
190032 191308       rbuUnlockShm(p);
190033 191309       rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
190034 191310     }
190035 191311     return rc;
190036 191312   }
190037 191313   
190038         -/*
190039         -** Given that zWal points to a buffer containing a wal file name passed to 
190040         -** either the xOpen() or xAccess() VFS method, return a pointer to the
190041         -** file-handle opened by the same database connection on the corresponding
190042         -** database file.
190043         -*/
190044         -static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal){
190045         -  rbu_file *pDb;
190046         -  sqlite3_mutex_enter(pRbuVfs->mutex);
190047         -  for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
190048         -  sqlite3_mutex_leave(pRbuVfs->mutex);
190049         -  return pDb;
190050         -}
190051         -
190052 191314   /* 
190053 191315   ** A main database named zName has just been opened. The following 
190054 191316   ** function returns a pointer to a buffer owned by SQLite that contains
190055 191317   ** the name of the *-wal file this db connection will use. SQLite
190056 191318   ** happens to pass a pointer to this buffer when using xAccess()
190057 191319   ** or xOpen() to operate on the *-wal file.  
190058 191320   */
................................................................................
190123 191385         ** (pFd->zWal) to point to a buffer owned by SQLite that contains
190124 191386         ** the name of the *-wal file this db connection will use. SQLite
190125 191387         ** happens to pass a pointer to this buffer when using xAccess()
190126 191388         ** or xOpen() to operate on the *-wal file.  */
190127 191389         pFd->zWal = rbuMainToWal(zName, flags);
190128 191390       }
190129 191391       else if( flags & SQLITE_OPEN_WAL ){
190130         -      rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName);
       191392  +      rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0);
190131 191393         if( pDb ){
190132 191394           if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
190133 191395             /* This call is to open a *-wal file. Intead, open the *-oal. This
190134 191396             ** code ensures that the string passed to xOpen() is terminated by a
190135 191397             ** pair of '\0' bytes in case the VFS attempts to extract a URI 
190136 191398             ** parameter from it.  */
190137 191399             const char *zBase = zName;
................................................................................
190175 191437     }
190176 191438     if( pFd->pReal->pMethods ){
190177 191439       /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
190178 191440       ** pointer and, if the file is a main database file, link it into the
190179 191441       ** mutex protected linked list of all such files.  */
190180 191442       pFile->pMethods = &rbuvfs_io_methods;
190181 191443       if( flags & SQLITE_OPEN_MAIN_DB ){
190182         -      sqlite3_mutex_enter(pRbuVfs->mutex);
190183         -      pFd->pMainNext = pRbuVfs->pMain;
190184         -      pRbuVfs->pMain = pFd;
190185         -      sqlite3_mutex_leave(pRbuVfs->mutex);
       191444  +      rbuMainlistAdd(pFd);
190186 191445       }
190187 191446     }else{
190188 191447       sqlite3_free(pFd->zDel);
190189 191448     }
190190 191449   
190191 191450     return rc;
190192 191451   }
................................................................................
190226 191485     **
190227 191486     **   b) if the *-wal file does not exist, claim that it does anyway,
190228 191487     **      causing SQLite to call xOpen() to open it. This call will also
190229 191488     **      be intercepted (see the rbuVfsOpen() function) and the *-oal
190230 191489     **      file opened instead.
190231 191490     */
190232 191491     if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
190233         -    rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath);
       191492  +    rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
190234 191493       if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
190235 191494         if( *pResOut ){
190236 191495           rc = SQLITE_CANTOPEN;
190237 191496         }else{
190238 191497           sqlite3_int64 sz = 0;
190239 191498           rc = rbuVfsFileSize(&pDb->base, &sz);
190240 191499           *pResOut = (sz>0);
................................................................................
190639 191898   ** operate on a different database schema, so check for it.
190640 191899   **
190641 191900   ** idxNum is normally 0, but will be 1 if a schema=? constraint exists.
190642 191901   */
190643 191902   static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
190644 191903     int i;
190645 191904   
190646         -  pIdxInfo->estimatedCost = 1.0e6;  /* Initial cost estimate */
190647         -
190648 191905     /* Look for a valid schema=? constraint.  If found, change the idxNum to
190649 191906     ** 1 and request the value of that constraint be sent to xFilter.  And
190650 191907     ** lower the cost estimate to encourage the constrained version to be
190651 191908     ** used.
190652 191909     */
190653 191910     for(i=0; i<pIdxInfo->nConstraint; i++){
190654         -    if( pIdxInfo->aConstraint[i].usable==0 ) continue;
190655         -    if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
190656 191911       if( pIdxInfo->aConstraint[i].iColumn!=10 ) continue;
       191912  +    if( pIdxInfo->aConstraint[i].usable==0 ) return SQLITE_CONSTRAINT;
       191913  +    if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
190657 191914       pIdxInfo->idxNum = 1;
190658 191915       pIdxInfo->estimatedCost = 1.0;
190659 191916       pIdxInfo->aConstraintUsage[i].argvIndex = 1;
190660 191917       pIdxInfo->aConstraintUsage[i].omit = 1;
190661 191918       break;
190662 191919     }
190663 191920   
................................................................................
190699 191956       pCsr->iDb = pTab->iDb;
190700 191957     }
190701 191958   
190702 191959     *ppCursor = (sqlite3_vtab_cursor *)pCsr;
190703 191960     return SQLITE_OK;
190704 191961   }
190705 191962   
190706         -static void statClearPage(StatPage *p){
       191963  +static void statClearCells(StatPage *p){
190707 191964     int i;
190708 191965     if( p->aCell ){
190709 191966       for(i=0; i<p->nCell; i++){
190710 191967         sqlite3_free(p->aCell[i].aOvfl);
190711 191968       }
190712 191969       sqlite3_free(p->aCell);
190713 191970     }
       191971  +  p->nCell = 0;
       191972  +  p->aCell = 0;
       191973  +}
       191974  +
       191975  +static void statClearPage(StatPage *p){
       191976  +  statClearCells(p);
190714 191977     sqlite3PagerUnref(p->pPg);
190715 191978     sqlite3_free(p->zPath);
190716 191979     memset(p, 0, sizeof(StatPage));
190717 191980   }
190718 191981   
190719 191982   static void statResetCsr(StatCursor *pCsr){
190720 191983     int i;
................................................................................
190769 192032     int isLeaf;
190770 192033     int szPage;
190771 192034   
190772 192035     u8 *aData = sqlite3PagerGetData(p->pPg);
190773 192036     u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
190774 192037   
190775 192038     p->flags = aHdr[0];
       192039  +  if( p->flags==0x0A || p->flags==0x0D ){
       192040  +    isLeaf = 1;
       192041  +    nHdr = 8;
       192042  +  }else if( p->flags==0x05 || p->flags==0x02 ){
       192043  +    isLeaf = 0;
       192044  +    nHdr = 12;
       192045  +  }else{
       192046  +    goto statPageIsCorrupt;
       192047  +  }
       192048  +  if( p->iPgno==1 ) nHdr += 100;
190776 192049     p->nCell = get2byte(&aHdr[3]);
190777 192050     p->nMxPayload = 0;
190778         -
190779         -  isLeaf = (p->flags==0x0A || p->flags==0x0D);
190780         -  nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100;
       192051  +  szPage = sqlite3BtreeGetPageSize(pBt);
190781 192052   
190782 192053     nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
190783 192054     nUnused += (int)aHdr[7];
190784 192055     iOff = get2byte(&aHdr[1]);
190785 192056     while( iOff ){
       192057  +    int iNext;
       192058  +    if( iOff>=szPage ) goto statPageIsCorrupt;
190786 192059       nUnused += get2byte(&aData[iOff+2]);
190787         -    iOff = get2byte(&aData[iOff]);
       192060  +    iNext = get2byte(&aData[iOff]);
       192061  +    if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
       192062  +    iOff = iNext;
190788 192063     }
190789 192064     p->nUnused = nUnused;
190790 192065     p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
190791         -  szPage = sqlite3BtreeGetPageSize(pBt);
190792 192066   
190793 192067     if( p->nCell ){
190794 192068       int i;                        /* Used to iterate through cells */
190795 192069       int nUsable;                  /* Usable bytes per page */
190796 192070   
190797 192071       sqlite3BtreeEnter(pBt);
190798 192072       nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
................................................................................
190801 192075       if( p->aCell==0 ) return SQLITE_NOMEM_BKPT;
190802 192076       memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
190803 192077   
190804 192078       for(i=0; i<p->nCell; i++){
190805 192079         StatCell *pCell = &p->aCell[i];
190806 192080   
190807 192081         iOff = get2byte(&aData[nHdr+i*2]);
       192082  +      if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
190808 192083         if( !isLeaf ){
190809 192084           pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
190810 192085           iOff += 4;
190811 192086         }
190812 192087         if( p->flags==0x05 ){
190813 192088           /* A table interior node. nPayload==0. */
190814 192089         }else{
................................................................................
190817 192092           iOff += getVarint32(&aData[iOff], nPayload);
190818 192093           if( p->flags==0x0D ){
190819 192094             u64 dummy;
190820 192095             iOff += sqlite3GetVarint(&aData[iOff], &dummy);
190821 192096           }
190822 192097           if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
190823 192098           getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
       192099  +        if( nLocal<0 ) goto statPageIsCorrupt;
190824 192100           pCell->nLocal = nLocal;
190825         -        assert( nLocal>=0 );
190826 192101           assert( nPayload>=(u32)nLocal );
190827 192102           assert( nLocal<=(nUsable-35) );
190828 192103           if( nPayload>(u32)nLocal ){
190829 192104             int j;
190830 192105             int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
       192106  +          if( iOff+nLocal>nUsable ) goto statPageIsCorrupt;
190831 192107             pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
190832 192108             pCell->nOvfl = nOvfl;
190833 192109             pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
190834 192110             if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
190835 192111             pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
190836 192112             for(j=1; j<nOvfl; j++){
190837 192113               int rc;
................................................................................
190847 192123             }
190848 192124           }
190849 192125         }
190850 192126       }
190851 192127     }
190852 192128   
190853 192129     return SQLITE_OK;
       192130  +
       192131  +statPageIsCorrupt:
       192132  +  p->flags = 0;
       192133  +  statClearCells(p);
       192134  +  return SQLITE_OK;
190854 192135   }
190855 192136   
190856 192137   /*
190857 192138   ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
190858 192139   ** the current value of pCsr->iPageno.
190859 192140   */
190860 192141   static void statSizeAndOffset(StatCursor *pCsr){
................................................................................
191142 192423       0,                            /* xCommit */
191143 192424       0,                            /* xRollback */
191144 192425       0,                            /* xFindMethod */
191145 192426       0,                            /* xRename */
191146 192427       0,                            /* xSavepoint */
191147 192428       0,                            /* xRelease */
191148 192429       0,                            /* xRollbackTo */
       192430  +    0                             /* xShadowName */
191149 192431     };
191150 192432     return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
191151 192433   }
191152 192434   #elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
191153 192435   SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
191154 192436   #endif /* SQLITE_ENABLE_DBSTAT_VTAB */
191155 192437   
................................................................................
191272 192554     ** unavailable
191273 192555     */
191274 192556     for(i=0; i<pIdxInfo->nConstraint; i++){
191275 192557       struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
191276 192558       if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
191277 192559       if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
191278 192560       if( !p->usable ){
191279         -      /* No solution.  Use the default SQLITE_BIG_DBL cost */
191280         -      pIdxInfo->estimatedRows = 0x7fffffff;
191281         -      return SQLITE_OK;
       192561  +      /* No solution. */
       192562  +      return SQLITE_CONSTRAINT;
191282 192563       }
191283 192564       iPlan = 2;
191284 192565       pIdxInfo->aConstraintUsage[i].argvIndex = 1;
191285 192566       pIdxInfo->aConstraintUsage[i].omit = 1;
191286 192567       break;
191287 192568     }
191288 192569   
................................................................................
191466 192747     char *zErr = 0;
191467 192748     const char *zSchema;
191468 192749     int iDb;
191469 192750     Btree *pBt;
191470 192751     Pager *pPager;
191471 192752     int szPage;
191472 192753   
       192754  +  if( pTab->db->flags & SQLITE_Defensive ){
       192755  +    zErr = "read-only";
       192756  +    goto update_fail;
       192757  +  }
191473 192758     if( argc==1 ){
191474 192759       zErr = "cannot delete";
191475 192760       goto update_fail;
191476 192761     }
191477 192762     pgno = sqlite3_value_int(argv[0]);
191478 192763     if( (Pgno)sqlite3_value_int(argv[1])!=pgno ){
191479 192764       zErr = "cannot insert";
................................................................................
191556 192841       0,                            /* xCommit */
191557 192842       0,                            /* xRollback */
191558 192843       0,                            /* xFindMethod */
191559 192844       0,                            /* xRename */
191560 192845       0,                            /* xSavepoint */
191561 192846       0,                            /* xRelease */
191562 192847       0,                            /* xRollbackTo */
       192848  +    0                             /* xShadowName */
191563 192849     };
191564 192850     return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0);
191565 192851   }
191566 192852   #elif defined(SQLITE_ENABLE_DBPAGE_VTAB)
191567 192853   SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK; }
191568 192854   #endif /* SQLITE_ENABLE_DBSTAT_VTAB */
191569 192855   
................................................................................
191591 192877   #ifndef SESSIONS_STRM_CHUNK_SIZE
191592 192878   # ifdef SQLITE_TEST
191593 192879   #   define SESSIONS_STRM_CHUNK_SIZE 64
191594 192880   # else
191595 192881   #   define SESSIONS_STRM_CHUNK_SIZE 1024
191596 192882   # endif
191597 192883   #endif
       192884  +
       192885  +static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE;
191598 192886   
191599 192887   typedef struct SessionHook SessionHook;
191600 192888   struct SessionHook {
191601 192889     void *pCtx;
191602 192890     int (*xOld)(void*,int,sqlite3_value**);
191603 192891     int (*xNew)(void*,int,sqlite3_value**);
191604 192892     int (*xCount)(void*);
................................................................................
191654 192942   /*
191655 192943   ** Structure for changeset iterators.
191656 192944   */
191657 192945   struct sqlite3_changeset_iter {
191658 192946     SessionInput in;                /* Input buffer or stream */
191659 192947     SessionBuffer tblhdr;           /* Buffer to hold apValue/zTab/abPK/ */
191660 192948     int bPatchset;                  /* True if this is a patchset */
       192949  +  int bInvert;                    /* True to invert changeset */
191661 192950     int rc;                         /* Iterator error code */
191662 192951     sqlite3_stmt *pConflict;        /* Points to conflicting row, if any */
191663 192952     char *zTab;                     /* Current table */
191664 192953     int nCol;                       /* Number of columns in zTab */
191665 192954     int op;                         /* Current operation */
191666 192955     int bIndirect;                  /* True if current change was indirect */
191667 192956     u8 *abPK;                       /* Primary key array */
................................................................................
191810 193099   ** columns and columns that are modified by the UPDATE are set to "undefined".
191811 193100   ** PRIMARY KEY fields contain the values identifying the table row to update,
191812 193101   ** and fields associated with modified columns contain the new column values.
191813 193102   **
191814 193103   ** The records associated with INSERT changes are in the same format as for
191815 193104   ** changesets. It is not possible for a record associated with an INSERT
191816 193105   ** change to contain a field set to "undefined".
       193106  +**
       193107  +** REBASE BLOB FORMAT:
       193108  +**
       193109  +** A rebase blob may be output by sqlite3changeset_apply_v2() and its 
       193110  +** streaming equivalent for use with the sqlite3_rebaser APIs to rebase
       193111  +** existing changesets. A rebase blob contains one entry for each conflict
       193112  +** resolved using either the OMIT or REPLACE strategies within the apply_v2()
       193113  +** call.
       193114  +**
       193115  +** The format used for a rebase blob is very similar to that used for
       193116  +** changesets. All entries related to a single table are grouped together.
       193117  +**
       193118  +** Each group of entries begins with a table header in changeset format:
       193119  +**
       193120  +**   1 byte: Constant 0x54 (capital 'T')
       193121  +**   Varint: Number of columns in the table.
       193122  +**   nCol bytes: 0x01 for PK columns, 0x00 otherwise.
       193123  +**   N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
       193124  +**
       193125  +** Followed by one or more entries associated with the table.
       193126  +**
       193127  +**   1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09).
       193128  +**   1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT.
       193129  +**   record: (in the record format defined above).
       193130  +**
       193131  +** In a rebase blob, the first field is set to SQLITE_INSERT if the change
       193132  +** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if
       193133  +** it was a DELETE. The second field is set to 0x01 if the conflict 
       193134  +** resolution strategy was REPLACE, or 0x00 if it was OMIT.
       193135  +**
       193136  +** If the change that caused the conflict was a DELETE, then the single
       193137  +** record is a copy of the old.* record from the original changeset. If it
       193138  +** was an INSERT, then the single record is a copy of the new.* record. If
       193139  +** the conflicting change was an UPDATE, then the single record is a copy
       193140  +** of the new.* record with the PK fields filled in based on the original
       193141  +** old.* record.
191817 193142   */
191818 193143   
191819 193144   /*
191820 193145   ** For each row modified during a session, there exists a single instance of
191821 193146   ** this structure stored in a SessionTable.aChange[] hash table.
191822 193147   */
191823 193148   struct SessionChange {
................................................................................
193360 194685   **
193361 194686   ** If successful, return zero. Otherwise, if an OOM condition is encountered,
193362 194687   ** set *pRc to SQLITE_NOMEM and return non-zero.
193363 194688   */
193364 194689   static int sessionBufferGrow(SessionBuffer *p, int nByte, int *pRc){
193365 194690     if( *pRc==SQLITE_OK && p->nAlloc-p->nBuf<nByte ){
193366 194691       u8 *aNew;
193367         -    int nNew = p->nAlloc ? p->nAlloc : 128;
       194692  +    i64 nNew = p->nAlloc ? p->nAlloc : 128;
193368 194693       do {
193369 194694         nNew = nNew*2;
193370         -    }while( nNew<(p->nBuf+nByte) );
       194695  +    }while( (nNew-p->nBuf)<nByte );
193371 194696   
193372         -    aNew = (u8 *)sqlite3_realloc(p->aBuf, nNew);
       194697  +    aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
193373 194698       if( 0==aNew ){
193374 194699         *pRc = SQLITE_NOMEM;
193375 194700       }else{
193376 194701         p->aBuf = aNew;
193377 194702         p->nAlloc = nNew;
193378 194703       }
193379 194704     }
................................................................................
193963 195288             }else if( p->op!=SQLITE_INSERT ){
193964 195289               rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
193965 195290             }
193966 195291             if( rc==SQLITE_OK ){
193967 195292               rc = sqlite3_reset(pSel);
193968 195293             }
193969 195294   
193970         -          /* If the buffer is now larger than SESSIONS_STRM_CHUNK_SIZE, pass
       195295  +          /* If the buffer is now larger than sessions_strm_chunk_size, pass
193971 195296             ** its contents to the xOutput() callback. */
193972 195297             if( xOutput 
193973 195298              && rc==SQLITE_OK 
193974 195299              && buf.nBuf>nNoop 
193975         -           && buf.nBuf>SESSIONS_STRM_CHUNK_SIZE 
       195300  +           && buf.nBuf>sessions_strm_chunk_size 
193976 195301             ){
193977 195302               rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
193978 195303               nNoop = -1;
193979 195304               buf.nBuf = 0;
193980 195305             }
193981 195306   
193982 195307           }
................................................................................
194107 195432   ** Do the work for either sqlite3changeset_start() or start_strm().
194108 195433   */
194109 195434   static int sessionChangesetStart(
194110 195435     sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
194111 195436     int (*xInput)(void *pIn, void *pData, int *pnData),
194112 195437     void *pIn,
194113 195438     int nChangeset,                 /* Size of buffer pChangeset in bytes */
194114         -  void *pChangeset                /* Pointer to buffer containing changeset */
       195439  +  void *pChangeset,               /* Pointer to buffer containing changeset */
       195440  +  int bInvert                     /* True to invert changeset */
194115 195441   ){
194116 195442     sqlite3_changeset_iter *pRet;   /* Iterator to return */
194117 195443     int nByte;                      /* Number of bytes to allocate for iterator */
194118 195444   
194119 195445     assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
194120 195446   
194121 195447     /* Zero the output variable in case an error occurs. */
................................................................................
194127 195453     if( !pRet ) return SQLITE_NOMEM;
194128 195454     memset(pRet, 0, sizeof(sqlite3_changeset_iter));
194129 195455     pRet->in.aData = (u8 *)pChangeset;
194130 195456     pRet->in.nData = nChangeset;
194131 195457     pRet->in.xInput = xInput;
194132 195458     pRet->in.pIn = pIn;
194133 195459     pRet->in.bEof = (xInput ? 0 : 1);
       195460  +  pRet->bInvert = bInvert;
194134 195461   
194135 195462     /* Populate the output variable and return success. */
194136 195463     *pp = pRet;
194137 195464     return SQLITE_OK;
194138 195465   }
194139 195466   
194140 195467   /*
................................................................................
194141 195468   ** Create an iterator used to iterate through the contents of a changeset.
194142 195469   */
194143 195470   SQLITE_API int sqlite3changeset_start(
194144 195471     sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
194145 195472     int nChangeset,                 /* Size of buffer pChangeset in bytes */
194146 195473     void *pChangeset                /* Pointer to buffer containing changeset */
194147 195474   ){
194148         -  return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset);
       195475  +  return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0);
       195476  +}
       195477  +SQLITE_API int sqlite3changeset_start_v2(
       195478  +  sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
       195479  +  int nChangeset,                 /* Size of buffer pChangeset in bytes */
       195480  +  void *pChangeset,               /* Pointer to buffer containing changeset */
       195481  +  int flags
       195482  +){
       195483  +  int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
       195484  +  return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert);
194149 195485   }
194150 195486   
194151 195487   /*
194152 195488   ** Streaming version of sqlite3changeset_start().
194153 195489   */
194154 195490   SQLITE_API int sqlite3changeset_start_strm(
194155 195491     sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
194156 195492     int (*xInput)(void *pIn, void *pData, int *pnData),
194157 195493     void *pIn
194158 195494   ){
194159         -  return sessionChangesetStart(pp, xInput, pIn, 0, 0);
       195495  +  return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0);
       195496  +}
       195497  +SQLITE_API int sqlite3changeset_start_v2_strm(
       195498  +  sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
       195499  +  int (*xInput)(void *pIn, void *pData, int *pnData),
       195500  +  void *pIn,
       195501  +  int flags
       195502  +){
       195503  +  int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
       195504  +  return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert);
194160 195505   }
194161 195506   
194162 195507   /*
194163 195508   ** If the SessionInput object passed as the only argument is a streaming
194164 195509   ** object and the buffer is full, discard some data to free up space.
194165 195510   */
194166 195511   static void sessionDiscardData(SessionInput *pIn){
194167         -  if( pIn->xInput && pIn->iNext>=SESSIONS_STRM_CHUNK_SIZE ){
       195512  +  if( pIn->xInput && pIn->iNext>=sessions_strm_chunk_size ){
194168 195513       int nMove = pIn->buf.nBuf - pIn->iNext;
194169 195514       assert( nMove>=0 );
194170 195515       if( nMove>0 ){
194171 195516         memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
194172 195517       }
194173 195518       pIn->buf.nBuf -= pIn->iNext;
194174 195519       pIn->iNext = 0;
................................................................................
194183 195528   **
194184 195529   ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
194185 195530   */
194186 195531   static int sessionInputBuffer(SessionInput *pIn, int nByte){
194187 195532     int rc = SQLITE_OK;
194188 195533     if( pIn->xInput ){
194189 195534       while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
194190         -      int nNew = SESSIONS_STRM_CHUNK_SIZE;
       195535  +      int nNew = sessions_strm_chunk_size;
194191 195536   
194192 195537         if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
194193 195538         if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
194194 195539           rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
194195 195540           if( nNew==0 ){
194196 195541             pIn->bEof = 1;
194197 195542           }else{
................................................................................
194531 195876       if( sessionChangesetReadTblhdr(p) ) return p->rc;
194532 195877       if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
194533 195878       p->in.iCurrent = p->in.iNext;
194534 195879       if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
194535 195880       op = p->in.aData[p->in.iNext++];
194536 195881     }
194537 195882   
194538         -  if( p->zTab==0 ){
       195883  +  if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
194539 195884       /* The first record in the changeset is not a table header. Must be a
194540 195885       ** corrupt changeset. */
194541         -    assert( p->in.iNext==1 );
       195886  +    assert( p->in.iNext==1 || p->zTab );
194542 195887       return (p->rc = SQLITE_CORRUPT_BKPT);
194543 195888     }
194544 195889   
194545 195890     p->op = op;
194546 195891     p->bIndirect = p->in.aData[p->in.iNext++];
194547 195892     if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
194548 195893       return (p->rc = SQLITE_CORRUPT_BKPT);
................................................................................
194559 195904         nVal = p->nCol;
194560 195905       }
194561 195906       p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
194562 195907       if( p->rc!=SQLITE_OK ) return p->rc;
194563 195908       *paRec = &p->in.aData[p->in.iNext];
194564 195909       p->in.iNext += *pnRec;
194565 195910     }else{
       195911  +    sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
       195912  +    sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
194566 195913   
194567 195914       /* If this is an UPDATE or DELETE, read the old.* record. */
194568 195915       if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
194569 195916         u8 *abPK = p->bPatchset ? p->abPK : 0;
194570         -      p->rc = sessionReadRecord(&p->in, p->nCol, abPK, p->apValue);
       195917  +      p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld);
194571 195918         if( p->rc!=SQLITE_OK ) return p->rc;
194572 195919       }
194573 195920   
194574 195921       /* If this is an INSERT or UPDATE, read the new.* record. */
194575 195922       if( p->op!=SQLITE_DELETE ){
194576         -      p->rc = sessionReadRecord(&p->in, p->nCol, 0, &p->apValue[p->nCol]);
       195923  +      p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew);
194577 195924         if( p->rc!=SQLITE_OK ) return p->rc;
194578 195925       }
194579 195926   
194580         -    if( p->bPatchset && p->op==SQLITE_UPDATE ){
       195927  +    if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE ){
194581 195928         /* If this is an UPDATE that is part of a patchset, then all PK and
194582 195929         ** modified fields are present in the new.* record. The old.* record
194583 195930         ** is currently completely empty. This block shifts the PK fields from
194584 195931         ** new.* to old.*, to accommodate the code that reads these arrays.  */
194585 195932         for(i=0; i<p->nCol; i++){
194586         -        assert( p->apValue[i]==0 );
       195933  +        assert( p->bPatchset==0 || p->apValue[i]==0 );
194587 195934           if( p->abPK[i] ){
       195935  +          assert( p->apValue[i]==0 );
194588 195936             p->apValue[i] = p->apValue[i+p->nCol];
194589 195937             if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
194590 195938             p->apValue[i+p->nCol] = 0;
194591 195939           }
194592 195940         }
       195941  +    }else if( p->bInvert ){
       195942  +      if( p->op==SQLITE_INSERT ) p->op = SQLITE_DELETE;
       195943  +      else if( p->op==SQLITE_DELETE ) p->op = SQLITE_INSERT;
194593 195944       }
194594 195945     }
194595 195946   
194596 195947     return SQLITE_ROW;
194597 195948   }
194598 195949   
194599 195950   /*
................................................................................
194902 196253   
194903 196254         default:
194904 196255           rc = SQLITE_CORRUPT_BKPT;
194905 196256           goto finished_invert;
194906 196257       }
194907 196258   
194908 196259       assert( rc==SQLITE_OK );
194909         -    if( xOutput && sOut.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
       196260  +    if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){
194910 196261         rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
194911 196262         sOut.nBuf = 0;
194912 196263         if( rc!=SQLITE_OK ) goto finished_invert;
194913 196264       }
194914 196265     }
194915 196266   
194916 196267     assert( rc==SQLITE_OK );
................................................................................
194981 196332     int nCol;                       /* Size of azCol[] and abPK[] arrays */
194982 196333     const char **azCol;             /* Array of column names */
194983 196334     u8 *abPK;                       /* Boolean array - true if column is in PK */
194984 196335     int bStat1;                     /* True if table is sqlite_stat1 */
194985 196336     int bDeferConstraints;          /* True to defer constraints */
194986 196337     SessionBuffer constraints;      /* Deferred constraints are stored here */
194987 196338     SessionBuffer rebase;           /* Rebase information (if any) here */
194988         -  int bRebaseStarted;             /* If table header is already in rebase */
       196339  +  u8 bRebaseStarted;              /* If table header is already in rebase */
       196340  +  u8 bRebase;                     /* True to collect rebase information */
194989 196341   };
194990 196342   
194991 196343   /*
194992 196344   ** Formulate a statement to DELETE a row from database db. Assuming a table
194993 196345   ** structure like this:
194994 196346   **
194995 196347   **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
................................................................................
195378 196730   */
195379 196731   static int sessionRebaseAdd(
195380 196732     SessionApplyCtx *p,             /* Apply context */
195381 196733     int eType,                      /* Conflict resolution (OMIT or REPLACE) */
195382 196734     sqlite3_changeset_iter *pIter   /* Iterator pointing at current change */
195383 196735   ){
195384 196736     int rc = SQLITE_OK;
195385         -  int i;
195386         -  int eOp = pIter->op;
195387         -  if( p->bRebaseStarted==0 ){
195388         -    /* Append a table-header to the rebase buffer */
195389         -    const char *zTab = pIter->zTab;
195390         -    sessionAppendByte(&p->rebase, 'T', &rc);
195391         -    sessionAppendVarint(&p->rebase, p->nCol, &rc);
195392         -    sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
195393         -    sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
195394         -    p->bRebaseStarted = 1;
195395         -  }
195396         -
195397         -  assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
195398         -  assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
195399         -
195400         -  sessionAppendByte(&p->rebase, 
195401         -      (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
195402         -  );
195403         -  sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
195404         -  for(i=0; i<p->nCol; i++){
195405         -    sqlite3_value *pVal = 0;
195406         -    if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
195407         -      sqlite3changeset_old(pIter, i, &pVal);
195408         -    }else{
195409         -      sqlite3changeset_new(pIter, i, &pVal);
195410         -    }
195411         -    sessionAppendValue(&p->rebase, pVal, &rc);
195412         -  }
195413         -
       196737  +  if( p->bRebase ){
       196738  +    int i;
       196739  +    int eOp = pIter->op;
       196740  +    if( p->bRebaseStarted==0 ){
       196741  +      /* Append a table-header to the rebase buffer */
       196742  +      const char *zTab = pIter->zTab;
       196743  +      sessionAppendByte(&p->rebase, 'T', &rc);
       196744  +      sessionAppendVarint(&p->rebase, p->nCol, &rc);
       196745  +      sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
       196746  +      sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
       196747  +      p->bRebaseStarted = 1;
       196748  +    }
       196749  +
       196750  +    assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
       196751  +    assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
       196752  +
       196753  +    sessionAppendByte(&p->rebase, 
       196754  +        (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
       196755  +        );
       196756  +    sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
       196757  +    for(i=0; i<p->nCol; i++){
       196758  +      sqlite3_value *pVal = 0;
       196759  +      if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
       196760  +        sqlite3changeset_old(pIter, i, &pVal);
       196761  +      }else{
       196762  +        sqlite3changeset_new(pIter, i, &pVal);
       196763  +      }
       196764  +      sessionAppendValue(&p->rebase, pVal, &rc);
       196765  +    }
       196766  +  }
195414 196767     return rc;
195415 196768   }
195416 196769   
195417 196770   /*
195418 196771   ** Invoke the conflict handler for the change that the changeset iterator
195419 196772   ** currently points to.
195420 196773   **
................................................................................
195749 197102     int rc = SQLITE_OK;
195750 197103   
195751 197104     while( pApply->constraints.nBuf ){
195752 197105       sqlite3_changeset_iter *pIter2 = 0;
195753 197106       SessionBuffer cons = pApply->constraints;
195754 197107       memset(&pApply->constraints, 0, sizeof(SessionBuffer));
195755 197108   
195756         -    rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf);
       197109  +    rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf, 0);
195757 197110       if( rc==SQLITE_OK ){
195758 197111         int nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
195759 197112         int rc2;
195760 197113         pIter2->bPatchset = bPatchset;
195761 197114         pIter2->zTab = (char*)zTab;
195762 197115         pIter2->nCol = pApply->nCol;
195763 197116         pIter2->abPK = pApply->abPK;
................................................................................
195815 197168     SessionApplyCtx sApply;         /* changeset_apply() context object */
195816 197169     int bPatchset;
195817 197170   
195818 197171     assert( xConflict!=0 );
195819 197172   
195820 197173     pIter->in.bNoDiscard = 1;
195821 197174     memset(&sApply, 0, sizeof(sApply));
       197175  +  sApply.bRebase = (ppRebase && pnRebase);
195822 197176     sqlite3_mutex_enter(sqlite3_db_mutex(db));
195823 197177     if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
195824 197178       rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
195825 197179     }
195826 197180     if( rc==SQLITE_OK ){
195827 197181       rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
195828 197182     }
................................................................................
195965 197319         rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
195966 197320       }else{
195967 197321         sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
195968 197322         sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
195969 197323       }
195970 197324     }
195971 197325   
195972         -  if( rc==SQLITE_OK && bPatchset==0 && ppRebase && pnRebase ){
       197326  +  assert( sApply.bRebase || sApply.rebase.nBuf==0 );
       197327  +  if( rc==SQLITE_OK && bPatchset==0 && sApply.bRebase ){
195973 197328       *ppRebase = (void*)sApply.rebase.aBuf;
195974 197329       *pnRebase = sApply.rebase.nBuf;
195975 197330       sApply.rebase.aBuf = 0;
195976 197331     }
195977 197332     sqlite3_finalize(sApply.pInsert);
195978 197333     sqlite3_finalize(sApply.pDelete);
195979 197334     sqlite3_finalize(sApply.pUpdate);
................................................................................
196003 197358       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
196004 197359     ),
196005 197360     void *pCtx,                     /* First argument passed to xConflict */
196006 197361     void **ppRebase, int *pnRebase,
196007 197362     int flags
196008 197363   ){
196009 197364     sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
196010         -  int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
       197365  +  int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
       197366  +  int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset,bInverse);
196011 197367     if( rc==SQLITE_OK ){
196012 197368       rc = sessionChangesetApply(
196013 197369           db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
196014 197370       );
196015 197371     }
196016 197372     return rc;
196017 197373   }
................................................................................
196060 197416       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
196061 197417     ),
196062 197418     void *pCtx,                     /* First argument passed to xConflict */
196063 197419     void **ppRebase, int *pnRebase,
196064 197420     int flags
196065 197421   ){
196066 197422     sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
196067         -  int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
       197423  +  int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
       197424  +  int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse);
196068 197425     if( rc==SQLITE_OK ){
196069 197426       rc = sessionChangesetApply(
196070 197427           db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
196071 197428       );
196072 197429     }
196073 197430     return rc;
196074 197431   }
................................................................................
196433 197790       sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
196434 197791       for(i=0; i<pTab->nChange; i++){
196435 197792         SessionChange *p;
196436 197793         for(p=pTab->apChange[i]; p; p=p->pNext){
196437 197794           sessionAppendByte(&buf, p->op, &rc);
196438 197795           sessionAppendByte(&buf, p->bIndirect, &rc);
196439 197796           sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
       197797  +        if( rc==SQLITE_OK && xOutput && buf.nBuf>=sessions_strm_chunk_size ){
       197798  +          rc = xOutput(pOut, buf.aBuf, buf.nBuf);
       197799  +          buf.nBuf = 0;
       197800  +        }
196440 197801         }
196441 197802       }
196442         -
196443         -    if( rc==SQLITE_OK && xOutput && buf.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
196444         -      rc = xOutput(pOut, buf.aBuf, buf.nBuf);
196445         -      buf.nBuf = 0;
196446         -    }
196447 197803     }
196448 197804   
196449 197805     if( rc==SQLITE_OK ){
196450 197806       if( xOutput ){
196451 197807         if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
196452 197808       }else{
196453 197809         *ppOut = buf.aBuf;
................................................................................
196830 198186       }
196831 198187   
196832 198188       if( bDone==0 ){
196833 198189         sessionAppendByte(&sOut, pIter->op, &rc);
196834 198190         sessionAppendByte(&sOut, pIter->bIndirect, &rc);
196835 198191         sessionAppendBlob(&sOut, aRec, nRec, &rc);
196836 198192       }
196837         -    if( rc==SQLITE_OK && xOutput && sOut.nBuf>SESSIONS_STRM_CHUNK_SIZE ){
       198193  +    if( rc==SQLITE_OK && xOutput && sOut.nBuf>sessions_strm_chunk_size ){
196838 198194         rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
196839 198195         sOut.nBuf = 0;
196840 198196       }
196841 198197       if( rc ) break;
196842 198198     }
196843 198199   
196844 198200     if( rc!=SQLITE_OK ){
................................................................................
196940 198296   */
196941 198297   SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
196942 198298     if( p ){
196943 198299       sessionDeleteTable(p->grp.pList);
196944 198300       sqlite3_free(p);
196945 198301     }
196946 198302   }
       198303  +
       198304  +/* 
       198305  +** Global configuration
       198306  +*/
       198307  +SQLITE_API int sqlite3session_config(int op, void *pArg){
       198308  +  int rc = SQLITE_OK;
       198309  +  switch( op ){
       198310  +    case SQLITE_SESSION_CONFIG_STRMSIZE: {
       198311  +      int *pInt = (int*)pArg;
       198312  +      if( *pInt>0 ){
       198313  +        sessions_strm_chunk_size = *pInt;
       198314  +      }
       198315  +      *pInt = sessions_strm_chunk_size;
       198316  +      break;
       198317  +    }
       198318  +    default:
       198319  +      rc = SQLITE_MISUSE;
       198320  +      break;
       198321  +  }
       198322  +  return rc;
       198323  +}
196947 198324   
196948 198325   #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
196949 198326   
196950 198327   /************** End of sqlite3session.c **************************************/
196951 198328   /************** Begin file fts5.c ********************************************/
196952 198329   
196953 198330   
................................................................................
197402 198779   **            document such as "I won first place" is tokenized, entries are
197403 198780   **            added to the FTS index for "i", "won", "first", "1st" and
197404 198781   **            "place".
197405 198782   **
197406 198783   **            This way, even if the tokenizer does not provide synonyms
197407 198784   **            when tokenizing query text (it should not - to do would be
197408 198785   **            inefficient), it doesn't matter if the user queries for 
197409         -**            'first + place' or '1st + place', as there are entires in the
       198786  +**            'first + place' or '1st + place', as there are entries in the
197410 198787   **            FTS index corresponding to both forms of the first token.
197411 198788   **   </ol>
197412 198789   **
197413 198790   **   Whether it is parsing document or query text, any call to xToken that
197414 198791   **   specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
197415 198792   **   is considered to supply a synonym for the previous token. For example,
197416 198793   **   when parsing the document "I won first place", a tokenizer that supports
................................................................................
197430 198807   **   There is no limit to the number of synonyms that may be provided for a
197431 198808   **   single token.
197432 198809   **
197433 198810   **   In many cases, method (1) above is the best approach. It does not add 
197434 198811   **   extra data to the FTS index or require FTS5 to query for multiple terms,
197435 198812   **   so it is efficient in terms of disk space and query speed. However, it
197436 198813   **   does not support prefix queries very well. If, as suggested above, the
197437         -**   token "first" is subsituted for "1st" by the tokenizer, then the query:
       198814  +**   token "first" is substituted for "1st" by the tokenizer, then the query:
197438 198815   **
197439 198816   **   <codeblock>
197440 198817   **     ... MATCH '1s*'</codeblock>
197441 198818   **
197442 198819   **   will not match documents that contain the token "1st" (as the tokenizer
197443 198820   **   will probably not map "1s" to any prefix of "first").
197444 198821   **
................................................................................
198375 199752   ** of this template is copied straight through into the generate parser
198376 199753   ** source file.
198377 199754   **
198378 199755   ** The following is the concatenation of all %include directives from the
198379 199756   ** input grammar file:
198380 199757   */
198381 199758   /* #include <stdio.h> */
       199759  +/* #include <assert.h> */
198382 199760   /************ Begin %include sections from the grammar ************************/
198383 199761   
198384 199762   /* #include "fts5Int.h" */
198385 199763   /* #include "fts5parse.h" */
198386 199764   
198387 199765   /*
198388 199766   ** Disable all error recovery processing in the parser push-down
................................................................................
199702 201080                fts5yyTracePrompt,fts5yyTokenName[fts5yymajor]);
199703 201081           }
199704 201082   #endif
199705 201083           fts5yy_destructor(fts5yypParser, (fts5YYCODETYPE)fts5yymajor, &fts5yyminorunion);
199706 201084           fts5yymajor = fts5YYNOCODE;
199707 201085         }else{
199708 201086           while( fts5yypParser->fts5yytos >= fts5yypParser->fts5yystack
199709         -            && fts5yymx != fts5YYERRORSYMBOL
199710 201087               && (fts5yyact = fts5yy_find_reduce_action(
199711 201088                           fts5yypParser->fts5yytos->stateno,
199712         -                        fts5YYERRORSYMBOL)) >= fts5YY_MIN_REDUCE
       201089  +                        fts5YYERRORSYMBOL)) > fts5YY_MAX_SHIFTREDUCE
199713 201090           ){
199714 201091             fts5yy_pop_parser_stack(fts5yypParser);
199715 201092           }
199716 201093           if( fts5yypParser->fts5yytos < fts5yypParser->fts5yystack || fts5yymajor==0 ){
199717 201094             fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
199718 201095             fts5yy_parse_failed(fts5yypParser);
199719 201096   #ifndef fts5YYNOERRORRECOVERY
................................................................................
210655 212032   
210656 212033       if( p->rc ){
210657 212034         sqlite3Fts5IterClose((Fts5IndexIter*)pRet);
210658 212035         pRet = 0;
210659 212036         fts5CloseReader(p);
210660 212037       }
210661 212038   
210662         -    *ppIter = &pRet->base;
       212039  +    *ppIter = (Fts5IndexIter*)pRet;
210663 212040       sqlite3Fts5BufferFree(&buf);
210664 212041     }
210665 212042     return fts5IndexReturn(p);
210666 212043   }
210667 212044   
210668 212045   /*
210669 212046   ** Return true if the iterator passed as the only argument is at EOF.
................................................................................
214404 215781   static void fts5SourceIdFunc(
214405 215782     sqlite3_context *pCtx,          /* Function call context */
214406 215783     int nArg,                       /* Number of args */
214407 215784     sqlite3_value **apUnused        /* Function arguments */
214408 215785   ){
214409 215786     assert( nArg==0 );
214410 215787     UNUSED_PARAM2(nArg, apUnused);
214411         -  sqlite3_result_text(pCtx, "fts5: 2018-09-11 19:05:32 0af18674ca5b34e67e1685be3adcdd99a7b8650589c6a7cde7ad9ef1852a8777", -1, SQLITE_TRANSIENT);
       215788  +  sqlite3_result_text(pCtx, "fts5: 2018-11-27 17:02:38 3c893d9bcc34c391505b08cc35808dd3abddf3c5d952798334c296547bcae6a6", -1, SQLITE_TRANSIENT);
       215789  +}
       215790  +
       215791  +/*
       215792  +** Return true if zName is the extension on one of the shadow tables used
       215793  +** by this module.
       215794  +*/
       215795  +static int fts5ShadowName(const char *zName){
       215796  +  static const char *azName[] = {
       215797  +    "config", "content", "data", "docsize", "idx"
       215798  +  };
       215799  +  unsigned int i;
       215800  +  for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
       215801  +    if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
       215802  +  }
       215803  +  return 0;
214412 215804   }
214413 215805   
214414 215806   static int fts5Init(sqlite3 *db){
214415 215807     static const sqlite3_module fts5Mod = {
214416         -    /* iVersion      */ 2,
       215808  +    /* iVersion      */ 3,
214417 215809       /* xCreate       */ fts5CreateMethod,
214418 215810       /* xConnect      */ fts5ConnectMethod,
214419 215811       /* xBestIndex    */ fts5BestIndexMethod,
214420 215812       /* xDisconnect   */ fts5DisconnectMethod,
214421 215813       /* xDestroy      */ fts5DestroyMethod,
214422 215814       /* xOpen         */ fts5OpenMethod,
214423 215815       /* xClose        */ fts5CloseMethod,
................................................................................
214432 215824       /* xCommit       */ fts5CommitMethod,
214433 215825       /* xRollback     */ fts5RollbackMethod,
214434 215826       /* xFindFunction */ fts5FindFunctionMethod,
214435 215827       /* xRename       */ fts5RenameMethod,
214436 215828       /* xSavepoint    */ fts5SavepointMethod,
214437 215829       /* xRelease      */ fts5ReleaseMethod,
214438 215830       /* xRollbackTo   */ fts5RollbackToMethod,
       215831  +    /* xShadowName   */ fts5ShadowName
214439 215832     };
214440 215833   
214441 215834     int rc;
214442 215835     Fts5Global *pGlobal = 0;
214443 215836   
214444 215837     pGlobal = (Fts5Global*)sqlite3_malloc(sizeof(Fts5Global));
214445 215838     if( pGlobal==0 ){
................................................................................
218476 219869   static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){
218477 219870     int eDetail = pCsr->pConfig->eDetail;
218478 219871     int rc = SQLITE_OK;
218479 219872     Fts5IndexIter *pIter = pCsr->pIter;
218480 219873     i64 *pp = &pCsr->iInstPos;
218481 219874     int *po = &pCsr->iInstOff;
218482 219875     
       219876  +  assert( sqlite3Fts5IterEof(pIter)==0 );
       219877  +  assert( pCsr->bEof==0 );
218483 219878     while( eDetail==FTS5_DETAIL_NONE
218484 219879         || sqlite3Fts5PoslistNext64(pIter->pData, pIter->nData, po, pp) 
218485 219880     ){
218486 219881       pCsr->iInstPos = 0;
218487 219882       pCsr->iInstOff = 0;
218488 219883   
218489 219884       rc = sqlite3Fts5IterNextScan(pCsr->pIter);
218490 219885       if( rc==SQLITE_OK ){
218491 219886         rc = fts5VocabInstanceNewTerm(pCsr);
218492         -      if( eDetail==FTS5_DETAIL_NONE ) break;
       219887  +      if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE ) break;
218493 219888       }
218494 219889       if( rc ){
218495 219890         pCsr->bEof = 1;
218496 219891         break;
218497 219892       }
218498 219893     }
218499 219894   
................................................................................
218800 220195       /* xCommit       */ 0,
218801 220196       /* xRollback     */ 0,
218802 220197       /* xFindFunction */ 0,
218803 220198       /* xRename       */ 0,
218804 220199       /* xSavepoint    */ 0,
218805 220200       /* xRelease      */ 0,
218806 220201       /* xRollbackTo   */ 0,
       220202  +    /* xShadowName   */ 0
218807 220203     };
218808 220204     void *p = (void*)pGlobal;
218809 220205   
218810 220206     return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0);
218811 220207   }
218812         -
218813         -
218814 220208   
218815 220209   
218816 220210       
218817 220211   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
218818 220212   
218819 220213   /************** End of fts5.c ************************************************/
218820 220214   /************** Begin file stmt.c ********************************************/
................................................................................
219082 220476     0,                         /* xCommit */
219083 220477     0,                         /* xRollback */
219084 220478     0,                         /* xFindMethod */
219085 220479     0,                         /* xRename */
219086 220480     0,                         /* xSavepoint */
219087 220481     0,                         /* xRelease */
219088 220482     0,                         /* xRollbackTo */
       220483  +  0,                         /* xShadowName */
219089 220484   };
219090 220485   
219091 220486   #endif /* SQLITE_OMIT_VIRTUALTABLE */
219092 220487   
219093 220488   SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3 *db){
219094 220489     int rc = SQLITE_OK;
219095 220490   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
219114 220509   #endif
219115 220510     return rc;
219116 220511   }
219117 220512   #endif /* SQLITE_CORE */
219118 220513   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
219119 220514   
219120 220515   /************** End of stmt.c ************************************************/
219121         -#if __LINE__!=219121
       220516  +#if __LINE__!=220516
219122 220517   #undef SQLITE_SOURCE_ID
219123         -#define SQLITE_SOURCE_ID      "2018-09-12 08:51:48 572de7e4e33562c72cd90790b267ba389370f21ddcaebc4db609fd76ae9balt2"
       220518  +#define SQLITE_SOURCE_ID      "2018-11-27 19:47:55 0ea049f342d11c676e148239e45d252164081362e921a4beb735d6899eb7alt2"
219124 220519   #endif
219125 220520   /* Return the source-id for this library */
219126 220521   SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
219127 220522   /************************** End of sqlite3.c ******************************/

Changes to src/sqlite3.h.

   119    119   ** been edited in any way since it was last checked in, then the last
   120    120   ** four hexadecimal digits of the hash may be modified.
   121    121   **
   122    122   ** See also: [sqlite3_libversion()],
   123    123   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   124    124   ** [sqlite_version()] and [sqlite_source_id()].
   125    125   */
   126         -#define SQLITE_VERSION        "3.25.0"
   127         -#define SQLITE_VERSION_NUMBER 3025000
   128         -#define SQLITE_SOURCE_ID      "2018-09-12 08:51:48 572de7e4e33562c72cd90790b267ba389370f21ddcaebc4db609fd76ae9b7ada"
          126  +#define SQLITE_VERSION        "3.26.0"
          127  +#define SQLITE_VERSION_NUMBER 3026000
          128  +#define SQLITE_SOURCE_ID      "2018-11-27 19:47:55 0ea049f342d11c676e148239e45d252164081362e921a4beb735d6899eb77344"
   129    129   
   130    130   /*
   131    131   ** CAPI3REF: Run-Time Library Version Numbers
   132    132   ** KEYWORDS: sqlite3_version sqlite3_sourceid
   133    133   **
   134    134   ** These interfaces provide the same information as the [SQLITE_VERSION],
   135    135   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  1078   1078   **
  1079   1079   ** <li>[[SQLITE_FCNTL_DATA_VERSION]]
  1080   1080   ** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
  1081   1081   ** a database file.  The argument is a pointer to a 32-bit unsigned integer.
  1082   1082   ** The "data version" for the pager is written into the pointer.  The
  1083   1083   ** "data version" changes whenever any change occurs to the corresponding
  1084   1084   ** database file, either through SQL statements on the same database
  1085         -** connection, or through transactions committed by separate database
         1085  +** connection or through transactions committed by separate database
  1086   1086   ** connections possibly in other processes. The [sqlite3_total_changes()]
  1087   1087   ** interface can be used to find if any database on the connection has changed,
  1088         -** but that interface response to changes on TEMP as well as MAIN and does
         1088  +** but that interface responds to changes on TEMP as well as MAIN and does
  1089   1089   ** not provide a mechanism to detect changes to MAIN only.  Also, the
  1090         -** [sqlite3_total_changes()] interface response to internal changes only and
         1090  +** [sqlite3_total_changes()] interface responds to internal changes only and
  1091   1091   ** omits changes made by other database connections.  The
  1092   1092   ** [PRAGMA data_version] command provide a mechanism to detect changes to
  1093   1093   ** a single attached database that occur due to other database connections,
  1094         -** but omits changes implemented by the database connection for which it is
         1094  +** but omits changes implemented by the database connection on which it is
  1095   1095   ** called.  This file control is the only mechanism to detect changes that
  1096         -** happen either internally or externally on a single database.
         1096  +** happen either internally or externally and that are associated with
         1097  +** a particular attached database.
  1097   1098   ** </ul>
  1098   1099   */
  1099   1100   #define SQLITE_FCNTL_LOCKSTATE               1
  1100   1101   #define SQLITE_FCNTL_GET_LOCKPROXYFILE       2
  1101   1102   #define SQLITE_FCNTL_SET_LOCKPROXYFILE       3
  1102   1103   #define SQLITE_FCNTL_LAST_ERRNO              4
  1103   1104   #define SQLITE_FCNTL_SIZE_HINT               5
................................................................................
  2012   2013   ** Existing configuration options might be discontinued.  Applications
  2013   2014   ** should check the return code from [sqlite3_db_config()] to make sure that
  2014   2015   ** the call worked.  ^The [sqlite3_db_config()] interface will return a
  2015   2016   ** non-zero [error code] if a discontinued or unsupported configuration option
  2016   2017   ** is invoked.
  2017   2018   **
  2018   2019   ** <dl>
         2020  +** [[SQLITE_DBCONFIG_LOOKASIDE]]
  2019   2021   ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
  2020   2022   ** <dd> ^This option takes three additional arguments that determine the 
  2021   2023   ** [lookaside memory allocator] configuration for the [database connection].
  2022   2024   ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
  2023   2025   ** pointer to a memory buffer to use for lookaside memory.
  2024   2026   ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
  2025   2027   ** may be NULL in which case SQLite will allocate the
................................................................................
  2034   2036   ** connection is not currently using lookaside memory, or in other words
  2035   2037   ** when the "current value" returned by
  2036   2038   ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
  2037   2039   ** Any attempt to change the lookaside memory configuration when lookaside
  2038   2040   ** memory is in use leaves the configuration unchanged and returns 
  2039   2041   ** [SQLITE_BUSY].)^</dd>
  2040   2042   **
         2043  +** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
  2041   2044   ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
  2042   2045   ** <dd> ^This option is used to enable or disable the enforcement of
  2043   2046   ** [foreign key constraints].  There should be two additional arguments.
  2044   2047   ** The first argument is an integer which is 0 to disable FK enforcement,
  2045   2048   ** positive to enable FK enforcement or negative to leave FK enforcement
  2046   2049   ** unchanged.  The second parameter is a pointer to an integer into which
  2047   2050   ** is written 0 or 1 to indicate whether FK enforcement is off or on
  2048   2051   ** following this call.  The second parameter may be a NULL pointer, in
  2049   2052   ** which case the FK enforcement setting is not reported back. </dd>
  2050   2053   **
         2054  +** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
  2051   2055   ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
  2052   2056   ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
  2053   2057   ** There should be two additional arguments.
  2054   2058   ** The first argument is an integer which is 0 to disable triggers,
  2055   2059   ** positive to enable triggers or negative to leave the setting unchanged.
  2056   2060   ** The second parameter is a pointer to an integer into which
  2057   2061   ** is written 0 or 1 to indicate whether triggers are disabled or enabled
  2058   2062   ** following this call.  The second parameter may be a NULL pointer, in
  2059   2063   ** which case the trigger setting is not reported back. </dd>
  2060   2064   **
         2065  +** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
  2061   2066   ** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
  2062   2067   ** <dd> ^This option is used to enable or disable the two-argument
  2063   2068   ** version of the [fts3_tokenizer()] function which is part of the
  2064   2069   ** [FTS3] full-text search engine extension.
  2065   2070   ** There should be two additional arguments.
  2066   2071   ** The first argument is an integer which is 0 to disable fts3_tokenizer() or
  2067   2072   ** positive to enable fts3_tokenizer() or negative to leave the setting
  2068   2073   ** unchanged.
  2069   2074   ** The second parameter is a pointer to an integer into which
  2070   2075   ** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
  2071   2076   ** following this call.  The second parameter may be a NULL pointer, in
  2072   2077   ** which case the new setting is not reported back. </dd>
  2073   2078   **
         2079  +** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
  2074   2080   ** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
  2075   2081   ** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
  2076   2082   ** interface independently of the [load_extension()] SQL function.
  2077   2083   ** The [sqlite3_enable_load_extension()] API enables or disables both the
  2078   2084   ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
  2079   2085   ** There should be two additional arguments.
  2080   2086   ** When the first argument to this interface is 1, then only the C-API is
................................................................................
  2084   2090   ** C-API or the SQL function.
  2085   2091   ** The second parameter is a pointer to an integer into which
  2086   2092   ** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
  2087   2093   ** is disabled or enabled following this call.  The second parameter may
  2088   2094   ** be a NULL pointer, in which case the new setting is not reported back.
  2089   2095   ** </dd>
  2090   2096   **
  2091         -** <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
         2097  +** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
  2092   2098   ** <dd> ^This option is used to change the name of the "main" database
  2093   2099   ** schema.  ^The sole argument is a pointer to a constant UTF8 string
  2094   2100   ** which will become the new schema name in place of "main".  ^SQLite
  2095   2101   ** does not make a copy of the new main schema name string, so the application
  2096   2102   ** must ensure that the argument passed into this DBCONFIG option is unchanged
  2097   2103   ** until after the database connection closes.
  2098   2104   ** </dd>
  2099   2105   **
         2106  +** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]] 
  2100   2107   ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
  2101   2108   ** <dd> Usually, when a database in wal mode is closed or detached from a 
  2102   2109   ** database handle, SQLite checks if this will mean that there are now no 
  2103   2110   ** connections at all to the database. If so, it performs a checkpoint 
  2104   2111   ** operation before closing the connection. This option may be used to
  2105   2112   ** override this behaviour. The first parameter passed to this operation
  2106   2113   ** is an integer - positive to disable checkpoints-on-close, or zero (the
  2107   2114   ** default) to enable them, and negative to leave the setting unchanged.
  2108   2115   ** The second parameter is a pointer to an integer
  2109   2116   ** into which is written 0 or 1 to indicate whether checkpoints-on-close
  2110   2117   ** have been disabled - 0 if they are not disabled, 1 if they are.
  2111   2118   ** </dd>
  2112   2119   **
  2113         -** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
         2120  +** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
  2114   2121   ** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
  2115   2122   ** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
  2116   2123   ** a single SQL query statement will always use the same algorithm regardless
  2117   2124   ** of values of [bound parameters].)^ The QPSG disables some query optimizations
  2118   2125   ** that look at the values of bound parameters, which can make some queries
  2119   2126   ** slower.  But the QPSG has the advantage of more predictable behavior.  With
  2120   2127   ** the QPSG active, SQLite will always use the same query plan in the field as
................................................................................
  2122   2129   ** The first argument to this setting is an integer which is 0 to disable 
  2123   2130   ** the QPSG, positive to enable QPSG, or negative to leave the setting
  2124   2131   ** unchanged. The second parameter is a pointer to an integer into which
  2125   2132   ** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
  2126   2133   ** following this call.
  2127   2134   ** </dd>
  2128   2135   **
  2129         -** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
         2136  +** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
  2130   2137   ** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
  2131   2138   ** include output for any operations performed by trigger programs. This
  2132   2139   ** option is used to set or clear (the default) a flag that governs this
  2133   2140   ** behavior. The first parameter passed to this operation is an integer -
  2134   2141   ** positive to enable output for trigger programs, or zero to disable it,
  2135   2142   ** or negative to leave the setting unchanged.
  2136   2143   ** The second parameter is a pointer to an integer into which is written 
  2137   2144   ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
  2138   2145   ** it is not disabled, 1 if it is.  
  2139   2146   ** </dd>
  2140   2147   **
  2141         -** <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
         2148  +** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
  2142   2149   ** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
  2143   2150   ** [VACUUM] in order to reset a database back to an empty database
  2144   2151   ** with no schema and no content. The following process works even for
  2145   2152   ** a badly corrupted database file:
  2146   2153   ** <ol>
  2147   2154   ** <li> If the database connection is newly opened, make sure it has read the
  2148   2155   **      database schema by preparing then discarding some query against the
................................................................................
  2153   2160   ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
  2154   2161   ** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
  2155   2162   ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
  2156   2163   ** </ol>
  2157   2164   ** Because resetting a database is destructive and irreversible, the
  2158   2165   ** process requires the use of this obscure API and multiple steps to help
  2159   2166   ** ensure that it does not happen by accident.
         2167  +**
         2168  +** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
         2169  +** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
         2170  +** "defensive" flag for a database connection.  When the defensive
         2171  +** flag is enabled, language features that allow ordinary SQL to 
         2172  +** deliberately corrupt the database file are disabled.  The disabled
         2173  +** features include but are not limited to the following:
         2174  +** <ul>
         2175  +** <li> The [PRAGMA writable_schema=ON] statement.
         2176  +** <li> Writes to the [sqlite_dbpage] virtual table.
         2177  +** <li> Direct writes to [shadow tables].
         2178  +** </ul>
  2160   2179   ** </dd>
  2161   2180   ** </dl>
  2162   2181   */
  2163   2182   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
  2164   2183   #define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
  2165   2184   #define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
  2166   2185   #define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
  2167   2186   #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
  2168   2187   #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
  2169   2188   #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
  2170   2189   #define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
  2171   2190   #define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
  2172   2191   #define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
  2173         -#define SQLITE_DBCONFIG_MAX                   1009 /* Largest DBCONFIG */
         2192  +#define SQLITE_DBCONFIG_DEFENSIVE             1010 /* int int* */
         2193  +#define SQLITE_DBCONFIG_MAX                   1010 /* Largest DBCONFIG */
  2174   2194   
  2175   2195   /*
  2176   2196   ** CAPI3REF: Enable Or Disable Extended Result Codes
  2177   2197   ** METHOD: sqlite3
  2178   2198   **
  2179   2199   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  2180   2200   ** [extended result codes] feature of SQLite. ^The extended result
................................................................................
  3604   3624   ** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
  3605   3625   ** and [sqlite3_prepare16_v3()] assume that the prepared statement will 
  3606   3626   ** be used just once or at most a few times and then destroyed using
  3607   3627   ** [sqlite3_finalize()] relatively soon. The current implementation acts
  3608   3628   ** on this hint by avoiding the use of [lookaside memory] so as not to
  3609   3629   ** deplete the limited store of lookaside memory. Future versions of
  3610   3630   ** SQLite may act on this hint differently.
         3631  +**
         3632  +** [[SQLITE_PREPARE_NORMALIZE]] ^(<dt>SQLITE_PREPARE_NORMALIZE</dt>
         3633  +** <dd>The SQLITE_PREPARE_NORMALIZE flag indicates that a normalized
         3634  +** representation of the SQL statement should be calculated and then
         3635  +** associated with the prepared statement, which can be obtained via
         3636  +** the [sqlite3_normalized_sql()] interface.  The semantics used to
         3637  +** normalize a SQL statement are unspecified and subject to change.
         3638  +** At a minimum, literal values will be replaced with suitable
         3639  +** placeholders.
  3611   3640   ** </dl>
  3612   3641   */
  3613   3642   #define SQLITE_PREPARE_PERSISTENT              0x01
         3643  +#define SQLITE_PREPARE_NORMALIZE               0x02
  3614   3644   
  3615   3645   /*
  3616   3646   ** CAPI3REF: Compiling An SQL Statement
  3617   3647   ** KEYWORDS: {SQL statement compiler}
  3618   3648   ** METHOD: sqlite3
  3619   3649   ** CONSTRUCTOR: sqlite3_stmt
  3620   3650   **
................................................................................
  3764   3794   ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
  3765   3795   ** SQL text used to create [prepared statement] P if P was
  3766   3796   ** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
  3767   3797   ** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
  3768   3798   ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
  3769   3799   ** string containing the SQL text of prepared statement P with
  3770   3800   ** [bound parameters] expanded.
         3801  +** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
         3802  +** string containing the normalized SQL text of prepared statement P.  The
         3803  +** semantics used to normalize a SQL statement are unspecified and subject
         3804  +** to change.  At a minimum, literal values will be replaced with suitable
         3805  +** placeholders.
  3771   3806   **
  3772   3807   ** ^(For example, if a prepared statement is created using the SQL
  3773   3808   ** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
  3774   3809   ** and parameter :xyz is unbound, then sqlite3_sql() will return
  3775   3810   ** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
  3776   3811   ** will return "SELECT 2345,NULL".)^
  3777   3812   **
................................................................................
  3779   3814   ** is available to hold the result, or if the result would exceed the
  3780   3815   ** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
  3781   3816   **
  3782   3817   ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
  3783   3818   ** bound parameter expansions.  ^The [SQLITE_OMIT_TRACE] compile-time
  3784   3819   ** option causes sqlite3_expanded_sql() to always return NULL.
  3785   3820   **
  3786         -** ^The string returned by sqlite3_sql(P) is managed by SQLite and is
  3787         -** automatically freed when the prepared statement is finalized.
         3821  +** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
         3822  +** are managed by SQLite and are automatically freed when the prepared
         3823  +** statement is finalized.
  3788   3824   ** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
  3789   3825   ** is obtained from [sqlite3_malloc()] and must be free by the application
  3790   3826   ** by passing it to [sqlite3_free()].
  3791   3827   */
  3792   3828   SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
  3793   3829   SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
         3830  +SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
  3794   3831   
  3795   3832   /*
  3796   3833   ** CAPI3REF: Determine If An SQL Statement Writes The Database
  3797   3834   ** METHOD: sqlite3_stmt
  3798   3835   **
  3799   3836   ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  3800   3837   ** and only if the [prepared statement] X makes no direct changes to
................................................................................
  4727   4764   ** parameters. ^An aggregate SQL function requires an implementation of xStep
  4728   4765   ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
  4729   4766   ** SQL function or aggregate, pass NULL pointers for all three function
  4730   4767   ** callbacks.
  4731   4768   **
  4732   4769   ** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue 
  4733   4770   ** and xInverse) passed to sqlite3_create_window_function are pointers to
  4734         -** C-lanugage callbacks that implement the new function. xStep and xFinal
         4771  +** C-language callbacks that implement the new function. xStep and xFinal
  4735   4772   ** must both be non-NULL. xValue and xInverse may either both be NULL, in
  4736   4773   ** which case a regular aggregate function is created, or must both be 
  4737   4774   ** non-NULL, in which case the new function may be used as either an aggregate
  4738   4775   ** or aggregate window function. More details regarding the implementation
  4739   4776   ** of aggregate window functions are 
  4740   4777   ** [user-defined window functions|available here].
  4741   4778   **
................................................................................
  6276   6313                          void **ppArg);
  6277   6314     int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
  6278   6315     /* The methods above are in version 1 of the sqlite_module object. Those 
  6279   6316     ** below are for version 2 and greater. */
  6280   6317     int (*xSavepoint)(sqlite3_vtab *pVTab, int);
  6281   6318     int (*xRelease)(sqlite3_vtab *pVTab, int);
  6282   6319     int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
         6320  +  /* The methods above are in versions 1 and 2 of the sqlite_module object.
         6321  +  ** Those below are for version 3 and greater. */
         6322  +  int (*xShadowName)(const char*);
  6283   6323   };
  6284   6324   
  6285   6325   /*
  6286   6326   ** CAPI3REF: Virtual Table Indexing Information
  6287   6327   ** KEYWORDS: sqlite3_index_info
  6288   6328   **
  6289   6329   ** The sqlite3_index_info structure and its substructures is used as part
................................................................................
  7198   7238   #define SQLITE_TESTCTRL_PENDING_BYTE            11
  7199   7239   #define SQLITE_TESTCTRL_ASSERT                  12
  7200   7240   #define SQLITE_TESTCTRL_ALWAYS                  13
  7201   7241   #define SQLITE_TESTCTRL_RESERVE                 14
  7202   7242   #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  7203   7243   #define SQLITE_TESTCTRL_ISKEYWORD               16  /* NOT USED */
  7204   7244   #define SQLITE_TESTCTRL_SCRATCHMALLOC           17  /* NOT USED */
         7245  +#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS      17
  7205   7246   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  7206   7247   #define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
  7207   7248   #define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
  7208   7249   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  7209   7250   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  7210   7251   #define SQLITE_TESTCTRL_BYTEORDER               22
  7211   7252   #define SQLITE_TESTCTRL_ISINIT                  23
................................................................................
  8610   8651   ** CAPI3REF: Virtual Table Configuration Options
  8611   8652   **
  8612   8653   ** These macros define the various options to the
  8613   8654   ** [sqlite3_vtab_config()] interface that [virtual table] implementations
  8614   8655   ** can use to customize and optimize their behavior.
  8615   8656   **
  8616   8657   ** <dl>
         8658  +** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
  8617   8659   ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
  8618   8660   ** <dd>Calls of the form
  8619   8661   ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
  8620   8662   ** where X is an integer.  If X is zero, then the [virtual table] whose
  8621   8663   ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
  8622   8664   ** support constraints.  In this configuration (which is the default) if
  8623   8665   ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
................................................................................
  9253   9295   ** The following are allowed values for 6th argument (the F argument) to
  9254   9296   ** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
  9255   9297   **
  9256   9298   ** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
  9257   9299   ** in the P argument is held in memory obtained from [sqlite3_malloc64()]
  9258   9300   ** and that SQLite should take ownership of this memory and automatically
  9259   9301   ** free it when it has finished using it.  Without this flag, the caller
  9260         -** is resposible for freeing any dynamically allocated memory.
         9302  +** is responsible for freeing any dynamically allocated memory.
  9261   9303   **
  9262   9304   ** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
  9263   9305   ** grow the size of the database using calls to [sqlite3_realloc64()].  This
  9264   9306   ** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
  9265   9307   ** Without this flag, the deserialized database cannot increase in size beyond
  9266   9308   ** the number of bytes specified by the M parameter.
  9267   9309   **
................................................................................
  9875   9917   ** [sqlite3changeset_invert()] functions, all changes within the changeset 
  9876   9918   ** that apply to a single table are grouped together. This means that when 
  9877   9919   ** an application iterates through a changeset using an iterator created by 
  9878   9920   ** this function, all changes that relate to a single table are visited 
  9879   9921   ** consecutively. There is no chance that the iterator will visit a change 
  9880   9922   ** the applies to table X, then one for table Y, and then later on visit 
  9881   9923   ** another change for table X.
         9924  +**
         9925  +** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
         9926  +** may be modified by passing a combination of
         9927  +** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
         9928  +**
         9929  +** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
         9930  +** and therefore subject to change.
  9882   9931   */
  9883   9932   SQLITE_API int sqlite3changeset_start(
  9884   9933     sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
  9885   9934     int nChangeset,                 /* Size of changeset blob in bytes */
  9886   9935     void *pChangeset                /* Pointer to blob containing changeset */
  9887   9936   );
         9937  +SQLITE_API int sqlite3changeset_start_v2(
         9938  +  sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
         9939  +  int nChangeset,                 /* Size of changeset blob in bytes */
         9940  +  void *pChangeset,               /* Pointer to blob containing changeset */
         9941  +  int flags                       /* SESSION_CHANGESETSTART_* flags */
         9942  +);
         9943  +
         9944  +/*
         9945  +** CAPI3REF: Flags for sqlite3changeset_start_v2
         9946  +**
         9947  +** The following flags may passed via the 4th parameter to
         9948  +** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
         9949  +**
         9950  +** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
         9951  +**   Invert the changeset while iterating through it. This is equivalent to
         9952  +**   inverting a changeset using sqlite3changeset_invert() before applying it.
         9953  +**   It is an error to specify this flag with a patchset.
         9954  +*/
         9955  +#define SQLITE_CHANGESETSTART_INVERT        0x0002
  9888   9956   
  9889   9957   
  9890   9958   /*
  9891   9959   ** CAPI3REF: Advance A Changeset Iterator
  9892   9960   ** METHOD: sqlite3_changeset_iter
  9893   9961   **
  9894   9962   ** This function may only be used with iterators created by function
................................................................................
 10535  10603     int(*xConflict)(
 10536  10604       void *pCtx,                   /* Copy of sixth arg to _apply() */
 10537  10605       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 10538  10606       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 10539  10607     ),
 10540  10608     void *pCtx,                     /* First argument passed to xConflict */
 10541  10609     void **ppRebase, int *pnRebase, /* OUT: Rebase data */
 10542         -  int flags                       /* Combination of SESSION_APPLY_* flags */
        10610  +  int flags                       /* SESSION_CHANGESETAPPLY_* flags */
 10543  10611   );
 10544  10612   
 10545  10613   /*
 10546  10614   ** CAPI3REF: Flags for sqlite3changeset_apply_v2
 10547  10615   **
 10548  10616   ** The following flags may passed via the 9th parameter to
 10549  10617   ** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
................................................................................
 10553  10621   **   Usually, the sessions module encloses all operations performed by
 10554  10622   **   a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
 10555  10623   **   SAVEPOINT is committed if the changeset or patchset is successfully
 10556  10624   **   applied, or rolled back if an error occurs. Specifying this flag
 10557  10625   **   causes the sessions module to omit this savepoint. In this case, if the
 10558  10626   **   caller has an open transaction or savepoint when apply_v2() is called, 
 10559  10627   **   it may revert the partially applied changeset by rolling it back.
        10628  +**
        10629  +** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
        10630  +**   Invert the changeset before applying it. This is equivalent to inverting
        10631  +**   a changeset using sqlite3changeset_invert() before applying it. It is
        10632  +**   an error to specify this flag with a patchset.
 10560  10633   */
 10561  10634   #define SQLITE_CHANGESETAPPLY_NOSAVEPOINT   0x0001
        10635  +#define SQLITE_CHANGESETAPPLY_INVERT        0x0002
 10562  10636   
 10563  10637   /* 
 10564  10638   ** CAPI3REF: Constants Passed To The Conflict Handler
 10565  10639   **
 10566  10640   ** Values that may be passed as the second argument to a conflict-handler.
 10567  10641   **
 10568  10642   ** <dl>
................................................................................
 10947  11021     int (*xOutput)(void *pOut, const void *pData, int nData),
 10948  11022     void *pOut
 10949  11023   );
 10950  11024   SQLITE_API int sqlite3changeset_start_strm(
 10951  11025     sqlite3_changeset_iter **pp,
 10952  11026     int (*xInput)(void *pIn, void *pData, int *pnData),
 10953  11027     void *pIn
        11028  +);
        11029  +SQLITE_API int sqlite3changeset_start_v2_strm(
        11030  +  sqlite3_changeset_iter **pp,
        11031  +  int (*xInput)(void *pIn, void *pData, int *pnData),
        11032  +  void *pIn,
        11033  +  int flags
 10954  11034   );
 10955  11035   SQLITE_API int sqlite3session_changeset_strm(
 10956  11036     sqlite3_session *pSession,
 10957  11037     int (*xOutput)(void *pOut, const void *pData, int nData),
 10958  11038     void *pOut
 10959  11039   );
 10960  11040   SQLITE_API int sqlite3session_patchset_strm(
................................................................................
 10974  11054     sqlite3_rebaser *pRebaser,
 10975  11055     int (*xInput)(void *pIn, void *pData, int *pnData),
 10976  11056     void *pIn,
 10977  11057     int (*xOutput)(void *pOut, const void *pData, int nData),
 10978  11058     void *pOut
 10979  11059   );
 10980  11060   
        11061  +/*
        11062  +** CAPI3REF: Configure global parameters
        11063  +**
        11064  +** The sqlite3session_config() interface is used to make global configuration
        11065  +** changes to the sessions module in order to tune it to the specific needs 
        11066  +** of the application.
        11067  +**
        11068  +** The sqlite3session_config() interface is not threadsafe. If it is invoked
        11069  +** while any other thread is inside any other sessions method then the
        11070  +** results are undefined. Furthermore, if it is invoked after any sessions
        11071  +** related objects have been created, the results are also undefined. 
        11072  +**
        11073  +** The first argument to the sqlite3session_config() function must be one
        11074  +** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The 
        11075  +** interpretation of the (void*) value passed as the second parameter and
        11076  +** the effect of calling this function depends on the value of the first
        11077  +** parameter.
        11078  +**
        11079  +** <dl>
        11080  +** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
        11081  +**    By default, the sessions module streaming interfaces attempt to input
        11082  +**    and output data in approximately 1 KiB chunks. This operand may be used
        11083  +**    to set and query the value of this configuration setting. The pointer
        11084  +**    passed as the second argument must point to a value of type (int).
        11085  +**    If this value is greater than 0, it is used as the new streaming data
        11086  +**    chunk size for both input and output. Before returning, the (int) value
        11087  +**    pointed to by pArg is set to the final value of the streaming interface
        11088  +**    chunk size.
        11089  +** </dl>
        11090  +**
        11091  +** This function returns SQLITE_OK if successful, or an SQLite error code
        11092  +** otherwise.
        11093  +*/
        11094  +SQLITE_API int sqlite3session_config(int op, void *pArg);
        11095  +
        11096  +/*
        11097  +** CAPI3REF: Values for sqlite3session_config().
        11098  +*/
        11099  +#define SQLITE_SESSION_CONFIG_STRMSIZE 1
 10981  11100   
 10982  11101   /*
 10983  11102   ** Make sure we can call this stuff from C++.
 10984  11103   */
 10985  11104   #ifdef __cplusplus
 10986  11105   }
 10987  11106   #endif
................................................................................
 11431  11550   **            document such as "I won first place" is tokenized, entries are
 11432  11551   **            added to the FTS index for "i", "won", "first", "1st" and
 11433  11552   **            "place".
 11434  11553   **
 11435  11554   **            This way, even if the tokenizer does not provide synonyms
 11436  11555   **            when tokenizing query text (it should not - to do would be
 11437  11556   **            inefficient), it doesn't matter if the user queries for 
 11438         -**            'first + place' or '1st + place', as there are entires in the
        11557  +**            'first + place' or '1st + place', as there are entries in the
 11439  11558   **            FTS index corresponding to both forms of the first token.
 11440  11559   **   </ol>
 11441  11560   **
 11442  11561   **   Whether it is parsing document or query text, any call to xToken that
 11443  11562   **   specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
 11444  11563   **   is considered to supply a synonym for the previous token. For example,
 11445  11564   **   when parsing the document "I won first place", a tokenizer that supports
................................................................................
 11459  11578   **   There is no limit to the number of synonyms that may be provided for a
 11460  11579   **   single token.
 11461  11580   **
 11462  11581   **   In many cases, method (1) above is the best approach. It does not add 
 11463  11582   **   extra data to the FTS index or require FTS5 to query for multiple terms,
 11464  11583   **   so it is efficient in terms of disk space and query speed. However, it
 11465  11584   **   does not support prefix queries very well. If, as suggested above, the
 11466         -**   token "first" is subsituted for "1st" by the tokenizer, then the query:
        11585  +**   token "first" is substituted for "1st" by the tokenizer, then the query:
 11467  11586   **
 11468  11587   **   <codeblock>
 11469  11588   **     ... MATCH '1s*'</codeblock>
 11470  11589   **
 11471  11590   **   will not match documents that contain the token "1st" (as the tokenizer
 11472  11591   **   will probably not map "1s" to any prefix of "first").
 11473  11592   **