SQLite Android Bindings
Check-in [41612ba03b]
Not logged in

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

Overview
Comment:Update this project to versino 3.26.0.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 41612ba03b3c045f9910ef4716e703c11ead0152
User & Date: dan 2018-12-01 13:44:42
Context
2019-01-22
17:37
Change the STL used by this project from "stlport_static" to "c++_static". stlport_static is no longer supported. check-in: 07738ecfc7 user: dan tags: trunk
2018-12-01
13:44
Update this project to versino 3.26.0. check-in: 41612ba03b user: dan tags: trunk
2018-11-05
20:54
Update this project to SQLite version 3.25.3. check-in: 317855cf62 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to sqlite3/src/main/jni/sqlite/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.25.3.  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.3"
  1160         -#define SQLITE_VERSION_NUMBER 3025003
  1161         -#define SQLITE_SOURCE_ID      "2018-11-05 20:37:38 89e099fbe5e13c33e683bef07361231ca525b88f7907be7092058007b75036f2"
         1165  +#define SQLITE_VERSION        "3.26.0"
         1166  +#define SQLITE_VERSION_NUMBER 3026000
         1167  +#define SQLITE_SOURCE_ID      "2018-12-01 12:34:55 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238b4f9"
  1162   1168   
  1163   1169   /*
  1164   1170   ** CAPI3REF: Run-Time Library Version Numbers
  1165   1171   ** KEYWORDS: sqlite3_version sqlite3_sourceid
  1166   1172   **
  1167   1173   ** These interfaces provide the same information as the [SQLITE_VERSION],
  1168   1174   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  3046   3052   ** Existing configuration options might be discontinued.  Applications
  3047   3053   ** should check the return code from [sqlite3_db_config()] to make sure that
  3048   3054   ** the call worked.  ^The [sqlite3_db_config()] interface will return a
  3049   3055   ** non-zero [error code] if a discontinued or unsupported configuration option
  3050   3056   ** is invoked.
  3051   3057   **
  3052   3058   ** <dl>
         3059  +** [[SQLITE_DBCONFIG_LOOKASIDE]]
  3053   3060   ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
  3054   3061   ** <dd> ^This option takes three additional arguments that determine the 
  3055   3062   ** [lookaside memory allocator] configuration for the [database connection].
  3056   3063   ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
  3057   3064   ** pointer to a memory buffer to use for lookaside memory.
  3058   3065   ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
  3059   3066   ** may be NULL in which case SQLite will allocate the
................................................................................
  3068   3075   ** connection is not currently using lookaside memory, or in other words
  3069   3076   ** when the "current value" returned by
  3070   3077   ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
  3071   3078   ** Any attempt to change the lookaside memory configuration when lookaside
  3072   3079   ** memory is in use leaves the configuration unchanged and returns 
  3073   3080   ** [SQLITE_BUSY].)^</dd>
  3074   3081   **
         3082  +** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
  3075   3083   ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
  3076   3084   ** <dd> ^This option is used to enable or disable the enforcement of
  3077   3085   ** [foreign key constraints].  There should be two additional arguments.
  3078   3086   ** The first argument is an integer which is 0 to disable FK enforcement,
  3079   3087   ** positive to enable FK enforcement or negative to leave FK enforcement
  3080   3088   ** unchanged.  The second parameter is a pointer to an integer into which
  3081   3089   ** is written 0 or 1 to indicate whether FK enforcement is off or on
  3082   3090   ** following this call.  The second parameter may be a NULL pointer, in
  3083   3091   ** which case the FK enforcement setting is not reported back. </dd>
  3084   3092   **
         3093  +** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
  3085   3094   ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
  3086   3095   ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
  3087   3096   ** There should be two additional arguments.
  3088   3097   ** The first argument is an integer which is 0 to disable triggers,
  3089   3098   ** positive to enable triggers or negative to leave the setting unchanged.
  3090   3099   ** The second parameter is a pointer to an integer into which
  3091   3100   ** is written 0 or 1 to indicate whether triggers are disabled or enabled
  3092   3101   ** following this call.  The second parameter may be a NULL pointer, in
  3093   3102   ** which case the trigger setting is not reported back. </dd>
  3094   3103   **
         3104  +** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
  3095   3105   ** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
  3096   3106   ** <dd> ^This option is used to enable or disable the two-argument
  3097   3107   ** version of the [fts3_tokenizer()] function which is part of the
  3098   3108   ** [FTS3] full-text search engine extension.
  3099   3109   ** There should be two additional arguments.
  3100   3110   ** The first argument is an integer which is 0 to disable fts3_tokenizer() or
  3101   3111   ** positive to enable fts3_tokenizer() or negative to leave the setting
  3102   3112   ** unchanged.
  3103   3113   ** The second parameter is a pointer to an integer into which
  3104   3114   ** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
  3105   3115   ** following this call.  The second parameter may be a NULL pointer, in
  3106   3116   ** which case the new setting is not reported back. </dd>
  3107   3117   **
         3118  +** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
  3108   3119   ** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
  3109   3120   ** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
  3110   3121   ** interface independently of the [load_extension()] SQL function.
  3111   3122   ** The [sqlite3_enable_load_extension()] API enables or disables both the
  3112   3123   ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
  3113   3124   ** There should be two additional arguments.
  3114   3125   ** When the first argument to this interface is 1, then only the C-API is
................................................................................
  3118   3129   ** C-API or the SQL function.
  3119   3130   ** The second parameter is a pointer to an integer into which
  3120   3131   ** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
  3121   3132   ** is disabled or enabled following this call.  The second parameter may
  3122   3133   ** be a NULL pointer, in which case the new setting is not reported back.
  3123   3134   ** </dd>
  3124   3135   **
  3125         -** <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
         3136  +** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
  3126   3137   ** <dd> ^This option is used to change the name of the "main" database
  3127   3138   ** schema.  ^The sole argument is a pointer to a constant UTF8 string
  3128   3139   ** which will become the new schema name in place of "main".  ^SQLite
  3129   3140   ** does not make a copy of the new main schema name string, so the application
  3130   3141   ** must ensure that the argument passed into this DBCONFIG option is unchanged
  3131   3142   ** until after the database connection closes.
  3132   3143   ** </dd>
  3133   3144   **
         3145  +** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]] 
  3134   3146   ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
  3135   3147   ** <dd> Usually, when a database in wal mode is closed or detached from a 
  3136   3148   ** database handle, SQLite checks if this will mean that there are now no 
  3137   3149   ** connections at all to the database. If so, it performs a checkpoint 
  3138   3150   ** operation before closing the connection. This option may be used to
  3139   3151   ** override this behaviour. The first parameter passed to this operation
  3140   3152   ** is an integer - positive to disable checkpoints-on-close, or zero (the
  3141   3153   ** default) to enable them, and negative to leave the setting unchanged.
  3142   3154   ** The second parameter is a pointer to an integer
  3143   3155   ** into which is written 0 or 1 to indicate whether checkpoints-on-close
  3144   3156   ** have been disabled - 0 if they are not disabled, 1 if they are.
  3145   3157   ** </dd>
  3146   3158   **
  3147         -** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
         3159  +** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
  3148   3160   ** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
  3149   3161   ** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
  3150   3162   ** a single SQL query statement will always use the same algorithm regardless
  3151   3163   ** of values of [bound parameters].)^ The QPSG disables some query optimizations
  3152   3164   ** that look at the values of bound parameters, which can make some queries
  3153   3165   ** slower.  But the QPSG has the advantage of more predictable behavior.  With
  3154   3166   ** the QPSG active, SQLite will always use the same query plan in the field as
................................................................................
  3156   3168   ** The first argument to this setting is an integer which is 0 to disable 
  3157   3169   ** the QPSG, positive to enable QPSG, or negative to leave the setting
  3158   3170   ** unchanged. The second parameter is a pointer to an integer into which
  3159   3171   ** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
  3160   3172   ** following this call.
  3161   3173   ** </dd>
  3162   3174   **
  3163         -** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
         3175  +** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
  3164   3176   ** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
  3165   3177   ** include output for any operations performed by trigger programs. This
  3166   3178   ** option is used to set or clear (the default) a flag that governs this
  3167   3179   ** behavior. The first parameter passed to this operation is an integer -
  3168   3180   ** positive to enable output for trigger programs, or zero to disable it,
  3169   3181   ** or negative to leave the setting unchanged.
  3170   3182   ** The second parameter is a pointer to an integer into which is written 
  3171   3183   ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
  3172   3184   ** it is not disabled, 1 if it is.  
  3173   3185   ** </dd>
  3174   3186   **
  3175         -** <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
         3187  +** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
  3176   3188   ** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
  3177   3189   ** [VACUUM] in order to reset a database back to an empty database
  3178   3190   ** with no schema and no content. The following process works even for
  3179   3191   ** a badly corrupted database file:
  3180   3192   ** <ol>
  3181   3193   ** <li> If the database connection is newly opened, make sure it has read the
  3182   3194   **      database schema by preparing then discarding some query against the
................................................................................
  3187   3199   ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
  3188   3200   ** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
  3189   3201   ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
  3190   3202   ** </ol>
  3191   3203   ** Because resetting a database is destructive and irreversible, the
  3192   3204   ** process requires the use of this obscure API and multiple steps to help
  3193   3205   ** ensure that it does not happen by accident.
         3206  +**
         3207  +** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
         3208  +** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
         3209  +** "defensive" flag for a database connection.  When the defensive
         3210  +** flag is enabled, language features that allow ordinary SQL to 
         3211  +** deliberately corrupt the database file are disabled.  The disabled
         3212  +** features include but are not limited to the following:
         3213  +** <ul>
         3214  +** <li> The [PRAGMA writable_schema=ON] statement.
         3215  +** <li> Writes to the [sqlite_dbpage] virtual table.
         3216  +** <li> Direct writes to [shadow tables].
         3217  +** </ul>
  3194   3218   ** </dd>
  3195   3219   ** </dl>
  3196   3220   */
  3197   3221   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
  3198   3222   #define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
  3199   3223   #define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
  3200   3224   #define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
  3201   3225   #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
  3202   3226   #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
  3203   3227   #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
  3204   3228   #define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
  3205   3229   #define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
  3206   3230   #define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
  3207         -#define SQLITE_DBCONFIG_MAX                   1009 /* Largest DBCONFIG */
         3231  +#define SQLITE_DBCONFIG_DEFENSIVE             1010 /* int int* */
         3232  +#define SQLITE_DBCONFIG_MAX                   1010 /* Largest DBCONFIG */
  3208   3233   
  3209   3234   /*
  3210   3235   ** CAPI3REF: Enable Or Disable Extended Result Codes
  3211   3236   ** METHOD: sqlite3
  3212   3237   **
  3213   3238   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  3214   3239   ** [extended result codes] feature of SQLite. ^The extended result
................................................................................
  4638   4663   ** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
  4639   4664   ** and [sqlite3_prepare16_v3()] assume that the prepared statement will 
  4640   4665   ** be used just once or at most a few times and then destroyed using
  4641   4666   ** [sqlite3_finalize()] relatively soon. The current implementation acts
  4642   4667   ** on this hint by avoiding the use of [lookaside memory] so as not to
  4643   4668   ** deplete the limited store of lookaside memory. Future versions of
  4644   4669   ** SQLite may act on this hint differently.
         4670  +**
         4671  +** [[SQLITE_PREPARE_NORMALIZE]] ^(<dt>SQLITE_PREPARE_NORMALIZE</dt>
         4672  +** <dd>The SQLITE_PREPARE_NORMALIZE flag indicates that a normalized
         4673  +** representation of the SQL statement should be calculated and then
         4674  +** associated with the prepared statement, which can be obtained via
         4675  +** the [sqlite3_normalized_sql()] interface.)^  The semantics used to
         4676  +** normalize a SQL statement are unspecified and subject to change.
         4677  +** At a minimum, literal values will be replaced with suitable
         4678  +** placeholders.
  4645   4679   ** </dl>
  4646   4680   */
  4647   4681   #define SQLITE_PREPARE_PERSISTENT              0x01
         4682  +#define SQLITE_PREPARE_NORMALIZE               0x02
  4648   4683   
  4649   4684   /*
  4650   4685   ** CAPI3REF: Compiling An SQL Statement
  4651   4686   ** KEYWORDS: {SQL statement compiler}
  4652   4687   ** METHOD: sqlite3
  4653   4688   ** CONSTRUCTOR: sqlite3_stmt
  4654   4689   **
................................................................................
  4798   4833   ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
  4799   4834   ** SQL text used to create [prepared statement] P if P was
  4800   4835   ** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
  4801   4836   ** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
  4802   4837   ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
  4803   4838   ** string containing the SQL text of prepared statement P with
  4804   4839   ** [bound parameters] expanded.
         4840  +** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
         4841  +** string containing the normalized SQL text of prepared statement P.  The
         4842  +** semantics used to normalize a SQL statement are unspecified and subject
         4843  +** to change.  At a minimum, literal values will be replaced with suitable
         4844  +** placeholders.
  4805   4845   **
  4806   4846   ** ^(For example, if a prepared statement is created using the SQL
  4807   4847   ** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
  4808   4848   ** and parameter :xyz is unbound, then sqlite3_sql() will return
  4809   4849   ** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
  4810   4850   ** will return "SELECT 2345,NULL".)^
  4811   4851   **
................................................................................
  4813   4853   ** is available to hold the result, or if the result would exceed the
  4814   4854   ** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
  4815   4855   **
  4816   4856   ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
  4817   4857   ** bound parameter expansions.  ^The [SQLITE_OMIT_TRACE] compile-time
  4818   4858   ** option causes sqlite3_expanded_sql() to always return NULL.
  4819   4859   **
  4820         -** ^The string returned by sqlite3_sql(P) is managed by SQLite and is
  4821         -** automatically freed when the prepared statement is finalized.
         4860  +** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
         4861  +** are managed by SQLite and are automatically freed when the prepared
         4862  +** statement is finalized.
  4822   4863   ** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
  4823   4864   ** is obtained from [sqlite3_malloc()] and must be free by the application
  4824   4865   ** by passing it to [sqlite3_free()].
  4825   4866   */
  4826   4867   SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
  4827   4868   SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
         4869  +SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
  4828   4870   
  4829   4871   /*
  4830   4872   ** CAPI3REF: Determine If An SQL Statement Writes The Database
  4831   4873   ** METHOD: sqlite3_stmt
  4832   4874   **
  4833   4875   ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  4834   4876   ** and only if the [prepared statement] X makes no direct changes to
................................................................................
  7310   7352                          void **ppArg);
  7311   7353     int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
  7312   7354     /* The methods above are in version 1 of the sqlite_module object. Those 
  7313   7355     ** below are for version 2 and greater. */
  7314   7356     int (*xSavepoint)(sqlite3_vtab *pVTab, int);
  7315   7357     int (*xRelease)(sqlite3_vtab *pVTab, int);
  7316   7358     int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
         7359  +  /* The methods above are in versions 1 and 2 of the sqlite_module object.
         7360  +  ** Those below are for version 3 and greater. */
         7361  +  int (*xShadowName)(const char*);
  7317   7362   };
  7318   7363   
  7319   7364   /*
  7320   7365   ** CAPI3REF: Virtual Table Indexing Information
  7321   7366   ** KEYWORDS: sqlite3_index_info
  7322   7367   **
  7323   7368   ** The sqlite3_index_info structure and its substructures is used as part
................................................................................
  8232   8277   #define SQLITE_TESTCTRL_PENDING_BYTE            11
  8233   8278   #define SQLITE_TESTCTRL_ASSERT                  12
  8234   8279   #define SQLITE_TESTCTRL_ALWAYS                  13
  8235   8280   #define SQLITE_TESTCTRL_RESERVE                 14
  8236   8281   #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  8237   8282   #define SQLITE_TESTCTRL_ISKEYWORD               16  /* NOT USED */
  8238   8283   #define SQLITE_TESTCTRL_SCRATCHMALLOC           17  /* NOT USED */
         8284  +#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS      17
  8239   8285   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  8240   8286   #define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
  8241   8287   #define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
  8242   8288   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  8243   8289   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  8244   8290   #define SQLITE_TESTCTRL_BYTEORDER               22
  8245   8291   #define SQLITE_TESTCTRL_ISINIT                  23
................................................................................
  9644   9690   ** CAPI3REF: Virtual Table Configuration Options
  9645   9691   **
  9646   9692   ** These macros define the various options to the
  9647   9693   ** [sqlite3_vtab_config()] interface that [virtual table] implementations
  9648   9694   ** can use to customize and optimize their behavior.
  9649   9695   **
  9650   9696   ** <dl>
         9697  +** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
  9651   9698   ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
  9652   9699   ** <dd>Calls of the form
  9653   9700   ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
  9654   9701   ** where X is an integer.  If X is zero, then the [virtual table] whose
  9655   9702   ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
  9656   9703   ** support constraints.  In this configuration (which is the default) if
  9657   9704   ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
................................................................................
 10413  10460     unsigned int *anQueue;            /* Number of pending entries in the queue */
 10414  10461     int nCoord;                       /* Number of coordinates */
 10415  10462     int iLevel;                       /* Level of current node or entry */
 10416  10463     int mxLevel;                      /* The largest iLevel value in the tree */
 10417  10464     sqlite3_int64 iRowid;             /* Rowid for current entry */
 10418  10465     sqlite3_rtree_dbl rParentScore;   /* Score of parent node */
 10419  10466     int eParentWithin;                /* Visibility of parent node */
 10420         -  int eWithin;                      /* OUT: Visiblity */
        10467  +  int eWithin;                      /* OUT: Visibility */
 10421  10468     sqlite3_rtree_dbl rScore;         /* OUT: Write the score here */
 10422  10469     /* The following fields are only available in 3.8.11 and later */
 10423  10470     sqlite3_value **apSqlParam;       /* Original SQL values of parameters */
 10424  10471   };
 10425  10472   
 10426  10473   /*
 10427  10474   ** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
................................................................................
 10909  10956   ** [sqlite3changeset_invert()] functions, all changes within the changeset 
 10910  10957   ** that apply to a single table are grouped together. This means that when 
 10911  10958   ** an application iterates through a changeset using an iterator created by 
 10912  10959   ** this function, all changes that relate to a single table are visited 
 10913  10960   ** consecutively. There is no chance that the iterator will visit a change 
 10914  10961   ** the applies to table X, then one for table Y, and then later on visit 
 10915  10962   ** another change for table X.
        10963  +**
        10964  +** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
        10965  +** may be modified by passing a combination of
        10966  +** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
        10967  +**
        10968  +** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
        10969  +** and therefore subject to change.
 10916  10970   */
 10917  10971   SQLITE_API int sqlite3changeset_start(
 10918  10972     sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
 10919  10973     int nChangeset,                 /* Size of changeset blob in bytes */
 10920  10974     void *pChangeset                /* Pointer to blob containing changeset */
 10921  10975   );
        10976  +SQLITE_API int sqlite3changeset_start_v2(
        10977  +  sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
        10978  +  int nChangeset,                 /* Size of changeset blob in bytes */
        10979  +  void *pChangeset,               /* Pointer to blob containing changeset */
        10980  +  int flags                       /* SESSION_CHANGESETSTART_* flags */
        10981  +);
        10982  +
        10983  +/*
        10984  +** CAPI3REF: Flags for sqlite3changeset_start_v2
        10985  +**
        10986  +** The following flags may passed via the 4th parameter to
        10987  +** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
        10988  +**
        10989  +** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
        10990  +**   Invert the changeset while iterating through it. This is equivalent to
        10991  +**   inverting a changeset using sqlite3changeset_invert() before applying it.
        10992  +**   It is an error to specify this flag with a patchset.
        10993  +*/
        10994  +#define SQLITE_CHANGESETSTART_INVERT        0x0002
 10922  10995   
 10923  10996   
 10924  10997   /*
 10925  10998   ** CAPI3REF: Advance A Changeset Iterator
 10926  10999   ** METHOD: sqlite3_changeset_iter
 10927  11000   **
 10928  11001   ** This function may only be used with iterators created by function
................................................................................
 11569  11642     int(*xConflict)(
 11570  11643       void *pCtx,                   /* Copy of sixth arg to _apply() */
 11571  11644       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 11572  11645       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 11573  11646     ),
 11574  11647     void *pCtx,                     /* First argument passed to xConflict */
 11575  11648     void **ppRebase, int *pnRebase, /* OUT: Rebase data */
 11576         -  int flags                       /* Combination of SESSION_APPLY_* flags */
        11649  +  int flags                       /* SESSION_CHANGESETAPPLY_* flags */
 11577  11650   );
 11578  11651   
 11579  11652   /*
 11580  11653   ** CAPI3REF: Flags for sqlite3changeset_apply_v2
 11581  11654   **
 11582  11655   ** The following flags may passed via the 9th parameter to
 11583  11656   ** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
................................................................................
 11587  11660   **   Usually, the sessions module encloses all operations performed by
 11588  11661   **   a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
 11589  11662   **   SAVEPOINT is committed if the changeset or patchset is successfully
 11590  11663   **   applied, or rolled back if an error occurs. Specifying this flag
 11591  11664   **   causes the sessions module to omit this savepoint. In this case, if the
 11592  11665   **   caller has an open transaction or savepoint when apply_v2() is called, 
 11593  11666   **   it may revert the partially applied changeset by rolling it back.
        11667  +**
        11668  +** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
        11669  +**   Invert the changeset before applying it. This is equivalent to inverting
        11670  +**   a changeset using sqlite3changeset_invert() before applying it. It is
        11671  +**   an error to specify this flag with a patchset.
 11594  11672   */
 11595  11673   #define SQLITE_CHANGESETAPPLY_NOSAVEPOINT   0x0001
        11674  +#define SQLITE_CHANGESETAPPLY_INVERT        0x0002
 11596  11675   
 11597  11676   /* 
 11598  11677   ** CAPI3REF: Constants Passed To The Conflict Handler
 11599  11678   **
 11600  11679   ** Values that may be passed as the second argument to a conflict-handler.
 11601  11680   **
 11602  11681   ** <dl>
................................................................................
 11981  12060     int (*xOutput)(void *pOut, const void *pData, int nData),
 11982  12061     void *pOut
 11983  12062   );
 11984  12063   SQLITE_API int sqlite3changeset_start_strm(
 11985  12064     sqlite3_changeset_iter **pp,
 11986  12065     int (*xInput)(void *pIn, void *pData, int *pnData),
 11987  12066     void *pIn
        12067  +);
        12068  +SQLITE_API int sqlite3changeset_start_v2_strm(
        12069  +  sqlite3_changeset_iter **pp,
        12070  +  int (*xInput)(void *pIn, void *pData, int *pnData),
        12071  +  void *pIn,
        12072  +  int flags
 11988  12073   );
 11989  12074   SQLITE_API int sqlite3session_changeset_strm(
 11990  12075     sqlite3_session *pSession,
 11991  12076     int (*xOutput)(void *pOut, const void *pData, int nData),
 11992  12077     void *pOut
 11993  12078   );
 11994  12079   SQLITE_API int sqlite3session_patchset_strm(
................................................................................
 12008  12093     sqlite3_rebaser *pRebaser,
 12009  12094     int (*xInput)(void *pIn, void *pData, int *pnData),
 12010  12095     void *pIn,
 12011  12096     int (*xOutput)(void *pOut, const void *pData, int nData),
 12012  12097     void *pOut
 12013  12098   );
 12014  12099   
        12100  +/*
        12101  +** CAPI3REF: Configure global parameters
        12102  +**
        12103  +** The sqlite3session_config() interface is used to make global configuration
        12104  +** changes to the sessions module in order to tune it to the specific needs 
        12105  +** of the application.
        12106  +**
        12107  +** The sqlite3session_config() interface is not threadsafe. If it is invoked
        12108  +** while any other thread is inside any other sessions method then the
        12109  +** results are undefined. Furthermore, if it is invoked after any sessions
        12110  +** related objects have been created, the results are also undefined. 
        12111  +**
        12112  +** The first argument to the sqlite3session_config() function must be one
        12113  +** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The 
        12114  +** interpretation of the (void*) value passed as the second parameter and
        12115  +** the effect of calling this function depends on the value of the first
        12116  +** parameter.
        12117  +**
        12118  +** <dl>
        12119  +** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
        12120  +**    By default, the sessions module streaming interfaces attempt to input
        12121  +**    and output data in approximately 1 KiB chunks. This operand may be used
        12122  +**    to set and query the value of this configuration setting. The pointer
        12123  +**    passed as the second argument must point to a value of type (int).
        12124  +**    If this value is greater than 0, it is used as the new streaming data
        12125  +**    chunk size for both input and output. Before returning, the (int) value
        12126  +**    pointed to by pArg is set to the final value of the streaming interface
        12127  +**    chunk size.
        12128  +** </dl>
        12129  +**
        12130  +** This function returns SQLITE_OK if successful, or an SQLite error code
        12131  +** otherwise.
        12132  +*/
        12133  +SQLITE_API int sqlite3session_config(int op, void *pArg);
        12134  +
        12135  +/*
        12136  +** CAPI3REF: Values for sqlite3session_config().
        12137  +*/
        12138  +#define SQLITE_SESSION_CONFIG_STRMSIZE 1
 12015  12139   
 12016  12140   /*
 12017  12141   ** Make sure we can call this stuff from C++.
 12018  12142   */
 12019  12143   #if 0
 12020  12144   }
 12021  12145   #endif
................................................................................
 15273  15397   
 15274  15398   #ifndef SQLITE_OMIT_WAL
 15275  15399   SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
 15276  15400   SQLITE_PRIVATE   int sqlite3PagerWalSupported(Pager *pPager);
 15277  15401   SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
 15278  15402   SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
 15279  15403   SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
 15280         -# ifdef SQLITE_DIRECT_OVERFLOW_READ
 15281         -SQLITE_PRIVATE   int sqlite3PagerUseWal(Pager *pPager, Pgno);
 15282         -# endif
 15283  15404   # ifdef SQLITE_ENABLE_SNAPSHOT
 15284  15405   SQLITE_PRIVATE   int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
 15285  15406   SQLITE_PRIVATE   int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
 15286  15407   SQLITE_PRIVATE   int sqlite3PagerSnapshotRecover(Pager *pPager);
 15287  15408   SQLITE_PRIVATE   int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
 15288  15409   SQLITE_PRIVATE   void sqlite3PagerSnapshotUnlock(Pager *pPager);
 15289  15410   # endif
 15290         -#else
 15291         -# define sqlite3PagerUseWal(x,y) 0
        15411  +#endif
        15412  +
        15413  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
        15414  +SQLITE_PRIVATE   int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
 15292  15415   #endif
 15293  15416   
 15294  15417   #ifdef SQLITE_ENABLE_ZIPVFS
 15295  15418   SQLITE_PRIVATE   int sqlite3PagerWalFramesize(Pager *pPager);
 15296  15419   #endif
 15297  15420   
 15298  15421   /* Functions used to query pager state and configuration. */
................................................................................
 15528  15651   
 15529  15652   /* Return the header size */
 15530  15653   SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
 15531  15654   SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
 15532  15655   
 15533  15656   /* Number of dirty pages as a percentage of the configured cache size */
 15534  15657   SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*);
        15658  +
        15659  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
        15660  +SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache);
        15661  +#endif
 15535  15662   
 15536  15663   #endif /* _PCACHE_H_ */
 15537  15664   
 15538  15665   /************** End of pcache.h **********************************************/
 15539  15666   /************** Continuing where we left off in sqliteInt.h ******************/
 15540  15667   /************** Include os.h in the middle of sqliteInt.h ********************/
 15541  15668   /************** Begin file os.h **********************************************/
................................................................................
 16034  16161   };
 16035  16162   
 16036  16163   /*
 16037  16164   ** A hash table for built-in function definitions.  (Application-defined
 16038  16165   ** functions use a regular table table from hash.h.)
 16039  16166   **
 16040  16167   ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
 16041         -** Collisions are on the FuncDef.u.pHash chain.
        16168  +** Collisions are on the FuncDef.u.pHash chain.  Use the SQLITE_FUNC_HASH()
        16169  +** macro to compute a hash on the function name.
 16042  16170   */
 16043  16171   #define SQLITE_FUNC_HASH_SZ 23
 16044  16172   struct FuncDefHash {
 16045  16173     FuncDef *a[SQLITE_FUNC_HASH_SZ];       /* Hash table for functions */
 16046  16174   };
        16175  +#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
 16047  16176   
 16048  16177   #ifdef SQLITE_USER_AUTHENTICATION
 16049  16178   /*
 16050  16179   ** Information held in the "sqlite3" database connection object and used
 16051  16180   ** to manage user authentication.
 16052  16181   */
 16053  16182   typedef struct sqlite3_userauth sqlite3_userauth;
................................................................................
 16100  16229     sqlite3_vfs *pVfs;            /* OS Interface */
 16101  16230     struct Vdbe *pVdbe;           /* List of active virtual machines */
 16102  16231     CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
 16103  16232     sqlite3_mutex *mutex;         /* Connection mutex */
 16104  16233     Db *aDb;                      /* All backends */
 16105  16234     int nDb;                      /* Number of backends currently in use */
 16106  16235     u32 mDbFlags;                 /* flags recording internal state */
 16107         -  u32 flags;                    /* flags settable by pragmas. See below */
        16236  +  u64 flags;                    /* flags settable by pragmas. See below */
 16108  16237     i64 lastRowid;                /* ROWID of most recent insert (see above) */
 16109  16238     i64 szMmap;                   /* Default mmap_size setting */
 16110  16239     u32 nSchemaLock;              /* Do not reset the schema when non-zero */
 16111  16240     unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
 16112  16241     int errCode;                  /* Most recent error code (SQLITE_*) */
 16113  16242     int errMask;                  /* & result codes with this before returning */
 16114  16243     int iSysErrno;                /* Errno value from last system error */
................................................................................
 16266  16395   #define SQLITE_QueryOnly      0x00100000  /* Disable database changes */
 16267  16396   #define SQLITE_CellSizeCk     0x00200000  /* Check btree cell sizes on load */
 16268  16397   #define SQLITE_Fts3Tokenizer  0x00400000  /* Enable fts3_tokenizer(2) */
 16269  16398   #define SQLITE_EnableQPSG     0x00800000  /* Query Planner Stability Guarantee*/
 16270  16399   #define SQLITE_TriggerEQP     0x01000000  /* Show trigger EXPLAIN QUERY PLAN */
 16271  16400   #define SQLITE_ResetDatabase  0x02000000  /* Reset the database */
 16272  16401   #define SQLITE_LegacyAlter    0x04000000  /* Legacy ALTER TABLE behaviour */
        16402  +#define SQLITE_NoSchemaError  0x08000000  /* Do not report schema parse errors*/
        16403  +#define SQLITE_Defensive      0x10000000  /* Input SQL is likely hostile */
 16273  16404   
 16274  16405   /* Flags used only if debugging */
        16406  +#define HI(X)  ((u64)(X)<<32)
 16275  16407   #ifdef SQLITE_DEBUG
 16276         -#define SQLITE_SqlTrace       0x08000000  /* Debug print SQL as it executes */
 16277         -#define SQLITE_VdbeListing    0x10000000  /* Debug listings of VDBE programs */
 16278         -#define SQLITE_VdbeTrace      0x20000000  /* True to trace VDBE execution */
 16279         -#define SQLITE_VdbeAddopTrace 0x40000000  /* Trace sqlite3VdbeAddOp() calls */
 16280         -#define SQLITE_VdbeEQP        0x80000000  /* Debug EXPLAIN QUERY PLAN */
        16408  +#define SQLITE_SqlTrace       HI(0x0001)  /* Debug print SQL as it executes */
        16409  +#define SQLITE_VdbeListing    HI(0x0002)  /* Debug listings of VDBE progs */
        16410  +#define SQLITE_VdbeTrace      HI(0x0004)  /* True to trace VDBE execution */
        16411  +#define SQLITE_VdbeAddopTrace HI(0x0008)  /* Trace sqlite3VdbeAddOp() calls */
        16412  +#define SQLITE_VdbeEQP        HI(0x0010)  /* Debug EXPLAIN QUERY PLAN */
 16281  16413   #endif
 16282  16414   
 16283  16415   /*
 16284  16416   ** Allowed values for sqlite3.mDbFlags
 16285  16417   */
 16286  16418   #define DBFLAG_SchemaChange   0x0001  /* Uncommitted Hash table changes */
 16287  16419   #define DBFLAG_PreferBuiltin  0x0002  /* Preference to built-in funcs */
................................................................................
 16407  16539   #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
 16408  16540   #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
 16409  16541   #define SQLITE_FUNC_MINMAX   0x1000 /* True for min() and max() aggregates */
 16410  16542   #define SQLITE_FUNC_SLOCHNG  0x2000 /* "Slow Change". Value constant during a
 16411  16543                                       ** single query - might change over time */
 16412  16544   #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
 16413  16545   #define SQLITE_FUNC_OFFSET   0x8000 /* Built-in sqlite_offset() function */
 16414         -#define SQLITE_FUNC_WINDOW  0x10000 /* Built-in window-only function */
 16415         -#define SQLITE_FUNC_WINDOW_SIZE  0x20000  /* Requires partition size as arg. */
        16546  +#define SQLITE_FUNC_WINDOW   0x00010000 /* Built-in window-only function */
        16547  +#define SQLITE_FUNC_WINDOW_SIZE 0x20000 /* Requires partition size as arg. */
        16548  +#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
 16416  16549   
 16417  16550   /*
 16418  16551   ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
 16419  16552   ** used to create the initializers for the FuncDef structures.
 16420  16553   **
 16421  16554   **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
 16422  16555   **     Used to create a scalar function definition of a function zName
................................................................................
 16484  16617      (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
 16485  16618   #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue) \
 16486  16619     {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
 16487  16620      SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,0,#zName, {0}}
 16488  16621   #define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \
 16489  16622     {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
 16490  16623      SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xFinal,0,#zName, {0}}
 16491         -
 16492  16624   #define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \
 16493  16625     {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \
 16494  16626      SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
        16627  +#define INTERNAL_FUNCTION(zName, nArg, xFunc) \
        16628  +  {nArg, SQLITE_FUNC_INTERNAL|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
        16629  +   0, 0, xFunc, 0, 0, 0, #zName, {0} }
        16630  +
 16495  16631   
 16496  16632   /*
 16497  16633   ** All current savepoints are stored in a linked list starting at
 16498  16634   ** sqlite3.pSavepoint. The first element in the list is the most recently
 16499  16635   ** opened savepoint. Savepoints are added to the list by the vdbe
 16500  16636   ** OP_Savepoint instruction.
 16501  16637   */
................................................................................
 16672  16808   /*
 16673  16809   ** The schema for each SQL table and view is represented in memory
 16674  16810   ** by an instance of the following structure.
 16675  16811   */
 16676  16812   struct Table {
 16677  16813     char *zName;         /* Name of the table or view */
 16678  16814     Column *aCol;        /* Information about each column */
        16815  +#ifdef SQLITE_ENABLE_NORMALIZE
        16816  +  Hash *pColHash;      /* All columns indexed by name */
        16817  +#endif
 16679  16818     Index *pIndex;       /* List of SQL indexes on this table. */
 16680  16819     Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
 16681  16820     FKey *pFKey;         /* Linked list of all foreign keys in this table */
 16682  16821     char *zColAff;       /* String defining the affinity of each column */
 16683  16822     ExprList *pCheck;    /* All CHECK constraints */
 16684  16823                          /*   ... also used as column name list in a VIEW */
 16685  16824     int tnum;            /* Root BTree page for this table */
................................................................................
 16722  16861   #define TF_HasStat1        0x0010    /* nRowLogEst set from sqlite_stat1 */
 16723  16862   #define TF_WithoutRowid    0x0020    /* No rowid.  PRIMARY KEY is the key */
 16724  16863   #define TF_NoVisibleRowid  0x0040    /* No user-visible "rowid" column */
 16725  16864   #define TF_OOOHidden       0x0080    /* Out-of-Order hidden columns */
 16726  16865   #define TF_StatsUsed       0x0100    /* Query planner decisions affected by
 16727  16866                                        ** Index.aiRowLogEst[] values */
 16728  16867   #define TF_HasNotNull      0x0200    /* Contains NOT NULL constraints */
        16868  +#define TF_Shadow          0x0400    /* True for a shadow table */
 16729  16869   
 16730  16870   /*
 16731  16871   ** Test to see whether or not a table is a virtual table.  This is
 16732  16872   ** done as a macro so that it will be optimized out when virtual
 16733  16873   ** table support is omitted from the build.
 16734  16874   */
 16735  16875   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
 17008  17148     void *p;          /* Pointer to sampled record */
 17009  17149     int n;            /* Size of record in bytes */
 17010  17150     tRowcnt *anEq;    /* Est. number of rows where the key equals this sample */
 17011  17151     tRowcnt *anLt;    /* Est. number of rows where key is less than this sample */
 17012  17152     tRowcnt *anDLt;   /* Est. number of distinct keys less than this sample */
 17013  17153   };
 17014  17154   
        17155  +/*
        17156  +** Possible values to use within the flags argument to sqlite3GetToken().
        17157  +*/
        17158  +#define SQLITE_TOKEN_QUOTED    0x1 /* Token is a quoted identifier. */
        17159  +#define SQLITE_TOKEN_KEYWORD   0x2 /* Token is a keyword. */
        17160  +
 17015  17161   /*
 17016  17162   ** Each token coming out of the lexer is an instance of
 17017  17163   ** this structure.  Tokens are also used as part of an expression.
 17018  17164   **
 17019  17165   ** The memory that "z" points to is owned by other objects.  Take care
 17020  17166   ** that the owner of the "z" string does not deallocate the string before
 17021  17167   ** the Token goes out of scope!  Very often, the "z" points to some place
................................................................................
 17189  17335                            ** TK_SELECT_COLUMN: column of the result vector */
 17190  17336     i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
 17191  17337     i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
 17192  17338     u8 op2;                /* TK_REGISTER: original value of Expr.op
 17193  17339                            ** TK_COLUMN: the value of p5 for OP_Column
 17194  17340                            ** TK_AGG_FUNCTION: nesting depth */
 17195  17341     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
 17196         -  Table *pTab;           /* Table for TK_COLUMN expressions.  Can be NULL
 17197         -                         ** for a column of an index on an expression */
 17198         -#ifndef SQLITE_OMIT_WINDOWFUNC
 17199         -  Window *pWin;          /* Window definition for window functions */
 17200         -#endif
        17342  +  union {
        17343  +    Table *pTab;           /* TK_COLUMN: Table containing column. Can be NULL
        17344  +                           ** for a column of an index on an expression */
        17345  +    Window *pWin;          /* TK_FUNCTION: Window definition for the func */
        17346  +  } y;
 17201  17347   };
 17202  17348   
 17203  17349   /*
 17204  17350   ** The following are the meanings of bits in the Expr.flags field.
 17205  17351   */
 17206  17352   #define EP_FromJoin  0x000001 /* Originates in ON/USING clause of outer join */
 17207  17353   #define EP_Agg       0x000002 /* Contains one or more aggregate functions */
................................................................................
 17223  17369   #define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
 17224  17370   #define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
 17225  17371   #define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
 17226  17372   #define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
 17227  17373   #define EP_Subquery  0x200000 /* Tree contains a TK_SELECT operator */
 17228  17374   #define EP_Alias     0x400000 /* Is an alias for a result set column */
 17229  17375   #define EP_Leaf      0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
        17376  +#define EP_WinFunc  0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
 17230  17377   
 17231  17378   /*
 17232  17379   ** The EP_Propagate mask is a set of properties that automatically propagate
 17233  17380   ** upwards into parent nodes.
 17234  17381   */
 17235  17382   #define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
 17236  17383   
................................................................................
 18127  18274     void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx);  /* Callback */
 18128  18275     void *pVdbeBranchArg;                                     /* 1st argument */
 18129  18276   #endif
 18130  18277   #ifndef SQLITE_UNTESTABLE
 18131  18278     int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
 18132  18279   #endif
 18133  18280     int bLocaltimeFault;              /* True to fail localtime() calls */
        18281  +  int bInternalFunctions;           /* Internal SQL functions are visible */
 18134  18282     int iOnceResetThreshold;          /* When to reset OP_Once counters */
 18135  18283     u32 szSorterRef;                  /* Min size in bytes to use sorter-refs */
 18136  18284   };
 18137  18285   
 18138  18286   /*
 18139  18287   ** This macro is used inside of assert() statements to indicate that
 18140  18288   ** the assert is only valid on a well-formed database.  Instead of:
................................................................................
 18380  18528   SQLITE_PRIVATE int sqlite3IsIdChar(u8);
 18381  18529   
 18382  18530   /*
 18383  18531   ** Internal function prototypes
 18384  18532   */
 18385  18533   SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*);
 18386  18534   SQLITE_PRIVATE int sqlite3Strlen30(const char*);
        18535  +#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff)
 18387  18536   SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*);
 18388  18537   #define sqlite3StrNICmp sqlite3_strnicmp
 18389  18538   
 18390  18539   SQLITE_PRIVATE int sqlite3MallocInit(void);
 18391  18540   SQLITE_PRIVATE void sqlite3MallocEnd(void);
 18392  18541   SQLITE_PRIVATE void *sqlite3Malloc(u64);
 18393  18542   SQLITE_PRIVATE void *sqlite3MallocZero(u64);
................................................................................
 18496  18645   SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
 18497  18646   #endif
 18498  18647   
 18499  18648   #if defined(SQLITE_DEBUG)
 18500  18649   SQLITE_PRIVATE   void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
 18501  18650   SQLITE_PRIVATE   void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
 18502  18651   SQLITE_PRIVATE   void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
        18652  +SQLITE_PRIVATE   void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
 18503  18653   SQLITE_PRIVATE   void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
 18504  18654   SQLITE_PRIVATE   void sqlite3TreeViewWith(TreeView*, const With*, u8);
 18505  18655   #ifndef SQLITE_OMIT_WINDOWFUNC
 18506  18656   SQLITE_PRIVATE   void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
 18507  18657   SQLITE_PRIVATE   void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
 18508  18658   #endif
 18509  18659   #endif
................................................................................
 18728  18878   #ifdef SQLITE_ENABLE_CURSOR_HINTS
 18729  18879   SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
 18730  18880   #endif
 18731  18881   SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
 18732  18882   SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
 18733  18883   SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
 18734  18884   SQLITE_PRIVATE int sqlite3IsRowid(const char*);
        18885  +#ifdef SQLITE_ENABLE_NORMALIZE
        18886  +SQLITE_PRIVATE int sqlite3IsRowidN(const char*, int);
        18887  +#endif
 18735  18888   SQLITE_PRIVATE void sqlite3GenerateRowDelete(
 18736  18889       Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
 18737  18890   SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
 18738  18891   SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
 18739  18892   SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
        18893  +SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
 18740  18894   SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
 18741  18895                                        u8,u8,int,int*,int*,Upsert*);
 18742  18896   #ifdef SQLITE_ENABLE_NULL_TRIM
 18743  18897   SQLITE_PRIVATE   void sqlite3SetMakeRecordP5(Vdbe*,Table*);
 18744  18898   #else
 18745  18899   # define sqlite3SetMakeRecordP5(A,B)
 18746  18900   #endif
................................................................................
 18753  18907   SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
 18754  18908   SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*);
 18755  18909   SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
 18756  18910   SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
 18757  18911   SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
 18758  18912   SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
 18759  18913   SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
        18914  +#ifdef SQLITE_ENABLE_NORMALIZE
        18915  +SQLITE_PRIVATE FuncDef *sqlite3FunctionSearchN(int,const char*,int);
        18916  +#endif
 18760  18917   SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
 18761  18918   SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
 18762  18919   SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
 18763  18920   SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
 18764  18921   SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
 18765  18922   SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
 18766  18923   SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
................................................................................
 18910  19067   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
 18911  19068   SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
 18912  19069   SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*);
 18913  19070   SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
 18914  19071   SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
 18915  19072   SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
 18916  19073   SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
        19074  +SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3*);
 18917  19075   SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
 18918  19076   SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
 18919  19077   SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
 18920  19078   SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
 18921  19079   SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
 18922  19080   SQLITE_PRIVATE int sqlite3AbsInt32(int);
 18923  19081   #ifdef SQLITE_ENABLE_8_3_NAMES
................................................................................
 18956  19114   #endif
 18957  19115   SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
 18958  19116   SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 18959  19117   SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 18960  19118   SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 18961  19119   SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
 18962  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
 18963  19124   SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 18964  19125   SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
 18965  19126   SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr *, int, int);
 18966  19127   SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
 18967  19128   SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
 18968  19129   SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
 18969  19130   SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
................................................................................
 19113  19274   SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
 19114  19275   SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
 19115  19276   SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
 19116  19277   SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
 19117  19278   SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
 19118  19279   SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
 19119  19280   SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
        19281  +#ifdef SQLITE_ENABLE_NORMALIZE
        19282  +SQLITE_PRIVATE void sqlite3Normalize(Vdbe*, const char*, int, u8);
        19283  +#endif
 19120  19284   SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
 19121  19285   SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
 19122  19286   SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
 19123  19287   SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
 19124  19288   SQLITE_PRIVATE const char *sqlite3JournalModename(int);
 19125  19289   #ifndef SQLITE_OMIT_WAL
 19126  19290   SQLITE_PRIVATE   int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
................................................................................
 19574  19738      0,                         /* xVdbeBranch */
 19575  19739      0,                         /* pVbeBranchArg */
 19576  19740   #endif
 19577  19741   #ifndef SQLITE_UNTESTABLE
 19578  19742      0,                         /* xTestCallback */
 19579  19743   #endif
 19580  19744      0,                         /* bLocaltimeFault */
        19745  +   0,                         /* bInternalFunctions */
 19581  19746      0x7ffffffe,                /* iOnceResetThreshold */
 19582  19747      SQLITE_DEFAULT_SORTERREF_SIZE   /* szSorterRef */
 19583  19748   };
 19584  19749   
 19585  19750   /*
 19586  19751   ** Hash table for global functions - functions common to all
 19587  19752   ** database connections.  After initialization, this table is
................................................................................
 20065  20230     bft usesStmtJournal:1;  /* True if uses a statement journal */
 20066  20231     bft readOnly:1;         /* True for statements that do not write */
 20067  20232     bft bIsReader:1;        /* True for statements that read */
 20068  20233     yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
 20069  20234     yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
 20070  20235     u32 aCounter[7];        /* Counters used by sqlite3_stmt_status() */
 20071  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
 20072  20240     void *pFree;            /* Free this when deleting the vdbe */
 20073  20241     VdbeFrame *pFrame;      /* Parent frame */
 20074  20242     VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
 20075  20243     int nFrame;             /* Number of frames in pFrame list */
 20076  20244     u32 expmask;            /* Binding to these vars invalidates VM */
 20077  20245     SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
 20078  20246     AuxData *pAuxData;      /* Linked list of auxdata allocations */
................................................................................
 20127  20295   SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
 20128  20296   SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
 20129  20297   
 20130  20298   int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
 20131  20299   SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
 20132  20300   SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
 20133  20301   SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
        20302  +#ifndef SQLITE_OMIT_EXPLAIN
 20134  20303   SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
        20304  +#endif
 20135  20305   SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
 20136  20306   SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
 20137  20307   SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
 20138  20308   SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
 20139  20309   SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
 20140  20310   SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
 20141  20311   SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
................................................................................
 20166  20336   SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
 20167  20337   SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
 20168  20338   SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
 20169  20339   SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
 20170  20340   #ifndef SQLITE_OMIT_WINDOWFUNC
 20171  20341   SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
 20172  20342   #endif
        20343  +#ifndef SQLITE_OMIT_EXPLAIN
 20173  20344   SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
        20345  +#endif
 20174  20346   SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
 20175  20347   SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
 20176  20348   SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
 20177  20349   #ifdef SQLITE_DEBUG
 20178  20350   SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame*);
 20179  20351   #endif
 20180  20352   SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void*);      /* Destructor on Mem */
................................................................................
 28293  28465         sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
 28294  28466         sqlite3TreeViewPop(pView);
 28295  28467       }
 28296  28468       sqlite3TreeViewPop(pView);
 28297  28469     }
 28298  28470   }
 28299  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  +}
 28300  28508   
 28301  28509   /*
 28302  28510   ** Generate a human-readable description of a Select object.
 28303  28511   */
 28304  28512   SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
 28305  28513     int n = 0;
 28306  28514     int cnt = 0;
................................................................................
 28347  28555         for(pX=p->pWin; pX; pX=pX->pNextWin){
 28348  28556           sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
 28349  28557         }
 28350  28558         sqlite3TreeViewPop(pView);
 28351  28559       }
 28352  28560   #endif
 28353  28561       if( p->pSrc && p->pSrc->nSrc ){
 28354         -      int i;
 28355  28562         pView = sqlite3TreeViewPush(pView, (n--)>0);
 28356  28563         sqlite3TreeViewLine(pView, "FROM");
 28357         -      for(i=0; i<p->pSrc->nSrc; i++){
 28358         -        struct SrcList_item *pItem = &p->pSrc->a[i];
 28359         -        StrAccum x;
 28360         -        char zLine[100];
 28361         -        sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
 28362         -        sqlite3_str_appendf(&x, "{%d,*}", pItem->iCursor);
 28363         -        if( pItem->zDatabase ){
 28364         -          sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
 28365         -        }else if( pItem->zName ){
 28366         -          sqlite3_str_appendf(&x, " %s", pItem->zName);
 28367         -        }
 28368         -        if( pItem->pTab ){
 28369         -          sqlite3_str_appendf(&x, " tabname=%Q", pItem->pTab->zName);
 28370         -        }
 28371         -        if( pItem->zAlias ){
 28372         -          sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
 28373         -        }
 28374         -        if( pItem->fg.jointype & JT_LEFT ){
 28375         -          sqlite3_str_appendf(&x, " LEFT-JOIN");
 28376         -        }
 28377         -        sqlite3StrAccumFinish(&x);
 28378         -        sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); 
 28379         -        if( pItem->pSelect ){
 28380         -          sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
 28381         -        }
 28382         -        if( pItem->fg.isTabFunc ){
 28383         -          sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
 28384         -        }
 28385         -        sqlite3TreeViewPop(pView);
 28386         -      }
        28564  +      sqlite3TreeViewSrcList(pView, p->pSrc);
 28387  28565         sqlite3TreeViewPop(pView);
 28388  28566       }
 28389  28567       if( p->pWhere ){
 28390  28568         sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
 28391  28569         sqlite3TreeViewExpr(pView, p->pWhere, 0);
 28392  28570         sqlite3TreeViewPop(pView);
 28393  28571       }
................................................................................
 28669  28847         Window *pWin;
 28670  28848         if( ExprHasProperty(pExpr, EP_TokenOnly) ){
 28671  28849           pFarg = 0;
 28672  28850           pWin = 0;
 28673  28851         }else{
 28674  28852           pFarg = pExpr->x.pList;
 28675  28853   #ifndef SQLITE_OMIT_WINDOWFUNC
 28676         -        pWin = pExpr->pWin;
        28854  +        pWin = pExpr->y.pWin;
 28677  28855   #else
 28678  28856           pWin = 0;
 28679  28857   #endif 
 28680  28858         }
 28681  28859         if( pExpr->op==TK_AGG_FUNCTION ){
 28682  28860           sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
 28683  28861                                pExpr->op2, pExpr->u.zToken);
................................................................................
 31497  31675       ** 0x9e3779b1 is 2654435761 which is the closest prime number to
 31498  31676       ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
 31499  31677       h += sqlite3UpperToLower[c];
 31500  31678       h *= 0x9e3779b1;
 31501  31679     }
 31502  31680     return h;
 31503  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 */
 31504  31696   
 31505  31697   
 31506  31698   /* Link pNew element into the hash table pH.  If pEntry!=0 then also
 31507  31699   ** insert pNew into the pEntry hash bucket.
 31508  31700   */
 31509  31701   static void insertElement(
 31510  31702     Hash *pH,              /* The complete hash table */
................................................................................
 31608  31800       if( sqlite3StrICmp(elem->pKey,pKey)==0 ){ 
 31609  31801         return elem;
 31610  31802       }
 31611  31803       elem = elem->next;
 31612  31804     }
 31613  31805     return &nullElement;
 31614  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 */
 31615  31841   
 31616  31842   /* Remove a single entry from the hash table given a pointer to that
 31617  31843   ** element and a hash on the element's key.
 31618  31844   */
 31619  31845   static void removeElementGivenHash(
 31620  31846     Hash *pH,         /* The pH containing "elem" */
 31621  31847     HashElem* elem,   /* The element to be removed from the pH */
................................................................................
 31652  31878   ** found, or NULL if there is no match.
 31653  31879   */
 31654  31880   SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){
 31655  31881     assert( pH!=0 );
 31656  31882     assert( pKey!=0 );
 31657  31883     return findElementWithHash(pH, pKey, 0)->data;
 31658  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 */
 31659  31893   
 31660  31894   /* Insert an element into the hash table pH.  The key is pKey
 31661  31895   ** and the data is "data".
 31662  31896   **
 31663  31897   ** If no element exists with a matching key, then a new
 31664  31898   ** element is created and NULL is returned.
 31665  31899   **
................................................................................
 32034  32268   
 32035  32269   /*
 32036  32270   ** Allowed values of unixFile.fsFlags
 32037  32271   */
 32038  32272   #define SQLITE_FSFLAGS_IS_MSDOS     0x1
 32039  32273   
 32040  32274   /*
 32041         -** If we are to be thread-safe, include the pthreads header and define
 32042         -** the SQLITE_UNIX_THREADS macro.
        32275  +** If we are to be thread-safe, include the pthreads header.
 32043  32276   */
 32044  32277   #if SQLITE_THREADSAFE
 32045  32278   /* # include <pthread.h> */
 32046         -# define SQLITE_UNIX_THREADS 1
 32047  32279   #endif
 32048  32280   
 32049  32281   /*
 32050  32282   ** Default permissions when creating a new file
 32051  32283   */
 32052  32284   #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
 32053  32285   # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
................................................................................
 33215  33447     /* WAS:  ino_t ino;   */
 33216  33448     u64 ino;                   /* Inode number */
 33217  33449   #endif
 33218  33450   };
 33219  33451   
 33220  33452   /*
 33221  33453   ** An instance of the following structure is allocated for each open
 33222         -** inode.  Or, on LinuxThreads, there is one of these structures for
 33223         -** each inode opened by each thread.
        33454  +** inode.
 33224  33455   **
 33225  33456   ** A single inode can have multiple file descriptors, so each unixFile
 33226  33457   ** structure contains a pointer to an instance of this object and this
 33227  33458   ** object keeps a count of the number of unixFile pointing to it.
 33228  33459   **
 33229  33460   ** Mutex rules:
 33230  33461   **
................................................................................
 33262  33493     sem_t *pSem;                    /* Named POSIX semaphore */
 33263  33494     char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
 33264  33495   #endif
 33265  33496   };
 33266  33497   
 33267  33498   /*
 33268  33499   ** A lists of all unixInodeInfo objects.
        33500  +**
        33501  +** Must hold unixBigLock in order to read or write this variable.
 33269  33502   */
 33270  33503   static unixInodeInfo *inodeList = 0;  /* All unixInodeInfo objects */
 33271  33504   
 33272  33505   #ifdef SQLITE_DEBUG
 33273  33506   /*
 33274         -** True if the inode mutex is held, or not.  Used only within assert()
 33275         -** 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.
 33276  33510   */
 33277  33511   int unixFileMutexHeld(unixFile *pFile){
 33278  33512     assert( pFile->pInode );
 33279  33513     return sqlite3_mutex_held(pFile->pInode->pLockMutex);
 33280  33514   }
 33281  33515   int unixFileMutexNotheld(unixFile *pFile){
 33282  33516     assert( pFile->pInode );
................................................................................
 33396  33630     }
 33397  33631     pInode->pUnused = 0;
 33398  33632   }
 33399  33633   
 33400  33634   /*
 33401  33635   ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
 33402  33636   **
 33403         -** The mutex entered using the unixEnterMutex() function must be held
 33404         -** 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.
 33405  33639   */
 33406  33640   static void releaseInodeInfo(unixFile *pFile){
 33407  33641     unixInodeInfo *pInode = pFile->pInode;
 33408  33642     assert( unixMutexHeld() );
 33409  33643     assert( unixFileMutexNotheld(pFile) );
 33410  33644     if( ALWAYS(pInode) ){
 33411  33645       pInode->nRef--;
................................................................................
 33432  33666   }
 33433  33667   
 33434  33668   /*
 33435  33669   ** Given a file descriptor, locate the unixInodeInfo object that
 33436  33670   ** describes that file descriptor.  Create a new one if necessary.  The
 33437  33671   ** return value might be uninitialized if an error occurs.
 33438  33672   **
 33439         -** The mutex entered using the unixEnterMutex() function must be held
 33440         -** when this function is called.
        33673  +** The global mutex must held when calling this routine.
 33441  33674   **
 33442  33675   ** Return an appropriate error code.
 33443  33676   */
 33444  33677   static int findInodeInfo(
 33445  33678     unixFile *pFile,               /* Unix file with file desc used in the key */
 33446  33679     unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
 33447  33680   ){
................................................................................
 33494  33727     memset(&fileId, 0, sizeof(fileId));
 33495  33728     fileId.dev = statbuf.st_dev;
 33496  33729   #if OS_VXWORKS
 33497  33730     fileId.pId = pFile->pId;
 33498  33731   #else
 33499  33732     fileId.ino = (u64)statbuf.st_ino;
 33500  33733   #endif
        33734  +  assert( unixMutexHeld() );
 33501  33735     pInode = inodeList;
 33502  33736     while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
 33503  33737       pInode = pInode->pNext;
 33504  33738     }
 33505  33739     if( pInode==0 ){
 33506  33740       pInode = sqlite3_malloc64( sizeof(*pInode) );
 33507  33741       if( pInode==0 ){
................................................................................
 33513  33747         pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 33514  33748         if( pInode->pLockMutex==0 ){
 33515  33749           sqlite3_free(pInode);
 33516  33750           return SQLITE_NOMEM_BKPT;
 33517  33751         }
 33518  33752       }
 33519  33753       pInode->nRef = 1;
        33754  +    assert( unixMutexHeld() );
 33520  33755       pInode->pNext = inodeList;
 33521  33756       pInode->pPrev = 0;
 33522  33757       if( inodeList ) inodeList->pPrev = pInode;
 33523  33758       inodeList = pInode;
 33524  33759     }else{
 33525  33760       pInode->nRef++;
 33526  33761     }
................................................................................
 36310  36545   ** unixMutexHeld() must be true when creating or destroying
 36311  36546   ** this object or while reading or writing the following fields:
 36312  36547   **
 36313  36548   **      nRef
 36314  36549   **
 36315  36550   ** The following fields are read-only after the object is created:
 36316  36551   ** 
 36317         -**      fid
        36552  +**      hShm
 36318  36553   **      zFilename
 36319  36554   **
 36320         -** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
        36555  +** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and
 36321  36556   ** unixMutexHeld() is true when reading or writing any other field
 36322  36557   ** in this structure.
 36323  36558   */
 36324  36559   struct unixShmNode {
 36325  36560     unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
 36326         -  sqlite3_mutex *mutex;      /* Mutex to access this object */
        36561  +  sqlite3_mutex *pShmMutex;  /* Mutex to access this object */
 36327  36562     char *zFilename;           /* Name of the mmapped file */
 36328         -  int h;                     /* Open file descriptor */
        36563  +  int hShm;                  /* Open file descriptor */
 36329  36564     int szRegion;              /* Size of shared-memory regions */
 36330  36565     u16 nRegion;               /* Size of array apRegion */
 36331  36566     u8 isReadonly;             /* True if read-only */
 36332  36567     u8 isUnlocked;             /* True if no DMS lock held */
 36333  36568     char **apRegion;           /* Array of mapped shared-memory regions */
 36334  36569     int nRef;                  /* Number of unixShm objects pointing to this */
 36335  36570     unixShm *pFirst;           /* All unixShm objects pointing to this */
................................................................................
 36343  36578   /*
 36344  36579   ** Structure used internally by this VFS to record the state of an
 36345  36580   ** open shared memory connection.
 36346  36581   **
 36347  36582   ** The following fields are initialized when this object is created and
 36348  36583   ** are read-only thereafter:
 36349  36584   **
 36350         -**    unixShm.pFile
        36585  +**    unixShm.pShmNode
 36351  36586   **    unixShm.id
 36352  36587   **
 36353         -** All other fields are read/write.  The unixShm.pFile->mutex must be held
 36354         -** 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.
 36355  36590   */
 36356  36591   struct unixShm {
 36357  36592     unixShmNode *pShmNode;     /* The underlying unixShmNode object */
 36358  36593     unixShm *pNext;            /* Next unixShm with the same unixShmNode */
 36359         -  u8 hasMutex;               /* True if holding the unixShmNode mutex */
        36594  +  u8 hasMutex;               /* True if holding the unixShmNode->pShmMutex */
 36360  36595     u8 id;                     /* Id of this connection within its unixShmNode */
 36361  36596     u16 sharedMask;            /* Mask of shared locks held */
 36362  36597     u16 exclMask;              /* Mask of exclusive locks held */
 36363  36598   };
 36364  36599   
 36365  36600   /*
 36366  36601   ** Constants used for locking
................................................................................
 36382  36617   ){
 36383  36618     unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
 36384  36619     struct flock f;        /* The posix advisory locking structure */
 36385  36620     int rc = SQLITE_OK;    /* Result code form fcntl() */
 36386  36621   
 36387  36622     /* Access to the unixShmNode object is serialized by the caller */
 36388  36623     pShmNode = pFile->pInode->pShmNode;
 36389         -  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() );
 36390  36626   
 36391  36627     /* Shared locks never span more than one byte */
 36392  36628     assert( n==1 || lockType!=F_RDLCK );
 36393  36629   
 36394  36630     /* Locks are within range */
 36395  36631     assert( n>=1 && n<=SQLITE_SHM_NLOCK );
 36396  36632   
 36397         -  if( pShmNode->h>=0 ){
        36633  +  if( pShmNode->hShm>=0 ){
 36398  36634       /* Initialize the locking parameters */
 36399  36635       f.l_type = lockType;
 36400  36636       f.l_whence = SEEK_SET;
 36401  36637       f.l_start = ofst;
 36402  36638       f.l_len = n;
 36403         -    rc = osSetPosixAdvisoryLock(pShmNode->h, &f, pFile);
        36639  +    rc = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
 36404  36640       rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
 36405  36641     }
 36406  36642   
 36407  36643     /* Update the global lock state and do debug tracing */
 36408  36644   #ifdef SQLITE_DEBUG
 36409  36645     { u16 mask;
 36410  36646     OSTRACE(("SHM-LOCK "));
................................................................................
 36468  36704   static void unixShmPurge(unixFile *pFd){
 36469  36705     unixShmNode *p = pFd->pInode->pShmNode;
 36470  36706     assert( unixMutexHeld() );
 36471  36707     if( p && ALWAYS(p->nRef==0) ){
 36472  36708       int nShmPerMap = unixShmRegionPerMap();
 36473  36709       int i;
 36474  36710       assert( p->pInode==pFd->pInode );
 36475         -    sqlite3_mutex_free(p->mutex);
        36711  +    sqlite3_mutex_free(p->pShmMutex);
 36476  36712       for(i=0; i<p->nRegion; i+=nShmPerMap){
 36477         -      if( p->h>=0 ){
        36713  +      if( p->hShm>=0 ){
 36478  36714           osMunmap(p->apRegion[i], p->szRegion);
 36479  36715         }else{
 36480  36716           sqlite3_free(p->apRegion[i]);
 36481  36717         }
 36482  36718       }
 36483  36719       sqlite3_free(p->apRegion);
 36484         -    if( p->h>=0 ){
 36485         -      robust_close(pFd, p->h, __LINE__);
 36486         -      p->h = -1;
        36720  +    if( p->hShm>=0 ){
        36721  +      robust_close(pFd, p->hShm, __LINE__);
        36722  +      p->hShm = -1;
 36487  36723       }
 36488  36724       p->pInode->pShmNode = 0;
 36489  36725       sqlite3_free(p);
 36490  36726     }
 36491  36727   }
 36492  36728   
 36493  36729   /*
................................................................................
 36521  36757     ** process might open and use the *-shm file without truncating it.
 36522  36758     ** And if the *-shm file has been corrupted by a power failure or
 36523  36759     ** system crash, the database itself may also become corrupt.  */
 36524  36760     lock.l_whence = SEEK_SET;
 36525  36761     lock.l_start = UNIX_SHM_DMS;
 36526  36762     lock.l_len = 1;
 36527  36763     lock.l_type = F_WRLCK;
 36528         -  if( osFcntl(pShmNode->h, F_GETLK, &lock)!=0 ) {
        36764  +  if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
 36529  36765       rc = SQLITE_IOERR_LOCK;
 36530  36766     }else if( lock.l_type==F_UNLCK ){
 36531  36767       if( pShmNode->isReadonly ){
 36532  36768         pShmNode->isUnlocked = 1;
 36533  36769         rc = SQLITE_READONLY_CANTINIT;
 36534  36770       }else{
 36535  36771         rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1);
 36536         -      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) ){
 36537  36778           rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
 36538  36779         }
 36539  36780       }
 36540  36781     }else if( lock.l_type==F_WRLCK ){
 36541  36782       rc = SQLITE_BUSY;
 36542  36783     }
 36543  36784   
................................................................................
 36635  36876       sqlite3_snprintf(nShmFilename, zShm, 
 36636  36877                        SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
 36637  36878                        (u32)sStat.st_ino, (u32)sStat.st_dev);
 36638  36879   #else
 36639  36880       sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
 36640  36881       sqlite3FileSuffix3(pDbFd->zPath, zShm);
 36641  36882   #endif
 36642         -    pShmNode->h = -1;
        36883  +    pShmNode->hShm = -1;
 36643  36884       pDbFd->pInode->pShmNode = pShmNode;
 36644  36885       pShmNode->pInode = pDbFd->pInode;
 36645  36886       if( sqlite3GlobalConfig.bCoreMutex ){
 36646         -      pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 36647         -      if( pShmNode->mutex==0 ){
        36887  +      pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
        36888  +      if( pShmNode->pShmMutex==0 ){
 36648  36889           rc = SQLITE_NOMEM_BKPT;
 36649  36890           goto shm_open_err;
 36650  36891         }
 36651  36892       }
 36652  36893   
 36653  36894       if( pInode->bProcessLock==0 ){
 36654  36895         if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
 36655         -        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));
 36656  36897         }
 36657         -      if( pShmNode->h<0 ){
 36658         -        pShmNode->h = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777));
 36659         -        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 ){
 36660  36901             rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm);
 36661  36902             goto shm_open_err;
 36662  36903           }
 36663  36904           pShmNode->isReadonly = 1;
 36664  36905         }
 36665  36906   
 36666  36907         /* If this process is running as root, make sure that the SHM file
 36667  36908         ** is owned by the same user that owns the original database.  Otherwise,
 36668  36909         ** the original owner will not be able to connect.
 36669  36910         */
 36670         -      robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
        36911  +      robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
 36671  36912   
 36672  36913         rc = unixLockSharedMemory(pDbFd, pShmNode);
 36673  36914         if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
 36674  36915       }
 36675  36916     }
 36676  36917   
 36677  36918     /* Make the new connection a child of the unixShmNode */
................................................................................
 36683  36924     pDbFd->pShm = p;
 36684  36925     unixLeaveMutex();
 36685  36926   
 36686  36927     /* The reference count on pShmNode has already been incremented under
 36687  36928     ** the cover of the unixEnterMutex() mutex and the pointer from the
 36688  36929     ** new (struct unixShm) object to the pShmNode has been set. All that is
 36689  36930     ** left to do is to link the new object into the linked list starting
 36690         -  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
 36691         -  ** mutex.
        36931  +  ** at pShmNode->pFirst. This must be done while holding the
        36932  +  ** pShmNode->pShmMutex.
 36692  36933     */
 36693         -  sqlite3_mutex_enter(pShmNode->mutex);
        36934  +  sqlite3_mutex_enter(pShmNode->pShmMutex);
 36694  36935     p->pNext = pShmNode->pFirst;
 36695  36936     pShmNode->pFirst = p;
 36696         -  sqlite3_mutex_leave(pShmNode->mutex);
        36937  +  sqlite3_mutex_leave(pShmNode->pShmMutex);
 36697  36938     return rc;
 36698  36939   
 36699  36940     /* Jump here on any error */
 36700  36941   shm_open_err:
 36701  36942     unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
 36702  36943     sqlite3_free(p);
 36703  36944     unixLeaveMutex();
................................................................................
 36741  36982     if( pDbFd->pShm==0 ){
 36742  36983       rc = unixOpenSharedMemory(pDbFd);
 36743  36984       if( rc!=SQLITE_OK ) return rc;
 36744  36985     }
 36745  36986   
 36746  36987     p = pDbFd->pShm;
 36747  36988     pShmNode = p->pShmNode;
 36748         -  sqlite3_mutex_enter(pShmNode->mutex);
        36989  +  sqlite3_mutex_enter(pShmNode->pShmMutex);
 36749  36990     if( pShmNode->isUnlocked ){
 36750  36991       rc = unixLockSharedMemory(pDbFd, pShmNode);
 36751  36992       if( rc!=SQLITE_OK ) goto shmpage_out;
 36752  36993       pShmNode->isUnlocked = 0;
 36753  36994     }
 36754  36995     assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
 36755  36996     assert( pShmNode->pInode==pDbFd->pInode );
 36756         -  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
 36757         -  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 );
 36758  36999   
 36759  37000     /* Minimum number of regions required to be mapped. */
 36760  37001     nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
 36761  37002   
 36762  37003     if( pShmNode->nRegion<nReqRegion ){
 36763  37004       char **apNew;                      /* New apRegion[] array */
 36764  37005       int nByte = nReqRegion*szRegion;   /* Minimum required file size */
 36765  37006       struct stat sStat;                 /* Used by fstat() */
 36766  37007   
 36767  37008       pShmNode->szRegion = szRegion;
 36768  37009   
 36769         -    if( pShmNode->h>=0 ){
        37010  +    if( pShmNode->hShm>=0 ){
 36770  37011         /* The requested region is not mapped into this processes address space.
 36771  37012         ** Check to see if it has been allocated (i.e. if the wal-index file is
 36772  37013         ** large enough to contain the requested region).
 36773  37014         */
 36774         -      if( osFstat(pShmNode->h, &sStat) ){
        37015  +      if( osFstat(pShmNode->hShm, &sStat) ){
 36775  37016           rc = SQLITE_IOERR_SHMSIZE;
 36776  37017           goto shmpage_out;
 36777  37018         }
 36778  37019     
 36779  37020         if( sStat.st_size<nByte ){
 36780  37021           /* The requested memory region does not exist. If bExtend is set to
 36781  37022           ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
................................................................................
 36795  37036             static const int pgsz = 4096;
 36796  37037             int iPg;
 36797  37038   
 36798  37039             /* Write to the last byte of each newly allocated or extended page */
 36799  37040             assert( (nByte % pgsz)==0 );
 36800  37041             for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
 36801  37042               int x = 0;
 36802         -            if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){
        37043  +            if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
 36803  37044                 const char *zFile = pShmNode->zFilename;
 36804  37045                 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
 36805  37046                 goto shmpage_out;
 36806  37047               }
 36807  37048             }
 36808  37049           }
 36809  37050         }
................................................................................
 36818  37059         goto shmpage_out;
 36819  37060       }
 36820  37061       pShmNode->apRegion = apNew;
 36821  37062       while( pShmNode->nRegion<nReqRegion ){
 36822  37063         int nMap = szRegion*nShmPerMap;
 36823  37064         int i;
 36824  37065         void *pMem;
 36825         -      if( pShmNode->h>=0 ){
        37066  +      if( pShmNode->hShm>=0 ){
 36826  37067           pMem = osMmap(0, nMap,
 36827  37068               pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
 36828         -            MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
        37069  +            MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
 36829  37070           );
 36830  37071           if( pMem==MAP_FAILED ){
 36831  37072             rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
 36832  37073             goto shmpage_out;
 36833  37074           }
 36834  37075         }else{
 36835         -        pMem = sqlite3_malloc64(szRegion);
        37076  +        pMem = sqlite3_malloc64(nMap);
 36836  37077           if( pMem==0 ){
 36837  37078             rc = SQLITE_NOMEM_BKPT;
 36838  37079             goto shmpage_out;
 36839  37080           }
 36840         -        memset(pMem, 0, szRegion);
        37081  +        memset(pMem, 0, nMap);
 36841  37082         }
 36842  37083   
 36843  37084         for(i=0; i<nShmPerMap; i++){
 36844  37085           pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
 36845  37086         }
 36846  37087         pShmNode->nRegion += nShmPerMap;
 36847  37088       }
................................................................................
 36850  37091   shmpage_out:
 36851  37092     if( pShmNode->nRegion>iRegion ){
 36852  37093       *pp = pShmNode->apRegion[iRegion];
 36853  37094     }else{
 36854  37095       *pp = 0;
 36855  37096     }
 36856  37097     if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
 36857         -  sqlite3_mutex_leave(pShmNode->mutex);
        37098  +  sqlite3_mutex_leave(pShmNode->pShmMutex);
 36858  37099     return rc;
 36859  37100   }
 36860  37101   
 36861  37102   /*
 36862  37103   ** Change the lock state for a shared-memory segment.
 36863  37104   **
 36864  37105   ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
................................................................................
 36884  37125     assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
 36885  37126     assert( n>=1 );
 36886  37127     assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
 36887  37128          || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
 36888  37129          || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
 36889  37130          || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
 36890  37131     assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
 36891         -  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
 36892         -  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 );
 36893  37134   
 36894  37135     mask = (1<<(ofst+n)) - (1<<ofst);
 36895  37136     assert( n>1 || mask==(1<<ofst) );
 36896         -  sqlite3_mutex_enter(pShmNode->mutex);
        37137  +  sqlite3_mutex_enter(pShmNode->pShmMutex);
 36897  37138     if( flags & SQLITE_SHM_UNLOCK ){
 36898  37139       u16 allMask = 0; /* Mask of locks held by siblings */
 36899  37140   
 36900  37141       /* See if any siblings hold this same lock */
 36901  37142       for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 36902  37143         if( pX==p ) continue;
 36903  37144         assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
................................................................................
 36962  37203         rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
 36963  37204         if( rc==SQLITE_OK ){
 36964  37205           assert( (p->sharedMask & mask)==0 );
 36965  37206           p->exclMask |= mask;
 36966  37207         }
 36967  37208       }
 36968  37209     }
 36969         -  sqlite3_mutex_leave(pShmNode->mutex);
        37210  +  sqlite3_mutex_leave(pShmNode->pShmMutex);
 36970  37211     OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
 36971  37212              p->id, osGetpid(0), p->sharedMask, p->exclMask));
 36972  37213     return rc;
 36973  37214   }
 36974  37215   
 36975  37216   /*
 36976  37217   ** Implement a memory barrier or memory fence on shared memory.  
................................................................................
 37012  37253     pShmNode = p->pShmNode;
 37013  37254   
 37014  37255     assert( pShmNode==pDbFd->pInode->pShmNode );
 37015  37256     assert( pShmNode->pInode==pDbFd->pInode );
 37016  37257   
 37017  37258     /* Remove connection p from the set of connections associated
 37018  37259     ** with pShmNode */
 37019         -  sqlite3_mutex_enter(pShmNode->mutex);
        37260  +  sqlite3_mutex_enter(pShmNode->pShmMutex);
 37020  37261     for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
 37021  37262     *pp = p->pNext;
 37022  37263   
 37023  37264     /* Free the connection p */
 37024  37265     sqlite3_free(p);
 37025  37266     pDbFd->pShm = 0;
 37026         -  sqlite3_mutex_leave(pShmNode->mutex);
        37267  +  sqlite3_mutex_leave(pShmNode->pShmMutex);
 37027  37268   
 37028  37269     /* If pShmNode->nRef has reached 0, then close the underlying
 37029  37270     ** shared-memory file, too */
 37030  37271     assert( unixFileMutexNotheld(pDbFd) );
 37031  37272     unixEnterMutex();
 37032  37273     assert( pShmNode->nRef>0 );
 37033  37274     pShmNode->nRef--;
 37034  37275     if( pShmNode->nRef==0 ){
 37035         -    if( deleteFlag && pShmNode->h>=0 ){
        37276  +    if( deleteFlag && pShmNode->hShm>=0 ){
 37036  37277         osUnlink(pShmNode->zFilename);
 37037  37278       }
 37038  37279       unixShmPurge(pDbFd);
 37039  37280     }
 37040  37281     unixLeaveMutex();
 37041  37282   
 37042  37283     return SQLITE_OK;
................................................................................
 40446  40687     winceLock local;        /* Locks obtained by this instance of winFile */
 40447  40688     winceLock *shared;      /* Global shared lock memory for the file  */
 40448  40689   #endif
 40449  40690   #if SQLITE_MAX_MMAP_SIZE>0
 40450  40691     int nFetchOut;                /* Number of outstanding xFetch references */
 40451  40692     HANDLE hMap;                  /* Handle for accessing memory mapping */
 40452  40693     void *pMapRegion;             /* Area memory mapped */
 40453         -  sqlite3_int64 mmapSize;       /* Usable size of mapped region */
 40454         -  sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
        40694  +  sqlite3_int64 mmapSize;       /* Size of mapped region */
 40455  40695     sqlite3_int64 mmapSizeMax;    /* Configured FCNTL_MMAP_SIZE value */
 40456  40696   #endif
 40457  40697   };
 40458  40698   
 40459  40699   /*
 40460  40700   ** The winVfsAppData structure is used for the pAppData member for all of the
 40461  40701   ** Win32 VFS variants.
................................................................................
 43068  43308   */
 43069  43309   static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
 43070  43310     winFile *pFile = (winFile*)id;  /* File handle object */
 43071  43311     int rc = SQLITE_OK;             /* Return code for this function */
 43072  43312     DWORD lastErrno;
 43073  43313   #if SQLITE_MAX_MMAP_SIZE>0
 43074  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  +  }
 43075  43335   #endif
 43076  43336   
 43077  43337     assert( pFile );
 43078  43338     SimulateIOError(return SQLITE_IOERR_TRUNCATE);
 43079  43339     OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
 43080  43340              osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
 43081  43341   
................................................................................
 44496  44756   /*
 44497  44757   ** Cleans up the mapped region of the specified file, if any.
 44498  44758   */
 44499  44759   #if SQLITE_MAX_MMAP_SIZE>0
 44500  44760   static int winUnmapfile(winFile *pFile){
 44501  44761     assert( pFile!=0 );
 44502  44762     OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
 44503         -           "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n",
        44763  +           "mmapSize=%lld, mmapSizeMax=%lld\n",
 44504  44764              osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
 44505         -           pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax));
        44765  +           pFile->mmapSize, pFile->mmapSizeMax));
 44506  44766     if( pFile->pMapRegion ){
 44507  44767       if( !osUnmapViewOfFile(pFile->pMapRegion) ){
 44508  44768         pFile->lastErrno = osGetLastError();
 44509  44769         OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
 44510  44770                  "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
 44511  44771                  pFile->pMapRegion));
 44512  44772         return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
 44513  44773                            "winUnmapfile1", pFile->zPath);
 44514  44774       }
 44515  44775       pFile->pMapRegion = 0;
 44516  44776       pFile->mmapSize = 0;
 44517         -    pFile->mmapSizeActual = 0;
 44518  44777     }
 44519  44778     if( pFile->hMap!=NULL ){
 44520  44779       if( !osCloseHandle(pFile->hMap) ){
 44521  44780         pFile->lastErrno = osGetLastError();
 44522  44781         OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
 44523  44782                  osGetCurrentProcessId(), pFile, pFile->hMap));
 44524  44783         return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
................................................................................
 44621  44880         /* Log the error, but continue normal operation using xRead/xWrite */
 44622  44881         OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
 44623  44882                  osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
 44624  44883         return SQLITE_OK;
 44625  44884       }
 44626  44885       pFd->pMapRegion = pNew;
 44627  44886       pFd->mmapSize = nMap;
 44628         -    pFd->mmapSizeActual = nMap;
 44629  44887     }
 44630  44888   
 44631  44889     OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
 44632  44890              osGetCurrentProcessId(), pFd));
 44633  44891     return SQLITE_OK;
 44634  44892   }
 44635  44893   #endif /* SQLITE_MAX_MMAP_SIZE>0 */
................................................................................
 45423  45681     }
 45424  45682     pFile->lastErrno = NO_ERROR;
 45425  45683     pFile->zPath = zName;
 45426  45684   #if SQLITE_MAX_MMAP_SIZE>0
 45427  45685     pFile->hMap = NULL;
 45428  45686     pFile->pMapRegion = 0;
 45429  45687     pFile->mmapSize = 0;
 45430         -  pFile->mmapSizeActual = 0;
 45431  45688     pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
 45432  45689   #endif
 45433  45690   
 45434  45691     OpenCounter(+1);
 45435  45692     return rc;
 45436  45693   }
 45437  45694   
................................................................................
 47320  47577   **   such that p was added to the list more recently than p->pDirtyNext.
 47321  47578   **   PCache.pDirty points to the first (newest) element in the list and
 47322  47579   **   pDirtyTail to the last (oldest).
 47323  47580   **
 47324  47581   **   The PCache.pSynced variable is used to optimize searching for a dirty
 47325  47582   **   page to eject from the cache mid-transaction. It is better to eject
 47326  47583   **   a page that does not require a journal sync than one that does. 
 47327         -**   Therefore, pSynced is maintained to that it *almost* always points
        47584  +**   Therefore, pSynced is maintained so that it *almost* always points
 47328  47585   **   to either the oldest page in the pDirty/pDirtyTail list that has a
 47329  47586   **   clear PGHDR_NEED_SYNC flag or to a page that is older than this one
 47330  47587   **   (so that the right page to eject can be found by following pDirtyPrev
 47331  47588   **   pointers).
 47332  47589   */
 47333  47590   struct PCache {
 47334  47591     PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
................................................................................
 48143  48400   SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
 48144  48401     PgHdr *pDirty;
 48145  48402     int nDirty = 0;
 48146  48403     int nCache = numberOfCachePages(pCache);
 48147  48404     for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
 48148  48405     return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
 48149  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
 48150  48416   
 48151  48417   #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
 48152  48418   /*
 48153  48419   ** For all dirty pages currently in the cache, invoke the specified
 48154  48420   ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
 48155  48421   ** defined.
 48156  48422   */
................................................................................
 48267  48533     PgHdr1 *pNext;                 /* Next in hash table chain */
 48268  48534     PCache1 *pCache;               /* Cache that currently owns this page */
 48269  48535     PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
 48270  48536     PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
 48271  48537   };
 48272  48538   
 48273  48539   /*
 48274         -** A page is pinned if it is no on the LRU list
        48540  +** A page is pinned if it is not on the LRU list.  To be "pinned" means
        48541  +** that the page is in active use and must not be deallocated.
 48275  48542   */
 48276  48543   #define PAGE_IS_PINNED(p)    ((p)->pLruNext==0)
 48277  48544   #define PAGE_IS_UNPINNED(p)  ((p)->pLruNext!=0)
 48278  48545   
 48279  48546   /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
 48280  48547   ** of one or more PCaches that are able to recycle each other's unpinned
 48281  48548   ** pages when they are under memory pressure.  A PGroup is an instance of
................................................................................
 50907  51174   **
 50908  51175   ** instead of
 50909  51176   **
 50910  51177   **   if( pPager->jfd->pMethods ){ ...
 50911  51178   */
 50912  51179   #define isOpen(pFd) ((pFd)->pMethods!=0)
 50913  51180   
        51181  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
 50914  51182   /*
 50915         -** Return true if this pager uses a write-ahead log to read page pgno.
 50916         -** Return false if the pager reads pgno directly from the database.
        51183  +** Return true if page pgno can be read directly from the database file
        51184  +** by the b-tree layer. This is the case if:
        51185  +**
        51186  +**   * the database file is open,
        51187  +**   * there are no dirty pages in the cache, and
        51188  +**   * the desired page is not currently in the wal file.
 50917  51189   */
 50918         -#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_DIRECT_OVERFLOW_READ)
 50919         -SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno pgno){
 50920         -  u32 iRead = 0;
 50921         -  int rc;
 50922         -  if( pPager->pWal==0 ) return 0;
 50923         -  rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
 50924         -  return rc || iRead;
        51190  +SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
        51191  +  if( pPager->fd->pMethods==0 ) return 0;
        51192  +  if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
        51193  +#ifndef SQLITE_OMIT_WAL
        51194  +  if( pPager->pWal ){
        51195  +    u32 iRead = 0;
        51196  +    int rc;
        51197  +    rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
        51198  +    return (rc==SQLITE_OK && iRead==0);
        51199  +  }
        51200  +#endif
        51201  +  return 1;
 50925  51202   }
 50926  51203   #endif
        51204  +
 50927  51205   #ifndef SQLITE_OMIT_WAL
 50928  51206   # define pagerUseWal(x) ((x)->pWal!=0)
 50929  51207   #else
 50930  51208   # define pagerUseWal(x) 0
 50931  51209   # define pagerRollbackWal(x) 0
 50932  51210   # define pagerWalFrames(v,w,x,y) 0
 50933  51211   # define pagerOpenWalIfPresent(z) SQLITE_OK
................................................................................
 57103  57381   SQLITE_PRIVATE void sqlite3PagerSetCodec(
 57104  57382     Pager *pPager,
 57105  57383     void *(*xCodec)(void*,void*,Pgno,int),
 57106  57384     void (*xCodecSizeChng)(void*,int,int),
 57107  57385     void (*xCodecFree)(void*),
 57108  57386     void *pCodec
 57109  57387   ){
 57110         -  if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
        57388  +  if( pPager->xCodecFree ){
        57389  +    pPager->xCodecFree(pPager->pCodec);
        57390  +  }else{
        57391  +    pager_reset(pPager);
        57392  +  }
 57111  57393     pPager->xCodec = pPager->memDb ? 0 : xCodec;
 57112  57394     pPager->xCodecSizeChng = xCodecSizeChng;
 57113  57395     pPager->xCodecFree = xCodecFree;
 57114  57396     pPager->pCodec = pCodec;
 57115  57397     setGetterMethod(pPager);
 57116  57398     pagerReportSize(pPager);
 57117  57399   }
................................................................................
 65763  66045         pBt->usableSize = usableSize;
 65764  66046         pBt->pageSize = pageSize;
 65765  66047         freeTempSpace(pBt);
 65766  66048         rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
 65767  66049                                      pageSize-usableSize);
 65768  66050         return rc;
 65769  66051       }
 65770         -    if( (pBt->db->flags & SQLITE_WriteSchema)==0 && nPage>nPageFile ){
        66052  +    if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){
 65771  66053         rc = SQLITE_CORRUPT_BKPT;
 65772  66054         goto page1_init_failed;
 65773  66055       }
 65774  66056       /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
 65775  66057       ** be less than 480. In other words, if the page size is 512, then the
 65776  66058       ** reserved space size cannot exceed 32. */
 65777  66059       if( usableSize<480 ){
................................................................................
 66237  66519     Pager *pPager = pBt->pPager;
 66238  66520     int rc;
 66239  66521   
 66240  66522     assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || 
 66241  66523         eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
 66242  66524     assert( sqlite3_mutex_held(pBt->mutex) );
 66243  66525     assert( pDbPage->pBt==pBt );
        66526  +  if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT;
 66244  66527   
 66245  66528     /* Move page iDbPage from its current location to page number iFreePage */
 66246  66529     TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", 
 66247  66530         iDbPage, iFreePage, iPtrPage, eType));
 66248  66531     rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
 66249  66532     if( rc!=SQLITE_OK ){
 66250  66533       return rc;
................................................................................
 67408  67691             rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
 67409  67692           }
 67410  67693           offset -= ovflSize;
 67411  67694         }else{
 67412  67695           /* Need to read this page properly. It contains some of the
 67413  67696           ** range of data that is being read (eOp==0) or written (eOp!=0).
 67414  67697           */
 67415         -#ifdef SQLITE_DIRECT_OVERFLOW_READ
 67416         -        sqlite3_file *fd;      /* File from which to do direct overflow read */
 67417         -#endif
 67418  67698           int a = amt;
 67419  67699           if( a + offset > ovflSize ){
 67420  67700             a = ovflSize - offset;
 67421  67701           }
 67422  67702   
 67423  67703   #ifdef SQLITE_DIRECT_OVERFLOW_READ
 67424  67704           /* If all the following are true:
 67425  67705           **
 67426  67706           **   1) this is a read operation, and 
 67427  67707           **   2) data is required from the start of this overflow page, and
 67428         -        **   3) there is no open write-transaction, and
        67708  +        **   3) there are no dirty pages in the page-cache
 67429  67709           **   4) the database is file-backed, and
 67430  67710           **   5) the page is not in the WAL file
 67431  67711           **   6) at least 4 bytes have already been read into the output buffer 
 67432  67712           **
 67433  67713           ** then data can be read directly from the database file into the
 67434  67714           ** output buffer, bypassing the page-cache altogether. This speeds
 67435  67715           ** up loading large records that span many overflow pages.
 67436  67716           */
 67437  67717           if( eOp==0                                             /* (1) */
 67438  67718            && offset==0                                          /* (2) */
 67439         -         && pBt->inTransaction==TRANS_READ                     /* (3) */
 67440         -         && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (4) */
 67441         -         && 0==sqlite3PagerUseWal(pBt->pPager, nextPage)       /* (5) */
        67719  +         && sqlite3PagerDirectReadOk(pBt->pPager, nextPage)    /* (3,4,5) */
 67442  67720            && &pBuf[-4]>=pBufStart                               /* (6) */
 67443  67721           ){
        67722  +          sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
 67444  67723             u8 aSave[4];
 67445  67724             u8 *aWrite = &pBuf[-4];
 67446  67725             assert( aWrite>=pBufStart );                         /* due to (6) */
 67447  67726             memcpy(aSave, aWrite, 4);
 67448  67727             rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
 67449  67728             nextPage = get4byte(aWrite);
 67450  67729             memcpy(aWrite, aSave, 4);
................................................................................
 74032  74311     */
 74033  74312     if( fg & MEM_Int ){
 74034  74313       sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
 74035  74314     }else{
 74036  74315       assert( fg & MEM_Real );
 74037  74316       sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
 74038  74317     }
 74039         -  pMem->n = sqlite3Strlen30(pMem->z);
        74318  +  assert( pMem->z!=0 );
        74319  +  pMem->n = sqlite3Strlen30NN(pMem->z);
 74040  74320     pMem->enc = SQLITE_UTF8;
 74041  74321     pMem->flags |= MEM_Str|MEM_Term;
 74042  74322     if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
 74043  74323     sqlite3VdbeChangeEncoding(pMem, enc);
 74044  74324     return SQLITE_OK;
 74045  74325   }
 74046  74326   
................................................................................
 75607  75887     if( p==0 ) return;
 75608  75888     p->prepFlags = prepFlags;
 75609  75889     if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
 75610  75890       p->expmask = 0;
 75611  75891     }
 75612  75892     assert( p->zSql==0 );
 75613  75893     p->zSql = sqlite3DbStrNDup(p->db, z, n);
        75894  +#ifdef SQLITE_ENABLE_NORMALIZE
        75895  +  assert( p->zNormSql==0 );
        75896  +  if( p->zSql && (prepFlags & SQLITE_PREPARE_NORMALIZE)!=0 ){
        75897  +    sqlite3Normalize(p, p->zSql, n, prepFlags);
        75898  +    assert( p->zNormSql!=0 || p->db->mallocFailed );
        75899  +  }
        75900  +#endif
 75614  75901   }
 75615  75902   
 75616  75903   /*
 75617  75904   ** Swap all content between two VDBE structures.
 75618  75905   */
 75619  75906   SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
 75620  75907     Vdbe tmp, *pTmp;
................................................................................
 75628  75915     pB->pNext = pTmp;
 75629  75916     pTmp = pA->pPrev;
 75630  75917     pA->pPrev = pB->pPrev;
 75631  75918     pB->pPrev = pTmp;
 75632  75919     zTmp = pA->zSql;
 75633  75920     pA->zSql = pB->zSql;
 75634  75921     pB->zSql = zTmp;
        75922  +#ifdef SQLITE_ENABLE_NORMALIZE
        75923  +  zTmp = pA->zNormSql;
        75924  +  pA->zNormSql = pB->zNormSql;
        75925  +  pB->zNormSql = zTmp;
        75926  +#endif
 75635  75927     pB->expmask = pA->expmask;
 75636  75928     pB->prepFlags = pA->prepFlags;
 75637  75929     memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
 75638  75930     pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
 75639  75931   }
 75640  75932   
 75641  75933   /*
................................................................................
 78699  78991       releaseMemArray(p->aVar, p->nVar);
 78700  78992       sqlite3DbFree(db, p->pVList);
 78701  78993       sqlite3DbFree(db, p->pFree);
 78702  78994     }
 78703  78995     vdbeFreeOpArray(db, p->aOp, p->nOp);
 78704  78996     sqlite3DbFree(db, p->aColName);
 78705  78997     sqlite3DbFree(db, p->zSql);
        78998  +#ifdef SQLITE_ENABLE_NORMALIZE
        78999  +  sqlite3DbFree(db, p->zNormSql);
        79000  +#endif
 78706  79001   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 78707  79002     {
 78708  79003       int i;
 78709  79004       for(i=0; i<p->nScan; i++){
 78710  79005         sqlite3DbFree(db, p->aScan[i].zName);
 78711  79006       }
 78712  79007       sqlite3DbFree(db, p->aScan);
................................................................................
 82113  82408       z = sqlite3VdbeExpandSql(p, zSql);
 82114  82409       sqlite3_mutex_leave(p->db->mutex);
 82115  82410     }
 82116  82411     return z;
 82117  82412   #endif
 82118  82413   }
 82119  82414   
        82415  +#ifdef SQLITE_ENABLE_NORMALIZE
        82416  +/*
        82417  +** Return the normalized SQL associated with a prepared statement.
        82418  +*/
        82419  +SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
        82420  +  Vdbe *p = (Vdbe *)pStmt;
        82421  +  return p ? p->zNormSql : 0;
        82422  +}
        82423  +#endif /* SQLITE_ENABLE_NORMALIZE */
        82424  +
 82120  82425   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 82121  82426   /*
 82122  82427   ** Allocate and populate an UnpackedRecord structure based on the serialized
 82123  82428   ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
 82124  82429   ** if successful, or a NULL pointer if an OOM error is encountered.
 82125  82430   */
 82126  82431   static UnpackedRecord *vdbeUnpackRecord(
................................................................................
 85552  85857     }else{
 85553  85858       /* Rare case of a really large header */
 85554  85859       nVarint = sqlite3VarintLen(nHdr);
 85555  85860       nHdr += nVarint;
 85556  85861       if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
 85557  85862     }
 85558  85863     nByte = nHdr+nData;
 85559         -  if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 85560         -    goto too_big;
 85561         -  }
 85562  85864   
 85563  85865     /* Make sure the output register has a buffer large enough to store 
 85564  85866     ** the new record. The output register (pOp->p3) is not allowed to
 85565  85867     ** be one of the input registers (because the following call to
 85566  85868     ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
 85567  85869     */
 85568         -  if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
 85569         -    goto no_mem;
        85870  +  if( nByte+nZero<=pOut->szMalloc ){
        85871  +    /* The output register is already large enough to hold the record.
        85872  +    ** No error checks or buffer enlargement is required */
        85873  +    pOut->z = pOut->zMalloc;
        85874  +  }else{
        85875  +    /* Need to make sure that the output is not too big and then enlarge
        85876  +    ** the output register to hold the full result */
        85877  +    if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
        85878  +      goto too_big;
        85879  +    }
        85880  +    if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
        85881  +      goto no_mem;
        85882  +    }
 85570  85883     }
 85571  85884     zNewRecord = (u8 *)pOut->z;
 85572  85885   
 85573  85886     /* Write the record */
 85574  85887     i = putVarint32(zNewRecord, nHdr);
 85575  85888     j = nHdr;
 85576  85889     assert( pData0<=pLast );
................................................................................
 88418  88731       db->mDbFlags |= DBFLAG_SchemaChange;
 88419  88732       p->expired = 0;
 88420  88733     }else
 88421  88734   #endif
 88422  88735     {
 88423  88736       zMaster = MASTER_NAME;
 88424  88737       initData.db = db;
 88425         -    initData.iDb = pOp->p1;
        88738  +    initData.iDb = iDb;
 88426  88739       initData.pzErrMsg = &p->zErrMsg;
 88427  88740       initData.mInitFlags = 0;
 88428  88741       zSql = sqlite3MPrintf(db,
 88429  88742          "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
 88430  88743          db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
 88431  88744       if( zSql==0 ){
 88432  88745         rc = SQLITE_NOMEM_BKPT;
................................................................................
 94001  94314           continue;
 94002  94315         }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 94003  94316           if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
 94004  94317         }else if( pExpr->x.pList ){
 94005  94318           if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
 94006  94319         }
 94007  94320   #ifndef SQLITE_OMIT_WINDOWFUNC
 94008         -      if( !ExprHasProperty(pExpr, EP_Reduced) && pExpr->pWin ){
 94009         -        Window *pWin = pExpr->pWin;
        94321  +      if( ExprHasProperty(pExpr, EP_WinFunc) ){
        94322  +        Window *pWin = pExpr->y.pWin;
 94010  94323           if( sqlite3WalkExprList(pWalker, pWin->pPartition) ) return WRC_Abort;
 94011  94324           if( sqlite3WalkExprList(pWalker, pWin->pOrderBy) ) return WRC_Abort;
 94012  94325           if( sqlite3WalkExpr(pWalker, pWin->pFilter) ) return WRC_Abort;
 94013  94326         }
 94014  94327   #endif
 94015  94328       }
 94016  94329       break;
................................................................................
 94275  94588   ** expression node refer back to that source column.  The following changes
 94276  94589   ** are made to pExpr:
 94277  94590   **
 94278  94591   **    pExpr->iDb           Set the index in db->aDb[] of the database X
 94279  94592   **                         (even if X is implied).
 94280  94593   **    pExpr->iTable        Set to the cursor number for the table obtained
 94281  94594   **                         from pSrcList.
 94282         -**    pExpr->pTab          Points to the Table structure of X.Y (even if
        94595  +**    pExpr->y.pTab        Points to the Table structure of X.Y (even if
 94283  94596   **                         X and/or Y are implied.)
 94284  94597   **    pExpr->iColumn       Set to the column number within the table.
 94285  94598   **    pExpr->op            Set to TK_COLUMN.
 94286  94599   **    pExpr->pLeft         Any expression this points to is deleted
 94287  94600   **    pExpr->pRight        Any expression this points to is deleted.
 94288  94601   **
 94289  94602   ** The zDb variable is the name of the database (the "X").  This value may be
................................................................................
 94319  94632   
 94320  94633     assert( pNC );     /* the name context cannot be NULL. */
 94321  94634     assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
 94322  94635     assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 94323  94636   
 94324  94637     /* Initialize the node to no-match */
 94325  94638     pExpr->iTable = -1;
 94326         -  pExpr->pTab = 0;
 94327  94639     ExprSetVVAProperty(pExpr, EP_NoReduce);
 94328  94640   
 94329  94641     /* Translate the schema name in zDb into a pointer to the corresponding
 94330  94642     ** schema.  If not found, pSchema will remain NULL and nothing will match
 94331  94643     ** resulting in an appropriate error message toward the end of this routine
 94332  94644     */
 94333  94645     if( zDb ){
................................................................................
 94381  94693           if( zTab ){
 94382  94694             const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
 94383  94695             assert( zTabName!=0 );
 94384  94696             if( sqlite3StrICmp(zTabName, zTab)!=0 ){
 94385  94697               continue;
 94386  94698             }
 94387  94699             if( IN_RENAME_OBJECT && pItem->zAlias ){
 94388         -            sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->pTab);
        94700  +            sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
 94389  94701             }
 94390  94702           }
 94391  94703           if( 0==(cntTab++) ){
 94392  94704             pMatch = pItem;
 94393  94705           }
 94394  94706           for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
 94395  94707             if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
................................................................................
 94407  94719               pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
 94408  94720               break;
 94409  94721             }
 94410  94722           }
 94411  94723         }
 94412  94724         if( pMatch ){
 94413  94725           pExpr->iTable = pMatch->iCursor;
 94414         -        pExpr->pTab = pMatch->pTab;
        94726  +        pExpr->y.pTab = pMatch->pTab;
 94415  94727           /* RIGHT JOIN not (yet) supported */
 94416  94728           assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
 94417  94729           if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
 94418  94730             ExprSetProperty(pExpr, EP_CanBeNull);
 94419  94731           }
 94420         -        pSchema = pExpr->pTab->pSchema;
        94732  +        pSchema = pExpr->y.pTab->pSchema;
 94421  94733         }
 94422  94734       } /* if( pSrcList ) */
 94423  94735   
 94424  94736   #if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
 94425  94737       /* If we have not already resolved the name, then maybe 
 94426  94738       ** it is a new.* or old.* trigger argument reference.  Or
 94427  94739       ** maybe it is an excluded.* from an upsert.
................................................................................
 94470  94782           if( iCol<pTab->nCol ){
 94471  94783             cnt++;
 94472  94784   #ifndef SQLITE_OMIT_UPSERT
 94473  94785             if( pExpr->iTable==2 ){
 94474  94786               testcase( iCol==(-1) );
 94475  94787               if( IN_RENAME_OBJECT ){
 94476  94788                 pExpr->iColumn = iCol;
 94477         -              pExpr->pTab = pTab;
        94789  +              pExpr->y.pTab = pTab;
 94478  94790                 eNewExprOp = TK_COLUMN;
 94479  94791               }else{
 94480  94792                 pExpr->iTable = pNC->uNC.pUpsert->regData + iCol;
 94481  94793                 eNewExprOp = TK_REGISTER;
 94482  94794                 ExprSetProperty(pExpr, EP_Alias);
 94483  94795               }
 94484  94796             }else
................................................................................
 94492  94804                 testcase( iCol==32 );
 94493  94805                 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 94494  94806               }else{
 94495  94807                 testcase( iCol==31 );
 94496  94808                 testcase( iCol==32 );
 94497  94809                 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 94498  94810               }
 94499         -            pExpr->pTab = pTab;
        94811  +            pExpr->y.pTab = pTab;
 94500  94812               pExpr->iColumn = (i16)iCol;
 94501  94813               eNewExprOp = TK_TRIGGER;
 94502  94814   #endif /* SQLITE_OMIT_TRIGGER */
 94503  94815             }
 94504  94816           }
 94505  94817         }
 94506  94818       }
................................................................................
 94592  94904     ** Because no reference was made to outer contexts, the pNC->nRef
 94593  94905     ** fields are not changed in any context.
 94594  94906     */
 94595  94907     if( cnt==0 && zTab==0 ){
 94596  94908       assert( pExpr->op==TK_ID );
 94597  94909       if( ExprHasProperty(pExpr,EP_DblQuoted) ){
 94598  94910         pExpr->op = TK_STRING;
 94599         -      pExpr->pTab = 0;
        94911  +      pExpr->y.pTab = 0;
 94600  94912         return WRC_Prune;
 94601  94913       }
 94602  94914       if( sqlite3ExprIdToTrueFalse(pExpr) ){
 94603  94915         return WRC_Prune;
 94604  94916       }
 94605  94917     }
 94606  94918   
................................................................................
 94670  94982   ** Allocate and return a pointer to an expression to load the column iCol
 94671  94983   ** from datasource iSrc in SrcList pSrc.
 94672  94984   */
 94673  94985   SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
 94674  94986     Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
 94675  94987     if( p ){
 94676  94988       struct SrcList_item *pItem = &pSrc->a[iSrc];
 94677         -    p->pTab = pItem->pTab;
        94989  +    p->y.pTab = pItem->pTab;
 94678  94990       p->iTable = pItem->iCursor;
 94679         -    if( p->pTab->iPKey==iCol ){
        94991  +    if( p->y.pTab->iPKey==iCol ){
 94680  94992         p->iColumn = -1;
 94681  94993       }else{
 94682  94994         p->iColumn = (ynVar)iCol;
 94683  94995         testcase( iCol==BMS );
 94684  94996         testcase( iCol==BMS-1 );
 94685  94997         pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
 94686  94998       }
................................................................................
 94762  95074       case TK_ROW: {
 94763  95075         SrcList *pSrcList = pNC->pSrcList;
 94764  95076         struct SrcList_item *pItem;
 94765  95077         assert( pSrcList && pSrcList->nSrc==1 );
 94766  95078         pItem = pSrcList->a;
 94767  95079         assert( HasRowid(pItem->pTab) && pItem->pTab->pSelect==0 );
 94768  95080         pExpr->op = TK_COLUMN;
 94769         -      pExpr->pTab = pItem->pTab;
        95081  +      pExpr->y.pTab = pItem->pTab;
 94770  95082         pExpr->iTable = pItem->iCursor;
 94771  95083         pExpr->iColumn = -1;
 94772  95084         pExpr->affinity = SQLITE_AFF_INTEGER;
 94773  95085         break;
 94774  95086       }
 94775  95087   #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
 94776  95088             && !defined(SQLITE_OMIT_SUBQUERY) */
................................................................................
 94806  95118             pLeft = pRight->pLeft;
 94807  95119             pRight = pRight->pRight;
 94808  95120           }
 94809  95121           zTable = pLeft->u.zToken;
 94810  95122           zColumn = pRight->u.zToken;
 94811  95123           if( IN_RENAME_OBJECT ){
 94812  95124             sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
 94813         -        }
 94814         -        if( IN_RENAME_OBJECT ){
 94815         -          sqlite3RenameTokenRemap(pParse, (void*)&pExpr->pTab, (void*)pLeft);
        95125  +          sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
 94816  95126           }
 94817  95127         }
 94818  95128         return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
 94819  95129       }
 94820  95130   
 94821  95131       /* Resolve function names
 94822  95132       */
................................................................................
 94890  95200           if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
 94891  95201             /* Date/time functions that use 'now', and other functions like
 94892  95202             ** sqlite_version() that might change over time cannot be used
 94893  95203             ** in an index. */
 94894  95204             notValid(pParse, pNC, "non-deterministic functions",
 94895  95205                      NC_IdxExpr|NC_PartIdx);
 94896  95206           }
        95207  +        if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
        95208  +         && pParse->nested==0
        95209  +         && sqlite3Config.bInternalFunctions==0
        95210  +        ){
        95211  +          /* Internal-use-only functions are disallowed unless the
        95212  +          ** SQL is being compiled using sqlite3NestedParse() */
        95213  +          no_such_func = 1;
        95214  +          pDef = 0;
        95215  +        }
 94897  95216         }
 94898  95217   
 94899  95218         if( 0==IN_RENAME_OBJECT ){
 94900  95219   #ifndef SQLITE_OMIT_WINDOWFUNC
 94901  95220           assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
 94902  95221             || (pDef->xValue==0 && pDef->xInverse==0)
 94903  95222             || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
 94904  95223           );
 94905         -        if( pDef && pDef->xValue==0 && pExpr->pWin ){
        95224  +        if( pDef && pDef->xValue==0 && ExprHasProperty(pExpr, EP_WinFunc) ){
 94906  95225             sqlite3ErrorMsg(pParse, 
 94907  95226                 "%.*s() may not be used as a window function", nId, zId
 94908  95227             );
 94909  95228             pNC->nErr++;
 94910  95229           }else if( 
 94911  95230                 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
 94912         -           || (is_agg && (pDef->funcFlags & SQLITE_FUNC_WINDOW) && !pExpr->pWin)
 94913         -           || (is_agg && pExpr->pWin && (pNC->ncFlags & NC_AllowWin)==0)
        95231  +           || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pExpr->y.pWin)
        95232  +           || (is_agg && pExpr->y.pWin && (pNC->ncFlags & NC_AllowWin)==0)
 94914  95233           ){
 94915  95234             const char *zType;
 94916         -          if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pExpr->pWin ){
        95235  +          if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pExpr->y.pWin ){
 94917  95236               zType = "window";
 94918  95237             }else{
 94919  95238               zType = "aggregate";
 94920  95239             }
 94921  95240             sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId);
 94922  95241             pNC->nErr++;
 94923  95242             is_agg = 0;
................................................................................
 94939  95258           }else if( wrong_num_args ){
 94940  95259             sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
 94941  95260                  nId, zId);
 94942  95261             pNC->nErr++;
 94943  95262           }
 94944  95263           if( is_agg ){
 94945  95264   #ifndef SQLITE_OMIT_WINDOWFUNC
 94946         -          pNC->ncFlags &= ~(pExpr->pWin ? NC_AllowWin : NC_AllowAgg);
        95265  +          pNC->ncFlags &= ~(pExpr->y.pWin ? NC_AllowWin : NC_AllowAgg);
 94947  95266   #else
 94948  95267             pNC->ncFlags &= ~NC_AllowAgg;
 94949  95268   #endif
 94950  95269           }
 94951  95270         }
 94952  95271         sqlite3WalkExprList(pWalker, pList);
 94953  95272         if( is_agg ){
 94954  95273   #ifndef SQLITE_OMIT_WINDOWFUNC
 94955         -        if( pExpr->pWin ){
        95274  +        if( pExpr->y.pWin ){
 94956  95275             Select *pSel = pNC->pWinSelect;
 94957         -          sqlite3WalkExprList(pWalker, pExpr->pWin->pPartition);
 94958         -          sqlite3WalkExprList(pWalker, pExpr->pWin->pOrderBy);
 94959         -          sqlite3WalkExpr(pWalker, pExpr->pWin->pFilter);
 94960         -          sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->pWin, pDef);
        95276  +          sqlite3WalkExprList(pWalker, pExpr->y.pWin->pPartition);
        95277  +          sqlite3WalkExprList(pWalker, pExpr->y.pWin->pOrderBy);
        95278  +          sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
        95279  +          sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef);
 94961  95280             if( 0==pSel->pWin 
 94962         -           || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->pWin) 
        95281  +           || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin) 
 94963  95282             ){
 94964         -            pExpr->pWin->pNextWin = pSel->pWin;
 94965         -            pSel->pWin = pExpr->pWin;
        95283  +            pExpr->y.pWin->pNextWin = pSel->pWin;
        95284  +            pSel->pWin = pExpr->y.pWin;
 94966  95285             }
 94967  95286             pNC->ncFlags |= NC_AllowWin;
 94968  95287           }else
 94969  95288   #endif /* SQLITE_OMIT_WINDOWFUNC */
 94970  95289           {
 94971  95290             NameContext *pNC2 = pNC;
 94972  95291             pExpr->op = TK_AGG_FUNCTION;
................................................................................
 95381  95700       pItem->u.x.iOrderByCol = 0;
 95382  95701       if( sqlite3ResolveExprNames(pNC, pE) ){
 95383  95702         return 1;
 95384  95703       }
 95385  95704       for(j=0; j<pSelect->pEList->nExpr; j++){
 95386  95705         if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
 95387  95706   #ifndef SQLITE_OMIT_WINDOWFUNC
 95388         -        if( pE->pWin ){
        95707  +        if( ExprHasProperty(pE, EP_WinFunc) ){
 95389  95708             /* Since this window function is being changed into a reference
 95390  95709             ** to the same window function the result set, remove the instance
 95391  95710             ** of this window function from the Select.pWin list. */
 95392  95711             Window **pp;
 95393  95712             for(pp=&pSelect->pWin; *pp; pp=&(*pp)->pNextWin){
 95394         -            if( *pp==pE->pWin ){
        95713  +            if( *pp==pE->y.pWin ){
 95395  95714                 *pp = (*pp)->pNextWin;
 95396  95715               }    
 95397  95716             }
 95398  95717           }
 95399  95718   #endif
 95400  95719           pItem->u.x.iOrderByCol = j+1;
 95401  95720         }
................................................................................
 95850  96169     if( op==TK_REGISTER ) op = pExpr->op2;
 95851  96170   #ifndef SQLITE_OMIT_CAST
 95852  96171     if( op==TK_CAST ){
 95853  96172       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 95854  96173       return sqlite3AffinityType(pExpr->u.zToken, 0);
 95855  96174     }
 95856  96175   #endif
 95857         -  if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->pTab ){
 95858         -    return sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
        96176  +  if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->y.pTab ){
        96177  +    return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
 95859  96178     }
 95860  96179     if( op==TK_SELECT_COLUMN ){
 95861  96180       assert( pExpr->pLeft->flags&EP_xIsSelect );
 95862  96181       return sqlite3ExprAffinity(
 95863  96182           pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
 95864  96183       );
 95865  96184     }
................................................................................
 95935  96254     CollSeq *pColl = 0;
 95936  96255     Expr *p = pExpr;
 95937  96256     while( p ){
 95938  96257       int op = p->op;
 95939  96258       if( p->flags & EP_Generic ) break;
 95940  96259       if( (op==TK_AGG_COLUMN || op==TK_COLUMN
 95941  96260             || op==TK_REGISTER || op==TK_TRIGGER)
 95942         -     && p->pTab!=0
        96261  +     && p->y.pTab!=0
 95943  96262       ){
 95944         -      /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
        96263  +      /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
 95945  96264         ** a TK_COLUMN but was previously evaluated and cached in a register */
 95946  96265         int j = p->iColumn;
 95947  96266         if( j>=0 ){
 95948         -        const char *zColl = p->pTab->aCol[j].zColl;
        96267  +        const char *zColl = p->y.pTab->aCol[j].zColl;
 95949  96268           pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
 95950  96269         }
 95951  96270         break;
 95952  96271       }
 95953  96272       if( op==TK_CAST || op==TK_UPLUS ){
 95954  96273         p = p->pLeft;
 95955  96274         continue;
................................................................................
 96844  97163   /*
 96845  97164   ** Recursively delete an expression tree.
 96846  97165   */
 96847  97166   static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
 96848  97167     assert( p!=0 );
 96849  97168     /* Sanity check: Assert that the IntValue is non-negative if it exists */
 96850  97169     assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
        97170  +
        97171  +  assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed );
        97172  +  assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced)
        97173  +          || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) );
 96851  97174   #ifdef SQLITE_DEBUG
 96852  97175     if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
 96853  97176       assert( p->pLeft==0 );
 96854  97177       assert( p->pRight==0 );
 96855  97178       assert( p->x.pSelect==0 );
 96856  97179     }
 96857  97180   #endif
................................................................................
 96862  97185       if( p->pRight ){
 96863  97186         sqlite3ExprDeleteNN(db, p->pRight);
 96864  97187       }else if( ExprHasProperty(p, EP_xIsSelect) ){
 96865  97188         sqlite3SelectDelete(db, p->x.pSelect);
 96866  97189       }else{
 96867  97190         sqlite3ExprListDelete(db, p->x.pList);
 96868  97191       }
 96869         -    if( !ExprHasProperty(p, EP_Reduced) ){
 96870         -      sqlite3WindowDelete(db, p->pWin);
        97192  +    if( ExprHasProperty(p, EP_WinFunc) ){
        97193  +      assert( p->op==TK_FUNCTION );
        97194  +      sqlite3WindowDelete(db, p->y.pWin);
 96871  97195       }
 96872  97196     }
 96873  97197     if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
 96874  97198     if( !ExprHasProperty(p, EP_Static) ){
 96875  97199       sqlite3DbFreeNN(db, p);
 96876  97200     }
 96877  97201   }
................................................................................
 96927  97251   static int dupedExprStructSize(Expr *p, int flags){
 96928  97252     int nSize;
 96929  97253     assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
 96930  97254     assert( EXPR_FULLSIZE<=0xfff );
 96931  97255     assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
 96932  97256     if( 0==flags || p->op==TK_SELECT_COLUMN 
 96933  97257   #ifndef SQLITE_OMIT_WINDOWFUNC
 96934         -   || p->pWin 
        97258  +   || ExprHasProperty(p, EP_WinFunc)
 96935  97259   #endif
 96936  97260     ){
 96937  97261       nSize = EXPR_FULLSIZE;
 96938  97262     }else{
 96939  97263       assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
 96940  97264       assert( !ExprHasProperty(p, EP_FromJoin) ); 
 96941  97265       assert( !ExprHasProperty(p, EP_MemToken) );
................................................................................
 96954  97278   ** This function returns the space in bytes required to store the copy 
 96955  97279   ** of the Expr structure and a copy of the Expr.u.zToken string (if that
 96956  97280   ** string is defined.)
 96957  97281   */
 96958  97282   static int dupedExprNodeSize(Expr *p, int flags){
 96959  97283     int nByte = dupedExprStructSize(p, flags) & 0xfff;
 96960  97284     if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
 96961         -    nByte += sqlite3Strlen30(p->u.zToken)+1;
        97285  +    nByte += sqlite3Strlen30NN(p->u.zToken)+1;
 96962  97286     }
 96963  97287     return ROUND8(nByte);
 96964  97288   }
 96965  97289   
 96966  97290   /*
 96967  97291   ** Return the number of bytes required to create a duplicate of the 
 96968  97292   ** expression passed as the first argument. The second argument is a
................................................................................
 97057  97381           pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
 97058  97382         }else{
 97059  97383           pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
 97060  97384         }
 97061  97385       }
 97062  97386   
 97063  97387       /* Fill in pNew->pLeft and pNew->pRight. */
 97064         -    zAlloc += dupedExprNodeSize(p, dupFlags);
 97065         -    if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
        97388  +    if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){
        97389  +      zAlloc += dupedExprNodeSize(p, dupFlags);
 97066  97390         if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
 97067  97391           pNew->pLeft = p->pLeft ?
 97068  97392                         exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
 97069  97393           pNew->pRight = p->pRight ?
 97070  97394                          exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
 97071  97395         }
 97072         -    }else{
 97073  97396   #ifndef SQLITE_OMIT_WINDOWFUNC
 97074         -      if( ExprHasProperty(p, EP_Reduced|EP_TokenOnly) ){
 97075         -        pNew->pWin = 0;
 97076         -      }else{
 97077         -        pNew->pWin = sqlite3WindowDup(db, pNew, p->pWin);
        97397  +      if( ExprHasProperty(p, EP_WinFunc) ){
        97398  +        pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
        97399  +        assert( ExprHasProperty(pNew, EP_WinFunc) );
 97078  97400         }
 97079  97401   #endif /* SQLITE_OMIT_WINDOWFUNC */
        97402  +      if( pzBuffer ){
        97403  +        *pzBuffer = zAlloc;
        97404  +      }
        97405  +    }else{
 97080  97406         if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
 97081  97407           if( pNew->op==TK_SELECT_COLUMN ){
 97082  97408             pNew->pLeft = p->pLeft;
 97083  97409             assert( p->iColumn==0 || p->pRight==0 );
 97084  97410             assert( p->pRight==0  || p->pRight==p->pLeft );
 97085  97411           }else{
 97086  97412             pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
 97087  97413           }
 97088  97414           pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
 97089  97415         }
 97090  97416       }
 97091         -    if( pzBuffer ){
 97092         -      *pzBuffer = zAlloc;
 97093         -    }
 97094  97417     }
 97095  97418     return pNew;
 97096  97419   }
 97097  97420   
 97098  97421   /*
 97099  97422   ** Create and return a deep copy of the object passed as the second 
 97100  97423   ** argument. If an OOM condition is encountered, NULL is returned
................................................................................
 97881  98204       case TK_INTEGER:
 97882  98205       case TK_STRING:
 97883  98206       case TK_FLOAT:
 97884  98207       case TK_BLOB:
 97885  98208         return 0;
 97886  98209       case TK_COLUMN:
 97887  98210         return ExprHasProperty(p, EP_CanBeNull) ||
 97888         -             p->pTab==0 ||  /* Reference to column of index on expression */
 97889         -             (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
        98211  +             p->y.pTab==0 ||  /* Reference to column of index on expression */
        98212  +             (p->iColumn>=0 && p->y.pTab->aCol[p->iColumn].notNull==0);
 97890  98213       default:
 97891  98214         return 1;
 97892  98215     }
 97893  98216   }
 97894  98217   
 97895  98218   /*
 97896  98219   ** Return TRUE if the given expression is a constant which would be
................................................................................
 97937  98260   */
 97938  98261   SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
 97939  98262     if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
 97940  98263     if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
 97941  98264     if( sqlite3StrICmp(z, "OID")==0 ) return 1;
 97942  98265     return 0;
 97943  98266   }
        98267  +#ifdef SQLITE_ENABLE_NORMALIZE
        98268  +SQLITE_PRIVATE int sqlite3IsRowidN(const char *z, int n){
        98269  +  if( sqlite3StrNICmp(z, "_ROWID_", n)==0 ) return 1;
        98270  +  if( sqlite3StrNICmp(z, "ROWID", n)==0 ) return 1;
        98271  +  if( sqlite3StrNICmp(z, "OID", n)==0 ) return 1;
        98272  +  return 0;
        98273  +}
        98274  +#endif
 97944  98275   
 97945  98276   /*
 97946  98277   ** pX is the RHS of an IN operator.  If pX is a SELECT statement 
 97947  98278   ** that can be simplified to a direct table access, then return
 97948  98279   ** a pointer to the SELECT statement.  If pX is not a SELECT statement,
 97949  98280   ** or if the SELECT statement needs to be manifested into a transient
 97950  98281   ** table, then return NULL.
................................................................................
 99170  99501           /* This COLUMN expression is really a constant due to WHERE clause
 99171  99502           ** constraints, and that constant is coded by the pExpr->pLeft
 99172  99503           ** expresssion.  However, make sure the constant has the correct
 99173  99504           ** datatype by applying the Affinity of the table column to the
 99174  99505           ** constant.
 99175  99506           */
 99176  99507           int iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
 99177         -        int aff = sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
        99508  +        int aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
 99178  99509           if( aff!=SQLITE_AFF_BLOB ){
 99179  99510             static const char zAff[] = "B\000C\000D\000E";
 99180  99511             assert( SQLITE_AFF_BLOB=='A' );
 99181  99512             assert( SQLITE_AFF_TEXT=='B' );
 99182  99513             if( iReg!=target ){
 99183  99514               sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target);
 99184  99515               iReg = target;
................................................................................
 99194  99525             return pExpr->iColumn - pParse->iSelfTab;
 99195  99526           }else{
 99196  99527             /* Coding an expression that is part of an index where column names
 99197  99528             ** in the index refer to the table to which the index belongs */
 99198  99529             iTab = pParse->iSelfTab - 1;
 99199  99530           }
 99200  99531         }
 99201         -      return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
        99532  +      return sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
 99202  99533                                  pExpr->iColumn, iTab, target,
 99203  99534                                  pExpr->op2);
 99204  99535       }
 99205  99536       case TK_INTEGER: {
 99206  99537         codeInteger(pParse, pExpr, 0, target);
 99207  99538         return target;
 99208  99539       }
................................................................................
 99408  99739         u32 constMask = 0;     /* Mask of function arguments that are constant */
 99409  99740         int i;                 /* Loop counter */
 99410  99741         sqlite3 *db = pParse->db;  /* The database connection */
 99411  99742         u8 enc = ENC(db);      /* The text encoding used by this database */
 99412  99743         CollSeq *pColl = 0;    /* A collating sequence */
 99413  99744   
 99414  99745   #ifndef SQLITE_OMIT_WINDOWFUNC
 99415         -      if( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) && pExpr->pWin ){
 99416         -        return pExpr->pWin->regResult;
        99746  +      if( ExprHasProperty(pExpr, EP_WinFunc) ){
        99747  +        return pExpr->y.pWin->regResult;
 99417  99748         }
 99418  99749   #endif
 99419  99750   
 99420  99751         if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
 99421  99752           /* SQL functions can be expensive. So try to move constant functions
 99422  99753           ** out of the inner loop, even if that means an extra OP_Copy. */
 99423  99754           return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
................................................................................
 99652  99983         **
 99653  99984         ** Then p1 is interpreted as follows:
 99654  99985         **
 99655  99986         **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
 99656  99987         **   p1==1   ->    old.a         p1==4   ->    new.a
 99657  99988         **   p1==2   ->    old.b         p1==5   ->    new.b       
 99658  99989         */
 99659         -      Table *pTab = pExpr->pTab;
        99990  +      Table *pTab = pExpr->y.pTab;
 99660  99991         int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
 99661  99992   
 99662  99993         assert( pExpr->iTable==0 || pExpr->iTable==1 );
 99663  99994         assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
 99664  99995         assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
 99665  99996         assert( p1>=0 && p1<(pTab->nCol*2+2) );
 99666  99997   
 99667  99998         sqlite3VdbeAddOp2(v, OP_Param, p1, target);
 99668  99999         VdbeComment((v, "r[%d]=%s.%s", target,
 99669 100000           (pExpr->iTable ? "new" : "old"),
 99670         -        (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName)
       100001  +        (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[pExpr->iColumn].zName)
 99671 100002         ));
 99672 100003   
 99673 100004   #ifndef SQLITE_OMIT_FLOATING_POINT
 99674 100005         /* If the column has REAL affinity, it may currently be stored as an
 99675 100006         ** integer. Use OP_RealAffinity to make sure it is really real.
 99676 100007         **
 99677 100008         ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
................................................................................
100514 100845         return 1;
100515 100846       }
100516 100847       return 2;
100517 100848     }
100518 100849     if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
100519 100850       if( pA->op==TK_FUNCTION ){
100520 100851         if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
       100852  +#ifndef SQLITE_OMIT_WINDOWFUNC
       100853  +      /* Justification for the assert():
       100854  +      ** window functions have p->op==TK_FUNCTION but aggregate functions
       100855  +      ** have p->op==TK_AGG_FUNCTION.  So any comparison between an aggregate
       100856  +      ** function and a window function should have failed before reaching
       100857  +      ** this point.  And, it is not possible to have a window function and
       100858  +      ** a scalar function with the same name and number of arguments.  So
       100859  +      ** if we reach this point, either A and B both window functions or
       100860  +      ** neither are a window functions. */
       100861  +      assert( ExprHasProperty(pA,EP_WinFunc)==ExprHasProperty(pB,EP_WinFunc) );
       100862  +      if( ExprHasProperty(pA,EP_WinFunc) ){
       100863  +        if( sqlite3WindowCompare(pParse,pA->y.pWin,pB->y.pWin)!=0 ) return 2;
       100864  +      }
       100865  +#endif
100521 100866       }else if( pA->op==TK_COLLATE ){
100522 100867         if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
100523 100868       }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
100524 100869         return 2;
100525 100870       }
100526 100871     }
100527 100872     if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
................................................................................
100533 100878       if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
100534 100879       assert( (combinedFlags & EP_Reduced)==0 );
100535 100880       if( pA->op!=TK_STRING && pA->op!=TK_TRUEFALSE ){
100536 100881         if( pA->iColumn!=pB->iColumn ) return 2;
100537 100882         if( pA->iTable!=pB->iTable 
100538 100883          && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
100539 100884       }
100540         -#ifndef SQLITE_OMIT_WINDOWFUNC
100541         -    /* Justification for the assert():
100542         -    ** window functions have p->op==TK_FUNCTION but aggregate functions
100543         -    ** have p->op==TK_AGG_FUNCTION.  So any comparison between an aggregate
100544         -    ** function and a window function should have failed before reaching
100545         -    ** this point.  And, it is not possible to have a window function and
100546         -    ** a scalar function with the same name and number of arguments.  So
100547         -    ** if we reach this point, either A and B both window functions or
100548         -    ** neither are a window functions. */
100549         -    assert( (pA->pWin==0)==(pB->pWin==0) );
100550         -
100551         -    if( pA->pWin!=0 ){
100552         -      if( sqlite3WindowCompare(pParse,pA->pWin,pB->pWin)!=0 ) return 2;
100553         -    }
100554         -#endif
100555 100885     }
100556 100886     return 0;
100557 100887   }
100558 100888   
100559 100889   /*
100560 100890   ** Compare two ExprList objects.  Return 0 if they are identical and 
100561 100891   ** non-zero if they differ in any way.
................................................................................
100688 101018       case TK_GE:
100689 101019         testcase( pExpr->op==TK_EQ );
100690 101020         testcase( pExpr->op==TK_NE );
100691 101021         testcase( pExpr->op==TK_LT );
100692 101022         testcase( pExpr->op==TK_LE );
100693 101023         testcase( pExpr->op==TK_GT );
100694 101024         testcase( pExpr->op==TK_GE );
100695         -      if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->pTab))
100696         -       || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->pTab))
       101025  +      if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->y.pTab))
       101026  +       || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->y.pTab))
100697 101027         ){
100698 101028          return WRC_Prune;
100699 101029         }
100700 101030       default:
100701 101031         return WRC_Continue;
100702 101032     }
100703 101033   }
................................................................................
100920 101250                   break;
100921 101251                 }
100922 101252               }
100923 101253               if( (k>=pAggInfo->nColumn)
100924 101254                && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 
100925 101255               ){
100926 101256                 pCol = &pAggInfo->aCol[k];
100927         -              pCol->pTab = pExpr->pTab;
       101257  +              pCol->pTab = pExpr->y.pTab;
100928 101258                 pCol->iTable = pExpr->iTable;
100929 101259                 pCol->iColumn = pExpr->iColumn;
100930 101260                 pCol->iMem = ++pParse->nMem;
100931 101261                 pCol->iSorterColumn = -1;
100932 101262                 pCol->pExpr = pExpr;
100933 101263                 if( pAggInfo->pGroupBy ){
100934 101264                   int j, n;
................................................................................
101803 102133     }
101804 102134   }
101805 102135   #else
101806 102136   # define renameTokenCheckAll(x,y)
101807 102137   #endif
101808 102138   
101809 102139   /*
101810         -** Add a new RenameToken object mapping parse tree element pPtr into
101811         -** token *pToken to the Parse object currently under construction.
       102140  +** Remember that the parser tree element pPtr was created using
       102141  +** the token pToken.
101812 102142   **
101813         -** Return a copy of pPtr.
       102143  +** In other words, construct a new RenameToken object and add it
       102144  +** to the list of RenameToken objects currently being built up
       102145  +** in pParse->pRename.
       102146  +**
       102147  +** The pPtr argument is returned so that this routine can be used
       102148  +** with tail recursion in tokenExpr() routine, for a small performance
       102149  +** improvement.
101814 102150   */
101815 102151   SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
101816 102152     RenameToken *pNew;
101817 102153     assert( pPtr || pParse->db->mallocFailed );
101818 102154     renameTokenCheckAll(pParse, pPtr);
101819 102155     pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
101820 102156     if( pNew ){
................................................................................
101939 102275     if( pExpr->op==TK_TRIGGER 
101940 102276      && pExpr->iColumn==p->iCol 
101941 102277      && pWalker->pParse->pTriggerTab==p->pTab
101942 102278     ){
101943 102279       renameTokenFind(pWalker->pParse, p, (void*)pExpr);
101944 102280     }else if( pExpr->op==TK_COLUMN 
101945 102281      && pExpr->iColumn==p->iCol 
101946         -   && p->pTab==pExpr->pTab
       102282  +   && p->pTab==pExpr->y.pTab
101947 102283     ){
101948 102284       renameTokenFind(pWalker->pParse, p, (void*)pExpr);
101949 102285     }
101950 102286     return WRC_Continue;
101951 102287   }
101952 102288   
101953 102289   /*
................................................................................
102197 102533     memset(&sNC, 0, sizeof(sNC));
102198 102534     sNC.pParse = pParse;
102199 102535     assert( pNew->pTabSchema );
102200 102536     pParse->pTriggerTab = sqlite3FindTable(db, pNew->table, 
102201 102537         db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
102202 102538     );
102203 102539     pParse->eTriggerOp = pNew->op;
       102540  +  /* ALWAYS() because if the table of the trigger does not exist, the
       102541  +  ** error would have been hit before this point */
       102542  +  if( ALWAYS(pParse->pTriggerTab) ){
       102543  +    rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
       102544  +  }
102204 102545   
102205 102546     /* Resolve symbols in WHEN clause */
102206         -  if( pNew->pWhen ){
       102547  +  if( rc==SQLITE_OK && pNew->pWhen ){
102207 102548       rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
102208 102549     }
102209 102550   
102210 102551     for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
102211 102552       if( pStep->pSelect ){
102212 102553         sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
102213 102554         if( pParse->nErr ) rc = pParse->rc;
................................................................................
102313 102654   **   8. bTemp:    True if zSql comes from temp schema
102314 102655   **
102315 102656   ** Do a column rename operation on the CREATE statement given in zSql.
102316 102657   ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
102317 102658   ** into zNew.  The name should be quoted if bQuote is true.
102318 102659   **
102319 102660   ** This function is used internally by the ALTER TABLE RENAME COLUMN command.
102320         -** Though accessible to application code, it is not intended for use by
102321         -** applications.  The existance of this function, and the way it works,
102322         -** is subject to change without notice.
102323         -**
102324         -** If any of the parameters are out-of-bounds, then simply return NULL.
102325         -** An out-of-bounds parameter can only occur when the application calls
102326         -** this function directly.  The parameters will always be well-formed when
102327         -** this routine is invoked by the bytecode for a legitimate ALTER TABLE
102328         -** statement.
       102661  +** It is only accessible to SQL created using sqlite3NestedParse().  It is
       102662  +** not reachable from ordinary SQL passed into sqlite3_prepare().
102329 102663   */
102330 102664   static void renameColumnFunc(
102331 102665     sqlite3_context *context,
102332 102666     int NotUsed,
102333 102667     sqlite3_value **argv
102334 102668   ){
102335 102669     sqlite3 *db = sqlite3_context_db_handle(context);
................................................................................
102477 102811   }
102478 102812   
102479 102813   /*
102480 102814   ** Walker expression callback used by "RENAME TABLE". 
102481 102815   */
102482 102816   static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
102483 102817     RenameCtx *p = pWalker->u.pRename;
102484         -  if( pExpr->op==TK_COLUMN && p->pTab==pExpr->pTab ){
102485         -    renameTokenFind(pWalker->pParse, p, (void*)&pExpr->pTab);
       102818  +  if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){
       102819  +    renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
102486 102820     }
102487 102821     return WRC_Continue;
102488 102822   }
102489 102823   
102490 102824   /*
102491 102825   ** Walker select callback used by "RENAME TABLE". 
102492 102826   */
................................................................................
102575 102909               sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
102576 102910               if( sParse.nErr ) rc = sParse.rc;
102577 102911               sqlite3WalkSelect(&sWalker, pTab->pSelect);
102578 102912             }
102579 102913           }else{
102580 102914             /* Modify any FK definitions to point to the new table. */
102581 102915   #ifndef SQLITE_OMIT_FOREIGN_KEY
102582         -          if( db->flags & SQLITE_ForeignKeys ){
       102916  +          if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){
102583 102917               FKey *pFKey;
102584 102918               for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
102585 102919                 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
102586 102920                   renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
102587 102921                 }
102588 102922               }
102589 102923             }
................................................................................
102729 103063   }
102730 103064   
102731 103065   /*
102732 103066   ** Register built-in functions used to help implement ALTER TABLE
102733 103067   */
102734 103068   SQLITE_PRIVATE void sqlite3AlterFunctions(void){
102735 103069     static FuncDef aAlterTableFuncs[] = {
102736         -    FUNCTION(sqlite_rename_column,  9, 0, 0, renameColumnFunc),
102737         -    FUNCTION(sqlite_rename_table,  7, 0, 0, renameTableFunc),
102738         -    FUNCTION(sqlite_rename_test,  5, 0, 0, renameTableTest),
       103070  +    INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc),
       103071  +    INTERNAL_FUNCTION(sqlite_rename_table,  7, renameTableFunc),
       103072  +    INTERNAL_FUNCTION(sqlite_rename_test,   5, renameTableTest),
102739 103073     };
102740 103074     sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
102741 103075   }
102742 103076   #endif  /* SQLITE_ALTER_TABLE */
102743 103077   
102744 103078   /************** End of alter.c ***********************************************/
102745 103079   /************** Begin file analyze.c *****************************************/
................................................................................
104780 105114       ** reopen it as a MemDB */
104781 105115       pVfs = sqlite3_vfs_find("memdb");
104782 105116       if( pVfs==0 ) return;
104783 105117       pNew = &db->aDb[db->init.iDb];
104784 105118       if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
104785 105119       pNew->pBt = 0;
104786 105120       pNew->pSchema = 0;
104787         -    rc = sqlite3BtreeOpen(pVfs, "x", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
       105121  +    rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
104788 105122     }else{
104789 105123       /* This is a real ATTACH
104790 105124       **
104791 105125       ** Check for the following errors:
104792 105126       **
104793 105127       **     * Too many attached databases,
104794 105128       **     * Transaction currently open
................................................................................
105460 105794     Table *pTab = 0;      /* The table being read */
105461 105795     const char *zCol;     /* Name of the column of the table */
105462 105796     int iSrc;             /* Index in pTabList->a[] of table being read */
105463 105797     int iDb;              /* The index of the database the expression refers to */
105464 105798     int iCol;             /* Index of column in table */
105465 105799   
105466 105800     assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
       105801  +  assert( !IN_RENAME_OBJECT || db->xAuth==0 );
105467 105802     if( db->xAuth==0 ) return;
105468 105803     iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
105469 105804     if( iDb<0 ){
105470 105805       /* An attempt to read a column out of a subquery or other
105471 105806       ** temporary table. */
105472 105807       return;
105473 105808     }
................................................................................
105516 105851   ){
105517 105852     sqlite3 *db = pParse->db;
105518 105853     int rc;
105519 105854   
105520 105855     /* Don't do any authorization checks if the database is initialising
105521 105856     ** or if the parser is being invoked from within sqlite3_declare_vtab.
105522 105857     */
       105858  +  assert( !IN_RENAME_OBJECT || db->xAuth==0 );
105523 105859     if( db->init.busy || IN_SPECIAL_PARSE ){
105524 105860       return SQLITE_OK;
105525 105861     }
105526 105862   
105527 105863     if( db->xAuth==0 ){
105528 105864       return SQLITE_OK;
105529 105865     }
................................................................................
105939 106275       return 0;
105940 106276     }
105941 106277   
105942 106278     p = sqlite3FindTable(db, zName, zDbase);
105943 106279     if( p==0 ){
105944 106280       const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
105945 106281   #ifndef SQLITE_OMIT_VIRTUALTABLE
105946         -    if( sqlite3FindDbName(db, zDbase)<1 ){
105947         -      /* If zName is the not the name of a table in the schema created using
105948         -      ** CREATE, then check to see if it is the name of an virtual table that
105949         -      ** can be an eponymous virtual table. */
105950         -      Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
105951         -      if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
105952         -        pMod = sqlite3PragmaVtabRegister(db, zName);
105953         -      }
105954         -      if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
105955         -        return pMod->pEpoTab;
105956         -      }
       106282  +    /* If zName is the not the name of a table in the schema created using
       106283  +    ** CREATE, then check to see if it is the name of an virtual table that
       106284  +    ** can be an eponymous virtual table. */
       106285  +    Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
       106286  +    if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
       106287  +      pMod = sqlite3PragmaVtabRegister(db, zName);
       106288  +    }
       106289  +    if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
       106290  +      return pMod->pEpoTab;
105957 106291       }
105958 106292   #endif
105959 106293       if( (flags & LOCATE_NOERR)==0 ){
105960 106294         if( zDbase ){
105961 106295           sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
105962 106296         }else{
105963 106297           sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
................................................................................
106129 106463   /*
106130 106464   ** Erase all schema information from all attached databases (including
106131 106465   ** "main" and "temp") for a single database connection.
106132 106466   */
106133 106467   SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
106134 106468     int i;
106135 106469     sqlite3BtreeEnterAll(db);
106136         -  assert( db->nSchemaLock==0 );
106137 106470     for(i=0; i<db->nDb; i++){
106138 106471       Db *pDb = &db->aDb[i];
106139 106472       if( pDb->pSchema ){
106140         -      sqlite3SchemaClear(pDb->pSchema);
       106473  +      if( db->nSchemaLock==0 ){
       106474  +        sqlite3SchemaClear(pDb->pSchema);
       106475  +      }else{
       106476  +        DbSetProperty(db, i, DB_ResetWanted);
       106477  +      }
106141 106478       }
106142 106479     }
106143 106480     db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
106144 106481     sqlite3VtabUnlockList(db);
106145 106482     sqlite3BtreeLeaveAll(db);
106146         -  sqlite3CollapseDatabaseArray(db);
       106483  +  if( db->nSchemaLock==0 ){
       106484  +    sqlite3CollapseDatabaseArray(db);
       106485  +  }
106147 106486   }
106148 106487   
106149 106488   /*
106150 106489   ** This routine is called when a commit occurs.
106151 106490   */
106152 106491   SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
106153 106492     db->mDbFlags &= ~DBFLAG_SchemaChange;
................................................................................
106216 106555     }
106217 106556   
106218 106557     /* Delete any foreign keys attached to this table. */
106219 106558     sqlite3FkDelete(db, pTable);
106220 106559   
106221 106560     /* Delete the Table structure itself.
106222 106561     */
       106562  +#ifdef SQLITE_ENABLE_NORMALIZE
       106563  +  if( pTable->pColHash ){
       106564  +    sqlite3HashClear(pTable->pColHash);
       106565  +    sqlite3_free(pTable->pColHash);
       106566  +  }
       106567  +#endif
106223 106568     sqlite3DeleteColumnNames(db, pTable);
106224 106569     sqlite3DbFree(db, pTable->zName);
106225 106570     sqlite3DbFree(db, pTable->zColAff);
106226 106571     sqlite3SelectDelete(db, pTable->pSelect);
106227 106572     sqlite3ExprListDelete(db, pTable->pCheck);
106228 106573   #ifndef SQLITE_OMIT_VIRTUALTABLE
106229 106574     sqlite3VtabClear(db, pTable);
................................................................................
106373 106718       assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT
106374 106719                || (db->mDbFlags & DBFLAG_Vacuum)!=0);
106375 106720       iDb = db->init.iDb;
106376 106721       *pUnqual = pName1;
106377 106722     }
106378 106723     return iDb;
106379 106724   }
       106725  +
       106726  +/*
       106727  +** True if PRAGMA writable_schema is ON
       106728  +*/
       106729  +SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3 *db){
       106730  +  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
       106731  +  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
       106732  +               SQLITE_WriteSchema );
       106733  +  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
       106734  +               SQLITE_Defensive );
       106735  +  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
       106736  +               (SQLITE_WriteSchema|SQLITE_Defensive) );
       106737  +  return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
       106738  +}
106380 106739   
106381 106740   /*
106382 106741   ** This routine is used to check if the UTF-8 string zName is a legal
106383 106742   ** unqualified name for a new schema object (table, index, view or
106384 106743   ** trigger). All names are legal except those that begin with the string
106385 106744   ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
106386 106745   ** is reserved for internal use.
106387 106746   */
106388 106747   SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
106389 106748     if( !pParse->db->init.busy && pParse->nested==0 
106390         -          && (pParse->db->flags & SQLITE_WriteSchema)==0
       106749  +          && sqlite3WritableSchema(pParse->db)==0
106391 106750             && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
106392 106751       sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
106393 106752       return SQLITE_ERROR;
106394 106753     }
106395 106754     return SQLITE_OK;
106396 106755   }
106397 106756   
................................................................................
107459 107818       assert( pTab->nCol==j );
107460 107819     }else{
107461 107820       pPk->nColumn = pTab->nCol;
107462 107821     }
107463 107822     recomputeColumnsNotIndexed(pPk);
107464 107823   }
107465 107824   
       107825  +#ifndef SQLITE_OMIT_VIRTUALTABLE
       107826  +/*
       107827  +** Return true if zName is a shadow table name in the current database
       107828  +** connection.
       107829  +**
       107830  +** zName is temporarily modified while this routine is running, but is
       107831  +** restored to its original value prior to this routine returning.
       107832  +*/
       107833  +static int isShadowTableName(sqlite3 *db, char *zName){
       107834  +  char *zTail;                  /* Pointer to the last "_" in zName */
       107835  +  Table *pTab;                  /* Table that zName is a shadow of */
       107836  +  Module *pMod;                 /* Module for the virtual table */
       107837  +
       107838  +  zTail = strrchr(zName, '_');
       107839  +  if( zTail==0 ) return 0;
       107840  +  *zTail = 0;
       107841  +  pTab = sqlite3FindTable(db, zName, 0);
       107842  +  *zTail = '_';
       107843  +  if( pTab==0 ) return 0;
       107844  +  if( !IsVirtual(pTab) ) return 0;
       107845  +  pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]);
       107846  +  if( pMod==0 ) return 0;
       107847  +  if( pMod->pModule->iVersion<3 ) return 0;
       107848  +  if( pMod->pModule->xShadowName==0 ) return 0;
       107849  +  return pMod->pModule->xShadowName(zTail+1);
       107850  +}
       107851  +#else
       107852  +# define isShadowTableName(x,y) 0
       107853  +#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
       107854  +
107466 107855   /*
107467 107856   ** This routine is called to report the final ")" that terminates
107468 107857   ** a CREATE TABLE statement.
107469 107858   **
107470 107859   ** The table structure that other action routines have been building
107471 107860   ** is added to the internal hash tables, assuming no errors have
107472 107861   ** occurred.
................................................................................
107497 107886   
107498 107887     if( pEnd==0 && pSelect==0 ){
107499 107888       return;
107500 107889     }
107501 107890     assert( !db->mallocFailed );
107502 107891     p = pParse->pNewTable;
107503 107892     if( p==0 ) return;
       107893  +
       107894  +  if( pSelect==0 && isShadowTableName(db, p->zName) ){
       107895  +    p->tabFlags |= TF_Shadow;
       107896  +  }
107504 107897   
107505 107898     /* If the db->init.busy is 1 it means we are reading the SQL off the
107506 107899     ** "sqlite_master" or "sqlite_temp_master" table on the disk.
107507 107900     ** So do not write to the disk again.  Extract the root page number
107508 107901     ** for the table from the db->init.newTnum field.  (The page number
107509 107902     ** should have been put there by the sqliteOpenCb routine.)
107510 107903     **
................................................................................
108005 108398   ** Also write code to modify the sqlite_master table and internal schema
108006 108399   ** if a root-page of another table is moved by the btree-layer whilst
108007 108400   ** erasing iTable (this can happen with an auto-vacuum database).
108008 108401   */ 
108009 108402   static void destroyRootPage(Parse *pParse, int iTable, int iDb){
108010 108403     Vdbe *v = sqlite3GetVdbe(pParse);
108011 108404     int r1 = sqlite3GetTempReg(pParse);
108012         -  assert( iTable>1 );
       108405  +  if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema");
108013 108406     sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
108014 108407     sqlite3MayAbort(pParse);
108015 108408   #ifndef SQLITE_OMIT_AUTOVACUUM
108016 108409     /* OP_Destroy stores an in integer r1. If this integer
108017 108410     ** is non-zero, then it is the root page number of a table moved to
108018 108411     ** location iTable. The following code modifies the sqlite_master table to
108019 108412     ** reflect this.
................................................................................
110460 110853     for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
110461 110854       if( sqlite3StrICmp(p->zName, zFunc)==0 ){
110462 110855         return p;
110463 110856       }
110464 110857     }
110465 110858     return 0;
110466 110859   }
       110860  +#ifdef SQLITE_ENABLE_NORMALIZE
       110861  +SQLITE_PRIVATE FuncDef *sqlite3FunctionSearchN(
       110862  +  int h,               /* Hash of the name */
       110863  +  const char *zFunc,   /* Name of function */
       110864  +  int nFunc            /* Length of the name */
       110865  +){
       110866  +  FuncDef *p;
       110867  +  for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
       110868  +    if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 ){
       110869  +      return p;
       110870  +    }
       110871  +  }
       110872  +  return 0;
       110873  +}
       110874  +#endif /* SQLITE_ENABLE_NORMALIZE */
110467 110875   
110468 110876   /*
110469 110877   ** Insert a new FuncDef into a FuncDefHash hash table.
110470 110878   */
110471 110879   SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(
110472 110880     FuncDef *aDef,      /* List of global functions to be inserted */
110473 110881     int nDef            /* Length of the apDef[] list */
110474 110882   ){
110475 110883     int i;
110476 110884     for(i=0; i<nDef; i++){
110477 110885       FuncDef *pOther;
110478 110886       const char *zName = aDef[i].zName;
110479 110887       int nName = sqlite3Strlen30(zName);
110480         -    int h = (zName[0] + nName) % SQLITE_FUNC_HASH_SZ;
       110888  +    int h = SQLITE_FUNC_HASH(zName[0], nName);
110481 110889       assert( zName[0]>='a' && zName[0]<='z' );
110482 110890       pOther = functionSearch(h, zName);
110483 110891       if( pOther ){
110484 110892         assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
110485 110893         aDef[i].pNext = pOther->pNext;
110486 110894         pOther->pNext = &aDef[i];
110487 110895       }else{
................................................................................
110552 110960     ** install a new function.  Whatever FuncDef structure is returned it will
110553 110961     ** have fields overwritten with new information appropriate for the
110554 110962     ** new function.  But the FuncDefs for built-in functions are read-only.
110555 110963     ** So we must not search for built-ins when creating a new function.
110556 110964     */ 
110557 110965     if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
110558 110966       bestScore = 0;
110559         -    h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % SQLITE_FUNC_HASH_SZ;
       110967  +    h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
110560 110968       p = functionSearch(h, zName);
110561 110969       while( p ){
110562 110970         int score = matchQuality(p, nArg, enc);
110563 110971         if( score>bestScore ){
110564 110972           pBest = p;
110565 110973           bestScore = score;
110566 110974         }
................................................................................
110699 111107       pTab->nTabRef++;
110700 111108     }
110701 111109     if( sqlite3IndexedByLookup(pParse, pItem) ){
110702 111110       pTab = 0;
110703 111111     }
110704 111112     return pTab;
110705 111113   }
       111114  +
       111115  +/* Return true if table pTab is read-only.
       111116  +**
       111117  +** A table is read-only if any of the following are true:
       111118  +**
       111119  +**   1) It is a virtual table and no implementation of the xUpdate method
       111120  +**      has been provided
       111121  +**
       111122  +**   2) It is a system table (i.e. sqlite_master), this call is not
       111123  +**      part of a nested parse and writable_schema pragma has not 
       111124  +**      been specified
       111125  +**
       111126  +**   3) The table is a shadow table, the database connection is in
       111127  +**      defensive mode, and the current sqlite3_prepare()
       111128  +**      is for a top-level SQL statement.
       111129  +*/
       111130  +static int tabIsReadOnly(Parse *pParse, Table *pTab){
       111131  +  sqlite3 *db;
       111132  +  if( IsVirtual(pTab) ){
       111133  +    return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
       111134  +  }
       111135  +  if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
       111136  +  db = pParse->db;
       111137  +  if( (pTab->tabFlags & TF_Readonly)!=0 ){
       111138  +    return sqlite3WritableSchema(db)==0 && pParse->nested==0;
       111139  +  }
       111140  +  assert( pTab->tabFlags & TF_Shadow );
       111141  +  return (db->flags & SQLITE_Defensive)!=0 
       111142  +#ifndef SQLITE_OMIT_VIRTUALTABLE
       111143  +          && db->pVtabCtx==0
       111144  +#endif
       111145  +          && db->nVdbeExec==0;
       111146  +}
110706 111147   
110707 111148   /*
110708 111149   ** Check to make sure the given table is writable.  If it is not
110709 111150   ** writable, generate an error message and return 1.  If it is
110710 111151   ** writable return 0;
110711 111152   */
110712 111153   SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
110713         -  /* A table is not writable under the following circumstances:
110714         -  **
110715         -  **   1) It is a virtual table and no implementation of the xUpdate method
110716         -  **      has been provided, or
110717         -  **   2) It is a system table (i.e. sqlite_master), this call is not
110718         -  **      part of a nested parse and writable_schema pragma has not 
110719         -  **      been specified.
110720         -  **
110721         -  ** In either case leave an error message in pParse and return non-zero.
110722         -  */
110723         -  if( ( IsVirtual(pTab) 
110724         -     && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
110725         -   || ( (pTab->tabFlags & TF_Readonly)!=0
110726         -     && (pParse->db->flags & SQLITE_WriteSchema)==0
110727         -     && pParse->nested==0 )
110728         -  ){
       111154  +  if( tabIsReadOnly(pParse, pTab) ){
110729 111155       sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
110730 111156       return 1;
110731 111157     }
110732         -
110733 111158   #ifndef SQLITE_OMIT_VIEW
110734 111159     if( !viewOk && pTab->pSelect ){
110735 111160       sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
110736 111161       return 1;
110737 111162     }
110738 111163   #endif
110739 111164     return 0;
................................................................................
114129 114554     sqlite3 *db,      /* The database connection */
114130 114555     Table *pTab,      /* The table whose column is desired */
114131 114556     int iCursor,      /* The open cursor on the table */
114132 114557     i16 iCol          /* The column that is wanted */
114133 114558   ){
114134 114559     Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
114135 114560     if( pExpr ){
114136         -    pExpr->pTab = pTab;
       114561  +    pExpr->y.pTab = pTab;
114137 114562       pExpr->iTable = iCursor;
114138 114563       pExpr->iColumn = iCol;
114139 114564     }
114140 114565     return pExpr;
114141 114566   }
114142 114567   
114143 114568   /*
................................................................................
115205 115630         zColAff[i] = pTab->aCol[i].affinity;
115206 115631       }
115207 115632       do{
115208 115633         zColAff[i--] = 0;
115209 115634       }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
115210 115635       pTab->zColAff = zColAff;
115211 115636     }
115212         -  i = sqlite3Strlen30(zColAff);
       115637  +  assert( zColAff!=0 );
       115638  +  i = sqlite3Strlen30NN(zColAff);
115213 115639     if( i ){
115214 115640       if( iReg ){
115215 115641         sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
115216 115642       }else{
115217 115643         sqlite3VdbeChangeP4(v, -1, zColAff, i);
115218 115644       }
115219 115645     }
................................................................................
116185 116611    #undef pTrigger
116186 116612   #endif
116187 116613   #ifdef tmask
116188 116614    #undef tmask
116189 116615   #endif
116190 116616   
116191 116617   /*
116192         -** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged()
       116618  +** Meanings of bits in of pWalker->eCode for 
       116619  +** sqlite3ExprReferencesUpdatedColumn()
116193 116620   */
116194 116621   #define CKCNSTRNT_COLUMN   0x01    /* CHECK constraint uses a changing column */
116195 116622   #define CKCNSTRNT_ROWID    0x02    /* CHECK constraint references the ROWID */
116196 116623   
116197         -/* This is the Walker callback from checkConstraintUnchanged().  Set
116198         -** bit 0x01 of pWalker->eCode if
116199         -** pWalker->eCode to 0 if this expression node references any of the
       116624  +/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
       116625  +*  Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
       116626  +** expression node references any of the
116200 116627   ** columns that are being modifed by an UPDATE statement.
116201 116628   */
116202 116629   static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
116203 116630     if( pExpr->op==TK_COLUMN ){
116204 116631       assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
116205 116632       if( pExpr->iColumn>=0 ){
116206 116633         if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
................................................................................
116214 116641   }
116215 116642   
116216 116643   /*
116217 116644   ** pExpr is a CHECK constraint on a row that is being UPDATE-ed.  The
116218 116645   ** only columns that are modified by the UPDATE are those for which
116219 116646   ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
116220 116647   **
116221         -** Return true if CHECK constraint pExpr does not use any of the
       116648  +** Return true if CHECK constraint pExpr uses any of the
116222 116649   ** changing columns (or the rowid if it is changing).  In other words,
116223         -** return true if this CHECK constraint can be skipped when validating
       116650  +** return true if this CHECK constraint must be validated for
116224 116651   ** the new row in the UPDATE statement.
       116652  +**
       116653  +** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
       116654  +** The operation of this routine is the same - return true if an only if
       116655  +** the expression uses one or more of columns identified by the second and
       116656  +** third arguments.
116225 116657   */
116226         -static int checkConstraintUnchanged(Expr *pExpr, int *aiChng, int chngRowid){
       116658  +SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(
       116659  +  Expr *pExpr,    /* The expression to be checked */
       116660  +  int *aiChng,    /* aiChng[x]>=0 if column x changed by the UPDATE */
       116661  +  int chngRowid   /* True if UPDATE changes the rowid */
       116662  +){
116227 116663     Walker w;
116228 116664     memset(&w, 0, sizeof(w));
116229 116665     w.eCode = 0;
116230 116666     w.xExprCallback = checkConstraintExprNode;
116231 116667     w.u.aiCol = aiChng;
116232 116668     sqlite3WalkExpr(&w, pExpr);
116233 116669     if( !chngRowid ){
................................................................................
116234 116670       testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
116235 116671       w.eCode &= ~CKCNSTRNT_ROWID;
116236 116672     }
116237 116673     testcase( w.eCode==0 );
116238 116674     testcase( w.eCode==CKCNSTRNT_COLUMN );
116239 116675     testcase( w.eCode==CKCNSTRNT_ROWID );
116240 116676     testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
116241         -  return !w.eCode;
       116677  +  return w.eCode!=0;
116242 116678   }
116243 116679   
116244 116680   /*
116245 116681   ** Generate code to do constraint checks prior to an INSERT or an UPDATE
116246 116682   ** on table pTab.
116247 116683   **
116248 116684   ** The regNewData parameter is the first register in a range that contains
................................................................................
116440 116876     if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
116441 116877       ExprList *pCheck = pTab->pCheck;
116442 116878       pParse->iSelfTab = -(regNewData+1);
116443 116879       onError = overrideError!=OE_Default ? overrideError : OE_Abort;
116444 116880       for(i=0; i<pCheck->nExpr; i++){
116445 116881         int allOk;
116446 116882         Expr *pExpr = pCheck->a[i].pExpr;
116447         -      if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue;
       116883  +      if( aiChng
       116884  +       && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
       116885  +      ){
       116886  +        /* The check constraints do not reference any of the columns being
       116887  +        ** updated so there is no point it verifying the check constraint */
       116888  +        continue;
       116889  +      }
116448 116890         allOk = sqlite3VdbeMakeLabel(v);
116449 116891         sqlite3VdbeVerifyAbortable(v, onError);
116450 116892         sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
116451 116893         if( onError==OE_Ignore ){
116452 116894           sqlite3VdbeGoto(v, ignoreDest);
116453 116895         }else{
116454 116896           char *zName = pCheck->a[i].zName;
................................................................................
117941 118383     void (*str_append)(sqlite3_str*, const char *zIn, int N);
117942 118384     void (*str_appendall)(sqlite3_str*, const char *zIn);
117943 118385     void (*str_appendchar)(sqlite3_str*, int N, char C);
117944 118386     void (*str_reset)(sqlite3_str*);
117945 118387     int (*str_errcode)(sqlite3_str*);
117946 118388     int (*str_length)(sqlite3_str*);
117947 118389     char *(*str_value)(sqlite3_str*);
       118390  +  /* Version 3.25.0 and later */
117948 118391     int (*create_window_function)(sqlite3*,const char*,int,int,void*,
117949 118392                               void (*xStep)(sqlite3_context*,int,sqlite3_value**),
117950 118393                               void (*xFinal)(sqlite3_context*),
117951 118394                               void (*xValue)(sqlite3_context*),
117952 118395                               void (*xInv)(sqlite3_context*,int,sqlite3_value**),
117953 118396                               void(*xDestroy)(void*));
       118397  +  /* Version 3.26.0 and later */
       118398  +  const char *(*normalized_sql)(sqlite3_stmt*);
117954 118399   };
117955 118400   
117956 118401   /*
117957 118402   ** This is the function signature used for all extension entry points.  It
117958 118403   ** is also defined in the file "loadext.c".
117959 118404   */
117960 118405   typedef int (*sqlite3_loadext_entry)(
................................................................................
118234 118679   #define sqlite3_str_appendchar         sqlite3_api->str_appendchar
118235 118680   #define sqlite3_str_reset              sqlite3_api->str_reset
118236 118681   #define sqlite3_str_errcode            sqlite3_api->str_errcode
118237 118682   #define sqlite3_str_length             sqlite3_api->str_length
118238 118683   #define sqlite3_str_value              sqlite3_api->str_value
118239 118684   /* Version 3.25.0 and later */
118240 118685   #define sqlite3_create_window_function sqlite3_api->create_window_function
       118686  +/* Version 3.26.0 and later */
       118687  +#define sqlite3_normalized_sql         sqlite3_api->normalized_sql
118241 118688   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
118242 118689   
118243 118690   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
118244 118691     /* This case when the file really is being compiled as a loadable 
118245 118692     ** extension */
118246 118693   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
118247 118694   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
................................................................................
118322 118769   
118323 118770   #ifdef SQLITE_OMIT_VIRTUALTABLE
118324 118771   # define sqlite3_create_module 0
118325 118772   # define sqlite3_create_module_v2 0
118326 118773   # define sqlite3_declare_vtab 0
118327 118774   # define sqlite3_vtab_config 0
118328 118775   # define sqlite3_vtab_on_conflict 0
       118776  +# define sqlite3_vtab_collation 0
118329 118777   #endif
118330 118778   
118331 118779   #ifdef SQLITE_OMIT_SHARED_CACHE
118332 118780   # define sqlite3_enable_shared_cache 0
118333 118781   #endif
118334 118782   
118335 118783   #if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
................................................................................
118689 119137     sqlite3_str_appendall,
118690 119138     sqlite3_str_appendchar,
118691 119139     sqlite3_str_reset,
118692 119140     sqlite3_str_errcode,
118693 119141     sqlite3_str_length,
118694 119142     sqlite3_str_value,
118695 119143     /* Version 3.25.0 and later */
118696         -  sqlite3_create_window_function
       119144  +  sqlite3_create_window_function,
       119145  +  /* Version 3.26.0 and later */
       119146  +#ifdef SQLITE_ENABLE_NORMALIZE
       119147  +  sqlite3_normalized_sql
       119148  +#else
       119149  +  0
       119150  +#endif
118697 119151   };
118698 119152   
118699 119153   /*
118700 119154   ** Attempt to load an SQLite extension library contained in the file
118701 119155   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
118702 119156   ** default entry point name (sqlite3_extension_init) is used.  Use
118703 119157   ** of the default name is recommended.
................................................................................
119139 119593   #define PragTyp_TEMP_STORE_DIRECTORY          36
119140 119594   #define PragTyp_THREADS                       37
119141 119595   #define PragTyp_WAL_AUTOCHECKPOINT            38
119142 119596   #define PragTyp_WAL_CHECKPOINT                39
119143 119597   #define PragTyp_ACTIVATE_EXTENSIONS           40
119144 119598   #define PragTyp_HEXKEY                        41
119145 119599   #define PragTyp_KEY                           42
119146         -#define PragTyp_REKEY                         43
119147         -#define PragTyp_LOCK_STATUS                   44
119148         -#define PragTyp_PARSER_TRACE                  45
119149         -#define PragTyp_STATS                         46
       119600  +#define PragTyp_LOCK_STATUS                   43
       119601  +#define PragTyp_PARSER_TRACE                  44
       119602  +#define PragTyp_STATS                         45
119150 119603   
119151 119604   /* Property flags associated with various pragma. */
119152 119605   #define PragFlg_NeedSchema 0x01 /* Force schema load before running */
119153 119606   #define PragFlg_NoColumns  0x02 /* OP_ResultRow called with zero columns */
119154 119607   #define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */
119155 119608   #define PragFlg_ReadOnly   0x08 /* Read-only HEADER_VALUE */
119156 119609   #define PragFlg_Result0    0x10 /* Acts as query when no argument */
................................................................................
119159 119612   #define PragFlg_SchemaReq  0x80 /* Schema required - "main" is default */
119160 119613   
119161 119614   /* Names of columns for pragmas that return multi-column result
119162 119615   ** or that return single-column results where the name of the
119163 119616   ** result column is different from the name of the pragma
119164 119617   */
119165 119618   static const char *const pragCName[] = {
119166         -  /*   0 */ "cache_size",  /* Used by: default_cache_size */
119167         -  /*   1 */ "cid",         /* Used by: table_info */
119168         -  /*   2 */ "name",       
119169         -  /*   3 */ "type",       
119170         -  /*   4 */ "notnull",    
119171         -  /*   5 */ "dflt_value", 
119172         -  /*   6 */ "pk",         
119173         -  /*   7 */ "tbl",         /* Used by: stats */
119174         -  /*   8 */ "idx",        
119175         -  /*   9 */ "wdth",       
119176         -  /*  10 */ "hght",       
119177         -  /*  11 */ "flgs",       
119178         -  /*  12 */ "seqno",       /* Used by: index_info */
119179         -  /*  13 */ "cid",        
119180         -  /*  14 */ "name",       
       119619  +  /*   0 */ "id",          /* Used by: foreign_key_list */
       119620  +  /*   1 */ "seq",        
       119621  +  /*   2 */ "table",      
       119622  +  /*   3 */ "from",       
       119623  +  /*   4 */ "to",         
       119624  +  /*   5 */ "on_update",  
       119625  +  /*   6 */ "on_delete",  
       119626  +  /*   7 */ "match",      
       119627  +  /*   8 */ "cid",         /* Used by: table_xinfo */
       119628  +  /*   9 */ "name",       
       119629  +  /*  10 */ "type",       
       119630  +  /*  11 */ "notnull",    
       119631  +  /*  12 */ "dflt_value", 
       119632  +  /*  13 */ "pk",         
       119633  +  /*  14 */ "hidden",     
       119634  +                           /* table_info reuses 8 */
119181 119635     /*  15 */ "seqno",       /* Used by: index_xinfo */
119182 119636     /*  16 */ "cid",        
119183 119637     /*  17 */ "name",       
119184 119638     /*  18 */ "desc",       
119185 119639     /*  19 */ "coll",       
119186 119640     /*  20 */ "key",        
119187         -  /*  21 */ "seq",         /* Used by: index_list */
119188         -  /*  22 */ "name",       
119189         -  /*  23 */ "unique",     
119190         -  /*  24 */ "origin",     
119191         -  /*  25 */ "partial",    
119192         -  /*  26 */ "seq",         /* Used by: database_list */
       119641  +  /*  21 */ "tbl",         /* Used by: stats */
       119642  +  /*  22 */ "idx",        
       119643  +  /*  23 */ "wdth",       
       119644  +  /*  24 */ "hght",       
       119645  +  /*  25 */ "flgs",       
       119646  +  /*  26 */ "seq",         /* Used by: index_list */
119193 119647     /*  27 */ "name",       
119194         -  /*  28 */ "file",       
119195         -  /*  29 */ "name",        /* Used by: function_list */
119196         -  /*  30 */ "builtin",    
119197         -  /*  31 */ "name",        /* Used by: module_list pragma_list */
119198         -  /*  32 */ "seq",         /* Used by: collation_list */
119199         -  /*  33 */ "name",       
119200         -  /*  34 */ "id",          /* Used by: foreign_key_list */
119201         -  /*  35 */ "seq",        
119202         -  /*  36 */ "table",      
119203         -  /*  37 */ "from",       
119204         -  /*  38 */ "to",         
119205         -  /*  39 */ "on_update",  
119206         -  /*  40 */ "on_delete",  
119207         -  /*  41 */ "match",      
119208         -  /*  42 */ "table",       /* Used by: foreign_key_check */
119209         -  /*  43 */ "rowid",      
119210         -  /*  44 */ "parent",     
119211         -  /*  45 */ "fkid",       
119212         -  /*  46 */ "busy",        /* Used by: wal_checkpoint */
119213         -  /*  47 */ "log",        
119214         -  /*  48 */ "checkpointed",
119215         -  /*  49 */ "timeout",     /* Used by: busy_timeout */
119216         -  /*  50 */ "database",    /* Used by: lock_status */
119217         -  /*  51 */ "status",     
       119648  +  /*  28 */ "unique",     
       119649  +  /*  29 */ "origin",     
       119650  +  /*  30 */ "partial",    
       119651  +  /*  31 */ "table",       /* Used by: foreign_key_check */
       119652  +  /*  32 */ "rowid",      
       119653  +  /*  33 */ "parent",     
       119654  +  /*  34 */ "fkid",       
       119655  +                           /* index_info reuses 15 */
       119656  +  /*  35 */ "seq",         /* Used by: database_list */
       119657  +  /*  36 */ "name",       
       119658  +  /*  37 */ "file",       
       119659  +  /*  38 */ "busy",        /* Used by: wal_checkpoint */
       119660  +  /*  39 */ "log",        
       119661  +  /*  40 */ "checkpointed",
       119662  +  /*  41 */ "name",        /* Used by: function_list */
       119663  +  /*  42 */ "builtin",    
       119664  +                           /* collation_list reuses 26 */
       119665  +  /*  43 */ "database",    /* Used by: lock_status */
       119666  +  /*  44 */ "status",     
       119667  +  /*  45 */ "cache_size",  /* Used by: default_cache_size */
       119668  +                           /* module_list pragma_list reuses 9 */
       119669  +  /*  46 */ "timeout",     /* Used by: busy_timeout */
119218 119670   };
119219 119671   
119220 119672   /* Definitions of all built-in pragmas */
119221 119673   typedef struct PragmaName {
119222 119674     const char *const zName; /* Name of pragma */
119223 119675     u8 ePragTyp;             /* PragTyp_XXX value */
119224 119676     u8 mPragFlg;             /* Zero or more PragFlg_XXX values */
119225 119677     u8 iPragCName;           /* Start of column names in pragCName[] */
119226 119678     u8 nPragCName;           /* Num of col names. 0 means use pragma name */
119227         -  u32 iArg;                /* Extra argument */
       119679  +  u64 iArg;                /* Extra argument */
119228 119680   } PragmaName;
119229 119681   static const PragmaName aPragmaName[] = {
119230 119682   #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
119231 119683    {/* zName:     */ "activate_extensions",
119232 119684     /* ePragTyp:  */ PragTyp_ACTIVATE_EXTENSIONS,
119233 119685     /* ePragFlg:  */ 0,
119234 119686     /* ColNames:  */ 0, 0,
................................................................................
119256 119708     /* ColNames:  */ 0, 0,
119257 119709     /* iArg:      */ SQLITE_AutoIndex },
119258 119710   #endif
119259 119711   #endif
119260 119712    {/* zName:     */ "busy_timeout",
119261 119713     /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
119262 119714     /* ePragFlg:  */ PragFlg_Result0,
119263         -  /* ColNames:  */ 49, 1,
       119715  +  /* ColNames:  */ 46, 1,
119264 119716     /* iArg:      */ 0 },
119265 119717   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119266 119718    {/* zName:     */ "cache_size",
119267 119719     /* ePragTyp:  */ PragTyp_CACHE_SIZE,
119268 119720     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119269 119721     /* ColNames:  */ 0, 0,
119270 119722     /* iArg:      */ 0 },
................................................................................
119293 119745     /* ColNames:  */ 0, 0,
119294 119746     /* iArg:      */ SQLITE_CkptFullFSync },
119295 119747   #endif
119296 119748   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119297 119749    {/* zName:     */ "collation_list",
119298 119750     /* ePragTyp:  */ PragTyp_COLLATION_LIST,
119299 119751     /* ePragFlg:  */ PragFlg_Result0,
119300         -  /* ColNames:  */ 32, 2,
       119752  +  /* ColNames:  */ 26, 2,
119301 119753     /* iArg:      */ 0 },
119302 119754   #endif
119303 119755   #if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
119304 119756    {/* zName:     */ "compile_options",
119305 119757     /* ePragTyp:  */ PragTyp_COMPILE_OPTIONS,
119306 119758     /* ePragFlg:  */ PragFlg_Result0,
119307 119759     /* ColNames:  */ 0, 0,
................................................................................
119328 119780     /* ColNames:  */ 0, 0,
119329 119781     /* iArg:      */ BTREE_DATA_VERSION },
119330 119782   #endif
119331 119783   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119332 119784    {/* zName:     */ "database_list",
119333 119785     /* ePragTyp:  */ PragTyp_DATABASE_LIST,
119334 119786     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0,
119335         -  /* ColNames:  */ 26, 3,
       119787  +  /* ColNames:  */ 35, 3,
119336 119788     /* iArg:      */ 0 },
119337 119789   #endif
119338 119790   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
119339 119791    {/* zName:     */ "default_cache_size",
119340 119792     /* ePragTyp:  */ PragTyp_DEFAULT_CACHE_SIZE,
119341 119793     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119342         -  /* ColNames:  */ 0, 1,
       119794  +  /* ColNames:  */ 45, 1,
119343 119795     /* iArg:      */ 0 },
119344 119796   #endif
119345 119797   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119346 119798   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119347 119799    {/* zName:     */ "defer_foreign_keys",
119348 119800     /* ePragTyp:  */ PragTyp_FLAG,
119349 119801     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
................................................................................
119365 119817     /* ColNames:  */ 0, 0,
119366 119818     /* iArg:      */ 0 },
119367 119819   #endif
119368 119820   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119369 119821    {/* zName:     */ "foreign_key_check",
119370 119822     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_CHECK,
119371 119823     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0,
119372         -  /* ColNames:  */ 42, 4,
       119824  +  /* ColNames:  */ 31, 4,
119373 119825     /* iArg:      */ 0 },
119374 119826   #endif
119375 119827   #if !defined(SQLITE_OMIT_FOREIGN_KEY)
119376 119828    {/* zName:     */ "foreign_key_list",
119377 119829     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_LIST,
119378 119830     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119379         -  /* ColNames:  */ 34, 8,
       119831  +  /* ColNames:  */ 0, 8,
119380 119832     /* iArg:      */ 0 },
119381 119833   #endif
119382 119834   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119383 119835   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119384 119836    {/* zName:     */ "foreign_keys",
119385 119837     /* ePragTyp:  */ PragTyp_FLAG,
119386 119838     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
................................................................................
119408 119860     /* iArg:      */ SQLITE_FullFSync },
119409 119861   #endif
119410 119862   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119411 119863   #if defined(SQLITE_INTROSPECTION_PRAGMAS)
119412 119864    {/* zName:     */ "function_list",
119413 119865     /* ePragTyp:  */ PragTyp_FUNCTION_LIST,
119414 119866     /* ePragFlg:  */ PragFlg_Result0,
119415         -  /* ColNames:  */ 29, 2,
       119867  +  /* ColNames:  */ 41, 2,
119416 119868     /* iArg:      */ 0 },
119417 119869   #endif
119418 119870   #endif
119419 119871   #if defined(SQLITE_HAS_CODEC)
119420 119872    {/* zName:     */ "hexkey",
119421 119873     /* ePragTyp:  */ PragTyp_HEXKEY,
119422 119874     /* ePragFlg:  */ 0,
119423 119875     /* ColNames:  */ 0, 0,
119424         -  /* iArg:      */ 0 },
       119876  +  /* iArg:      */ 2 },
119425 119877    {/* zName:     */ "hexrekey",
119426 119878     /* ePragTyp:  */ PragTyp_HEXKEY,
119427 119879     /* ePragFlg:  */ 0,
119428 119880     /* ColNames:  */ 0, 0,
119429         -  /* iArg:      */ 0 },
       119881  +  /* iArg:      */ 3 },
119430 119882   #endif
119431 119883   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119432 119884   #if !defined(SQLITE_OMIT_CHECK)
119433 119885    {/* zName:     */ "ignore_check_constraints",
119434 119886     /* ePragTyp:  */ PragTyp_FLAG,
119435 119887     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119436 119888     /* ColNames:  */ 0, 0,
................................................................................
119444 119896     /* ColNames:  */ 0, 0,
119445 119897     /* iArg:      */ 0 },
119446 119898   #endif
119447 119899   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119448 119900    {/* zName:     */ "index_info",
119449 119901     /* ePragTyp:  */ PragTyp_INDEX_INFO,
119450 119902     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119451         -  /* ColNames:  */ 12, 3,
       119903  +  /* ColNames:  */ 15, 3,
119452 119904     /* iArg:      */ 0 },
119453 119905    {/* zName:     */ "index_list",
119454 119906     /* ePragTyp:  */ PragTyp_INDEX_LIST,
119455 119907     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119456         -  /* ColNames:  */ 21, 5,
       119908  +  /* ColNames:  */ 26, 5,
119457 119909     /* iArg:      */ 0 },
119458 119910    {/* zName:     */ "index_xinfo",
119459 119911     /* ePragTyp:  */ PragTyp_INDEX_INFO,
119460 119912     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119461 119913     /* ColNames:  */ 15, 6,
119462 119914     /* iArg:      */ 1 },
119463 119915   #endif
................................................................................
119506 119958     /* ColNames:  */ 0, 0,
119507 119959     /* iArg:      */ 0 },
119508 119960   #endif
119509 119961   #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
119510 119962    {/* zName:     */ "lock_status",
119511 119963     /* ePragTyp:  */ PragTyp_LOCK_STATUS,
119512 119964     /* ePragFlg:  */ PragFlg_Result0,
119513         -  /* ColNames:  */ 50, 2,
       119965  +  /* ColNames:  */ 43, 2,
119514 119966     /* iArg:      */ 0 },
119515 119967   #endif
119516 119968   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119517 119969    {/* zName:     */ "locking_mode",
119518 119970     /* ePragTyp:  */ PragTyp_LOCKING_MODE,
119519 119971     /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq,
119520 119972     /* ColNames:  */ 0, 0,
................................................................................
119532 119984   #endif
119533 119985   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119534 119986   #if !defined(SQLITE_OMIT_VIRTUALTABLE)
119535 119987   #if defined(SQLITE_INTROSPECTION_PRAGMAS)
119536 119988    {/* zName:     */ "module_list",
119537 119989     /* ePragTyp:  */ PragTyp_MODULE_LIST,
119538 119990     /* ePragFlg:  */ PragFlg_Result0,
119539         -  /* ColNames:  */ 31, 1,
       119991  +  /* ColNames:  */ 9, 1,
119540 119992     /* iArg:      */ 0 },
119541 119993   #endif
119542 119994   #endif
119543 119995   #endif
119544 119996    {/* zName:     */ "optimize",
119545 119997     /* ePragTyp:  */ PragTyp_OPTIMIZE,
119546 119998     /* ePragFlg:  */ PragFlg_Result1|PragFlg_NeedSchema,
................................................................................
119565 120017     /* ColNames:  */ 0, 0,
119566 120018     /* iArg:      */ 0 },
119567 120019   #endif
119568 120020   #if defined(SQLITE_INTROSPECTION_PRAGMAS)
119569 120021    {/* zName:     */ "pragma_list",
119570 120022     /* ePragTyp:  */ PragTyp_PRAGMA_LIST,
119571 120023     /* ePragFlg:  */ PragFlg_Result0,
119572         -  /* ColNames:  */ 31, 1,
       120024  +  /* ColNames:  */ 9, 1,
119573 120025     /* iArg:      */ 0 },
119574 120026   #endif
119575 120027   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119576 120028    {/* zName:     */ "query_only",
119577 120029     /* ePragTyp:  */ PragTyp_FLAG,
119578 120030     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119579 120031     /* ColNames:  */ 0, 0,
................................................................................
119596 120048     /* ePragTyp:  */ PragTyp_FLAG,
119597 120049     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119598 120050     /* ColNames:  */ 0, 0,
119599 120051     /* iArg:      */ SQLITE_RecTriggers },
119600 120052   #endif
119601 120053   #if defined(SQLITE_HAS_CODEC)
119602 120054    {/* zName:     */ "rekey",
119603         -  /* ePragTyp:  */ PragTyp_REKEY,
       120055  +  /* ePragTyp:  */ PragTyp_KEY,
119604 120056     /* ePragFlg:  */ 0,
119605 120057     /* ColNames:  */ 0, 0,
119606         -  /* iArg:      */ 0 },
       120058  +  /* iArg:      */ 1 },
119607 120059   #endif
119608 120060   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119609 120061    {/* zName:     */ "reverse_unordered_selects",
119610 120062     /* ePragTyp:  */ PragTyp_FLAG,
119611 120063     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119612 120064     /* ColNames:  */ 0, 0,
119613 120065     /* iArg:      */ SQLITE_ReverseOrder },
................................................................................
119652 120104     /* iArg:      */ SQLITE_SqlTrace },
119653 120105   #endif
119654 120106   #endif
119655 120107   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
119656 120108    {/* zName:     */ "stats",
119657 120109     /* ePragTyp:  */ PragTyp_STATS,
119658 120110     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
119659         -  /* ColNames:  */ 7, 5,
       120111  +  /* ColNames:  */ 21, 5,
119660 120112     /* iArg:      */ 0 },
119661 120113   #endif
119662 120114   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119663 120115    {/* zName:     */ "synchronous",
119664 120116     /* ePragTyp:  */ PragTyp_SYNCHRONOUS,
119665 120117     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119666 120118     /* ColNames:  */ 0, 0,
119667 120119     /* iArg:      */ 0 },
119668 120120   #endif
119669 120121   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119670 120122    {/* zName:     */ "table_info",
119671 120123     /* ePragTyp:  */ PragTyp_TABLE_INFO,
119672 120124     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119673         -  /* ColNames:  */ 1, 6,
       120125  +  /* ColNames:  */ 8, 6,
119674 120126     /* iArg:      */ 0 },
       120127  + {/* zName:     */ "table_xinfo",
       120128  +  /* ePragTyp:  */ PragTyp_TABLE_INFO,
       120129  +  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
       120130  +  /* ColNames:  */ 8, 7,
       120131  +  /* iArg:      */ 1 },
119675 120132   #endif
119676 120133   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119677 120134    {/* zName:     */ "temp_store",
119678 120135     /* ePragTyp:  */ PragTyp_TEMP_STORE,
119679 120136     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119680 120137     /* ColNames:  */ 0, 0,
119681 120138     /* iArg:      */ 0 },
119682 120139    {/* zName:     */ "temp_store_directory",
119683 120140     /* ePragTyp:  */ PragTyp_TEMP_STORE_DIRECTORY,
119684 120141     /* ePragFlg:  */ PragFlg_NoColumns1,
119685 120142     /* ColNames:  */ 0, 0,
119686 120143     /* iArg:      */ 0 },
       120144  +#endif
       120145  +#if defined(SQLITE_HAS_CODEC)
       120146  + {/* zName:     */ "textkey",
       120147  +  /* ePragTyp:  */ PragTyp_KEY,
       120148  +  /* ePragFlg:  */ 0,
       120149  +  /* ColNames:  */ 0, 0,
       120150  +  /* iArg:      */ 4 },
       120151  + {/* zName:     */ "textrekey",
       120152  +  /* ePragTyp:  */ PragTyp_KEY,
       120153  +  /* ePragFlg:  */ 0,
       120154  +  /* ColNames:  */ 0, 0,
       120155  +  /* iArg:      */ 5 },
119687 120156   #endif
119688 120157    {/* zName:     */ "threads",
119689 120158     /* ePragTyp:  */ PragTyp_THREADS,
119690 120159     /* ePragFlg:  */ PragFlg_Result0,
119691 120160     /* ColNames:  */ 0, 0,
119692 120161     /* iArg:      */ 0 },
119693 120162   #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
................................................................................
119731 120200     /* ePragTyp:  */ PragTyp_WAL_AUTOCHECKPOINT,
119732 120201     /* ePragFlg:  */ 0,
119733 120202     /* ColNames:  */ 0, 0,
119734 120203     /* iArg:      */ 0 },
119735 120204    {/* zName:     */ "wal_checkpoint",
119736 120205     /* ePragTyp:  */ PragTyp_WAL_CHECKPOINT,
119737 120206     /* ePragFlg:  */ PragFlg_NeedSchema,
119738         -  /* ColNames:  */ 46, 3,
       120207  +  /* ColNames:  */ 38, 3,
119739 120208     /* iArg:      */ 0 },
119740 120209   #endif
119741 120210   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119742 120211    {/* zName:     */ "writable_schema",
119743 120212     /* ePragTyp:  */ PragTyp_FLAG,
119744 120213     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
119745 120214     /* ColNames:  */ 0, 0,
119746         -  /* iArg:      */ SQLITE_WriteSchema },
       120215  +  /* iArg:      */ SQLITE_WriteSchema|SQLITE_NoSchemaError },
119747 120216   #endif
119748 120217   };
119749         -/* Number of pragmas: 61 on by default, 78 total. */
       120218  +/* Number of pragmas: 62 on by default, 81 total. */
119750 120219   
119751 120220   /************** End of pragma.h **********************************************/
119752 120221   /************** Continuing where we left off in pragma.c *********************/
119753 120222   
119754 120223   /*
119755 120224   ** Interpret the given string as a safety level.  Return 0 for OFF,
119756 120225   ** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA.  Return 1 for an empty or 
................................................................................
120754 121223   
120755 121224   #ifndef SQLITE_OMIT_FLAG_PRAGMAS
120756 121225     case PragTyp_FLAG: {
120757 121226       if( zRight==0 ){
120758 121227         setPragmaResultColumnNames(v, pPragma);
120759 121228         returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
120760 121229       }else{
120761         -      int mask = pPragma->iArg;    /* Mask of bits to set or clear. */
       121230  +      u64 mask = pPragma->iArg;    /* Mask of bits to set or clear. */
120762 121231         if( db->autoCommit==0 ){
120763 121232           /* Foreign key support may not be enabled or disabled while not
120764 121233           ** in auto-commit mode.  */
120765 121234           mask &= ~(SQLITE_ForeignKeys);
120766 121235         }
120767 121236   #if SQLITE_USER_AUTHENTICATION
120768 121237         if( db->auth.authLevel==UAUTH_User ){
................................................................................
120803 121272     ** dflt_value: The default value for the column, if any.
120804 121273     ** pk:         Non-zero for PK fields.
120805 121274     */
120806 121275     case PragTyp_TABLE_INFO: if( zRight ){
120807 121276       Table *pTab;
120808 121277       pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
120809 121278       if( pTab ){
       121279  +      int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
120810 121280         int i, k;
120811 121281         int nHidden = 0;
120812 121282         Column *pCol;
120813 121283         Index *pPk = sqlite3PrimaryKeyIndex(pTab);
120814         -      pParse->nMem = 6;
120815         -      sqlite3CodeVerifySchema(pParse, iDb);
       121284  +      pParse->nMem = 7;
       121285  +      sqlite3CodeVerifySchema(pParse, iTabDb);
120816 121286         sqlite3ViewGetColumnNames(pParse, pTab);
120817 121287         for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
120818         -        if( IsHiddenColumn(pCol) ){
       121288  +        int isHidden = IsHiddenColumn(pCol);
       121289  +        if( isHidden && pPragma->iArg==0 ){
120819 121290             nHidden++;
120820 121291             continue;
120821 121292           }
120822 121293           if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
120823 121294             k = 0;
120824 121295           }else if( pPk==0 ){
120825 121296             k = 1;
120826 121297           }else{
120827 121298             for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
120828 121299           }
120829 121300           assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN );
120830         -        sqlite3VdbeMultiLoad(v, 1, "issisi",
       121301  +        sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
120831 121302                  i-nHidden,
120832 121303                  pCol->zName,
120833 121304                  sqlite3ColumnType(pCol,""),
120834 121305                  pCol->notNull ? 1 : 0,
120835 121306                  pCol->pDflt ? pCol->pDflt->u.zToken : 0,
120836         -               k);
       121307  +               k,
       121308  +               isHidden);
120837 121309         }
120838 121310       }
120839 121311     }
120840 121312     break;
120841 121313   
120842 121314   #ifdef SQLITE_DEBUG
120843 121315     case PragTyp_STATS: {
................................................................................
120867 121339   #endif
120868 121340   
120869 121341     case PragTyp_INDEX_INFO: if( zRight ){
120870 121342       Index *pIdx;
120871 121343       Table *pTab;
120872 121344       pIdx = sqlite3FindIndex(db, zRight, zDb);
120873 121345       if( pIdx ){
       121346  +      int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
120874 121347         int i;
120875 121348         int mx;
120876 121349         if( pPragma->iArg ){
120877 121350           /* PRAGMA index_xinfo (newer version with more rows and columns) */
120878 121351           mx = pIdx->nColumn;
120879 121352           pParse->nMem = 6;
120880 121353         }else{
120881 121354           /* PRAGMA index_info (legacy version) */
120882 121355           mx = pIdx->nKeyCol;
120883 121356           pParse->nMem = 3;
120884 121357         }
120885 121358         pTab = pIdx->pTable;
120886         -      sqlite3CodeVerifySchema(pParse, iDb);
       121359  +      sqlite3CodeVerifySchema(pParse, iIdxDb);
120887 121360         assert( pParse->nMem<=pPragma->nPragCName );
120888 121361         for(i=0; i<mx; i++){
120889 121362           i16 cnum = pIdx->aiColumn[i];
120890 121363           sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
120891 121364                                cnum<0 ? 0 : pTab->aCol[cnum].zName);
120892 121365           if( pPragma->iArg ){
120893 121366             sqlite3VdbeMultiLoad(v, 4, "isiX",
................................................................................
120903 121376   
120904 121377     case PragTyp_INDEX_LIST: if( zRight ){
120905 121378       Index *pIdx;
120906 121379       Table *pTab;
120907 121380       int i;
120908 121381       pTab = sqlite3FindTable(db, zRight, zDb);
120909 121382       if( pTab ){
       121383  +      int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
120910 121384         pParse->nMem = 5;
120911         -      sqlite3CodeVerifySchema(pParse, iDb);
       121385  +      sqlite3CodeVerifySchema(pParse, iTabDb);
120912 121386         for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
120913 121387           const char *azOrigin[] = { "c", "u", "pk" };
120914 121388           sqlite3VdbeMultiLoad(v, 1, "isisi",
120915 121389              i,
120916 121390              pIdx->zName,
120917 121391              IsUniqueIndex(pIdx),
120918 121392              azOrigin[pIdx->idxType],
................................................................................
120951 121425     case PragTyp_FUNCTION_LIST: {
120952 121426       int i;
120953 121427       HashElem *j;
120954 121428       FuncDef *p;
120955 121429       pParse->nMem = 2;
120956 121430       for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
120957 121431         for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
       121432  +        if( p->funcFlags & SQLITE_FUNC_INTERNAL ) continue;
120958 121433           sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 1);
120959 121434         }
120960 121435       }
120961 121436       for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
120962 121437         p = (FuncDef*)sqliteHashData(j);
120963 121438         sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 0);
120964 121439       }
................................................................................
120992 121467     case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
120993 121468       FKey *pFK;
120994 121469       Table *pTab;
120995 121470       pTab = sqlite3FindTable(db, zRight, zDb);
120996 121471       if( pTab ){
120997 121472         pFK = pTab->pFKey;
120998 121473         if( pFK ){
       121474  +        int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
120999 121475           int i = 0; 
121000 121476           pParse->nMem = 8;
121001         -        sqlite3CodeVerifySchema(pParse, iDb);
       121477  +        sqlite3CodeVerifySchema(pParse, iTabDb);
121002 121478           while(pFK){
121003 121479             int j;
121004 121480             for(j=0; j<pFK->nCol; j++){
121005 121481               sqlite3VdbeMultiLoad(v, 1, "iissssss",
121006 121482                      i,
121007 121483                      j,
121008 121484                      pFK->zTo,
................................................................................
121039 121515       int addrOk;            /* Jump here if the key is OK */
121040 121516       int *aiCols;           /* child to parent column mapping */
121041 121517   
121042 121518       regResult = pParse->nMem+1;
121043 121519       pParse->nMem += 4;
121044 121520       regKey = ++pParse->nMem;
121045 121521       regRow = ++pParse->nMem;
121046         -    sqlite3CodeVerifySchema(pParse, iDb);
121047 121522       k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
121048 121523       while( k ){
       121524  +      int iTabDb;
121049 121525         if( zRight ){
121050 121526           pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
121051 121527           k = 0;
121052 121528         }else{
121053 121529           pTab = (Table*)sqliteHashData(k);
121054 121530           k = sqliteHashNext(k);
121055 121531         }
121056 121532         if( pTab==0 || pTab->pFKey==0 ) continue;
121057         -      sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
       121533  +      iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
       121534  +      sqlite3CodeVerifySchema(pParse, iTabDb);
       121535  +      sqlite3TableLock(pParse, iTabDb, pTab->tnum, 0, pTab->zName);
121058 121536         if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
121059         -      sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
       121537  +      sqlite3OpenTable(pParse, 0, iTabDb, pTab, OP_OpenRead);
121060 121538         sqlite3VdbeLoadString(v, regResult, pTab->zName);
121061 121539         for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
121062 121540           pParent = sqlite3FindTable(db, pFK->zTo, zDb);
121063 121541           if( pParent==0 ) continue;
121064 121542           pIdx = 0;
121065         -        sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
       121543  +        sqlite3TableLock(pParse, iTabDb, pParent->tnum, 0, pParent->zName);
121066 121544           x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
121067 121545           if( x==0 ){
121068 121546             if( pIdx==0 ){
121069         -            sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
       121547  +            sqlite3OpenTable(pParse, i, iTabDb, pParent, OP_OpenRead);
121070 121548             }else{
121071         -            sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
       121549  +            sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iTabDb);
121072 121550               sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
121073 121551             }
121074 121552           }else{
121075 121553             k = 0;
121076 121554             break;
121077 121555           }
121078 121556         }
................................................................................
121833 122311         sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
121834 122312       }
121835 122313       break;
121836 122314     }
121837 122315   #endif
121838 122316   
121839 122317   #ifdef SQLITE_HAS_CODEC
       122318  +  /* Pragma        iArg
       122319  +  ** ----------   ------
       122320  +  **  key           0
       122321  +  **  rekey         1
       122322  +  **  hexkey        2
       122323  +  **  hexrekey      3
       122324  +  **  textkey       4
       122325  +  **  textrekey     5
       122326  +  */
121840 122327     case PragTyp_KEY: {
121841         -    if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
121842         -    break;
121843         -  }
121844         -  case PragTyp_REKEY: {
121845         -    if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
       122328  +    if( zRight ){
       122329  +      int n = pPragma->iArg<4 ? sqlite3Strlen30(zRight) : -1;
       122330  +      if( (pPragma->iArg & 1)==0 ){
       122331  +        sqlite3_key_v2(db, zDb, zRight, n);
       122332  +      }else{
       122333  +        sqlite3_rekey_v2(db, zDb, zRight, n);
       122334  +      }
       122335  +    }
121846 122336       break;
121847 122337     }
121848 122338     case PragTyp_HEXKEY: {
121849 122339       if( zRight ){
121850 122340         u8 iByte;
121851 122341         int i;
121852 122342         char zKey[40];
121853 122343         for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
121854 122344           iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
121855 122345           if( (i&1)!=0 ) zKey[i/2] = iByte;
121856 122346         }
121857         -      if( (zLeft[3] & 0xf)==0xb ){
       122347  +      if( (pPragma->iArg & 1)==0 ){
121858 122348           sqlite3_key_v2(db, zDb, zKey, i/2);
121859 122349         }else{
121860 122350           sqlite3_rekey_v2(db, zDb, zKey, i/2);
121861 122351         }
121862 122352       }
121863 122353       break;
121864 122354     }
................................................................................
122180 122670     0,                           /* xSync - sync transaction */
122181 122671     0,                           /* xCommit - commit transaction */
122182 122672     0,                           /* xRollback - rollback transaction */
122183 122673     0,                           /* xFindFunction - function overloading */
122184 122674     0,                           /* xRename - rename the table */
122185 122675     0,                           /* xSavepoint */
122186 122676     0,                           /* xRelease */
122187         -  0                            /* xRollbackTo */
       122677  +  0,                           /* xRollbackTo */
       122678  +  0                            /* xShadowName */
122188 122679   };
122189 122680   
122190 122681   /*
122191 122682   ** Check to see if zTabName is really the name of a pragma.  If it is,
122192 122683   ** then register an eponymous virtual table for that pragma and return
122193 122684   ** a pointer to the Module object for the new virtual table.
122194 122685   */
................................................................................
122533 123024       }
122534 123025   #endif
122535 123026     }
122536 123027     if( db->mallocFailed ){
122537 123028       rc = SQLITE_NOMEM_BKPT;
122538 123029       sqlite3ResetAllSchemasOfConnection(db);
122539 123030     }
122540         -  if( rc==SQLITE_OK || (db->flags&SQLITE_WriteSchema)){
122541         -    /* Black magic: If the SQLITE_WriteSchema flag is set, then consider
       123031  +  if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){
       123032  +    /* Black magic: If the SQLITE_NoSchemaError flag is set, then consider
122542 123033       ** the schema loaded, even if errors occurred. In this situation the 
122543 123034       ** current sqlite3_prepare() operation will fail, but the following one
122544 123035       ** will attempt to compile the supplied statement against whatever subset
122545 123036       ** of the schema was loaded before the error occurred. The primary
122546 123037       ** purpose of this is to allow access to the sqlite_master table
122547 123038       ** even when its contents have been corrupted.
122548 123039       */
................................................................................
122915 123406     sqlite3BtreeLeaveAll(db);
122916 123407     rc = sqlite3ApiExit(db, rc);
122917 123408     assert( (rc&db->errMask)==rc );
122918 123409     sqlite3_mutex_leave(db->mutex);
122919 123410     return rc;
122920 123411   }
122921 123412   
       123413  +#ifdef SQLITE_ENABLE_NORMALIZE
       123414  +/*
       123415  +** Checks if the specified token is a table, column, or function name,
       123416  +** based on the databases associated with the statement being prepared.
       123417  +** If the function fails, zero is returned and pRc is filled with the
       123418  +** error code.
       123419  +*/
       123420  +static int shouldTreatAsIdentifier(
       123421  +  sqlite3 *db,        /* Database handle. */
       123422  +  const char *zToken, /* Pointer to start of token to be checked */
       123423  +  int nToken,         /* Length of token to be checked */
       123424  +  int *pRc            /* Pointer to error code upon failure */
       123425  +){
       123426  +  int bFound = 0;     /* Non-zero if token is an identifier name. */
       123427  +  int i, j;           /* Database and column loop indexes. */
       123428  +  Schema *pSchema;    /* Schema for current database. */
       123429  +  Hash *pHash;        /* Hash table of tables for current database. */
       123430  +  HashElem *e;        /* Hash element for hash table iteration. */
       123431  +  Table *pTab;        /* Database table for columns being checked. */
       123432  +
       123433  +  if( sqlite3IsRowidN(zToken, nToken) ){
       123434  +    return 1;
       123435  +  }
       123436  +  if( nToken>0 ){
       123437  +    int hash = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zToken[0]], nToken);
       123438  +    if( sqlite3FunctionSearchN(hash, zToken, nToken) ) return 1;
       123439  +  }
       123440  +  assert( db!=0 );
       123441  +  sqlite3_mutex_enter(db->mutex);
       123442  +  sqlite3BtreeEnterAll(db);
       123443  +  for(i=0; i<db->nDb; i++){
       123444  +    pHash = &db->aFunc;
       123445  +    if( sqlite3HashFindN(pHash, zToken, nToken) ){
       123446  +      bFound = 1;
       123447  +      break;
       123448  +    }
       123449  +    pSchema = db->aDb[i].pSchema;
       123450  +    if( pSchema==0 ) continue;
       123451  +    pHash = &pSchema->tblHash;
       123452  +    if( sqlite3HashFindN(pHash, zToken, nToken) ){
       123453  +      bFound = 1;
       123454  +      break;
       123455  +    }
       123456  +    for(e=sqliteHashFirst(pHash); e; e=sqliteHashNext(e)){
       123457  +      pTab = sqliteHashData(e);
       123458  +      if( pTab==0 ) continue;
       123459  +      pHash = pTab->pColHash;
       123460  +      if( pHash==0 ){
       123461  +        pTab->pColHash = pHash = sqlite3_malloc(sizeof(Hash));
       123462  +        if( pHash ){
       123463  +          sqlite3HashInit(pHash);
       123464  +          for(j=0; j<pTab->nCol; j++){
       123465  +            Column *pCol = &pTab->aCol[j];
       123466  +            sqlite3HashInsert(pHash, pCol->zName, pCol);
       123467  +          }
       123468  +        }else{
       123469  +          *pRc = SQLITE_NOMEM_BKPT;
       123470  +          bFound = 0;
       123471  +          goto done;
       123472  +        }
       123473  +      }
       123474  +      if( pHash && sqlite3HashFindN(pHash, zToken, nToken) ){
       123475  +        bFound = 1;
       123476  +        goto done;
       123477  +      }
       123478  +    }
       123479  +  }
       123480  +done:
       123481  +  sqlite3BtreeLeaveAll(db);
       123482  +  sqlite3_mutex_leave(db->mutex);
       123483  +  return bFound;
       123484  +}
       123485  +
       123486  +/*
       123487  +** Attempt to estimate the final output buffer size needed for the fully
       123488  +** normalized version of the specified SQL string.  This should take into
       123489  +** account any potential expansion that could occur (e.g. via IN clauses
       123490  +** being expanded, etc).  This size returned is the total number of bytes
       123491  +** including the NUL terminator.
       123492  +*/
       123493  +static int estimateNormalizedSize(
       123494  +  const char *zSql, /* The original SQL string */
       123495  +  int nSql,         /* Length of original SQL string */
       123496  +  u8 prepFlags      /* The flags passed to sqlite3_prepare_v3() */
       123497  +){
       123498  +  int nOut = nSql + 4;
       123499  +  const char *z = zSql;
       123500  +  while( nOut<nSql*5 ){
       123501  +    while( z[0]!=0 && z[0]!='I' && z[0]!='i' ){ z++; }
       123502  +    if( z[0]==0 ) break;
       123503  +    z++;
       123504  +    if( z[0]!='N' && z[0]!='n' ) break;
       123505  +    z++;
       123506  +    while( sqlite3Isspace(z[0]) ){ z++; }
       123507  +    if( z[0]!='(' ) break;
       123508  +    z++;
       123509  +    nOut += 5; /* ?,?,? */
       123510  +  }
       123511  +  return nOut;
       123512  +}
       123513  +
       123514  +/*
       123515  +** Copy the current token into the output buffer while dealing with quoted
       123516  +** identifiers.  By default, all letters will be converted into lowercase.
       123517  +** If the bUpper flag is set, uppercase will be used.  The piOut argument
       123518  +** will be used to update the target index into the output string.
       123519  +*/
       123520  +static void copyNormalizedToken(
       123521  +  const char *zSql, /* The original SQL string */
       123522  +  int iIn,          /* Current index into the original SQL string */
       123523  +  int nToken,       /* Number of bytes in the current token */
       123524  +  int tokenFlags,   /* Flags returned by the tokenizer */
       123525  +  char *zOut,       /* The output string */
       123526  +  int *piOut        /* Pointer to target index into the output string */
       123527  +){
       123528  +  int bQuoted = tokenFlags & SQLITE_TOKEN_QUOTED;
       123529  +  int bKeyword = tokenFlags & SQLITE_TOKEN_KEYWORD;
       123530  +  int j = *piOut, k = 0;
       123531  +  for(; k<nToken; k++){
       123532  +    if( bQuoted ){
       123533  +      if( k==0 && iIn>0 ){
       123534  +        zOut[j++] = '"';
       123535  +        continue;
       123536  +      }else if( k==nToken-1 ){
       123537  +        zOut[j++] = '"';
       123538  +        continue;
       123539  +      }
       123540  +    }
       123541  +    if( bKeyword ){
       123542  +      zOut[j++] = sqlite3Toupper(zSql[iIn+k]);
       123543  +    }else{
       123544  +      zOut[j++] = sqlite3Tolower(zSql[iIn+k]);
       123545  +    }
       123546  +  }
       123547  +  *piOut = j;
       123548  +}
       123549  +
       123550  +/*
       123551  +** Perform normalization of the SQL contained in the prepared statement and
       123552  +** store the result in the zNormSql field.  The schema for the associated
       123553  +** databases are consulted while performing the normalization in order to
       123554  +** determine if a token appears to be an identifier.  All identifiers are
       123555  +** left intact in the normalized SQL and all literals are replaced with a
       123556  +** single '?'.
       123557  +*/
       123558  +SQLITE_PRIVATE void sqlite3Normalize(
       123559  +  Vdbe *pVdbe,      /* VM being reprepared */
       123560  +  const char *zSql, /* The original SQL string */
       123561  +  int nSql,         /* Size of the input string in bytes */
       123562  +  u8 prepFlags      /* The flags passed to sqlite3_prepare_v3() */
       123563  +){
       123564  +  sqlite3 *db;           /* Database handle. */
       123565  +  char *z;               /* The output string */
       123566  +  int nZ;                /* Size of the output string in bytes */
       123567  +  int i;                 /* Next character to read from zSql[] */
       123568  +  int j;                 /* Next character to fill in on z[] */
       123569  +  int tokenType = 0;     /* Type of the next token */
       123570  +  int prevTokenType = 0; /* Type of the previous token, except spaces */
       123571  +  int n;                 /* Size of the next token */
       123572  +  int nParen = 0;        /* Nesting level of parenthesis */
       123573  +  Hash inHash;           /* Table of parenthesis levels to output index. */
       123574  +
       123575  +  db = sqlite3VdbeDb(pVdbe);
       123576  +  assert( db!=0 );
       123577  +  assert( pVdbe->zNormSql==0 );
       123578  +  if( zSql==0 ) return;
       123579  +  nZ = estimateNormalizedSize(zSql, nSql, prepFlags);
       123580  +  z = sqlite3DbMallocRawNN(db, nZ);
       123581  +  if( z==0 ) return;
       123582  +  sqlite3HashInit(&inHash);
       123583  +  for(i=j=0; i<nSql && zSql[i]; i+=n){
       123584  +    int flags = 0;
       123585  +    if( tokenType!=TK_SPACE ) prevTokenType = tokenType;
       123586  +    n = sqlite3GetTokenNormalized((unsigned char*)zSql+i, &tokenType, &flags);
       123587  +    switch( tokenType ){
       123588  +      case TK_SPACE: {
       123589  +        break;
       123590  +      }
       123591  +      case TK_ILLEGAL: {
       123592  +        sqlite3DbFree(db, z);
       123593  +        sqlite3HashClear(&inHash);
       123594  +        return;
       123595  +      }
       123596  +      case TK_STRING:
       123597  +      case TK_INTEGER:
       123598  +      case TK_FLOAT:
       123599  +      case TK_VARIABLE:
       123600  +      case TK_BLOB: {
       123601  +        z[j++] = '?';
       123602  +        break;
       123603  +      }
       123604  +      case TK_LP:
       123605  +      case TK_RP: {
       123606  +        if( tokenType==TK_LP ){
       123607  +          nParen++;
       123608  +          if( prevTokenType==TK_IN ){
       123609  +            assert( nParen<nSql );
       123610  +            sqlite3HashInsert(&inHash, zSql+nParen, SQLITE_INT_TO_PTR(j));
       123611  +          }
       123612  +        }else{
       123613  +          int jj;
       123614  +          assert( nParen<nSql );
       123615  +          jj = SQLITE_PTR_TO_INT(sqlite3HashFind(&inHash, zSql+nParen));
       123616  +          if( jj>0 ){
       123617  +            sqlite3HashInsert(&inHash, zSql+nParen, 0);
       123618  +            assert( jj+6<nZ );
       123619  +            memcpy(z+jj+1, "?,?,?", 5);
       123620  +            j = jj+6;
       123621  +            assert( nZ-1-j>=0 );
       123622  +            assert( nZ-1-j<nZ );
       123623  +            memset(z+j, 0, nZ-1-j);
       123624  +          }
       123625  +          nParen--;
       123626  +        }
       123627  +        assert( nParen>=0 );
       123628  +        /* Fall through */
       123629  +      }
       123630  +      case TK_MINUS:
       123631  +      case TK_SEMI:
       123632  +      case TK_PLUS:
       123633  +      case TK_STAR:
       123634  +      case TK_SLASH:
       123635  +      case TK_REM:
       123636  +      case TK_EQ:
       123637  +      case TK_LE:
       123638  +      case TK_NE:
       123639  +      case TK_LSHIFT:
       123640  +      case TK_LT:
       123641  +      case TK_RSHIFT:
       123642  +      case TK_GT:
       123643  +      case TK_GE:
       123644  +      case TK_BITOR:
       123645  +      case TK_CONCAT:
       123646  +      case TK_COMMA:
       123647  +      case TK_BITAND:
       123648  +      case TK_BITNOT:
       123649  +      case TK_DOT:
       123650  +      case TK_IN:
       123651  +      case TK_IS:
       123652  +      case TK_NOT:
       123653  +      case TK_NULL:
       123654  +      case TK_ID: {
       123655  +        if( tokenType==TK_NULL ){
       123656  +          if( prevTokenType==TK_IS || prevTokenType==TK_NOT ){
       123657  +            /* NULL is a keyword in this case, not a literal value */
       123658  +          }else{
       123659  +            /* Here the NULL is a literal value */
       123660  +            z[j++] = '?';
       123661  +            break;
       123662  +          }
       123663  +        }
       123664  +        if( j>0 && sqlite3IsIdChar(z[j-1]) && sqlite3IsIdChar(zSql[i]) ){
       123665  +          z[j++] = ' ';
       123666  +        }
       123667  +        if( tokenType==TK_ID ){
       123668  +          int i2 = i, n2 = n, rc = SQLITE_OK;
       123669  +          if( nParen>0 ){
       123670  +            assert( nParen<nSql );
       123671  +            sqlite3HashInsert(&inHash, zSql+nParen, 0);
       123672  +          }
       123673  +          if( flags&SQLITE_TOKEN_QUOTED ){ i2++; n2-=2; }
       123674  +          if( shouldTreatAsIdentifier(db, zSql+i2, n2, &rc)==0 ){
       123675  +            if( rc!=SQLITE_OK ){
       123676  +              sqlite3DbFree(db, z);
       123677  +              sqlite3HashClear(&inHash);
       123678  +              return;
       123679  +            }
       123680  +            if( sqlite3_keyword_check(zSql+i2, n2)==0 ){
       123681  +              z[j++] = '?';
       123682  +              break;
       123683  +            }
       123684  +          }
       123685  +        }
       123686  +        copyNormalizedToken(zSql, i, n, flags, z, &j);
       123687  +        break;
       123688  +      }
       123689  +    }
       123690  +  }
       123691  +  assert( j<nZ && "one" );
       123692  +  while( j>0 && z[j-1]==' ' ){ j--; }
       123693  +  if( j>0 && z[j-1]!=';' ){ z[j++] = ';'; }
       123694  +  z[j] = 0;
       123695  +  assert( j<nZ && "two" );
       123696  +  pVdbe->zNormSql = z;
       123697  +  sqlite3HashClear(&inHash);
       123698  +}
       123699  +#endif /* SQLITE_ENABLE_NORMALIZE */
       123700  +
122922 123701   /*
122923 123702   ** Rerun the compilation of a statement after a schema change.
122924 123703   **
122925 123704   ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
122926 123705   ** if the statement cannot be recompiled because another connection has
122927 123706   ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
122928 123707   ** occurs, return SQLITE_SCHEMA.
................................................................................
123927 124706     int i;
123928 124707     int nDefer = 0;
123929 124708     ExprList *pExtra = 0;
123930 124709     for(i=0; i<pEList->nExpr; i++){
123931 124710       struct ExprList_item *pItem = &pEList->a[i];
123932 124711       if( pItem->u.x.iOrderByCol==0 ){
123933 124712         Expr *pExpr = pItem->pExpr;
123934         -      Table *pTab = pExpr->pTab;
       124713  +      Table *pTab = pExpr->y.pTab;
123935 124714         if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)
123936 124715          && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
123937 124716         ){
123938 124717           int j;
123939 124718           for(j=0; j<nDefer; j++){
123940 124719             if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
123941 124720           }
................................................................................
123950 124729                 pPk = sqlite3PrimaryKeyIndex(pTab);
123951 124730                 nKey = pPk->nKeyCol;
123952 124731               }
123953 124732               for(k=0; k<nKey; k++){
123954 124733                 Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
123955 124734                 if( pNew ){
123956 124735                   pNew->iTable = pExpr->iTable;
123957         -                pNew->pTab = pExpr->pTab;
       124736  +                pNew->y.pTab = pExpr->y.pTab;
123958 124737                   pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
123959 124738                   pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
123960 124739                 }
123961 124740               }
123962         -            pSort->aDefer[nDefer].pTab = pExpr->pTab;
       124741  +            pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
123963 124742               pSort->aDefer[nDefer].iCsr = pExpr->iTable;
123964 124743               pSort->aDefer[nDefer].nKey = nKey;
123965 124744               nDefer++;
123966 124745             }
123967 124746           }
123968 124747           pItem->bSorterRef = 1;
123969 124748         }
................................................................................
124804 125583           ** This is not a problem, as the column type of "t1.col" is never
124805 125584           ** used. When columnType() is called on the expression 
124806 125585           ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
124807 125586           ** branch below.  */
124808 125587           break;
124809 125588         }
124810 125589   
124811         -      assert( pTab && pExpr->pTab==pTab );
       125590  +      assert( pTab && pExpr->y.pTab==pTab );
124812 125591         if( pS ){
124813 125592           /* The "table" is actually a sub-select or a view in the FROM clause
124814 125593           ** of the SELECT statement. Return the declaration type and origin
124815 125594           ** data for the result-set column of the sub-select.
124816 125595           */
124817 125596           if( iCol>=0 && iCol<pS->pEList->nExpr ){
124818 125597             /* If iCol is less than zero, then the expression requests the
................................................................................
124989 125768     srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
124990 125769     sqlite3VdbeSetNumCols(v, pEList->nExpr);
124991 125770     for(i=0; i<pEList->nExpr; i++){
124992 125771       Expr *p = pEList->a[i].pExpr;
124993 125772   
124994 125773       assert( p!=0 );
124995 125774       assert( p->op!=TK_AGG_COLUMN );  /* Agg processing has not run yet */
124996         -    assert( p->op!=TK_COLUMN || p->pTab!=0 ); /* Covering idx not yet coded */
       125775  +    assert( p->op!=TK_COLUMN || p->y.pTab!=0 ); /* Covering idx not yet coded */
124997 125776       if( pEList->a[i].zName ){
124998 125777         /* An AS clause always takes first priority */
124999 125778         char *zName = pEList->a[i].zName;
125000 125779         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
125001 125780       }else if( srcName && p->op==TK_COLUMN ){
125002 125781         char *zCol;
125003 125782         int iCol = p->iColumn;
125004         -      pTab = p->pTab;
       125783  +      pTab = p->y.pTab;
125005 125784         assert( pTab!=0 );
125006 125785         if( iCol<0 ) iCol = pTab->iPKey;
125007 125786         assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
125008 125787         if( iCol<0 ){
125009 125788           zCol = "rowid";
125010 125789         }else{
125011 125790           zCol = pTab->aCol[iCol].zName;
................................................................................
125088 125867           pColExpr = pColExpr->pRight;
125089 125868           assert( pColExpr!=0 );
125090 125869         }
125091 125870         assert( pColExpr->op!=TK_AGG_COLUMN );
125092 125871         if( pColExpr->op==TK_COLUMN ){
125093 125872           /* For columns use the column name name */
125094 125873           int iCol = pColExpr->iColumn;
125095         -        Table *pTab = pColExpr->pTab;
       125874  +        Table *pTab = pColExpr->y.pTab;
125096 125875           assert( pTab!=0 );
125097 125876           if( iCol<0 ) iCol = pTab->iPKey;
125098 125877           zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
125099 125878         }else if( pColExpr->op==TK_ID ){
125100 125879           assert( !ExprHasProperty(pColExpr, EP_IntValue) );
125101 125880           zName = pColExpr->u.zToken;
125102 125881         }else{
................................................................................
131212 131991     }
131213 131992   #ifndef SQLITE_OMIT_FLOATING_POINT
131214 131993     if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
131215 131994       sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
131216 131995     }
131217 131996   #endif
131218 131997   }
       131998  +
       131999  +/*
       132000  +** Check to see if column iCol of index pIdx references any of the
       132001  +** columns defined by aXRef and chngRowid.  Return true if it does
       132002  +** and false if not.  This is an optimization.  False-positives are a
       132003  +** performance degradation, but false-negatives can result in a corrupt
       132004  +** index and incorrect answers.
       132005  +**
       132006  +** aXRef[j] will be non-negative if column j of the original table is
       132007  +** being updated.  chngRowid will be true if the rowid of the table is
       132008  +** being updated.
       132009  +*/
       132010  +static int indexColumnIsBeingUpdated(
       132011  +  Index *pIdx,      /* The index to check */
       132012  +  int iCol,         /* Which column of the index to check */
       132013  +  int *aXRef,       /* aXRef[j]>=0 if column j is being updated */
       132014  +  int chngRowid     /* true if the rowid is being updated */
       132015  +){
       132016  +  i16 iIdxCol = pIdx->aiColumn[iCol];
       132017  +  assert( iIdxCol!=XN_ROWID ); /* Cannot index rowid */
       132018  +  if( iIdxCol>=0 ){
       132019  +    return aXRef[iIdxCol]>=0;
       132020  +  }
       132021  +  assert( iIdxCol==XN_EXPR );
       132022  +  assert( pIdx->aColExpr!=0 );
       132023  +  assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
       132024  +  return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
       132025  +                                            aXRef,chngRowid);
       132026  +}
       132027  +
       132028  +/*
       132029  +** Check to see if index pIdx is a partial index whose conditional
       132030  +** expression might change values due to an UPDATE.  Return true if
       132031  +** the index is subject to change and false if the index is guaranteed
       132032  +** to be unchanged.  This is an optimization.  False-positives are a
       132033  +** performance degradation, but false-negatives can result in a corrupt
       132034  +** index and incorrect answers.
       132035  +**
       132036  +** aXRef[j] will be non-negative if column j of the original table is
       132037  +** being updated.  chngRowid will be true if the rowid of the table is
       132038  +** being updated.
       132039  +*/
       132040  +static int indexWhereClauseMightChange(
       132041  +  Index *pIdx,      /* The index to check */
       132042  +  int *aXRef,       /* aXRef[j]>=0 if column j is being updated */
       132043  +  int chngRowid     /* true if the rowid is being updated */
       132044  +){
       132045  +  if( pIdx->pPartIdxWhere==0 ) return 0;
       132046  +  return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
       132047  +                                            aXRef, chngRowid);
       132048  +}
131219 132049   
131220 132050   /*
131221 132051   ** Process an UPDATE statement.
131222 132052   **
131223 132053   **   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
131224 132054   **          \_______/ \________/     \______/       \________________/
131225 132055   *            onError   pTabList      pChanges             pWhere
................................................................................
131436 132266     pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
131437 132267   
131438 132268     hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
131439 132269   
131440 132270     /* There is one entry in the aRegIdx[] array for each index on the table
131441 132271     ** being updated.  Fill in aRegIdx[] with a register number that will hold
131442 132272     ** the key for accessing each index.
131443         -  **
131444         -  ** FIXME:  Be smarter about omitting indexes that use expressions.
131445 132273     */
131446 132274     for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
131447 132275       int reg;
131448         -    if( chngKey || hasFK>1 || pIdx->pPartIdxWhere || pIdx==pPk ){
       132276  +    if( chngKey || hasFK>1 || pIdx==pPk
       132277  +     || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
       132278  +    ){
131449 132279         reg = ++pParse->nMem;
131450 132280         pParse->nMem += pIdx->nColumn;
131451 132281       }else{
131452 132282         reg = 0;
131453 132283         for(i=0; i<pIdx->nKeyCol; i++){
131454         -        i16 iIdxCol = pIdx->aiColumn[i];
131455         -        if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){
       132284  +        if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){
131456 132285             reg = ++pParse->nMem;
131457 132286             pParse->nMem += pIdx->nColumn;
131458 132287             if( (onError==OE_Replace)
131459 132288              || (onError==OE_Default && pIdx->onError==OE_Replace) 
131460 132289             ){
131461 132290               bReplace = 1;
131462 132291             }
................................................................................
132498 133327     saved_flags = db->flags;
132499 133328     saved_mDbFlags = db->mDbFlags;
132500 133329     saved_nChange = db->nChange;
132501 133330     saved_nTotalChange = db->nTotalChange;
132502 133331     saved_mTrace = db->mTrace;
132503 133332     db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
132504 133333     db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
132505         -  db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_CountRows);
       133334  +  db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder
       133335  +                   | SQLITE_Defensive | SQLITE_CountRows);
132506 133336     db->mTrace = 0;
132507 133337   
132508 133338     zDbMain = db->aDb[iDb].zDbSName;
132509 133339     pMain = db->aDb[iDb].pBt;
132510 133340     isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
132511 133341   
132512 133342     /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
................................................................................
133040 133870   SQLITE_PRIVATE void sqlite3VtabBeginParse(
133041 133871     Parse *pParse,        /* Parsing context */
133042 133872     Token *pName1,        /* Name of new table, or database name */
133043 133873     Token *pName2,        /* Name of new table or NULL */
133044 133874     Token *pModuleName,   /* Name of the module for the virtual table */
133045 133875     int ifNotExists       /* No error if the table already exists */
133046 133876   ){
133047         -  int iDb;              /* The database the table is being created in */
133048 133877     Table *pTable;        /* The new virtual table */
133049 133878     sqlite3 *db;          /* Database connection */
133050 133879   
133051 133880     sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
133052 133881     pTable = pParse->pNewTable;
133053 133882     if( pTable==0 ) return;
133054 133883     assert( 0==pTable->pIndex );
133055 133884   
133056 133885     db = pParse->db;
133057         -  iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
133058         -  assert( iDb>=0 );
133059 133886   
133060 133887     assert( pTable->nModuleArg==0 );
133061 133888     addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
133062 133889     addModuleArgument(db, pTable, 0);
133063 133890     addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
133064 133891     assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
133065 133892          || (pParse->sNameToken.z==pName1->z && pName2->z==0)
................................................................................
133071 133898   #ifndef SQLITE_OMIT_AUTHORIZATION
133072 133899     /* Creating a virtual table invokes the authorization callback twice.
133073 133900     ** The first invocation, to obtain permission to INSERT a row into the
133074 133901     ** sqlite_master table, has already been made by sqlite3StartTable().
133075 133902     ** The second call, to obtain permission to create the table, is made now.
133076 133903     */
133077 133904     if( pTable->azModuleArg ){
       133905  +    int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
       133906  +    assert( iDb>=0 ); /* The database the table is being created in */
133078 133907       sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 
133079 133908               pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName);
133080 133909     }
133081 133910   #endif
133082 133911   }
133083 133912   
133084 133913   /*
................................................................................
133765 134594     void *pArg = 0;
133766 134595     FuncDef *pNew;
133767 134596     int rc = 0;
133768 134597   
133769 134598     /* Check to see the left operand is a column in a virtual table */
133770 134599     if( NEVER(pExpr==0) ) return pDef;
133771 134600     if( pExpr->op!=TK_COLUMN ) return pDef;
133772         -  pTab = pExpr->pTab;
       134601  +  pTab = pExpr->y.pTab;
133773 134602     if( pTab==0 ) return pDef;
133774 134603     if( !IsVirtual(pTab) ) return pDef;
133775 134604     pVtab = sqlite3GetVTable(db, pTab)->pVtab;
133776 134605     assert( pVtab!=0 );
133777 134606     assert( pVtab->pModule!=0 );
133778 134607     pMod = (sqlite3_module *)pVtab->pModule;
133779 134608     if( pMod->xFindFunction==0 ) return pDef;
................................................................................
134385 135214     WhereLoop *pNew;          /* Template WhereLoop */
134386 135215     WhereOrSet *pOrSet;       /* Record best loops here, if not NULL */
134387 135216   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
134388 135217     UnpackedRecord *pRec;     /* Probe for stat4 (if required) */
134389 135218     int nRecValid;            /* Number of valid fields currently in pRec */
134390 135219   #endif
134391 135220     unsigned int bldFlags;    /* SQLITE_BLDF_* flags */
       135221  +  unsigned int iPlanLimit;  /* Search limiter */
134392 135222   };
134393 135223   
134394 135224   /* Allowed values for WhereLoopBuider.bldFlags */
134395 135225   #define SQLITE_BLDF_INDEXED  0x0001   /* An index is used */
134396 135226   #define SQLITE_BLDF_UNIQUE   0x0002   /* All keys of a UNIQUE index used */
       135227  +
       135228  +/* The WhereLoopBuilder.iPlanLimit is used to limit the number of
       135229  +** index+constraint combinations the query planner will consider for a
       135230  +** particular query.  If this parameter is unlimited, then certain
       135231  +** pathological queries can spend excess time in the sqlite3WhereBegin()
       135232  +** routine.  The limit is high enough that is should not impact real-world
       135233  +** queries.
       135234  +**
       135235  +** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit.  The limit is
       135236  +** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM
       135237  +** clause is processed, so that every table in a join is guaranteed to be
       135238  +** able to propose a some index+constraint combinations even if the initial
       135239  +** baseline limit was exhausted by prior tables of the join.
       135240  +*/
       135241  +#ifndef SQLITE_QUERY_PLANNER_LIMIT
       135242  +# define SQLITE_QUERY_PLANNER_LIMIT 20000
       135243  +#endif
       135244  +#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
       135245  +# define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
       135246  +#endif
134397 135247   
134398 135248   /*
134399 135249   ** The WHERE clause processing routine has two halves.  The
134400 135250   ** first part does the start of the WHERE loop and the second
134401 135251   ** half does the tail of the WHERE loop.  An instance of
134402 135252   ** this structure is returned by the first half and passed
134403 135253   ** into the second half to give some continuity.
................................................................................
135644 136494   */
135645 136495   static int whereIndexExprTransNode(Walker *p, Expr *pExpr){
135646 136496     IdxExprTrans *pX = p->u.pIdxTrans;
135647 136497     if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
135648 136498       pExpr->op = TK_COLUMN;
135649 136499       pExpr->iTable = pX->iIdxCur;
135650 136500       pExpr->iColumn = pX->iIdxCol;
135651         -    pExpr->pTab = 0;
       136501  +    pExpr->y.pTab = 0;
135652 136502       return WRC_Prune;
135653 136503     }else{
135654 136504       return WRC_Continue;
135655 136505     }
135656 136506   }
135657 136507   
135658 136508   /*
................................................................................
137044 137894           */
137045 137895           if( sqlite3Isdigit(zNew[0])
137046 137896            || zNew[0]=='-'
137047 137897            || (zNew[0]+1=='0' && iTo==1)
137048 137898           ){
137049 137899             if( pLeft->op!=TK_COLUMN 
137050 137900              || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
137051         -           || IsVirtual(pLeft->pTab)  /* Value might be numeric */
       137901  +           || IsVirtual(pLeft->y.pTab)  /* Value might be numeric */
137052 137902             ){
137053 137903               sqlite3ExprDelete(db, pPrefix);
137054 137904               sqlite3ValueFree(pVal);
137055 137905               return 0;
137056 137906             }
137057 137907           }
137058 137908         }
................................................................................
137145 137995       ** virtual table on their second argument, which is the same as
137146 137996       ** the left-hand side operand in their in-fix form.
137147 137997       **
137148 137998       **       vtab_column MATCH expression
137149 137999       **       MATCH(expression,vtab_column)
137150 138000       */
137151 138001       pCol = pList->a[1].pExpr;
137152         -    if( pCol->op==TK_COLUMN && IsVirtual(pCol->pTab) ){
       138002  +    if( pCol->op==TK_COLUMN && IsVirtual(pCol->y.pTab) ){
137153 138003         for(i=0; i<ArraySize(aOp); i++){
137154 138004           if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
137155 138005             *peOp2 = aOp[i].eOp2;
137156 138006             *ppRight = pList->a[0].pExpr;
137157 138007             *ppLeft = pCol;
137158 138008             return 1;
137159 138009           }
................................................................................
137167 138017       **      OVERLOADED(vtab_column,expression)
137168 138018       **
137169 138019       ** Historically, xFindFunction expected to see lower-case function
137170 138020       ** names.  But for this use case, xFindFunction is expected to deal
137171 138021       ** with function names in an arbitrary case.
137172 138022       */
137173 138023       pCol = pList->a[0].pExpr;
137174         -    if( pCol->op==TK_COLUMN && IsVirtual(pCol->pTab) ){
       138024  +    if( pCol->op==TK_COLUMN && IsVirtual(pCol->y.pTab) ){
137175 138025         sqlite3_vtab *pVtab;
137176 138026         sqlite3_module *pMod;
137177 138027         void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
137178 138028         void *pNotUsed;
137179         -      pVtab = sqlite3GetVTable(db, pCol->pTab)->pVtab;
       138029  +      pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
137180 138030         assert( pVtab!=0 );
137181 138031         assert( pVtab->pModule!=0 );
137182 138032         pMod = (sqlite3_module *)pVtab->pModule;
137183 138033         if( pMod->xFindFunction!=0 ){
137184 138034           i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
137185 138035           if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
137186 138036             *peOp2 = i;
................................................................................
137190 138040           }
137191 138041         }
137192 138042       }
137193 138043     }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
137194 138044       int res = 0;
137195 138045       Expr *pLeft = pExpr->pLeft;
137196 138046       Expr *pRight = pExpr->pRight;
137197         -    if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->pTab) ){
       138047  +    if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->y.pTab) ){
137198 138048         res++;
137199 138049       }
137200         -    if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->pTab) ){
       138050  +    if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->y.pTab) ){
137201 138051         res++;
137202 138052         SWAP(Expr*, pLeft, pRight);
137203 138053       }
137204 138054       *ppLeft = pLeft;
137205 138055       *ppRight = pRight;
137206 138056       if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
137207 138057       if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
................................................................................
138337 139187     Expr *pTerm;
138338 139188     if( pItem->fg.isTabFunc==0 ) return;
138339 139189     pTab = pItem->pTab;
138340 139190     assert( pTab!=0 );
138341 139191     pArgs = pItem->u1.pFuncArg;
138342 139192     if( pArgs==0 ) return;
138343 139193     for(j=k=0; j<pArgs->nExpr; j++){
       139194  +    Expr *pRhs;
138344 139195       while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
138345 139196       if( k>=pTab->nCol ){
138346 139197         sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
138347 139198                         pTab->zName, j);
138348 139199         return;
138349 139200       }
138350 139201       pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
138351 139202       if( pColRef==0 ) return;
138352 139203       pColRef->iTable = pItem->iCursor;
138353 139204       pColRef->iColumn = k++;
138354         -    pColRef->pTab = pTab;
138355         -    pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef,
138356         -                         sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0));
       139205  +    pColRef->y.pTab = pTab;
       139206  +    pRhs = sqlite3PExpr(pParse, TK_UPLUS, 
       139207  +        sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
       139208  +    pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs);
138357 139209       whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
138358 139210     }
138359 139211   }
138360 139212   
138361 139213   /************** End of whereexpr.c *******************************************/
138362 139214   /************** Begin file where.c *******************************************/
138363 139215   /*
................................................................................
139212 140064     if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
139213 140065     if( pTabItem->fg.viaCoroutine ){
139214 140066       sqlite3VdbeChangeP2(v, addrCounter, regBase+n);
139215 140067       testcase( pParse->db->mallocFailed );
139216 140068       translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
139217 140069                             pTabItem->regResult, 1);
139218 140070       sqlite3VdbeGoto(v, addrTop);
139219         -    pTabItem->fg.viaCoroutine = 0;
139220 140071     }else{
139221 140072       sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
139222 140073     }
139223 140074     sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
139224 140075     sqlite3VdbeJumpHere(v, addrTop);
139225 140076     sqlite3ReleaseTempReg(pParse, regRecord);
139226 140077     
................................................................................
139390 140241   
139391 140242   /*
139392 140243   ** The table object reference passed as the second argument to this function
139393 140244   ** must represent a virtual table. This function invokes the xBestIndex()
139394 140245   ** method of the virtual table with the sqlite3_index_info object that
139395 140246   ** comes in as the 3rd argument to this function.
139396 140247   **
139397         -** If an error occurs, pParse is populated with an error message and a
139398         -** non-zero value is returned. Otherwise, 0 is returned and the output
139399         -** part of the sqlite3_index_info structure is left populated.
       140248  +** If an error occurs, pParse is populated with an error message and an
       140249  +** appropriate error code is returned.  A return of SQLITE_CONSTRAINT from
       140250  +** xBestIndex is not considered an error.  SQLITE_CONSTRAINT indicates that
       140251  +** the current configuration of "unusable" flags in sqlite3_index_info can
       140252  +** not result in a valid plan.
139400 140253   **
139401 140254   ** Whether or not an error is returned, it is the responsibility of the
139402 140255   ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
139403 140256   ** that this is required.
139404 140257   */
139405 140258   static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
139406 140259     sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
139407 140260     int rc;
139408 140261   
139409 140262     TRACE_IDX_INPUTS(p);
139410 140263     rc = pVtab->pModule->xBestIndex(pVtab, p);
139411 140264     TRACE_IDX_OUTPUTS(p);
139412 140265   
139413         -  if( rc!=SQLITE_OK ){
       140266  +  if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){
139414 140267       if( rc==SQLITE_NOMEM ){
139415 140268         sqlite3OomFault(pParse->db);
139416 140269       }else if( !pVtab->zErrMsg ){
139417 140270         sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
139418 140271       }else{
139419 140272         sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
139420 140273       }
139421 140274     }
139422 140275     sqlite3_free(pVtab->zErrMsg);
139423 140276     pVtab->zErrMsg = 0;
139424         -
139425         -#if 0
139426         -  /* This error is now caught by the caller.
139427         -  ** Search for "xBestIndex malfunction" below */
139428         -  for(i=0; i<p->nConstraint; i++){
139429         -    if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
139430         -      sqlite3ErrorMsg(pParse, 
139431         -          "table %s: xBestIndex returned an invalid plan", pTab->zName);
139432         -    }
139433         -  }
139434         -#endif
139435         -
139436         -  return pParse->nErr;
       140277  +  return rc;
139437 140278   }
139438 140279   #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
139439 140280   
139440 140281   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
139441 140282   /*
139442 140283   ** Estimate the location of a particular key among all keys in an
139443 140284   ** index.  Store the results in aStat as follows:
................................................................................
140483 141324   **    (4)  The template has the same or lower cost than the current loop
140484 141325   */
140485 141326   static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
140486 141327     WhereLoop **ppPrev, *p;
140487 141328     WhereInfo *pWInfo = pBuilder->pWInfo;
140488 141329     sqlite3 *db = pWInfo->pParse->db;
140489 141330     int rc;
       141331  +
       141332  +  /* Stop the search once we hit the query planner search limit */
       141333  +  if( pBuilder->iPlanLimit==0 ){
       141334  +    WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n"));
       141335  +    if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
       141336  +    return SQLITE_DONE;
       141337  +  }
       141338  +  pBuilder->iPlanLimit--;
140490 141339   
140491 141340     /* If pBuilder->pOrSet is defined, then only keep track of the costs
140492 141341     ** and prereqs.
140493 141342     */
140494 141343     if( pBuilder->pOrSet!=0 ){
140495 141344       if( pTemplate->nLTerm ){
140496 141345   #if WHERETRACE_ENABLED
................................................................................
141494 142343     pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
141495 142344     pIdxInfo->estimatedRows = 25;
141496 142345     pIdxInfo->idxFlags = 0;
141497 142346     pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
141498 142347   
141499 142348     /* Invoke the virtual table xBestIndex() method */
141500 142349     rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
141501         -  if( rc ) return rc;
       142350  +  if( rc ){
       142351  +    if( rc==SQLITE_CONSTRAINT ){
       142352  +      /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
       142353  +      ** that the particular combination of parameters provided is unusable.
       142354  +      ** Make no entries in the loop table.
       142355  +      */
       142356  +      WHERETRACE(0xffff, ("  ^^^^--- non-viable plan rejected!\n"));
       142357  +      return SQLITE_OK;
       142358  +    }
       142359  +    return rc;
       142360  +  }
141502 142361   
141503 142362     mxTerm = -1;
141504 142363     assert( pNew->nLSlot>=nConstraint );
141505 142364     for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
141506 142365     pNew->u.vtab.omitMask = 0;
141507 142366     pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
141508 142367     for(i=0; i<nConstraint; i++, pIdxCons++){
................................................................................
141890 142749     int rc = SQLITE_OK;
141891 142750     WhereLoop *pNew;
141892 142751     u8 priorJointype = 0;
141893 142752   
141894 142753     /* Loop over the tables in the join, from left to right */
141895 142754     pNew = pBuilder->pNew;
141896 142755     whereLoopInit(pNew);
       142756  +  pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
141897 142757     for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
141898 142758       Bitmask mUnusable = 0;
141899 142759       pNew->iTab = iTab;
       142760  +    pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
141900 142761       pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
141901 142762       if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
141902 142763         /* This condition is true when pItem is the FROM clause term on the
141903 142764         ** right-hand-side of a LEFT or CROSS JOIN.  */
141904 142765         mPrereq = mPrior;
141905 142766       }
141906 142767       priorJointype = pItem->fg.jointype;
................................................................................
141918 142779       {
141919 142780         rc = whereLoopAddBtree(pBuilder, mPrereq);
141920 142781       }
141921 142782       if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
141922 142783         rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
141923 142784       }
141924 142785       mPrior |= pNew->maskSelf;
141925         -    if( rc || db->mallocFailed ) break;
       142786  +    if( rc || db->mallocFailed ){
       142787  +      if( rc==SQLITE_DONE ){
       142788  +        /* We hit the query planner search limit set by iPlanLimit */
       142789  +        sqlite3_log(SQLITE_WARNING, "abbreviated query algorithm search");
       142790  +        rc = SQLITE_OK;
       142791  +      }else{
       142792  +        break;
       142793  +      }
       142794  +    }
141926 142795     }
141927 142796   
141928 142797     whereLoopClear(db, pNew);
141929 142798     return rc;
141930 142799   }
141931 142800   
141932 142801   /*
................................................................................
144300 145169         if( i==nSrc ) return WRC_Continue;
144301 145170       }
144302 145171     }
144303 145172   
144304 145173     switch( pExpr->op ){
144305 145174   
144306 145175       case TK_FUNCTION:
144307         -      if( pExpr->pWin==0 ){
       145176  +      if( !ExprHasProperty(pExpr, EP_WinFunc) ){
144308 145177           break;
144309 145178         }else{
144310 145179           Window *pWin;
144311 145180           for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
144312         -          if( pExpr->pWin==pWin ){
       145181  +          if( pExpr->y.pWin==pWin ){
144313 145182               assert( pWin->pOwner==pExpr );
144314 145183               return WRC_Prune;
144315 145184             }
144316 145185           }
144317 145186         }
144318 145187         /* Fall through.  */
144319 145188   
................................................................................
144422 145291   ** any SQL window functions, this function is a no-op. Otherwise, it 
144423 145292   ** rewrites the SELECT statement so that window function xStep functions
144424 145293   ** are invoked in the correct order as described under "SELECT REWRITING"
144425 145294   ** at the top of this file.
144426 145295   */
144427 145296   SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
144428 145297     int rc = SQLITE_OK;
144429         -  if( p->pWin ){
       145298  +  if( p->pWin && p->pPrior==0 ){
144430 145299       Vdbe *v = sqlite3GetVdbe(pParse);
144431 145300       sqlite3 *db = pParse->db;
144432 145301       Select *pSub = 0;             /* The subquery */
144433 145302       SrcList *pSrc = p->pSrc;
144434 145303       Expr *pWhere = p->pWhere;
144435 145304       ExprList *pGroupBy = p->pGroupBy;
144436 145305       Expr *pHaving = p->pHaving;
................................................................................
144635 145504   }
144636 145505   
144637 145506   /*
144638 145507   ** Attach window object pWin to expression p.
144639 145508   */
144640 145509   SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
144641 145510     if( p ){
       145511  +    assert( p->op==TK_FUNCTION );
144642 145512       /* This routine is only called for the parser.  If pWin was not
144643 145513       ** allocated due to an OOM, then the parser would fail before ever
144644 145514       ** invoking this routine */
144645 145515       if( ALWAYS(pWin) ){
144646         -      p->pWin = pWin;
       145516  +      p->y.pWin = pWin;
       145517  +      ExprSetProperty(p, EP_WinFunc);
144647 145518         pWin->pOwner = p;
144648 145519         if( p->flags & EP_Distinct ){
144649 145520           sqlite3ErrorMsg(pParse,
144650 145521              "DISTINCT is not supported for window functions");
144651 145522         }
144652 145523       }
144653 145524     }else{
................................................................................
145802 146673   /*
145803 146674   ** Allocate and return a duplicate of the Window object indicated by the
145804 146675   ** third argument. Set the Window.pOwner field of the new object to
145805 146676   ** pOwner.
145806 146677   */
145807 146678   SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
145808 146679     Window *pNew = 0;
145809         -  if( p ){
       146680  +  if( ALWAYS(p) ){
145810 146681       pNew = sqlite3DbMallocZero(db, sizeof(Window));
145811 146682       if( pNew ){
145812 146683         pNew->zName = sqlite3DbStrDup(db, p->zName);
145813 146684         pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
145814 146685         pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
145815 146686         pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
145816 146687         pNew->eType = p->eType;
................................................................................
145954 146825   ** of this template is copied straight through into the generate parser
145955 146826   ** source file.
145956 146827   **
145957 146828   ** The following is the concatenation of all %include directives from the
145958 146829   ** input grammar file:
145959 146830   */
145960 146831   /* #include <stdio.h> */
       146832  +/* #include <assert.h> */
145961 146833   /************ Begin %include sections from the grammar ************************/
145962 146834   
145963 146835   /* #include "sqliteInt.h" */
145964 146836   
145965 146837   /*
145966 146838   ** Disable all error recovery processing in the parser push-down
145967 146839   ** automaton.
................................................................................
146055 146927         p->op = (u8)op;
146056 146928         p->affinity = 0;
146057 146929         p->flags = EP_Leaf;
146058 146930         p->iAgg = -1;
146059 146931         p->pLeft = p->pRight = 0;
146060 146932         p->x.pList = 0;
146061 146933         p->pAggInfo = 0;
146062         -      p->pTab = 0;
       146934  +      p->y.pTab = 0;
146063 146935         p->op2 = 0;
146064 146936         p->iTable = 0;
146065 146937         p->iColumn = 0;
146066         -#ifndef SQLITE_OMIT_WINDOWFUNC
146067         -      p->pWin = 0;
146068         -#endif
146069 146938         p->u.zToken = (char*)&p[1];
146070 146939         memcpy(p->u.zToken, t.z, t.n);
146071 146940         p->u.zToken[t.n] = 0;
146072 146941         if( sqlite3Isquote(p->u.zToken[0]) ){
146073 146942           if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
146074 146943           sqlite3Dequote(p->u.zToken);
146075 146944         }
................................................................................
150253 151122                yyTracePrompt,yyTokenName[yymajor]);
150254 151123           }
150255 151124   #endif
150256 151125           yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
150257 151126           yymajor = YYNOCODE;
150258 151127         }else{
150259 151128           while( yypParser->yytos >= yypParser->yystack
150260         -            && yymx != YYERRORSYMBOL
150261 151129               && (yyact = yy_find_reduce_action(
150262 151130                           yypParser->yytos->stateno,
150263         -                        YYERRORSYMBOL)) >= YY_MIN_REDUCE
       151131  +                        YYERRORSYMBOL)) > YY_MAX_SHIFTREDUCE
150264 151132           ){
150265 151133             yy_pop_parser_stack(yypParser);
150266 151134           }
150267 151135           if( yypParser->yytos < yypParser->yystack || yymajor==0 ){
150268 151136             yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
150269 151137             yy_parse_failed(yypParser);
150270 151138   #ifndef YYNOERRORRECOVERY
................................................................................
151223 152091       }
151224 152092     }
151225 152093     while( IdChar(z[i]) ){ i++; }
151226 152094     *tokenType = TK_ID;
151227 152095     return i;
151228 152096   }
151229 152097   
       152098  +#ifdef SQLITE_ENABLE_NORMALIZE
       152099  +/*
       152100  +** Return the length (in bytes) of the token that begins at z[0].
       152101  +** Store the token type in *tokenType before returning.  If flags has
       152102  +** SQLITE_TOKEN_NORMALIZE flag enabled, use the identifier token type
       152103  +** for keywords.  Add SQLITE_TOKEN_QUOTED to flags if the token was
       152104  +** actually a quoted identifier.  Add SQLITE_TOKEN_KEYWORD to flags
       152105  +** if the token was recognized as a keyword; this is useful when the
       152106  +** SQLITE_TOKEN_NORMALIZE flag is used, because it enables the caller
       152107  +** to differentiate between a keyword being treated as an identifier
       152108  +** (for normalization purposes) and an actual identifier.
       152109  +*/
       152110  +SQLITE_PRIVATE int sqlite3GetTokenNormalized(
       152111  +  const unsigned char *z,
       152112  +  int *tokenType,
       152113  +  int *flags
       152114  +){
       152115  +  int n;
       152116  +  unsigned char iClass = aiClass[*z];
       152117  +  if( iClass==CC_KYWD ){
       152118  +    int i;
       152119  +    for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
       152120  +    if( IdChar(z[i]) ){
       152121  +      /* This token started out using characters that can appear in keywords,
       152122  +      ** but z[i] is a character not allowed within keywords, so this must
       152123  +      ** be an identifier instead */
       152124  +      i++;
       152125  +      while( IdChar(z[i]) ){ i++; }
       152126  +      *tokenType = TK_ID;
       152127  +      return i;
       152128  +    }
       152129  +    *tokenType = TK_ID;
       152130  +    n = keywordCode((char*)z, i, tokenType);
       152131  +    /* If the token is no longer considered to be an identifier, then it is a
       152132  +    ** keyword of some kind.  Make the token back into an identifier and then
       152133  +    ** set the SQLITE_TOKEN_KEYWORD flag.  Several non-identifier tokens are
       152134  +    ** used verbatim, including IN, IS, NOT, and NULL. */
       152135  +    switch( *tokenType ){
       152136  +      case TK_ID: {
       152137  +        /* do nothing, handled by caller */
       152138  +        break;
       152139  +      }
       152140  +      case TK_IN:
       152141  +      case TK_IS:
       152142  +      case TK_NOT:
       152143  +      case TK_NULL: {
       152144  +        *flags |= SQLITE_TOKEN_KEYWORD;
       152145  +        break;
       152146  +      }
       152147  +      default: {
       152148  +        *tokenType = TK_ID;
       152149  +        *flags |= SQLITE_TOKEN_KEYWORD;
       152150  +        break;
       152151  +      }
       152152  +    }
       152153  +  }else{
       152154  +    n = sqlite3GetToken(z, tokenType);
       152155  +    /* If the token is considered to be an identifier and the character class
       152156  +    ** of the first character is a quote, set the SQLITE_TOKEN_QUOTED flag. */
       152157  +    if( *tokenType==TK_ID && (iClass==CC_QUOTE || iClass==CC_QUOTE2) ){
       152158  +      *flags |= SQLITE_TOKEN_QUOTED;
       152159  +    }
       152160  +  }
       152161  +  return n;
       152162  +}
       152163  +#endif /* SQLITE_ENABLE_NORMALIZE */
       152164  +
151230 152165   /*
151231 152166   ** Run the parser on the given SQL string.  The parser structure is
151232 152167   ** passed in.  An SQLITE_ status code is returned.  If an error occurs
151233 152168   ** then an and attempt is made to write an error message into 
151234 152169   ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
151235 152170   ** error message.
151236 152171   */
................................................................................
152620 153555           { SQLITE_DBCONFIG_ENABLE_TRIGGER,        SQLITE_EnableTrigger  },
152621 153556           { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer  },
152622 153557           { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension  },
152623 153558           { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE,      SQLITE_NoCkptOnClose  },
152624 153559           { SQLITE_DBCONFIG_ENABLE_QPSG,           SQLITE_EnableQPSG     },
152625 153560           { SQLITE_DBCONFIG_TRIGGER_EQP,           SQLITE_TriggerEQP     },
152626 153561           { SQLITE_DBCONFIG_RESET_DATABASE,        SQLITE_ResetDatabase  },
       153562  +        { SQLITE_DBCONFIG_DEFENSIVE,             SQLITE_Defensive      },
152627 153563         };
152628 153564         unsigned int i;
152629 153565         rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
152630 153566         for(i=0; i<ArraySize(aFlagOp); i++){
152631 153567           if( aFlagOp[i].op==op ){
152632 153568             int onoff = va_arg(ap, int);
152633 153569             int *pRes = va_arg(ap, int*);
................................................................................
154847 155783   #endif
154848 155784   #if defined(SQLITE_ENABLE_FTS3_TOKENIZER)
154849 155785                    | SQLITE_Fts3Tokenizer
154850 155786   #endif
154851 155787   #if defined(SQLITE_ENABLE_QPSG)
154852 155788                    | SQLITE_EnableQPSG
154853 155789   #endif
       155790  +#if defined(SQLITE_DEFAULT_DEFENSIVE)
       155791  +                 | SQLITE_Defensive
       155792  +#endif
154854 155793         ;
154855 155794     sqlite3HashInit(&db->aCollSeq);
154856 155795   #ifndef SQLITE_OMIT_VIRTUALTABLE
154857 155796     sqlite3HashInit(&db->aModule);
154858 155797   #endif
154859 155798   
154860 155799     /* Add the default collation sequence BINARY. BINARY works for both UTF-8
................................................................................
155734 156673         sqlite3 *db = va_arg(ap, sqlite3*);
155735 156674         db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
155736 156675         break;
155737 156676       }
155738 156677   
155739 156678       /*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
155740 156679       **
155741         -    ** If parameter onoff is non-zero, configure the wrappers so that all
155742         -    ** subsequent calls to localtime() and variants fail. If onoff is zero,
155743         -    ** undo this setting.
       156680  +    ** If parameter onoff is non-zero, subsequent calls to localtime()
       156681  +    ** and its variants fail. If onoff is zero, undo this setting.
155744 156682       */
155745 156683       case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
155746 156684         sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
155747 156685         break;
155748 156686       }
       156687  +
       156688  +    /*   sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCS, int onoff);
       156689  +    **
       156690  +    ** If parameter onoff is non-zero, internal-use-only SQL functions
       156691  +    ** are visible to ordinary SQL.  This is useful for testing but is
       156692  +    ** unsafe because invalid parameters to those internal-use-only functions
       156693  +    ** can result in crashes or segfaults.
       156694  +    */
       156695  +    case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: {
       156696  +      sqlite3GlobalConfig.bInternalFunctions = va_arg(ap, int);
       156697  +      break;
       156698  +    }
155749 156699   
155750 156700       /*   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
155751 156701       **
155752 156702       ** Set or clear a flag that indicates that the database file is always well-
155753 156703       ** formed and never corrupt.  This flag is clear by default, indicating that
155754 156704       ** database files might have arbitrary corruption.  Setting the flag during
155755 156705       ** testing causes certain assert() statements in the code to be activated
................................................................................
161209 162159     UNUSED_PARAMETER(iSavepoint);
161210 162160     assert( p->inTransaction );
161211 162161     assert( p->mxSavepoint >= iSavepoint );
161212 162162     TESTONLY( p->mxSavepoint = iSavepoint );
161213 162163     sqlite3Fts3PendingTermsClear(p);
161214 162164     return SQLITE_OK;
161215 162165   }
       162166  +
       162167  +/*
       162168  +** Return true if zName is the extension on one of the shadow tables used
       162169  +** by this module.
       162170  +*/
       162171  +static int fts3ShadowName(const char *zName){
       162172  +  static const char *azName[] = {
       162173  +    "content", "docsize", "segdir", "segments", "stat", 
       162174  +  };
       162175  +  unsigned int i;
       162176  +  for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
       162177  +    if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
       162178  +  }
       162179  +  return 0;
       162180  +}
161216 162181   
161217 162182   static const sqlite3_module fts3Module = {
161218         -  /* iVersion      */ 2,
       162183  +  /* iVersion      */ 3,
161219 162184     /* xCreate       */ fts3CreateMethod,
161220 162185     /* xConnect      */ fts3ConnectMethod,
161221 162186     /* xBestIndex    */ fts3BestIndexMethod,
161222 162187     /* xDisconnect   */ fts3DisconnectMethod,
161223 162188     /* xDestroy      */ fts3DestroyMethod,
161224 162189     /* xOpen         */ fts3OpenMethod,
161225 162190     /* xClose        */ fts3CloseMethod,
................................................................................
161234 162199     /* xCommit       */ fts3CommitMethod,
161235 162200     /* xRollback     */ fts3RollbackMethod,
161236 162201     /* xFindFunction */ fts3FindFunctionMethod,
161237 162202     /* xRename */       fts3RenameMethod,
161238 162203     /* xSavepoint    */ fts3SavepointMethod,
161239 162204     /* xRelease      */ fts3ReleaseMethod,
161240 162205     /* xRollbackTo   */ fts3RollbackToMethod,
       162206  +  /* xShadowName   */ fts3ShadowName,
161241 162207   };
161242 162208   
161243 162209   /*
161244 162210   ** This function is registered as the module destructor (called when an
161245 162211   ** FTS3 enabled database connection is closed). It frees the memory
161246 162212   ** allocated for the tokenizer hash table.
161247 162213   */
................................................................................
161514 162480       }
161515 162481       assert( pToken->pSegcsr==0 );
161516 162482     }
161517 162483   
161518 162484     return rc;
161519 162485   }
161520 162486   
       162487  +#ifndef SQLITE_DISABLE_FTS4_DEFERRED
161521 162488   /*
161522 162489   ** This function is called on each phrase after the position lists for
161523 162490   ** any deferred tokens have been loaded into memory. It updates the phrases
161524 162491   ** current position list to include only those positions that are really
161525 162492   ** instances of the phrase (after considering deferred tokens). If this
161526 162493   ** means that the phrase does not appear in the current row, doclist.pList
161527 162494   ** and doclist.nList are both zeroed.
................................................................................
161617 162584         }
161618 162585         sqlite3_free(aPoslist);
161619 162586       }
161620 162587     }
161621 162588   
161622 162589     return SQLITE_OK;
161623 162590   }
       162591  +#endif /* SQLITE_DISABLE_FTS4_DEFERRED */
161624 162592   
161625 162593   /*
161626 162594   ** Maximum number of tokens a phrase may have to be considered for the
161627 162595   ** incremental doclists strategy.
161628 162596   */
161629 162597   #define MAX_INCR_PHRASE_TOKENS 4
161630 162598   
................................................................................
163865 164833        0,                           /* xSync         */
163866 164834        0,                           /* xCommit       */
163867 164835        0,                           /* xRollback     */
163868 164836        0,                           /* xFindFunction */
163869 164837        0,                           /* xRename       */
163870 164838        0,                           /* xSavepoint    */
163871 164839        0,                           /* xRelease      */
163872         -     0                            /* xRollbackTo   */
       164840  +     0,                           /* xRollbackTo   */
       164841  +     0                            /* xShadowName   */
163873 164842     };
163874 164843     int rc;                         /* Return code */
163875 164844   
163876 164845     rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
163877 164846     return rc;
163878 164847   }
163879 164848   
................................................................................
167424 168393        0,                           /* xSync         */
167425 168394        0,                           /* xCommit       */
167426 168395        0,                           /* xRollback     */
167427 168396        0,                           /* xFindFunction */
167428 168397        0,                           /* xRename       */
167429 168398        0,                           /* xSavepoint    */
167430 168399        0,                           /* xRelease      */
167431         -     0                            /* xRollbackTo   */
       168400  +     0,                           /* xRollbackTo   */
       168401  +     0                            /* xShadowName   */
167432 168402     };
167433 168403     int rc;                         /* Return code */
167434 168404   
167435 168405     rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
167436 168406     return rc;
167437 168407   }
167438 168408   
................................................................................
177601 178571   #define JEACH_VALUE   1
177602 178572   #define JEACH_TYPE    2
177603 178573   #define JEACH_ATOM    3
177604 178574   #define JEACH_ID      4
177605 178575   #define JEACH_PARENT  5
177606 178576   #define JEACH_FULLKEY 6
177607 178577   #define JEACH_PATH    7
       178578  +/* The xBestIndex method assumes that the JSON and ROOT columns are
       178579  +** the last two columns in the table.  Should this ever changes, be
       178580  +** sure to update the xBestIndex method. */
177608 178581   #define JEACH_JSON    8
177609 178582   #define JEACH_ROOT    9
177610 178583   
177611 178584     UNUSED_PARAM(pzErr);
177612 178585     UNUSED_PARAM(argv);
177613 178586     UNUSED_PARAM(argc);
177614 178587     UNUSED_PARAM(pAux);
................................................................................
177858 178831   ** 1 if the constraint is found, 3 if the constraint and zRoot are found,
177859 178832   ** and 0 otherwise.
177860 178833   */
177861 178834   static int jsonEachBestIndex(
177862 178835     sqlite3_vtab *tab,
177863 178836     sqlite3_index_info *pIdxInfo
177864 178837   ){
177865         -  int i;
177866         -  int jsonIdx = -1;
177867         -  int rootIdx = -1;
       178838  +  int i;                     /* Loop counter or computed array index */
       178839  +  int aIdx[2];               /* Index of constraints for JSON and ROOT */
       178840  +  int unusableMask = 0;      /* Mask of unusable JSON and ROOT constraints */
       178841  +  int idxMask = 0;           /* Mask of usable == constraints JSON and ROOT */
177868 178842     const struct sqlite3_index_constraint *pConstraint;
177869 178843   
       178844  +  /* This implementation assumes that JSON and ROOT are the last two
       178845  +  ** columns in the table */
       178846  +  assert( JEACH_ROOT == JEACH_JSON+1 );
177870 178847     UNUSED_PARAM(tab);
       178848  +  aIdx[0] = aIdx[1] = -1;
177871 178849     pConstraint = pIdxInfo->aConstraint;
177872 178850     for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
177873         -    if( pConstraint->usable==0 ) continue;
177874         -    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
177875         -    switch( pConstraint->iColumn ){
177876         -      case JEACH_JSON:   jsonIdx = i;    break;
177877         -      case JEACH_ROOT:   rootIdx = i;    break;
177878         -      default:           /* no-op */     break;
       178851  +    int iCol;
       178852  +    int iMask;
       178853  +    if( pConstraint->iColumn < JEACH_JSON ) continue;
       178854  +    iCol = pConstraint->iColumn - JEACH_JSON;
       178855  +    assert( iCol==0 || iCol==1 );
       178856  +    iMask = 1 << iCol;
       178857  +    if( pConstraint->usable==0 ){
       178858  +      unusableMask |= iMask;
       178859  +    }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
       178860  +      aIdx[iCol] = i;
       178861  +      idxMask |= iMask;
177879 178862       }
177880 178863     }
177881         -  if( jsonIdx<0 ){
       178864  +  if( (unusableMask & ~idxMask)!=0 ){
       178865  +    /* If there are any unusable constraints on JSON or ROOT, then reject
       178866  +    ** this entire plan */
       178867  +    return SQLITE_CONSTRAINT;
       178868  +  }
       178869  +  if( aIdx[0]<0 ){
       178870  +    /* No JSON input.  Leave estimatedCost at the huge value that it was
       178871  +    ** initialized to to discourage the query planner from selecting this
       178872  +    ** plan. */
177882 178873       pIdxInfo->idxNum = 0;
177883         -    pIdxInfo->estimatedCost = 1e99;
177884 178874     }else{
177885 178875       pIdxInfo->estimatedCost = 1.0;
177886         -    pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
177887         -    pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
177888         -    if( rootIdx<0 ){
177889         -      pIdxInfo->idxNum = 1;
       178876  +    i = aIdx[0];
       178877  +    pIdxInfo->aConstraintUsage[i].argvIndex = 1;
       178878  +    pIdxInfo->aConstraintUsage[i].omit = 1;
       178879  +    if( aIdx[1]<0 ){
       178880  +      pIdxInfo->idxNum = 1;  /* Only JSON supplied.  Plan 1 */
177890 178881       }else{
177891         -      pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2;
177892         -      pIdxInfo->aConstraintUsage[rootIdx].omit = 1;
177893         -      pIdxInfo->idxNum = 3;
       178882  +      i = aIdx[1];
       178883  +      pIdxInfo->aConstraintUsage[i].argvIndex = 2;
       178884  +      pIdxInfo->aConstraintUsage[i].omit = 1;
       178885  +      pIdxInfo->idxNum = 3;  /* Both JSON and ROOT are supplied.  Plan 3 */
177894 178886       }
177895 178887     }
177896 178888     return SQLITE_OK;
177897 178889   }
177898 178890   
177899 178891   /* Start a search on a new JSON string */
177900 178892   static int jsonEachFilter(
................................................................................
177995 178987     0,                         /* xSync */
177996 178988     0,                         /* xCommit */
177997 178989     0,                         /* xRollback */
177998 178990     0,                         /* xFindMethod */
177999 178991     0,                         /* xRename */
178000 178992     0,                         /* xSavepoint */
178001 178993     0,                         /* xRelease */
178002         -  0                          /* xRollbackTo */
       178994  +  0,                         /* xRollbackTo */
       178995  +  0                          /* xShadowName */
178003 178996   };
178004 178997   
178005 178998   /* The methods of the json_tree virtual table. */
178006 178999   static sqlite3_module jsonTreeModule = {
178007 179000     0,                         /* iVersion */
178008 179001     0,                         /* xCreate */
178009 179002     jsonEachConnect,           /* xConnect */
................................................................................
178022 179015     0,                         /* xSync */
178023 179016     0,                         /* xCommit */
178024 179017     0,                         /* xRollback */
178025 179018     0,                         /* xFindMethod */
178026 179019     0,                         /* xRename */
178027 179020     0,                         /* xSavepoint */
178028 179021     0,                         /* xRelease */
178029         -  0                          /* xRollbackTo */
       179022  +  0,                         /* xRollbackTo */
       179023  +  0                          /* xShadowName */
178030 179024   };
178031 179025   #endif /* SQLITE_OMIT_VIRTUALTABLE */
178032 179026   
178033 179027   /****************************************************************************
178034 179028   ** The following routines are the only publically visible identifiers in this
178035 179029   ** file.  Call the following routines in order to register the various SQL
178036 179030   ** functions and the virtual table implemented by this file.
................................................................................
181452 182446       }
181453 182447       sqlite3_free(zSql);
181454 182448     }
181455 182449   
181456 182450     return rc;
181457 182451   }
181458 182452   
       182453  +
       182454  +/*
       182455  +** Return true if zName is the extension on one of the shadow tables used
       182456  +** by this module.
       182457  +*/
       182458  +static int rtreeShadowName(const char *zName){
       182459  +  static const char *azName[] = {
       182460  +    "node", "parent", "rowid"
       182461  +  };
       182462  +  unsigned int i;
       182463  +  for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
       182464  +    if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
       182465  +  }
       182466  +  return 0;
       182467  +}
       182468  +
181459 182469   static sqlite3_module rtreeModule = {
181460         -  2,                          /* iVersion */
       182470  +  3,                          /* iVersion */
181461 182471     rtreeCreate,                /* xCreate - create a table */
181462 182472     rtreeConnect,               /* xConnect - connect to an existing table */
181463 182473     rtreeBestIndex,             /* xBestIndex - Determine search strategy */
181464 182474     rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
181465 182475     rtreeDestroy,               /* xDestroy - Drop a table */
181466 182476     rtreeOpen,                  /* xOpen - open a cursor */
181467 182477     rtreeClose,                 /* xClose - close a cursor */
................................................................................
181476 182486     rtreeEndTransaction,        /* xCommit - commit transaction */
181477 182487     rtreeEndTransaction,        /* xRollback - rollback transaction */
181478 182488     0,                          /* xFindFunction - function overloading */
181479 182489     rtreeRename,                /* xRename - rename the table */
181480 182490     rtreeSavepoint,             /* xSavepoint */
181481 182491     0,                          /* xRelease */
181482 182492     0,                          /* xRollbackTo */
       182493  +  rtreeShadowName             /* xShadowName */
181483 182494   };
181484 182495   
181485 182496   static int rtreeSqlInit(
181486 182497     Rtree *pRtree, 
181487 182498     sqlite3 *db, 
181488 182499     const char *zDb, 
181489 182500     const char *zPrefix, 
................................................................................
182466 183477   ** each segment is "outside" and the area to the left is "inside".
182467 183478   **
182468 183479   ** The on-disk representation consists of a 4-byte header followed by
182469 183480   ** the values.  The 4-byte header is:
182470 183481   **
182471 183482   **      encoding    (1 byte)   0=big-endian, 1=little-endian
182472 183483   **      nvertex     (3 bytes)  Number of vertexes as a big-endian integer
       183484  +**
       183485  +** Enough space is allocated for 4 coordinates, to work around over-zealous
       183486  +** warnings coming from some compiler (notably, clang). In reality, the size
       183487  +** of each GeoPoly memory allocate is adjusted as necessary so that the
       183488  +** GeoPoly.a[] array at the end is the appropriate size.
182473 183489   */
182474 183490   typedef struct GeoPoly GeoPoly;
182475 183491   struct GeoPoly {
182476 183492     int nVertex;          /* Number of vertexes */
182477 183493     unsigned char hdr[4]; /* Header for on-disk representation */
182478         -  GeoCoord a[2];    /* 2*nVertex values. X (longitude) first, then Y */
       183494  +  GeoCoord a[8];        /* 2*nVertex values. X (longitude) first, then Y */
182479 183495   };
       183496  +
       183497  +/* The size of a memory allocation needed for a GeoPoly object sufficient
       183498  +** to hold N coordinate pairs.
       183499  +*/
       183500  +#define GEOPOLY_SZ(N)  (sizeof(GeoPoly) + sizeof(GeoCoord)*2*((N)-4))
182480 183501   
182481 183502   /*
182482 183503   ** State of a parse of a GeoJSON input.
182483 183504   */
182484 183505   typedef struct GeoParse GeoParse;
182485 183506   struct GeoParse {
182486 183507     const unsigned char *z;   /* Unparsed input */
................................................................................
182498 183519     t = a[1];
182499 183520     a[1] = a[2];
182500 183521     a[2] = t;
182501 183522   }
182502 183523   
182503 183524   /* Skip whitespace.  Return the next non-whitespace character. */
182504 183525   static char geopolySkipSpace(GeoParse *p){
182505         -  while( p->z[0] && safe_isspace(p->z[0]) ) p->z++;
       183526  +  while( safe_isspace(p->z[0]) ) p->z++;
182506 183527     return p->z[0];
182507 183528   }
182508 183529   
182509 183530   /* Parse out a number.  Write the value into *pVal if pVal!=0.
182510 183531   ** return non-zero on success and zero if the next token is not a number.
182511 183532   */
182512 183533   static int geopolyParseNumber(GeoParse *p, GeoCoord *pVal){
................................................................................
182518 183539     if( c=='-' ){
182519 183540       j = 1;
182520 183541       c = z[j];
182521 183542     }
182522 183543     if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0;
182523 183544     for(;; j++){
182524 183545       c = z[j];
182525         -    if( c>='0' && c<='9' ) continue;
       183546  +    if( safe_isdigit(c) ) continue;
182526 183547       if( c=='.' ){
182527 183548         if( z[j-1]=='-' ) return 0;
182528 183549         if( seenDP ) return 0;
182529 183550         seenDP = 1;
182530 183551         continue;
182531 183552       }
182532 183553       if( c=='e' || c=='E' ){
................................................................................
182540 183561         }
182541 183562         if( c<'0' || c>'9' ) return 0;
182542 183563         continue;
182543 183564       }
182544 183565       break;
182545 183566     }
182546 183567     if( z[j-1]<'0' ) return 0;
182547         -  if( pVal ) *pVal = (GeoCoord)atof((const char*)p->z);
       183568  +  if( pVal ){
       183569  +#ifdef SQLITE_AMALGAMATION
       183570  +     /* The sqlite3AtoF() routine is much much faster than atof(), if it
       183571  +     ** is available */
       183572  +     double r;
       183573  +     (void)sqlite3AtoF((const char*)p->z, &r, j, SQLITE_UTF8);
       183574  +     *pVal = r;
       183575  +#else
       183576  +     *pVal = (GeoCoord)atof((const char*)p->z);
       183577  +#endif
       183578  +  }
182548 183579     p->z += j;
182549 183580     return 1;
182550 183581   }
182551 183582   
182552 183583   /*
182553 183584   ** If the input is a well-formed JSON array of coordinates with at least
182554 183585   ** four coordinates and where each coordinate is itself a two-value array,
................................................................................
182598 183629       }
182599 183630       if( geopolySkipSpace(&s)==']'
182600 183631        && s.nVertex>=4
182601 183632        && s.a[0]==s.a[s.nVertex*2-2]
182602 183633        && s.a[1]==s.a[s.nVertex*2-1]
182603 183634        && (s.z++, geopolySkipSpace(&s)==0)
182604 183635       ){
182605         -      int nByte;
182606 183636         GeoPoly *pOut;
182607 183637         int x = 1;
182608 183638         s.nVertex--;  /* Remove the redundant vertex at the end */
182609         -      nByte = sizeof(GeoPoly) * s.nVertex*2*sizeof(GeoCoord);
182610         -      pOut = sqlite3_malloc64( nByte );
       183639  +      pOut = sqlite3_malloc64( GEOPOLY_SZ(s.nVertex) );
182611 183640         x = 1;
182612 183641         if( pOut==0 ) goto parse_json_err;
182613 183642         pOut->nVertex = s.nVertex;
182614 183643         memcpy(pOut->a, s.a, s.nVertex*2*sizeof(GeoCoord));
182615 183644         pOut->hdr[0] = *(unsigned char*)&x;
182616 183645         pOut->hdr[1] = (s.nVertex>>16)&0xff;
182617 183646         pOut->hdr[2] = (s.nVertex>>8)&0xff;
................................................................................
182805 183834         p->a[ii*2+1] = y1;
182806 183835       }
182807 183836       sqlite3_result_blob(context, p->hdr, 
182808 183837          4+8*p->nVertex, SQLITE_TRANSIENT);
182809 183838       sqlite3_free(p);
182810 183839     }
182811 183840   }
       183841  +
       183842  +/*
       183843  +** Compute the area enclosed by the polygon.
       183844  +**
       183845  +** This routine can also be used to detect polygons that rotate in
       183846  +** the wrong direction.  Polygons are suppose to be counter-clockwise (CCW).
       183847  +** This routine returns a negative value for clockwise (CW) polygons.
       183848  +*/
       183849  +static double geopolyArea(GeoPoly *p){
       183850  +  double rArea = 0.0;
       183851  +  int ii;
       183852  +  for(ii=0; ii<p->nVertex-1; ii++){
       183853  +    rArea += (p->a[ii*2] - p->a[ii*2+2])           /* (x0 - x1) */
       183854  +              * (p->a[ii*2+1] + p->a[ii*2+3])      /* (y0 + y1) */
       183855  +              * 0.5;
       183856  +  }
       183857  +  rArea += (p->a[ii*2] - p->a[0])                  /* (xN - x0) */
       183858  +           * (p->a[ii*2+1] + p->a[1])              /* (yN + y0) */
       183859  +           * 0.5;
       183860  +  return rArea;
       183861  +}
182812 183862   
182813 183863   /*
182814 183864   ** Implementation of the geopoly_area(X) function.
182815 183865   **
182816 183866   ** If the input is a well-formed Geopoly BLOB then return the area
182817 183867   ** enclosed by the polygon.  If the polygon circulates clockwise instead
182818 183868   ** of counterclockwise (as it should) then return the negative of the
................................................................................
182821 183871   static void geopolyAreaFunc(
182822 183872     sqlite3_context *context,
182823 183873     int argc,
182824 183874     sqlite3_value **argv
182825 183875   ){
182826 183876     GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
182827 183877     if( p ){
182828         -    double rArea = 0.0;
182829         -    int ii;
182830         -    for(ii=0; ii<p->nVertex-1; ii++){
182831         -      rArea += (p->a[ii*2] - p->a[ii*2+2])           /* (x0 - x1) */
182832         -                * (p->a[ii*2+1] + p->a[ii*2+3])      /* (y0 + y1) */
182833         -                * 0.5;
182834         -    }
182835         -    rArea += (p->a[ii*2] - p->a[0])                  /* (xN - x0) */
182836         -             * (p->a[ii*2+1] + p->a[1])              /* (yN + y0) */
182837         -             * 0.5;
182838         -    sqlite3_result_double(context, rArea);
       183878  +    sqlite3_result_double(context, geopolyArea(p));
       183879  +    sqlite3_free(p);
       183880  +  }            
       183881  +}
       183882  +
       183883  +/*
       183884  +** Implementation of the geopoly_ccw(X) function.
       183885  +**
       183886  +** If the rotation of polygon X is clockwise (incorrect) instead of
       183887  +** counter-clockwise (the correct winding order according to RFC7946)
       183888  +** then reverse the order of the vertexes in polygon X.  
       183889  +**
       183890  +** In other words, this routine returns a CCW polygon regardless of the
       183891  +** winding order of its input.
       183892  +**
       183893  +** Use this routine to sanitize historical inputs that that sometimes
       183894  +** contain polygons that wind in the wrong direction.
       183895  +*/
       183896  +static void geopolyCcwFunc(
       183897  +  sqlite3_context *context,
       183898  +  int argc,
       183899  +  sqlite3_value **argv
       183900  +){
       183901  +  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
       183902  +  if( p ){
       183903  +    if( geopolyArea(p)<0.0 ){
       183904  +      int ii, jj;
       183905  +      for(ii=2, jj=p->nVertex*2 - 2; ii<jj; ii+=2, jj-=2){
       183906  +        GeoCoord t = p->a[ii];
       183907  +        p->a[ii] = p->a[jj];
       183908  +        p->a[jj] = t;
       183909  +        t = p->a[ii+1];
       183910  +        p->a[ii+1] = p->a[jj+1];
       183911  +        p->a[jj+1] = t;
       183912  +      }
       183913  +    }
       183914  +    sqlite3_result_blob(context, p->hdr, 
       183915  +       4+8*p->nVertex, SQLITE_TRANSIENT);
182839 183916       sqlite3_free(p);
182840 183917     }            
182841 183918   }
       183919  +
       183920  +#define GEOPOLY_PI 3.1415926535897932385
       183921  +
       183922  +/* Fast approximation for sine(X) for X between -0.5*pi and 2*pi
       183923  +*/
       183924  +static double geopolySine(double r){
       183925  +  assert( r>=-0.5*GEOPOLY_PI && r<=2.0*GEOPOLY_PI );
       183926  +  if( r>=1.5*GEOPOLY_PI ){
       183927  +    r -= 2.0*GEOPOLY_PI;
       183928  +  }
       183929  +  if( r>=0.5*GEOPOLY_PI ){
       183930  +    return -geopolySine(r-GEOPOLY_PI);
       183931  +  }else{
       183932  +    double r2 = r*r;
       183933  +    double r3 = r2*r;
       183934  +    double r5 = r3*r2;
       183935  +    return 0.9996949*r - 0.1656700*r3 + 0.0075134*r5;
       183936  +  }
       183937  +}
       183938  +
       183939  +/*
       183940  +** Function:   geopoly_regular(X,Y,R,N)
       183941  +**
       183942  +** Construct a simple, convex, regular polygon centered at X, Y
       183943  +** with circumradius R and with N sides.
       183944  +*/
       183945  +static void geopolyRegularFunc(
       183946  +  sqlite3_context *context,
       183947  +  int argc,
       183948  +  sqlite3_value **argv
       183949  +){
       183950  +  double x = sqlite3_value_double(argv[0]);
       183951  +  double y = sqlite3_value_double(argv[1]);
       183952  +  double r = sqlite3_value_double(argv[2]);
       183953  +  int n = sqlite3_value_int(argv[3]);
       183954  +  int i;
       183955  +  GeoPoly *p;
       183956  +
       183957  +  if( n<3 || r<=0.0 ) return;
       183958  +  if( n>1000 ) n = 1000;
       183959  +  p = sqlite3_malloc64( sizeof(*p) + (n-1)*2*sizeof(GeoCoord) );
       183960  +  if( p==0 ){
       183961  +    sqlite3_result_error_nomem(context);
       183962  +    return;
       183963  +  }
       183964  +  i = 1;
       183965  +  p->hdr[0] = *(unsigned char*)&i;
       183966  +  p->hdr[1] = 0;
       183967  +  p->hdr[2] = (n>>8)&0xff;
       183968  +  p->hdr[3] = n&0xff;
       183969  +  for(i=0; i<n; i++){
       183970  +    double rAngle = 2.0*GEOPOLY_PI*i/n;
       183971  +    p->a[i*2] = x - r*geopolySine(rAngle-0.5*GEOPOLY_PI);
       183972  +    p->a[i*2+1] = y + r*geopolySine(rAngle);
       183973  +  }
       183974  +  sqlite3_result_blob(context, p->hdr, 4+8*n, SQLITE_TRANSIENT);
       183975  +  sqlite3_free(p);
       183976  +}
182842 183977   
182843 183978   /*
182844 183979   ** If pPoly is a polygon, compute its bounding box. Then:
182845 183980   **
182846 183981   **    (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
182847 183982   **    (2) otherwise, compute a GeoPoly for the bounding box and return the
182848 183983   **        new GeoPoly
................................................................................
182880 184015         r = p->a[ii*2+1];
182881 184016         if( r<mnY ) mnY = (float)r;
182882 184017         else if( r>mxY ) mxY = (float)r;
182883 184018       }
182884 184019       if( pRc ) *pRc = SQLITE_OK;
182885 184020       if( aCoord==0 ){
182886 184021         geopolyBboxFill:
182887         -      pOut = sqlite3_realloc(p, sizeof(GeoPoly)+sizeof(GeoCoord)*6);
       184022  +      pOut = sqlite3_realloc(p, GEOPOLY_SZ(4));
182888 184023         if( pOut==0 ){
182889 184024           sqlite3_free(p);
182890 184025           if( context ) sqlite3_result_error_nomem(context);
182891 184026           if( pRc ) *pRc = SQLITE_NOMEM;
182892 184027           return 0;
182893 184028         }
182894 184029         pOut->nVertex = 4;
................................................................................
183908 185043       int jj;
183909 185044       int nChange = 0;
183910 185045       sqlite3_bind_int64(pUp, 1, cell.iRowid);
183911 185046       assert( pRtree->nAux>=1 );
183912 185047       if( sqlite3_value_nochange(aData[2]) ){
183913 185048         sqlite3_bind_null(pUp, 2);
183914 185049       }else{
183915         -      sqlite3_bind_value(pUp, 2, aData[2]);
       185050  +      GeoPoly *p = 0;
       185051  +      if( sqlite3_value_type(aData[2])==SQLITE_TEXT
       185052  +       && (p = geopolyFuncParam(0, aData[2], &rc))!=0
       185053  +       && rc==SQLITE_OK
       185054  +      ){
       185055  +        sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT);
       185056  +      }else{
       185057  +        sqlite3_bind_value(pUp, 2, aData[2]);
       185058  +      }
       185059  +      sqlite3_free(p);
183916 185060         nChange = 1;
183917 185061       }
183918 185062       for(jj=1; jj<pRtree->nAux; jj++){
183919 185063         nChange++;
183920 185064         sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
183921 185065       }
183922 185066       if( nChange ){
................................................................................
183952 185096       return SQLITE_INDEX_CONSTRAINT_FUNCTION+1;
183953 185097     }
183954 185098     return 0;
183955 185099   }
183956 185100   
183957 185101   
183958 185102   static sqlite3_module geopolyModule = {
183959         -  2,                          /* iVersion */
       185103  +  3,                          /* iVersion */
183960 185104     geopolyCreate,              /* xCreate - create a table */
183961 185105     geopolyConnect,             /* xConnect - connect to an existing table */
183962 185106     geopolyBestIndex,           /* xBestIndex - Determine search strategy */
183963 185107     rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
183964 185108     rtreeDestroy,               /* xDestroy - Drop a table */
183965 185109     rtreeOpen,                  /* xOpen - open a cursor */
183966 185110     rtreeClose,                 /* xClose - close a cursor */
................................................................................
183975 185119     rtreeEndTransaction,        /* xCommit - commit transaction */
183976 185120     rtreeEndTransaction,        /* xRollback - rollback transaction */
183977 185121     geopolyFindFunction,        /* xFindFunction - function overloading */
183978 185122     rtreeRename,                /* xRename - rename the table */
183979 185123     rtreeSavepoint,             /* xSavepoint */
183980 185124     0,                          /* xRelease */
183981 185125     0,                          /* xRollbackTo */
       185126  +  rtreeShadowName             /* xShadowName */
183982 185127   };
183983 185128   
183984 185129   static int sqlite3_geopoly_init(sqlite3 *db){
183985 185130     int rc = SQLITE_OK;
183986 185131     static const struct {
183987 185132       void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
183988         -    int nArg;
       185133  +    signed char nArg;
       185134  +    unsigned char bPure;
183989 185135       const char *zName;
183990 185136     } aFunc[] = {
183991         -     { geopolyAreaFunc,          1,    "geopoly_area"             },
183992         -     { geopolyBlobFunc,          1,    "geopoly_blob"             },
183993         -     { geopolyJsonFunc,          1,    "geopoly_json"             },
183994         -     { geopolySvgFunc,          -1,    "geopoly_svg"              },
183995         -     { geopolyWithinFunc,        2,    "geopoly_within"           },
183996         -     { geopolyContainsPointFunc, 3,    "geopoly_contains_point"   },
183997         -     { geopolyOverlapFunc,       2,    "geopoly_overlap"          },
183998         -     { geopolyDebugFunc,         1,    "geopoly_debug"            },
183999         -     { geopolyBBoxFunc,          1,    "geopoly_bbox"             },
184000         -     { geopolyXformFunc,         7,    "geopoly_xform"            },
       185137  +     { geopolyAreaFunc,          1, 1,    "geopoly_area"             },
       185138  +     { geopolyBlobFunc,          1, 1,    "geopoly_blob"             },
       185139  +     { geopolyJsonFunc,          1, 1,    "geopoly_json"             },
       185140  +     { geopolySvgFunc,          -1, 1,    "geopoly_svg"              },
       185141  +     { geopolyWithinFunc,        2, 1,    "geopoly_within"           },
       185142  +     { geopolyContainsPointFunc, 3, 1,    "geopoly_contains_point"   },
       185143  +     { geopolyOverlapFunc,       2, 1,    "geopoly_overlap"          },
       185144  +     { geopolyDebugFunc,         1, 0,    "geopoly_debug"            },
       185145  +     { geopolyBBoxFunc,          1, 1,    "geopoly_bbox"             },
       185146  +     { geopolyXformFunc,         7, 1,    "geopoly_xform"            },
       185147  +     { geopolyRegularFunc,       4, 1,    "geopoly_regular"          },
       185148  +     { geopolyCcwFunc,           1, 1,    "geopoly_ccw"              },
184001 185149     };
184002 185150     static const struct {
184003 185151       void (*xStep)(sqlite3_context*,int,sqlite3_value**);
184004 185152       void (*xFinal)(sqlite3_context*);
184005 185153       const char *zName;
184006 185154     } aAgg[] = {
184007 185155        { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox"    },
184008 185156     };
184009 185157     int i;
184010 185158     for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
       185159  +    int enc = aFunc[i].bPure ? SQLITE_UTF8|SQLITE_DETERMINISTIC : SQLITE_UTF8;
184011 185160       rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
184012         -                                 SQLITE_UTF8, 0,
       185161  +                                 enc, 0,
184013 185162                                    aFunc[i].xFunc, 0, 0);
184014 185163     }
184015 185164     for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
184016 185165       rc = sqlite3_create_function(db, aAgg[i].zName, 1, SQLITE_UTF8, 0,
184017 185166                                    0, aAgg[i].xStep, aAgg[i].xFinal);
184018 185167     }
184019 185168     if( rc==SQLITE_OK ){
................................................................................
186032 187181   ** space used by the RBU handle.
186033 187182   */
186034 187183   struct rbu_vfs {
186035 187184     sqlite3_vfs base;               /* rbu VFS shim methods */
186036 187185     sqlite3_vfs *pRealVfs;          /* Underlying VFS */
186037 187186     sqlite3_mutex *mutex;           /* Mutex to protect pMain */
186038 187187     sqlite3rbu *pRbu;               /* Owner RBU object */
186039         -  rbu_file *pMain;                /* Linked list of main db files */
       187188  +  rbu_file *pMain;                /* List of main db files */
       187189  +  rbu_file *pMainRbu;             /* List of main db files with pRbu!=0 */
186040 187190   };
186041 187191   
186042 187192   /*
186043 187193   ** Each file opened by an rbu VFS is represented by an instance of
186044 187194   ** the following structure.
186045 187195   **
186046 187196   ** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
................................................................................
186061 187211     int nShm;                       /* Number of entries in apShm[] array */
186062 187212     char **apShm;                   /* Array of mmap'd *-shm regions */
186063 187213     char *zDel;                     /* Delete this when closing file */
186064 187214   
186065 187215     const char *zWal;               /* Wal filename for this main db file */
186066 187216     rbu_file *pWalFd;               /* Wal file descriptor for this main db */
186067 187217     rbu_file *pMainNext;            /* Next MAIN_DB file */
       187218  +  rbu_file *pMainRbuNext;         /* Next MAIN_DB file with pRbu!=0 */
186068 187219   };
186069 187220   
186070 187221   /*
186071 187222   ** True for an RBU vacuum handle, or false otherwise.
186072 187223   */
186073 187224   #define rbuIsVacuum(p) ((p)->zTarget==0)
186074 187225   
................................................................................
189656 190807     i64 nDiff = nNew - pFd->sz;
189657 190808     pRbu->szTemp += nDiff;
189658 190809     pFd->sz = nNew;
189659 190810     assert( pRbu->szTemp>=0 );
189660 190811     if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL;
189661 190812     return SQLITE_OK;
189662 190813   }
       190814  +
       190815  +/*
       190816  +** Add an item to the main-db lists, if it is not already present.
       190817  +**
       190818  +** There are two main-db lists. One for all file descriptors, and one
       190819  +** for all file descriptors with rbu_file.pDb!=0. If the argument has
       190820  +** rbu_file.pDb!=0, then it is assumed to already be present on the
       190821  +** main list and is only added to the pDb!=0 list.
       190822  +*/
       190823  +static void rbuMainlistAdd(rbu_file *p){
       190824  +  rbu_vfs *pRbuVfs = p->pRbuVfs;
       190825  +  rbu_file *pIter;
       190826  +  assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) );
       190827  +  sqlite3_mutex_enter(pRbuVfs->mutex);
       190828  +  if( p->pRbu==0 ){
       190829  +    for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
       190830  +    p->pMainNext = pRbuVfs->pMain;
       190831  +    pRbuVfs->pMain = p;
       190832  +  }else{
       190833  +    for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
       190834  +    if( pIter==0 ){
       190835  +      p->pMainRbuNext = pRbuVfs->pMainRbu;
       190836  +      pRbuVfs->pMainRbu = p;
       190837  +    }
       190838  +  }
       190839  +  sqlite3_mutex_leave(pRbuVfs->mutex);
       190840  +}
       190841  +
       190842  +/*
       190843  +** Remove an item from the main-db lists.
       190844  +*/
       190845  +static void rbuMainlistRemove(rbu_file *p){
       190846  +  rbu_file **pp;
       190847  +  sqlite3_mutex_enter(p->pRbuVfs->mutex);
       190848  +  for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
       190849  +  if( *pp ) *pp = p->pMainNext;
       190850  +  p->pMainNext = 0;
       190851  +  for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
       190852  +  if( *pp ) *pp = p->pMainRbuNext;
       190853  +  p->pMainRbuNext = 0;
       190854  +  sqlite3_mutex_leave(p->pRbuVfs->mutex);
       190855  +}
       190856  +
       190857  +/*
       190858  +** Given that zWal points to a buffer containing a wal file name passed to 
       190859  +** either the xOpen() or xAccess() VFS method, search the main-db list for
       190860  +** a file-handle opened by the same database connection on the corresponding
       190861  +** database file.
       190862  +**
       190863  +** If parameter bRbu is true, only search for file-descriptors with
       190864  +** rbu_file.pDb!=0.
       190865  +*/
       190866  +static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){
       190867  +  rbu_file *pDb;
       190868  +  sqlite3_mutex_enter(pRbuVfs->mutex);
       190869  +  if( bRbu ){
       190870  +    for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
       190871  +  }else{
       190872  +    for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
       190873  +  }
       190874  +  sqlite3_mutex_leave(pRbuVfs->mutex);
       190875  +  return pDb;
       190876  +}
189663 190877   
189664 190878   /*
189665 190879   ** Close an rbu file.
189666 190880   */
189667 190881   static int rbuVfsClose(sqlite3_file *pFile){
189668 190882     rbu_file *p = (rbu_file*)pFile;
189669 190883     int rc;
................................................................................
189674 190888       sqlite3_free(p->apShm[i]);
189675 190889     }
189676 190890     sqlite3_free(p->apShm);
189677 190891     p->apShm = 0;
189678 190892     sqlite3_free(p->zDel);
189679 190893   
189680 190894     if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
189681         -    rbu_file **pp;
189682         -    sqlite3_mutex_enter(p->pRbuVfs->mutex);
189683         -    for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
189684         -    *pp = p->pMainNext;
189685         -    sqlite3_mutex_leave(p->pRbuVfs->mutex);
       190895  +    rbuMainlistRemove(p);
189686 190896       rbuUnlockShm(p);
189687 190897       p->pReal->pMethods->xShmUnmap(p->pReal, 0);
189688 190898     }
189689 190899     else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
189690 190900       rbuUpdateTempSize(p, 0);
189691 190901     }
       190902  +  assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p );
189692 190903   
189693 190904     /* Close the underlying file handle */
189694 190905     rc = p->pReal->pMethods->xClose(p->pReal);
189695 190906     return rc;
189696 190907   }
189697 190908   
189698 190909   
................................................................................
189943 191154         rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
189944 191155         if( rc==SQLITE_OK ){
189945 191156           rc = SQLITE_ERROR;
189946 191157           pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
189947 191158         }else if( rc==SQLITE_NOTFOUND ){
189948 191159           pRbu->pTargetFd = p;
189949 191160           p->pRbu = pRbu;
       191161  +        if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
       191162  +          rbuMainlistAdd(p);
       191163  +        }
189950 191164           if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
189951 191165           rc = SQLITE_OK;
189952 191166         }
189953 191167       }
189954 191168       return rc;
189955 191169     }
189956 191170     else if( op==SQLITE_FCNTL_RBUCNT ){
................................................................................
190104 191318       /* Release the checkpointer and writer locks */
190105 191319       rbuUnlockShm(p);
190106 191320       rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
190107 191321     }
190108 191322     return rc;
190109 191323   }
190110 191324   
190111         -/*
190112         -** Given that zWal points to a buffer containing a wal file name passed to 
190113         -** either the xOpen() or xAccess() VFS method, return a pointer to the
190114         -** file-handle opened by the same database connection on the corresponding
190115         -** database file.
190116         -*/
190117         -static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal){
190118         -  rbu_file *pDb;
190119         -  sqlite3_mutex_enter(pRbuVfs->mutex);
190120         -  for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
190121         -  sqlite3_mutex_leave(pRbuVfs->mutex);
190122         -  return pDb;
190123         -}
190124         -
190125 191325   /* 
190126 191326   ** A main database named zName has just been opened. The following 
190127 191327   ** function returns a pointer to a buffer owned by SQLite that contains
190128 191328   ** the name of the *-wal file this db connection will use. SQLite
190129 191329   ** happens to pass a pointer to this buffer when using xAccess()
190130 191330   ** or xOpen() to operate on the *-wal file.  
190131 191331   */
................................................................................
190196 191396         ** (pFd->zWal) to point to a buffer owned by SQLite that contains
190197 191397         ** the name of the *-wal file this db connection will use. SQLite
190198 191398         ** happens to pass a pointer to this buffer when using xAccess()
190199 191399         ** or xOpen() to operate on the *-wal file.  */
190200 191400         pFd->zWal = rbuMainToWal(zName, flags);
190201 191401       }
190202 191402       else if( flags & SQLITE_OPEN_WAL ){
190203         -      rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName);
       191403  +      rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0);
190204 191404         if( pDb ){
190205 191405           if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
190206 191406             /* This call is to open a *-wal file. Intead, open the *-oal. This
190207 191407             ** code ensures that the string passed to xOpen() is terminated by a
190208 191408             ** pair of '\0' bytes in case the VFS attempts to extract a URI 
190209 191409             ** parameter from it.  */
190210 191410             const char *zBase = zName;
................................................................................
190248 191448     }
190249 191449     if( pFd->pReal->pMethods ){
190250 191450       /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
190251 191451       ** pointer and, if the file is a main database file, link it into the
190252 191452       ** mutex protected linked list of all such files.  */
190253 191453       pFile->pMethods = &rbuvfs_io_methods;
190254 191454       if( flags & SQLITE_OPEN_MAIN_DB ){
190255         -      sqlite3_mutex_enter(pRbuVfs->mutex);
190256         -      pFd->pMainNext = pRbuVfs->pMain;
190257         -      pRbuVfs->pMain = pFd;
190258         -      sqlite3_mutex_leave(pRbuVfs->mutex);
       191455  +      rbuMainlistAdd(pFd);
190259 191456       }
190260 191457     }else{
190261 191458       sqlite3_free(pFd->zDel);
190262 191459     }
190263 191460   
190264 191461     return rc;
190265 191462   }
................................................................................
190299 191496     **
190300 191497     **   b) if the *-wal file does not exist, claim that it does anyway,
190301 191498     **      causing SQLite to call xOpen() to open it. This call will also
190302 191499     **      be intercepted (see the rbuVfsOpen() function) and the *-oal
190303 191500     **      file opened instead.
190304 191501     */
190305 191502     if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
190306         -    rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath);
       191503  +    rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
190307 191504       if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
190308 191505         if( *pResOut ){
190309 191506           rc = SQLITE_CANTOPEN;
190310 191507         }else{
190311 191508           sqlite3_int64 sz = 0;
190312 191509           rc = rbuVfsFileSize(&pDb->base, &sz);
190313 191510           *pResOut = (sz>0);
................................................................................
190712 191909   ** operate on a different database schema, so check for it.
190713 191910   **
190714 191911   ** idxNum is normally 0, but will be 1 if a schema=? constraint exists.
190715 191912   */
190716 191913   static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
190717 191914     int i;
190718 191915   
190719         -  pIdxInfo->estimatedCost = 1.0e6;  /* Initial cost estimate */
190720         -
190721 191916     /* Look for a valid schema=? constraint.  If found, change the idxNum to
190722 191917     ** 1 and request the value of that constraint be sent to xFilter.  And
190723 191918     ** lower the cost estimate to encourage the constrained version to be
190724 191919     ** used.
190725 191920     */
190726 191921     for(i=0; i<pIdxInfo->nConstraint; i++){
190727         -    if( pIdxInfo->aConstraint[i].usable==0 ) continue;
190728         -    if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
190729 191922       if( pIdxInfo->aConstraint[i].iColumn!=10 ) continue;
       191923  +    if( pIdxInfo->aConstraint[i].usable==0 ) return SQLITE_CONSTRAINT;
       191924  +    if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
190730 191925       pIdxInfo->idxNum = 1;
190731 191926       pIdxInfo->estimatedCost = 1.0;
190732 191927       pIdxInfo->aConstraintUsage[i].argvIndex = 1;
190733 191928       pIdxInfo->aConstraintUsage[i].omit = 1;
190734 191929       break;
190735 191930     }
190736 191931   
................................................................................
190772 191967       pCsr->iDb = pTab->iDb;
190773 191968     }
190774 191969   
190775 191970     *ppCursor = (sqlite3_vtab_cursor *)pCsr;
190776 191971     return SQLITE_OK;
190777 191972   }
190778 191973   
190779         -static void statClearPage(StatPage *p){
       191974  +static void statClearCells(StatPage *p){
190780 191975     int i;
190781 191976     if( p->aCell ){
190782 191977       for(i=0; i<p->nCell; i++){
190783 191978         sqlite3_free(p->aCell[i].aOvfl);
190784 191979       }
190785 191980       sqlite3_free(p->aCell);
190786 191981     }
       191982  +  p->nCell = 0;
       191983  +  p->aCell = 0;
       191984  +}
       191985  +
       191986  +static void statClearPage(StatPage *p){
       191987  +  statClearCells(p);
190787 191988     sqlite3PagerUnref(p->pPg);
190788 191989     sqlite3_free(p->zPath);
190789 191990     memset(p, 0, sizeof(StatPage));
190790 191991   }
190791 191992   
190792 191993   static void statResetCsr(StatCursor *pCsr){
190793 191994     int i;
................................................................................
190842 192043     int isLeaf;
190843 192044     int szPage;
190844 192045   
190845 192046     u8 *aData = sqlite3PagerGetData(p->pPg);
190846 192047     u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
190847 192048   
190848 192049     p->flags = aHdr[0];
       192050  +  if( p->flags==0x0A || p->flags==0x0D ){
       192051  +    isLeaf = 1;
       192052  +    nHdr = 8;
       192053  +  }else if( p->flags==0x05 || p->flags==0x02 ){
       192054  +    isLeaf = 0;
       192055  +    nHdr = 12;
       192056  +  }else{
       192057  +    goto statPageIsCorrupt;
       192058  +  }
       192059  +  if( p->iPgno==1 ) nHdr += 100;
190849 192060     p->nCell = get2byte(&aHdr[3]);
190850 192061     p->nMxPayload = 0;
190851         -
190852         -  isLeaf = (p->flags==0x0A || p->flags==0x0D);
190853         -  nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100;
       192062  +  szPage = sqlite3BtreeGetPageSize(pBt);
190854 192063   
190855 192064     nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
190856 192065     nUnused += (int)aHdr[7];
190857 192066     iOff = get2byte(&aHdr[1]);
190858 192067     while( iOff ){
       192068  +    int iNext;
       192069  +    if( iOff>=szPage ) goto statPageIsCorrupt;
190859 192070       nUnused += get2byte(&aData[iOff+2]);
190860         -    iOff = get2byte(&aData[iOff]);
       192071  +    iNext = get2byte(&aData[iOff]);
       192072  +    if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
       192073  +    iOff = iNext;
190861 192074     }
190862 192075     p->nUnused = nUnused;
190863 192076     p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
190864         -  szPage = sqlite3BtreeGetPageSize(pBt);
190865 192077   
190866 192078     if( p->nCell ){
190867 192079       int i;                        /* Used to iterate through cells */
190868 192080       int nUsable;                  /* Usable bytes per page */
190869 192081   
190870 192082       sqlite3BtreeEnter(pBt);
190871 192083       nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
................................................................................
190874 192086       if( p->aCell==0 ) return SQLITE_NOMEM_BKPT;
190875 192087       memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
190876 192088   
190877 192089       for(i=0; i<p->nCell; i++){
190878 192090         StatCell *pCell = &p->aCell[i];
190879 192091   
190880 192092         iOff = get2byte(&aData[nHdr+i*2]);
       192093  +      if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
190881 192094         if( !isLeaf ){
190882 192095           pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
190883 192096           iOff += 4;
190884 192097         }
190885 192098         if( p->flags==0x05 ){
190886 192099           /* A table interior node. nPayload==0. */
190887 192100         }else{
................................................................................
190890 192103           iOff += getVarint32(&aData[iOff], nPayload);
190891 192104           if( p->flags==0x0D ){
190892 192105             u64 dummy;
190893 192106             iOff += sqlite3GetVarint(&aData[iOff], &dummy);
190894 192107           }
190895 192108           if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
190896 192109           getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
       192110  +        if( nLocal<0 ) goto statPageIsCorrupt;
190897 192111           pCell->nLocal = nLocal;
190898         -        assert( nLocal>=0 );
190899 192112           assert( nPayload>=(u32)nLocal );
190900 192113           assert( nLocal<=(nUsable-35) );
190901 192114           if( nPayload>(u32)nLocal ){
190902 192115             int j;
190903 192116             int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
       192117  +          if( iOff+nLocal>nUsable ) goto statPageIsCorrupt;
190904 192118             pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
190905 192119             pCell->nOvfl = nOvfl;
190906 192120             pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
190907 192121             if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
190908 192122             pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
190909 192123             for(j=1; j<nOvfl; j++){
190910 192124               int rc;
................................................................................
190920 192134             }
190921 192135           }
190922 192136         }
190923 192137       }
190924 192138     }
190925 192139   
190926 192140     return SQLITE_OK;
       192141  +
       192142  +statPageIsCorrupt:
       192143  +  p->flags = 0;
       192144  +  statClearCells(p);
       192145  +  return SQLITE_OK;
190927 192146   }
190928 192147   
190929 192148   /*
190930 192149   ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
190931 192150   ** the current value of pCsr->iPageno.
190932 192151   */
190933 192152   static void statSizeAndOffset(StatCursor *pCsr){
................................................................................
191215 192434       0,                            /* xCommit */
191216 192435       0,                            /* xRollback */
191217 192436       0,                            /* xFindMethod */
191218 192437       0,                            /* xRename */
191219 192438       0,                            /* xSavepoint */
191220 192439       0,                            /* xRelease */
191221 192440       0,                            /* xRollbackTo */
       192441  +    0                             /* xShadowName */
191222 192442     };
191223 192443     return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
191224 192444   }
191225 192445   #elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
191226 192446   SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
191227 192447   #endif /* SQLITE_ENABLE_DBSTAT_VTAB */
191228 192448   
................................................................................
191345 192565     ** unavailable
191346 192566     */
191347 192567     for(i=0; i<pIdxInfo->nConstraint; i++){
191348 192568       struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
191349 192569       if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
191350 192570       if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
191351 192571       if( !p->usable ){
191352         -      /* No solution.  Use the default SQLITE_BIG_DBL cost */
191353         -      pIdxInfo->estimatedRows = 0x7fffffff;
191354         -      return SQLITE_OK;
       192572  +      /* No solution. */
       192573  +      return SQLITE_CONSTRAINT;
191355 192574       }
191356 192575       iPlan = 2;
191357 192576       pIdxInfo->aConstraintUsage[i].argvIndex = 1;
191358 192577       pIdxInfo->aConstraintUsage[i].omit = 1;
191359 192578       break;
191360 192579     }
191361 192580   
................................................................................
191539 192758     char *zErr = 0;
191540 192759     const char *zSchema;
191541 192760     int iDb;
191542 192761     Btree *pBt;
191543 192762     Pager *pPager;
191544 192763     int szPage;
191545 192764   
       192765  +  if( pTab->db->flags & SQLITE_Defensive ){
       192766  +    zErr = "read-only";
       192767  +    goto update_fail;
       192768  +  }
191546 192769     if( argc==1 ){
191547 192770       zErr = "cannot delete";
191548 192771       goto update_fail;
191549 192772     }
191550 192773     pgno = sqlite3_value_int(argv[0]);
191551 192774     if( (Pgno)sqlite3_value_int(argv[1])!=pgno ){
191552 192775       zErr = "cannot insert";
................................................................................
191629 192852       0,                            /* xCommit */
191630 192853       0,                            /* xRollback */
191631 192854       0,                            /* xFindMethod */
191632 192855       0,                            /* xRename */
191633 192856       0,                            /* xSavepoint */
191634 192857       0,                            /* xRelease */
191635 192858       0,                            /* xRollbackTo */
       192859  +    0                             /* xShadowName */
191636 192860     };
191637 192861     return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0);
191638 192862   }
191639 192863   #elif defined(SQLITE_ENABLE_DBPAGE_VTAB)
191640 192864   SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK; }
191641 192865   #endif /* SQLITE_ENABLE_DBSTAT_VTAB */
191642 192866   
................................................................................
191664 192888   #ifndef SESSIONS_STRM_CHUNK_SIZE
191665 192889   # ifdef SQLITE_TEST
191666 192890   #   define SESSIONS_STRM_CHUNK_SIZE 64
191667 192891   # else
191668 192892   #   define SESSIONS_STRM_CHUNK_SIZE 1024
191669 192893   # endif
191670 192894   #endif
       192895  +
       192896  +static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE;
191671 192897   
191672 192898   typedef struct SessionHook SessionHook;
191673 192899   struct SessionHook {
191674 192900     void *pCtx;
191675 192901     int (*xOld)(void*,int,sqlite3_value**);
191676 192902     int (*xNew)(void*,int,sqlite3_value**);
191677 192903     int (*xCount)(void*);
................................................................................
191727 192953   /*
191728 192954   ** Structure for changeset iterators.
191729 192955   */
191730 192956   struct sqlite3_changeset_iter {
191731 192957     SessionInput in;                /* Input buffer or stream */
191732 192958     SessionBuffer tblhdr;           /* Buffer to hold apValue/zTab/abPK/ */
191733 192959     int bPatchset;                  /* True if this is a patchset */
       192960  +  int bInvert;                    /* True to invert changeset */
191734 192961     int rc;                         /* Iterator error code */
191735 192962     sqlite3_stmt *pConflict;        /* Points to conflicting row, if any */
191736 192963     char *zTab;                     /* Current table */
191737 192964     int nCol;                       /* Number of columns in zTab */
191738 192965     int op;                         /* Current operation */
191739 192966     int bIndirect;                  /* True if current change was indirect */
191740 192967     u8 *abPK;                       /* Primary key array */
................................................................................
191883 193110   ** columns and columns that are modified by the UPDATE are set to "undefined".
191884 193111   ** PRIMARY KEY fields contain the values identifying the table row to update,
191885 193112   ** and fields associated with modified columns contain the new column values.
191886 193113   **
191887 193114   ** The records associated with INSERT changes are in the same format as for
191888 193115   ** changesets. It is not possible for a record associated with an INSERT
191889 193116   ** change to contain a field set to "undefined".
       193117  +**
       193118  +** REBASE BLOB FORMAT:
       193119  +**
       193120  +** A rebase blob may be output by sqlite3changeset_apply_v2() and its 
       193121  +** streaming equivalent for use with the sqlite3_rebaser APIs to rebase
       193122  +** existing changesets. A rebase blob contains one entry for each conflict
       193123  +** resolved using either the OMIT or REPLACE strategies within the apply_v2()
       193124  +** call.
       193125  +**
       193126  +** The format used for a rebase blob is very similar to that used for
       193127  +** changesets. All entries related to a single table are grouped together.
       193128  +**
       193129  +** Each group of entries begins with a table header in changeset format:
       193130  +**
       193131  +**   1 byte: Constant 0x54 (capital 'T')
       193132  +**   Varint: Number of columns in the table.
       193133  +**   nCol bytes: 0x01 for PK columns, 0x00 otherwise.
       193134  +**   N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
       193135  +**
       193136  +** Followed by one or more entries associated with the table.
       193137  +**
       193138  +**   1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09).
       193139  +**   1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT.
       193140  +**   record: (in the record format defined above).
       193141  +**
       193142  +** In a rebase blob, the first field is set to SQLITE_INSERT if the change
       193143  +** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if
       193144  +** it was a DELETE. The second field is set to 0x01 if the conflict 
       193145  +** resolution strategy was REPLACE, or 0x00 if it was OMIT.
       193146  +**
       193147  +** If the change that caused the conflict was a DELETE, then the single
       193148  +** record is a copy of the old.* record from the original changeset. If it
       193149  +** was an INSERT, then the single record is a copy of the new.* record. If
       193150  +** the conflicting change was an UPDATE, then the single record is a copy
       193151  +** of the new.* record with the PK fields filled in based on the original
       193152  +** old.* record.
191890 193153   */
191891 193154   
191892 193155   /*
191893 193156   ** For each row modified during a session, there exists a single instance of
191894 193157   ** this structure stored in a SessionTable.aChange[] hash table.
191895 193158   */
191896 193159   struct SessionChange {
................................................................................
193433 194696   **
193434 194697   ** If successful, return zero. Otherwise, if an OOM condition is encountered,
193435 194698   ** set *pRc to SQLITE_NOMEM and return non-zero.
193436 194699   */
193437 194700   static int sessionBufferGrow(SessionBuffer *p, int nByte, int *pRc){
193438 194701     if( *pRc==SQLITE_OK && p->nAlloc-p->nBuf<nByte ){
193439 194702       u8 *aNew;
193440         -    int nNew = p->nAlloc ? p->nAlloc : 128;
       194703  +    i64 nNew = p->nAlloc ? p->nAlloc : 128;
193441 194704       do {
193442 194705         nNew = nNew*2;
193443         -    }while( nNew<(p->nBuf+nByte) );
       194706  +    }while( (nNew-p->nBuf)<nByte );
193444 194707   
193445         -    aNew = (u8 *)sqlite3_realloc(p->aBuf, nNew);
       194708  +    aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
193446 194709       if( 0==aNew ){
193447 194710         *pRc = SQLITE_NOMEM;
193448 194711       }else{
193449 194712         p->aBuf = aNew;
193450 194713         p->nAlloc = nNew;
193451 194714       }
193452 194715     }
................................................................................
194036 195299             }else if( p->op!=SQLITE_INSERT ){
194037 195300               rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
194038 195301             }
194039 195302             if( rc==SQLITE_OK ){
194040 195303               rc = sqlite3_reset(pSel);
194041 195304             }
194042 195305   
194043         -          /* If the buffer is now larger than SESSIONS_STRM_CHUNK_SIZE, pass
       195306  +          /* If the buffer is now larger than sessions_strm_chunk_size, pass
194044 195307             ** its contents to the xOutput() callback. */
194045 195308             if( xOutput 
194046 195309              && rc==SQLITE_OK 
194047 195310              && buf.nBuf>nNoop 
194048         -           && buf.nBuf>SESSIONS_STRM_CHUNK_SIZE 
       195311  +           && buf.nBuf>sessions_strm_chunk_size 
194049 195312             ){
194050 195313               rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
194051 195314               nNoop = -1;
194052 195315               buf.nBuf = 0;
194053 195316             }
194054 195317   
194055 195318           }
................................................................................
194180 195443   ** Do the work for either sqlite3changeset_start() or start_strm().
194181 195444   */
194182 195445   static int sessionChangesetStart(
194183 195446     sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
194184 195447     int (*xInput)(void *pIn, void *pData, int *pnData),
194185 195448     void *pIn,
194186 195449     int nChangeset,                 /* Size of buffer pChangeset in bytes */
194187         -  void *pChangeset                /* Pointer to buffer containing changeset */
       195450  +  void *pChangeset,               /* Pointer to buffer containing changeset */
       195451  +  int bInvert                     /* True to invert changeset */
194188 195452   ){
194189 195453     sqlite3_changeset_iter *pRet;   /* Iterator to return */
194190 195454     int nByte;                      /* Number of bytes to allocate for iterator */
194191 195455   
194192 195456     assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
194193 195457   
194194 195458     /* Zero the output variable in case an error occurs. */
................................................................................
194200 195464     if( !pRet ) return SQLITE_NOMEM;
194201 195465     memset(pRet, 0, sizeof(sqlite3_changeset_iter));
194202 195466     pRet->in.aData = (u8 *)pChangeset;
194203 195467     pRet->in.nData = nChangeset;
194204 195468     pRet->in.xInput = xInput;
194205 195469     pRet->in.pIn = pIn;
194206 195470     pRet->in.bEof = (xInput ? 0 : 1);
       195471  +  pRet->bInvert = bInvert;
194207 195472   
194208 195473     /* Populate the output variable and return success. */
194209 195474     *pp = pRet;
194210 195475     return SQLITE_OK;
194211 195476   }
194212 195477   
194213 195478   /*
................................................................................
194214 195479   ** Create an iterator used to iterate through the contents of a changeset.
194215 195480   */
194216 195481   SQLITE_API int sqlite3changeset_start(
194217 195482     sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
194218 195483     int nChangeset,                 /* Size of buffer pChangeset in bytes */
194219 195484     void *pChangeset                /* Pointer to buffer containing changeset */
194220 195485   ){
194221         -  return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset);
       195486  +  return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0);
       195487  +}
       195488  +SQLITE_API int sqlite3changeset_start_v2(
       195489  +  sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
       195490  +  int nChangeset,                 /* Size of buffer pChangeset in bytes */
       195491  +  void *pChangeset,               /* Pointer to buffer containing changeset */
       195492  +  int flags
       195493  +){
       195494  +  int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
       195495  +  return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert);
194222 195496   }
194223 195497   
194224 195498   /*
194225 195499   ** Streaming version of sqlite3changeset_start().
194226 195500   */
194227 195501   SQLITE_API int sqlite3changeset_start_strm(
194228 195502     sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
194229 195503     int (*xInput)(void *pIn, void *pData, int *pnData),
194230 195504     void *pIn
194231 195505   ){
194232         -  return sessionChangesetStart(pp, xInput, pIn, 0, 0);
       195506  +  return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0);
       195507  +}
       195508  +SQLITE_API int sqlite3changeset_start_v2_strm(
       195509  +  sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
       195510  +  int (*xInput)(void *pIn, void *pData, int *pnData),
       195511  +  void *pIn,
       195512  +  int flags
       195513  +){
       195514  +  int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
       195515  +  return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert);
194233 195516   }
194234 195517   
194235 195518   /*
194236 195519   ** If the SessionInput object passed as the only argument is a streaming
194237 195520   ** object and the buffer is full, discard some data to free up space.
194238 195521   */
194239 195522   static void sessionDiscardData(SessionInput *pIn){
194240         -  if( pIn->xInput && pIn->iNext>=SESSIONS_STRM_CHUNK_SIZE ){
       195523  +  if( pIn->xInput && pIn->iNext>=sessions_strm_chunk_size ){
194241 195524       int nMove = pIn->buf.nBuf - pIn->iNext;
194242 195525       assert( nMove>=0 );
194243 195526       if( nMove>0 ){
194244 195527         memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
194245 195528       }
194246 195529       pIn->buf.nBuf -= pIn->iNext;
194247 195530       pIn->iNext = 0;
................................................................................
194256 195539   **
194257 195540   ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
194258 195541   */
194259 195542   static int sessionInputBuffer(SessionInput *pIn, int nByte){
194260 195543     int rc = SQLITE_OK;
194261 195544     if( pIn->xInput ){
194262 195545       while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
194263         -      int nNew = SESSIONS_STRM_CHUNK_SIZE;
       195546  +      int nNew = sessions_strm_chunk_size;
194264 195547   
194265 195548         if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
194266 195549         if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
194267 195550           rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
194268 195551           if( nNew==0 ){
194269 195552             pIn->bEof = 1;
194270 195553           }else{
................................................................................
194604 195887       if( sessionChangesetReadTblhdr(p) ) return p->rc;
194605 195888       if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
194606 195889       p->in.iCurrent = p->in.iNext;
194607 195890       if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
194608 195891       op = p->in.aData[p->in.iNext++];
194609 195892     }
194610 195893   
194611         -  if( p->zTab==0 ){
       195894  +  if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
194612 195895       /* The first record in the changeset is not a table header. Must be a
194613 195896       ** corrupt changeset. */
194614         -    assert( p->in.iNext==1 );
       195897  +    assert( p->in.iNext==1 || p->zTab );
194615 195898       return (p->rc = SQLITE_CORRUPT_BKPT);
194616 195899     }
194617 195900   
194618 195901     p->op = op;
194619 195902     p->bIndirect = p->in.aData[p->in.iNext++];
194620 195903     if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
194621 195904       return (p->rc = SQLITE_CORRUPT_BKPT);
................................................................................
194632 195915         nVal = p->nCol;
194633 195916       }
194634 195917       p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
194635 195918       if( p->rc!=SQLITE_OK ) return p->rc;
194636 195919       *paRec = &p->in.aData[p->in.iNext];
194637 195920       p->in.iNext += *pnRec;
194638 195921     }else{
       195922  +    sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
       195923  +    sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
194639 195924   
194640 195925       /* If this is an UPDATE or DELETE, read the old.* record. */
194641 195926       if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
194642 195927         u8 *abPK = p->bPatchset ? p->abPK : 0;
194643         -      p->rc = sessionReadRecord(&p->in, p->nCol, abPK, p->apValue);
       195928  +      p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld);
194644 195929         if( p->rc!=SQLITE_OK ) return p->rc;
194645 195930       }
194646 195931   
194647 195932       /* If this is an INSERT or UPDATE, read the new.* record. */
194648 195933       if( p->op!=SQLITE_DELETE ){
194649         -      p->rc = sessionReadRecord(&p->in, p->nCol, 0, &p->apValue[p->nCol]);
       195934  +      p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew);
194650 195935         if( p->rc!=SQLITE_OK ) return p->rc;
194651 195936       }
194652 195937   
194653         -    if( p->bPatchset && p->op==SQLITE_UPDATE ){
       195938  +    if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE ){
194654 195939         /* If this is an UPDATE that is part of a patchset, then all PK and
194655 195940         ** modified fields are present in the new.* record. The old.* record
194656 195941         ** is currently completely empty. This block shifts the PK fields from
194657 195942         ** new.* to old.*, to accommodate the code that reads these arrays.  */
194658 195943         for(i=0; i<p->nCol; i++){
194659         -        assert( p->apValue[i]==0 );
       195944  +        assert( p->bPatchset==0 || p->apValue[i]==0 );
194660 195945           if( p->abPK[i] ){
       195946  +          assert( p->apValue[i]==0 );
194661 195947             p->apValue[i] = p->apValue[i+p->nCol];
194662 195948             if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
194663 195949             p->apValue[i+p->nCol] = 0;
194664 195950           }
194665 195951         }
       195952  +    }else if( p->bInvert ){
       195953  +      if( p->op==SQLITE_INSERT ) p->op = SQLITE_DELETE;
       195954  +      else if( p->op==SQLITE_DELETE ) p->op = SQLITE_INSERT;
194666 195955       }
194667 195956     }
194668 195957   
194669 195958     return SQLITE_ROW;
194670 195959   }
194671 195960   
194672 195961   /*
................................................................................
194975 196264   
194976 196265         default:
194977 196266           rc = SQLITE_CORRUPT_BKPT;
194978 196267           goto finished_invert;
194979 196268       }
194980 196269   
194981 196270       assert( rc==SQLITE_OK );
194982         -    if( xOutput && sOut.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
       196271  +    if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){
194983 196272         rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
194984 196273         sOut.nBuf = 0;
194985 196274         if( rc!=SQLITE_OK ) goto finished_invert;
194986 196275       }
194987 196276     }
194988 196277   
194989 196278     assert( rc==SQLITE_OK );
................................................................................
195054 196343     int nCol;                       /* Size of azCol[] and abPK[] arrays */
195055 196344     const char **azCol;             /* Array of column names */
195056 196345     u8 *abPK;                       /* Boolean array - true if column is in PK */
195057 196346     int bStat1;                     /* True if table is sqlite_stat1 */
195058 196347     int bDeferConstraints;          /* True to defer constraints */
195059 196348     SessionBuffer constraints;      /* Deferred constraints are stored here */
195060 196349     SessionBuffer rebase;           /* Rebase information (if any) here */
195061         -  int bRebaseStarted;             /* If table header is already in rebase */
       196350  +  u8 bRebaseStarted;              /* If table header is already in rebase */
       196351  +  u8 bRebase;                     /* True to collect rebase information */
195062 196352   };
195063 196353   
195064 196354   /*
195065 196355   ** Formulate a statement to DELETE a row from database db. Assuming a table
195066 196356   ** structure like this:
195067 196357   **
195068 196358   **     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
................................................................................
195451 196741   */
195452 196742   static int sessionRebaseAdd(
195453 196743     SessionApplyCtx *p,             /* Apply context */
195454 196744     int eType,                      /* Conflict resolution (OMIT or REPLACE) */
195455 196745     sqlite3_changeset_iter *pIter   /* Iterator pointing at current change */
195456 196746   ){
195457 196747     int rc = SQLITE_OK;
195458         -  int i;
195459         -  int eOp = pIter->op;
195460         -  if( p->bRebaseStarted==0 ){
195461         -    /* Append a table-header to the rebase buffer */
195462         -    const char *zTab = pIter->zTab;
195463         -    sessionAppendByte(&p->rebase, 'T', &rc);
195464         -    sessionAppendVarint(&p->rebase, p->nCol, &rc);
195465         -    sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
195466         -    sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
195467         -    p->bRebaseStarted = 1;
195468         -  }
195469         -
195470         -  assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
195471         -  assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
195472         -
195473         -  sessionAppendByte(&p->rebase, 
195474         -      (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
195475         -  );
195476         -  sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
195477         -  for(i=0; i<p->nCol; i++){
195478         -    sqlite3_value *pVal = 0;
195479         -    if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
195480         -      sqlite3changeset_old(pIter, i, &pVal);
195481         -    }else{
195482         -      sqlite3changeset_new(pIter, i, &pVal);
195483         -    }
195484         -    sessionAppendValue(&p->rebase, pVal, &rc);
195485         -  }
195486         -
       196748  +  if( p->bRebase ){
       196749  +    int i;
       196750  +    int eOp = pIter->op;
       196751  +    if( p->bRebaseStarted==0 ){
       196752  +      /* Append a table-header to the rebase buffer */
       196753  +      const char *zTab = pIter->zTab;
       196754  +      sessionAppendByte(&p->rebase, 'T', &rc);
       196755  +      sessionAppendVarint(&p->rebase, p->nCol, &rc);
       196756  +      sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
       196757  +      sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
       196758  +      p->bRebaseStarted = 1;
       196759  +    }
       196760  +
       196761  +    assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
       196762  +    assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
       196763  +
       196764  +    sessionAppendByte(&p->rebase, 
       196765  +        (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
       196766  +        );
       196767  +    sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
       196768  +    for(i=0; i<p->nCol; i++){
       196769  +      sqlite3_value *pVal = 0;
       196770  +      if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
       196771  +        sqlite3changeset_old(pIter, i, &pVal);
       196772  +      }else{
       196773  +        sqlite3changeset_new(pIter, i, &pVal);
       196774  +      }
       196775  +      sessionAppendValue(&p->rebase, pVal, &rc);
       196776  +    }
       196777  +  }
195487 196778     return rc;
195488 196779   }
195489 196780   
195490 196781   /*
195491 196782   ** Invoke the conflict handler for the change that the changeset iterator
195492 196783   ** currently points to.
195493 196784   **
................................................................................
195822 197113     int rc = SQLITE_OK;
195823 197114   
195824 197115     while( pApply->constraints.nBuf ){
195825 197116       sqlite3_changeset_iter *pIter2 = 0;
195826 197117       SessionBuffer cons = pApply->constraints;
195827 197118       memset(&pApply->constraints, 0, sizeof(SessionBuffer));
195828 197119   
195829         -    rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf);
       197120  +    rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf, 0);
195830 197121       if( rc==SQLITE_OK ){
195831 197122         int nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
195832 197123         int rc2;
195833 197124         pIter2->bPatchset = bPatchset;
195834 197125         pIter2->zTab = (char*)zTab;
195835 197126         pIter2->nCol = pApply->nCol;
195836 197127         pIter2->abPK = pApply->abPK;
................................................................................
195888 197179     SessionApplyCtx sApply;         /* changeset_apply() context object */
195889 197180     int bPatchset;
195890 197181   
195891 197182     assert( xConflict!=0 );
195892 197183   
195893 197184     pIter->in.bNoDiscard = 1;
195894 197185     memset(&sApply, 0, sizeof(sApply));
       197186  +  sApply.bRebase = (ppRebase && pnRebase);
195895 197187     sqlite3_mutex_enter(sqlite3_db_mutex(db));
195896 197188     if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
195897 197189       rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
195898 197190     }
195899 197191     if( rc==SQLITE_OK ){
195900 197192       rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
195901 197193     }
................................................................................
196038 197330         rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
196039 197331       }else{
196040 197332         sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
196041 197333         sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
196042 197334       }
196043 197335     }
196044 197336   
196045         -  if( rc==SQLITE_OK && bPatchset==0 && ppRebase && pnRebase ){
       197337  +  assert( sApply.bRebase || sApply.rebase.nBuf==0 );
       197338  +  if( rc==SQLITE_OK && bPatchset==0 && sApply.bRebase ){
196046 197339       *ppRebase = (void*)sApply.rebase.aBuf;
196047 197340       *pnRebase = sApply.rebase.nBuf;
196048 197341       sApply.rebase.aBuf = 0;
196049 197342     }
196050 197343     sqlite3_finalize(sApply.pInsert);
196051 197344     sqlite3_finalize(sApply.pDelete);
196052 197345     sqlite3_finalize(sApply.pUpdate);
................................................................................
196076 197369       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
196077 197370     ),
196078 197371     void *pCtx,                     /* First argument passed to xConflict */
196079 197372     void **ppRebase, int *pnRebase,
196080 197373     int flags
196081 197374   ){
196082 197375     sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
196083         -  int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
       197376  +  int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
       197377  +  int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset,bInverse);
196084 197378     if( rc==SQLITE_OK ){
196085 197379       rc = sessionChangesetApply(
196086 197380           db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
196087 197381       );
196088 197382     }
196089 197383     return rc;
196090 197384   }
................................................................................
196133 197427       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
196134 197428     ),
196135 197429     void *pCtx,                     /* First argument passed to xConflict */
196136 197430     void **ppRebase, int *pnRebase,
196137 197431     int flags
196138 197432   ){
196139 197433     sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
196140         -  int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
       197434  +  int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
       197435  +  int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse);
196141 197436     if( rc==SQLITE_OK ){
196142 197437       rc = sessionChangesetApply(
196143 197438           db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
196144 197439       );
196145 197440     }
196146 197441     return rc;
196147 197442   }
................................................................................
196506 197801       sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
196507 197802       for(i=0; i<pTab->nChange; i++){
196508 197803         SessionChange *p;
196509 197804         for(p=pTab->apChange[i]; p; p=p->pNext){
196510 197805           sessionAppendByte(&buf, p->op, &rc);
196511 197806           sessionAppendByte(&buf, p->bIndirect, &rc);
196512 197807           sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
       197808  +        if( rc==SQLITE_OK && xOutput && buf.nBuf>=sessions_strm_chunk_size ){
       197809  +          rc = xOutput(pOut, buf.aBuf, buf.nBuf);
       197810  +          buf.nBuf = 0;
       197811  +        }
196513 197812         }
196514 197813       }
196515         -
196516         -    if( rc==SQLITE_OK && xOutput && buf.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
196517         -      rc = xOutput(pOut, buf.aBuf, buf.nBuf);
196518         -      buf.nBuf = 0;
196519         -    }
196520 197814     }
196521 197815   
196522 197816     if( rc==SQLITE_OK ){
196523 197817       if( xOutput ){
196524 197818         if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
196525 197819       }else{
196526 197820         *ppOut = buf.aBuf;
................................................................................
196903 198197       }
196904 198198   
196905 198199       if( bDone==0 ){
196906 198200         sessionAppendByte(&sOut, pIter->op, &rc);
196907 198201         sessionAppendByte(&sOut, pIter->bIndirect, &rc);
196908 198202         sessionAppendBlob(&sOut, aRec, nRec, &rc);
196909 198203       }
196910         -    if( rc==SQLITE_OK && xOutput && sOut.nBuf>SESSIONS_STRM_CHUNK_SIZE ){
       198204  +    if( rc==SQLITE_OK && xOutput && sOut.nBuf>sessions_strm_chunk_size ){
196911 198205         rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
196912 198206         sOut.nBuf = 0;
196913 198207       }
196914 198208       if( rc ) break;
196915 198209     }
196916 198210   
196917 198211     if( rc!=SQLITE_OK ){
................................................................................
197013 198307   */
197014 198308   SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
197015 198309     if( p ){
197016 198310       sessionDeleteTable(p->grp.pList);
197017 198311       sqlite3_free(p);
197018 198312     }
197019 198313   }
       198314  +
       198315  +/* 
       198316  +** Global configuration
       198317  +*/
       198318  +SQLITE_API int sqlite3session_config(int op, void *pArg){
       198319  +  int rc = SQLITE_OK;
       198320  +  switch( op ){
       198321  +    case SQLITE_SESSION_CONFIG_STRMSIZE: {
       198322  +      int *pInt = (int*)pArg;
       198323  +      if( *pInt>0 ){
       198324  +        sessions_strm_chunk_size = *pInt;
       198325  +      }
       198326  +      *pInt = sessions_strm_chunk_size;
       198327  +      break;
       198328  +    }
       198329  +    default:
       198330  +      rc = SQLITE_MISUSE;
       198331  +      break;
       198332  +  }
       198333  +  return rc;
       198334  +}
197020 198335   
197021 198336   #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
197022 198337   
197023 198338   /************** End of sqlite3session.c **************************************/
197024 198339   /************** Begin file fts5.c ********************************************/
197025 198340   
197026 198341   
................................................................................
198448 199763   ** of this template is copied straight through into the generate parser
198449 199764   ** source file.
198450 199765   **
198451 199766   ** The following is the concatenation of all %include directives from the
198452 199767   ** input grammar file:
198453 199768   */
198454 199769   /* #include <stdio.h> */
       199770  +/* #include <assert.h> */
198455 199771   /************ Begin %include sections from the grammar ************************/
198456 199772   
198457 199773   /* #include "fts5Int.h" */
198458 199774   /* #include "fts5parse.h" */
198459 199775   
198460 199776   /*
198461 199777   ** Disable all error recovery processing in the parser push-down
................................................................................
199775 201091                fts5yyTracePrompt,fts5yyTokenName[fts5yymajor]);
199776 201092           }
199777 201093   #endif
199778 201094           fts5yy_destructor(fts5yypParser, (fts5YYCODETYPE)fts5yymajor, &fts5yyminorunion);
199779 201095           fts5yymajor = fts5YYNOCODE;
199780 201096         }else{
199781 201097           while( fts5yypParser->fts5yytos >= fts5yypParser->fts5yystack
199782         -            && fts5yymx != fts5YYERRORSYMBOL
199783 201098               && (fts5yyact = fts5yy_find_reduce_action(
199784 201099                           fts5yypParser->fts5yytos->stateno,
199785         -                        fts5YYERRORSYMBOL)) >= fts5YY_MIN_REDUCE
       201100  +                        fts5YYERRORSYMBOL)) > fts5YY_MAX_SHIFTREDUCE
199786 201101           ){
199787 201102             fts5yy_pop_parser_stack(fts5yypParser);
199788 201103           }
199789 201104           if( fts5yypParser->fts5yytos < fts5yypParser->fts5yystack || fts5yymajor==0 ){
199790 201105             fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
199791 201106             fts5yy_parse_failed(fts5yypParser);
199792 201107   #ifndef fts5YYNOERRORRECOVERY
................................................................................
210728 212043   
210729 212044       if( p->rc ){
210730 212045         sqlite3Fts5IterClose((Fts5IndexIter*)pRet);
210731 212046         pRet = 0;
210732 212047         fts5CloseReader(p);
210733 212048       }
210734 212049   
210735         -    *ppIter = &pRet->base;
       212050  +    *ppIter = (Fts5IndexIter*)pRet;
210736 212051       sqlite3Fts5BufferFree(&buf);
210737 212052     }
210738 212053     return fts5IndexReturn(p);
210739 212054   }
210740 212055   
210741 212056   /*
210742 212057   ** Return true if the iterator passed as the only argument is at EOF.
................................................................................
214477 215792   static void fts5SourceIdFunc(
214478 215793     sqlite3_context *pCtx,          /* Function call context */
214479 215794     int nArg,                       /* Number of args */
214480 215795     sqlite3_value **apUnused        /* Function arguments */
214481 215796   ){
214482 215797     assert( nArg==0 );
214483 215798     UNUSED_PARAM2(nArg, apUnused);
214484         -  sqlite3_result_text(pCtx, "fts5: 2018-11-05 20:37:38 89e099fbe5e13c33e683bef07361231ca525b88f7907be7092058007b75036f2", -1, SQLITE_TRANSIENT);
       215799  +  sqlite3_result_text(pCtx, "fts5: 2018-12-01 12:34:55 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238b4f9", -1, SQLITE_TRANSIENT);
       215800  +}
       215801  +
       215802  +/*
       215803  +** Return true if zName is the extension on one of the shadow tables used
       215804  +** by this module.
       215805  +*/
       215806  +static int fts5ShadowName(const char *zName){
       215807  +  static const char *azName[] = {
       215808  +    "config", "content", "data", "docsize", "idx"
       215809  +  };
       215810  +  unsigned int i;
       215811  +  for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
       215812  +    if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
       215813  +  }
       215814  +  return 0;
214485 215815   }
214486 215816   
214487 215817   static int fts5Init(sqlite3 *db){
214488 215818     static const sqlite3_module fts5Mod = {
214489         -    /* iVersion      */ 2,
       215819  +    /* iVersion      */ 3,
214490 215820       /* xCreate       */ fts5CreateMethod,
214491 215821       /* xConnect      */ fts5ConnectMethod,
214492 215822       /* xBestIndex    */ fts5BestIndexMethod,
214493 215823       /* xDisconnect   */ fts5DisconnectMethod,
214494 215824       /* xDestroy      */ fts5DestroyMethod,
214495 215825       /* xOpen         */ fts5OpenMethod,
214496 215826       /* xClose        */ fts5CloseMethod,
................................................................................
214505 215835       /* xCommit       */ fts5CommitMethod,
214506 215836       /* xRollback     */ fts5RollbackMethod,
214507 215837       /* xFindFunction */ fts5FindFunctionMethod,
214508 215838       /* xRename       */ fts5RenameMethod,
214509 215839       /* xSavepoint    */ fts5SavepointMethod,
214510 215840       /* xRelease      */ fts5ReleaseMethod,
214511 215841       /* xRollbackTo   */ fts5RollbackToMethod,
       215842  +    /* xShadowName   */ fts5ShadowName
214512 215843     };
214513 215844   
214514 215845     int rc;
214515 215846     Fts5Global *pGlobal = 0;
214516 215847   
214517 215848     pGlobal = (Fts5Global*)sqlite3_malloc(sizeof(Fts5Global));
214518 215849     if( pGlobal==0 ){
................................................................................
218549 219880   static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){
218550 219881     int eDetail = pCsr->pConfig->eDetail;
218551 219882     int rc = SQLITE_OK;
218552 219883     Fts5IndexIter *pIter = pCsr->pIter;
218553 219884     i64 *pp = &pCsr->iInstPos;
218554 219885     int *po = &pCsr->iInstOff;
218555 219886     
       219887  +  assert( sqlite3Fts5IterEof(pIter)==0 );
       219888  +  assert( pCsr->bEof==0 );
218556 219889     while( eDetail==FTS5_DETAIL_NONE
218557 219890         || sqlite3Fts5PoslistNext64(pIter->pData, pIter->nData, po, pp) 
218558 219891     ){
218559 219892       pCsr->iInstPos = 0;
218560 219893       pCsr->iInstOff = 0;
218561 219894   
218562 219895       rc = sqlite3Fts5IterNextScan(pCsr->pIter);
218563 219896       if( rc==SQLITE_OK ){
218564 219897         rc = fts5VocabInstanceNewTerm(pCsr);
218565         -      if( eDetail==FTS5_DETAIL_NONE ) break;
       219898  +      if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE ) break;
218566 219899       }
218567 219900       if( rc ){
218568 219901         pCsr->bEof = 1;
218569 219902         break;
218570 219903       }
218571 219904     }
218572 219905   
................................................................................
218873 220206       /* xCommit       */ 0,
218874 220207       /* xRollback     */ 0,
218875 220208       /* xFindFunction */ 0,
218876 220209       /* xRename       */ 0,
218877 220210       /* xSavepoint    */ 0,
218878 220211       /* xRelease      */ 0,
218879 220212       /* xRollbackTo   */ 0,
       220213  +    /* xShadowName   */ 0
218880 220214     };
218881 220215     void *p = (void*)pGlobal;
218882 220216   
218883 220217     return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0);
218884 220218   }
218885         -
218886         -
218887 220219   
218888 220220   
218889 220221       
218890 220222   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
218891 220223   
218892 220224   /************** End of fts5.c ************************************************/
218893 220225   /************** Begin file stmt.c ********************************************/
................................................................................
219155 220487     0,                         /* xCommit */
219156 220488     0,                         /* xRollback */
219157 220489     0,                         /* xFindMethod */
219158 220490     0,                         /* xRename */
219159 220491     0,                         /* xSavepoint */
219160 220492     0,                         /* xRelease */
219161 220493     0,                         /* xRollbackTo */
       220494  +  0,                         /* xShadowName */
219162 220495   };
219163 220496   
219164 220497   #endif /* SQLITE_OMIT_VIRTUALTABLE */
219165 220498   
219166 220499   SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3 *db){
219167 220500     int rc = SQLITE_OK;
219168 220501   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
219187 220520   #endif
219188 220521     return rc;
219189 220522   }
219190 220523   #endif /* SQLITE_CORE */
219191 220524   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
219192 220525   
219193 220526   /************** End of stmt.c ************************************************/
219194         -#if __LINE__!=219194
       220527  +#if __LINE__!=220527
219195 220528   #undef SQLITE_SOURCE_ID
219196         -#define SQLITE_SOURCE_ID      "2018-11-05 20:37:38 89e099fbe5e13c33e683bef07361231ca525b88f7907be7092058007b750alt2"
       220529  +#define SQLITE_SOURCE_ID      "2018-12-01 12:34:55 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238alt2"
219197 220530   #endif
219198 220531   /* Return the source-id for this library */
219199 220532   SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
219200 220533   /************************** End of sqlite3.c ******************************/

Changes to sqlite3/src/main/jni/sqlite/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.3"
   127         -#define SQLITE_VERSION_NUMBER 3025003
   128         -#define SQLITE_SOURCE_ID      "2018-11-05 20:37:38 89e099fbe5e13c33e683bef07361231ca525b88f7907be7092058007b75036f2"
          126  +#define SQLITE_VERSION        "3.26.0"
          127  +#define SQLITE_VERSION_NUMBER 3026000
          128  +#define SQLITE_SOURCE_ID      "2018-12-01 12:34:55 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238b4f9"
   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
................................................................................
  2013   2013   ** Existing configuration options might be discontinued.  Applications
  2014   2014   ** should check the return code from [sqlite3_db_config()] to make sure that
  2015   2015   ** the call worked.  ^The [sqlite3_db_config()] interface will return a
  2016   2016   ** non-zero [error code] if a discontinued or unsupported configuration option
  2017   2017   ** is invoked.
  2018   2018   **
  2019   2019   ** <dl>
         2020  +** [[SQLITE_DBCONFIG_LOOKASIDE]]
  2020   2021   ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
  2021   2022   ** <dd> ^This option takes three additional arguments that determine the 
  2022   2023   ** [lookaside memory allocator] configuration for the [database connection].
  2023   2024   ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
  2024   2025   ** pointer to a memory buffer to use for lookaside memory.
  2025   2026   ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
  2026   2027   ** may be NULL in which case SQLite will allocate the
................................................................................
  2035   2036   ** connection is not currently using lookaside memory, or in other words
  2036   2037   ** when the "current value" returned by
  2037   2038   ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
  2038   2039   ** Any attempt to change the lookaside memory configuration when lookaside
  2039   2040   ** memory is in use leaves the configuration unchanged and returns 
  2040   2041   ** [SQLITE_BUSY].)^</dd>
  2041   2042   **
         2043  +** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
  2042   2044   ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
  2043   2045   ** <dd> ^This option is used to enable or disable the enforcement of
  2044   2046   ** [foreign key constraints].  There should be two additional arguments.
  2045   2047   ** The first argument is an integer which is 0 to disable FK enforcement,
  2046   2048   ** positive to enable FK enforcement or negative to leave FK enforcement
  2047   2049   ** unchanged.  The second parameter is a pointer to an integer into which
  2048   2050   ** is written 0 or 1 to indicate whether FK enforcement is off or on
  2049   2051   ** following this call.  The second parameter may be a NULL pointer, in
  2050   2052   ** which case the FK enforcement setting is not reported back. </dd>
  2051   2053   **
         2054  +** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
  2052   2055   ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
  2053   2056   ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
  2054   2057   ** There should be two additional arguments.
  2055   2058   ** The first argument is an integer which is 0 to disable triggers,
  2056   2059   ** positive to enable triggers or negative to leave the setting unchanged.
  2057   2060   ** The second parameter is a pointer to an integer into which
  2058   2061   ** is written 0 or 1 to indicate whether triggers are disabled or enabled
  2059   2062   ** following this call.  The second parameter may be a NULL pointer, in
  2060   2063   ** which case the trigger setting is not reported back. </dd>
  2061   2064   **
         2065  +** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
  2062   2066   ** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
  2063   2067   ** <dd> ^This option is used to enable or disable the two-argument
  2064   2068   ** version of the [fts3_tokenizer()] function which is part of the
  2065   2069   ** [FTS3] full-text search engine extension.
  2066   2070   ** There should be two additional arguments.
  2067   2071   ** The first argument is an integer which is 0 to disable fts3_tokenizer() or
  2068   2072   ** positive to enable fts3_tokenizer() or negative to leave the setting
  2069   2073   ** unchanged.
  2070   2074   ** The second parameter is a pointer to an integer into which
  2071   2075   ** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
  2072   2076   ** following this call.  The second parameter may be a NULL pointer, in
  2073   2077   ** which case the new setting is not reported back. </dd>
  2074   2078   **
         2079  +** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
  2075   2080   ** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
  2076   2081   ** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
  2077   2082   ** interface independently of the [load_extension()] SQL function.
  2078   2083   ** The [sqlite3_enable_load_extension()] API enables or disables both the
  2079   2084   ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
  2080   2085   ** There should be two additional arguments.
  2081   2086   ** When the first argument to this interface is 1, then only the C-API is
................................................................................
  2085   2090   ** C-API or the SQL function.
  2086   2091   ** The second parameter is a pointer to an integer into which
  2087   2092   ** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
  2088   2093   ** is disabled or enabled following this call.  The second parameter may
  2089   2094   ** be a NULL pointer, in which case the new setting is not reported back.
  2090   2095   ** </dd>
  2091   2096   **
  2092         -** <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
         2097  +** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
  2093   2098   ** <dd> ^This option is used to change the name of the "main" database
  2094   2099   ** schema.  ^The sole argument is a pointer to a constant UTF8 string
  2095   2100   ** which will become the new schema name in place of "main".  ^SQLite
  2096   2101   ** does not make a copy of the new main schema name string, so the application
  2097   2102   ** must ensure that the argument passed into this DBCONFIG option is unchanged
  2098   2103   ** until after the database connection closes.
  2099   2104   ** </dd>
  2100   2105   **
         2106  +** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]] 
  2101   2107   ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
  2102   2108   ** <dd> Usually, when a database in wal mode is closed or detached from a 
  2103   2109   ** database handle, SQLite checks if this will mean that there are now no 
  2104   2110   ** connections at all to the database. If so, it performs a checkpoint 
  2105   2111   ** operation before closing the connection. This option may be used to
  2106   2112   ** override this behaviour. The first parameter passed to this operation
  2107   2113   ** is an integer - positive to disable checkpoints-on-close, or zero (the
  2108   2114   ** default) to enable them, and negative to leave the setting unchanged.
  2109   2115   ** The second parameter is a pointer to an integer
  2110   2116   ** into which is written 0 or 1 to indicate whether checkpoints-on-close
  2111   2117   ** have been disabled - 0 if they are not disabled, 1 if they are.
  2112   2118   ** </dd>
  2113   2119   **
  2114         -** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
         2120  +** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
  2115   2121   ** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
  2116   2122   ** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
  2117   2123   ** a single SQL query statement will always use the same algorithm regardless
  2118   2124   ** of values of [bound parameters].)^ The QPSG disables some query optimizations
  2119   2125   ** that look at the values of bound parameters, which can make some queries
  2120   2126   ** slower.  But the QPSG has the advantage of more predictable behavior.  With
  2121   2127   ** the QPSG active, SQLite will always use the same query plan in the field as
................................................................................
  2123   2129   ** The first argument to this setting is an integer which is 0 to disable 
  2124   2130   ** the QPSG, positive to enable QPSG, or negative to leave the setting
  2125   2131   ** unchanged. The second parameter is a pointer to an integer into which
  2126   2132   ** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
  2127   2133   ** following this call.
  2128   2134   ** </dd>
  2129   2135   **
  2130         -** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
         2136  +** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
  2131   2137   ** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
  2132   2138   ** include output for any operations performed by trigger programs. This
  2133   2139   ** option is used to set or clear (the default) a flag that governs this
  2134   2140   ** behavior. The first parameter passed to this operation is an integer -
  2135   2141   ** positive to enable output for trigger programs, or zero to disable it,
  2136   2142   ** or negative to leave the setting unchanged.
  2137   2143   ** The second parameter is a pointer to an integer into which is written 
  2138   2144   ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
  2139   2145   ** it is not disabled, 1 if it is.  
  2140   2146   ** </dd>
  2141   2147   **
  2142         -** <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
         2148  +** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
  2143   2149   ** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
  2144   2150   ** [VACUUM] in order to reset a database back to an empty database
  2145   2151   ** with no schema and no content. The following process works even for
  2146   2152   ** a badly corrupted database file:
  2147   2153   ** <ol>
  2148   2154   ** <li> If the database connection is newly opened, make sure it has read the
  2149   2155   **      database schema by preparing then discarding some query against the
................................................................................
  2154   2160   ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
  2155   2161   ** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
  2156   2162   ** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
  2157   2163   ** </ol>
  2158   2164   ** Because resetting a database is destructive and irreversible, the
  2159   2165   ** process requires the use of this obscure API and multiple steps to help
  2160   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>
  2161   2179   ** </dd>
  2162   2180   ** </dl>
  2163   2181   */
  2164   2182   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
  2165   2183   #define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
  2166   2184   #define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
  2167   2185   #define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
  2168   2186   #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
  2169   2187   #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
  2170   2188   #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
  2171   2189   #define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
  2172   2190   #define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
  2173   2191   #define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
  2174         -#define SQLITE_DBCONFIG_MAX                   1009 /* Largest DBCONFIG */
         2192  +#define SQLITE_DBCONFIG_DEFENSIVE             1010 /* int int* */
         2193  +#define SQLITE_DBCONFIG_MAX                   1010 /* Largest DBCONFIG */
  2175   2194   
  2176   2195   /*
  2177   2196   ** CAPI3REF: Enable Or Disable Extended Result Codes
  2178   2197   ** METHOD: sqlite3
  2179   2198   **
  2180   2199   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  2181   2200   ** [extended result codes] feature of SQLite. ^The extended result
................................................................................
  3605   3624   ** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
  3606   3625   ** and [sqlite3_prepare16_v3()] assume that the prepared statement will 
  3607   3626   ** be used just once or at most a few times and then destroyed using
  3608   3627   ** [sqlite3_finalize()] relatively soon. The current implementation acts
  3609   3628   ** on this hint by avoiding the use of [lookaside memory] so as not to
  3610   3629   ** deplete the limited store of lookaside memory. Future versions of
  3611   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.
  3612   3640   ** </dl>
  3613   3641   */
  3614   3642   #define SQLITE_PREPARE_PERSISTENT              0x01
         3643  +#define SQLITE_PREPARE_NORMALIZE               0x02
  3615   3644   
  3616   3645   /*
  3617   3646   ** CAPI3REF: Compiling An SQL Statement
  3618   3647   ** KEYWORDS: {SQL statement compiler}
  3619   3648   ** METHOD: sqlite3
  3620   3649   ** CONSTRUCTOR: sqlite3_stmt
  3621   3650   **
................................................................................
  3765   3794   ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
  3766   3795   ** SQL text used to create [prepared statement] P if P was
  3767   3796   ** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
  3768   3797   ** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
  3769   3798   ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
  3770   3799   ** string containing the SQL text of prepared statement P with
  3771   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.
  3772   3806   **
  3773   3807   ** ^(For example, if a prepared statement is created using the SQL
  3774   3808   ** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
  3775   3809   ** and parameter :xyz is unbound, then sqlite3_sql() will return
  3776   3810   ** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
  3777   3811   ** will return "SELECT 2345,NULL".)^
  3778   3812   **
................................................................................
  3780   3814   ** is available to hold the result, or if the result would exceed the
  3781   3815   ** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
  3782   3816   **
  3783   3817   ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
  3784   3818   ** bound parameter expansions.  ^The [SQLITE_OMIT_TRACE] compile-time
  3785   3819   ** option causes sqlite3_expanded_sql() to always return NULL.
  3786   3820   **
  3787         -** ^The string returned by sqlite3_sql(P) is managed by SQLite and is
  3788         -** 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.
  3789   3824   ** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
  3790   3825   ** is obtained from [sqlite3_malloc()] and must be free by the application
  3791   3826   ** by passing it to [sqlite3_free()].
  3792   3827   */
  3793   3828   SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
  3794   3829   SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
         3830  +SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
  3795   3831   
  3796   3832   /*
  3797   3833   ** CAPI3REF: Determine If An SQL Statement Writes The Database
  3798   3834   ** METHOD: sqlite3_stmt
  3799   3835   **
  3800   3836   ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  3801   3837   ** and only if the [prepared statement] X makes no direct changes to
................................................................................
  6277   6313                          void **ppArg);
  6278   6314     int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
  6279   6315     /* The methods above are in version 1 of the sqlite_module object. Those 
  6280   6316     ** below are for version 2 and greater. */
  6281   6317     int (*xSavepoint)(sqlite3_vtab *pVTab, int);
  6282   6318     int (*xRelease)(sqlite3_vtab *pVTab, int);
  6283   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*);
  6284   6323   };
  6285   6324   
  6286   6325   /*
  6287   6326   ** CAPI3REF: Virtual Table Indexing Information
  6288   6327   ** KEYWORDS: sqlite3_index_info
  6289   6328   **
  6290   6329   ** The sqlite3_index_info structure and its substructures is used as part
................................................................................
  7199   7238   #define SQLITE_TESTCTRL_PENDING_BYTE            11
  7200   7239   #define SQLITE_TESTCTRL_ASSERT                  12
  7201   7240   #define SQLITE_TESTCTRL_ALWAYS                  13
  7202   7241   #define SQLITE_TESTCTRL_RESERVE                 14
  7203   7242   #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  7204   7243   #define SQLITE_TESTCTRL_ISKEYWORD               16  /* NOT USED */
  7205   7244   #define SQLITE_TESTCTRL_SCRATCHMALLOC           17  /* NOT USED */
         7245  +#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS      17
  7206   7246   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  7207   7247   #define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
  7208   7248   #define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
  7209   7249   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  7210   7250   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  7211   7251   #define SQLITE_TESTCTRL_BYTEORDER               22
  7212   7252   #define SQLITE_TESTCTRL_ISINIT                  23
................................................................................
  8611   8651   ** CAPI3REF: Virtual Table Configuration Options
  8612   8652   **
  8613   8653   ** These macros define the various options to the
  8614   8654   ** [sqlite3_vtab_config()] interface that [virtual table] implementations
  8615   8655   ** can use to customize and optimize their behavior.
  8616   8656   **
  8617   8657   ** <dl>
         8658  +** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
  8618   8659   ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
  8619   8660   ** <dd>Calls of the form
  8620   8661   ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
  8621   8662   ** where X is an integer.  If X is zero, then the [virtual table] whose
  8622   8663   ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
  8623   8664   ** support constraints.  In this configuration (which is the default) if
  8624   8665   ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
................................................................................
  9380   9421     unsigned int *anQueue;            /* Number of pending entries in the queue */
  9381   9422     int nCoord;                       /* Number of coordinates */
  9382   9423     int iLevel;                       /* Level of current node or entry */
  9383   9424     int mxLevel;                      /* The largest iLevel value in the tree */
  9384   9425     sqlite3_int64 iRowid;             /* Rowid for current entry */
  9385   9426     sqlite3_rtree_dbl rParentScore;   /* Score of parent node */
  9386   9427     int eParentWithin;                /* Visibility of parent node */
  9387         -  int eWithin;                      /* OUT: Visiblity */
         9428  +  int eWithin;                      /* OUT: Visibility */
  9388   9429     sqlite3_rtree_dbl rScore;         /* OUT: Write the score here */
  9389   9430     /* The following fields are only available in 3.8.11 and later */
  9390   9431     sqlite3_value **apSqlParam;       /* Original SQL values of parameters */
  9391   9432   };
  9392   9433   
  9393   9434   /*
  9394   9435   ** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
................................................................................
  9876   9917   ** [sqlite3changeset_invert()] functions, all changes within the changeset 
  9877   9918   ** that apply to a single table are grouped together. This means that when 
  9878   9919   ** an application iterates through a changeset using an iterator created by 
  9879   9920   ** this function, all changes that relate to a single table are visited 
  9880   9921   ** consecutively. There is no chance that the iterator will visit a change 
  9881   9922   ** the applies to table X, then one for table Y, and then later on visit 
  9882   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.
  9883   9931   */
  9884   9932   SQLITE_API int sqlite3changeset_start(
  9885   9933     sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
  9886   9934     int nChangeset,                 /* Size of changeset blob in bytes */
  9887   9935     void *pChangeset                /* Pointer to blob containing changeset */
  9888   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
  9889   9956   
  9890   9957   
  9891   9958   /*
  9892   9959   ** CAPI3REF: Advance A Changeset Iterator
  9893   9960   ** METHOD: sqlite3_changeset_iter
  9894   9961   **
  9895   9962   ** This function may only be used with iterators created by function
................................................................................
 10536  10603     int(*xConflict)(
 10537  10604       void *pCtx,                   /* Copy of sixth arg to _apply() */
 10538  10605       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 10539  10606       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 10540  10607     ),
 10541  10608     void *pCtx,                     /* First argument passed to xConflict */
 10542  10609     void **ppRebase, int *pnRebase, /* OUT: Rebase data */
 10543         -  int flags                       /* Combination of SESSION_APPLY_* flags */
        10610  +  int flags                       /* SESSION_CHANGESETAPPLY_* flags */
 10544  10611   );
 10545  10612   
 10546  10613   /*
 10547  10614   ** CAPI3REF: Flags for sqlite3changeset_apply_v2
 10548  10615   **
 10549  10616   ** The following flags may passed via the 9th parameter to
 10550  10617   ** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
................................................................................
 10554  10621   **   Usually, the sessions module encloses all operations performed by
 10555  10622   **   a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
 10556  10623   **   SAVEPOINT is committed if the changeset or patchset is successfully
 10557  10624   **   applied, or rolled back if an error occurs. Specifying this flag
 10558  10625   **   causes the sessions module to omit this savepoint. In this case, if the
 10559  10626   **   caller has an open transaction or savepoint when apply_v2() is called, 
 10560  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.
 10561  10633   */
 10562  10634   #define SQLITE_CHANGESETAPPLY_NOSAVEPOINT   0x0001
        10635  +#define SQLITE_CHANGESETAPPLY_INVERT        0x0002
 10563  10636   
 10564  10637   /* 
 10565  10638   ** CAPI3REF: Constants Passed To The Conflict Handler
 10566  10639   **
 10567  10640   ** Values that may be passed as the second argument to a conflict-handler.
 10568  10641   **
 10569  10642   ** <dl>
................................................................................
 10948  11021     int (*xOutput)(void *pOut, const void *pData, int nData),
 10949  11022     void *pOut
 10950  11023   );
 10951  11024   SQLITE_API int sqlite3changeset_start_strm(
 10952  11025     sqlite3_changeset_iter **pp,
 10953  11026     int (*xInput)(void *pIn, void *pData, int *pnData),
 10954  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
 10955  11034   );
 10956  11035   SQLITE_API int sqlite3session_changeset_strm(
 10957  11036     sqlite3_session *pSession,
 10958  11037     int (*xOutput)(void *pOut, const void *pData, int nData),
 10959  11038     void *pOut
 10960  11039   );
 10961  11040   SQLITE_API int sqlite3session_patchset_strm(
................................................................................
 10975  11054     sqlite3_rebaser *pRebaser,
 10976  11055     int (*xInput)(void *pIn, void *pData, int *pnData),
 10977  11056     void *pIn,
 10978  11057     int (*xOutput)(void *pOut, const void *pData, int nData),
 10979  11058     void *pOut
 10980  11059   );
 10981  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
 10982  11100   
 10983  11101   /*
 10984  11102   ** Make sure we can call this stuff from C++.
 10985  11103   */
 10986  11104   #ifdef __cplusplus
 10987  11105   }
 10988  11106   #endif