System.Data.SQLite
Check-in [dedde051c4]
Not logged in

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

Overview
Comment:Update core SQLite to 3.7.9 release.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: dedde051c4f60af9a7fe0be2934d9097c39ffae8
User & Date: mistachkin 2011-11-05 23:56:54
Context
2011-11-06
00:39
Update build number to 77. check-in: edbed78c0e user: mistachkin tags: trunk
2011-11-05
23:56
Update core SQLite to 3.7.9 release. check-in: dedde051c4 user: mistachkin tags: trunk
2011-10-29
21:16
Fix error checking by portions of the batch tools that create missing directories on an as-needed basis. check-in: 299d71992b user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to SQLite.Interop/props/sqlite3.props.

     5      5    *
     6      6    * Written by Joe Mistachkin.
     7      7    * Released to the public domain, use at your own risk!
     8      8    *
     9      9   -->
    10     10   <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
    11     11     <PropertyGroup Label="UserMacros">
    12         -    <SQLITE_MANIFEST_VERSION>3.7.8</SQLITE_MANIFEST_VERSION>
    13         -    <SQLITE_RC_VERSION>3,7,8</SQLITE_RC_VERSION>
    14         -    <SQLITE_COMMON_DEFINES>SQLITE_THREADSAFE=1;SQLITE_ENABLE_COLUMN_METADATA=1;SQLITE_ENABLE_STAT2=1;SQLITE_ENABLE_FTS3=1;SQLITE_ENABLE_LOAD_EXTENSION=1;SQLITE_ENABLE_RTREE=1;SQLITE_SOUNDEX=1</SQLITE_COMMON_DEFINES>
           12  +    <SQLITE_MANIFEST_VERSION>3.7.9</SQLITE_MANIFEST_VERSION>
           13  +    <SQLITE_RC_VERSION>3,7,9</SQLITE_RC_VERSION>
           14  +    <SQLITE_COMMON_DEFINES>SQLITE_THREADSAFE=1;SQLITE_ENABLE_COLUMN_METADATA=1;SQLITE_ENABLE_STAT3=1;SQLITE_ENABLE_FTS3=1;SQLITE_ENABLE_LOAD_EXTENSION=1;SQLITE_ENABLE_RTREE=1;SQLITE_SOUNDEX=1</SQLITE_COMMON_DEFINES>
    15     15       <SQLITE_EXTRA_DEFINES>SQLITE_HAS_CODEC=1</SQLITE_EXTRA_DEFINES>
    16     16       <SQLITE_WINCE_DEFINES>SQLITE_OMIT_WAL=1</SQLITE_WINCE_DEFINES>
    17     17       <SQLITE_DEBUG_DEFINES>SQLITE_DEBUG=1;SQLITE_MEMDEBUG=1</SQLITE_DEBUG_DEFINES>
    18     18       <SQLITE_RELEASE_DEFINES>SQLITE_WIN32_MALLOC=1</SQLITE_RELEASE_DEFINES>
    19     19       <SQLITE_DISABLE_WARNINGS>4018;4055;4057;4090;4100;4127;4132;4146;4152;4210;4244;4245;4389;4701;4706;4996</SQLITE_DISABLE_WARNINGS>
    20     20       <SQLITE_DISABLE_X64_WARNINGS>4267;4306</SQLITE_DISABLE_X64_WARNINGS>
    21     21     </PropertyGroup>

Changes to SQLite.Interop/props/sqlite3.vsprops.

    10     10   <VisualStudioPropertySheet
    11     11   	ProjectType="Visual C++"
    12     12   	Version="8.00"
    13     13   	Name="sqlite3"
    14     14   	>
    15     15   	<UserMacro
    16     16   		Name="SQLITE_MANIFEST_VERSION"
    17         -		Value="3.7.8"
           17  +		Value="3.7.9"
    18     18   		PerformEnvironmentSet="true"
    19     19   	/>
    20     20   	<UserMacro
    21     21   		Name="SQLITE_RC_VERSION"
    22         -		Value="3,7,8"
           22  +		Value="3,7,9"
    23     23   		PerformEnvironmentSet="true"
    24     24   	/>
    25     25   	<UserMacro
    26     26   		Name="SQLITE_COMMON_DEFINES"
    27         -		Value="SQLITE_THREADSAFE=1;SQLITE_ENABLE_COLUMN_METADATA=1;SQLITE_ENABLE_STAT2=1;SQLITE_ENABLE_FTS3=1;SQLITE_ENABLE_LOAD_EXTENSION=1;SQLITE_ENABLE_RTREE=1;SQLITE_SOUNDEX=1"
           27  +		Value="SQLITE_THREADSAFE=1;SQLITE_ENABLE_COLUMN_METADATA=1;SQLITE_ENABLE_STAT3=1;SQLITE_ENABLE_FTS3=1;SQLITE_ENABLE_LOAD_EXTENSION=1;SQLITE_ENABLE_RTREE=1;SQLITE_SOUNDEX=1"
    28     28   		PerformEnvironmentSet="true"
    29     29   	/>
    30     30   	<UserMacro
    31     31   		Name="SQLITE_EXTRA_DEFINES"
    32     32   		Value="SQLITE_HAS_CODEC=1"
    33     33   		PerformEnvironmentSet="true"
    34     34   	/>

Changes to SQLite.Interop/src/core/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.7.8.  By combining all the individual C code files into this 
            3  +** version 3.7.9.  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
................................................................................
   312    312   #ifdef HAVE_STDINT_H
   313    313   #include <stdint.h>
   314    314   #endif
   315    315   #ifdef HAVE_INTTYPES_H
   316    316   #include <inttypes.h>
   317    317   #endif
   318    318   
   319         -/*
   320         -** The number of samples of an index that SQLite takes in order to 
   321         -** construct a histogram of the table content when running ANALYZE
   322         -** and with SQLITE_ENABLE_STAT2
   323         -*/
   324         -#define SQLITE_INDEX_SAMPLES 10
   325         -
   326    319   /*
   327    320   ** The following macros are used to cast pointers to integers and
   328    321   ** integers to pointers.  The way you do this varies from one compiler
   329    322   ** to the next, so we have developed the following set of #if statements
   330    323   ** to generate appropriate macros for a wide range of compilers.
   331    324   **
   332    325   ** The correct "ANSI" way to do this is to use the intptr_t type. 
................................................................................
   392    385   **
   393    386   ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
   394    387   ** assert() macro is enabled, each call into the Win32 native heap subsystem
   395    388   ** will cause HeapValidate to be called.  If heap validation should fail, an
   396    389   ** assertion will be triggered.
   397    390   **
   398    391   ** (Historical note:  There used to be several other options, but we've
   399         -** pared it down to just these two.)
          392  +** pared it down to just these three.)
   400    393   **
   401    394   ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
   402    395   ** the default.
   403    396   */
   404    397   #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
   405    398   # error "At most one of the following compile-time configuration options\
   406    399    is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG"
................................................................................
   652    645   ** string contains the date and time of the check-in (UTC) and an SHA1
   653    646   ** hash of the entire source tree.
   654    647   **
   655    648   ** See also: [sqlite3_libversion()],
   656    649   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   657    650   ** [sqlite_version()] and [sqlite_source_id()].
   658    651   */
   659         -#define SQLITE_VERSION        "3.7.8"
   660         -#define SQLITE_VERSION_NUMBER 3007008
   661         -#define SQLITE_SOURCE_ID      "2011-09-19 14:49:19 3e0da808d2f5b4d12046e05980ca04578f581177"
          652  +#define SQLITE_VERSION        "3.7.9"
          653  +#define SQLITE_VERSION_NUMBER 3007009
          654  +#define SQLITE_SOURCE_ID      "2011-11-01 00:52:41 c7c6050ef060877ebe77b41d959e9df13f8c9b5e"
   662    655   
   663    656   /*
   664    657   ** CAPI3REF: Run-Time Library Version Numbers
   665    658   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   666    659   **
   667    660   ** These interfaces provide the same information as the [SQLITE_VERSION],
   668    661   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  1291   1284   ** opcode as doing so may disrupt the operation of the specialized VFSes
  1292   1285   ** that do require it.  
  1293   1286   **
  1294   1287   ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
  1295   1288   ** retry counts and intervals for certain disk I/O operations for the
  1296   1289   ** windows [VFS] in order to work to provide robustness against
  1297   1290   ** anti-virus programs.  By default, the windows VFS will retry file read,
  1298         -** file write, and file delete opertions up to 10 times, with a delay
         1291  +** file write, and file delete operations up to 10 times, with a delay
  1299   1292   ** of 25 milliseconds before the first retry and with the delay increasing
  1300   1293   ** by an additional 25 milliseconds with each subsequent retry.  This
  1301   1294   ** opcode allows those to values (10 retries and 25 milliseconds of delay)
  1302   1295   ** to be adjusted.  The values are changed for all database connections
  1303   1296   ** within the same process.  The argument is a pointer to an array of two
  1304   1297   ** integers where the first integer i the new retry count and the second
  1305   1298   ** integer is the delay.  If either integer is negative, then the setting
................................................................................
  1316   1309   ** have write permission on the directory containing the database file want
  1317   1310   ** to read the database file, as the WAL and shared memory files must exist
  1318   1311   ** in order for the database to be readable.  The fourth parameter to
  1319   1312   ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
  1320   1313   ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
  1321   1314   ** WAL mode.  If the integer is -1, then it is overwritten with the current
  1322   1315   ** WAL persistence setting.
  1323         -** 
         1316  +**
         1317  +** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
         1318  +** a write transaction to indicate that, unless it is rolled back for some
         1319  +** reason, the entire database file will be overwritten by the current 
         1320  +** transaction. This is used by VACUUM operations.
  1324   1321   */
  1325   1322   #define SQLITE_FCNTL_LOCKSTATE        1
  1326   1323   #define SQLITE_GET_LOCKPROXYFILE      2
  1327   1324   #define SQLITE_SET_LOCKPROXYFILE      3
  1328   1325   #define SQLITE_LAST_ERRNO             4
  1329   1326   #define SQLITE_FCNTL_SIZE_HINT        5
  1330   1327   #define SQLITE_FCNTL_CHUNK_SIZE       6
  1331   1328   #define SQLITE_FCNTL_FILE_POINTER     7
  1332   1329   #define SQLITE_FCNTL_SYNC_OMITTED     8
  1333   1330   #define SQLITE_FCNTL_WIN32_AV_RETRY   9
  1334   1331   #define SQLITE_FCNTL_PERSIST_WAL     10
         1332  +#define SQLITE_FCNTL_OVERWRITE       11
  1335   1333   
  1336   1334   /*
  1337   1335   ** CAPI3REF: Mutex Handle
  1338   1336   **
  1339   1337   ** The mutex module within SQLite defines [sqlite3_mutex] to be an
  1340   1338   ** abstract type for a mutex object.  The SQLite core never looks
  1341   1339   ** at the internal representation of an [sqlite3_mutex].  It only
................................................................................
  1944   1942   ** to using its default memory allocator (the system malloc() implementation),
  1945   1943   ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
  1946   1944   ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
  1947   1945   ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
  1948   1946   ** allocator is engaged to handle all of SQLites memory allocation needs.
  1949   1947   ** The first pointer (the memory pointer) must be aligned to an 8-byte
  1950   1948   ** boundary or subsequent behavior of SQLite will be undefined.
  1951         -** The minimum allocation size is capped at 2^12. Reasonable values
  1952         -** for the minimum allocation size are 2^5 through 2^8.</dd>
         1949  +** The minimum allocation size is capped at 2**12. Reasonable values
         1950  +** for the minimum allocation size are 2**5 through 2**8.</dd>
  1953   1951   **
  1954   1952   ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
  1955   1953   ** <dd> ^(This option takes a single argument which is a pointer to an
  1956   1954   ** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
  1957   1955   ** alternative low-level mutex routines to be used in place
  1958   1956   ** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
  1959   1957   ** content of the [sqlite3_mutex_methods] structure before the call to
................................................................................
  3344   3342   ** first zero terminator. ^If nByte is non-negative, then it is the maximum
  3345   3343   ** number of  bytes read from zSql.  ^When nByte is non-negative, the
  3346   3344   ** zSql string ends at either the first '\000' or '\u0000' character or
  3347   3345   ** the nByte-th byte, whichever comes first. If the caller knows
  3348   3346   ** that the supplied string is nul-terminated, then there is a small
  3349   3347   ** performance advantage to be gained by passing an nByte parameter that
  3350   3348   ** is equal to the number of bytes in the input string <i>including</i>
  3351         -** the nul-terminator bytes.
         3349  +** the nul-terminator bytes as this saves SQLite from having to
         3350  +** make a copy of the input string.
  3352   3351   **
  3353   3352   ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
  3354   3353   ** past the end of the first SQL statement in zSql.  These routines only
  3355   3354   ** compile the first statement in zSql, so *pzTail is left pointing to
  3356   3355   ** what remains uncompiled.
  3357   3356   **
  3358   3357   ** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
................................................................................
  3395   3394   ** WHERE clause might influence the choice of query plan for a statement,
  3396   3395   ** then the statement will be automatically recompiled, as if there had been 
  3397   3396   ** a schema change, on the first  [sqlite3_step()] call following any change
  3398   3397   ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  3399   3398   ** ^The specific value of WHERE-clause [parameter] might influence the 
  3400   3399   ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  3401   3400   ** or [GLOB] operator or if the parameter is compared to an indexed column
  3402         -** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
         3401  +** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  3403   3402   ** the 
  3404   3403   ** </li>
  3405   3404   ** </ol>
  3406   3405   */
  3407   3406   SQLITE_API int sqlite3_prepare(
  3408   3407     sqlite3 *db,            /* Database handle */
  3409   3408     const char *zSql,       /* SQL statement, UTF-8 encoded */
................................................................................
  3565   3564   ** ^The third argument is the value to bind to the parameter.
  3566   3565   **
  3567   3566   ** ^(In those routines that have a fourth argument, its value is the
  3568   3567   ** number of bytes in the parameter.  To be clear: the value is the
  3569   3568   ** number of <u>bytes</u> in the value, not the number of characters.)^
  3570   3569   ** ^If the fourth parameter is negative, the length of the string is
  3571   3570   ** the number of bytes up to the first zero terminator.
         3571  +** If a non-negative fourth parameter is provided to sqlite3_bind_text()
         3572  +** or sqlite3_bind_text16() then that parameter must be the byte offset
         3573  +** where the NUL terminator would occur assuming the string were NUL
         3574  +** terminated.  If any NUL characters occur at byte offsets less than 
         3575  +** the value of the fourth parameter then the resulting string value will
         3576  +** contain embedded NULs.  The result of expressions involving strings
         3577  +** with embedded NULs is undefined.
  3572   3578   **
  3573   3579   ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
  3574   3580   ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
  3575   3581   ** string after SQLite has finished with it.  ^The destructor is called
  3576   3582   ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
  3577   3583   ** sqlite3_bind_text(), or sqlite3_bind_text16() fails.  
  3578   3584   ** ^If the fifth argument is
................................................................................
  3898   3904   **
  3899   3905   ** ^The sqlite3_data_count(P) interface returns the number of columns in the
  3900   3906   ** current row of the result set of [prepared statement] P.
  3901   3907   ** ^If prepared statement P does not have results ready to return
  3902   3908   ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
  3903   3909   ** interfaces) then sqlite3_data_count(P) returns 0.
  3904   3910   ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
         3911  +** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
         3912  +** [sqlite3_step](P) returned [SQLITE_DONE].  ^The sqlite3_data_count(P)
         3913  +** will return non-zero if previous call to [sqlite3_step](P) returned
         3914  +** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
         3915  +** where it always returns zero since each step of that multi-step
         3916  +** pragma returns 0 columns of data.
  3905   3917   **
  3906   3918   ** See also: [sqlite3_column_count()]
  3907   3919   */
  3908   3920   SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
  3909   3921   
  3910   3922   /*
  3911   3923   ** CAPI3REF: Fundamental Datatypes
................................................................................
  4577   4589   ** the 2nd parameter of the sqlite3_result_text* interfaces.
  4578   4590   ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  4579   4591   ** is negative, then SQLite takes result text from the 2nd parameter
  4580   4592   ** through the first zero character.
  4581   4593   ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  4582   4594   ** is non-negative, then as many bytes (not characters) of the text
  4583   4595   ** pointed to by the 2nd parameter are taken as the application-defined
  4584         -** function result.
         4596  +** function result.  If the 3rd parameter is non-negative, then it
         4597  +** must be the byte offset into the string where the NUL terminator would
         4598  +** appear if the string where NUL terminated.  If any NUL characters occur
         4599  +** in the string at a byte offset that is less than the value of the 3rd
         4600  +** parameter, then the resulting string will contain embedded NULs and the
         4601  +** result of expressions operating on strings with embedded NULs is undefined.
  4585   4602   ** ^If the 4th parameter to the sqlite3_result_text* interfaces
  4586   4603   ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
  4587   4604   ** function as the destructor on the text or BLOB result when it has
  4588   4605   ** finished using that result.
  4589   4606   ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
  4590   4607   ** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
  4591   4608   ** assumes that the text or BLOB result is in constant space and does not
................................................................................
  6360   6377   **
  6361   6378   ** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
  6362   6379   ** <dd>This parameter returns the approximate number of of bytes of heap
  6363   6380   ** and lookaside memory used by all prepared statements associated with
  6364   6381   ** the database connection.)^
  6365   6382   ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
  6366   6383   ** </dd>
         6384  +**
         6385  +** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
         6386  +** <dd>This parameter returns the number of pager cache hits that have
         6387  +** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT 
         6388  +** is always 0.
         6389  +** </dd>
         6390  +**
         6391  +** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
         6392  +** <dd>This parameter returns the number of pager cache misses that have
         6393  +** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS 
         6394  +** is always 0.
         6395  +** </dd>
  6367   6396   ** </dl>
  6368   6397   */
  6369   6398   #define SQLITE_DBSTATUS_LOOKASIDE_USED       0
  6370   6399   #define SQLITE_DBSTATUS_CACHE_USED           1
  6371   6400   #define SQLITE_DBSTATUS_SCHEMA_USED          2
  6372   6401   #define SQLITE_DBSTATUS_STMT_USED            3
  6373   6402   #define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
  6374   6403   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  6375   6404   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  6376         -#define SQLITE_DBSTATUS_MAX                  6   /* Largest defined DBSTATUS */
         6405  +#define SQLITE_DBSTATUS_CACHE_HIT            7
         6406  +#define SQLITE_DBSTATUS_CACHE_MISS           8
         6407  +#define SQLITE_DBSTATUS_MAX                  8   /* Largest defined DBSTATUS */
  6377   6408   
  6378   6409   
  6379   6410   /*
  6380   6411   ** CAPI3REF: Prepared Statement Status
  6381   6412   **
  6382   6413   ** ^(Each prepared statement maintains various
  6383   6414   ** [SQLITE_STMTSTATUS counters] that measure the number
................................................................................
  6423   6454   **
  6424   6455   ** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
  6425   6456   ** <dd>^This is the number of rows inserted into transient indices that
  6426   6457   ** were created automatically in order to help joins run faster.
  6427   6458   ** A non-zero value in this counter may indicate an opportunity to
  6428   6459   ** improvement performance by adding permanent indices that do not
  6429   6460   ** need to be reinitialized each time the statement is run.</dd>
  6430         -**
  6431   6461   ** </dl>
  6432   6462   */
  6433   6463   #define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
  6434   6464   #define SQLITE_STMTSTATUS_SORT              2
  6435   6465   #define SQLITE_STMTSTATUS_AUTOINDEX         3
  6436   6466   
  6437   6467   /*
................................................................................
  7709   7739   ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
  7710   7740   ** that can be stored in a u32 without loss of data.  The value
  7711   7741   ** is 0x00000000ffffffff.  But because of quirks of some compilers, we
  7712   7742   ** have to specify the value in the less intuitive manner shown:
  7713   7743   */
  7714   7744   #define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
  7715   7745   
         7746  +/*
         7747  +** The datatype used to store estimates of the number of rows in a
         7748  +** table or index.  This is an unsigned integer type.  For 99.9% of
         7749  +** the world, a 32-bit integer is sufficient.  But a 64-bit integer
         7750  +** can be used at compile-time if desired.
         7751  +*/
         7752  +#ifdef SQLITE_64BIT_STATS
         7753  + typedef u64 tRowcnt;    /* 64-bit only if requested at compile-time */
         7754  +#else
         7755  + typedef u32 tRowcnt;    /* 32-bit is the default */
         7756  +#endif
         7757  +
  7716   7758   /*
  7717   7759   ** Macros to determine whether the machine is big or little endian,
  7718   7760   ** evaluated at runtime.
  7719   7761   */
  7720   7762   #ifdef SQLITE_AMALGAMATION
  7721   7763   SQLITE_PRIVATE const int sqlite3one = 1;
  7722   7764   #else
................................................................................
  8740   8782   SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*);
  8741   8783   SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
  8742   8784   SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
  8743   8785   SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
  8744   8786   SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
  8745   8787   SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
  8746   8788   SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
         8789  +SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
         8790  +SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
  8747   8791   
  8748   8792   /* Functions used to truncate the database file. */
  8749   8793   SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
  8750   8794   
  8751   8795   #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
  8752   8796   SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
  8753   8797   #endif
................................................................................
  9276   9320   
  9277   9321   #ifdef SQLITE_MUTEX_OMIT
  9278   9322   /*
  9279   9323   ** If this is a no-op implementation, implement everything as macros.
  9280   9324   */
  9281   9325   #define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
  9282   9326   #define sqlite3_mutex_free(X)
  9283         -#define sqlite3_mutex_enter(X)
         9327  +#define sqlite3_mutex_enter(X)    
  9284   9328   #define sqlite3_mutex_try(X)      SQLITE_OK
  9285         -#define sqlite3_mutex_leave(X)
         9329  +#define sqlite3_mutex_leave(X)    
  9286   9330   #define sqlite3_mutex_held(X)     ((void)(X),1)
  9287   9331   #define sqlite3_mutex_notheld(X)  ((void)(X),1)
  9288   9332   #define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
  9289   9333   #define sqlite3MutexInit()        SQLITE_OK
  9290   9334   #define sqlite3MutexEnd()
         9335  +#define MUTEX_LOGIC(X)
         9336  +#else
         9337  +#define MUTEX_LOGIC(X)            X
  9291   9338   #endif /* defined(SQLITE_MUTEX_OMIT) */
  9292   9339   
  9293   9340   /************** End of mutex.h ***********************************************/
  9294   9341   /************** Continuing where we left off in sqliteInt.h ******************/
  9295   9342   
  9296   9343   
  9297   9344   /*
................................................................................
  9916   9963   struct Table {
  9917   9964     char *zName;         /* Name of the table or view */
  9918   9965     int iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
  9919   9966     int nCol;            /* Number of columns in this table */
  9920   9967     Column *aCol;        /* Information about each column */
  9921   9968     Index *pIndex;       /* List of SQL indexes on this table. */
  9922   9969     int tnum;            /* Root BTree node for this table (see note above) */
  9923         -  unsigned nRowEst;    /* Estimated rows in table - from sqlite_stat1 table */
         9970  +  tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
  9924   9971     Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
  9925   9972     u16 nRef;            /* Number of pointers to this Table */
  9926   9973     u8 tabFlags;         /* Mask of TF_* values */
  9927   9974     u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
  9928   9975     FKey *pFKey;         /* Linked list of all foreign keys in this table */
  9929   9976     char *zColAff;       /* String defining the affinity of each column */
  9930   9977   #ifndef SQLITE_OMIT_CHECK
................................................................................
 10115  10162   ** algorithm to employ whenever an attempt is made to insert a non-unique
 10116  10163   ** element.
 10117  10164   */
 10118  10165   struct Index {
 10119  10166     char *zName;     /* Name of this index */
 10120  10167     int nColumn;     /* Number of columns in the table used by this index */
 10121  10168     int *aiColumn;   /* Which columns are used by this index.  1st is 0 */
 10122         -  unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
        10169  +  tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
 10123  10170     Table *pTable;   /* The SQL table being indexed */
 10124  10171     int tnum;        /* Page containing root of this index in database file */
 10125  10172     u8 onError;      /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
 10126  10173     u8 autoIndex;    /* True if is automatically created (ex: by UNIQUE) */
 10127  10174     u8 bUnordered;   /* Use this index for == or IN queries only */
 10128  10175     char *zColAff;   /* String defining the affinity of each column */
 10129  10176     Index *pNext;    /* The next index associated with the same table */
 10130  10177     Schema *pSchema; /* Schema containing this index */
 10131  10178     u8 *aSortOrder;  /* Array of size Index.nColumn. True==DESC, False==ASC */
 10132  10179     char **azColl;   /* Array of collation sequence names for index */
 10133         -  IndexSample *aSample;    /* Array of SQLITE_INDEX_SAMPLES samples */
        10180  +#ifdef SQLITE_ENABLE_STAT3
        10181  +  int nSample;             /* Number of elements in aSample[] */
        10182  +  tRowcnt avgEq;           /* Average nEq value for key values not in aSample */
        10183  +  IndexSample *aSample;    /* Samples of the left-most key */
        10184  +#endif
 10134  10185   };
 10135  10186   
 10136  10187   /*
 10137         -** Each sample stored in the sqlite_stat2 table is represented in memory 
 10138         -** using a structure of this type.
        10188  +** Each sample stored in the sqlite_stat3 table is represented in memory 
        10189  +** using a structure of this type.  See documentation at the top of the
        10190  +** analyze.c source file for additional information.
 10139  10191   */
 10140  10192   struct IndexSample {
 10141  10193     union {
 10142  10194       char *z;        /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
 10143         -    double r;       /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
        10195  +    double r;       /* Value if eType is SQLITE_FLOAT */
        10196  +    i64 i;          /* Value if eType is SQLITE_INTEGER */
 10144  10197     } u;
 10145  10198     u8 eType;         /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
 10146         -  u8 nByte;         /* Size in byte of text or blob. */
        10199  +  int nByte;        /* Size in byte of text or blob. */
        10200  +  tRowcnt nEq;      /* Est. number of rows where the key equals this sample */
        10201  +  tRowcnt nLt;      /* Est. number of rows where key is less than this sample */
        10202  +  tRowcnt nDLt;     /* Est. number of distinct keys less than this sample */
 10147  10203   };
 10148  10204   
 10149  10205   /*
 10150  10206   ** Each token coming out of the lexer is an instance of
 10151  10207   ** this structure.  Tokens are also used as part of an expression.
 10152  10208   **
 10153  10209   ** Note if Token.z==0 then Token.dyn and Token.n are undefined and
................................................................................
 10591  10647   ** and the WhereInfo.wctrlFlags member.
 10592  10648   */
 10593  10649   #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
 10594  10650   #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
 10595  10651   #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
 10596  10652   #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
 10597  10653   #define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
 10598         -#define WHERE_OMIT_OPEN        0x0010 /* Table cursors are already open */
 10599         -#define WHERE_OMIT_CLOSE       0x0020 /* Omit close of table & index cursors */
 10600         -#define WHERE_FORCE_TABLE      0x0040 /* Do not use an index-only search */
 10601         -#define WHERE_ONETABLE_ONLY    0x0080 /* Only code the 1st table in pTabList */
        10654  +#define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
        10655  +#define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
        10656  +#define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
        10657  +#define WHERE_AND_ONLY         0x0080 /* Don't use indices for OR terms */
 10602  10658   
 10603  10659   /*
 10604  10660   ** The WHERE clause processing routine has two halves.  The
 10605  10661   ** first part does the start of the WHERE loop and the second
 10606  10662   ** half does the tail of the WHERE loop.  An instance of
 10607  10663   ** this structure is returned by the first half and passed
 10608  10664   ** into the second half to give some continuity.
................................................................................
 11348  11404   #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
 11349  11405   SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
 11350  11406   #else
 11351  11407   # define sqlite3ViewGetColumnNames(A,B) 0
 11352  11408   #endif
 11353  11409   
 11354  11410   SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
        11411  +SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
 11355  11412   SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
 11356  11413   #ifndef SQLITE_OMIT_AUTOINCREMENT
 11357  11414   SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
 11358  11415   SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
 11359  11416   #else
 11360  11417   # define sqlite3AutoincrementBegin(X)
 11361  11418   # define sqlite3AutoincrementEnd(X)
................................................................................
 11604  11661   SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
 11605  11662   SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
 11606  11663   SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
 11607  11664                           void(*)(void*));
 11608  11665   SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
 11609  11666   SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
 11610  11667   SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
 11611         -#ifdef SQLITE_ENABLE_STAT2
        11668  +#ifdef SQLITE_ENABLE_STAT3
 11612  11669   SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
 11613  11670   #endif
 11614  11671   SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
 11615  11672   SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
 11616  11673   #ifndef SQLITE_AMALGAMATION
 11617  11674   SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
 11618  11675   SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
................................................................................
 11706  11763   #  define sqlite3VtabRollback(X)
 11707  11764   #  define sqlite3VtabCommit(X)
 11708  11765   #  define sqlite3VtabInSync(db) 0
 11709  11766   #  define sqlite3VtabLock(X) 
 11710  11767   #  define sqlite3VtabUnlock(X)
 11711  11768   #  define sqlite3VtabUnlockList(X)
 11712  11769   #  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
        11770  +#  define sqlite3GetVTable(X,Y)  ((VTable*)0)
 11713  11771   #else
 11714  11772   SQLITE_PRIVATE    void sqlite3VtabClear(sqlite3 *db, Table*);
 11715  11773   SQLITE_PRIVATE    int sqlite3VtabSync(sqlite3 *db, char **);
 11716  11774   SQLITE_PRIVATE    int sqlite3VtabRollback(sqlite3 *db);
 11717  11775   SQLITE_PRIVATE    int sqlite3VtabCommit(sqlite3 *db);
 11718  11776   SQLITE_PRIVATE    void sqlite3VtabLock(VTable *);
 11719  11777   SQLITE_PRIVATE    void sqlite3VtabUnlock(VTable *);
 11720  11778   SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
 11721  11779   SQLITE_PRIVATE    int sqlite3VtabSavepoint(sqlite3 *, int, int);
        11780  +SQLITE_PRIVATE    VTable *sqlite3GetVTable(sqlite3*, Table*);
 11722  11781   #  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
 11723  11782   #endif
 11724  11783   SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
 11725  11784   SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
 11726  11785   SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
 11727  11786   SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
 11728  11787   SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
................................................................................
 11734  11793   SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
 11735  11794   SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
 11736  11795   SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
 11737  11796   SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
 11738  11797   SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
 11739  11798   SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
 11740  11799   SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
 11741         -SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
 11742  11800   SQLITE_PRIVATE const char *sqlite3JournalModename(int);
 11743  11801   SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
 11744  11802   SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
 11745  11803   
 11746  11804   /* Declarations for functions in fkey.c. All of these are replaced by
 11747  11805   ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
 11748  11806   ** key functionality is available. If OMIT_TRIGGER is defined but
................................................................................
 12228  12286   #endif
 12229  12287   #ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
 12230  12288     "ENABLE_OVERSIZE_CELL_CHECK",
 12231  12289   #endif
 12232  12290   #ifdef SQLITE_ENABLE_RTREE
 12233  12291     "ENABLE_RTREE",
 12234  12292   #endif
 12235         -#ifdef SQLITE_ENABLE_STAT2
 12236         -  "ENABLE_STAT2",
        12293  +#ifdef SQLITE_ENABLE_STAT3
        12294  +  "ENABLE_STAT3",
 12237  12295   #endif
 12238  12296   #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
 12239  12297     "ENABLE_UNLOCK_NOTIFY",
 12240  12298   #endif
 12241  12299   #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
 12242  12300     "ENABLE_UPDATE_DELETE_LIMIT",
 12243  12301   #endif
................................................................................
 12443  12501   #endif
 12444  12502   #ifdef SQLITE_OMIT_WSD
 12445  12503     "OMIT_WSD",
 12446  12504   #endif
 12447  12505   #ifdef SQLITE_OMIT_XFER_OPT
 12448  12506     "OMIT_XFER_OPT",
 12449  12507   #endif
 12450         -#ifdef SQLITE_PAGECACHE_BLOCKALLOC
 12451         -  "PAGECACHE_BLOCKALLOC",
 12452         -#endif
 12453  12508   #ifdef SQLITE_PERFORMANCE_TRACE
 12454  12509     "PERFORMANCE_TRACE",
 12455  12510   #endif
 12456  12511   #ifdef SQLITE_PROXY_DEBUG
 12457  12512     "PROXY_DEBUG",
 12458  12513   #endif
 12459  12514   #ifdef SQLITE_SECURE_DELETE
................................................................................
 12931  12986   SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
 12932  12987   SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
 12933  12988   SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
 12934  12989   SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
 12935  12990   SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
 12936  12991   SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
 12937  12992   SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
        12993  +SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
 12938  12994   
 12939  12995   #ifdef SQLITE_OMIT_MERGE_SORT
 12940  12996   # define sqlite3VdbeSorterInit(Y,Z)      SQLITE_OK
 12941  12997   # define sqlite3VdbeSorterWrite(X,Y,Z)   SQLITE_OK
 12942  12998   # define sqlite3VdbeSorterClose(Y,Z)
 12943  12999   # define sqlite3VdbeSorterRowkey(Y,Z)    SQLITE_OK
 12944  13000   # define sqlite3VdbeSorterRewind(X,Y,Z)  SQLITE_OK
................................................................................
 13187  13243         db->pnBytesFreed = 0;
 13188  13244   
 13189  13245         *pHighwater = 0;
 13190  13246         *pCurrent = nByte;
 13191  13247   
 13192  13248         break;
 13193  13249       }
        13250  +
        13251  +    /*
        13252  +    ** Set *pCurrent to the total cache hits or misses encountered by all
        13253  +    ** pagers the database handle is connected to. *pHighwater is always set 
        13254  +    ** to zero.
        13255  +    */
        13256  +    case SQLITE_DBSTATUS_CACHE_HIT:
        13257  +    case SQLITE_DBSTATUS_CACHE_MISS: {
        13258  +      int i;
        13259  +      int nRet = 0;
        13260  +      assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
        13261  +
        13262  +      for(i=0; i<db->nDb; i++){
        13263  +        if( db->aDb[i].pBt ){
        13264  +          Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
        13265  +          sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
        13266  +        }
        13267  +      }
        13268  +      *pHighwater = 0;
        13269  +      *pCurrent = nRet;
        13270  +      break;
        13271  +    }
 13194  13272   
 13195  13273       default: {
 13196  13274         rc = SQLITE_ERROR;
 13197  13275       }
 13198  13276     }
 13199  13277     sqlite3_mutex_leave(db->mutex);
 13200  13278     return rc;
................................................................................
 13488  13566     if( p->validTZ ){
 13489  13567       computeJD(p);
 13490  13568     }
 13491  13569     return 0;
 13492  13570   }
 13493  13571   
 13494  13572   /*
 13495         -** Set the time to the current time reported by the VFS
        13573  +** Set the time to the current time reported by the VFS.
        13574  +**
        13575  +** Return the number of errors.
 13496  13576   */
 13497         -static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
        13577  +static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
 13498  13578     sqlite3 *db = sqlite3_context_db_handle(context);
 13499         -  sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD);
 13500         -  p->validJD = 1;
        13579  +  if( sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD)==SQLITE_OK ){
        13580  +    p->validJD = 1;
        13581  +    return 0;
        13582  +  }else{
        13583  +    return 1;
        13584  +  }
 13501  13585   }
 13502  13586   
 13503  13587   /*
 13504  13588   ** Attempt to parse the given string into a Julian Day Number.  Return
 13505  13589   ** the number of errors.
 13506  13590   **
 13507  13591   ** The following are acceptable forms for the input string:
................................................................................
 13523  13607   ){
 13524  13608     double r;
 13525  13609     if( parseYyyyMmDd(zDate,p)==0 ){
 13526  13610       return 0;
 13527  13611     }else if( parseHhMmSs(zDate, p)==0 ){
 13528  13612       return 0;
 13529  13613     }else if( sqlite3StrICmp(zDate,"now")==0){
 13530         -    setDateTimeToCurrent(context, p);
 13531         -    return 0;
        13614  +    return setDateTimeToCurrent(context, p);
 13532  13615     }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
 13533  13616       p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
 13534  13617       p->validJD = 1;
 13535  13618       return 0;
 13536  13619     }
 13537  13620     return 1;
 13538  13621   }
................................................................................
 13951  14034     DateTime *p
 13952  14035   ){
 13953  14036     int i;
 13954  14037     const unsigned char *z;
 13955  14038     int eType;
 13956  14039     memset(p, 0, sizeof(*p));
 13957  14040     if( argc==0 ){
 13958         -    setDateTimeToCurrent(context, p);
 13959         -  }else if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
        14041  +    return setDateTimeToCurrent(context, p);
        14042  +  }
        14043  +  if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
 13960  14044                      || eType==SQLITE_INTEGER ){
 13961  14045       p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
 13962  14046       p->validJD = 1;
 13963  14047     }else{
 13964  14048       z = sqlite3_value_text(argv[0]);
 13965  14049       if( !z || parseDateOrTime(context, (char*)z, p) ){
 13966  14050         return 1;
................................................................................
 14264  14348     int argc,
 14265  14349     sqlite3_value **argv
 14266  14350   ){
 14267  14351     time_t t;
 14268  14352     char *zFormat = (char *)sqlite3_user_data(context);
 14269  14353     sqlite3 *db;
 14270  14354     sqlite3_int64 iT;
        14355  +  struct tm *pTm;
        14356  +  struct tm sNow;
 14271  14357     char zBuf[20];
 14272  14358   
 14273  14359     UNUSED_PARAMETER(argc);
 14274  14360     UNUSED_PARAMETER(argv);
 14275  14361   
 14276  14362     db = sqlite3_context_db_handle(context);
 14277         -  sqlite3OsCurrentTimeInt64(db->pVfs, &iT);
        14363  +  if( sqlite3OsCurrentTimeInt64(db->pVfs, &iT) ) return;
 14278  14364     t = iT/1000 - 10000*(sqlite3_int64)21086676;
 14279  14365   #ifdef HAVE_GMTIME_R
 14280         -  {
 14281         -    struct tm sNow;
 14282         -    gmtime_r(&t, &sNow);
        14366  +  pTm = gmtime_r(&t, &sNow);
        14367  +#else
        14368  +  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
        14369  +  pTm = gmtime(&t);
        14370  +  if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
        14371  +  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
        14372  +#endif
        14373  +  if( pTm ){
 14283  14374       strftime(zBuf, 20, zFormat, &sNow);
        14375  +    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 14284  14376     }
 14285         -#else
 14286         -  {
 14287         -    struct tm *pTm;
 14288         -    sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 14289         -    pTm = gmtime(&t);
 14290         -    strftime(zBuf, 20, zFormat, pTm);
 14291         -    sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 14292         -  }
 14293         -#endif
 14294         -
 14295         -  sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 14296  14377   }
 14297  14378   #endif
 14298  14379   
 14299  14380   /*
 14300  14381   ** This function registered all of the above C functions as SQL
 14301  14382   ** functions.  This should be the only routine in this file with
 14302  14383   ** external linkage.
................................................................................
 14623  14704   
 14624  14705   /*
 14625  14706   ** Register a VFS with the system.  It is harmless to register the same
 14626  14707   ** VFS multiple times.  The new VFS becomes the default if makeDflt is
 14627  14708   ** true.
 14628  14709   */
 14629  14710   SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
 14630         -  sqlite3_mutex *mutex = 0;
        14711  +  MUTEX_LOGIC(sqlite3_mutex *mutex;)
 14631  14712   #ifndef SQLITE_OMIT_AUTOINIT
 14632  14713     int rc = sqlite3_initialize();
 14633  14714     if( rc ) return rc;
 14634  14715   #endif
 14635         -  mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
        14716  +  MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
 14636  14717     sqlite3_mutex_enter(mutex);
 14637  14718     vfsUnlink(pVfs);
 14638  14719     if( makeDflt || vfsList==0 ){
 14639  14720       pVfs->pNext = vfsList;
 14640  14721       vfsList = pVfs;
 14641  14722     }else{
 14642  14723       pVfs->pNext = vfsList->pNext;
................................................................................
 18876  18957   ** the public domain.  The original comments are included here for
 18877  18958   ** completeness.  They are very out-of-date but might be useful as
 18878  18959   ** an historical reference.  Most of the "enhancements" have been backed
 18879  18960   ** out so that the functionality is now the same as standard printf().
 18880  18961   **
 18881  18962   **************************************************************************
 18882  18963   **
 18883         -** The following modules is an enhanced replacement for the "printf" subroutines
 18884         -** found in the standard C library.  The following enhancements are
 18885         -** supported:
 18886         -**
 18887         -**      +  Additional functions.  The standard set of "printf" functions
 18888         -**         includes printf, fprintf, sprintf, vprintf, vfprintf, and
 18889         -**         vsprintf.  This module adds the following:
 18890         -**
 18891         -**           *  snprintf -- Works like sprintf, but has an extra argument
 18892         -**                          which is the size of the buffer written to.
 18893         -**
 18894         -**           *  mprintf --  Similar to sprintf.  Writes output to memory
 18895         -**                          obtained from malloc.
 18896         -**
 18897         -**           *  xprintf --  Calls a function to dispose of output.
 18898         -**
 18899         -**           *  nprintf --  No output, but returns the number of characters
 18900         -**                          that would have been output by printf.
 18901         -**
 18902         -**           *  A v- version (ex: vsnprintf) of every function is also
 18903         -**              supplied.
 18904         -**
 18905         -**      +  A few extensions to the formatting notation are supported:
 18906         -**
 18907         -**           *  The "=" flag (similar to "-") causes the output to be
 18908         -**              be centered in the appropriately sized field.
 18909         -**
 18910         -**           *  The %b field outputs an integer in binary notation.
 18911         -**
 18912         -**           *  The %c field now accepts a precision.  The character output
 18913         -**              is repeated by the number of times the precision specifies.
 18914         -**
 18915         -**           *  The %' field works like %c, but takes as its character the
 18916         -**              next character of the format string, instead of the next
 18917         -**              argument.  For example,  printf("%.78'-")  prints 78 minus
 18918         -**              signs, the same as  printf("%.78c",'-').
 18919         -**
 18920         -**      +  When compiled using GCC on a SPARC, this version of printf is
 18921         -**         faster than the library printf for SUN OS 4.1.
 18922         -**
 18923         -**      +  All functions are fully reentrant.
 18924         -**
        18964  +** This file contains code for a set of "printf"-like routines.  These
        18965  +** routines format strings much like the printf() from the standard C
        18966  +** library, though the implementation here has enhancements to support
        18967  +** SQLlite.
 18925  18968   */
 18926  18969   
 18927  18970   /*
 18928  18971   ** Conversion types fall into various categories as defined by the
 18929  18972   ** following enumeration.
 18930  18973   */
 18931  18974   #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
................................................................................
 19055  19098     if( N>0 ){
 19056  19099       sqlite3StrAccumAppend(pAccum, zSpaces, N);
 19057  19100     }
 19058  19101   }
 19059  19102   
 19060  19103   /*
 19061  19104   ** On machines with a small stack size, you can redefine the
 19062         -** SQLITE_PRINT_BUF_SIZE to be less than 350.
        19105  +** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
 19063  19106   */
 19064  19107   #ifndef SQLITE_PRINT_BUF_SIZE
 19065         -# if defined(SQLITE_SMALL_STACK)
 19066         -#   define SQLITE_PRINT_BUF_SIZE 50
 19067         -# else
 19068         -#   define SQLITE_PRINT_BUF_SIZE 350
 19069         -# endif
        19108  +# define SQLITE_PRINT_BUF_SIZE 70
 19070  19109   #endif
 19071  19110   #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
 19072  19111   
 19073  19112   /*
 19074         -** The root program.  All variations call this core.
 19075         -**
 19076         -** INPUTS:
 19077         -**   func   This is a pointer to a function taking three arguments
 19078         -**            1. A pointer to anything.  Same as the "arg" parameter.
 19079         -**            2. A pointer to the list of characters to be output
 19080         -**               (Note, this list is NOT null terminated.)
 19081         -**            3. An integer number of characters to be output.
 19082         -**               (Note: This number might be zero.)
 19083         -**
 19084         -**   arg    This is the pointer to anything which will be passed as the
 19085         -**          first argument to "func".  Use it for whatever you like.
 19086         -**
 19087         -**   fmt    This is the format string, as in the usual print.
 19088         -**
 19089         -**   ap     This is a pointer to a list of arguments.  Same as in
 19090         -**          vfprint.
 19091         -**
 19092         -** OUTPUTS:
 19093         -**          The return value is the total number of characters sent to
 19094         -**          the function "func".  Returns -1 on a error.
 19095         -**
 19096         -** Note that the order in which automatic variables are declared below
 19097         -** seems to make a big difference in determining how fast this beast
 19098         -** will run.
        19113  +** Render a string given by "fmt" into the StrAccum object.
 19099  19114   */
 19100  19115   SQLITE_PRIVATE void sqlite3VXPrintf(
 19101  19116     StrAccum *pAccum,                  /* Accumulate results here */
 19102  19117     int useExtended,                   /* Allow extended %-conversions */
 19103  19118     const char *fmt,                   /* Format string */
 19104  19119     va_list ap                         /* arguments */
 19105  19120   ){
................................................................................
 19114  19129     etByte flag_blanksign;     /* True if " " flag is present */
 19115  19130     etByte flag_alternateform; /* True if "#" flag is present */
 19116  19131     etByte flag_altform2;      /* True if "!" flag is present */
 19117  19132     etByte flag_zeropad;       /* True if field width constant starts with zero */
 19118  19133     etByte flag_long;          /* True if "l" flag is present */
 19119  19134     etByte flag_longlong;      /* True if the "ll" flag is present */
 19120  19135     etByte done;               /* Loop termination flag */
        19136  +  etByte xtype = 0;          /* Conversion paradigm */
        19137  +  char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
 19121  19138     sqlite_uint64 longvalue;   /* Value for integer types */
 19122  19139     LONGDOUBLE_TYPE realvalue; /* Value for real types */
 19123  19140     const et_info *infop;      /* Pointer to the appropriate info structure */
 19124         -  char buf[etBUFSIZE];       /* Conversion buffer */
 19125         -  char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
 19126         -  etByte xtype = 0;          /* Conversion paradigm */
 19127         -  char *zExtra;              /* Extra memory used for etTCLESCAPE conversions */
        19141  +  char *zOut;                /* Rendering buffer */
        19142  +  int nOut;                  /* Size of the rendering buffer */
        19143  +  char *zExtra;              /* Malloced memory used by some conversion */
 19128  19144   #ifndef SQLITE_OMIT_FLOATING_POINT
 19129  19145     int  exp, e2;              /* exponent of real numbers */
        19146  +  int nsd;                   /* Number of significant digits returned */
 19130  19147     double rounder;            /* Used for rounding floating point values */
 19131  19148     etByte flag_dp;            /* True if decimal point should be shown */
 19132  19149     etByte flag_rtz;           /* True if trailing zeros should be removed */
 19133         -  etByte flag_exp;           /* True to force display of the exponent */
 19134         -  int nsd;                   /* Number of significant digits returned */
 19135  19150   #endif
        19151  +  char buf[etBUFSIZE];       /* Conversion buffer */
 19136  19152   
 19137         -  length = 0;
 19138  19153     bufpt = 0;
 19139  19154     for(; (c=(*fmt))!=0; ++fmt){
 19140  19155       if( c!='%' ){
 19141  19156         int amt;
 19142  19157         bufpt = (char *)fmt;
 19143  19158         amt = 1;
 19144  19159         while( (c=(*++fmt))!='%' && c!=0 ) amt++;
................................................................................
 19175  19190         c = *++fmt;
 19176  19191       }else{
 19177  19192         while( c>='0' && c<='9' ){
 19178  19193           width = width*10 + c - '0';
 19179  19194           c = *++fmt;
 19180  19195         }
 19181  19196       }
 19182         -    if( width > etBUFSIZE-10 ){
 19183         -      width = etBUFSIZE-10;
 19184         -    }
 19185  19197       /* Get the precision */
 19186  19198       if( c=='.' ){
 19187  19199         precision = 0;
 19188  19200         c = *++fmt;
 19189  19201         if( c=='*' ){
 19190  19202           precision = va_arg(ap,int);
 19191  19203           if( precision<0 ) precision = -precision;
................................................................................
 19224  19236             return;
 19225  19237           }
 19226  19238           break;
 19227  19239         }
 19228  19240       }
 19229  19241       zExtra = 0;
 19230  19242   
 19231         -
 19232         -    /* Limit the precision to prevent overflowing buf[] during conversion */
 19233         -    if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
 19234         -      precision = etBUFSIZE-40;
 19235         -    }
 19236         -
 19237  19243       /*
 19238  19244       ** At this point, variables are initialized as follows:
 19239  19245       **
 19240  19246       **   flag_alternateform          TRUE if a '#' is present.
 19241  19247       **   flag_altform2               TRUE if a '!' is present.
 19242  19248       **   flag_plussign               TRUE if a '+' is present.
 19243  19249       **   flag_leftjustify            TRUE if a '-' is present or if the
................................................................................
 19294  19300             }
 19295  19301             prefix = 0;
 19296  19302           }
 19297  19303           if( longvalue==0 ) flag_alternateform = 0;
 19298  19304           if( flag_zeropad && precision<width-(prefix!=0) ){
 19299  19305             precision = width-(prefix!=0);
 19300  19306           }
 19301         -        bufpt = &buf[etBUFSIZE-1];
        19307  +        if( precision<etBUFSIZE-10 ){
        19308  +          nOut = etBUFSIZE;
        19309  +          zOut = buf;
        19310  +        }else{
        19311  +          nOut = precision + 10;
        19312  +          zOut = zExtra = sqlite3Malloc( nOut );
        19313  +          if( zOut==0 ){
        19314  +            pAccum->mallocFailed = 1;
        19315  +            return;
        19316  +          }
        19317  +        }
        19318  +        bufpt = &zOut[nOut-1];
 19302  19319           if( xtype==etORDINAL ){
 19303  19320             static const char zOrd[] = "thstndrd";
 19304  19321             int x = (int)(longvalue % 10);
 19305  19322             if( x>=4 || (longvalue/10)%10==1 ){
 19306  19323               x = 0;
 19307  19324             }
 19308         -          buf[etBUFSIZE-3] = zOrd[x*2];
 19309         -          buf[etBUFSIZE-2] = zOrd[x*2+1];
 19310         -          bufpt -= 2;
        19325  +          *(--bufpt) = zOrd[x*2+1];
        19326  +          *(--bufpt) = zOrd[x*2];
 19311  19327           }
 19312  19328           {
 19313  19329             register const char *cset;      /* Use registers for speed */
 19314  19330             register int base;
 19315  19331             cset = &aDigits[infop->charset];
 19316  19332             base = infop->base;
 19317  19333             do{                                           /* Convert to ascii */
 19318  19334               *(--bufpt) = cset[longvalue%base];
 19319  19335               longvalue = longvalue/base;
 19320  19336             }while( longvalue>0 );
 19321  19337           }
 19322         -        length = (int)(&buf[etBUFSIZE-1]-bufpt);
        19338  +        length = (int)(&zOut[nOut-1]-bufpt);
 19323  19339           for(idx=precision-length; idx>0; idx--){
 19324  19340             *(--bufpt) = '0';                             /* Zero pad */
 19325  19341           }
 19326  19342           if( prefix ) *(--bufpt) = prefix;               /* Add sign */
 19327  19343           if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
 19328  19344             const char *pre;
 19329  19345             char x;
 19330  19346             pre = &aPrefix[infop->prefix];
 19331  19347             for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
 19332  19348           }
 19333         -        length = (int)(&buf[etBUFSIZE-1]-bufpt);
        19349  +        length = (int)(&zOut[nOut-1]-bufpt);
 19334  19350           break;
 19335  19351         case etFLOAT:
 19336  19352         case etEXP:
 19337  19353         case etGENERIC:
 19338  19354           realvalue = va_arg(ap,double);
 19339  19355   #ifdef SQLITE_OMIT_FLOATING_POINT
 19340  19356           length = 0;
 19341  19357   #else
 19342  19358           if( precision<0 ) precision = 6;         /* Set default precision */
 19343         -        if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
 19344  19359           if( realvalue<0.0 ){
 19345  19360             realvalue = -realvalue;
 19346  19361             prefix = '-';
 19347  19362           }else{
 19348  19363             if( flag_plussign )          prefix = '+';
 19349  19364             else if( flag_blanksign )    prefix = ' ';
 19350  19365             else                         prefix = 0;
................................................................................
 19384  19399             }
 19385  19400           }
 19386  19401           bufpt = buf;
 19387  19402           /*
 19388  19403           ** If the field type is etGENERIC, then convert to either etEXP
 19389  19404           ** or etFLOAT, as appropriate.
 19390  19405           */
 19391         -        flag_exp = xtype==etEXP;
 19392  19406           if( xtype!=etFLOAT ){
 19393  19407             realvalue += rounder;
 19394  19408             if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
 19395  19409           }
 19396  19410           if( xtype==etGENERIC ){
 19397  19411             flag_rtz = !flag_alternateform;
 19398  19412             if( exp<-4 || exp>precision ){
................................................................................
 19405  19419             flag_rtz = 0;
 19406  19420           }
 19407  19421           if( xtype==etEXP ){
 19408  19422             e2 = 0;
 19409  19423           }else{
 19410  19424             e2 = exp;
 19411  19425           }
        19426  +        if( e2+precision+width > etBUFSIZE - 15 ){
        19427  +          bufpt = zExtra = sqlite3Malloc( e2+precision+width+15 );
        19428  +          if( bufpt==0 ){
        19429  +            pAccum->mallocFailed = 1;
        19430  +            return;
        19431  +          }
        19432  +        }
        19433  +        zOut = bufpt;
 19412  19434           nsd = 0;
 19413  19435           flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
 19414  19436           /* The sign in front of the number */
 19415  19437           if( prefix ){
 19416  19438             *(bufpt++) = prefix;
 19417  19439           }
 19418  19440           /* Digits prior to the decimal point */
................................................................................
 19436  19458           /* Significant digits after the decimal point */
 19437  19459           while( (precision--)>0 ){
 19438  19460             *(bufpt++) = et_getdigit(&realvalue,&nsd);
 19439  19461           }
 19440  19462           /* Remove trailing zeros and the "." if no digits follow the "." */
 19441  19463           if( flag_rtz && flag_dp ){
 19442  19464             while( bufpt[-1]=='0' ) *(--bufpt) = 0;
 19443         -          assert( bufpt>buf );
        19465  +          assert( bufpt>zOut );
 19444  19466             if( bufpt[-1]=='.' ){
 19445  19467               if( flag_altform2 ){
 19446  19468                 *(bufpt++) = '0';
 19447  19469               }else{
 19448  19470                 *(--bufpt) = 0;
 19449  19471               }
 19450  19472             }
 19451  19473           }
 19452  19474           /* Add the "eNNN" suffix */
 19453         -        if( flag_exp || xtype==etEXP ){
        19475  +        if( xtype==etEXP ){
 19454  19476             *(bufpt++) = aDigits[infop->charset];
 19455  19477             if( exp<0 ){
 19456  19478               *(bufpt++) = '-'; exp = -exp;
 19457  19479             }else{
 19458  19480               *(bufpt++) = '+';
 19459  19481             }
 19460  19482             if( exp>=100 ){
................................................................................
 19465  19487             *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
 19466  19488           }
 19467  19489           *bufpt = 0;
 19468  19490   
 19469  19491           /* The converted number is in buf[] and zero terminated. Output it.
 19470  19492           ** Note that the number is in the usual order, not reversed as with
 19471  19493           ** integer conversions. */
 19472         -        length = (int)(bufpt-buf);
 19473         -        bufpt = buf;
        19494  +        length = (int)(bufpt-zOut);
        19495  +        bufpt = zOut;
 19474  19496   
 19475  19497           /* Special case:  Add leading zeros if the flag_zeropad flag is
 19476  19498           ** set and we are not left justified */
 19477  19499           if( flag_zeropad && !flag_leftjustify && length < width){
 19478  19500             int i;
 19479  19501             int nPad = width - length;
 19480  19502             for(i=width; i>=nPad; i--){
................................................................................
 19604  19626       if( flag_leftjustify ){
 19605  19627         register int nspace;
 19606  19628         nspace = width-length;
 19607  19629         if( nspace>0 ){
 19608  19630           appendSpace(pAccum, nspace);
 19609  19631         }
 19610  19632       }
 19611         -    if( zExtra ){
 19612         -      sqlite3_free(zExtra);
 19613         -    }
        19633  +    sqlite3_free(zExtra);
 19614  19634     }/* End for loop over the format string */
 19615  19635   } /* End of function */
 19616  19636   
 19617  19637   /*
 19618  19638   ** Append N bytes of text from z to the StrAccum object.
 19619  19639   */
 19620  19640   SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
 19621  19641     assert( z!=0 || N==0 );
 19622  19642     if( p->tooBig | p->mallocFailed ){
 19623  19643       testcase(p->tooBig);
 19624  19644       testcase(p->mallocFailed);
 19625  19645       return;
 19626  19646     }
        19647  +  assert( p->zText!=0 || p->nChar==0 );
 19627  19648     if( N<0 ){
 19628  19649       N = sqlite3Strlen30(z);
 19629  19650     }
 19630  19651     if( N==0 || NEVER(z==0) ){
 19631  19652       return;
 19632  19653     }
 19633  19654     if( p->nChar+N >= p->nAlloc ){
................................................................................
 19651  19672         }
 19652  19673         if( p->useMalloc==1 ){
 19653  19674           zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
 19654  19675         }else{
 19655  19676           zNew = sqlite3_realloc(zOld, p->nAlloc);
 19656  19677         }
 19657  19678         if( zNew ){
 19658         -        if( zOld==0 ) memcpy(zNew, p->zText, p->nChar);
        19679  +        if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
 19659  19680           p->zText = zNew;
 19660  19681         }else{
 19661  19682           p->mallocFailed = 1;
 19662  19683           sqlite3StrAccumReset(p);
 19663  19684           return;
 19664  19685         }
 19665  19686       }
 19666  19687     }
        19688  +  assert( p->zText );
 19667  19689     memcpy(&p->zText[p->nChar], z, N);
 19668  19690     p->nChar += N;
 19669  19691   }
 19670  19692   
 19671  19693   /*
 19672  19694   ** Finish off a string by making sure it is zero-terminated.
 19673  19695   ** Return a pointer to the resulting string.  Return a NULL
................................................................................
 20509  20531   ** is set to the length of the returned string in bytes. The call should
 20510  20532   ** arrange to call sqlite3DbFree() on the returned pointer when it is
 20511  20533   ** no longer required.
 20512  20534   ** 
 20513  20535   ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
 20514  20536   ** flag set.
 20515  20537   */
 20516         -#ifdef SQLITE_ENABLE_STAT2
        20538  +#ifdef SQLITE_ENABLE_STAT3
 20517  20539   SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
 20518  20540     Mem m;
 20519  20541     memset(&m, 0, sizeof(m));
 20520  20542     m.db = db;
 20521  20543     sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
 20522  20544     if( sqlite3VdbeMemTranslate(&m, enc) ){
 20523  20545       assert( db->mallocFailed );
................................................................................
 20938  20960         esign = -1;
 20939  20961         z+=incr;
 20940  20962       }else if( *z=='+' ){
 20941  20963         z+=incr;
 20942  20964       }
 20943  20965       /* copy digits to exponent */
 20944  20966       while( z<zEnd && sqlite3Isdigit(*z) ){
 20945         -      e = e*10 + (*z - '0');
        20967  +      e = e<10000 ? (e*10 + (*z - '0')) : 10000;
 20946  20968         z+=incr;
 20947  20969         eValid = 1;
 20948  20970       }
 20949  20971     }
 20950  20972   
 20951  20973     /* skip trailing spaces */
 20952  20974     if( nDigits && eValid ){
................................................................................
 20989  21011           if( esign<0 ){
 20990  21012             result = s / scale;
 20991  21013             result /= 1.0e+308;
 20992  21014           }else{
 20993  21015             result = s * scale;
 20994  21016             result *= 1.0e+308;
 20995  21017           }
        21018  +      }else if( e>=342 ){
        21019  +        if( esign<0 ){
        21020  +          result = 0.0*s;
        21021  +        }else{
        21022  +          result = 1e308*1e308*s;  /* Infinity */
        21023  +        }
 20996  21024         }else{
 20997  21025           /* 1.0e+22 is the largest power of 10 than can be 
 20998  21026           ** represented exactly. */
 20999  21027           while( e%22 ) { scale *= 1.0e+1; e -= 1; }
 21000  21028           while( e>0 ) { scale *= 1.0e+22; e -= 22; }
 21001  21029           if( esign<0 ){
 21002  21030             result = s / scale;
................................................................................
 25100  25128   #ifdef SQLITE_DEBUG
 25101  25129   static int unixMutexHeld(void) {
 25102  25130     return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 25103  25131   }
 25104  25132   #endif
 25105  25133   
 25106  25134   
 25107         -#ifdef SQLITE_DEBUG
        25135  +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 25108  25136   /*
 25109  25137   ** Helper function for printing out trace information from debugging
 25110  25138   ** binaries. This returns the string represetation of the supplied
 25111  25139   ** integer lock-type.
 25112  25140   */
 25113  25141   static const char *azFileLock(int eFileLock){
 25114  25142     switch( eFileLock ){
................................................................................
 25935  25963     ** The reason a single byte cannot be used instead of the 'shared byte
 25936  25964     ** range' is that some versions of windows do not support read-locks. By
 25937  25965     ** locking a random byte from a range, concurrent SHARED locks may exist
 25938  25966     ** even if the locking primitive used is always a write-lock.
 25939  25967     */
 25940  25968     int rc = SQLITE_OK;
 25941  25969     unixFile *pFile = (unixFile*)id;
 25942         -  unixInodeInfo *pInode = pFile->pInode;
        25970  +  unixInodeInfo *pInode;
 25943  25971     struct flock lock;
 25944  25972     int tErrno = 0;
 25945  25973   
 25946  25974     assert( pFile );
 25947  25975     OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
 25948  25976         azFileLock(eFileLock), azFileLock(pFile->eFileLock),
 25949         -      azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
        25977  +      azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid()));
 25950  25978   
 25951  25979     /* If there is already a lock of this type or more restrictive on the
 25952  25980     ** unixFile, do nothing. Don't use the end_lock: exit path, as
 25953  25981     ** unixEnterMutex() hasn't been called yet.
 25954  25982     */
 25955  25983     if( pFile->eFileLock>=eFileLock ){
 25956  25984       OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
................................................................................
 26146  26174   ** remove the write lock on a region when a read lock is set.
 26147  26175   */
 26148  26176   static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
 26149  26177     unixFile *pFile = (unixFile*)id;
 26150  26178     unixInodeInfo *pInode;
 26151  26179     struct flock lock;
 26152  26180     int rc = SQLITE_OK;
 26153         -  int h;
 26154  26181   
 26155  26182     assert( pFile );
 26156  26183     OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
 26157  26184         pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
 26158  26185         getpid()));
 26159  26186   
 26160  26187     assert( eFileLock<=SHARED_LOCK );
 26161  26188     if( pFile->eFileLock<=eFileLock ){
 26162  26189       return SQLITE_OK;
 26163  26190     }
 26164  26191     unixEnterMutex();
 26165         -  h = pFile->h;
 26166  26192     pInode = pFile->pInode;
 26167  26193     assert( pInode->nShared!=0 );
 26168  26194     if( pFile->eFileLock>SHARED_LOCK ){
 26169  26195       assert( pInode->eFileLock==pFile->eFileLock );
 26170         -    SimulateIOErrorBenign(1);
 26171         -    SimulateIOError( h=(-1) )
 26172         -    SimulateIOErrorBenign(0);
 26173  26196   
 26174  26197   #ifndef NDEBUG
 26175  26198       /* When reducing a lock such that other processes can start
 26176  26199       ** reading the database file again, make sure that the
 26177  26200       ** transaction counter was updated if any part of the database
 26178  26201       ** file changed.  If the transaction counter is not updated,
 26179  26202       ** other connections to the same file might not realize that
 26180  26203       ** the file has changed and hence might not know to flush their
 26181  26204       ** cache.  The use of a stale cache can lead to database corruption.
 26182  26205       */
 26183         -#if 0
 26184         -    assert( pFile->inNormalWrite==0
 26185         -         || pFile->dbUpdate==0
 26186         -         || pFile->transCntrChng==1 );
 26187         -#endif
 26188  26206       pFile->inNormalWrite = 0;
 26189  26207   #endif
 26190  26208   
 26191  26209       /* downgrading to a shared lock on NFS involves clearing the write lock
 26192  26210       ** before establishing the readlock - to avoid a race condition we downgrade
 26193  26211       ** the lock in 2 blocks, so that part of the range will be covered by a 
 26194  26212       ** write lock until the rest is covered by a read lock:
................................................................................
 26282  26300       ** the lock.
 26283  26301       */
 26284  26302       pInode->nShared--;
 26285  26303       if( pInode->nShared==0 ){
 26286  26304         lock.l_type = F_UNLCK;
 26287  26305         lock.l_whence = SEEK_SET;
 26288  26306         lock.l_start = lock.l_len = 0L;
 26289         -      SimulateIOErrorBenign(1);
 26290         -      SimulateIOError( h=(-1) )
 26291         -      SimulateIOErrorBenign(0);
 26292  26307         if( unixFileLock(pFile, &lock)==0 ){
 26293  26308           pInode->eFileLock = NO_LOCK;
 26294  26309         }else{
 26295  26310           rc = SQLITE_IOERR_UNLOCK;
 26296  26311   	pFile->lastErrno = errno;
 26297  26312           pInode->eFileLock = NO_LOCK;
 26298  26313           pFile->eFileLock = NO_LOCK;
................................................................................
 28426  28441       pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 28427  28442       if( pShmNode->mutex==0 ){
 28428  28443         rc = SQLITE_NOMEM;
 28429  28444         goto shm_open_err;
 28430  28445       }
 28431  28446   
 28432  28447       if( pInode->bProcessLock==0 ){
 28433         -      pShmNode->h = robust_open(zShmFilename, O_RDWR|O_CREAT,
 28434         -                               (sStat.st_mode & 0777));
        28448  +      const char *zRO;
        28449  +      int openFlags = O_RDWR | O_CREAT;
        28450  +      zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm");
        28451  +      if( zRO && sqlite3GetBoolean(zRO) ){
        28452  +        openFlags = O_RDONLY;
        28453  +        pShmNode->isReadonly = 1;
        28454  +      }
        28455  +      pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
 28435  28456         if( pShmNode->h<0 ){
 28436         -        const char *zRO;
 28437         -        zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm");
 28438         -        if( zRO && sqlite3GetBoolean(zRO) ){
 28439         -          pShmNode->h = robust_open(zShmFilename, O_RDONLY,
 28440         -                                    (sStat.st_mode & 0777));
 28441         -          pShmNode->isReadonly = 1;
 28442         -        }
 28443  28457           if( pShmNode->h<0 ){
 28444  28458             rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
 28445  28459             goto shm_open_err;
 28446  28460           }
 28447  28461         }
 28448  28462     
 28449  28463         /* Check to see if another process is holding the dead-man switch.
................................................................................
 29120  29134     */
 29121  29135   #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 29122  29136     assert( zFilename==0 || zFilename[0]=='/' 
 29123  29137       || pVfs->pAppData==(void*)&autolockIoFinder );
 29124  29138   #else
 29125  29139     assert( zFilename==0 || zFilename[0]=='/' );
 29126  29140   #endif
        29141  +
        29142  +  /* No locking occurs in temporary files */
        29143  +  assert( zFilename!=0 || noLock );
 29127  29144   
 29128  29145     OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
 29129  29146     pNew->h = h;
 29130  29147     pNew->zPath = zFilename;
 29131  29148     if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
 29132  29149       pNew->ctrlFlags = UNIXFILE_EXCL;
 29133  29150     }else{
................................................................................
 29222  29239   
 29223  29240     else if( pLockingStyle == &dotlockIoMethods ){
 29224  29241       /* Dotfile locking uses the file path so it needs to be included in
 29225  29242       ** the dotlockLockingContext 
 29226  29243       */
 29227  29244       char *zLockFile;
 29228  29245       int nFilename;
        29246  +    assert( zFilename!=0 );
 29229  29247       nFilename = (int)strlen(zFilename) + 6;
 29230  29248       zLockFile = (char *)sqlite3_malloc(nFilename);
 29231  29249       if( zLockFile==0 ){
 29232  29250         rc = SQLITE_NOMEM;
 29233  29251       }else{
 29234  29252         sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
 29235  29253       }
................................................................................
 29456  29474       ** the following naming conventions:
 29457  29475       **
 29458  29476       **   "<path to db>-journal"
 29459  29477       **   "<path to db>-wal"
 29460  29478       **   "<path to db>-journalNN"
 29461  29479       **   "<path to db>-walNN"
 29462  29480       **
 29463         -    ** where NN is a 4 digit decimal number. The NN naming schemes are 
        29481  +    ** where NN is a decimal number. The NN naming schemes are 
 29464  29482       ** used by the test_multiplex.c module.
 29465  29483       */
 29466  29484       nDb = sqlite3Strlen30(zPath) - 1; 
 29467         -    while( nDb>0 && zPath[nDb]!='-' ) nDb--;
 29468         -    if( nDb==0 ) return SQLITE_OK;
        29485  +#ifdef SQLITE_ENABLE_8_3_NAMES
        29486  +    while( nDb>0 && !sqlite3Isalnum(zPath[nDb]) ) nDb--;
        29487  +    if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
        29488  +#else
        29489  +    while( zPath[nDb]!='-' ){
        29490  +      assert( nDb>0 );
        29491  +      assert( zPath[nDb]!='\n' );
        29492  +      nDb--;
        29493  +    }
        29494  +#endif
 29469  29495       memcpy(zDb, zPath, nDb);
 29470  29496       zDb[nDb] = '\0';
 29471  29497   
 29472  29498       if( 0==osStat(zDb, &sStat) ){
 29473  29499         *pMode = sStat.st_mode & 0777;
 29474  29500       }else{
 29475  29501         rc = SQLITE_IOERR_FSTAT;
................................................................................
 29993  30019   /*
 29994  30020   ** Find the current time (in Universal Coordinated Time).  Write into *piNow
 29995  30021   ** the current time and date as a Julian Day number times 86_400_000.  In
 29996  30022   ** other words, write into *piNow the number of milliseconds since the Julian
 29997  30023   ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
 29998  30024   ** proleptic Gregorian calendar.
 29999  30025   **
 30000         -** On success, return 0.  Return 1 if the time and date cannot be found.
        30026  +** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date 
        30027  +** cannot be found.
 30001  30028   */
 30002  30029   static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
 30003  30030     static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
        30031  +  int rc = SQLITE_OK;
 30004  30032   #if defined(NO_GETTOD)
 30005  30033     time_t t;
 30006  30034     time(&t);
 30007  30035     *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
 30008  30036   #elif OS_VXWORKS
 30009  30037     struct timespec sNow;
 30010  30038     clock_gettime(CLOCK_REALTIME, &sNow);
 30011  30039     *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
 30012  30040   #else
 30013  30041     struct timeval sNow;
 30014         -  gettimeofday(&sNow, 0);
 30015         -  *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
        30042  +  if( gettimeofday(&sNow, 0)==0 ){
        30043  +    *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
        30044  +  }else{
        30045  +    rc = SQLITE_ERROR;
        30046  +  }
 30016  30047   #endif
 30017  30048   
 30018  30049   #ifdef SQLITE_TEST
 30019  30050     if( sqlite3_current_time ){
 30020  30051       *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
 30021  30052     }
 30022  30053   #endif
 30023  30054     UNUSED_PARAMETER(NotUsed);
 30024         -  return 0;
        30055  +  return rc;
 30025  30056   }
 30026  30057   
 30027  30058   /*
 30028  30059   ** Find the current time (in Universal Coordinated Time).  Write the
 30029  30060   ** current time and date as a Julian Day number into *prNow and
 30030  30061   ** return 0.  Return 1 if the time and date cannot be found.
 30031  30062   */
 30032  30063   static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
 30033         -  sqlite3_int64 i;
        30064  +  sqlite3_int64 i = 0;
        30065  +  int rc;
 30034  30066     UNUSED_PARAMETER(NotUsed);
 30035         -  unixCurrentTimeInt64(0, &i);
        30067  +  rc = unixCurrentTimeInt64(0, &i);
 30036  30068     *prNow = i/86400000.0;
 30037         -  return 0;
        30069  +  return rc;
 30038  30070   }
 30039  30071   
 30040  30072   /*
 30041  30073   ** We added the xGetLastError() method with the intention of providing
 30042  30074   ** better low-level error messages when operating-system problems come up
 30043  30075   ** during SQLite operation.  But so far, none of that has been implemented
 30044  30076   ** in the core.  So this routine is never called.  For now, it is merely
................................................................................
 34167  34199              h, zName, dwDesiredAccess, 
 34168  34200              h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
 34169  34201   
 34170  34202     if( h==INVALID_HANDLE_VALUE ){
 34171  34203       pFile->lastErrno = GetLastError();
 34172  34204       winLogError(SQLITE_CANTOPEN, "winOpen", zUtf8Name);
 34173  34205       free(zConverted);
 34174         -    if( isReadWrite ){
        34206  +    if( isReadWrite && !isExclusive ){
 34175  34207         return winOpen(pVfs, zName, id, 
 34176  34208                ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
 34177  34209       }else{
 34178  34210         return SQLITE_CANTOPEN_BKPT;
 34179  34211       }
 34180  34212     }
 34181  34213   
................................................................................
 34533  34565     free(zConverted);
 34534  34566     return (void*)h;
 34535  34567   }
 34536  34568   static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
 34537  34569     UNUSED_PARAMETER(pVfs);
 34538  34570     getLastErrorMsg(nBuf, zBufOut);
 34539  34571   }
 34540         -void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
        34572  +static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
 34541  34573     UNUSED_PARAMETER(pVfs);
 34542  34574   #if SQLITE_OS_WINCE
 34543  34575     /* The GetProcAddressA() routine is only available on wince. */
 34544  34576     return (void(*)(void))GetProcAddressA((HANDLE)pHandle, zSymbol);
 34545  34577   #else
 34546  34578     /* All other windows platforms expect GetProcAddress() to take
 34547  34579     ** an Ansi string regardless of the _UNICODE setting */
 34548  34580     return (void(*)(void))GetProcAddress((HANDLE)pHandle, zSymbol);
 34549  34581   #endif
 34550  34582   }
 34551         -void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
        34583  +static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
 34552  34584     UNUSED_PARAMETER(pVfs);
 34553  34585     FreeLibrary((HANDLE)pHandle);
 34554  34586   }
 34555  34587   #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
 34556  34588     #define winDlOpen  0
 34557  34589     #define winDlError 0
 34558  34590     #define winDlSym   0
................................................................................
 34618  34650   /*
 34619  34651   ** Find the current time (in Universal Coordinated Time).  Write into *piNow
 34620  34652   ** the current time and date as a Julian Day number times 86_400_000.  In
 34621  34653   ** other words, write into *piNow the number of milliseconds since the Julian
 34622  34654   ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
 34623  34655   ** proleptic Gregorian calendar.
 34624  34656   **
 34625         -** On success, return 0.  Return 1 if the time and date cannot be found.
        34657  +** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date 
        34658  +** cannot be found.
 34626  34659   */
 34627  34660   static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
 34628  34661     /* FILETIME structure is a 64-bit value representing the number of 
 34629  34662        100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). 
 34630  34663     */
 34631  34664     FILETIME ft;
 34632  34665     static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
................................................................................
 34638  34671         (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
 34639  34672   
 34640  34673   #if SQLITE_OS_WINCE
 34641  34674     SYSTEMTIME time;
 34642  34675     GetSystemTime(&time);
 34643  34676     /* if SystemTimeToFileTime() fails, it returns zero. */
 34644  34677     if (!SystemTimeToFileTime(&time,&ft)){
 34645         -    return 1;
        34678  +    return SQLITE_ERROR;
 34646  34679     }
 34647  34680   #else
 34648  34681     GetSystemTimeAsFileTime( &ft );
 34649  34682   #endif
 34650  34683   
 34651  34684     *piNow = winFiletimeEpoch +
 34652  34685               ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + 
................................................................................
 34654  34687   
 34655  34688   #ifdef SQLITE_TEST
 34656  34689     if( sqlite3_current_time ){
 34657  34690       *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
 34658  34691     }
 34659  34692   #endif
 34660  34693     UNUSED_PARAMETER(pVfs);
 34661         -  return 0;
        34694  +  return SQLITE_OK;
 34662  34695   }
 34663  34696   
 34664  34697   /*
 34665  34698   ** Find the current time (in Universal Coordinated Time).  Write the
 34666  34699   ** current time and date as a Julian Day number into *prNow and
 34667  34700   ** return 0.  Return 1 if the time and date cannot be found.
 34668  34701   */
 34669         -int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
        34702  +static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
 34670  34703     int rc;
 34671  34704     sqlite3_int64 i;
 34672  34705     rc = winCurrentTimeInt64(pVfs, &i);
 34673  34706     if( !rc ){
 34674  34707       *prNow = i/86400000.0;
 34675  34708     }
 34676  34709     return rc;
................................................................................
 35787  35820   
 35788  35821   
 35789  35822   typedef struct PCache1 PCache1;
 35790  35823   typedef struct PgHdr1 PgHdr1;
 35791  35824   typedef struct PgFreeslot PgFreeslot;
 35792  35825   typedef struct PGroup PGroup;
 35793  35826   
 35794         -typedef struct PGroupBlock PGroupBlock;
 35795         -typedef struct PGroupBlockList PGroupBlockList;
 35796  35827   
 35797  35828   /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
 35798  35829   ** of one or more PCaches that are able to recycle each others unpinned
 35799  35830   ** pages when they are under memory pressure.  A PGroup is an instance of
 35800  35831   ** the following object.
 35801  35832   **
 35802  35833   ** This page cache implementation works in one of two modes:
................................................................................
 35819  35850   struct PGroup {
 35820  35851     sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
 35821  35852     int nMaxPage;                  /* Sum of nMax for purgeable caches */
 35822  35853     int nMinPage;                  /* Sum of nMin for purgeable caches */
 35823  35854     int mxPinned;                  /* nMaxpage + 10 - nMinPage */
 35824  35855     int nCurrentPage;              /* Number of purgeable pages allocated */
 35825  35856     PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
 35826         -#ifdef SQLITE_PAGECACHE_BLOCKALLOC
 35827         -  int isBusy;                    /* Do not run ReleaseMemory() if true */
 35828         -  PGroupBlockList *pBlockList;   /* List of block-lists for this group */
 35829         -#endif
 35830  35857   };
 35831  35858   
 35832         -/*
 35833         -** If SQLITE_PAGECACHE_BLOCKALLOC is defined when the library is built,
 35834         -** each PGroup structure has a linked list of the the following starting
 35835         -** at PGroup.pBlockList. There is one entry for each distinct page-size 
 35836         -** currently used by members of the PGroup (i.e. 1024 bytes, 4096 bytes
 35837         -** etc.). Variable PGroupBlockList.nByte is set to the actual allocation
 35838         -** size requested by each pcache, which is the database page-size plus
 35839         -** the various header structures used by the pcache, pager and btree layers.
 35840         -** Usually around (pgsz+200) bytes.
 35841         -**
 35842         -** This size (pgsz+200) bytes is not allocated efficiently by some
 35843         -** implementations of malloc. In particular, some implementations are only
 35844         -** able to allocate blocks of memory chunks of 2^N bytes, where N is some
 35845         -** integer value. Since the page-size is a power of 2, this means we
 35846         -** end up wasting (pgsz-200) bytes in each allocation.
 35847         -**
 35848         -** If SQLITE_PAGECACHE_BLOCKALLOC is defined, the (pgsz+200) byte blocks
 35849         -** are not allocated directly. Instead, blocks of roughly M*(pgsz+200) bytes 
 35850         -** are requested from malloc allocator. After a block is returned,
 35851         -** sqlite3MallocSize() is used to determine how many (pgsz+200) byte
 35852         -** allocations can fit in the space returned by malloc(). This value may
 35853         -** be more than M.
 35854         -**
 35855         -** The blocks are stored in a doubly-linked list. Variable PGroupBlock.nEntry
 35856         -** contains the number of allocations that will fit in the aData[] space.
 35857         -** nEntry is limited to the number of bits in bitmask mUsed. If a slot
 35858         -** within aData is in use, the corresponding bit in mUsed is set. Thus
 35859         -** when (mUsed+1==(1 << nEntry)) the block is completely full.
 35860         -**
 35861         -** Each time a slot within a block is freed, the block is moved to the start
 35862         -** of the linked-list. And if a block becomes completely full, then it is
 35863         -** moved to the end of the list. As a result, when searching for a free
 35864         -** slot, only the first block in the list need be examined. If it is full,
 35865         -** then it is guaranteed that all blocks are full.
 35866         -*/
 35867         -struct PGroupBlockList {
 35868         -  int nByte;                     /* Size of each allocation in bytes */
 35869         -  PGroupBlock *pFirst;           /* First PGroupBlock in list */
 35870         -  PGroupBlock *pLast;            /* Last PGroupBlock in list */
 35871         -  PGroupBlockList *pNext;        /* Next block-list attached to group */
 35872         -};
 35873         -
 35874         -struct PGroupBlock {
 35875         -  Bitmask mUsed;                 /* Mask of used slots */
 35876         -  int nEntry;                    /* Maximum number of allocations in aData[] */
 35877         -  u8 *aData;                     /* Pointer to data block */
 35878         -  PGroupBlock *pNext;            /* Next PGroupBlock in list */
 35879         -  PGroupBlock *pPrev;            /* Previous PGroupBlock in list */
 35880         -  PGroupBlockList *pList;        /* Owner list */
 35881         -};
 35882         -
 35883         -/* Minimum value for PGroupBlock.nEntry */
 35884         -#define PAGECACHE_BLOCKALLOC_MINENTRY 15
 35885         -
 35886  35859   /* Each page cache is an instance of the following object.  Every
 35887  35860   ** open database file (including each in-memory database and each
 35888  35861   ** temporary or transient database) has a single page cache which
 35889  35862   ** is an instance of this object.
 35890  35863   **
 35891  35864   ** Pointers to structures of this type are cast and returned as 
 35892  35865   ** opaque sqlite3_pcache* handles.
................................................................................
 35981  35954   ** a pointer to a block of szPage bytes of data and the return value is
 35982  35955   ** a pointer to the associated PgHdr1 structure.
 35983  35956   **
 35984  35957   **   assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
 35985  35958   */
 35986  35959   #define PGHDR1_TO_PAGE(p)    (void*)(((char*)p) - p->pCache->szPage)
 35987  35960   #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
 35988         -
 35989         -/*
 35990         -** Blocks used by the SQLITE_PAGECACHE_BLOCKALLOC blocks to store/retrieve 
 35991         -** a PGroupBlock pointer based on a pointer to a page buffer. 
 35992         -*/
 35993         -#define PAGE_SET_BLOCKPTR(pCache, pPg, pBlock) \
 35994         -  ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) = pBlock )
 35995         -
 35996         -#define PAGE_GET_BLOCKPTR(pCache, pPg) \
 35997         -  ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) )
 35998         -
 35999  35961   
 36000  35962   /*
 36001  35963   ** Macros to enter and leave the PCache LRU mutex.
 36002  35964   */
 36003  35965   #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
 36004  35966   #define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
 36005  35967   
................................................................................
 36118  36080       iSize = sqlite3MallocSize(p);
 36119  36081       sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
 36120  36082       return iSize;
 36121  36083     }
 36122  36084   }
 36123  36085   #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
 36124  36086   
 36125         -#ifdef SQLITE_PAGECACHE_BLOCKALLOC
 36126         -/*
 36127         -** The block pBlock belongs to list pList but is not currently linked in.
 36128         -** Insert it into the start of the list.
 36129         -*/
 36130         -static void addBlockToList(PGroupBlockList *pList, PGroupBlock *pBlock){
 36131         -  pBlock->pPrev = 0;
 36132         -  pBlock->pNext = pList->pFirst;
 36133         -  pList->pFirst = pBlock;
 36134         -  if( pBlock->pNext ){
 36135         -    pBlock->pNext->pPrev = pBlock;
 36136         -  }else{
 36137         -    assert( pList->pLast==0 );
 36138         -    pList->pLast = pBlock;
 36139         -  }
 36140         -}
 36141         -
 36142         -/*
 36143         -** If there are no blocks in the list headed by pList, remove pList
 36144         -** from the pGroup->pBlockList list and free it with sqlite3_free().
 36145         -*/
 36146         -static void freeListIfEmpty(PGroup *pGroup, PGroupBlockList *pList){
 36147         -  assert( sqlite3_mutex_held(pGroup->mutex) );
 36148         -  if( pList->pFirst==0 ){
 36149         -    PGroupBlockList **pp;
 36150         -    for(pp=&pGroup->pBlockList; *pp!=pList; pp=&(*pp)->pNext);
 36151         -    *pp = (*pp)->pNext;
 36152         -    sqlite3_free(pList);
 36153         -  }
 36154         -}
 36155         -#endif /* SQLITE_PAGECACHE_BLOCKALLOC */
 36156         -
 36157  36087   /*
 36158  36088   ** Allocate a new page object initially associated with cache pCache.
 36159  36089   */
 36160  36090   static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
 36161  36091     int nByte = sizeof(PgHdr1) + pCache->szPage;
 36162         -  void *pPg = 0;
 36163         -  PgHdr1 *p;
        36092  +  PgHdr1 *p = 0;
        36093  +  void *pPg;
 36164  36094   
 36165         -#ifdef SQLITE_PAGECACHE_BLOCKALLOC
 36166         -  PGroup *pGroup = pCache->pGroup;
 36167         -  PGroupBlockList *pList;
 36168         -  PGroupBlock *pBlock;
 36169         -  int i;
 36170         -
 36171         -  nByte += sizeof(PGroupBlockList *);
 36172         -  nByte = ROUND8(nByte);
 36173         -
 36174         -  for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
 36175         -    if( pList->nByte==nByte ) break;
 36176         -  }
 36177         -  if( pList==0 ){
 36178         -    PGroupBlockList *pNew;
 36179         -    assert( pGroup->isBusy==0 );
 36180         -    assert( sqlite3_mutex_held(pGroup->mutex) );
 36181         -    pGroup->isBusy = 1;  /* Disable sqlite3PcacheReleaseMemory() */
 36182         -    pNew = (PGroupBlockList *)sqlite3MallocZero(sizeof(PGroupBlockList));
 36183         -    pGroup->isBusy = 0;  /* Reenable sqlite3PcacheReleaseMemory() */
 36184         -    if( pNew==0 ){
 36185         -      /* malloc() failure. Return early. */
 36186         -      return 0;
 36187         -    }
 36188         -#ifdef SQLITE_DEBUG
 36189         -    for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
 36190         -      assert( pList->nByte!=nByte );
 36191         -    }
 36192         -#endif
 36193         -    pNew->nByte = nByte;
 36194         -    pNew->pNext = pGroup->pBlockList;
 36195         -    pGroup->pBlockList = pNew;
 36196         -    pList = pNew;
 36197         -  }
 36198         -
 36199         -  pBlock = pList->pFirst;
 36200         -  if( pBlock==0 || pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) ){
 36201         -    int sz;
 36202         -
 36203         -    /* Allocate a new block. Try to allocate enough space for the PGroupBlock
 36204         -    ** structure and MINENTRY allocations of nByte bytes each. If the 
 36205         -    ** allocator returns more memory than requested, then more than MINENTRY 
 36206         -    ** allocations may fit in it. */
 36207         -    assert( sqlite3_mutex_held(pGroup->mutex) );
 36208         -    pcache1LeaveMutex(pCache->pGroup);
 36209         -    sz = sizeof(PGroupBlock) + PAGECACHE_BLOCKALLOC_MINENTRY * nByte;
 36210         -    pBlock = (PGroupBlock *)sqlite3Malloc(sz);
 36211         -    pcache1EnterMutex(pCache->pGroup);
 36212         -
 36213         -    if( !pBlock ){
 36214         -      freeListIfEmpty(pGroup, pList);
 36215         -      return 0;
 36216         -    }
 36217         -    pBlock->nEntry = (sqlite3MallocSize(pBlock) - sizeof(PGroupBlock)) / nByte;
 36218         -    if( pBlock->nEntry>=BMS ){
 36219         -      pBlock->nEntry = BMS-1;
 36220         -    }
 36221         -    pBlock->pList = pList;
 36222         -    pBlock->mUsed = 0;
 36223         -    pBlock->aData = (u8 *)&pBlock[1];
 36224         -    addBlockToList(pList, pBlock);
 36225         -
 36226         -    sz = sqlite3MallocSize(pBlock);
 36227         -    sqlite3_mutex_enter(pcache1.mutex);
 36228         -    sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
 36229         -    sqlite3_mutex_leave(pcache1.mutex);
 36230         -  }
 36231         -
 36232         -  for(i=0; pPg==0 && ALWAYS(i<pBlock->nEntry); i++){
 36233         -    if( 0==(pBlock->mUsed & ((Bitmask)1<<i)) ){
 36234         -      pBlock->mUsed |= ((Bitmask)1<<i);
 36235         -      pPg = (void *)&pBlock->aData[pList->nByte * i];
 36236         -    }
 36237         -  }
 36238         -  assert( pPg );
 36239         -  PAGE_SET_BLOCKPTR(pCache, pPg, pBlock);
 36240         -
 36241         -  /* If the block is now full, shift it to the end of the list */
 36242         -  if( pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) && pList->pLast!=pBlock ){
 36243         -    assert( pList->pFirst==pBlock );
 36244         -    assert( pBlock->pPrev==0 );
 36245         -    assert( pList->pLast->pNext==0 );
 36246         -    pList->pFirst = pBlock->pNext;
 36247         -    pList->pFirst->pPrev = 0;
 36248         -    pBlock->pPrev = pList->pLast;
 36249         -    pBlock->pNext = 0;
 36250         -    pList->pLast->pNext = pBlock;
 36251         -    pList->pLast = pBlock;
 36252         -  }
 36253         -  p = PAGE_TO_PGHDR1(pCache, pPg);
 36254         -  if( pCache->bPurgeable ){
 36255         -    pCache->pGroup->nCurrentPage++;
 36256         -  }
 36257         -#else
 36258  36095     /* The group mutex must be released before pcache1Alloc() is called. This
 36259  36096     ** is because it may call sqlite3_release_memory(), which assumes that 
 36260  36097     ** this mutex is not held. */
 36261  36098     assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
 36262  36099     pcache1LeaveMutex(pCache->pGroup);
 36263  36100     pPg = pcache1Alloc(nByte);
 36264  36101     pcache1EnterMutex(pCache->pGroup);
        36102  +
 36265  36103     if( pPg ){
 36266  36104       p = PAGE_TO_PGHDR1(pCache, pPg);
 36267  36105       if( pCache->bPurgeable ){
 36268  36106         pCache->pGroup->nCurrentPage++;
 36269  36107       }
 36270         -  }else{
 36271         -    p = 0;
 36272  36108     }
 36273         -#endif
 36274  36109     return p;
 36275  36110   }
 36276  36111   
 36277  36112   /*
 36278  36113   ** Free a page object allocated by pcache1AllocPage().
 36279  36114   **
 36280  36115   ** The pointer is allowed to be NULL, which is prudent.  But it turns out
 36281  36116   ** that the current implementation happens to never call this routine
 36282  36117   ** with a NULL pointer, so we mark the NULL test with ALWAYS().
 36283  36118   */
 36284  36119   static void pcache1FreePage(PgHdr1 *p){
 36285  36120     if( ALWAYS(p) ){
 36286  36121       PCache1 *pCache = p->pCache;
 36287         -    void *pPg = PGHDR1_TO_PAGE(p);
 36288         -
 36289         -#ifdef SQLITE_PAGECACHE_BLOCKALLOC
 36290         -    PGroupBlock *pBlock = PAGE_GET_BLOCKPTR(pCache, pPg);
 36291         -    PGroupBlockList *pList = pBlock->pList;
 36292         -    int i = ((u8 *)pPg - pBlock->aData) / pList->nByte;
 36293         -
 36294         -    assert( pPg==(void *)&pBlock->aData[i*pList->nByte] );
 36295         -    assert( pBlock->mUsed & ((Bitmask)1<<i) );
 36296         -    pBlock->mUsed &= ~((Bitmask)1<<i);
 36297         -
 36298         -    /* Remove the block from the list. If it is completely empty, free it.
 36299         -    ** Or if it is not completely empty, re-insert it at the start of the
 36300         -    ** list. */
 36301         -    if( pList->pFirst==pBlock ){
 36302         -      pList->pFirst = pBlock->pNext;
 36303         -      if( pList->pFirst ) pList->pFirst->pPrev = 0;
 36304         -    }else{
 36305         -      pBlock->pPrev->pNext = pBlock->pNext;
 36306         -    }
 36307         -    if( pList->pLast==pBlock ){
 36308         -      pList->pLast = pBlock->pPrev;
 36309         -      if( pList->pLast ) pList->pLast->pNext = 0;
 36310         -    }else{
 36311         -      pBlock->pNext->pPrev = pBlock->pPrev;
 36312         -    }
 36313         -
 36314         -    if( pBlock->mUsed==0 ){
 36315         -      PGroup *pGroup = p->pCache->pGroup;
 36316         -
 36317         -      int sz = sqlite3MallocSize(pBlock);
 36318         -      sqlite3_mutex_enter(pcache1.mutex);
 36319         -      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -sz);
 36320         -      sqlite3_mutex_leave(pcache1.mutex);
 36321         -      freeListIfEmpty(pGroup, pList);
 36322         -      sqlite3_free(pBlock);
 36323         -    }else{
 36324         -      addBlockToList(pList, pBlock);
 36325         -    }
 36326         -#else
 36327  36122       assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
 36328         -    pcache1Free(pPg);
 36329         -#endif
        36123  +    pcache1Free(PGHDR1_TO_PAGE(p));
 36330  36124       if( pCache->bPurgeable ){
 36331  36125         pCache->pGroup->nCurrentPage--;
 36332  36126       }
 36333  36127     }
 36334  36128   }
 36335  36129   
 36336  36130   /*
................................................................................
 36933  36727   **
 36934  36728   ** nReq is the number of bytes of memory required. Once this much has
 36935  36729   ** been released, the function returns. The return value is the total number 
 36936  36730   ** of bytes of memory released.
 36937  36731   */
 36938  36732   SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
 36939  36733     int nFree = 0;
 36940         -#ifdef SQLITE_PAGECACHE_BLOCKALLOC
 36941         -  if( pcache1.grp.isBusy ) return 0;
 36942         -#endif
 36943  36734     assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
 36944  36735     assert( sqlite3_mutex_notheld(pcache1.mutex) );
 36945  36736     if( pcache1.pStart==0 ){
 36946  36737       PgHdr1 *p;
 36947  36738       pcache1EnterMutex(&pcache1.grp);
 36948  36739       while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
 36949  36740         nFree += pcache1MemSize(PGHDR1_TO_PAGE(p));
................................................................................
 38198  37989     int pageSize;               /* Number of bytes in a page */
 38199  37990     Pgno mxPgno;                /* Maximum allowed size of the database */
 38200  37991     i64 journalSizeLimit;       /* Size limit for persistent journal files */
 38201  37992     char *zFilename;            /* Name of the database file */
 38202  37993     char *zJournal;             /* Name of the journal file */
 38203  37994     int (*xBusyHandler)(void*); /* Function to call when busy */
 38204  37995     void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
        37996  +  int nHit, nMiss;            /* Total cache hits and misses */
 38205  37997   #ifdef SQLITE_TEST
 38206         -  int nHit, nMiss;            /* Cache hits and missing */
 38207  37998     int nRead, nWrite;          /* Database pages read/written */
 38208  37999   #endif
 38209  38000     void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
 38210  38001   #ifdef SQLITE_HAS_CODEC
 38211  38002     void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
 38212  38003     void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
 38213  38004     void (*xCodecFree)(void*);             /* Destructor for the codec */
................................................................................
 40231  40022         if( needPagerReset ){
 40232  40023           pager_reset(pPager);
 40233  40024           needPagerReset = 0;
 40234  40025         }
 40235  40026         rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
 40236  40027         if( rc!=SQLITE_OK ){
 40237  40028           if( rc==SQLITE_DONE ){
 40238         -          rc = SQLITE_OK;
 40239  40029             pPager->journalOff = szJ;
 40240  40030             break;
 40241  40031           }else if( rc==SQLITE_IOERR_SHORT_READ ){
 40242  40032             /* If the journal has been truncated, simply stop reading and
 40243  40033             ** processing the journal. This might happen if the journal was
 40244  40034             ** not completely written and synced prior to a crash.  In that
 40245  40035             ** case, the database should have never been written in the
................................................................................
 40493  40283   ){
 40494  40284     int rc;                         /* Return code */
 40495  40285   #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
 40496  40286     PgHdr *p;                       /* For looping over pages */
 40497  40287   #endif
 40498  40288   
 40499  40289     assert( pPager->pWal );
        40290  +  assert( pList );
 40500  40291   #ifdef SQLITE_DEBUG
 40501  40292     /* Verify that the page list is in accending order */
 40502  40293     for(p=pList; p && p->pDirty; p=p->pDirty){
 40503  40294       assert( p->pgno < p->pDirty->pgno );
 40504  40295     }
 40505  40296   #endif
 40506  40297   
................................................................................
 41697  41488     ** pages belonging to the same sector.
 41698  41489     **
 41699  41490     ** The doNotSpill flag inhibits all cache spilling regardless of whether
 41700  41491     ** or not a sync is required.  This is set during a rollback.
 41701  41492     **
 41702  41493     ** Spilling is also prohibited when in an error state since that could
 41703  41494     ** lead to database corruption.   In the current implementaton it 
 41704         -  ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
        41495  +  ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
 41705  41496     ** while in the error state, hence it is impossible for this routine to
 41706  41497     ** be called in the error state.  Nevertheless, we include a NEVER()
 41707  41498     ** test for the error state as a safeguard against future changes.
 41708  41499     */
 41709  41500     if( NEVER(pPager->errCode) ) return SQLITE_OK;
 41710  41501     if( pPager->doNotSpill ) return SQLITE_OK;
 41711  41502     if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
................................................................................
 42533  42324     assert( (*ppPage)->pgno==pgno );
 42534  42325     assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
 42535  42326   
 42536  42327     if( (*ppPage)->pPager && !noContent ){
 42537  42328       /* In this case the pcache already contains an initialized copy of
 42538  42329       ** the page. Return without further ado.  */
 42539  42330       assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
 42540         -    PAGER_INCR(pPager->nHit);
        42331  +    pPager->nHit++;
 42541  42332       return SQLITE_OK;
 42542  42333   
 42543  42334     }else{
 42544  42335       /* The pager cache has created a new page. Its content needs to 
 42545  42336       ** be initialized.  */
 42546  42337   
 42547         -    PAGER_INCR(pPager->nMiss);
 42548  42338       pPg = *ppPage;
 42549  42339       pPg->pPager = pPager;
 42550  42340   
 42551  42341       /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
 42552  42342       ** number greater than this, or the unused locking-page, is requested. */
 42553  42343       if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
 42554  42344         rc = SQLITE_CORRUPT_BKPT;
................................................................................
 42576  42366           testcase( rc==SQLITE_NOMEM );
 42577  42367           sqlite3EndBenignMalloc();
 42578  42368         }
 42579  42369         memset(pPg->pData, 0, pPager->pageSize);
 42580  42370         IOTRACE(("ZERO %p %d\n", pPager, pgno));
 42581  42371       }else{
 42582  42372         assert( pPg->pPager==pPager );
        42373  +      pPager->nMiss++;
 42583  42374         rc = readDbPage(pPg);
 42584  42375         if( rc!=SQLITE_OK ){
 42585  42376           goto pager_acquire_err;
 42586  42377         }
 42587  42378       }
 42588  42379       pager_set_pagehash(pPg);
 42589  42380     }
................................................................................
 43609  43400     a[7] = pPager->nMiss;
 43610  43401     a[8] = 0;  /* Used to be pPager->nOvfl */
 43611  43402     a[9] = pPager->nRead;
 43612  43403     a[10] = pPager->nWrite;
 43613  43404     return a;
 43614  43405   }
 43615  43406   #endif
        43407  +
        43408  +/*
        43409  +** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
        43410  +** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
        43411  +** current cache hit or miss count, according to the value of eStat. If the 
        43412  +** reset parameter is non-zero, the cache hit or miss count is zeroed before 
        43413  +** returning.
        43414  +*/
        43415  +SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
        43416  +  int *piStat;
        43417  +
        43418  +  assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
        43419  +       || eStat==SQLITE_DBSTATUS_CACHE_MISS
        43420  +  );
        43421  +  if( eStat==SQLITE_DBSTATUS_CACHE_HIT ){
        43422  +    piStat = &pPager->nHit;
        43423  +  }else{
        43424  +    piStat = &pPager->nMiss;
        43425  +  }
        43426  +
        43427  +  *pnVal += *piStat;
        43428  +  if( reset ){
        43429  +    *piStat = 0;
        43430  +  }
        43431  +}
 43616  43432   
 43617  43433   /*
 43618  43434   ** Return true if this is an in-memory pager.
 43619  43435   */
 43620  43436   SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
 43621  43437     return MEMDB;
 43622  43438   }
................................................................................
 44338  44154         rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
 44339  44155                              pPager->pageSize, (u8*)pPager->pTmpSpace);
 44340  44156         pPager->pWal = 0;
 44341  44157       }
 44342  44158     }
 44343  44159     return rc;
 44344  44160   }
        44161  +
        44162  +/*
        44163  +** Unless this is an in-memory or temporary database, clear the pager cache.
        44164  +*/
        44165  +SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
        44166  +  if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
        44167  +}
 44345  44168   
 44346  44169   #ifdef SQLITE_HAS_CODEC
 44347  44170   /*
 44348  44171   ** This function is called by the wal module when writing page content
 44349  44172   ** into the log file.
 44350  44173   **
 44351  44174   ** This function returns a pointer to a buffer containing the encrypted
................................................................................
 46704  46527     /* If iRead is non-zero, then it is the log frame number that contains the
 46705  46528     ** required page. Read and return data from the log file.
 46706  46529     */
 46707  46530     if( iRead ){
 46708  46531       int sz;
 46709  46532       i64 iOffset;
 46710  46533       sz = pWal->hdr.szPage;
 46711         -    sz = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
        46534  +    sz = (sz&0xfe00) + ((sz&0x0001)<<16);
 46712  46535       testcase( sz<=32768 );
 46713  46536       testcase( sz>=65536 );
 46714  46537       iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
 46715  46538       *pInWal = 1;
 46716  46539       /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
 46717  46540       return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset);
 46718  46541     }
................................................................................
 50017  49840     ** If this Btree is a candidate for shared cache, try to find an
 50018  49841     ** existing BtShared object that we can share with
 50019  49842     */
 50020  49843     if( isMemdb==0 && isTempDb==0 ){
 50021  49844       if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
 50022  49845         int nFullPathname = pVfs->mxPathname+1;
 50023  49846         char *zFullPathname = sqlite3Malloc(nFullPathname);
 50024         -      sqlite3_mutex *mutexShared;
        49847  +      MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
 50025  49848         p->sharable = 1;
 50026  49849         if( !zFullPathname ){
 50027  49850           sqlite3_free(p);
 50028  49851           return SQLITE_NOMEM;
 50029  49852         }
 50030  49853         sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
        49854  +#if SQLITE_THREADSAFE
 50031  49855         mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
 50032  49856         sqlite3_mutex_enter(mutexOpen);
 50033  49857         mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 50034  49858         sqlite3_mutex_enter(mutexShared);
        49859  +#endif
 50035  49860         for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
 50036  49861           assert( pBt->nRef>0 );
 50037  49862           if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
 50038  49863                    && sqlite3PagerVfs(pBt->pPager)==pVfs ){
 50039  49864             int iDb;
 50040  49865             for(iDb=db->nDb-1; iDb>=0; iDb--){
 50041  49866               Btree *pExisting = db->aDb[iDb].pBt;
................................................................................
 50133  49958       pBt->usableSize = pBt->pageSize - nReserve;
 50134  49959       assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
 50135  49960      
 50136  49961   #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
 50137  49962       /* Add the new BtShared object to the linked list sharable BtShareds.
 50138  49963       */
 50139  49964       if( p->sharable ){
 50140         -      sqlite3_mutex *mutexShared;
        49965  +      MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
 50141  49966         pBt->nRef = 1;
 50142         -      mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
        49967  +      MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
 50143  49968         if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
 50144  49969           pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
 50145  49970           if( pBt->mutex==0 ){
 50146  49971             rc = SQLITE_NOMEM;
 50147  49972             db->mallocFailed = 0;
 50148  49973             goto btree_open_out;
 50149  49974           }
................................................................................
 50217  50042   ** Decrement the BtShared.nRef counter.  When it reaches zero,
 50218  50043   ** remove the BtShared structure from the sharing list.  Return
 50219  50044   ** true if the BtShared.nRef counter reaches zero and return
 50220  50045   ** false if it is still positive.
 50221  50046   */
 50222  50047   static int removeFromSharingList(BtShared *pBt){
 50223  50048   #ifndef SQLITE_OMIT_SHARED_CACHE
 50224         -  sqlite3_mutex *pMaster;
        50049  +  MUTEX_LOGIC( sqlite3_mutex *pMaster; )
 50225  50050     BtShared *pList;
 50226  50051     int removed = 0;
 50227  50052   
 50228  50053     assert( sqlite3_mutex_notheld(pBt->mutex) );
 50229         -  pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
        50054  +  MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
 50230  50055     sqlite3_mutex_enter(pMaster);
 50231  50056     pBt->nRef--;
 50232  50057     if( pBt->nRef<=0 ){
 50233  50058       if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
 50234  50059         GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
 50235  50060       }else{
 50236  50061         pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
................................................................................
 52189  52014   #endif
 52190  52015             rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
 52191  52016           offset -= ovflSize;
 52192  52017         }else{
 52193  52018           /* Need to read this page properly. It contains some of the
 52194  52019           ** range of data that is being read (eOp==0) or written (eOp!=0).
 52195  52020           */
 52196         -        DbPage *pDbPage;
        52021  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
        52022  +        sqlite3_file *fd;
        52023  +#endif
 52197  52024           int a = amt;
 52198         -        rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
 52199         -        if( rc==SQLITE_OK ){
 52200         -          aPayload = sqlite3PagerGetData(pDbPage);
 52201         -          nextPage = get4byte(aPayload);
 52202         -          if( a + offset > ovflSize ){
 52203         -            a = ovflSize - offset;
 52204         -          }
 52205         -          rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
 52206         -          sqlite3PagerUnref(pDbPage);
 52207         -          offset = 0;
 52208         -          amt -= a;
 52209         -          pBuf += a;
 52210         -        }
        52025  +        if( a + offset > ovflSize ){
        52026  +          a = ovflSize - offset;
        52027  +        }
        52028  +
        52029  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
        52030  +        /* If all the following are true:
        52031  +        **
        52032  +        **   1) this is a read operation, and 
        52033  +        **   2) data is required from the start of this overflow page, and
        52034  +        **   3) the database is file-backed, and
        52035  +        **   4) there is no open write-transaction, and
        52036  +        **   5) the database is not a WAL database,
        52037  +        **
        52038  +        ** then data can be read directly from the database file into the
        52039  +        ** output buffer, bypassing the page-cache altogether. This speeds
        52040  +        ** up loading large records that span many overflow pages.
        52041  +        */
        52042  +        if( eOp==0                                             /* (1) */
        52043  +         && offset==0                                          /* (2) */
        52044  +         && pBt->inTransaction==TRANS_READ                     /* (4) */
        52045  +         && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
        52046  +         && pBt->pPage1->aData[19]==0x01                       /* (5) */
        52047  +        ){
        52048  +          u8 aSave[4];
        52049  +          u8 *aWrite = &pBuf[-4];
        52050  +          memcpy(aSave, aWrite, 4);
        52051  +          rc = sqlite3OsRead(fd, aWrite, a+4, pBt->pageSize * (nextPage-1));
        52052  +          nextPage = get4byte(aWrite);
        52053  +          memcpy(aWrite, aSave, 4);
        52054  +        }else
        52055  +#endif
        52056  +
        52057  +        {
        52058  +          DbPage *pDbPage;
        52059  +          rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
        52060  +          if( rc==SQLITE_OK ){
        52061  +            aPayload = sqlite3PagerGetData(pDbPage);
        52062  +            nextPage = get4byte(aPayload);
        52063  +            rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
        52064  +            sqlite3PagerUnref(pDbPage);
        52065  +            offset = 0;
        52066  +          }
        52067  +        }
        52068  +        amt -= a;
        52069  +        pBuf += a;
 52211  52070         }
 52212  52071       }
 52213  52072     }
 52214  52073   
 52215  52074     if( rc==SQLITE_OK && amt>0 ){
 52216  52075       return SQLITE_CORRUPT_BKPT;
 52217  52076     }
................................................................................
 52802  52661             c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
 52803  52662             sqlite3_free(pCellKey);
 52804  52663           }
 52805  52664         }
 52806  52665         if( c==0 ){
 52807  52666           if( pPage->intKey && !pPage->leaf ){
 52808  52667             lwr = idx;
 52809         -          upr = lwr - 1;
 52810  52668             break;
 52811  52669           }else{
 52812  52670             *pRes = 0;
 52813  52671             rc = SQLITE_OK;
 52814  52672             goto moveto_finish;
 52815  52673           }
 52816  52674         }
................................................................................
 52820  52678           upr = idx-1;
 52821  52679         }
 52822  52680         if( lwr>upr ){
 52823  52681           break;
 52824  52682         }
 52825  52683         pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2);
 52826  52684       }
 52827         -    assert( lwr==upr+1 );
        52685  +    assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
 52828  52686       assert( pPage->isInit );
 52829  52687       if( pPage->leaf ){
 52830  52688         chldPg = 0;
 52831  52689       }else if( lwr>=pPage->nCell ){
 52832  52690         chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 52833  52691       }else{
 52834  52692         chldPg = get4byte(findCell(pPage, lwr));
................................................................................
 53085  52943         }else{
 53086  52944           rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
 53087  52945         }
 53088  52946         if( rc ){
 53089  52947           pTrunk = 0;
 53090  52948           goto end_allocate_page;
 53091  52949         }
        52950  +      assert( pTrunk!=0 );
        52951  +      assert( pTrunk->aData!=0 );
 53092  52952   
 53093  52953         k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
 53094  52954         if( k==0 && !searchList ){
 53095  52955           /* The trunk has no leaves and the list is not being searched. 
 53096  52956           ** So extract the trunk page itself and use it as the newly 
 53097  52957           ** allocated page */
 53098  52958           assert( pPrevTrunk==0 );
................................................................................
 54212  54072         /* Drop the cell from the parent page. apDiv[i] still points to
 54213  54073         ** the cell within the parent, even though it has been dropped.
 54214  54074         ** This is safe because dropping a cell only overwrites the first
 54215  54075         ** four bytes of it, and this function does not need the first
 54216  54076         ** four bytes of the divider cell. So the pointer is safe to use
 54217  54077         ** later on.  
 54218  54078         **
 54219         -      ** Unless SQLite is compiled in secure-delete mode. In this case,
        54079  +      ** But not if we are in secure-delete mode. In secure-delete mode,
 54220  54080         ** the dropCell() routine will overwrite the entire cell with zeroes.
 54221  54081         ** In this case, temporarily copy the cell into the aOvflSpace[]
 54222  54082         ** buffer. It will be copied out again as soon as the aSpace[] buffer
 54223  54083         ** is allocated.  */
 54224  54084         if( pBt->secureDelete ){
 54225         -        int iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
        54085  +        int iOff;
        54086  +
        54087  +        iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
 54226  54088           if( (iOff+szNew[i])>(int)pBt->usableSize ){
 54227  54089             rc = SQLITE_CORRUPT_BKPT;
 54228  54090             memset(apOld, 0, (i+1)*sizeof(MemPage*));
 54229  54091             goto balance_cleanup;
 54230  54092           }else{
 54231  54093             memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
 54232  54094             apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
................................................................................
 54638  54500       k = 0;                             /* Current 'new' sibling page */
 54639  54501       for(i=0; i<nCell; i++){
 54640  54502         int isDivider = 0;
 54641  54503         while( i==iNextOld ){
 54642  54504           /* Cell i is the cell immediately following the last cell on old
 54643  54505           ** sibling page j. If the siblings are not leaf pages of an
 54644  54506           ** intkey b-tree, then cell i was a divider cell. */
        54507  +        assert( j+1 < ArraySize(apCopy) );
 54645  54508           pOld = apCopy[++j];
 54646  54509           iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
 54647  54510           if( pOld->nOverflow ){
 54648  54511             nOverflow = pOld->nOverflow;
 54649  54512             iOverflow = i + !leafData + pOld->aOvfl[0].idx;
 54650  54513           }
 54651  54514           isDivider = !leafData;  
................................................................................
 56980  56843   }
 56981  56844   
 56982  56845   /*
 56983  56846   ** Release all resources associated with an sqlite3_backup* handle.
 56984  56847   */
 56985  56848   SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
 56986  56849     sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
 56987         -  sqlite3_mutex *mutex;                /* Mutex to protect source database */
        56850  +  MUTEX_LOGIC( sqlite3_mutex *mutex; ) /* Mutex to protect source database */
 56988  56851     int rc;                              /* Value to return */
 56989  56852   
 56990  56853     /* Enter the mutexes */
 56991  56854     if( p==0 ) return SQLITE_OK;
 56992  56855     sqlite3_mutex_enter(p->pSrcDb->mutex);
 56993  56856     sqlite3BtreeEnter(p->pSrc);
 56994         -  mutex = p->pSrcDb->mutex;
        56857  +  MUTEX_LOGIC( mutex = p->pSrcDb->mutex; )
 56995  56858     if( p->pDestDb ){
 56996  56859       sqlite3_mutex_enter(p->pDestDb->mutex);
 56997  56860     }
 56998  56861   
 56999  56862     /* Detach this backup from the source pager. */
 57000  56863     if( p->pDestDb ){
 57001  56864       p->pSrc->nBackup--;
................................................................................
 57106  56969   **
 57107  56970   ** The size of file pTo may be reduced by this operation. If anything 
 57108  56971   ** goes wrong, the transaction on pTo is rolled back. If successful, the 
 57109  56972   ** transaction is committed before returning.
 57110  56973   */
 57111  56974   SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
 57112  56975     int rc;
        56976  +  sqlite3_file *pFd;              /* File descriptor for database pTo */
 57113  56977     sqlite3_backup b;
 57114  56978     sqlite3BtreeEnter(pTo);
 57115  56979     sqlite3BtreeEnter(pFrom);
        56980  +
        56981  +  assert( sqlite3BtreeIsInTrans(pTo) );
        56982  +  pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
        56983  +  if( pFd->pMethods ){
        56984  +    i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
        56985  +    sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
        56986  +  }
 57116  56987   
 57117  56988     /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
 57118  56989     ** to 0. This is used by the implementations of sqlite3_backup_step()
 57119  56990     ** and sqlite3_backup_finish() to detect that they are being called
 57120  56991     ** from this function, not directly by the user.
 57121  56992     */
 57122  56993     memset(&b, 0, sizeof(b));
................................................................................
 57133  57004     ** or an error code.
 57134  57005     */
 57135  57006     sqlite3_backup_step(&b, 0x7FFFFFFF);
 57136  57007     assert( b.rc!=SQLITE_OK );
 57137  57008     rc = sqlite3_backup_finish(&b);
 57138  57009     if( rc==SQLITE_OK ){
 57139  57010       pTo->pBt->pageSizeFixed = 0;
        57011  +  }else{
        57012  +    sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
 57140  57013     }
 57141  57014   
        57015  +  assert( sqlite3BtreeIsInTrans(pTo)==0 );
 57142  57016     sqlite3BtreeLeave(pFrom);
 57143  57017     sqlite3BtreeLeave(pTo);
 57144  57018     return rc;
 57145  57019   }
 57146  57020   #endif /* SQLITE_OMIT_VACUUM */
 57147  57021   
 57148  57022   /************** End of backup.c **********************************************/
................................................................................
 58169  58043   
 58170  58044     if( !pExpr ){
 58171  58045       *ppVal = 0;
 58172  58046       return SQLITE_OK;
 58173  58047     }
 58174  58048     op = pExpr->op;
 58175  58049   
 58176         -  /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2.
        58050  +  /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
 58177  58051     ** The ifdef here is to enable us to achieve 100% branch test coverage even
 58178         -  ** when SQLITE_ENABLE_STAT2 is omitted.
        58052  +  ** when SQLITE_ENABLE_STAT3 is omitted.
 58179  58053     */
 58180         -#ifdef SQLITE_ENABLE_STAT2
        58054  +#ifdef SQLITE_ENABLE_STAT3
 58181  58055     if( op==TK_REGISTER ) op = pExpr->op2;
 58182  58056   #else
 58183  58057     if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
 58184  58058   #endif
 58185  58059   
 58186  58060     /* Handle negative integers in a single step.  This is needed in the
 58187  58061     ** case when the value is -9223372036854775808.
................................................................................
 58872  58746   }
 58873  58747   
 58874  58748   /*
 58875  58749   ** Change the P2 operand of instruction addr so that it points to
 58876  58750   ** the address of the next instruction to be coded.
 58877  58751   */
 58878  58752   SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
 58879         -  assert( addr>=0 );
 58880         -  sqlite3VdbeChangeP2(p, addr, p->nOp);
        58753  +  assert( addr>=0 || p->db->mallocFailed );
        58754  +  if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
 58881  58755   }
 58882  58756   
 58883  58757   
 58884  58758   /*
 58885  58759   ** If the input FuncDef structure is ephemeral, then free it.  If
 58886  58760   ** the FuncDef is not ephermal, then do nothing.
 58887  58761   */
................................................................................
 59078  58952   #ifndef NDEBUG
 59079  58953   /*
 59080  58954   ** Change the comment on the the most recently coded instruction.  Or
 59081  58955   ** insert a No-op and add the comment to that new instruction.  This
 59082  58956   ** makes the code easier to read during debugging.  None of this happens
 59083  58957   ** in a production build.
 59084  58958   */
 59085         -SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
 59086         -  va_list ap;
 59087         -  if( !p ) return;
        58959  +static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
 59088  58960     assert( p->nOp>0 || p->aOp==0 );
 59089  58961     assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
 59090  58962     if( p->nOp ){
 59091         -    char **pz = &p->aOp[p->nOp-1].zComment;
        58963  +    assert( p->aOp );
        58964  +    sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
        58965  +    p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
        58966  +  }
        58967  +}
        58968  +SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
        58969  +  va_list ap;
        58970  +  if( p ){
 59092  58971       va_start(ap, zFormat);
 59093         -    sqlite3DbFree(p->db, *pz);
 59094         -    *pz = sqlite3VMPrintf(p->db, zFormat, ap);
        58972  +    vdbeVComment(p, zFormat, ap);
 59095  58973       va_end(ap);
 59096  58974     }
 59097  58975   }
 59098  58976   SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
 59099  58977     va_list ap;
 59100         -  if( !p ) return;
 59101         -  sqlite3VdbeAddOp0(p, OP_Noop);
 59102         -  assert( p->nOp>0 || p->aOp==0 );
 59103         -  assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
 59104         -  if( p->nOp ){
 59105         -    char **pz = &p->aOp[p->nOp-1].zComment;
        58978  +  if( p ){
        58979  +    sqlite3VdbeAddOp0(p, OP_Noop);
 59106  58980       va_start(ap, zFormat);
 59107         -    sqlite3DbFree(p->db, *pz);
 59108         -    *pz = sqlite3VMPrintf(p->db, zFormat, ap);
        58981  +    vdbeVComment(p, zFormat, ap);
 59109  58982       va_end(ap);
 59110  58983     }
 59111  58984   }
 59112  58985   #endif  /* NDEBUG */
 59113  58986   
 59114  58987   /*
 59115  58988   ** Return the opcode for a given address.  If the address is -1, then
................................................................................
 59439  59312     int nRow;                            /* Stop when row count reaches this */
 59440  59313     int nSub = 0;                        /* Number of sub-vdbes seen so far */
 59441  59314     SubProgram **apSub = 0;              /* Array of sub-vdbes */
 59442  59315     Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
 59443  59316     sqlite3 *db = p->db;                 /* The database connection */
 59444  59317     int i;                               /* Loop counter */
 59445  59318     int rc = SQLITE_OK;                  /* Return code */
 59446         -  Mem *pMem = p->pResultSet = &p->aMem[1];  /* First Mem of result set */
        59319  +  Mem *pMem = &p->aMem[1];             /* First Mem of result set */
 59447  59320   
 59448  59321     assert( p->explain );
 59449  59322     assert( p->magic==VDBE_MAGIC_RUN );
 59450  59323     assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
 59451  59324   
 59452  59325     /* Even though this opcode does not use dynamic strings for
 59453  59326     ** the result, result columns may become dynamic if the user calls
 59454  59327     ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
 59455  59328     */
 59456  59329     releaseMemArray(pMem, 8);
        59330  +  p->pResultSet = 0;
 59457  59331   
 59458  59332     if( p->rc==SQLITE_NOMEM ){
 59459  59333       /* This happens if a malloc() inside a call to sqlite3_column_text() or
 59460  59334       ** sqlite3_column_text16() failed.  */
 59461  59335       db->mallocFailed = 1;
 59462  59336       return SQLITE_ERROR;
 59463  59337     }
................................................................................
 59604  59478         {
 59605  59479           pMem->flags = MEM_Null;                       /* Comment */
 59606  59480           pMem->type = SQLITE_NULL;
 59607  59481         }
 59608  59482       }
 59609  59483   
 59610  59484       p->nResColumn = 8 - 4*(p->explain-1);
        59485  +    p->pResultSet = &p->aMem[1];
 59611  59486       p->rc = SQLITE_OK;
 59612  59487       rc = SQLITE_ROW;
 59613  59488     }
 59614  59489     return rc;
 59615  59490   }
 59616  59491   #endif /* SQLITE_OMIT_EXPLAIN */
 59617  59492   
................................................................................
 60605  60480   /*
 60606  60481   ** Each VDBE holds the result of the most recent sqlite3_step() call
 60607  60482   ** in p->rc.  This routine sets that result back to SQLITE_OK.
 60608  60483   */
 60609  60484   SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
 60610  60485     p->rc = SQLITE_OK;
 60611  60486   }
        60487  +
        60488  +/*
        60489  +** Copy the error code and error message belonging to the VDBE passed
        60490  +** as the first argument to its database handle (so that they will be 
        60491  +** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
        60492  +**
        60493  +** This function does not clear the VDBE error code or message, just
        60494  +** copies them to the database handle.
        60495  +*/
        60496  +SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
        60497  +  sqlite3 *db = p->db;
        60498  +  int rc = p->rc;
        60499  +  if( p->zErrMsg ){
        60500  +    u8 mallocFailed = db->mallocFailed;
        60501  +    sqlite3BeginBenignMalloc();
        60502  +    sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
        60503  +    sqlite3EndBenignMalloc();
        60504  +    db->mallocFailed = mallocFailed;
        60505  +    db->errCode = rc;
        60506  +  }else{
        60507  +    sqlite3Error(db, rc, 0);
        60508  +  }
        60509  +  return rc;
        60510  +}
 60612  60511   
 60613  60512   /*
 60614  60513   ** Clean up a VDBE after execution but do not delete the VDBE just yet.
 60615  60514   ** Write any error messages into *pzErrMsg.  Return the result code.
 60616  60515   **
 60617  60516   ** After this routine is run, the VDBE should be ready to be executed
 60618  60517   ** again.
................................................................................
 60633  60532   
 60634  60533     /* If the VDBE has be run even partially, then transfer the error code
 60635  60534     ** and error message from the VDBE into the main database structure.  But
 60636  60535     ** if the VDBE has just been set to run but has not actually executed any
 60637  60536     ** instructions yet, leave the main database error information unchanged.
 60638  60537     */
 60639  60538     if( p->pc>=0 ){
 60640         -    if( p->zErrMsg ){
 60641         -      sqlite3BeginBenignMalloc();
 60642         -      sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT);
 60643         -      sqlite3EndBenignMalloc();
 60644         -      db->errCode = p->rc;
 60645         -      sqlite3DbFree(db, p->zErrMsg);
 60646         -      p->zErrMsg = 0;
 60647         -    }else if( p->rc ){
 60648         -      sqlite3Error(db, p->rc, 0);
 60649         -    }else{
 60650         -      sqlite3Error(db, SQLITE_OK, 0);
 60651         -    }
        60539  +    sqlite3VdbeTransferError(p);
        60540  +    sqlite3DbFree(db, p->zErrMsg);
        60541  +    p->zErrMsg = 0;
 60652  60542       if( p->runOnlyOnce ) p->expired = 1;
 60653  60543     }else if( p->rc && p->expired ){
 60654  60544       /* The expired flag was set on the VDBE before the first call
 60655  60545       ** to sqlite3_step(). For consistency (since sqlite3_step() was
 60656  60546       ** called), set the database error in this case as well.
 60657  60547       */
 60658  60548       sqlite3Error(db, p->rc, 0);
................................................................................
 61359  61249   
 61360  61250     /* Get the size of the index entry.  Only indices entries of less
 61361  61251     ** than 2GiB are support - anything large must be database corruption.
 61362  61252     ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
 61363  61253     ** this code can safely assume that nCellKey is 32-bits  
 61364  61254     */
 61365  61255     assert( sqlite3BtreeCursorIsValid(pCur) );
 61366         -  rc = sqlite3BtreeKeySize(pCur, &nCellKey);
        61256  +  VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
 61367  61257     assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
 61368  61258     assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
 61369  61259   
 61370  61260     /* Read in the complete content of the index entry */
 61371  61261     memset(&m, 0, sizeof(m));
 61372  61262     rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
 61373  61263     if( rc ){
................................................................................
 61434  61324   ){
 61435  61325     i64 nCellKey = 0;
 61436  61326     int rc;
 61437  61327     BtCursor *pCur = pC->pCursor;
 61438  61328     Mem m;
 61439  61329   
 61440  61330     assert( sqlite3BtreeCursorIsValid(pCur) );
 61441         -  rc = sqlite3BtreeKeySize(pCur, &nCellKey);
        61331  +  VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
 61442  61332     assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
 61443  61333     /* nCellKey will always be between 0 and 0xffffffff because of the say
 61444  61334     ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
 61445  61335     if( nCellKey<=0 || nCellKey>0x7fffffff ){
 61446  61336       *res = 0;
 61447  61337       return SQLITE_CORRUPT_BKPT;
 61448  61338     }
................................................................................
 61990  61880     );
 61991  61881     assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
 61992  61882     if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
 61993  61883       /* If this statement was prepared using sqlite3_prepare_v2(), and an
 61994  61884       ** error has occured, then return the error code in p->rc to the
 61995  61885       ** caller. Set the error code in the database handle to the same value.
 61996  61886       */ 
 61997         -    rc = db->errCode = p->rc;
        61887  +    rc = sqlite3VdbeTransferError(p);
 61998  61888     }
 61999  61889     return (rc&db->errMask);
 62000  61890   }
 62001  61891   
 62002  61892   /*
 62003  61893   ** The maximum number of times that a statement will try to reparse
 62004  61894   ** itself before giving up and returning SQLITE_SCHEMA.
................................................................................
 65710  65600       if( u.am.pC->nullRow ){
 65711  65601         u.am.payloadSize = 0;
 65712  65602       }else if( u.am.pC->cacheStatus==p->cacheCtr ){
 65713  65603         u.am.payloadSize = u.am.pC->payloadSize;
 65714  65604         u.am.zRec = (char*)u.am.pC->aRow;
 65715  65605       }else if( u.am.pC->isIndex ){
 65716  65606         assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
 65717         -      rc = sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64);
        65607  +      VVA_ONLY(rc =) sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64);
 65718  65608         assert( rc==SQLITE_OK );   /* True because of CursorMoveto() call above */
 65719  65609         /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
 65720  65610         ** payload size, so it is impossible for u.am.payloadSize64 to be
 65721  65611         ** larger than 32 bits. */
 65722  65612         assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 );
 65723  65613         u.am.payloadSize = (u32)u.am.payloadSize64;
 65724  65614       }else{
 65725  65615         assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
 65726         -      rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
        65616  +      VVA_ONLY(rc =) sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
 65727  65617         assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
 65728  65618       }
 65729  65619     }else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){
 65730  65620       u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
 65731  65621       assert( u.am.pReg->flags & MEM_Blob );
 65732  65622       assert( memIsValid(u.am.pReg) );
 65733  65623       u.am.payloadSize = u.am.pReg->n;
................................................................................
 67771  67661     */
 67772  67662     assert( u.bk.pC->deferredMoveto==0 );
 67773  67663     rc = sqlite3VdbeCursorMoveto(u.bk.pC);
 67774  67664     if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
 67775  67665   
 67776  67666     if( u.bk.pC->isIndex ){
 67777  67667       assert( !u.bk.pC->isTable );
 67778         -    rc = sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64);
        67668  +    VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64);
 67779  67669       assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
 67780  67670       if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 67781  67671         goto too_big;
 67782  67672       }
 67783  67673       u.bk.n = (u32)u.bk.n64;
 67784  67674     }else{
 67785         -    rc = sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n);
        67675  +    VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n);
 67786  67676       assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
 67787  67677       if( u.bk.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 67788  67678         goto too_big;
 67789  67679       }
 67790  67680     }
 67791  67681     if( sqlite3VdbeMemGrow(pOut, u.bk.n, 0) ){
 67792  67682       goto no_mem;
................................................................................
 67890  67780     int res;
 67891  67781   #endif /* local variables moved into u.bn */
 67892  67782   
 67893  67783     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67894  67784     u.bn.pC = p->apCsr[pOp->p1];
 67895  67785     assert( u.bn.pC!=0 );
 67896  67786     u.bn.pCrsr = u.bn.pC->pCursor;
 67897         -  if( NEVER(u.bn.pCrsr==0) ){
 67898         -    u.bn.res = 1;
 67899         -  }else{
        67787  +  u.bn.res = 0;
        67788  +  if( ALWAYS(u.bn.pCrsr!=0) ){
 67900  67789       rc = sqlite3BtreeLast(u.bn.pCrsr, &u.bn.res);
 67901  67790     }
 67902  67791     u.bn.pC->nullRow = (u8)u.bn.res;
 67903  67792     u.bn.pC->deferredMoveto = 0;
 67904  67793     u.bn.pC->rowidIsValid = 0;
 67905  67794     u.bn.pC->cacheStatus = CACHE_STALE;
 67906  67795     if( pOp->p2>0 && u.bn.res ){
................................................................................
 69093  68982   #ifndef SQLITE_OMIT_WAL
 69094  68983     u.ch.zFilename = sqlite3PagerFilename(u.ch.pPager);
 69095  68984   
 69096  68985     /* Do not allow a transition to journal_mode=WAL for a database
 69097  68986     ** in temporary storage or if the VFS does not support shared memory
 69098  68987     */
 69099  68988     if( u.ch.eNew==PAGER_JOURNALMODE_WAL
 69100         -   && (u.ch.zFilename[0]==0                         /* Temp file */
        68989  +   && (sqlite3Strlen30(u.ch.zFilename)==0           /* Temp file */
 69101  68990          || !sqlite3PagerWalSupported(u.ch.pPager))   /* No shared-memory support */
 69102  68991     ){
 69103  68992       u.ch.eNew = u.ch.eOld;
 69104  68993     }
 69105  68994   
 69106  68995     if( (u.ch.eNew!=u.ch.eOld)
 69107  68996      && (u.ch.eOld==PAGER_JOURNALMODE_WAL || u.ch.eNew==PAGER_JOURNALMODE_WAL)
................................................................................
 69528  69417   
 69529  69418     u.co.pVtab = pOp->p4.pVtab->pVtab;
 69530  69419     u.co.pName = &aMem[pOp->p1];
 69531  69420     assert( u.co.pVtab->pModule->xRename );
 69532  69421     assert( memIsValid(u.co.pName) );
 69533  69422     REGISTER_TRACE(pOp->p1, u.co.pName);
 69534  69423     assert( u.co.pName->flags & MEM_Str );
 69535         -  rc = u.co.pVtab->pModule->xRename(u.co.pVtab, u.co.pName->z);
 69536         -  importVtabErrMsg(p, u.co.pVtab);
 69537         -  p->expired = 0;
 69538         -
        69424  +  testcase( u.co.pName->enc==SQLITE_UTF8 );
        69425  +  testcase( u.co.pName->enc==SQLITE_UTF16BE );
        69426  +  testcase( u.co.pName->enc==SQLITE_UTF16LE );
        69427  +  rc = sqlite3VdbeChangeEncoding(u.co.pName, SQLITE_UTF8);
        69428  +  if( rc==SQLITE_OK ){
        69429  +    rc = u.co.pVtab->pModule->xRename(u.co.pVtab, u.co.pName->z);
        69430  +    importVtabErrMsg(p, u.co.pVtab);
        69431  +    p->expired = 0;
        69432  +  }
 69539  69433     break;
 69540  69434   }
 69541  69435   #endif
 69542  69436   
 69543  69437   #ifndef SQLITE_OMIT_VIRTUALTABLE
 69544  69438   /* Opcode: VUpdate P1 P2 P3 P4 *
 69545  69439   **
................................................................................
 71889  71783     ** allowing it to be repopulated by the memcpy() on the following line.
 71890  71784     */
 71891  71785     ExprSetProperty(pExpr, EP_Static);
 71892  71786     sqlite3ExprDelete(db, pExpr);
 71893  71787     memcpy(pExpr, pDup, sizeof(*pExpr));
 71894  71788     sqlite3DbFree(db, pDup);
 71895  71789   }
        71790  +
        71791  +
        71792  +/*
        71793  +** Return TRUE if the name zCol occurs anywhere in the USING clause.
        71794  +**
        71795  +** Return FALSE if the USING clause is NULL or if it does not contain
        71796  +** zCol.
        71797  +*/
        71798  +static int nameInUsingClause(IdList *pUsing, const char *zCol){
        71799  +  if( pUsing ){
        71800  +    int k;
        71801  +    for(k=0; k<pUsing->nId; k++){
        71802  +      if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
        71803  +    }
        71804  +  }
        71805  +  return 0;
        71806  +}
        71807  +
 71896  71808   
 71897  71809   /*
 71898  71810   ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
 71899  71811   ** that name in the set of source tables in pSrcList and make the pExpr 
 71900  71812   ** expression node refer back to that source column.  The following changes
 71901  71813   ** are made to pExpr:
 71902  71814   **
................................................................................
 71981  71893             pExpr->iTable = pItem->iCursor;
 71982  71894             pExpr->pTab = pTab;
 71983  71895             pSchema = pTab->pSchema;
 71984  71896             pMatch = pItem;
 71985  71897           }
 71986  71898           for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
 71987  71899             if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
 71988         -            IdList *pUsing;
        71900  +            /* If there has been exactly one prior match and this match
        71901  +            ** is for the right-hand table of a NATURAL JOIN or is in a 
        71902  +            ** USING clause, then skip this match.
        71903  +            */
        71904  +            if( cnt==1 ){
        71905  +              if( pItem->jointype & JT_NATURAL ) continue;
        71906  +              if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
        71907  +            }
 71989  71908               cnt++;
 71990  71909               pExpr->iTable = pItem->iCursor;
 71991  71910               pExpr->pTab = pTab;
 71992  71911               pMatch = pItem;
 71993  71912               pSchema = pTab->pSchema;
 71994  71913               /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
 71995  71914               pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
 71996         -            if( i<pSrcList->nSrc-1 ){
 71997         -              if( pItem[1].jointype & JT_NATURAL ){
 71998         -                /* If this match occurred in the left table of a natural join,
 71999         -                ** then skip the right table to avoid a duplicate match */
 72000         -                pItem++;
 72001         -                i++;
 72002         -              }else if( (pUsing = pItem[1].pUsing)!=0 ){
 72003         -                /* If this match occurs on a column that is in the USING clause
 72004         -                ** of a join, skip the search of the right table of the join
 72005         -                ** to avoid a duplicate match there. */
 72006         -                int k;
 72007         -                for(k=0; k<pUsing->nId; k++){
 72008         -                  if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
 72009         -                    pItem++;
 72010         -                    i++;
 72011         -                    break;
 72012         -                  }
 72013         -                }
 72014         -              }
 72015         -            }
 72016  71915               break;
 72017  71916             }
 72018  71917           }
 72019  71918         }
 72020  71919       }
 72021  71920   
 72022  71921   #ifndef SQLITE_OMIT_TRIGGER
................................................................................
 73415  73314       if( pToken ){
 73416  73315         if( nExtra==0 ){
 73417  73316           pNew->flags |= EP_IntValue;
 73418  73317           pNew->u.iValue = iValue;
 73419  73318         }else{
 73420  73319           int c;
 73421  73320           pNew->u.zToken = (char*)&pNew[1];
 73422         -        memcpy(pNew->u.zToken, pToken->z, pToken->n);
        73321  +        assert( pToken->z!=0 || pToken->n==0 );
        73322  +        if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
 73423  73323           pNew->u.zToken[pToken->n] = 0;
 73424  73324           if( dequote && nExtra>=3 
 73425  73325                && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
 73426  73326             sqlite3Dequote(pNew->u.zToken);
 73427  73327             if( c=='"' ) pNew->flags |= EP_DblQuoted;
 73428  73328           }
 73429  73329         }
................................................................................
 74454  74354     /* Check to see if an existing table or index can be used to
 74455  74355     ** satisfy the query.  This is preferable to generating a new 
 74456  74356     ** ephemeral table.
 74457  74357     */
 74458  74358     p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
 74459  74359     if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
 74460  74360       sqlite3 *db = pParse->db;              /* Database connection */
 74461         -    Expr *pExpr = p->pEList->a[0].pExpr;   /* Expression <column> */
 74462         -    int iCol = pExpr->iColumn;             /* Index of column <column> */
 74463  74361       Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
 74464         -    Table *pTab = p->pSrc->a[0].pTab;      /* Table <table>. */
        74362  +    Table *pTab;                           /* Table <table>. */
        74363  +    Expr *pExpr;                           /* Expression <column> */
        74364  +    int iCol;                              /* Index of column <column> */
 74465  74365       int iDb;                               /* Database idx for pTab */
        74366  +
        74367  +    assert( p );                        /* Because of isCandidateForInOpt(p) */
        74368  +    assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
        74369  +    assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
        74370  +    assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
        74371  +    pTab = p->pSrc->a[0].pTab;
        74372  +    pExpr = p->pEList->a[0].pExpr;
        74373  +    iCol = pExpr->iColumn;
 74466  74374      
 74467  74375       /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
 74468  74376       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 74469  74377       sqlite3CodeVerifySchema(pParse, iDb);
 74470  74378       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 74471  74379   
 74472  74380       /* This function is only called from two places. In both cases the vdbe
................................................................................
 76465  76373     if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
 76466  76374     if( ExprHasProperty(pA, EP_IntValue) ){
 76467  76375       if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
 76468  76376         return 2;
 76469  76377       }
 76470  76378     }else if( pA->op!=TK_COLUMN && pA->u.zToken ){
 76471  76379       if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
 76472         -    if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ){
        76380  +    if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
 76473  76381         return 2;
 76474  76382       }
 76475  76383     }
 76476  76384     if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
 76477  76385     if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
 76478  76386     return 0;
 76479  76387   }
................................................................................
 77608  77516   **
 77609  77517   **    May you do good and not evil.
 77610  77518   **    May you find forgiveness for yourself and forgive others.
 77611  77519   **    May you share freely, never taking more than you give.
 77612  77520   **
 77613  77521   *************************************************************************
 77614  77522   ** This file contains code associated with the ANALYZE command.
        77523  +**
        77524  +** The ANALYZE command gather statistics about the content of tables
        77525  +** and indices.  These statistics are made available to the query planner
        77526  +** to help it make better decisions about how to perform queries.
        77527  +**
        77528  +** The following system tables are or have been supported:
        77529  +**
        77530  +**    CREATE TABLE sqlite_stat1(tbl, idx, stat);
        77531  +**    CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
        77532  +**    CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
        77533  +**
        77534  +** Additional tables might be added in future releases of SQLite.
        77535  +** The sqlite_stat2 table is not created or used unless the SQLite version
        77536  +** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
        77537  +** with SQLITE_ENABLE_STAT2.  The sqlite_stat2 table is deprecated.
        77538  +** The sqlite_stat2 table is superceded by sqlite_stat3, which is only
        77539  +** created and used by SQLite versions 3.7.9 and later and with
        77540  +** SQLITE_ENABLE_STAT3 defined.  The fucntionality of sqlite_stat3
        77541  +** is a superset of sqlite_stat2.  
        77542  +**
        77543  +** Format of sqlite_stat1:
        77544  +**
        77545  +** There is normally one row per index, with the index identified by the
        77546  +** name in the idx column.  The tbl column is the name of the table to
        77547  +** which the index belongs.  In each such row, the stat column will be
        77548  +** a string consisting of a list of integers.  The first integer in this
        77549  +** list is the number of rows in the index and in the table.  The second
        77550  +** integer is the average number of rows in the index that have the same
        77551  +** value in the first column of the index.  The third integer is the average
        77552  +** number of rows in the index that have the same value for the first two
        77553  +** columns.  The N-th integer (for N>1) is the average number of rows in 
        77554  +** the index which have the same value for the first N-1 columns.  For
        77555  +** a K-column index, there will be K+1 integers in the stat column.  If
        77556  +** the index is unique, then the last integer will be 1.
        77557  +**
        77558  +** The list of integers in the stat column can optionally be followed
        77559  +** by the keyword "unordered".  The "unordered" keyword, if it is present,
        77560  +** must be separated from the last integer by a single space.  If the
        77561  +** "unordered" keyword is present, then the query planner assumes that
        77562  +** the index is unordered and will not use the index for a range query.
        77563  +** 
        77564  +** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
        77565  +** column contains a single integer which is the (estimated) number of
        77566  +** rows in the table identified by sqlite_stat1.tbl.
        77567  +**
        77568  +** Format of sqlite_stat2:
        77569  +**
        77570  +** The sqlite_stat2 is only created and is only used if SQLite is compiled
        77571  +** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
        77572  +** 3.6.18 and 3.7.8.  The "stat2" table contains additional information
        77573  +** about the distribution of keys within an index.  The index is identified by
        77574  +** the "idx" column and the "tbl" column is the name of the table to which
        77575  +** the index belongs.  There are usually 10 rows in the sqlite_stat2
        77576  +** table for each index.
        77577  +**
        77578  +** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
        77579  +** inclusive are samples of the left-most key value in the index taken at
        77580  +** evenly spaced points along the index.  Let the number of samples be S
        77581  +** (10 in the standard build) and let C be the number of rows in the index.
        77582  +** Then the sampled rows are given by:
        77583  +**
        77584  +**     rownumber = (i*C*2 + C)/(S*2)
        77585  +**
        77586  +** For i between 0 and S-1.  Conceptually, the index space is divided into
        77587  +** S uniform buckets and the samples are the middle row from each bucket.
        77588  +**
        77589  +** The format for sqlite_stat2 is recorded here for legacy reference.  This
        77590  +** version of SQLite does not support sqlite_stat2.  It neither reads nor
        77591  +** writes the sqlite_stat2 table.  This version of SQLite only supports
        77592  +** sqlite_stat3.
        77593  +**
        77594  +** Format for sqlite_stat3:
        77595  +**
        77596  +** The sqlite_stat3 is an enhancement to sqlite_stat2.  A new name is
        77597  +** used to avoid compatibility problems.  
        77598  +**
        77599  +** The format of the sqlite_stat3 table is similar to the format of
        77600  +** the sqlite_stat2 table.  There are multiple entries for each index.
        77601  +** The idx column names the index and the tbl column is the table of the
        77602  +** index.  If the idx and tbl columns are the same, then the sample is
        77603  +** of the INTEGER PRIMARY KEY.  The sample column is a value taken from
        77604  +** the left-most column of the index.  The nEq column is the approximate
        77605  +** number of entires in the index whose left-most column exactly matches
        77606  +** the sample.  nLt is the approximate number of entires whose left-most
        77607  +** column is less than the sample.  The nDLt column is the approximate
        77608  +** number of distinct left-most entries in the index that are less than
        77609  +** the sample.
        77610  +**
        77611  +** Future versions of SQLite might change to store a string containing
        77612  +** multiple integers values in the nDLt column of sqlite_stat3.  The first
        77613  +** integer will be the number of prior index entires that are distinct in
        77614  +** the left-most column.  The second integer will be the number of prior index
        77615  +** entries that are distinct in the first two columns.  The third integer
        77616  +** will be the number of prior index entries that are distinct in the first
        77617  +** three columns.  And so forth.  With that extension, the nDLt field is
        77618  +** similar in function to the sqlite_stat1.stat field.
        77619  +**
        77620  +** There can be an arbitrary number of sqlite_stat3 entries per index.
        77621  +** The ANALYZE command will typically generate sqlite_stat3 tables
        77622  +** that contain between 10 and 40 samples which are distributed across
        77623  +** the key space, though not uniformly, and which include samples with
        77624  +** largest possible nEq values.
 77615  77625   */
 77616  77626   #ifndef SQLITE_OMIT_ANALYZE
 77617  77627   
 77618  77628   /*
 77619  77629   ** This routine generates code that opens the sqlite_stat1 table for
 77620  77630   ** writing with cursor iStatCur. If the library was built with the
 77621         -** SQLITE_ENABLE_STAT2 macro defined, then the sqlite_stat2 table is
        77631  +** SQLITE_ENABLE_STAT3 macro defined, then the sqlite_stat3 table is
 77622  77632   ** opened for writing using cursor (iStatCur+1)
 77623  77633   **
 77624  77634   ** If the sqlite_stat1 tables does not previously exist, it is created.
 77625         -** Similarly, if the sqlite_stat2 table does not exist and the library
 77626         -** is compiled with SQLITE_ENABLE_STAT2 defined, it is created. 
        77635  +** Similarly, if the sqlite_stat3 table does not exist and the library
        77636  +** is compiled with SQLITE_ENABLE_STAT3 defined, it is created. 
 77627  77637   **
 77628  77638   ** Argument zWhere may be a pointer to a buffer containing a table name,
 77629  77639   ** or it may be a NULL pointer. If it is not NULL, then all entries in
 77630         -** the sqlite_stat1 and (if applicable) sqlite_stat2 tables associated
        77640  +** the sqlite_stat1 and (if applicable) sqlite_stat3 tables associated
 77631  77641   ** with the named table are deleted. If zWhere==0, then code is generated
 77632  77642   ** to delete all stat table entries.
 77633  77643   */
 77634  77644   static void openStatTable(
 77635  77645     Parse *pParse,          /* Parsing context */
 77636  77646     int iDb,                /* The database we are looking in */
 77637  77647     int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
................................................................................
 77639  77649     const char *zWhereType  /* Either "tbl" or "idx" */
 77640  77650   ){
 77641  77651     static const struct {
 77642  77652       const char *zName;
 77643  77653       const char *zCols;
 77644  77654     } aTable[] = {
 77645  77655       { "sqlite_stat1", "tbl,idx,stat" },
 77646         -#ifdef SQLITE_ENABLE_STAT2
 77647         -    { "sqlite_stat2", "tbl,idx,sampleno,sample" },
        77656  +#ifdef SQLITE_ENABLE_STAT3
        77657  +    { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
 77648  77658   #endif
 77649  77659     };
 77650  77660   
 77651  77661     int aRoot[] = {0, 0};
 77652  77662     u8 aCreateTbl[] = {0, 0};
 77653  77663   
 77654  77664     int i;
................................................................................
 77656  77666     Db *pDb;
 77657  77667     Vdbe *v = sqlite3GetVdbe(pParse);
 77658  77668     if( v==0 ) return;
 77659  77669     assert( sqlite3BtreeHoldsAllMutexes(db) );
 77660  77670     assert( sqlite3VdbeDb(v)==db );
 77661  77671     pDb = &db->aDb[iDb];
 77662  77672   
        77673  +  /* Create new statistic tables if they do not exist, or clear them
        77674  +  ** if they do already exist.
        77675  +  */
 77663  77676     for(i=0; i<ArraySize(aTable); i++){
 77664  77677       const char *zTab = aTable[i].zName;
 77665  77678       Table *pStat;
 77666  77679       if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
 77667  77680         /* The sqlite_stat[12] table does not exist. Create it. Note that a 
 77668  77681         ** side-effect of the CREATE TABLE statement is to leave the rootpage 
 77669  77682         ** of the new table in register pParse->regRoot. This is important 
................................................................................
 77686  77699         }else{
 77687  77700           /* The sqlite_stat[12] table already exists.  Delete all rows. */
 77688  77701           sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
 77689  77702         }
 77690  77703       }
 77691  77704     }
 77692  77705   
 77693         -  /* Open the sqlite_stat[12] tables for writing. */
        77706  +  /* Open the sqlite_stat[13] tables for writing. */
 77694  77707     for(i=0; i<ArraySize(aTable); i++){
 77695  77708       sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
 77696  77709       sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
 77697  77710       sqlite3VdbeChangeP5(v, aCreateTbl[i]);
 77698  77711     }
 77699  77712   }
        77713  +
        77714  +/*
        77715  +** Recommended number of samples for sqlite_stat3
        77716  +*/
        77717  +#ifndef SQLITE_STAT3_SAMPLES
        77718  +# define SQLITE_STAT3_SAMPLES 24
        77719  +#endif
        77720  +
        77721  +/*
        77722  +** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() -
        77723  +** share an instance of the following structure to hold their state
        77724  +** information.
        77725  +*/
        77726  +typedef struct Stat3Accum Stat3Accum;
        77727  +struct Stat3Accum {
        77728  +  tRowcnt nRow;             /* Number of rows in the entire table */
        77729  +  tRowcnt nPSample;         /* How often to do a periodic sample */
        77730  +  int iMin;                 /* Index of entry with minimum nEq and hash */
        77731  +  int mxSample;             /* Maximum number of samples to accumulate */
        77732  +  int nSample;              /* Current number of samples */
        77733  +  u32 iPrn;                 /* Pseudo-random number used for sampling */
        77734  +  struct Stat3Sample {
        77735  +    i64 iRowid;                /* Rowid in main table of the key */
        77736  +    tRowcnt nEq;               /* sqlite_stat3.nEq */
        77737  +    tRowcnt nLt;               /* sqlite_stat3.nLt */
        77738  +    tRowcnt nDLt;              /* sqlite_stat3.nDLt */
        77739  +    u8 isPSample;              /* True if a periodic sample */
        77740  +    u32 iHash;                 /* Tiebreaker hash */
        77741  +  } *a;                     /* An array of samples */
        77742  +};
        77743  +
        77744  +#ifdef SQLITE_ENABLE_STAT3
        77745  +/*
        77746  +** Implementation of the stat3_init(C,S) SQL function.  The two parameters
        77747  +** are the number of rows in the table or index (C) and the number of samples
        77748  +** to accumulate (S).
        77749  +**
        77750  +** This routine allocates the Stat3Accum object.
        77751  +**
        77752  +** The return value is the Stat3Accum object (P).
        77753  +*/
        77754  +static void stat3Init(
        77755  +  sqlite3_context *context,
        77756  +  int argc,
        77757  +  sqlite3_value **argv
        77758  +){
        77759  +  Stat3Accum *p;
        77760  +  tRowcnt nRow;
        77761  +  int mxSample;
        77762  +  int n;
        77763  +
        77764  +  UNUSED_PARAMETER(argc);
        77765  +  nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
        77766  +  mxSample = sqlite3_value_int(argv[1]);
        77767  +  n = sizeof(*p) + sizeof(p->a[0])*mxSample;
        77768  +  p = sqlite3_malloc( n );
        77769  +  if( p==0 ){
        77770  +    sqlite3_result_error_nomem(context);
        77771  +    return;
        77772  +  }
        77773  +  memset(p, 0, n);
        77774  +  p->a = (struct Stat3Sample*)&p[1];
        77775  +  p->nRow = nRow;
        77776  +  p->mxSample = mxSample;
        77777  +  p->nPSample = p->nRow/(mxSample/3+1) + 1;
        77778  +  sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
        77779  +  sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
        77780  +}
        77781  +static const FuncDef stat3InitFuncdef = {
        77782  +  2,                /* nArg */
        77783  +  SQLITE_UTF8,      /* iPrefEnc */
        77784  +  0,                /* flags */
        77785  +  0,                /* pUserData */
        77786  +  0,                /* pNext */
        77787  +  stat3Init,        /* xFunc */
        77788  +  0,                /* xStep */
        77789  +  0,                /* xFinalize */
        77790  +  "stat3_init",     /* zName */
        77791  +  0,                /* pHash */
        77792  +  0                 /* pDestructor */
        77793  +};
        77794  +
        77795  +
        77796  +/*
        77797  +** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function.  The
        77798  +** arguments describe a single key instance.  This routine makes the 
        77799  +** decision about whether or not to retain this key for the sqlite_stat3
        77800  +** table.
        77801  +**
        77802  +** The return value is NULL.
        77803  +*/
        77804  +static void stat3Push(
        77805  +  sqlite3_context *context,
        77806  +  int argc,
        77807  +  sqlite3_value **argv
        77808  +){
        77809  +  Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]);
        77810  +  tRowcnt nEq = sqlite3_value_int64(argv[0]);
        77811  +  tRowcnt nLt = sqlite3_value_int64(argv[1]);
        77812  +  tRowcnt nDLt = sqlite3_value_int64(argv[2]);
        77813  +  i64 rowid = sqlite3_value_int64(argv[3]);
        77814  +  u8 isPSample = 0;
        77815  +  u8 doInsert = 0;
        77816  +  int iMin = p->iMin;
        77817  +  struct Stat3Sample *pSample;
        77818  +  int i;
        77819  +  u32 h;
        77820  +
        77821  +  UNUSED_PARAMETER(context);
        77822  +  UNUSED_PARAMETER(argc);
        77823  +  if( nEq==0 ) return;
        77824  +  h = p->iPrn = p->iPrn*1103515245 + 12345;
        77825  +  if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){
        77826  +    doInsert = isPSample = 1;
        77827  +  }else if( p->nSample<p->mxSample ){
        77828  +    doInsert = 1;
        77829  +  }else{
        77830  +    if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){
        77831  +      doInsert = 1;
        77832  +    }
        77833  +  }
        77834  +  if( !doInsert ) return;
        77835  +  if( p->nSample==p->mxSample ){
        77836  +    assert( p->nSample - iMin - 1 >= 0 );
        77837  +    memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1));
        77838  +    pSample = &p->a[p->nSample-1];
        77839  +  }else{
        77840  +    pSample = &p->a[p->nSample++];
        77841  +  }
        77842  +  pSample->iRowid = rowid;
        77843  +  pSample->nEq = nEq;
        77844  +  pSample->nLt = nLt;
        77845  +  pSample->nDLt = nDLt;
        77846  +  pSample->iHash = h;
        77847  +  pSample->isPSample = isPSample;
        77848  +
        77849  +  /* Find the new minimum */
        77850  +  if( p->nSample==p->mxSample ){
        77851  +    pSample = p->a;
        77852  +    i = 0;
        77853  +    while( pSample->isPSample ){
        77854  +      i++;
        77855  +      pSample++;
        77856  +      assert( i<p->nSample );
        77857  +    }
        77858  +    nEq = pSample->nEq;
        77859  +    h = pSample->iHash;
        77860  +    iMin = i;
        77861  +    for(i++, pSample++; i<p->nSample; i++, pSample++){
        77862  +      if( pSample->isPSample ) continue;
        77863  +      if( pSample->nEq<nEq
        77864  +       || (pSample->nEq==nEq && pSample->iHash<h)
        77865  +      ){
        77866  +        iMin = i;
        77867  +        nEq = pSample->nEq;
        77868  +        h = pSample->iHash;
        77869  +      }
        77870  +    }
        77871  +    p->iMin = iMin;
        77872  +  }
        77873  +}
        77874  +static const FuncDef stat3PushFuncdef = {
        77875  +  5,                /* nArg */
        77876  +  SQLITE_UTF8,      /* iPrefEnc */
        77877  +  0,                /* flags */
        77878  +  0,                /* pUserData */
        77879  +  0,                /* pNext */
        77880  +  stat3Push,        /* xFunc */
        77881  +  0,                /* xStep */
        77882  +  0,                /* xFinalize */
        77883  +  "stat3_push",     /* zName */
        77884  +  0,                /* pHash */
        77885  +  0                 /* pDestructor */
        77886  +};
        77887  +
        77888  +/*
        77889  +** Implementation of the stat3_get(P,N,...) SQL function.  This routine is
        77890  +** used to query the results.  Content is returned for the Nth sqlite_stat3
        77891  +** row where N is between 0 and S-1 and S is the number of samples.  The
        77892  +** value returned depends on the number of arguments.
        77893  +**
        77894  +**   argc==2    result:  rowid
        77895  +**   argc==3    result:  nEq
        77896  +**   argc==4    result:  nLt
        77897  +**   argc==5    result:  nDLt
        77898  +*/
        77899  +static void stat3Get(
        77900  +  sqlite3_context *context,
        77901  +  int argc,
        77902  +  sqlite3_value **argv
        77903  +){
        77904  +  int n = sqlite3_value_int(argv[1]);
        77905  +  Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]);
        77906  +
        77907  +  assert( p!=0 );
        77908  +  if( p->nSample<=n ) return;
        77909  +  switch( argc ){
        77910  +    case 2:  sqlite3_result_int64(context, p->a[n].iRowid); break;
        77911  +    case 3:  sqlite3_result_int64(context, p->a[n].nEq);    break;
        77912  +    case 4:  sqlite3_result_int64(context, p->a[n].nLt);    break;
        77913  +    default: sqlite3_result_int64(context, p->a[n].nDLt);   break;
        77914  +  }
        77915  +}
        77916  +static const FuncDef stat3GetFuncdef = {
        77917  +  -1,               /* nArg */
        77918  +  SQLITE_UTF8,      /* iPrefEnc */
        77919  +  0,                /* flags */
        77920  +  0,                /* pUserData */
        77921  +  0,                /* pNext */
        77922  +  stat3Get,         /* xFunc */
        77923  +  0,                /* xStep */
        77924  +  0,                /* xFinalize */
        77925  +  "stat3_get",     /* zName */
        77926  +  0,                /* pHash */
        77927  +  0                 /* pDestructor */
        77928  +};
        77929  +#endif /* SQLITE_ENABLE_STAT3 */
        77930  +
        77931  +
        77932  +
 77700  77933   
 77701  77934   /*
 77702  77935   ** Generate code to do an analysis of all indices associated with
 77703  77936   ** a single table.
 77704  77937   */
 77705  77938   static void analyzeOneTable(
 77706  77939     Parse *pParse,   /* Parser context */
................................................................................
 77716  77949     int i;                       /* Loop counter */
 77717  77950     int topOfLoop;               /* The top of the loop */
 77718  77951     int endOfLoop;               /* The end of the loop */
 77719  77952     int jZeroRows = -1;          /* Jump from here if number of rows is zero */
 77720  77953     int iDb;                     /* Index of database containing pTab */
 77721  77954     int regTabname = iMem++;     /* Register containing table name */
 77722  77955     int regIdxname = iMem++;     /* Register containing index name */
 77723         -  int regSampleno = iMem++;    /* Register containing next sample number */
 77724         -  int regCol = iMem++;         /* Content of a column analyzed table */
        77956  +  int regStat1 = iMem++;       /* The stat column of sqlite_stat1 */
        77957  +#ifdef SQLITE_ENABLE_STAT3
        77958  +  int regNumEq = regStat1;     /* Number of instances.  Same as regStat1 */
        77959  +  int regNumLt = iMem++;       /* Number of keys less than regSample */
        77960  +  int regNumDLt = iMem++;      /* Number of distinct keys less than regSample */
        77961  +  int regSample = iMem++;      /* The next sample value */
        77962  +  int regRowid = regSample;    /* Rowid of a sample */
        77963  +  int regAccum = iMem++;       /* Register to hold Stat3Accum object */
        77964  +  int regLoop = iMem++;        /* Loop counter */
        77965  +  int regCount = iMem++;       /* Number of rows in the table or index */
        77966  +  int regTemp1 = iMem++;       /* Intermediate register */
        77967  +  int regTemp2 = iMem++;       /* Intermediate register */
        77968  +  int once = 1;                /* One-time initialization */
        77969  +  int shortJump = 0;           /* Instruction address */
        77970  +  int iTabCur = pParse->nTab++; /* Table cursor */
        77971  +#endif
        77972  +  int regCol = iMem++;         /* Content of a column in analyzed table */
 77725  77973     int regRec = iMem++;         /* Register holding completed record */
 77726  77974     int regTemp = iMem++;        /* Temporary use register */
 77727         -  int regRowid = iMem++;       /* Rowid for the inserted record */
        77975  +  int regNewRowid = iMem++;    /* Rowid for the inserted record */
 77728  77976   
 77729         -#ifdef SQLITE_ENABLE_STAT2
 77730         -  int addr = 0;                /* Instruction address */
 77731         -  int regTemp2 = iMem++;       /* Temporary use register */
 77732         -  int regSamplerecno = iMem++; /* Index of next sample to record */
 77733         -  int regRecno = iMem++;       /* Current sample index */
 77734         -  int regLast = iMem++;        /* Index of last sample to record */
 77735         -  int regFirst = iMem++;       /* Index of first sample to record */
 77736         -#endif
 77737  77977   
 77738  77978     v = sqlite3GetVdbe(pParse);
 77739  77979     if( v==0 || NEVER(pTab==0) ){
 77740  77980       return;
 77741  77981     }
 77742  77982     if( pTab->tnum==0 ){
 77743  77983       /* Do not gather statistics on views or virtual tables */
................................................................................
 77762  78002     sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 77763  78003   
 77764  78004     iIdxCur = pParse->nTab++;
 77765  78005     sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
 77766  78006     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 77767  78007       int nCol;
 77768  78008       KeyInfo *pKey;
        78009  +    int addrIfNot = 0;           /* address of OP_IfNot */
        78010  +    int *aChngAddr;              /* Array of jump instruction addresses */
 77769  78011   
 77770  78012       if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
        78013  +    VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
 77771  78014       nCol = pIdx->nColumn;
        78015  +    aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol);
        78016  +    if( aChngAddr==0 ) continue;
 77772  78017       pKey = sqlite3IndexKeyinfo(pParse, pIdx);
 77773  78018       if( iMem+1+(nCol*2)>pParse->nMem ){
 77774  78019         pParse->nMem = iMem+1+(nCol*2);
 77775  78020       }
 77776  78021   
 77777  78022       /* Open a cursor to the index to be analyzed. */
 77778  78023       assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
................................................................................
 77779  78024       sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
 77780  78025           (char *)pKey, P4_KEYINFO_HANDOFF);
 77781  78026       VdbeComment((v, "%s", pIdx->zName));
 77782  78027   
 77783  78028       /* Populate the register containing the index name. */
 77784  78029       sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
 77785  78030   
 77786         -#ifdef SQLITE_ENABLE_STAT2
 77787         -
 77788         -    /* If this iteration of the loop is generating code to analyze the
 77789         -    ** first index in the pTab->pIndex list, then register regLast has
 77790         -    ** not been populated. In this case populate it now.  */
 77791         -    if( pTab->pIndex==pIdx ){
 77792         -      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
 77793         -      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
 77794         -      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
 77795         -
 77796         -      sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
 77797         -      sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
 77798         -      addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
 77799         -      sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
 77800         -      sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
 77801         -      sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
 77802         -      sqlite3VdbeAddOp3(v, OP_Divide,  regTemp2, regLast, regLast);
 77803         -      sqlite3VdbeJumpHere(v, addr);
        78031  +#ifdef SQLITE_ENABLE_STAT3
        78032  +    if( once ){
        78033  +      once = 0;
        78034  +      sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
 77804  78035       }
 77805         -
 77806         -    /* Zero the regSampleno and regRecno registers. */
 77807         -    sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
 77808         -    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
 77809         -    sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
 77810         -#endif
        78036  +    sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
        78037  +    sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
        78038  +    sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
        78039  +    sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
        78040  +    sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
        78041  +    sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
        78042  +                      (char*)&stat3InitFuncdef, P4_FUNCDEF);
        78043  +    sqlite3VdbeChangeP5(v, 2);
        78044  +#endif /* SQLITE_ENABLE_STAT3 */
 77811  78045   
 77812  78046       /* The block of memory cells initialized here is used as follows.
 77813  78047       **
 77814  78048       **    iMem:                
 77815  78049       **        The total number of rows in the table.
 77816  78050       **
 77817  78051       **    iMem+1 .. iMem+nCol: 
................................................................................
 77833  78067       }
 77834  78068   
 77835  78069       /* Start the analysis loop. This loop runs through all the entries in
 77836  78070       ** the index b-tree.  */
 77837  78071       endOfLoop = sqlite3VdbeMakeLabel(v);
 77838  78072       sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
 77839  78073       topOfLoop = sqlite3VdbeCurrentAddr(v);
 77840         -    sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
        78074  +    sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);  /* Increment row counter */
 77841  78075   
 77842  78076       for(i=0; i<nCol; i++){
 77843  78077         CollSeq *pColl;
 77844  78078         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
 77845  78079         if( i==0 ){
 77846         -#ifdef SQLITE_ENABLE_STAT2
 77847         -        /* Check if the record that cursor iIdxCur points to contains a
 77848         -        ** value that should be stored in the sqlite_stat2 table. If so,
 77849         -        ** store it.  */
 77850         -        int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
 77851         -        assert( regTabname+1==regIdxname 
 77852         -             && regTabname+2==regSampleno
 77853         -             && regTabname+3==regCol
 77854         -        );
 77855         -        sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
 77856         -        sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
 77857         -        sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
 77858         -        sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
 77859         -
 77860         -        /* Calculate new values for regSamplerecno and regSampleno.
 77861         -        **
 77862         -        **   sampleno = sampleno + 1
 77863         -        **   samplerecno = samplerecno+(remaining records)/(remaining samples)
 77864         -        */
 77865         -        sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
 77866         -        sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
 77867         -        sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
 77868         -        sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
 77869         -        sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
 77870         -        sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
 77871         -        sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
 77872         -
 77873         -        sqlite3VdbeJumpHere(v, ne);
 77874         -        sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
 77875         -#endif
 77876         -
 77877  78080           /* Always record the very first row */
 77878         -        sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
        78081  +        addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
 77879  78082         }
 77880  78083         assert( pIdx->azColl!=0 );
 77881  78084         assert( pIdx->azColl[i]!=0 );
 77882  78085         pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
 77883         -      sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
 77884         -                       (char*)pColl, P4_COLLSEQ);
        78086  +      aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
        78087  +                                      (char*)pColl, P4_COLLSEQ);
 77885  78088         sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
 77886         -    }
 77887         -    if( db->mallocFailed ){
 77888         -      /* If a malloc failure has occurred, then the result of the expression 
 77889         -      ** passed as the second argument to the call to sqlite3VdbeJumpHere() 
 77890         -      ** below may be negative. Which causes an assert() to fail (or an
 77891         -      ** out-of-bounds write if SQLITE_DEBUG is not defined).  */
 77892         -      return;
        78089  +      VdbeComment((v, "jump if column %d changed", i));
        78090  +#ifdef SQLITE_ENABLE_STAT3
        78091  +      if( i==0 ){
        78092  +        sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
        78093  +        VdbeComment((v, "incr repeat count"));
        78094  +      }
        78095  +#endif
 77893  78096       }
 77894  78097       sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
 77895  78098       for(i=0; i<nCol; i++){
 77896         -      int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
        78099  +      sqlite3VdbeJumpHere(v, aChngAddr[i]);  /* Set jump dest for the OP_Ne */
 77897  78100         if( i==0 ){
 77898         -        sqlite3VdbeJumpHere(v, addr2-1);  /* Set jump dest for the OP_IfNot */
        78101  +        sqlite3VdbeJumpHere(v, addrIfNot);   /* Jump dest for OP_IfNot */
        78102  +#ifdef SQLITE_ENABLE_STAT3
        78103  +        sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
        78104  +                          (char*)&stat3PushFuncdef, P4_FUNCDEF);
        78105  +        sqlite3VdbeChangeP5(v, 5);
        78106  +        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
        78107  +        sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
        78108  +        sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
        78109  +        sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
        78110  +#endif        
 77899  78111         }
 77900         -      sqlite3VdbeJumpHere(v, addr2);      /* Set jump dest for the OP_Ne */
 77901  78112         sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
 77902  78113         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
 77903  78114       }
        78115  +    sqlite3DbFree(db, aChngAddr);
 77904  78116   
 77905         -    /* End of the analysis loop. */
        78117  +    /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
 77906  78118       sqlite3VdbeResolveLabel(v, endOfLoop);
        78119  +
 77907  78120       sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
 77908  78121       sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
        78122  +#ifdef SQLITE_ENABLE_STAT3
        78123  +    sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
        78124  +                      (char*)&stat3PushFuncdef, P4_FUNCDEF);
        78125  +    sqlite3VdbeChangeP5(v, 5);
        78126  +    sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop);
        78127  +    shortJump = 
        78128  +    sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1);
        78129  +    sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1,
        78130  +                      (char*)&stat3GetFuncdef, P4_FUNCDEF);
        78131  +    sqlite3VdbeChangeP5(v, 2);
        78132  +    sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1);
        78133  +    sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1);
        78134  +    sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample);
        78135  +    sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample);
        78136  +    sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq,
        78137  +                      (char*)&stat3GetFuncdef, P4_FUNCDEF);
        78138  +    sqlite3VdbeChangeP5(v, 3);
        78139  +    sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt,
        78140  +                      (char*)&stat3GetFuncdef, P4_FUNCDEF);
        78141  +    sqlite3VdbeChangeP5(v, 4);
        78142  +    sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt,
        78143  +                      (char*)&stat3GetFuncdef, P4_FUNCDEF);
        78144  +    sqlite3VdbeChangeP5(v, 5);
        78145  +    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0);
        78146  +    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
        78147  +    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid);
        78148  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump);
        78149  +    sqlite3VdbeJumpHere(v, shortJump+2);
        78150  +#endif        
 77909  78151   
 77910  78152       /* Store the results in sqlite_stat1.
 77911  78153       **
 77912  78154       ** The result is a single row of the sqlite_stat1 table.  The first
 77913  78155       ** two columns are the names of the table and index.  The third column
 77914  78156       ** is a string composed of a list of integer statistics about the
 77915  78157       ** index.  The first integer in the list is the total number of entries
................................................................................
 77921  78163       **
 77922  78164       **        I = (K+D-1)/D
 77923  78165       **
 77924  78166       ** If K==0 then no entry is made into the sqlite_stat1 table.  
 77925  78167       ** If K>0 then it is always the case the D>0 so division by zero
 77926  78168       ** is never possible.
 77927  78169       */
 77928         -    sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
        78170  +    sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
 77929  78171       if( jZeroRows<0 ){
 77930  78172         jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
 77931  78173       }
 77932  78174       for(i=0; i<nCol; i++){
 77933  78175         sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
 77934         -      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
        78176  +      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
 77935  78177         sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
 77936  78178         sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
 77937  78179         sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
 77938  78180         sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
 77939         -      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
        78181  +      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
 77940  78182       }
 77941  78183       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
 77942         -    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
 77943         -    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
        78184  +    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
        78185  +    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
 77944  78186       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 77945  78187     }
 77946  78188   
 77947  78189     /* If the table has no indices, create a single sqlite_stat1 entry
 77948  78190     ** containing NULL as the index name and the row count as the content.
 77949  78191     */
 77950  78192     if( pTab->pIndex==0 ){
 77951  78193       sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
 77952  78194       VdbeComment((v, "%s", pTab->zName));
 77953         -    sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
        78195  +    sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
 77954  78196       sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
 77955         -    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
        78197  +    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
 77956  78198     }else{
 77957  78199       sqlite3VdbeJumpHere(v, jZeroRows);
 77958  78200       jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
 77959  78201     }
 77960  78202     sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
 77961  78203     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
 77962         -  sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
 77963         -  sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
        78204  +  sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
        78205  +  sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
 77964  78206     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 77965  78207     if( pParse->nMem<regRec ) pParse->nMem = regRec;
 77966  78208     sqlite3VdbeJumpHere(v, jZeroRows);
 77967  78209   }
        78210  +
 77968  78211   
 77969  78212   /*
 77970  78213   ** Generate code that will cause the most recent index analysis to
 77971  78214   ** be loaded into internal hash tables where is can be used.
 77972  78215   */
 77973  78216   static void loadAnalysis(Parse *pParse, int iDb){
 77974  78217     Vdbe *v = sqlite3GetVdbe(pParse);
................................................................................
 77985  78228     Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
 77986  78229     HashElem *k;
 77987  78230     int iStatCur;
 77988  78231     int iMem;
 77989  78232   
 77990  78233     sqlite3BeginWriteOperation(pParse, 0, iDb);
 77991  78234     iStatCur = pParse->nTab;
 77992         -  pParse->nTab += 2;
        78235  +  pParse->nTab += 3;
 77993  78236     openStatTable(pParse, iDb, iStatCur, 0, 0);
 77994  78237     iMem = pParse->nMem+1;
 77995  78238     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 77996  78239     for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
 77997  78240       Table *pTab = (Table*)sqliteHashData(k);
 77998  78241       analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
 77999  78242     }
................................................................................
 78010  78253     int iStatCur;
 78011  78254   
 78012  78255     assert( pTab!=0 );
 78013  78256     assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 78014  78257     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 78015  78258     sqlite3BeginWriteOperation(pParse, 0, iDb);
 78016  78259     iStatCur = pParse->nTab;
 78017         -  pParse->nTab += 2;
        78260  +  pParse->nTab += 3;
 78018  78261     if( pOnlyIdx ){
 78019  78262       openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
 78020  78263     }else{
 78021  78264       openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
 78022  78265     }
 78023  78266     analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
 78024  78267     loadAnalysis(pParse, iDb);
................................................................................
 78115  78358   ** the table.
 78116  78359   */
 78117  78360   static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
 78118  78361     analysisInfo *pInfo = (analysisInfo*)pData;
 78119  78362     Index *pIndex;
 78120  78363     Table *pTable;
 78121  78364     int i, c, n;
 78122         -  unsigned int v;
        78365  +  tRowcnt v;
 78123  78366     const char *z;
 78124  78367   
 78125  78368     assert( argc==3 );
 78126  78369     UNUSED_PARAMETER2(NotUsed, argc);
 78127  78370   
 78128  78371     if( argv==0 || argv[0]==0 || argv[2]==0 ){
 78129  78372       return 0;
................................................................................
 78158  78401   }
 78159  78402   
 78160  78403   /*
 78161  78404   ** If the Index.aSample variable is not NULL, delete the aSample[] array
 78162  78405   ** and its contents.
 78163  78406   */
 78164  78407   SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
 78165         -#ifdef SQLITE_ENABLE_STAT2
        78408  +#ifdef SQLITE_ENABLE_STAT3
 78166  78409     if( pIdx->aSample ){
 78167  78410       int j;
 78168         -    for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
        78411  +    for(j=0; j<pIdx->nSample; j++){
 78169  78412         IndexSample *p = &pIdx->aSample[j];
 78170  78413         if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
 78171  78414           sqlite3DbFree(db, p->u.z);
 78172  78415         }
 78173  78416       }
 78174  78417       sqlite3DbFree(db, pIdx->aSample);
        78418  +  }
        78419  +  if( db && db->pnBytesFreed==0 ){
        78420  +    pIdx->nSample = 0;
        78421  +    pIdx->aSample = 0;
 78175  78422     }
 78176  78423   #else
 78177  78424     UNUSED_PARAMETER(db);
 78178  78425     UNUSED_PARAMETER(pIdx);
 78179  78426   #endif
 78180  78427   }
 78181  78428   
        78429  +#ifdef SQLITE_ENABLE_STAT3
 78182  78430   /*
 78183         -** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
        78431  +** Load content from the sqlite_stat3 table into the Index.aSample[]
        78432  +** arrays of all indices.
        78433  +*/
        78434  +static int loadStat3(sqlite3 *db, const char *zDb){
        78435  +  int rc;                       /* Result codes from subroutines */
        78436  +  sqlite3_stmt *pStmt = 0;      /* An SQL statement being run */
        78437  +  char *zSql;                   /* Text of the SQL statement */
        78438  +  Index *pPrevIdx = 0;          /* Previous index in the loop */
        78439  +  int idx = 0;                  /* slot in pIdx->aSample[] for next sample */
        78440  +  int eType;                    /* Datatype of a sample */
        78441  +  IndexSample *pSample;         /* A slot in pIdx->aSample[] */
        78442  +
        78443  +  if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
        78444  +    return SQLITE_OK;
        78445  +  }
        78446  +
        78447  +  zSql = sqlite3MPrintf(db, 
        78448  +      "SELECT idx,count(*) FROM %Q.sqlite_stat3"
        78449  +      " GROUP BY idx", zDb);
        78450  +  if( !zSql ){
        78451  +    return SQLITE_NOMEM;
        78452  +  }
        78453  +  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
        78454  +  sqlite3DbFree(db, zSql);
        78455  +  if( rc ) return rc;
        78456  +
        78457  +  while( sqlite3_step(pStmt)==SQLITE_ROW ){
        78458  +    char *zIndex;   /* Index name */
        78459  +    Index *pIdx;    /* Pointer to the index object */
        78460  +    int nSample;    /* Number of samples */
        78461  +
        78462  +    zIndex = (char *)sqlite3_column_text(pStmt, 0);
        78463  +    if( zIndex==0 ) continue;
        78464  +    nSample = sqlite3_column_int(pStmt, 1);
        78465  +    pIdx = sqlite3FindIndex(db, zIndex, zDb);
        78466  +    if( pIdx==0 ) continue;
        78467  +    assert( pIdx->nSample==0 );
        78468  +    pIdx->nSample = nSample;
        78469  +    pIdx->aSample = sqlite3MallocZero( nSample*sizeof(IndexSample) );
        78470  +    pIdx->avgEq = pIdx->aiRowEst[1];
        78471  +    if( pIdx->aSample==0 ){
        78472  +      db->mallocFailed = 1;
        78473  +      sqlite3_finalize(pStmt);
        78474  +      return SQLITE_NOMEM;
        78475  +    }
        78476  +  }
        78477  +  rc = sqlite3_finalize(pStmt);
        78478  +  if( rc ) return rc;
        78479  +
        78480  +  zSql = sqlite3MPrintf(db, 
        78481  +      "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
        78482  +  if( !zSql ){
        78483  +    return SQLITE_NOMEM;
        78484  +  }
        78485  +  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
        78486  +  sqlite3DbFree(db, zSql);
        78487  +  if( rc ) return rc;
        78488  +
        78489  +  while( sqlite3_step(pStmt)==SQLITE_ROW ){
        78490  +    char *zIndex;   /* Index name */
        78491  +    Index *pIdx;    /* Pointer to the index object */
        78492  +    int i;          /* Loop counter */
        78493  +    tRowcnt sumEq;  /* Sum of the nEq values */
        78494  +
        78495  +    zIndex = (char *)sqlite3_column_text(pStmt, 0);
        78496  +    if( zIndex==0 ) continue;
        78497  +    pIdx = sqlite3FindIndex(db, zIndex, zDb);
        78498  +    if( pIdx==0 ) continue;
        78499  +    if( pIdx==pPrevIdx ){
        78500  +      idx++;
        78501  +    }else{
        78502  +      pPrevIdx = pIdx;
        78503  +      idx = 0;
        78504  +    }
        78505  +    assert( idx<pIdx->nSample );
        78506  +    pSample = &pIdx->aSample[idx];
        78507  +    pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
        78508  +    pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
        78509  +    pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
        78510  +    if( idx==pIdx->nSample-1 ){
        78511  +      if( pSample->nDLt>0 ){
        78512  +        for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
        78513  +        pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
        78514  +      }
        78515  +      if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
        78516  +    }
        78517  +    eType = sqlite3_column_type(pStmt, 4);
        78518  +    pSample->eType = (u8)eType;
        78519  +    switch( eType ){
        78520  +      case SQLITE_INTEGER: {
        78521  +        pSample->u.i = sqlite3_column_int64(pStmt, 4);
        78522  +        break;
        78523  +      }
        78524  +      case SQLITE_FLOAT: {
        78525  +        pSample->u.r = sqlite3_column_double(pStmt, 4);
        78526  +        break;
        78527  +      }
        78528  +      case SQLITE_NULL: {
        78529  +        break;
        78530  +      }
        78531  +      default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
        78532  +        const char *z = (const char *)(
        78533  +              (eType==SQLITE_BLOB) ?
        78534  +              sqlite3_column_blob(pStmt, 4):
        78535  +              sqlite3_column_text(pStmt, 4)
        78536  +           );
        78537  +        int n = z ? sqlite3_column_bytes(pStmt, 4) : 0;
        78538  +        pSample->nByte = n;
        78539  +        if( n < 1){
        78540  +          pSample->u.z = 0;
        78541  +        }else{
        78542  +          pSample->u.z = sqlite3Malloc(n);
        78543  +          if( pSample->u.z==0 ){
        78544  +            db->mallocFailed = 1;
        78545  +            sqlite3_finalize(pStmt);
        78546  +            return SQLITE_NOMEM;
        78547  +          }
        78548  +          memcpy(pSample->u.z, z, n);
        78549  +        }
        78550  +      }
        78551  +    }
        78552  +  }
        78553  +  return sqlite3_finalize(pStmt);
        78554  +}
        78555  +#endif /* SQLITE_ENABLE_STAT3 */
        78556  +
        78557  +/*
        78558  +** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
 78184  78559   ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
 78185         -** arrays. The contents of sqlite_stat2 are used to populate the
        78560  +** arrays. The contents of sqlite_stat3 are used to populate the
 78186  78561   ** Index.aSample[] arrays.
 78187  78562   **
 78188  78563   ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
 78189         -** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined 
 78190         -** during compilation and the sqlite_stat2 table is present, no data is 
        78564  +** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined 
        78565  +** during compilation and the sqlite_stat3 table is present, no data is 
 78191  78566   ** read from it.
 78192  78567   **
 78193         -** If SQLITE_ENABLE_STAT2 was defined during compilation and the 
 78194         -** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
        78568  +** If SQLITE_ENABLE_STAT3 was defined during compilation and the 
        78569  +** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
 78195  78570   ** returned. However, in this case, data is read from the sqlite_stat1
 78196  78571   ** table (if it is present) before returning.
 78197  78572   **
 78198  78573   ** If an OOM error occurs, this function always sets db->mallocFailed.
 78199  78574   ** This means if the caller does not care about other errors, the return
 78200  78575   ** code may be ignored.
 78201  78576   */
................................................................................
 78209  78584     assert( db->aDb[iDb].pBt!=0 );
 78210  78585   
 78211  78586     /* Clear any prior statistics */
 78212  78587     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 78213  78588     for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
 78214  78589       Index *pIdx = sqliteHashData(i);
 78215  78590       sqlite3DefaultRowEst(pIdx);
        78591  +#ifdef SQLITE_ENABLE_STAT3
 78216  78592       sqlite3DeleteIndexSamples(db, pIdx);
 78217  78593       pIdx->aSample = 0;
        78594  +#endif
 78218  78595     }
 78219  78596   
 78220  78597     /* Check to make sure the sqlite_stat1 table exists */
 78221  78598     sInfo.db = db;
 78222  78599     sInfo.zDatabase = db->aDb[iDb].zName;
 78223  78600     if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
 78224  78601       return SQLITE_ERROR;
 78225  78602     }
 78226  78603   
 78227  78604     /* Load new statistics out of the sqlite_stat1 table */
 78228  78605     zSql = sqlite3MPrintf(db, 
 78229         -      "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
        78606  +      "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
 78230  78607     if( zSql==0 ){
 78231  78608       rc = SQLITE_NOMEM;
 78232  78609     }else{
 78233  78610       rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
 78234  78611       sqlite3DbFree(db, zSql);
 78235  78612     }
 78236  78613   
 78237  78614   
 78238         -  /* Load the statistics from the sqlite_stat2 table. */
 78239         -#ifdef SQLITE_ENABLE_STAT2
 78240         -  if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
 78241         -    rc = SQLITE_ERROR;
 78242         -  }
        78615  +  /* Load the statistics from the sqlite_stat3 table. */
        78616  +#ifdef SQLITE_ENABLE_STAT3
 78243  78617     if( rc==SQLITE_OK ){
 78244         -    sqlite3_stmt *pStmt = 0;
 78245         -
 78246         -    zSql = sqlite3MPrintf(db, 
 78247         -        "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
 78248         -    if( !zSql ){
 78249         -      rc = SQLITE_NOMEM;
 78250         -    }else{
 78251         -      rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
 78252         -      sqlite3DbFree(db, zSql);
 78253         -    }
 78254         -
 78255         -    if( rc==SQLITE_OK ){
 78256         -      while( sqlite3_step(pStmt)==SQLITE_ROW ){
 78257         -        char *zIndex;   /* Index name */
 78258         -        Index *pIdx;    /* Pointer to the index object */
 78259         -
 78260         -        zIndex = (char *)sqlite3_column_text(pStmt, 0);
 78261         -        pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
 78262         -        if( pIdx ){
 78263         -          int iSample = sqlite3_column_int(pStmt, 1);
 78264         -          if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
 78265         -            int eType = sqlite3_column_type(pStmt, 2);
 78266         -
 78267         -            if( pIdx->aSample==0 ){
 78268         -              static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
 78269         -              pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
 78270         -              if( pIdx->aSample==0 ){
 78271         -                db->mallocFailed = 1;
 78272         -                break;
 78273         -              }
 78274         -	      memset(pIdx->aSample, 0, sz);
 78275         -            }
 78276         -
 78277         -            assert( pIdx->aSample );
 78278         -            {
 78279         -              IndexSample *pSample = &pIdx->aSample[iSample];
 78280         -              pSample->eType = (u8)eType;
 78281         -              if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
 78282         -                pSample->u.r = sqlite3_column_double(pStmt, 2);
 78283         -              }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
 78284         -                const char *z = (const char *)(
 78285         -                    (eType==SQLITE_BLOB) ?
 78286         -                    sqlite3_column_blob(pStmt, 2):
 78287         -                    sqlite3_column_text(pStmt, 2)
 78288         -                );
 78289         -                int n = sqlite3_column_bytes(pStmt, 2);
 78290         -                if( n>24 ){
 78291         -                  n = 24;
 78292         -                }
 78293         -                pSample->nByte = (u8)n;
 78294         -                if( n < 1){
 78295         -                  pSample->u.z = 0;
 78296         -                }else{
 78297         -                  pSample->u.z = sqlite3DbStrNDup(0, z, n);
 78298         -                  if( pSample->u.z==0 ){
 78299         -                    db->mallocFailed = 1;
 78300         -                    break;
 78301         -                  }
 78302         -                }
 78303         -              }
 78304         -            }
 78305         -          }
 78306         -        }
 78307         -      }
 78308         -      rc = sqlite3_finalize(pStmt);
 78309         -    }
        78618  +    rc = loadStat3(db, sInfo.zDatabase);
 78310  78619     }
 78311  78620   #endif
 78312  78621   
 78313  78622     if( rc==SQLITE_NOMEM ){
 78314  78623       db->mallocFailed = 1;
 78315  78624     }
 78316  78625     return rc;
................................................................................
 81109  81418         iDestroyed = iLargest;
 81110  81419       }
 81111  81420     }
 81112  81421   #endif
 81113  81422   }
 81114  81423   
 81115  81424   /*
 81116         -** Remove entries from the sqlite_stat1 and sqlite_stat2 tables
        81425  +** Remove entries from the sqlite_statN tables (for N in (1,2,3))
 81117  81426   ** after a DROP INDEX or DROP TABLE command.
 81118  81427   */
 81119  81428   static void sqlite3ClearStatTables(
 81120  81429     Parse *pParse,         /* The parsing context */
 81121  81430     int iDb,               /* The database number */
 81122  81431     const char *zType,     /* "idx" or "tbl" */
 81123  81432     const char *zName      /* Name of index or table */
 81124  81433   ){
 81125         -  static const char *azStatTab[] = { "sqlite_stat1", "sqlite_stat2" };
 81126  81434     int i;
 81127  81435     const char *zDbName = pParse->db->aDb[iDb].zName;
 81128         -  for(i=0; i<ArraySize(azStatTab); i++){
 81129         -    if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){
        81436  +  for(i=1; i<=3; i++){
        81437  +    char zTab[24];
        81438  +    sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
        81439  +    if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
 81130  81440         sqlite3NestedParse(pParse,
 81131  81441           "DELETE FROM %Q.%s WHERE %s=%Q",
 81132         -        zDbName, azStatTab[i], zType, zName
        81442  +        zDbName, zTab, zType, zName
 81133  81443         );
 81134  81444       }
 81135  81445     }
 81136  81446   }
        81447  +
        81448  +/*
        81449  +** Generate code to drop a table.
        81450  +*/
        81451  +SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
        81452  +  Vdbe *v;
        81453  +  sqlite3 *db = pParse->db;
        81454  +  Trigger *pTrigger;
        81455  +  Db *pDb = &db->aDb[iDb];
        81456  +
        81457  +  v = sqlite3GetVdbe(pParse);
        81458  +  assert( v!=0 );
        81459  +  sqlite3BeginWriteOperation(pParse, 1, iDb);
        81460  +
        81461  +#ifndef SQLITE_OMIT_VIRTUALTABLE
        81462  +  if( IsVirtual(pTab) ){
        81463  +    sqlite3VdbeAddOp0(v, OP_VBegin);
        81464  +  }
        81465  +#endif
        81466  +
        81467  +  /* Drop all triggers associated with the table being dropped. Code
        81468  +  ** is generated to remove entries from sqlite_master and/or
        81469  +  ** sqlite_temp_master if required.
        81470  +  */
        81471  +  pTrigger = sqlite3TriggerList(pParse, pTab);
        81472  +  while( pTrigger ){
        81473  +    assert( pTrigger->pSchema==pTab->pSchema || 
        81474  +        pTrigger->pSchema==db->aDb[1].pSchema );
        81475  +    sqlite3DropTriggerPtr(pParse, pTrigger);
        81476  +    pTrigger = pTrigger->pNext;
        81477  +  }
        81478  +
        81479  +#ifndef SQLITE_OMIT_AUTOINCREMENT
        81480  +  /* Remove any entries of the sqlite_sequence table associated with
        81481  +  ** the table being dropped. This is done before the table is dropped
        81482  +  ** at the btree level, in case the sqlite_sequence table needs to
        81483  +  ** move as a result of the drop (can happen in auto-vacuum mode).
        81484  +  */
        81485  +  if( pTab->tabFlags & TF_Autoincrement ){
        81486  +    sqlite3NestedParse(pParse,
        81487  +      "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
        81488  +      pDb->zName, pTab->zName
        81489  +    );
        81490  +  }
        81491  +#endif
        81492  +
        81493  +  /* Drop all SQLITE_MASTER table and index entries that refer to the
        81494  +  ** table. The program name loops through the master table and deletes
        81495  +  ** every row that refers to a table of the same name as the one being
        81496  +  ** dropped. Triggers are handled seperately because a trigger can be
        81497  +  ** created in the temp database that refers to a table in another
        81498  +  ** database.
        81499  +  */
        81500  +  sqlite3NestedParse(pParse, 
        81501  +      "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
        81502  +      pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
        81503  +  if( !isView && !IsVirtual(pTab) ){
        81504  +    destroyTable(pParse, pTab);
        81505  +  }
        81506  +
        81507  +  /* Remove the table entry from SQLite's internal schema and modify
        81508  +  ** the schema cookie.
        81509  +  */
        81510  +  if( IsVirtual(pTab) ){
        81511  +    sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
        81512  +  }
        81513  +  sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
        81514  +  sqlite3ChangeCookie(pParse, iDb);
        81515  +  sqliteViewResetAll(db, iDb);
        81516  +}
 81137  81517   
 81138  81518   /*
 81139  81519   ** This routine is called to do the work of a DROP TABLE statement.
 81140  81520   ** pName is the name of the table to be dropped.
 81141  81521   */
 81142  81522   SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
 81143  81523     Table *pTab;
................................................................................
 81199  81579         goto exit_drop_table;
 81200  81580       }
 81201  81581       if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
 81202  81582         goto exit_drop_table;
 81203  81583       }
 81204  81584     }
 81205  81585   #endif
 81206         -  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
        81586  +  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
        81587  +    && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
 81207  81588       sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
 81208  81589       goto exit_drop_table;
 81209  81590     }
 81210  81591   
 81211  81592   #ifndef SQLITE_OMIT_VIEW
 81212  81593     /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
 81213  81594     ** on a table.
................................................................................
 81223  81604   #endif
 81224  81605   
 81225  81606     /* Generate code to remove the table from the master table
 81226  81607     ** on disk.
 81227  81608     */
 81228  81609     v = sqlite3GetVdbe(pParse);
 81229  81610     if( v ){
 81230         -    Trigger *pTrigger;
 81231         -    Db *pDb = &db->aDb[iDb];
 81232  81611       sqlite3BeginWriteOperation(pParse, 1, iDb);
 81233         -
 81234         -#ifndef SQLITE_OMIT_VIRTUALTABLE
 81235         -    if( IsVirtual(pTab) ){
 81236         -      sqlite3VdbeAddOp0(v, OP_VBegin);
 81237         -    }
 81238         -#endif
        81612  +    sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
 81239  81613       sqlite3FkDropTable(pParse, pName, pTab);
 81240         -
 81241         -    /* Drop all triggers associated with the table being dropped. Code
 81242         -    ** is generated to remove entries from sqlite_master and/or
 81243         -    ** sqlite_temp_master if required.
 81244         -    */
 81245         -    pTrigger = sqlite3TriggerList(pParse, pTab);
 81246         -    while( pTrigger ){
 81247         -      assert( pTrigger->pSchema==pTab->pSchema || 
 81248         -          pTrigger->pSchema==db->aDb[1].pSchema );
 81249         -      sqlite3DropTriggerPtr(pParse, pTrigger);
 81250         -      pTrigger = pTrigger->pNext;
 81251         -    }
 81252         -
 81253         -#ifndef SQLITE_OMIT_AUTOINCREMENT
 81254         -    /* Remove any entries of the sqlite_sequence table associated with
 81255         -    ** the table being dropped. This is done before the table is dropped
 81256         -    ** at the btree level, in case the sqlite_sequence table needs to
 81257         -    ** move as a result of the drop (can happen in auto-vacuum mode).
 81258         -    */
 81259         -    if( pTab->tabFlags & TF_Autoincrement ){
 81260         -      sqlite3NestedParse(pParse,
 81261         -        "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
 81262         -        pDb->zName, pTab->zName
 81263         -      );
 81264         -    }
 81265         -#endif
 81266         -
 81267         -    /* Drop all SQLITE_MASTER table and index entries that refer to the
 81268         -    ** table. The program name loops through the master table and deletes
 81269         -    ** every row that refers to a table of the same name as the one being
 81270         -    ** dropped. Triggers are handled seperately because a trigger can be
 81271         -    ** created in the temp database that refers to a table in another
 81272         -    ** database.
 81273         -    */
 81274         -    sqlite3NestedParse(pParse, 
 81275         -        "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
 81276         -        pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
 81277         -    sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
 81278         -    if( !isView && !IsVirtual(pTab) ){
 81279         -      destroyTable(pParse, pTab);
 81280         -    }
 81281         -
 81282         -    /* Remove the table entry from SQLite's internal schema and modify
 81283         -    ** the schema cookie.
 81284         -    */
 81285         -    if( IsVirtual(pTab) ){
 81286         -      sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
 81287         -    }
 81288         -    sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
 81289         -    sqlite3ChangeCookie(pParse, iDb);
        81614  +    sqlite3CodeDropTable(pParse, pTab, iDb, isView);
 81290  81615     }
 81291         -  sqliteViewResetAll(db, iDb);
 81292  81616   
 81293  81617   exit_drop_table:
 81294  81618     sqlite3SrcListDelete(db, pName);
 81295  81619   }
 81296  81620   
 81297  81621   /*
 81298  81622   ** This routine is called to create a new foreign key on the table
................................................................................
 81452  81776   ** the index already exists and must be cleared before being refilled and
 81453  81777   ** the root page number of the index is taken from pIndex->tnum.
 81454  81778   */
 81455  81779   static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
 81456  81780     Table *pTab = pIndex->pTable;  /* The table that is indexed */
 81457  81781     int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
 81458  81782     int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
 81459         -  int iSorter = iTab;            /* Cursor opened by OpenSorter (if in use) */
        81783  +  int iSorter;                   /* Cursor opened by OpenSorter (if in use) */
 81460  81784     int addr1;                     /* Address of top of loop */
 81461  81785     int addr2;                     /* Address to jump to for next iteration */
 81462  81786     int tnum;                      /* Root page of index */
 81463  81787     Vdbe *v;                       /* Generate code into this virtual machine */
 81464  81788     KeyInfo *pKey;                 /* KeyInfo for index */
        81789  +#ifdef SQLITE_OMIT_MERGE_SORT
 81465  81790     int regIdxKey;                 /* Registers containing the index key */
        81791  +#endif
 81466  81792     int regRecord;                 /* Register holding assemblied index record */
 81467  81793     sqlite3 *db = pParse->db;      /* The database connection */
 81468  81794     int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
 81469  81795   
 81470  81796   #ifndef SQLITE_OMIT_AUTHORIZATION
 81471  81797     if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
 81472  81798         db->aDb[iDb].zName ) ){
................................................................................
 81492  81818       sqlite3VdbeChangeP5(v, 1);
 81493  81819     }
 81494  81820   
 81495  81821   #ifndef SQLITE_OMIT_MERGE_SORT
 81496  81822     /* Open the sorter cursor if we are to use one. */
 81497  81823     iSorter = pParse->nTab++;
 81498  81824     sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
        81825  +#else
        81826  +  iSorter = iTab;
 81499  81827   #endif
 81500  81828   
 81501  81829     /* Open the table. Loop through all rows of the table, inserting index
 81502  81830     ** records into the sorter. */
 81503  81831     sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
 81504  81832     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
 81505         -  addr2 = addr1 + 1;
 81506  81833     regRecord = sqlite3GetTempReg(pParse);
 81507         -  regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
 81508  81834   
 81509  81835   #ifndef SQLITE_OMIT_MERGE_SORT
        81836  +  sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
 81510  81837     sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
 81511  81838     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
 81512  81839     sqlite3VdbeJumpHere(v, addr1);
 81513  81840     addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
 81514  81841     if( pIndex->onError!=OE_None ){
 81515  81842       int j2 = sqlite3VdbeCurrentAddr(v) + 3;
 81516  81843       sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
................................................................................
 81522  81849     }else{
 81523  81850       addr2 = sqlite3VdbeCurrentAddr(v);
 81524  81851     }
 81525  81852     sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
 81526  81853     sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
 81527  81854     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 81528  81855   #else
        81856  +  regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
        81857  +  addr2 = addr1 + 1;
 81529  81858     if( pIndex->onError!=OE_None ){
 81530  81859       const int regRowid = regIdxKey + pIndex->nColumn;
 81531  81860       const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
 81532  81861       void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
 81533  81862   
 81534  81863       /* The registers accessed by the OP_IsUnique opcode were allocated
 81535  81864       ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
................................................................................
 81619  81948       /* Use the two-part index name to determine the database 
 81620  81949       ** to search for the table. 'Fix' the table name to this db
 81621  81950       ** before looking up the table.
 81622  81951       */
 81623  81952       assert( pName1 && pName2 );
 81624  81953       iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 81625  81954       if( iDb<0 ) goto exit_create_index;
        81955  +    assert( pName && pName->z );
 81626  81956   
 81627  81957   #ifndef SQLITE_OMIT_TEMPDB
 81628  81958       /* If the index name was unqualified, check if the the table
 81629  81959       ** is a temp table. If so, set the database to 1. Do not do this
 81630  81960       ** if initialising a database schema.
 81631  81961       */
 81632  81962       if( !db->init.busy ){
................................................................................
 81646  81976       }
 81647  81977       pTab = sqlite3LocateTable(pParse, 0, pTblName->a[0].zName, 
 81648  81978           pTblName->a[0].zDatabase);
 81649  81979       if( !pTab || db->mallocFailed ) goto exit_create_index;
 81650  81980       assert( db->aDb[iDb].pSchema==pTab->pSchema );
 81651  81981     }else{
 81652  81982       assert( pName==0 );
        81983  +    assert( pStart==0 );
 81653  81984       pTab = pParse->pNewTable;
 81654  81985       if( !pTab ) goto exit_create_index;
 81655  81986       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 81656  81987     }
 81657  81988     pDb = &db->aDb[iDb];
 81658  81989   
 81659  81990     assert( pTab!=0 );
................................................................................
 81688  82019     ** If pName==0 it means that we are
 81689  82020     ** dealing with a primary key or UNIQUE constraint.  We have to invent our
 81690  82021     ** own name.
 81691  82022     */
 81692  82023     if( pName ){
 81693  82024       zName = sqlite3NameFromToken(db, pName);
 81694  82025       if( zName==0 ) goto exit_create_index;
        82026  +    assert( pName->z!=0 );
 81695  82027       if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
 81696  82028         goto exit_create_index;
 81697  82029       }
 81698  82030       if( !db->init.busy ){
 81699  82031         if( sqlite3FindTable(db, zName, 0)!=0 ){
 81700  82032           sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
 81701  82033           goto exit_create_index;
................................................................................
 81767  82099     /* 
 81768  82100     ** Allocate the index structure. 
 81769  82101     */
 81770  82102     nName = sqlite3Strlen30(zName);
 81771  82103     nCol = pList->nExpr;
 81772  82104     pIndex = sqlite3DbMallocZero(db, 
 81773  82105         sizeof(Index) +              /* Index structure  */
        82106  +      sizeof(tRowcnt)*(nCol+1) +   /* Index.aiRowEst   */
 81774  82107         sizeof(int)*nCol +           /* Index.aiColumn   */
 81775         -      sizeof(int)*(nCol+1) +       /* Index.aiRowEst   */
 81776  82108         sizeof(char *)*nCol +        /* Index.azColl     */
 81777  82109         sizeof(u8)*nCol +            /* Index.aSortOrder */
 81778  82110         nName + 1 +                  /* Index.zName      */
 81779  82111         nExtra                       /* Collation sequence names */
 81780  82112     );
 81781  82113     if( db->mallocFailed ){
 81782  82114       goto exit_create_index;
 81783  82115     }
 81784         -  pIndex->azColl = (char**)(&pIndex[1]);
        82116  +  pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]);
        82117  +  pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]);
 81785  82118     pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
 81786         -  pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
 81787         -  pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
        82119  +  pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
 81788  82120     pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
 81789  82121     zExtra = (char *)(&pIndex->zName[nName+1]);
 81790  82122     memcpy(pIndex->zName, zName, nName+1);
 81791  82123     pIndex->pTable = pTab;
 81792  82124     pIndex->nColumn = pList->nExpr;
 81793  82125     pIndex->onError = (u8)onError;
 81794  82126     pIndex->autoIndex = (u8)(pName==0);
................................................................................
 82057  82389   **           aiRowEst[N]>=1
 82058  82390   **
 82059  82391   ** Apart from that, we have little to go on besides intuition as to
 82060  82392   ** how aiRowEst[] should be initialized.  The numbers generated here
 82061  82393   ** are based on typical values found in actual indices.
 82062  82394   */
 82063  82395   SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
 82064         -  unsigned *a = pIdx->aiRowEst;
        82396  +  tRowcnt *a = pIdx->aiRowEst;
 82065  82397     int i;
 82066         -  unsigned n;
        82398  +  tRowcnt n;
 82067  82399     assert( a!=0 );
 82068  82400     a[0] = pIdx->pTable->nRowEst;
 82069  82401     if( a[0]<10 ) a[0] = 10;
 82070  82402     n = 10;
 82071  82403     for(i=1; i<=pIdx->nColumn; i++){
 82072  82404       a[i] = n;
 82073  82405       if( n>5 ) n--;
................................................................................
 82543  82875     sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
 82544  82876   }
 82545  82877   
 82546  82878   /*
 82547  82879   ** Commit a transaction
 82548  82880   */
 82549  82881   SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
 82550         -  sqlite3 *db;
 82551  82882     Vdbe *v;
 82552  82883   
 82553  82884     assert( pParse!=0 );
 82554         -  db = pParse->db;
 82555         -  assert( db!=0 );
 82556         -/*  if( db->aDb[0].pBt==0 ) return; */
        82885  +  assert( pParse->db!=0 );
 82557  82886     if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
 82558  82887       return;
 82559  82888     }
 82560  82889     v = sqlite3GetVdbe(pParse);
 82561  82890     if( v ){
 82562  82891       sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
 82563  82892     }
 82564  82893   }
 82565  82894   
 82566  82895   /*
 82567  82896   ** Rollback a transaction
 82568  82897   */
 82569  82898   SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
 82570         -  sqlite3 *db;
 82571  82899     Vdbe *v;
 82572  82900   
 82573  82901     assert( pParse!=0 );
 82574         -  db = pParse->db;
 82575         -  assert( db!=0 );
 82576         -/*  if( db->aDb[0].pBt==0 ) return; */
        82902  +  assert( pParse->db!=0 );
 82577  82903     if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
 82578  82904       return;
 82579  82905     }
 82580  82906     v = sqlite3GetVdbe(pParse);
 82581  82907     if( v ){
 82582  82908       sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
 82583  82909     }
................................................................................
 84375  84701     z2 = (char*)sqlite3_value_text(argv[0]);
 84376  84702     n = sqlite3_value_bytes(argv[0]);
 84377  84703     /* Verify that the call to _bytes() does not invalidate the _text() pointer */
 84378  84704     assert( z2==(char*)sqlite3_value_text(argv[0]) );
 84379  84705     if( z2 ){
 84380  84706       z1 = contextMalloc(context, ((i64)n)+1);
 84381  84707       if( z1 ){
 84382         -      memcpy(z1, z2, n+1);
 84383         -      for(i=0; z1[i]; i++){
 84384         -        z1[i] = (char)sqlite3Toupper(z1[i]);
        84708  +      for(i=0; i<n; i++){
        84709  +        z1[i] = (char)sqlite3Toupper(z2[i]);
 84385  84710         }
 84386         -      sqlite3_result_text(context, z1, -1, sqlite3_free);
        84711  +      sqlite3_result_text(context, z1, n, sqlite3_free);
 84387  84712       }
 84388  84713     }
 84389  84714   }
 84390  84715   static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 84391         -  u8 *z1;
        84716  +  char *z1;
 84392  84717     const char *z2;
 84393  84718     int i, n;
 84394  84719     UNUSED_PARAMETER(argc);
 84395  84720     z2 = (char*)sqlite3_value_text(argv[0]);
 84396  84721     n = sqlite3_value_bytes(argv[0]);
 84397  84722     /* Verify that the call to _bytes() does not invalidate the _text() pointer */
 84398  84723     assert( z2==(char*)sqlite3_value_text(argv[0]) );
 84399  84724     if( z2 ){
 84400  84725       z1 = contextMalloc(context, ((i64)n)+1);
 84401  84726       if( z1 ){
 84402         -      memcpy(z1, z2, n+1);
 84403         -      for(i=0; z1[i]; i++){
 84404         -        z1[i] = sqlite3Tolower(z1[i]);
        84727  +      for(i=0; i<n; i++){
        84728  +        z1[i] = sqlite3Tolower(z2[i]);
 84405  84729         }
 84406         -      sqlite3_result_text(context, (char *)z1, -1, sqlite3_free);
        84730  +      sqlite3_result_text(context, z1, n, sqlite3_free);
 84407  84731       }
 84408  84732     }
 84409  84733   }
 84410  84734   
 84411  84735   
 84412  84736   #if 0  /* This function is never used. */
 84413  84737   /*
................................................................................
 86776  87100       sqlite3ExprDelete(db, pWhen);
 86777  87101       sqlite3ExprListDelete(db, pList);
 86778  87102       sqlite3SelectDelete(db, pSelect);
 86779  87103       if( db->mallocFailed==1 ){
 86780  87104         fkTriggerDelete(db, pTrigger);
 86781  87105         return 0;
 86782  87106       }
        87107  +    assert( pStep!=0 );
 86783  87108   
 86784  87109       switch( action ){
 86785  87110         case OE_Restrict:
 86786  87111           pStep->op = TK_SELECT; 
 86787  87112           break;
 86788  87113         case OE_Cascade: 
 86789  87114           if( !pChanges ){ 
................................................................................
 88619  88944     ** the extra complication to make this rule less restrictive is probably
 88620  88945     ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
 88621  88946     */
 88622  88947     if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
 88623  88948       return 0;
 88624  88949     }
 88625  88950   #endif
        88951  +  if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
        88952  +    return 0;
        88953  +  }
 88626  88954   
 88627  88955     /* If we get this far, it means either:
 88628  88956     **
 88629  88957     **    *   We can always do the transfer if the table contains an
 88630  88958     **        an integer primary key
 88631  88959     **
 88632  88960     **    *   We can conditionally do the transfer if the destination
................................................................................
 88933  89261     int  (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
 88934  89262     int  (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
 88935  89263     int  (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
 88936  89264     int  (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
 88937  89265     int  (*busy_timeout)(sqlite3*,int ms);
 88938  89266     int  (*changes)(sqlite3*);
 88939  89267     int  (*close)(sqlite3*);
 88940         -  int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const char*));
 88941         -  int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const void*));
        89268  +  int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
        89269  +                           int eTextRep,const char*));
        89270  +  int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
        89271  +                             int eTextRep,const void*));
 88942  89272     const void * (*column_blob)(sqlite3_stmt*,int iCol);
 88943  89273     int  (*column_bytes)(sqlite3_stmt*,int iCol);
 88944  89274     int  (*column_bytes16)(sqlite3_stmt*,int iCol);
 88945  89275     int  (*column_count)(sqlite3_stmt*pStmt);
 88946  89276     const char * (*column_database_name)(sqlite3_stmt*,int);
 88947  89277     const void * (*column_database_name16)(sqlite3_stmt*,int);
 88948  89278     const char * (*column_decltype)(sqlite3_stmt*,int i);
................................................................................
 88959  89289     const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
 88960  89290     const void * (*column_text16)(sqlite3_stmt*,int iCol);
 88961  89291     int  (*column_type)(sqlite3_stmt*,int iCol);
 88962  89292     sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
 88963  89293     void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
 88964  89294     int  (*complete)(const char*sql);
 88965  89295     int  (*complete16)(const void*sql);
 88966         -  int  (*create_collation)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*));
 88967         -  int  (*create_collation16)(sqlite3*,const void*,int,void*,int(*)(void*,int,const void*,int,const void*));
 88968         -  int  (*create_function)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*));
 88969         -  int  (*create_function16)(sqlite3*,const void*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*));
        89296  +  int  (*create_collation)(sqlite3*,const char*,int,void*,
        89297  +                           int(*)(void*,int,const void*,int,const void*));
        89298  +  int  (*create_collation16)(sqlite3*,const void*,int,void*,
        89299  +                             int(*)(void*,int,const void*,int,const void*));
        89300  +  int  (*create_function)(sqlite3*,const char*,int,int,void*,
        89301  +                          void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
        89302  +                          void (*xStep)(sqlite3_context*,int,sqlite3_value**),
        89303  +                          void (*xFinal)(sqlite3_context*));
        89304  +  int  (*create_function16)(sqlite3*,const void*,int,int,void*,
        89305  +                            void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
        89306  +                            void (*xStep)(sqlite3_context*,int,sqlite3_value**),
        89307  +                            void (*xFinal)(sqlite3_context*));
 88970  89308     int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
 88971  89309     int  (*data_count)(sqlite3_stmt*pStmt);
 88972  89310     sqlite3 * (*db_handle)(sqlite3_stmt*);
 88973  89311     int (*declare_vtab)(sqlite3*,const char*);
 88974  89312     int  (*enable_shared_cache)(int);
 88975  89313     int  (*errcode)(sqlite3*db);
 88976  89314     const char * (*errmsg)(sqlite3*);
................................................................................
 89007  89345     void  (*result_null)(sqlite3_context*);
 89008  89346     void  (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
 89009  89347     void  (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
 89010  89348     void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
 89011  89349     void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
 89012  89350     void  (*result_value)(sqlite3_context*,sqlite3_value*);
 89013  89351     void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
 89014         -  int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,const char*,const char*),void*);
        89352  +  int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
        89353  +                         const char*,const char*),void*);
 89015  89354     void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
 89016  89355     char * (*snprintf)(int,char*,const char*,...);
 89017  89356     int  (*step)(sqlite3_stmt*);
 89018         -  int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,char const**,char const**,int*,int*,int*);
        89357  +  int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
        89358  +                                char const**,char const**,int*,int*,int*);
 89019  89359     void  (*thread_cleanup)(void);
 89020  89360     int  (*total_changes)(sqlite3*);
 89021  89361     void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
 89022  89362     int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
 89023         -  void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,sqlite_int64),void*);
        89363  +  void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
        89364  +                                         sqlite_int64),void*);
 89024  89365     void * (*user_data)(sqlite3_context*);
 89025  89366     const void * (*value_blob)(sqlite3_value*);
 89026  89367     int  (*value_bytes)(sqlite3_value*);
 89027  89368     int  (*value_bytes16)(sqlite3_value*);
 89028  89369     double  (*value_double)(sqlite3_value*);
 89029  89370     int  (*value_int)(sqlite3_value*);
 89030  89371     sqlite_int64  (*value_int64)(sqlite3_value*);
................................................................................
 89038  89379     /* Added ??? */
 89039  89380     int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
 89040  89381     /* Added by 3.3.13 */
 89041  89382     int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
 89042  89383     int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
 89043  89384     int (*clear_bindings)(sqlite3_stmt*);
 89044  89385     /* Added by 3.4.1 */
 89045         -  int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,void (*xDestroy)(void *));
        89386  +  int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
        89387  +                          void (*xDestroy)(void *));
 89046  89388     /* Added by 3.5.0 */
 89047  89389     int (*bind_zeroblob)(sqlite3_stmt*,int,int);
 89048  89390     int (*blob_bytes)(sqlite3_blob*);
 89049  89391     int (*blob_close)(sqlite3_blob*);
 89050         -  int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,int,sqlite3_blob**);
        89392  +  int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
        89393  +                   int,sqlite3_blob**);
 89051  89394     int (*blob_read)(sqlite3_blob*,void*,int,int);
 89052  89395     int (*blob_write)(sqlite3_blob*,const void*,int,int);
 89053         -  int (*create_collation_v2)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*),void(*)(void*));
        89396  +  int (*create_collation_v2)(sqlite3*,const char*,int,void*,
        89397  +                             int(*)(void*,int,const void*,int,const void*),
        89398  +                             void(*)(void*));
 89054  89399     int (*file_control)(sqlite3*,const char*,int,void*);
 89055  89400     sqlite3_int64 (*memory_highwater)(int);
 89056  89401     sqlite3_int64 (*memory_used)(void);
 89057  89402     sqlite3_mutex *(*mutex_alloc)(int);
 89058  89403     void (*mutex_enter)(sqlite3_mutex*);
 89059  89404     void (*mutex_free)(sqlite3_mutex*);
 89060  89405     void (*mutex_leave)(sqlite3_mutex*);
................................................................................
 89082  89427     int (*backup_finish)(sqlite3_backup*);
 89083  89428     sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
 89084  89429     int (*backup_pagecount)(sqlite3_backup*);
 89085  89430     int (*backup_remaining)(sqlite3_backup*);
 89086  89431     int (*backup_step)(sqlite3_backup*,int);
 89087  89432     const char *(*compileoption_get)(int);
 89088  89433     int (*compileoption_used)(const char*);
 89089         -  int (*create_function_v2)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*),void(*xDestroy)(void*));
        89434  +  int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
        89435  +                            void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
        89436  +                            void (*xStep)(sqlite3_context*,int,sqlite3_value**),
        89437  +                            void (*xFinal)(sqlite3_context*),
        89438  +                            void(*xDestroy)(void*));
 89090  89439     int (*db_config)(sqlite3*,int,...);
 89091  89440     sqlite3_mutex *(*db_mutex)(sqlite3*);
 89092  89441     int (*db_status)(sqlite3*,int,int*,int*,int);
 89093  89442     int (*extended_errcode)(sqlite3*);
 89094  89443     void (*log)(int,const char*,...);
 89095  89444     sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
 89096  89445     const char *(*sourceid)(void);
................................................................................
 89696  90045     char **pzErrMsg       /* Put error message here if not 0 */
 89697  90046   ){
 89698  90047     sqlite3_vfs *pVfs = db->pVfs;
 89699  90048     void *handle;
 89700  90049     int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
 89701  90050     char *zErrmsg = 0;
 89702  90051     void **aHandle;
 89703         -  const int nMsg = 300;
        90052  +  int nMsg = 300 + sqlite3Strlen30(zFile);
 89704  90053   
 89705  90054     if( pzErrMsg ) *pzErrMsg = 0;
 89706  90055   
 89707  90056     /* Ticket #1863.  To avoid a creating security problems for older
 89708  90057     ** applications that relink against newer versions of SQLite, the
 89709  90058     ** ability to run load_extension is turned off by default.  One
 89710  90059     ** must call sqlite3_enable_load_extension() to turn on extension
................................................................................
 89733  90082       }
 89734  90083       return SQLITE_ERROR;
 89735  90084     }
 89736  90085     xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
 89737  90086                      sqlite3OsDlSym(pVfs, handle, zProc);
 89738  90087     if( xInit==0 ){
 89739  90088       if( pzErrMsg ){
        90089  +      nMsg += sqlite3Strlen30(zProc);
 89740  90090         *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
 89741  90091         if( zErrmsg ){
 89742  90092           sqlite3_snprintf(nMsg, zErrmsg,
 89743  90093               "no entry point [%s] in shared library [%s]", zProc,zFile);
 89744  90094           sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
 89745  90095         }
 89746  90096         sqlite3OsDlClose(pVfs, handle);
................................................................................
 90418  90768     if( sqlite3StrICmp(zLeft,"page_count")==0
 90419  90769      || sqlite3StrICmp(zLeft,"max_page_count")==0
 90420  90770     ){
 90421  90771       int iReg;
 90422  90772       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 90423  90773       sqlite3CodeVerifySchema(pParse, iDb);
 90424  90774       iReg = ++pParse->nMem;
 90425         -    if( zLeft[0]=='p' ){
        90775  +    if( sqlite3Tolower(zLeft[0])=='p' ){
 90426  90776         sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
 90427  90777       }else{
 90428  90778         sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight));
 90429  90779       }
 90430  90780       sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
 90431  90781       sqlite3VdbeSetNumCols(v, 1);
 90432  90782       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
................................................................................
 90484  90834     **  PRAGMA [database.]journal_mode =
 90485  90835     **                      (delete|persist|off|truncate|memory|wal|off)
 90486  90836     */
 90487  90837     if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
 90488  90838       int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
 90489  90839       int ii;           /* Loop counter */
 90490  90840   
 90491         -    /* Force the schema to be loaded on all databases.  This cases all
 90492         -    ** database files to be opened and the journal_modes set. */
        90841  +    /* Force the schema to be loaded on all databases.  This causes all
        90842  +    ** database files to be opened and the journal_modes set.  This is
        90843  +    ** necessary because subsequent processing must know if the databases
        90844  +    ** are in WAL mode. */
 90493  90845       if( sqlite3ReadSchema(pParse) ){
 90494  90846         goto pragma_out;
 90495  90847       }
 90496  90848   
 90497  90849       sqlite3VdbeSetNumCols(v, 1);
 90498  90850       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
 90499  90851   
................................................................................
 91029  91381       static const VdbeOpList endCode[] = {
 91030  91382         { OP_AddImm,      1, 0,        0},    /* 0 */
 91031  91383         { OP_IfNeg,       1, 0,        0},    /* 1 */
 91032  91384         { OP_String8,     0, 3,        0},    /* 2 */
 91033  91385         { OP_ResultRow,   3, 1,        0},
 91034  91386       };
 91035  91387   
 91036         -    int isQuick = (zLeft[0]=='q');
        91388  +    int isQuick = (sqlite3Tolower(zLeft[0])=='q');
 91037  91389   
 91038  91390       /* Initialize the VDBE program */
 91039  91391       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 91040  91392       pParse->nMem = 6;
 91041  91393       sqlite3VdbeSetNumCols(v, 1);
 91042  91394       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
 91043  91395   
................................................................................
 92404  92756   ){
 92405  92757     Select *pNew;
 92406  92758     Select standin;
 92407  92759     sqlite3 *db = pParse->db;
 92408  92760     pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
 92409  92761     assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
 92410  92762     if( pNew==0 ){
        92763  +    assert( db->mallocFailed );
 92411  92764       pNew = &standin;
 92412  92765       memset(pNew, 0, sizeof(*pNew));
 92413  92766     }
 92414  92767     if( pEList==0 ){
 92415  92768       pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
 92416  92769     }
 92417  92770     pNew->pEList = pEList;
................................................................................
 92431  92784     if( db->mallocFailed ) {
 92432  92785       clearSelect(db, pNew);
 92433  92786       if( pNew!=&standin ) sqlite3DbFree(db, pNew);
 92434  92787       pNew = 0;
 92435  92788     }else{
 92436  92789       assert( pNew->pSrc!=0 || pParse->nErr>0 );
 92437  92790     }
        92791  +  assert( pNew!=&standin );
 92438  92792     return pNew;
 92439  92793   }
 92440  92794   
 92441  92795   /*
 92442  92796   ** Delete the given Select structure and all of its substructures.
 92443  92797   */
 92444  92798   SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
................................................................................
 93609  93963                  || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
 93610  93964       if( (zName = pEList->a[i].zName)!=0 ){
 93611  93965         /* If the column contains an "AS <name>" phrase, use <name> as the name */
 93612  93966         zName = sqlite3DbStrDup(db, zName);
 93613  93967       }else{
 93614  93968         Expr *pColExpr = p;  /* The expression that is the result column name */
 93615  93969         Table *pTab;         /* Table associated with this expression */
 93616         -      while( pColExpr->op==TK_DOT ) pColExpr = pColExpr->pRight;
        93970  +      while( pColExpr->op==TK_DOT ){
        93971  +        pColExpr = pColExpr->pRight;
        93972  +        assert( pColExpr!=0 );
        93973  +      }
 93617  93974         if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
 93618  93975           /* For columns use the column name name */
 93619  93976           int iCol = pColExpr->iColumn;
 93620  93977           pTab = pColExpr->pTab;
 93621  93978           if( iCol<0 ) iCol = pTab->iPKey;
 93622  93979           zName = sqlite3MPrintf(db, "%s",
 93623  93980                    iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
................................................................................
 98607  98964           if( pIdx->onError==OE_Replace ){
 98608  98965             openAll = 1;
 98609  98966             break;
 98610  98967           }
 98611  98968         }
 98612  98969       }
 98613  98970       for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
        98971  +      assert( aRegIdx );
 98614  98972         if( openAll || aRegIdx[i]>0 ){
 98615  98973           KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
 98616  98974           sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
 98617  98975                          (char*)pKey, P4_KEYINFO_HANDOFF);
 98618  98976           assert( pParse->nTab>iCur+i+1 );
 98619  98977         }
 98620  98978       }
................................................................................
 98780  99138     ** all record selected by the WHERE clause have been updated.
 98781  99139     */
 98782  99140     sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
 98783  99141     sqlite3VdbeJumpHere(v, addr);
 98784  99142   
 98785  99143     /* Close all tables */
 98786  99144     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
        99145  +    assert( aRegIdx );
 98787  99146       if( openAll || aRegIdx[i]>0 ){
 98788  99147         sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
 98789  99148       }
 98790  99149     }
 98791  99150     sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
 98792  99151   
 98793  99152     /* Update the sqlite_sequence table by storing the content of the
................................................................................
 98967  99326       return SQLITE_NOMEM;
 98968  99327     }
 98969  99328     if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
 98970  99329       sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
 98971  99330       return sqlite3_errcode(db);
 98972  99331     }
 98973  99332     VVA_ONLY( rc = ) sqlite3_step(pStmt);
 98974         -  assert( rc!=SQLITE_ROW );
        99333  +  assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
 98975  99334     return vacuumFinalize(db, pStmt, pzErrMsg);
 98976  99335   }
 98977  99336   
 98978  99337   /*
 98979  99338   ** Execute zSql on database db. The statement returns exactly
 98980  99339   ** one column. Execute this as SQL on the same database.
 98981  99340   */
................................................................................
 99185  99544         "  SELECT type, name, tbl_name, rootpage, sql"
 99186  99545         "    FROM main.sqlite_master"
 99187  99546         "   WHERE type='view' OR type='trigger'"
 99188  99547         "      OR (type='table' AND rootpage=0)"
 99189  99548     );
 99190  99549     if( rc ) goto end_of_vacuum;
 99191  99550   
 99192         -  /* At this point, unless the main db was completely empty, there is now a
 99193         -  ** transaction open on the vacuum database, but not on the main database.
 99194         -  ** Open a btree level transaction on the main database. This allows a
 99195         -  ** call to sqlite3BtreeCopyFile(). The main database btree level
 99196         -  ** transaction is then committed, so the SQL level never knows it was
 99197         -  ** opened for writing. This way, the SQL transaction used to create the
 99198         -  ** temporary database never needs to be committed.
        99551  +  /* At this point, there is a write transaction open on both the 
        99552  +  ** vacuum database and the main database. Assuming no error occurs,
        99553  +  ** both transactions are closed by this block - the main database
        99554  +  ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
        99555  +  ** call to sqlite3BtreeCommit().
 99199  99556     */
 99200  99557     {
 99201  99558       u32 meta;
 99202  99559       int i;
 99203  99560   
 99204  99561       /* This array determines which meta meta values are preserved in the
 99205  99562       ** vacuum.  Even entries are the meta value number and odd entries
................................................................................
100160 100517     assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
100161 100518     assert( iSavepoint>=0 );
100162 100519     if( db->aVTrans ){
100163 100520       int i;
100164 100521       for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
100165 100522         VTable *pVTab = db->aVTrans[i];
100166 100523         const sqlite3_module *pMod = pVTab->pMod->pModule;
100167         -      if( pMod->iVersion>=2 ){
       100524  +      if( pVTab->pVtab && pMod->iVersion>=2 ){
100168 100525           int (*xMethod)(sqlite3_vtab *, int);
100169 100526           switch( op ){
100170 100527             case SAVEPOINT_BEGIN:
100171 100528               xMethod = pMod->xSavepoint;
100172 100529               pVTab->iSavepoint = iSavepoint+1;
100173 100530               break;
100174 100531             case SAVEPOINT_ROLLBACK:
................................................................................
100175 100532               xMethod = pMod->xRollbackTo;
100176 100533               break;
100177 100534             default:
100178 100535               xMethod = pMod->xRelease;
100179 100536               break;
100180 100537           }
100181 100538           if( xMethod && pVTab->iSavepoint>iSavepoint ){
100182         -          rc = xMethod(db->aVTrans[i]->pVtab, iSavepoint);
       100539  +          rc = xMethod(pVTab->pVtab, iSavepoint);
100183 100540           }
100184 100541         }
100185 100542       }
100186 100543     }
100187 100544     return rc;
100188 100545   }
100189 100546   
................................................................................
100455 100812   #define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
100456 100813   #define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
100457 100814   #define TERM_CODED      0x04   /* This term is already coded */
100458 100815   #define TERM_COPIED     0x08   /* Has a child */
100459 100816   #define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
100460 100817   #define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
100461 100818   #define TERM_OR_OK      0x40   /* Used during OR-clause processing */
100462         -#ifdef SQLITE_ENABLE_STAT2
       100819  +#ifdef SQLITE_ENABLE_STAT3
100463 100820   #  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
100464 100821   #else
100465         -#  define TERM_VNULL    0x00   /* Disabled if not using stat2 */
       100822  +#  define TERM_VNULL    0x00   /* Disabled if not using stat3 */
100466 100823   #endif
100467 100824   
100468 100825   /*
100469 100826   ** An instance of the following structure holds all information about a
100470 100827   ** WHERE clause.  Mostly this is a container for one or more WhereTerms.
       100828  +**
       100829  +** Explanation of pOuter:  For a WHERE clause of the form
       100830  +**
       100831  +**           a AND ((b AND c) OR (d AND e)) AND f
       100832  +**
       100833  +** There are separate WhereClause objects for the whole clause and for
       100834  +** the subclauses "(b AND c)" and "(d AND e)".  The pOuter field of the
       100835  +** subclauses points to the WhereClause object for the whole clause.
100471 100836   */
100472 100837   struct WhereClause {
100473 100838     Parse *pParse;           /* The parser context */
100474 100839     WhereMaskSet *pMaskSet;  /* Mapping of table cursor numbers to bitmasks */
100475 100840     Bitmask vmask;           /* Bitmask identifying virtual table cursors */
       100841  +  WhereClause *pOuter;     /* Outer conjunction */
100476 100842     u8 op;                   /* Split operator.  TK_AND or TK_OR */
       100843  +  u16 wctrlFlags;          /* Might include WHERE_AND_ONLY */
100477 100844     int nTerm;               /* Number of terms */
100478 100845     int nSlot;               /* Number of entries in a[] */
100479 100846     WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
100480 100847   #if defined(SQLITE_SMALL_STACK)
100481 100848     WhereTerm aStatic[1];    /* Initial static space for a[] */
100482 100849   #else
100483 100850     WhereTerm aStatic[8];    /* Initial static space for a[] */
................................................................................
100598 100965   
100599 100966   /*
100600 100967   ** Initialize a preallocated WhereClause structure.
100601 100968   */
100602 100969   static void whereClauseInit(
100603 100970     WhereClause *pWC,        /* The WhereClause to be initialized */
100604 100971     Parse *pParse,           /* The parsing context */
100605         -  WhereMaskSet *pMaskSet   /* Mapping from table cursor numbers to bitmasks */
       100972  +  WhereMaskSet *pMaskSet,  /* Mapping from table cursor numbers to bitmasks */
       100973  +  u16 wctrlFlags           /* Might include WHERE_AND_ONLY */
100606 100974   ){
100607 100975     pWC->pParse = pParse;
100608 100976     pWC->pMaskSet = pMaskSet;
       100977  +  pWC->pOuter = 0;
100609 100978     pWC->nTerm = 0;
100610 100979     pWC->nSlot = ArraySize(pWC->aStatic);
100611 100980     pWC->a = pWC->aStatic;
100612 100981     pWC->vmask = 0;
       100982  +  pWC->wctrlFlags = wctrlFlags;
100613 100983   }
100614 100984   
100615 100985   /* Forward reference */
100616 100986   static void whereClauseClear(WhereClause*);
100617 100987   
100618 100988   /*
100619 100989   ** Deallocate all memory associated with a WhereOrInfo object.
................................................................................
100921 101291     u32 op,               /* Mask of WO_xx values describing operator */
100922 101292     Index *pIdx           /* Must be compatible with this index, if not NULL */
100923 101293   ){
100924 101294     WhereTerm *pTerm;
100925 101295     int k;
100926 101296     assert( iCur>=0 );
100927 101297     op &= WO_ALL;
100928         -  for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
100929         -    if( pTerm->leftCursor==iCur
100930         -       && (pTerm->prereqRight & notReady)==0
100931         -       && pTerm->u.leftColumn==iColumn
100932         -       && (pTerm->eOperator & op)!=0
100933         -    ){
100934         -      if( pIdx && pTerm->eOperator!=WO_ISNULL ){
100935         -        Expr *pX = pTerm->pExpr;
100936         -        CollSeq *pColl;
100937         -        char idxaff;
100938         -        int j;
100939         -        Parse *pParse = pWC->pParse;
100940         -
100941         -        idxaff = pIdx->pTable->aCol[iColumn].affinity;
100942         -        if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
100943         -
100944         -        /* Figure out the collation sequence required from an index for
100945         -        ** it to be useful for optimising expression pX. Store this
100946         -        ** value in variable pColl.
100947         -        */
100948         -        assert(pX->pLeft);
100949         -        pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
100950         -        assert(pColl || pParse->nErr);
100951         -
100952         -        for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
100953         -          if( NEVER(j>=pIdx->nColumn) ) return 0;
100954         -        }
100955         -        if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
100956         -      }
100957         -      return pTerm;
       101298  +  for(; pWC; pWC=pWC->pOuter){
       101299  +    for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
       101300  +      if( pTerm->leftCursor==iCur
       101301  +         && (pTerm->prereqRight & notReady)==0
       101302  +         && pTerm->u.leftColumn==iColumn
       101303  +         && (pTerm->eOperator & op)!=0
       101304  +      ){
       101305  +        if( pIdx && pTerm->eOperator!=WO_ISNULL ){
       101306  +          Expr *pX = pTerm->pExpr;
       101307  +          CollSeq *pColl;
       101308  +          char idxaff;
       101309  +          int j;
       101310  +          Parse *pParse = pWC->pParse;
       101311  +  
       101312  +          idxaff = pIdx->pTable->aCol[iColumn].affinity;
       101313  +          if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
       101314  +  
       101315  +          /* Figure out the collation sequence required from an index for
       101316  +          ** it to be useful for optimising expression pX. Store this
       101317  +          ** value in variable pColl.
       101318  +          */
       101319  +          assert(pX->pLeft);
       101320  +          pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
       101321  +          assert(pColl || pParse->nErr);
       101322  +  
       101323  +          for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
       101324  +            if( NEVER(j>=pIdx->nColumn) ) return 0;
       101325  +          }
       101326  +          if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
       101327  +        }
       101328  +        return pTerm;
       101329  +      }
100958 101330       }
100959 101331     }
100960 101332     return 0;
100961 101333   }
100962 101334   
100963 101335   /* Forward reference */
100964 101336   static void exprAnalyze(SrcList*, WhereClause*, int);
................................................................................
101027 101399     if( op==TK_VARIABLE ){
101028 101400       Vdbe *pReprepare = pParse->pReprepare;
101029 101401       int iCol = pRight->iColumn;
101030 101402       pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE);
101031 101403       if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
101032 101404         z = (char *)sqlite3_value_text(pVal);
101033 101405       }
101034         -    sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-23257-02778 */
       101406  +    sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
101035 101407       assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
101036 101408     }else if( op==TK_STRING ){
101037 101409       z = pRight->u.zToken;
101038 101410     }
101039 101411     if( z ){
101040 101412       cnt = 0;
101041 101413       while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
................................................................................
101045 101417         Expr *pPrefix;
101046 101418         *pisComplete = c==wc[0] && z[cnt+1]==0;
101047 101419         pPrefix = sqlite3Expr(db, TK_STRING, z);
101048 101420         if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
101049 101421         *ppPrefix = pPrefix;
101050 101422         if( op==TK_VARIABLE ){
101051 101423           Vdbe *v = pParse->pVdbe;
101052         -        sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-23257-02778 */
       101424  +        sqlite3VdbeSetVarmask(v, pRight->iColumn);
101053 101425           if( *pisComplete && pRight->u.zToken[1] ){
101054 101426             /* If the rhs of the LIKE expression is a variable, and the current
101055 101427             ** value of the variable means there is no need to invoke the LIKE
101056 101428             ** function, then no OP_Variable will be added to the program.
101057 101429             ** This causes problems for the sqlite3_bind_parameter_name()
101058 101430             ** API. To workaround them, add a dummy OP_Variable here.
101059 101431             */ 
................................................................................
101214 101586     */
101215 101587     assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
101216 101588     assert( pExpr->op==TK_OR );
101217 101589     pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
101218 101590     if( pOrInfo==0 ) return;
101219 101591     pTerm->wtFlags |= TERM_ORINFO;
101220 101592     pOrWc = &pOrInfo->wc;
101221         -  whereClauseInit(pOrWc, pWC->pParse, pMaskSet);
       101593  +  whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags);
101222 101594     whereSplit(pOrWc, pExpr, TK_OR);
101223 101595     exprAnalyzeAll(pSrc, pOrWc);
101224 101596     if( db->mallocFailed ) return;
101225 101597     assert( pOrWc->nTerm>=2 );
101226 101598   
101227 101599     /*
101228 101600     ** Compute the set of tables that might satisfy cases 1 or 2.
................................................................................
101241 101613           WhereTerm *pAndTerm;
101242 101614           int j;
101243 101615           Bitmask b = 0;
101244 101616           pOrTerm->u.pAndInfo = pAndInfo;
101245 101617           pOrTerm->wtFlags |= TERM_ANDINFO;
101246 101618           pOrTerm->eOperator = WO_AND;
101247 101619           pAndWC = &pAndInfo->wc;
101248         -        whereClauseInit(pAndWC, pWC->pParse, pMaskSet);
       101620  +        whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags);
101249 101621           whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
101250 101622           exprAnalyzeAll(pSrc, pAndWC);
       101623  +        pAndWC->pOuter = pWC;
101251 101624           testcase( db->mallocFailed );
101252 101625           if( !db->mallocFailed ){
101253 101626             for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
101254 101627               assert( pAndTerm->pExpr );
101255 101628               if( allowedOp(pAndTerm->pExpr->op) ){
101256 101629                 b |= getMask(pMaskSet, pAndTerm->leftCursor);
101257 101630               }
................................................................................
101677 102050         pTerm->nChild = 1;
101678 102051         pTerm->wtFlags |= TERM_COPIED;
101679 102052         pNewTerm->prereqAll = pTerm->prereqAll;
101680 102053       }
101681 102054     }
101682 102055   #endif /* SQLITE_OMIT_VIRTUALTABLE */
101683 102056   
101684         -#ifdef SQLITE_ENABLE_STAT2
101685         -  /* When sqlite_stat2 histogram data is available an operator of the
       102057  +#ifdef SQLITE_ENABLE_STAT3
       102058  +  /* When sqlite_stat3 histogram data is available an operator of the
101686 102059     ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
101687 102060     ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
101688 102061     ** virtual term of that form.
101689 102062     **
101690 102063     ** Note that the virtual term must be tagged with TERM_VNULL.  This
101691 102064     ** TERM_VNULL tag will suppress the not-null check at the beginning
101692 102065     ** of the loop.  Without the TERM_VNULL flag, the not-null check at
................................................................................
101716 102089         pNewTerm->iParent = idxTerm;
101717 102090         pTerm = &pWC->a[idxTerm];
101718 102091         pTerm->nChild = 1;
101719 102092         pTerm->wtFlags |= TERM_COPIED;
101720 102093         pNewTerm->prereqAll = pTerm->prereqAll;
101721 102094       }
101722 102095     }
101723         -#endif /* SQLITE_ENABLE_STAT2 */
       102096  +#endif /* SQLITE_ENABLE_STAT */
101724 102097   
101725 102098     /* Prevent ON clause terms of a LEFT JOIN from being used to drive
101726 102099     ** an index for tables to the left of the join.
101727 102100     */
101728 102101     pTerm->prereqRight |= extraRight;
101729 102102   }
101730 102103   
................................................................................
102138 102511   ){
102139 102512   #ifndef SQLITE_OMIT_OR_OPTIMIZATION
102140 102513     const int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
102141 102514     const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur);  /* Bitmask for pSrc */
102142 102515     WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm];        /* End of pWC->a[] */
102143 102516     WhereTerm *pTerm;                 /* A single term of the WHERE clause */
102144 102517   
102145         -  /* No OR-clause optimization allowed if the INDEXED BY or NOT INDEXED clauses
102146         -  ** are used */
       102518  +  /* The OR-clause optimization is disallowed if the INDEXED BY or
       102519  +  ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
102147 102520     if( pSrc->notIndexed || pSrc->pIndex!=0 ){
102148 102521       return;
       102522  +  }
       102523  +  if( pWC->wctrlFlags & WHERE_AND_ONLY ){
       102524  +    return;
102149 102525     }
102150 102526   
102151 102527     /* Search the WHERE clause terms for a usable WO_OR term. */
102152 102528     for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
102153 102529       if( pTerm->eOperator==WO_OR 
102154 102530        && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
102155 102531        && (pTerm->u.pOrInfo->indexable & maskSrc)!=0 
................................................................................
102170 102546           if( pOrTerm->eOperator==WO_AND ){
102171 102547             WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc;
102172 102548             bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
102173 102549           }else if( pOrTerm->leftCursor==iCur ){
102174 102550             WhereClause tempWC;
102175 102551             tempWC.pParse = pWC->pParse;
102176 102552             tempWC.pMaskSet = pWC->pMaskSet;
       102553  +          tempWC.pOuter = pWC;
102177 102554             tempWC.op = TK_AND;
102178 102555             tempWC.a = pOrTerm;
       102556  +          tempWC.wctrlFlags = 0;
102179 102557             tempWC.nTerm = 1;
102180 102558             bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
102181 102559           }else{
102182 102560             continue;
102183 102561           }
102184 102562           rTotal += sTermCost.rCost;
102185 102563           nRow += sTermCost.plan.nRow;
................................................................................
102764 103142     /* Try to find a more efficient access pattern by using multiple indexes
102765 103143     ** to optimize an OR expression within the WHERE clause. 
102766 103144     */
102767 103145     bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
102768 103146   }
102769 103147   #endif /* SQLITE_OMIT_VIRTUALTABLE */
102770 103148   
       103149  +#ifdef SQLITE_ENABLE_STAT3
102771 103150   /*
102772         -** Argument pIdx is a pointer to an index structure that has an array of
102773         -** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
102774         -** stored in Index.aSample. These samples divide the domain of values stored
102775         -** the index into (SQLITE_INDEX_SAMPLES+1) regions.
102776         -** Region 0 contains all values less than the first sample value. Region
102777         -** 1 contains values between the first and second samples.  Region 2 contains
102778         -** values between samples 2 and 3.  And so on.  Region SQLITE_INDEX_SAMPLES
102779         -** contains values larger than the last sample.
       103151  +** Estimate the location of a particular key among all keys in an
       103152  +** index.  Store the results in aStat as follows:
102780 103153   **
102781         -** If the index contains many duplicates of a single value, then it is
102782         -** possible that two or more adjacent samples can hold the same value.
102783         -** When that is the case, the smallest possible region code is returned
102784         -** when roundUp is false and the largest possible region code is returned
102785         -** when roundUp is true.
       103154  +**    aStat[0]      Est. number of rows less than pVal
       103155  +**    aStat[1]      Est. number of rows equal to pVal
102786 103156   **
102787         -** If successful, this function determines which of the regions value 
102788         -** pVal lies in, sets *piRegion to the region index (a value between 0
102789         -** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
102790         -** Or, if an OOM occurs while converting text values between encodings,
102791         -** SQLITE_NOMEM is returned and *piRegion is undefined.
       103157  +** Return SQLITE_OK on success.
102792 103158   */
102793         -#ifdef SQLITE_ENABLE_STAT2
102794         -static int whereRangeRegion(
       103159  +static int whereKeyStats(
102795 103160     Parse *pParse,              /* Database connection */
102796 103161     Index *pIdx,                /* Index to consider domain of */
102797 103162     sqlite3_value *pVal,        /* Value to consider */
102798         -  int roundUp,                /* Return largest valid region if true */
102799         -  int *piRegion               /* OUT: Region of domain in which value lies */
       103163  +  int roundUp,                /* Round up if true.  Round down if false */
       103164  +  tRowcnt *aStat              /* OUT: stats written here */
102800 103165   ){
       103166  +  tRowcnt n;
       103167  +  IndexSample *aSample;
       103168  +  int i, eType;
       103169  +  int isEq = 0;
       103170  +  i64 v;
       103171  +  double r, rS;
       103172  +
102801 103173     assert( roundUp==0 || roundUp==1 );
102802         -  if( ALWAYS(pVal) ){
102803         -    IndexSample *aSample = pIdx->aSample;
102804         -    int i = 0;
102805         -    int eType = sqlite3_value_type(pVal);
102806         -
102807         -    if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
102808         -      double r = sqlite3_value_double(pVal);
102809         -      for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
102810         -        if( aSample[i].eType==SQLITE_NULL ) continue;
102811         -        if( aSample[i].eType>=SQLITE_TEXT ) break;
102812         -        if( roundUp ){
102813         -          if( aSample[i].u.r>r ) break;
102814         -        }else{
102815         -          if( aSample[i].u.r>=r ) break;
102816         -        }
102817         -      }
102818         -    }else if( eType==SQLITE_NULL ){
102819         -      i = 0;
102820         -      if( roundUp ){
102821         -        while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
102822         -      }
102823         -    }else{ 
       103174  +  assert( pIdx->nSample>0 );
       103175  +  if( pVal==0 ) return SQLITE_ERROR;
       103176  +  n = pIdx->aiRowEst[0];
       103177  +  aSample = pIdx->aSample;
       103178  +  eType = sqlite3_value_type(pVal);
       103179  +
       103180  +  if( eType==SQLITE_INTEGER ){
       103181  +    v = sqlite3_value_int64(pVal);
       103182  +    r = (i64)v;
       103183  +    for(i=0; i<pIdx->nSample; i++){
       103184  +      if( aSample[i].eType==SQLITE_NULL ) continue;
       103185  +      if( aSample[i].eType>=SQLITE_TEXT ) break;
       103186  +      if( aSample[i].eType==SQLITE_INTEGER ){
       103187  +        if( aSample[i].u.i>=v ){
       103188  +          isEq = aSample[i].u.i==v;
       103189  +          break;
       103190  +        }
       103191  +      }else{
       103192  +        assert( aSample[i].eType==SQLITE_FLOAT );
       103193  +        if( aSample[i].u.r>=r ){
       103194  +          isEq = aSample[i].u.r==r;
       103195  +          break;
       103196  +        }
       103197  +      }
       103198  +    }
       103199  +  }else if( eType==SQLITE_FLOAT ){
       103200  +    r = sqlite3_value_double(pVal);
       103201  +    for(i=0; i<pIdx->nSample; i++){
       103202  +      if( aSample[i].eType==SQLITE_NULL ) continue;
       103203  +      if( aSample[i].eType>=SQLITE_TEXT ) break;
       103204  +      if( aSample[i].eType==SQLITE_FLOAT ){
       103205  +        rS = aSample[i].u.r;
       103206  +      }else{
       103207  +        rS = aSample[i].u.i;
       103208  +      }
       103209  +      if( rS>=r ){
       103210  +        isEq = rS==r;
       103211  +        break;
       103212  +      }
       103213  +    }
       103214  +  }else if( eType==SQLITE_NULL ){
       103215  +    i = 0;
       103216  +    if( aSample[0].eType==SQLITE_NULL ) isEq = 1;
       103217  +  }else{
       103218  +    assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
       103219  +    for(i=0; i<pIdx->nSample; i++){
       103220  +      if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
       103221  +        break;
       103222  +      }
       103223  +    }
       103224  +    if( i<pIdx->nSample ){      
102824 103225         sqlite3 *db = pParse->db;
102825 103226         CollSeq *pColl;
102826 103227         const u8 *z;
102827         -      int n;
102828         -
102829         -      /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
102830         -      assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
102831         -
102832 103228         if( eType==SQLITE_BLOB ){
102833 103229           z = (const u8 *)sqlite3_value_blob(pVal);
102834 103230           pColl = db->pDfltColl;
102835 103231           assert( pColl->enc==SQLITE_UTF8 );
102836 103232         }else{
102837 103233           pColl = sqlite3GetCollSeq(db, SQLITE_UTF8, 0, *pIdx->azColl);
102838 103234           if( pColl==0 ){
................................................................................
102843 103239           z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
102844 103240           if( !z ){
102845 103241             return SQLITE_NOMEM;
102846 103242           }
102847 103243           assert( z && pColl && pColl->xCmp );
102848 103244         }
102849 103245         n = sqlite3ValueBytes(pVal, pColl->enc);
102850         -
102851         -      for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
       103246  +  
       103247  +      for(; i<pIdx->nSample; i++){
102852 103248           int c;
102853 103249           int eSampletype = aSample[i].eType;
102854         -        if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
102855         -        if( (eSampletype!=eType) ) break;
       103250  +        if( eSampletype<eType ) continue;
       103251  +        if( eSampletype!=eType ) break;
102856 103252   #ifndef SQLITE_OMIT_UTF16
102857 103253           if( pColl->enc!=SQLITE_UTF8 ){
102858 103254             int nSample;
102859 103255             char *zSample = sqlite3Utf8to16(
102860 103256                 db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
102861 103257             );
102862 103258             if( !zSample ){
................................................................................
102866 103262             c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
102867 103263             sqlite3DbFree(db, zSample);
102868 103264           }else
102869 103265   #endif
102870 103266           {
102871 103267             c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
102872 103268           }
102873         -        if( c-roundUp>=0 ) break;
102874         -      }
102875         -    }
102876         -
102877         -    assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
102878         -    *piRegion = i;
102879         -  }
102880         -  return SQLITE_OK;
102881         -}
102882         -#endif   /* #ifdef SQLITE_ENABLE_STAT2 */
       103269  +        if( c>=0 ){
       103270  +          if( c==0 ) isEq = 1;
       103271  +          break;
       103272  +        }
       103273  +      }
       103274  +    }
       103275  +  }
       103276  +
       103277  +  /* At this point, aSample[i] is the first sample that is greater than
       103278  +  ** or equal to pVal.  Or if i==pIdx->nSample, then all samples are less
       103279  +  ** than pVal.  If aSample[i]==pVal, then isEq==1.
       103280  +  */
       103281  +  if( isEq ){
       103282  +    assert( i<pIdx->nSample );
       103283  +    aStat[0] = aSample[i].nLt;
       103284  +    aStat[1] = aSample[i].nEq;
       103285  +  }else{
       103286  +    tRowcnt iLower, iUpper, iGap;
       103287  +    if( i==0 ){
       103288  +      iLower = 0;
       103289  +      iUpper = aSample[0].nLt;
       103290  +    }else{
       103291  +      iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
       103292  +      iLower = aSample[i-1].nEq + aSample[i-1].nLt;
       103293  +    }
       103294  +    aStat[1] = pIdx->avgEq;
       103295  +    if( iLower>=iUpper ){
       103296  +      iGap = 0;
       103297  +    }else{
       103298  +      iGap = iUpper - iLower;
       103299  +    }
       103300  +    if( roundUp ){
       103301  +      iGap = (iGap*2)/3;
       103302  +    }else{
       103303  +      iGap = iGap/3;
       103304  +    }
       103305  +    aStat[0] = iLower + iGap;
       103306  +  }
       103307  +  return SQLITE_OK;
       103308  +}
       103309  +#endif /* SQLITE_ENABLE_STAT3 */
102883 103310   
102884 103311   /*
102885 103312   ** If expression pExpr represents a literal value, set *pp to point to
102886 103313   ** an sqlite3_value structure containing the same value, with affinity
102887 103314   ** aff applied to it, before returning. It is the responsibility of the 
102888 103315   ** caller to eventually release this structure by passing it to 
102889 103316   ** sqlite3ValueFree().
................................................................................
102893 103320   ** create an sqlite3_value structure containing this value, again with
102894 103321   ** affinity aff applied to it, instead.
102895 103322   **
102896 103323   ** If neither of the above apply, set *pp to NULL.
102897 103324   **
102898 103325   ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
102899 103326   */
102900         -#ifdef SQLITE_ENABLE_STAT2
       103327  +#ifdef SQLITE_ENABLE_STAT3
102901 103328   static int valueFromExpr(
102902 103329     Parse *pParse, 
102903 103330     Expr *pExpr, 
102904 103331     u8 aff, 
102905 103332     sqlite3_value **pp
102906 103333   ){
102907 103334     if( pExpr->op==TK_VARIABLE
102908 103335      || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
102909 103336     ){
102910 103337       int iVar = pExpr->iColumn;
102911         -    sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-23257-02778 */
       103338  +    sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
102912 103339       *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff);
102913 103340       return SQLITE_OK;
102914 103341     }
102915 103342     return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
102916 103343   }
102917 103344   #endif
102918 103345   
................................................................................
102941 103368   ** then nEq should be passed the value 1 (as the range restricted column,
102942 103369   ** b, is the second left-most column of the index). Or, if the query is:
102943 103370   **
102944 103371   **   ... FROM t1 WHERE a > ? AND a < ? ...
102945 103372   **
102946 103373   ** then nEq should be passed 0.
102947 103374   **
102948         -** The returned value is an integer between 1 and 100, inclusive. A return
102949         -** value of 1 indicates that the proposed range scan is expected to visit
102950         -** approximately 1/100th (1%) of the rows selected by the nEq equality
102951         -** constraints (if any). A return value of 100 indicates that it is expected
102952         -** that the range scan will visit every row (100%) selected by the equality
102953         -** constraints.
       103375  +** The returned value is an integer divisor to reduce the estimated
       103376  +** search space.  A return value of 1 means that range constraints are
       103377  +** no help at all.  A return value of 2 means range constraints are
       103378  +** expected to reduce the search space by half.  And so forth...
102954 103379   **
102955         -** In the absence of sqlite_stat2 ANALYZE data, each range inequality
102956         -** reduces the search space by 3/4ths.  Hence a single constraint (x>?)
102957         -** results in a return of 25 and a range constraint (x>? AND x<?) results
102958         -** in a return of 6.
       103380  +** In the absence of sqlite_stat3 ANALYZE data, each range inequality
       103381  +** reduces the search space by a factor of 4.  Hence a single constraint (x>?)
       103382  +** results in a return of 4 and a range constraint (x>? AND x<?) results
       103383  +** in a return of 16.
102959 103384   */
102960 103385   static int whereRangeScanEst(
102961 103386     Parse *pParse,       /* Parsing & code generating context */
102962 103387     Index *p,            /* The index containing the range-compared column; "x" */
102963 103388     int nEq,             /* index into p->aCol[] of the range-compared column */
102964 103389     WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
102965 103390     WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
102966         -  int *piEst           /* OUT: Return value */
       103391  +  double *pRangeDiv   /* OUT: Reduce search space by this divisor */
102967 103392   ){
102968 103393     int rc = SQLITE_OK;
102969 103394   
102970         -#ifdef SQLITE_ENABLE_STAT2
       103395  +#ifdef SQLITE_ENABLE_STAT3
102971 103396   
102972         -  if( nEq==0 && p->aSample ){
102973         -    sqlite3_value *pLowerVal = 0;
102974         -    sqlite3_value *pUpperVal = 0;
102975         -    int iEst;
102976         -    int iLower = 0;
102977         -    int iUpper = SQLITE_INDEX_SAMPLES;
102978         -    int roundUpUpper = 0;
102979         -    int roundUpLower = 0;
       103397  +  if( nEq==0 && p->nSample ){
       103398  +    sqlite3_value *pRangeVal;
       103399  +    tRowcnt iLower = 0;
       103400  +    tRowcnt iUpper = p->aiRowEst[0];
       103401  +    tRowcnt a[2];
102980 103402       u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
102981 103403   
102982 103404       if( pLower ){
102983 103405         Expr *pExpr = pLower->pExpr->pRight;
102984         -      rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
       103406  +      rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
102985 103407         assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
102986         -      roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
       103408  +      if( rc==SQLITE_OK
       103409  +       && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
       103410  +      ){
       103411  +        iLower = a[0];
       103412  +        if( pLower->eOperator==WO_GT ) iLower += a[1];
       103413  +      }
       103414  +      sqlite3ValueFree(pRangeVal);
102987 103415       }
102988 103416       if( rc==SQLITE_OK && pUpper ){
102989 103417         Expr *pExpr = pUpper->pExpr->pRight;
102990         -      rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
       103418  +      rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
102991 103419         assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
102992         -      roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
102993         -    }
102994         -
102995         -    if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
102996         -      sqlite3ValueFree(pLowerVal);
102997         -      sqlite3ValueFree(pUpperVal);
102998         -      goto range_est_fallback;
102999         -    }else if( pLowerVal==0 ){
103000         -      rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
103001         -      if( pLower ) iLower = iUpper/2;
103002         -    }else if( pUpperVal==0 ){
103003         -      rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
103004         -      if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
103005         -    }else{
103006         -      rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
103007         -      if( rc==SQLITE_OK ){
103008         -        rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
103009         -      }
103010         -    }
103011         -    WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
103012         -
103013         -    iEst = iUpper - iLower;
103014         -    testcase( iEst==SQLITE_INDEX_SAMPLES );
103015         -    assert( iEst<=SQLITE_INDEX_SAMPLES );
103016         -    if( iEst<1 ){
103017         -      *piEst = 50/SQLITE_INDEX_SAMPLES;
103018         -    }else{
103019         -      *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
103020         -    }
103021         -    sqlite3ValueFree(pLowerVal);
103022         -    sqlite3ValueFree(pUpperVal);
103023         -    return rc;
103024         -  }
103025         -range_est_fallback:
       103420  +      if( rc==SQLITE_OK
       103421  +       && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
       103422  +      ){
       103423  +        iUpper = a[0];
       103424  +        if( pUpper->eOperator==WO_LE ) iUpper += a[1];
       103425  +      }
       103426  +      sqlite3ValueFree(pRangeVal);
       103427  +    }
       103428  +    if( rc==SQLITE_OK ){
       103429  +      if( iUpper<=iLower ){
       103430  +        *pRangeDiv = (double)p->aiRowEst[0];
       103431  +      }else{
       103432  +        *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower);
       103433  +      }
       103434  +      WHERETRACE(("range scan regions: %u..%u  div=%g\n",
       103435  +                  (u32)iLower, (u32)iUpper, *pRangeDiv));
       103436  +      return SQLITE_OK;
       103437  +    }
       103438  +  }
103026 103439   #else
103027 103440     UNUSED_PARAMETER(pParse);
103028 103441     UNUSED_PARAMETER(p);
103029 103442     UNUSED_PARAMETER(nEq);
103030 103443   #endif
103031 103444     assert( pLower || pUpper );
103032         -  *piEst = 100;
103033         -  if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
103034         -  if( pUpper ) *piEst /= 4;
       103445  +  *pRangeDiv = (double)1;
       103446  +  if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
       103447  +  if( pUpper ) *pRangeDiv *= (double)4;
103035 103448     return rc;
103036 103449   }
103037 103450   
103038         -#ifdef SQLITE_ENABLE_STAT2
       103451  +#ifdef SQLITE_ENABLE_STAT3
103039 103452   /*
103040 103453   ** Estimate the number of rows that will be returned based on
103041 103454   ** an equality constraint x=VALUE and where that VALUE occurs in
103042 103455   ** the histogram data.  This only works when x is the left-most
103043         -** column of an index and sqlite_stat2 histogram data is available
       103456  +** column of an index and sqlite_stat3 histogram data is available
103044 103457   ** for that index.  When pExpr==NULL that means the constraint is
103045 103458   ** "x IS NULL" instead of "x=VALUE".
103046 103459   **
103047 103460   ** Write the estimated row count into *pnRow and return SQLITE_OK. 
103048 103461   ** If unable to make an estimate, leave *pnRow unchanged and return
103049 103462   ** non-zero.
103050 103463   **
................................................................................
103056 103469   static int whereEqualScanEst(
103057 103470     Parse *pParse,       /* Parsing & code generating context */
103058 103471     Index *p,            /* The index whose left-most column is pTerm */
103059 103472     Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
103060 103473     double *pnRow        /* Write the revised row estimate here */
103061 103474   ){
103062 103475     sqlite3_value *pRhs = 0;  /* VALUE on right-hand side of pTerm */
103063         -  int iLower, iUpper;       /* Range of histogram regions containing pRhs */
103064 103476     u8 aff;                   /* Column affinity */
103065 103477     int rc;                   /* Subfunction return code */
103066         -  double nRowEst;           /* New estimate of the number of rows */
       103478  +  tRowcnt a[2];             /* Statistics */
103067 103479   
103068 103480     assert( p->aSample!=0 );
       103481  +  assert( p->nSample>0 );
103069 103482     aff = p->pTable->aCol[p->aiColumn[0]].affinity;
103070 103483     if( pExpr ){
103071 103484       rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
103072 103485       if( rc ) goto whereEqualScanEst_cancel;
103073 103486     }else{
103074 103487       pRhs = sqlite3ValueNew(pParse->db);
103075 103488     }
103076 103489     if( pRhs==0 ) return SQLITE_NOTFOUND;
103077         -  rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
103078         -  if( rc ) goto whereEqualScanEst_cancel;
103079         -  rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
103080         -  if( rc ) goto whereEqualScanEst_cancel;
103081         -  WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
103082         -  if( iLower>=iUpper ){
103083         -    nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
103084         -    if( nRowEst<*pnRow ) *pnRow = nRowEst;
103085         -  }else{
103086         -    nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
103087         -    *pnRow = nRowEst;
       103490  +  rc = whereKeyStats(pParse, p, pRhs, 0, a);
       103491  +  if( rc==SQLITE_OK ){
       103492  +    WHERETRACE(("equality scan regions: %d\n", (int)a[1]));
       103493  +    *pnRow = a[1];
103088 103494     }
103089         -
103090 103495   whereEqualScanEst_cancel:
103091 103496     sqlite3ValueFree(pRhs);
103092 103497     return rc;
103093 103498   }
103094         -#endif /* defined(SQLITE_ENABLE_STAT2) */
       103499  +#endif /* defined(SQLITE_ENABLE_STAT3) */
103095 103500   
103096         -#ifdef SQLITE_ENABLE_STAT2
       103501  +#ifdef SQLITE_ENABLE_STAT3
103097 103502   /*
103098 103503   ** Estimate the number of rows that will be returned based on
103099 103504   ** an IN constraint where the right-hand side of the IN operator
103100 103505   ** is a list of values.  Example:
103101 103506   **
103102 103507   **        WHERE x IN (1,2,3,4)
103103 103508   **
................................................................................
103112 103517   */
103113 103518   static int whereInScanEst(
103114 103519     Parse *pParse,       /* Parsing & code generating context */
103115 103520     Index *p,            /* The index whose left-most column is pTerm */
103116 103521     ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
103117 103522     double *pnRow        /* Write the revised row estimate here */
103118 103523   ){
103119         -  sqlite3_value *pVal = 0;  /* One value from list */
103120         -  int iLower, iUpper;       /* Range of histogram regions containing pRhs */
103121         -  u8 aff;                   /* Column affinity */
103122         -  int rc = SQLITE_OK;       /* Subfunction return code */
103123         -  double nRowEst;           /* New estimate of the number of rows */
103124         -  int nSpan = 0;            /* Number of histogram regions spanned */
103125         -  int nSingle = 0;          /* Histogram regions hit by a single value */
103126         -  int nNotFound = 0;        /* Count of values that are not constants */
103127         -  int i;                               /* Loop counter */
103128         -  u8 aSpan[SQLITE_INDEX_SAMPLES+1];    /* Histogram regions that are spanned */
103129         -  u8 aSingle[SQLITE_INDEX_SAMPLES+1];  /* Histogram regions hit once */
       103524  +  int rc = SQLITE_OK;         /* Subfunction return code */
       103525  +  double nEst;                /* Number of rows for a single term */
       103526  +  double nRowEst = (double)0; /* New estimate of the number of rows */
       103527  +  int i;                      /* Loop counter */
103130 103528   
103131 103529     assert( p->aSample!=0 );
103132         -  aff = p->pTable->aCol[p->aiColumn[0]].affinity;
103133         -  memset(aSpan, 0, sizeof(aSpan));
103134         -  memset(aSingle, 0, sizeof(aSingle));
103135         -  for(i=0; i<pList->nExpr; i++){
103136         -    sqlite3ValueFree(pVal);
103137         -    rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
103138         -    if( rc ) break;
103139         -    if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
103140         -      nNotFound++;
103141         -      continue;
103142         -    }
103143         -    rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
103144         -    if( rc ) break;
103145         -    rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
103146         -    if( rc ) break;
103147         -    if( iLower>=iUpper ){
103148         -      aSingle[iLower] = 1;
103149         -    }else{
103150         -      assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
103151         -      while( iLower<iUpper ) aSpan[iLower++] = 1;
103152         -    }
       103530  +  for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
       103531  +    nEst = p->aiRowEst[0];
       103532  +    rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
       103533  +    nRowEst += nEst;
103153 103534     }
103154 103535     if( rc==SQLITE_OK ){
103155         -    for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
103156         -      if( aSpan[i] ){
103157         -        nSpan++;
103158         -      }else if( aSingle[i] ){
103159         -        nSingle++;
103160         -      }
103161         -    }
103162         -    nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
103163         -               + nNotFound*p->aiRowEst[1];
103164 103536       if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
103165 103537       *pnRow = nRowEst;
103166         -    WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
103167         -                 nSpan, nSingle, nNotFound, nRowEst));
       103538  +    WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
103168 103539     }
103169         -  sqlite3ValueFree(pVal);
103170 103540     return rc;
103171 103541   }
103172         -#endif /* defined(SQLITE_ENABLE_STAT2) */
       103542  +#endif /* defined(SQLITE_ENABLE_STAT3) */
103173 103543   
103174 103544   
103175 103545   /*
103176 103546   ** Find the best query plan for accessing a particular table.  Write the
103177 103547   ** best query plan and its cost into the WhereCost object supplied as the
103178 103548   ** last parameter.
103179 103549   **
................................................................................
103212 103582   ){
103213 103583     int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
103214 103584     Index *pProbe;              /* An index we are evaluating */
103215 103585     Index *pIdx;                /* Copy of pProbe, or zero for IPK index */
103216 103586     int eqTermMask;             /* Current mask of valid equality operators */
103217 103587     int idxEqTermMask;          /* Index mask of valid equality operators */
103218 103588     Index sPk;                  /* A fake index object for the primary key */
103219         -  unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
       103589  +  tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
103220 103590     int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
103221 103591     int wsFlagMask;             /* Allowed flags in pCost->plan.wsFlag */
103222 103592   
103223 103593     /* Initialize the cost to a worst-case value */
103224 103594     memset(pCost, 0, sizeof(*pCost));
103225 103595     pCost->rCost = SQLITE_BIG_DBL;
103226 103596   
................................................................................
103267 103637       eqTermMask = WO_EQ|WO_IN;
103268 103638       pIdx = 0;
103269 103639     }
103270 103640   
103271 103641     /* Loop over all indices looking for the best one to use
103272 103642     */
103273 103643     for(; pProbe; pIdx=pProbe=pProbe->pNext){
103274         -    const unsigned int * const aiRowEst = pProbe->aiRowEst;
       103644  +    const tRowcnt * const aiRowEst = pProbe->aiRowEst;
103275 103645       double cost;                /* Cost of using pProbe */
103276 103646       double nRow;                /* Estimated number of rows in result set */
103277         -    double log10N;              /* base-10 logarithm of nRow (inexact) */
       103647  +    double log10N = (double)1;  /* base-10 logarithm of nRow (inexact) */
103278 103648       int rev;                    /* True to scan in reverse order */
103279 103649       int wsFlags = 0;
103280 103650       Bitmask used = 0;
103281 103651   
103282 103652       /* The following variables are populated based on the properties of
103283 103653       ** index being evaluated. They are then used to determine the expected
103284 103654       ** cost and number of rows returned.
................................................................................
103310 103680       **
103311 103681       **  bInEst:  
103312 103682       **    Set to true if there was at least one "x IN (SELECT ...)" term used 
103313 103683       **    in determining the value of nInMul.  Note that the RHS of the
103314 103684       **    IN operator must be a SELECT, not a value list, for this variable
103315 103685       **    to be true.
103316 103686       **
103317         -    **  estBound:
103318         -    **    An estimate on the amount of the table that must be searched.  A
103319         -    **    value of 100 means the entire table is searched.  Range constraints
103320         -    **    might reduce this to a value less than 100 to indicate that only
103321         -    **    a fraction of the table needs searching.  In the absence of
103322         -    **    sqlite_stat2 ANALYZE data, a single inequality reduces the search
103323         -    **    space to 1/4rd its original size.  So an x>? constraint reduces
103324         -    **    estBound to 25.  Two constraints (x>? AND x<?) reduce estBound to 6.
       103687  +    **  rangeDiv:
       103688  +    **    An estimate of a divisor by which to reduce the search space due
       103689  +    **    to inequality constraints.  In the absence of sqlite_stat3 ANALYZE
       103690  +    **    data, a single inequality reduces the search space to 1/4rd its
       103691  +    **    original size (rangeDiv==4).  Two inequalities reduce the search
       103692  +    **    space to 1/16th of its original size (rangeDiv==16).
103325 103693       **
103326 103694       **  bSort:   
103327 103695       **    Boolean. True if there is an ORDER BY clause that will require an 
103328 103696       **    external sort (i.e. scanning the index being evaluated will not 
103329 103697       **    correctly order records).
103330 103698       **
103331 103699       **  bLookup: 
................................................................................
103342 103710       **
103343 103711       **             SELECT a, b    FROM tbl WHERE a = 1;
103344 103712       **             SELECT a, b, c FROM tbl WHERE a = 1;
103345 103713       */
103346 103714       int nEq;                      /* Number of == or IN terms matching index */
103347 103715       int bInEst = 0;               /* True if "x IN (SELECT...)" seen */
103348 103716       int nInMul = 1;               /* Number of distinct equalities to lookup */
103349         -    int estBound = 100;           /* Estimated reduction in search space */
       103717  +    double rangeDiv = (double)1;  /* Estimated reduction in search space */
103350 103718       int nBound = 0;               /* Number of range constraints seen */
103351 103719       int bSort = !!pOrderBy;       /* True if external sort required */
103352 103720       int bDist = !!pDistinct;      /* True if index cannot help with DISTINCT */
103353 103721       int bLookup = 0;              /* True if not a covering index */
103354 103722       WhereTerm *pTerm;             /* A single term of the WHERE clause */
103355         -#ifdef SQLITE_ENABLE_STAT2
       103723  +#ifdef SQLITE_ENABLE_STAT3
103356 103724       WhereTerm *pFirstTerm = 0;    /* First term matching the index */
103357 103725   #endif
103358 103726   
103359 103727       /* Determine the values of nEq and nInMul */
103360 103728       for(nEq=0; nEq<pProbe->nColumn; nEq++){
103361 103729         int j = pProbe->aiColumn[nEq];
103362 103730         pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
103363 103731         if( pTerm==0 ) break;
103364 103732         wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
       103733  +      testcase( pTerm->pWC!=pWC );
103365 103734         if( pTerm->eOperator & WO_IN ){
103366 103735           Expr *pExpr = pTerm->pExpr;
103367 103736           wsFlags |= WHERE_COLUMN_IN;
103368 103737           if( ExprHasProperty(pExpr, EP_xIsSelect) ){
103369 103738             /* "x IN (SELECT ...)":  Assume the SELECT returns 25 rows */
103370 103739             nInMul *= 25;
103371 103740             bInEst = 1;
................................................................................
103372 103741           }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
103373 103742             /* "x IN (value, value, ...)" */
103374 103743             nInMul *= pExpr->x.pList->nExpr;
103375 103744           }
103376 103745         }else if( pTerm->eOperator & WO_ISNULL ){
103377 103746           wsFlags |= WHERE_COLUMN_NULL;
103378 103747         }
103379         -#ifdef SQLITE_ENABLE_STAT2
       103748  +#ifdef SQLITE_ENABLE_STAT3
103380 103749         if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
103381 103750   #endif
103382 103751         used |= pTerm->prereqRight;
103383 103752       }
103384 103753   
103385         -    /* Determine the value of estBound. */
       103754  +    /* Determine the value of rangeDiv */
103386 103755       if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
103387 103756         int j = pProbe->aiColumn[nEq];
103388 103757         if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
103389 103758           WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
103390 103759           WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
103391         -        whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
       103760  +        whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
103392 103761           if( pTop ){
103393 103762             nBound = 1;
103394 103763             wsFlags |= WHERE_TOP_LIMIT;
103395 103764             used |= pTop->prereqRight;
       103765  +          testcase( pTop->pWC!=pWC );
103396 103766           }
103397 103767           if( pBtm ){
103398 103768             nBound++;
103399 103769             wsFlags |= WHERE_BTM_LIMIT;
103400 103770             used |= pBtm->prereqRight;
       103771  +          testcase( pBtm->pWC!=pWC );
103401 103772           }
103402 103773           wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
103403 103774         }
103404 103775       }else if( pProbe->onError!=OE_None ){
103405 103776         testcase( wsFlags & WHERE_COLUMN_IN );
103406 103777         testcase( wsFlags & WHERE_COLUMN_NULL );
103407 103778         if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
................................................................................
103456 103827       */
103457 103828       nRow = (double)(aiRowEst[nEq] * nInMul);
103458 103829       if( bInEst && nRow*2>aiRowEst[0] ){
103459 103830         nRow = aiRowEst[0]/2;
103460 103831         nInMul = (int)(nRow / aiRowEst[nEq]);
103461 103832       }
103462 103833   
103463         -#ifdef SQLITE_ENABLE_STAT2
       103834  +#ifdef SQLITE_ENABLE_STAT3
103464 103835       /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
103465 103836       ** and we do not think that values of x are unique and if histogram
103466 103837       ** data is available for column x, then it might be possible
103467 103838       ** to get a better estimate on the number of rows based on
103468 103839       ** VALUE and how common that value is according to the histogram.
103469 103840       */
103470 103841       if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 && aiRowEst[1]>1 ){
       103842  +      assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
103471 103843         if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
103472 103844           testcase( pFirstTerm->eOperator==WO_EQ );
103473 103845           testcase( pFirstTerm->eOperator==WO_ISNULL );
103474 103846           whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
103475         -      }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
       103847  +      }else if( bInEst==0 ){
       103848  +        assert( pFirstTerm->eOperator==WO_IN );
103476 103849           whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
103477 103850         }
103478 103851       }
103479         -#endif /* SQLITE_ENABLE_STAT2 */
       103852  +#endif /* SQLITE_ENABLE_STAT3 */
103480 103853   
103481 103854       /* Adjust the number of output rows and downward to reflect rows
103482 103855       ** that are excluded by range constraints.
103483 103856       */
103484         -    nRow = (nRow * (double)estBound) / (double)100;
       103857  +    nRow = nRow/rangeDiv;
103485 103858       if( nRow<1 ) nRow = 1;
103486 103859   
103487 103860       /* Experiments run on real SQLite databases show that the time needed
103488 103861       ** to do a binary search to locate a row in a table or index is roughly
103489 103862       ** log10(N) times the time to move from one row to the next row within
103490 103863       ** a table or index.  The actual times can vary, with the size of
103491 103864       ** records being an important factor.  Both moves and searches are
103492 103865       ** slower with larger records, presumably because fewer records fit
103493 103866       ** on one page and hence more pages have to be fetched.
103494 103867       **
103495         -    ** The ANALYZE command and the sqlite_stat1 and sqlite_stat2 tables do
       103868  +    ** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
103496 103869       ** not give us data on the relative sizes of table and index records.
103497 103870       ** So this computation assumes table records are about twice as big
103498 103871       ** as index records
103499 103872       */
103500 103873       if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
103501 103874         /* The cost of a full table scan is a number of move operations equal
103502 103875         ** to the number of rows in the table.
................................................................................
103606 103979           }
103607 103980         }
103608 103981         if( nRow<2 ) nRow = 2;
103609 103982       }
103610 103983   
103611 103984   
103612 103985       WHERETRACE((
103613         -      "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
       103986  +      "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
103614 103987         "         notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
103615 103988         pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), 
103616         -      nEq, nInMul, estBound, bSort, bLookup, wsFlags,
       103989  +      nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
103617 103990         notReady, log10N, nRow, cost, used
103618 103991       ));
103619 103992   
103620 103993       /* If this index is the best we have seen so far, then record this
103621 103994       ** index and its cost in the pCost structure.
103622 103995       */
103623 103996       if( (!pIdx || wsFlags)
................................................................................
104113 104486   ** Generate code for the start of the iLevel-th loop in the WHERE clause
104114 104487   ** implementation described by pWInfo.
104115 104488   */
104116 104489   static Bitmask codeOneLoopStart(
104117 104490     WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
104118 104491     int iLevel,          /* Which level of pWInfo->a[] should be coded */
104119 104492     u16 wctrlFlags,      /* One of the WHERE_* flags defined in sqliteInt.h */
104120         -  Bitmask notReady     /* Which tables are currently available */
       104493  +  Bitmask notReady,    /* Which tables are currently available */
       104494  +  Expr *pWhere         /* Complete WHERE clause */
104121 104495   ){
104122 104496     int j, k;            /* Loop counters */
104123 104497     int iCur;            /* The VDBE cursor for the table */
104124 104498     int addrNxt;         /* Where to jump to continue with the next IN case */
104125 104499     int omitTable;       /* True if we use the index only */
104126 104500     int bRev;            /* True if we need to scan in reverse order */
104127 104501     WhereLevel *pLevel;  /* The where level to be coded */
................................................................................
104595 104969   
104596 104970       int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
104597 104971       int regRowset = 0;                        /* Register for RowSet object */
104598 104972       int regRowid = 0;                         /* Register holding rowid */
104599 104973       int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
104600 104974       int iRetInit;                             /* Address of regReturn init */
104601 104975       int untestedTerms = 0;             /* Some terms not completely tested */
104602         -    int ii;
       104976  +    int ii;                            /* Loop counter */
       104977  +    Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
104603 104978      
104604 104979       pTerm = pLevel->plan.u.pTerm;
104605 104980       assert( pTerm!=0 );
104606 104981       assert( pTerm->eOperator==WO_OR );
104607 104982       assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
104608 104983       pOrWc = &pTerm->u.pOrInfo->wc;
104609 104984       pLevel->op = OP_Return;
................................................................................
104644 105019       */
104645 105020       if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
104646 105021         regRowset = ++pParse->nMem;
104647 105022         regRowid = ++pParse->nMem;
104648 105023         sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
104649 105024       }
104650 105025       iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
       105026  +
       105027  +    /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
       105028  +    ** Then for every term xN, evaluate as the subexpression: xN AND z
       105029  +    ** That way, terms in y that are factored into the disjunction will
       105030  +    ** be picked up by the recursive calls to sqlite3WhereBegin() below.
       105031  +    */
       105032  +    if( pWC->nTerm>1 ){
       105033  +      pAndExpr = sqlite3ExprAlloc(pParse->db, TK_AND, 0, 0);
       105034  +      pAndExpr->pRight = pWhere;
       105035  +    }
104651 105036   
104652 105037       for(ii=0; ii<pOrWc->nTerm; ii++){
104653 105038         WhereTerm *pOrTerm = &pOrWc->a[ii];
104654 105039         if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
104655 105040           WhereInfo *pSubWInfo;          /* Info for single OR-term scan */
       105041  +        Expr *pOrExpr = pOrTerm->pExpr;
       105042  +        if( pAndExpr ){
       105043  +          pAndExpr->pLeft = pOrExpr;
       105044  +          pOrExpr = pAndExpr;
       105045  +        }
104656 105046           /* Loop through table entries that match term pOrTerm. */
104657         -        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrTerm->pExpr, 0, 0,
104658         -                        WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE |
       105047  +        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
       105048  +                        WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
104659 105049                           WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY);
104660 105050           if( pSubWInfo ){
104661 105051             explainOneScan(
104662 105052                 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
104663 105053             );
104664 105054             if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
104665 105055               int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
................................................................................
104679 105069             if( pSubWInfo->untestedTerms ) untestedTerms = 1;
104680 105070   
104681 105071             /* Finish the loop through table entries that match term pOrTerm. */
104682 105072             sqlite3WhereEnd(pSubWInfo);
104683 105073           }
104684 105074         }
104685 105075       }
       105076  +    sqlite3DbFree(pParse->db, pAndExpr);
104686 105077       sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
104687 105078       sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
104688 105079       sqlite3VdbeResolveLabel(v, iLoopBody);
104689 105080   
104690 105081       if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
104691 105082       if( !untestedTerms ) disableTerm(pLevel, pTerm);
104692 105083     }else
................................................................................
104960 105351     ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
104961 105352     if( db->flags & SQLITE_DistinctOpt ) pDistinct = 0;
104962 105353   
104963 105354     /* Split the WHERE clause into separate subexpressions where each
104964 105355     ** subexpression is separated by an AND operator.
104965 105356     */
104966 105357     initMaskSet(pMaskSet);
104967         -  whereClauseInit(pWC, pParse, pMaskSet);
       105358  +  whereClauseInit(pWC, pParse, pMaskSet, wctrlFlags);
104968 105359     sqlite3ExprCodeConstants(pParse, pWhere);
104969 105360     whereSplit(pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
104970 105361       
104971 105362     /* Special case: a WHERE clause that is constant.  Evaluate the
104972 105363     ** expression and either jump over all of the code or fall thru.
104973 105364     */
104974 105365     if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
................................................................................
105199 105590         }
105200 105591       }
105201 105592       assert( bestJ>=0 );
105202 105593       assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
105203 105594       WHERETRACE(("*** Optimizer selects table %d for loop %d"
105204 105595                   " with cost=%g and nRow=%g\n",
105205 105596                   bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow));
105206         -    if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){
       105597  +    /* The ALWAYS() that follows was added to hush up clang scan-build */
       105598  +    if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 && ALWAYS(ppOrderBy) ){
105207 105599         *ppOrderBy = 0;
105208 105600       }
105209 105601       if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
105210 105602         assert( pWInfo->eDistinct==0 );
105211 105603         pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
105212 105604       }
105213 105605       andFlags &= bestPlan.plan.wsFlags;
................................................................................
105288 105680       if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
105289 105681         const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
105290 105682         int iCur = pTabItem->iCursor;
105291 105683         sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
105292 105684       }else
105293 105685   #endif
105294 105686       if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
105295         -         && (wctrlFlags & WHERE_OMIT_OPEN)==0 ){
       105687  +         && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
105296 105688         int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
105297 105689         sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
105298 105690         testcase( pTab->nCol==BMS-1 );
105299 105691         testcase( pTab->nCol==BMS );
105300 105692         if( !pWInfo->okOnePass && pTab->nCol<BMS ){
105301 105693           Bitmask b = pTabItem->colUsed;
105302 105694           int n = 0;
................................................................................
105333 105725     ** loop below generates code for a single nested loop of the VM
105334 105726     ** program.
105335 105727     */
105336 105728     notReady = ~(Bitmask)0;
105337 105729     for(i=0; i<nTabList; i++){
105338 105730       pLevel = &pWInfo->a[i];
105339 105731       explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
105340         -    notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady);
       105732  +    notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady, pWhere);
105341 105733       pWInfo->iContinue = pLevel->addrCont;
105342 105734     }
105343 105735   
105344 105736   #ifdef SQLITE_TEST  /* For testing and debugging use only */
105345 105737     /* Record in the query plan information about the current table
105346 105738     ** and the index used to access it (if any).  If the table itself
105347 105739     ** is not used, its name is just '{}'.  If no index is used
................................................................................
105468 105860     assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc );
105469 105861     for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
105470 105862       struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
105471 105863       Table *pTab = pTabItem->pTab;
105472 105864       assert( pTab!=0 );
105473 105865       if( (pTab->tabFlags & TF_Ephemeral)==0
105474 105866        && pTab->pSelect==0
105475         -     && (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0
       105867  +     && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
105476 105868       ){
105477 105869         int ws = pLevel->plan.wsFlags;
105478 105870         if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
105479 105871           sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
105480 105872         }
105481 105873         if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){
105482 105874           sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
................................................................................
108815 109207     void *yyp,                   /* The parser */
108816 109208     int yymajor,                 /* The major token code number */
108817 109209     sqlite3ParserTOKENTYPE yyminor       /* The value for the token */
108818 109210     sqlite3ParserARG_PDECL               /* Optional %extra_argument parameter */
108819 109211   ){
108820 109212     YYMINORTYPE yyminorunion;
108821 109213     int yyact;            /* The parser action. */
       109214  +#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
108822 109215     int yyendofinput;     /* True if we are at the end of input */
       109216  +#endif
108823 109217   #ifdef YYERRORSYMBOL
108824 109218     int yyerrorhit = 0;   /* True if yymajor has invoked an error */
108825 109219   #endif
108826 109220     yyParser *yypParser;  /* The parser */
108827 109221   
108828 109222     /* (re)initialize the parser, if necessary */
108829 109223     yypParser = (yyParser*)yyp;
................................................................................
108838 109232   #endif
108839 109233       yypParser->yyidx = 0;
108840 109234       yypParser->yyerrcnt = -1;
108841 109235       yypParser->yystack[0].stateno = 0;
108842 109236       yypParser->yystack[0].major = 0;
108843 109237     }
108844 109238     yyminorunion.yy0 = yyminor;
       109239  +#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
108845 109240     yyendofinput = (yymajor==0);
       109241  +#endif
108846 109242     sqlite3ParserARG_STORE;
108847 109243   
108848 109244   #ifndef NDEBUG
108849 109245     if( yyTraceFILE ){
108850 109246       fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
108851 109247     }
108852 109248   #endif
108853 109249   
108854 109250     do{
108855 109251       yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
108856 109252       if( yyact<YYNSTATE ){
108857         -      assert( !yyendofinput );  /* Impossible to shift the $ token */
108858 109253         yy_shift(yypParser,yyact,yymajor,&yyminorunion);
108859 109254         yypParser->yyerrcnt--;
108860 109255         yymajor = YYNOCODE;
108861 109256       }else if( yyact < YYNSTATE + YYNRULE ){
108862 109257         yy_reduce(yypParser,yyact-YYNSTATE);
108863 109258       }else{
108864 109259         assert( yyact == YY_ERROR_ACTION );
................................................................................
110242 110637   **    *  Calls to this routine from Y must block until the outer-most
110243 110638   **       call by X completes.
110244 110639   **
110245 110640   **    *  Recursive calls to this routine from thread X return immediately
110246 110641   **       without blocking.
110247 110642   */
110248 110643   SQLITE_API int sqlite3_initialize(void){
110249         -  sqlite3_mutex *pMaster;                      /* The main static mutex */
       110644  +  MUTEX_LOGIC( sqlite3_mutex *pMaster; )       /* The main static mutex */
110250 110645     int rc;                                      /* Result code */
110251 110646   
110252 110647   #ifdef SQLITE_OMIT_WSD
110253 110648     rc = sqlite3_wsd_init(4096, 24);
110254 110649     if( rc!=SQLITE_OK ){
110255 110650       return rc;
110256 110651     }
................................................................................
110276 110671   
110277 110672     /* Initialize the malloc() system and the recursive pInitMutex mutex.
110278 110673     ** This operation is protected by the STATIC_MASTER mutex.  Note that
110279 110674     ** MutexAlloc() is called for a static mutex prior to initializing the
110280 110675     ** malloc subsystem - this implies that the allocation of a static
110281 110676     ** mutex must not require support from the malloc subsystem.
110282 110677     */
110283         -  pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
       110678  +  MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
110284 110679     sqlite3_mutex_enter(pMaster);
110285 110680     sqlite3GlobalConfig.isMutexInit = 1;
110286 110681     if( !sqlite3GlobalConfig.isMallocInit ){
110287 110682       rc = sqlite3MallocInit();
110288 110683     }
110289 110684     if( rc==SQLITE_OK ){
110290 110685       sqlite3GlobalConfig.isMallocInit = 1;
................................................................................
111350 111745   */
111351 111746   SQLITE_API int sqlite3_overload_function(
111352 111747     sqlite3 *db,
111353 111748     const char *zName,
111354 111749     int nArg
111355 111750   ){
111356 111751     int nName = sqlite3Strlen30(zName);
111357         -  int rc;
       111752  +  int rc = SQLITE_OK;
111358 111753     sqlite3_mutex_enter(db->mutex);
111359 111754     if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
111360         -    sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
111361         -                      0, sqlite3InvalidFunction, 0, 0, 0);
       111755  +    rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
       111756  +                           0, sqlite3InvalidFunction, 0, 0, 0);
111362 111757     }
111363         -  rc = sqlite3ApiExit(db, SQLITE_OK);
       111758  +  rc = sqlite3ApiExit(db, rc);
111364 111759     sqlite3_mutex_leave(db->mutex);
111365 111760     return rc;
111366 111761   }
111367 111762   
111368 111763   #ifndef SQLITE_OMIT_TRACE
111369 111764   /*
111370 111765   ** Register a trace function.  The pArg from the previously registered trace
................................................................................
112418 112813   opendb_out:
112419 112814     sqlite3_free(zOpen);
112420 112815     if( db ){
112421 112816       assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
112422 112817       sqlite3_mutex_leave(db->mutex);
112423 112818     }
112424 112819     rc = sqlite3_errcode(db);
       112820  +  assert( db!=0 || rc==SQLITE_NOMEM );
112425 112821     if( rc==SQLITE_NOMEM ){
112426 112822       sqlite3_close(db);
112427 112823       db = 0;
112428 112824     }else if( rc!=SQLITE_OK ){
112429 112825       db->magic = SQLITE_MAGIC_SICK;
112430 112826     }
112431 112827     *ppDb = db;
................................................................................
114146 114542   #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
114147 114543   # define TESTONLY(X)  X
114148 114544   #else
114149 114545   # define TESTONLY(X)
114150 114546   #endif
114151 114547   
114152 114548   #endif /* SQLITE_AMALGAMATION */
       114549  +
       114550  +#ifdef SQLITE_DEBUG
       114551  +SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
       114552  +# define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
       114553  +#else
       114554  +# define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
       114555  +#endif
114153 114556   
114154 114557   typedef struct Fts3Table Fts3Table;
114155 114558   typedef struct Fts3Cursor Fts3Cursor;
114156 114559   typedef struct Fts3Expr Fts3Expr;
114157 114560   typedef struct Fts3Phrase Fts3Phrase;
114158 114561   typedef struct Fts3PhraseToken Fts3PhraseToken;
114159 114562   
................................................................................
114174 114577     sqlite3_vtab base;              /* Base class used by SQLite core */
114175 114578     sqlite3 *db;                    /* The database connection */
114176 114579     const char *zDb;                /* logical database name */
114177 114580     const char *zName;              /* virtual table name */
114178 114581     int nColumn;                    /* number of named columns in virtual table */
114179 114582     char **azColumn;                /* column names.  malloced */
114180 114583     sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
       114584  +  char *zContentTbl;              /* content=xxx option, or NULL */
114181 114585   
114182 114586     /* Precompiled statements used by the implementation. Each of these 
114183 114587     ** statements is run and reset within a single virtual table API call. 
114184 114588     */
114185 114589     sqlite3_stmt *aStmt[27];
114186 114590   
114187 114591     char *zReadExprlist;
................................................................................
114214 114618       int nPrefix;                  /* Prefix length (0 for main terms index) */
114215 114619       Fts3Hash hPending;            /* Pending terms table for this index */
114216 114620     } *aIndex;
114217 114621     int nMaxPendingData;            /* Max pending data before flush to disk */
114218 114622     int nPendingData;               /* Current bytes of pending data */
114219 114623     sqlite_int64 iPrevDocid;        /* Docid of most recently inserted document */
114220 114624   
114221         -#if defined(SQLITE_DEBUG)
       114625  +#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
114222 114626     /* State variables used for validating that the transaction control
114223 114627     ** methods of the virtual table are called at appropriate times.  These
114224 114628     ** values do not contribution to the FTS computation; they are used for
114225 114629     ** verifying the SQLite core.
114226 114630     */
114227 114631     int inTransaction;     /* True after xBegin but before xCommit/xRollback */
114228 114632     int mxSavepoint;       /* Largest valid xSavepoint integer */
................................................................................
114299 114703   ** For a sequence of tokens contained in double-quotes (i.e. "one two three")
114300 114704   ** nToken will be the number of tokens in the string.
114301 114705   */
114302 114706   struct Fts3PhraseToken {
114303 114707     char *z;                        /* Text of the token */
114304 114708     int n;                          /* Number of bytes in buffer z */
114305 114709     int isPrefix;                   /* True if token ends with a "*" character */
       114710  +  int bFirst;                     /* True if token must appear at position 0 */
114306 114711   
114307 114712     /* Variables above this point are populated when the expression is
114308 114713     ** parsed (by code in fts3_expr.c). Below this point the variables are
114309 114714     ** used when evaluating the expression. */
114310 114715     Fts3DeferredToken *pDeferred;   /* Deferred token object for this token */
114311 114716     Fts3MultiSegReader *pSegcsr;    /* Segment-reader for this token */
114312 114717   };
................................................................................
114417 114822   
114418 114823   /* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
114419 114824   #define FTS3_SEGMENT_REQUIRE_POS   0x00000001
114420 114825   #define FTS3_SEGMENT_IGNORE_EMPTY  0x00000002
114421 114826   #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
114422 114827   #define FTS3_SEGMENT_PREFIX        0x00000008
114423 114828   #define FTS3_SEGMENT_SCAN          0x00000010
       114829  +#define FTS3_SEGMENT_FIRST         0x00000020
114424 114830   
114425 114831   /* Type passed as 4th argument to SegmentReaderIterate() */
114426 114832   struct Fts3SegFilter {
114427 114833     const char *zTerm;
114428 114834     int nTerm;
114429 114835     int iCol;
114430 114836     int flags;
................................................................................
114456 114862   /* fts3.c */
114457 114863   SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
114458 114864   SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
114459 114865   SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
114460 114866   SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
114461 114867   SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
114462 114868   SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
114463         -
114464 114869   SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
       114870  +SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
114465 114871   
114466 114872   /* fts3_tokenizer.c */
114467 114873   SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
114468 114874   SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
114469 114875   SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, 
114470 114876       sqlite3_tokenizer **, char **
114471 114877   );
................................................................................
114476 114882   SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
114477 114883     const char *, const char *, int, int
114478 114884   );
114479 114885   SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
114480 114886   
114481 114887   /* fts3_expr.c */
114482 114888   SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, 
114483         -  char **, int, int, const char *, int, Fts3Expr **
       114889  +  char **, int, int, int, const char *, int, Fts3Expr **
114484 114890   );
114485 114891   SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
114486 114892   #ifdef SQLITE_TEST
114487 114893   SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
114488 114894   SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
114489 114895   #endif
114490 114896   
................................................................................
114647 115053   */
114648 115054   static void fts3GetReverseVarint(
114649 115055     char **pp, 
114650 115056     char *pStart, 
114651 115057     sqlite3_int64 *pVal
114652 115058   ){
114653 115059     sqlite3_int64 iVal;
114654         -  char *p = *pp;
       115060  +  char *p;
114655 115061   
114656 115062     /* Pointer p now points at the first byte past the varint we are 
114657 115063     ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
114658 115064     ** clear on character p[-1]. */
114659 115065     for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
114660 115066     p++;
114661 115067     *pp = p;
................................................................................
114677 115083     /* Free any prepared statements held */
114678 115084     for(i=0; i<SizeofArray(p->aStmt); i++){
114679 115085       sqlite3_finalize(p->aStmt[i]);
114680 115086     }
114681 115087     sqlite3_free(p->zSegmentsTbl);
114682 115088     sqlite3_free(p->zReadExprlist);
114683 115089     sqlite3_free(p->zWriteExprlist);
       115090  +  sqlite3_free(p->zContentTbl);
114684 115091   
114685 115092     /* Invoke the tokenizer destructor to free the tokenizer. */
114686 115093     p->pTokenizer->pModule->xDestroy(p->pTokenizer);
114687 115094   
114688 115095     sqlite3_free(p);
114689 115096     return SQLITE_OK;
114690 115097   }
................................................................................
114716 115123     }
114717 115124   }
114718 115125   
114719 115126   /*
114720 115127   ** The xDestroy() virtual table method.
114721 115128   */
114722 115129   static int fts3DestroyMethod(sqlite3_vtab *pVtab){
114723         -  int rc = SQLITE_OK;              /* Return code */
114724 115130     Fts3Table *p = (Fts3Table *)pVtab;
114725         -  sqlite3 *db = p->db;
       115131  +  int rc = SQLITE_OK;              /* Return code */
       115132  +  const char *zDb = p->zDb;        /* Name of database (e.g. "main", "temp") */
       115133  +  sqlite3 *db = p->db;             /* Database handle */
114726 115134   
114727 115135     /* Drop the shadow tables */
114728         -  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", p->zDb, p->zName);
114729         -  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", p->zDb,p->zName);
114730         -  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", p->zDb, p->zName);
114731         -  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", p->zDb, p->zName);
114732         -  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", p->zDb, p->zName);
       115136  +  if( p->zContentTbl==0 ){
       115137  +    fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName);
       115138  +  }
       115139  +  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName);
       115140  +  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName);
       115141  +  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName);
       115142  +  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName);
114733 115143   
114734 115144     /* If everything has worked, invoke fts3DisconnectMethod() to free the
114735 115145     ** memory associated with the Fts3Table structure and return SQLITE_OK.
114736 115146     ** Otherwise, return an SQLite error code.
114737 115147     */
114738 115148     return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
114739 115149   }
................................................................................
114787 115197   ** If the p->bHasDocsize boolean is true (indicating that this is an
114788 115198   ** FTS4 table, not an FTS3 table) then also create the %_docsize and
114789 115199   ** %_stat tables required by FTS4.
114790 115200   */
114791 115201   static int fts3CreateTables(Fts3Table *p){
114792 115202     int rc = SQLITE_OK;             /* Return code */
114793 115203     int i;                          /* Iterator variable */
114794         -  char *zContentCols;             /* Columns of %_content table */
114795 115204     sqlite3 *db = p->db;            /* The database connection */
114796 115205   
114797         -  /* Create a list of user columns for the content table */
114798         -  zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
114799         -  for(i=0; zContentCols && i<p->nColumn; i++){
114800         -    char *z = p->azColumn[i];
114801         -    zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
       115206  +  if( p->zContentTbl==0 ){
       115207  +    char *zContentCols;           /* Columns of %_content table */
       115208  +
       115209  +    /* Create a list of user columns for the content table */
       115210  +    zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
       115211  +    for(i=0; zContentCols && i<p->nColumn; i++){
       115212  +      char *z = p->azColumn[i];
       115213  +      zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
       115214  +    }
       115215  +    if( zContentCols==0 ) rc = SQLITE_NOMEM;
       115216  +  
       115217  +    /* Create the content table */
       115218  +    fts3DbExec(&rc, db, 
       115219  +       "CREATE TABLE %Q.'%q_content'(%s)",
       115220  +       p->zDb, p->zName, zContentCols
       115221  +    );
       115222  +    sqlite3_free(zContentCols);
114802 115223     }
114803         -  if( zContentCols==0 ) rc = SQLITE_NOMEM;
114804 115224   
114805         -  /* Create the content table */
114806         -  fts3DbExec(&rc, db, 
114807         -     "CREATE TABLE %Q.'%q_content'(%s)",
114808         -     p->zDb, p->zName, zContentCols
114809         -  );
114810         -  sqlite3_free(zContentCols);
114811 115225     /* Create other tables */
114812 115226     fts3DbExec(&rc, db, 
114813 115227         "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
114814 115228         p->zDb, p->zName
114815 115229     );
114816 115230     fts3DbExec(&rc, db, 
114817 115231         "CREATE TABLE %Q.'%q_segdir'("
................................................................................
114954 115368       *(z++) = '"';
114955 115369       *(z++) = '\0';
114956 115370     }
114957 115371     return zRet;
114958 115372   }
114959 115373   
114960 115374   /*
114961         -** Return a list of comma separated SQL expressions that could be used
114962         -** in a SELECT statement such as the following:
       115375  +** Return a list of comma separated SQL expressions and a FROM clause that 
       115376  +** could be used in a SELECT statement such as the following:
114963 115377   **
114964 115378   **     SELECT <list of expressions> FROM %_content AS x ...
114965 115379   **
114966 115380   ** to return the docid, followed by each column of text data in order
114967 115381   ** from left to write. If parameter zFunc is not NULL, then instead of
114968 115382   ** being returned directly each column of text data is passed to an SQL
114969 115383   ** function named zFunc first. For example, if zFunc is "unzip" and the
114970 115384   ** table has the three user-defined columns "a", "b", and "c", the following
114971 115385   ** string is returned:
114972 115386   **
114973         -**     "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c')"
       115387  +**     "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
114974 115388   **
114975 115389   ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
114976 115390   ** is the responsibility of the caller to eventually free it.
114977 115391   **
114978 115392   ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
114979 115393   ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
114980 115394   ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
................................................................................
114982 115396   */
114983 115397   static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
114984 115398     char *zRet = 0;
114985 115399     char *zFree = 0;
114986 115400     char *zFunction;
114987 115401     int i;
114988 115402   
114989         -  if( !zFunc ){
114990         -    zFunction = "";
       115403  +  if( p->zContentTbl==0 ){
       115404  +    if( !zFunc ){
       115405  +      zFunction = "";
       115406  +    }else{
       115407  +      zFree = zFunction = fts3QuoteId(zFunc);
       115408  +    }
       115409  +    fts3Appendf(pRc, &zRet, "docid");
       115410  +    for(i=0; i<p->nColumn; i++){
       115411  +      fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
       115412  +    }
       115413  +    sqlite3_free(zFree);
114991 115414     }else{
114992         -    zFree = zFunction = fts3QuoteId(zFunc);
       115415  +    fts3Appendf(pRc, &zRet, "rowid");
       115416  +    for(i=0; i<p->nColumn; i++){
       115417  +      fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
       115418  +    }
114993 115419     }
114994         -  fts3Appendf(pRc, &zRet, "docid");
114995         -  for(i=0; i<p->nColumn; i++){
114996         -    fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
114997         -  }
114998         -  sqlite3_free(zFree);
       115420  +  fts3Appendf(pRc, &zRet, "FROM '%q'.'%q%s' AS x", 
       115421  +      p->zDb,
       115422  +      (p->zContentTbl ? p->zContentTbl : p->zName),
       115423  +      (p->zContentTbl ? "" : "_content")
       115424  +  );
114999 115425     return zRet;
115000 115426   }
115001 115427   
115002 115428   /*
115003 115429   ** Return a list of N comma separated question marks, where N is the number
115004 115430   ** of columns in the %_content table (one for the docid plus one for each
115005 115431   ** user-defined text column).
................................................................................
115048 115474   **
115049 115475   ** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
115050 115476   ** the output value undefined. Otherwise SQLITE_OK is returned.
115051 115477   **
115052 115478   ** This function is used when parsing the "prefix=" FTS4 parameter.
115053 115479   */
115054 115480   static int fts3GobbleInt(const char **pp, int *pnOut){
115055         -  const char *p = *pp;            /* Iterator pointer */
       115481  +  const char *p;                  /* Iterator pointer */
115056 115482     int nInt = 0;                   /* Output value */
115057 115483   
115058 115484     for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
115059 115485       nInt = nInt * 10 + (p[0] - '0');
115060 115486     }
115061 115487     if( p==*pp ) return SQLITE_ERROR;
115062 115488     *pnOut = nInt;
................................................................................
115114 115540         aIndex[i].nPrefix = nPrefix;
115115 115541         p++;
115116 115542       }
115117 115543     }
115118 115544   
115119 115545     return SQLITE_OK;
115120 115546   }
       115547  +
       115548  +/*
       115549  +** This function is called when initializing an FTS4 table that uses the
       115550  +** content=xxx option. It determines the number of and names of the columns
       115551  +** of the new FTS4 table.
       115552  +**
       115553  +** The third argument passed to this function is the value passed to the
       115554  +** config=xxx option (i.e. "xxx"). This function queries the database for
       115555  +** a table of that name. If found, the output variables are populated
       115556  +** as follows:
       115557  +**
       115558  +**   *pnCol:   Set to the number of columns table xxx has,
       115559  +**
       115560  +**   *pnStr:   Set to the total amount of space required to store a copy
       115561  +**             of each columns name, including the nul-terminator.
       115562  +**
       115563  +**   *pazCol:  Set to point to an array of *pnCol strings. Each string is
       115564  +**             the name of the corresponding column in table xxx. The array
       115565  +**             and its contents are allocated using a single allocation. It
       115566  +**             is the responsibility of the caller to free this allocation
       115567  +**             by eventually passing the *pazCol value to sqlite3_free().
       115568  +**
       115569  +** If the table cannot be found, an error code is returned and the output
       115570  +** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
       115571  +** returned (and the output variables are undefined).
       115572  +*/
       115573  +static int fts3ContentColumns(
       115574  +  sqlite3 *db,                    /* Database handle */
       115575  +  const char *zDb,                /* Name of db (i.e. "main", "temp" etc.) */
       115576  +  const char *zTbl,               /* Name of content table */
       115577  +  const char ***pazCol,           /* OUT: Malloc'd array of column names */
       115578  +  int *pnCol,                     /* OUT: Size of array *pazCol */
       115579  +  int *pnStr                      /* OUT: Bytes of string content */
       115580  +){
       115581  +  int rc = SQLITE_OK;             /* Return code */
       115582  +  char *zSql;                     /* "SELECT *" statement on zTbl */  
       115583  +  sqlite3_stmt *pStmt = 0;        /* Compiled version of zSql */
       115584  +
       115585  +  zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
       115586  +  if( !zSql ){
       115587  +    rc = SQLITE_NOMEM;
       115588  +  }else{
       115589  +    rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
       115590  +  }
       115591  +  sqlite3_free(zSql);
       115592  +
       115593  +  if( rc==SQLITE_OK ){
       115594  +    const char **azCol;           /* Output array */
       115595  +    int nStr = 0;                 /* Size of all column names (incl. 0x00) */
       115596  +    int nCol;                     /* Number of table columns */
       115597  +    int i;                        /* Used to iterate through columns */
       115598  +
       115599  +    /* Loop through the returned columns. Set nStr to the number of bytes of
       115600  +    ** space required to store a copy of each column name, including the
       115601  +    ** nul-terminator byte.  */
       115602  +    nCol = sqlite3_column_count(pStmt);
       115603  +    for(i=0; i<nCol; i++){
       115604  +      const char *zCol = sqlite3_column_name(pStmt, i);
       115605  +      nStr += strlen(zCol) + 1;
       115606  +    }
       115607  +
       115608  +    /* Allocate and populate the array to return. */
       115609  +    azCol = (const char **)sqlite3_malloc(sizeof(char *) * nCol + nStr);
       115610  +    if( azCol==0 ){
       115611  +      rc = SQLITE_NOMEM;
       115612  +    }else{
       115613  +      char *p = (char *)&azCol[nCol];
       115614  +      for(i=0; i<nCol; i++){
       115615  +        const char *zCol = sqlite3_column_name(pStmt, i);
       115616  +        int n = strlen(zCol)+1;
       115617  +        memcpy(p, zCol, n);
       115618  +        azCol[i] = p;
       115619  +        p += n;
       115620  +      }
       115621  +    }
       115622  +    sqlite3_finalize(pStmt);
       115623  +
       115624  +    /* Set the output variables. */
       115625  +    *pnCol = nCol;
       115626  +    *pnStr = nStr;
       115627  +    *pazCol = azCol;
       115628  +  }
       115629  +
       115630  +  return rc;
       115631  +}
115121 115632   
115122 115633   /*
115123 115634   ** This function is the implementation of both the xConnect and xCreate
115124 115635   ** methods of the FTS3 virtual table.
115125 115636   **
115126 115637   ** The argv[] array contains the following:
115127 115638   **
................................................................................
115159 115670   
115160 115671     /* The results of parsing supported FTS4 key=value options: */
115161 115672     int bNoDocsize = 0;             /* True to omit %_docsize table */
115162 115673     int bDescIdx = 0;               /* True to store descending indexes */
115163 115674     char *zPrefix = 0;              /* Prefix parameter value (or NULL) */
115164 115675     char *zCompress = 0;            /* compress=? parameter (or NULL) */
115165 115676     char *zUncompress = 0;          /* uncompress=? parameter (or NULL) */
       115677  +  char *zContent = 0;             /* content=? parameter (or NULL) */
115166 115678   
115167 115679     assert( strlen(argv[0])==4 );
115168 115680     assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
115169 115681          || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
115170 115682     );
115171 115683   
115172 115684     nDb = (int)strlen(argv[1]) + 1;
................................................................................
115202 115714       }
115203 115715   
115204 115716       /* Check if it is an FTS4 special argument. */
115205 115717       else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
115206 115718         struct Fts4Option {
115207 115719           const char *zOpt;
115208 115720           int nOpt;
115209         -        char **pzVar;
115210 115721         } aFts4Opt[] = {
115211         -        { "matchinfo",   9, 0 },            /* 0 -> MATCHINFO */
115212         -        { "prefix",      6, 0 },            /* 1 -> PREFIX */
115213         -        { "compress",    8, 0 },            /* 2 -> COMPRESS */
115214         -        { "uncompress", 10, 0 },            /* 3 -> UNCOMPRESS */
115215         -        { "order",       5, 0 }             /* 4 -> ORDER */
       115722  +        { "matchinfo",   9 },     /* 0 -> MATCHINFO */
       115723  +        { "prefix",      6 },     /* 1 -> PREFIX */
       115724  +        { "compress",    8 },     /* 2 -> COMPRESS */
       115725  +        { "uncompress", 10 },     /* 3 -> UNCOMPRESS */
       115726  +        { "order",       5 },     /* 4 -> ORDER */
       115727  +        { "content",     7 }      /* 5 -> CONTENT */
115216 115728         };
115217 115729   
115218 115730         int iOpt;
115219 115731         if( !zVal ){
115220 115732           rc = SQLITE_NOMEM;
115221 115733         }else{
115222 115734           for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
................................................................................
115254 115766                 sqlite3_free(zUncompress);
115255 115767                 zUncompress = zVal;
115256 115768                 zVal = 0;
115257 115769                 break;
115258 115770   
115259 115771               case 4:               /* ORDER */
115260 115772                 if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) 
115261         -               && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 3)) 
       115773  +               && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) 
115262 115774                 ){
115263 115775                   *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal);
115264 115776                   rc = SQLITE_ERROR;
115265 115777                 }
115266 115778                 bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
115267 115779                 break;
       115780  +
       115781  +            default:              /* CONTENT */
       115782  +              assert( iOpt==5 );
       115783  +              sqlite3_free(zUncompress);
       115784  +              zContent = zVal;
       115785  +              zVal = 0;
       115786  +              break;
115268 115787             }
115269 115788           }
115270 115789           sqlite3_free(zVal);
115271 115790         }
115272 115791       }
115273 115792   
115274 115793       /* Otherwise, the argument is a column name. */
115275 115794       else {
115276 115795         nString += (int)(strlen(z) + 1);
115277 115796         aCol[nCol++] = z;
115278 115797       }
115279 115798     }
       115799  +
       115800  +  /* If a content=xxx option was specified, the following:
       115801  +  **
       115802  +  **   1. Ignore any compress= and uncompress= options.
       115803  +  **
       115804  +  **   2. If no column names were specified as part of the CREATE VIRTUAL
       115805  +  **      TABLE statement, use all columns from the content table.
       115806  +  */
       115807  +  if( rc==SQLITE_OK && zContent ){
       115808  +    sqlite3_free(zCompress); 
       115809  +    sqlite3_free(zUncompress); 
       115810  +    zCompress = 0;
       115811  +    zUncompress = 0;
       115812  +    if( nCol==0 ){
       115813  +      sqlite3_free((void*)aCol); 
       115814  +      aCol = 0;
       115815  +      rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString);
       115816  +    }
       115817  +    assert( rc!=SQLITE_OK || nCol>0 );
       115818  +  }
115280 115819     if( rc!=SQLITE_OK ) goto fts3_init_out;
115281 115820   
115282 115821     if( nCol==0 ){
115283 115822       assert( nString==0 );
115284 115823       aCol[0] = "content";
115285 115824       nString = 8;
115286 115825       nCol = 1;
................................................................................
115317 115856     p->nPendingData = 0;
115318 115857     p->azColumn = (char **)&p[1];
115319 115858     p->pTokenizer = pTokenizer;
115320 115859     p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
115321 115860     p->bHasDocsize = (isFts4 && bNoDocsize==0);
115322 115861     p->bHasStat = isFts4;
115323 115862     p->bDescIdx = bDescIdx;
       115863  +  p->zContentTbl = zContent;
       115864  +  zContent = 0;
115324 115865     TESTONLY( p->inTransaction = -1 );
115325 115866     TESTONLY( p->mxSavepoint = -1 );
115326 115867   
115327 115868     p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
115328 115869     memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
115329 115870     p->nIndex = nIndex;
115330 115871     for(i=0; i<nIndex; i++){
................................................................................
115378 115919     fts3DeclareVtab(&rc, p);
115379 115920   
115380 115921   fts3_init_out:
115381 115922     sqlite3_free(zPrefix);
115382 115923     sqlite3_free(aIndex);
115383 115924     sqlite3_free(zCompress);
115384 115925     sqlite3_free(zUncompress);
       115926  +  sqlite3_free(zContent);
115385 115927     sqlite3_free((void *)aCol);
115386 115928     if( rc!=SQLITE_OK ){
115387 115929       if( p ){
115388 115930         fts3DisconnectMethod((sqlite3_vtab *)p);
115389 115931       }else if( pTokenizer ){
115390 115932         pTokenizer->pModule->xDestroy(pTokenizer);
115391 115933       }
................................................................................
115528 116070     sqlite3Fts3FreeDeferredTokens(pCsr);
115529 116071     sqlite3_free(pCsr->aDoclist);
115530 116072     sqlite3_free(pCsr->aMatchinfo);
115531 116073     assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
115532 116074     sqlite3_free(pCsr);
115533 116075     return SQLITE_OK;
115534 116076   }
       116077  +
       116078  +/*
       116079  +** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
       116080  +** compose and prepare an SQL statement of the form:
       116081  +**
       116082  +**    "SELECT <columns> FROM %_content WHERE rowid = ?"
       116083  +**
       116084  +** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
       116085  +** it. If an error occurs, return an SQLite error code.
       116086  +**
       116087  +** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
       116088  +*/
       116089  +static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
       116090  +  int rc = SQLITE_OK;
       116091  +  if( pCsr->pStmt==0 ){
       116092  +    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
       116093  +    char *zSql;
       116094  +    zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
       116095  +    if( !zSql ) return SQLITE_NOMEM;
       116096  +    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
       116097  +    sqlite3_free(zSql);
       116098  +  }
       116099  +  *ppStmt = pCsr->pStmt;
       116100  +  return rc;
       116101  +}
115535 116102   
115536 116103   /*
115537 116104   ** Position the pCsr->pStmt statement so that it is on the row
115538 116105   ** of the %_content table that contains the last match.  Return
115539 116106   ** SQLITE_OK on success.  
115540 116107   */
115541 116108   static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
       116109  +  int rc = SQLITE_OK;
115542 116110     if( pCsr->isRequireSeek ){
115543         -    sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
115544         -    pCsr->isRequireSeek = 0;
115545         -    if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
115546         -      return SQLITE_OK;
115547         -    }else{
115548         -      int rc = sqlite3_reset(pCsr->pStmt);
115549         -      if( rc==SQLITE_OK ){
115550         -        /* If no row was found and no error has occured, then the %_content
115551         -        ** table is missing a row that is present in the full-text index.
115552         -        ** The data structures are corrupt.
115553         -        */
115554         -        rc = SQLITE_CORRUPT_VTAB;
115555         -      }
115556         -      pCsr->isEof = 1;
115557         -      if( pContext ){
115558         -        sqlite3_result_error_code(pContext, rc);
115559         -      }
115560         -      return rc;
115561         -    }
115562         -  }else{
115563         -    return SQLITE_OK;
115564         -  }
       116111  +    sqlite3_stmt *pStmt = 0;
       116112  +
       116113  +    rc = fts3CursorSeekStmt(pCsr, &pStmt);
       116114  +    if( rc==SQLITE_OK ){
       116115  +      sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
       116116  +      pCsr->isRequireSeek = 0;
       116117  +      if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
       116118  +        return SQLITE_OK;
       116119  +      }else{
       116120  +        rc = sqlite3_reset(pCsr->pStmt);
       116121  +        if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
       116122  +          /* If no row was found and no error has occured, then the %_content
       116123  +          ** table is missing a row that is present in the full-text index.
       116124  +          ** The data structures are corrupt.  */
       116125  +          rc = FTS_CORRUPT_VTAB;
       116126  +          pCsr->isEof = 1;
       116127  +        }
       116128  +      }
       116129  +    }
       116130  +  }
       116131  +
       116132  +  if( rc!=SQLITE_OK && pContext ){
       116133  +    sqlite3_result_error_code(pContext, rc);
       116134  +  }
       116135  +  return rc;
115565 116136   }
115566 116137   
115567 116138   /*
115568 116139   ** This function is used to process a single interior node when searching
115569 116140   ** a b-tree for a term or term prefix. The node data is passed to this 
115570 116141   ** function via the zNode/nNode parameters. The term to search for is
115571 116142   ** passed in zTerm/nTerm.
................................................................................
115607 116178     ** contents, or two zero bytes. Or, if the node is read from the %_segments
115608 116179     ** table, then there are always 20 bytes of zeroed padding following the
115609 116180     ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
115610 116181     */
115611 116182     zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
115612 116183     zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
115613 116184     if( zCsr>zEnd ){
115614         -    return SQLITE_CORRUPT_VTAB;
       116185  +    return FTS_CORRUPT_VTAB;
115615 116186     }
115616 116187     
115617 116188     while( zCsr<zEnd && (piFirst || piLast) ){
115618 116189       int cmp;                      /* memcmp() result */
115619 116190       int nSuffix;                  /* Size of term suffix */
115620 116191       int nPrefix = 0;              /* Size of term prefix */
115621 116192       int nBuffer;                  /* Total term size */
................................................................................
115625 116196       if( !isFirstTerm ){
115626 116197         zCsr += sqlite3Fts3GetVarint32(zCsr, &nPrefix);
115627 116198       }
115628 116199       isFirstTerm = 0;
115629 116200       zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix);
115630 116201       
115631 116202       if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
115632         -      rc = SQLITE_CORRUPT_VTAB;
       116203  +      rc = FTS_CORRUPT_VTAB;
115633 116204         goto finish_scan;
115634 116205       }
115635 116206       if( nPrefix+nSuffix>nAlloc ){
115636 116207         char *zNew;
115637 116208         nAlloc = (nPrefix+nSuffix) * 2;
115638 116209         zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
115639 116210         if( !zNew ){
115640 116211           rc = SQLITE_NOMEM;
115641 116212           goto finish_scan;
115642 116213         }
115643 116214         zBuffer = zNew;
115644 116215       }
       116216  +    assert( zBuffer );
115645 116217       memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
115646 116218       nBuffer = nPrefix + nSuffix;
115647 116219       zCsr += nSuffix;
115648 116220   
115649 116221       /* Compare the term we are searching for with the term just loaded from
115650 116222       ** the interior node. If the specified term is greater than or equal
115651 116223       ** to the term from the interior node, then all terms on the sub-tree 
................................................................................
115996 116568     char **pp,                      /* IN/OUT: Preallocated output buffer */
115997 116569     int nToken,                     /* Maximum difference in token positions */
115998 116570     int isSaveLeft,                 /* Save the left position */
115999 116571     int isExact,                    /* If *pp1 is exactly nTokens before *pp2 */
116000 116572     char **pp1,                     /* IN/OUT: Left input list */
116001 116573     char **pp2                      /* IN/OUT: Right input list */
116002 116574   ){
116003         -  char *p = (pp ? *pp : 0);
       116575  +  char *p = *pp;
116004 116576     char *p1 = *pp1;
116005 116577     char *p2 = *pp2;
116006 116578     int iCol1 = 0;
116007 116579     int iCol2 = 0;
116008 116580   
116009 116581     /* Never set both isSaveLeft and isExact for the same invocation. */
116010 116582     assert( isSaveLeft==0 || isExact==0 );
116011 116583   
116012         -  assert( *p1!=0 && *p2!=0 );
       116584  +  assert( p!=0 && *p1!=0 && *p2!=0 );
116013 116585     if( *p1==POS_COLUMN ){ 
116014 116586       p1++;
116015 116587       p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
116016 116588     }
116017 116589     if( *p2==POS_COLUMN ){ 
116018 116590       p2++;
116019 116591       p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
................................................................................
116022 116594     while( 1 ){
116023 116595       if( iCol1==iCol2 ){
116024 116596         char *pSave = p;
116025 116597         sqlite3_int64 iPrev = 0;
116026 116598         sqlite3_int64 iPos1 = 0;
116027 116599         sqlite3_int64 iPos2 = 0;
116028 116600   
116029         -      if( pp && iCol1 ){
       116601  +      if( iCol1 ){
116030 116602           *p++ = POS_COLUMN;
116031 116603           p += sqlite3Fts3PutVarint(p, iCol1);
116032 116604         }
116033 116605   
116034 116606         assert( *p1!=POS_END && *p1!=POS_COLUMN );
116035 116607         assert( *p2!=POS_END && *p2!=POS_COLUMN );
116036 116608         fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
................................................................................
116037 116609         fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
116038 116610   
116039 116611         while( 1 ){
116040 116612           if( iPos2==iPos1+nToken 
116041 116613            || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken) 
116042 116614           ){
116043 116615             sqlite3_int64 iSave;
116044         -          if( !pp ){
116045         -            fts3PoslistCopy(0, &p2);
116046         -            fts3PoslistCopy(0, &p1);
116047         -            *pp1 = p1;
116048         -            *pp2 = p2;
116049         -            return 1;
116050         -          }
116051 116616             iSave = isSaveLeft ? iPos1 : iPos2;
116052 116617             fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
116053 116618             pSave = 0;
       116619  +          assert( p );
116054 116620           }
116055 116621           if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
116056 116622             if( (*p2&0xFE)==0 ) break;
116057 116623             fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
116058 116624           }else{
116059 116625             if( (*p1&0xFE)==0 ) break;
116060 116626             fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
................................................................................
116095 116661       }
116096 116662     }
116097 116663   
116098 116664     fts3PoslistCopy(0, &p2);
116099 116665     fts3PoslistCopy(0, &p1);
116100 116666     *pp1 = p1;
116101 116667     *pp2 = p2;
116102         -  if( !pp || *pp==p ){
       116668  +  if( *pp==p ){
116103 116669       return 0;
116104 116670     }
116105 116671     *p++ = 0x00;
116106 116672     *pp = p;
116107 116673     return 1;
116108 116674   }
116109 116675   
................................................................................
116397 116963         fts3PoslistCopy(0, &p2);
116398 116964         fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
116399 116965       }
116400 116966     }
116401 116967   
116402 116968     *pnRight = p - aOut;
116403 116969   }
       116970  +
       116971  +/*
       116972  +** Argument pList points to a position list nList bytes in size. This
       116973  +** function checks to see if the position list contains any entries for
       116974  +** a token in position 0 (of any column). If so, it writes argument iDelta
       116975  +** to the output buffer pOut, followed by a position list consisting only
       116976  +** of the entries from pList at position 0, and terminated by an 0x00 byte.
       116977  +** The value returned is the number of bytes written to pOut (if any).
       116978  +*/
       116979  +SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
       116980  +  sqlite3_int64 iDelta,           /* Varint that may be written to pOut */
       116981  +  char *pList,                    /* Position list (no 0x00 term) */
       116982  +  int nList,                      /* Size of pList in bytes */
       116983  +  char *pOut                      /* Write output here */
       116984  +){
       116985  +  int nOut = 0;
       116986  +  int bWritten = 0;               /* True once iDelta has been written */
       116987  +  char *p = pList;
       116988  +  char *pEnd = &pList[nList];
       116989  +
       116990  +  if( *p!=0x01 ){
       116991  +    if( *p==0x02 ){
       116992  +      nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
       116993  +      pOut[nOut++] = 0x02;
       116994  +      bWritten = 1;
       116995  +    }
       116996  +    fts3ColumnlistCopy(0, &p);
       116997  +  }
       116998  +
       116999  +  while( p<pEnd && *p==0x01 ){
       117000  +    sqlite3_int64 iCol;
       117001  +    p++;
       117002  +    p += sqlite3Fts3GetVarint(p, &iCol);
       117003  +    if( *p==0x02 ){
       117004  +      if( bWritten==0 ){
       117005  +        nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
       117006  +        bWritten = 1;
       117007  +      }
       117008  +      pOut[nOut++] = 0x01;
       117009  +      nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
       117010  +      pOut[nOut++] = 0x02;
       117011  +    }
       117012  +    fts3ColumnlistCopy(0, &p);
       117013  +  }
       117014  +  if( bWritten ){
       117015  +    pOut[nOut++] = 0x00;
       117016  +  }
       117017  +
       117018  +  return nOut;
       117019  +}
116404 117020   
116405 117021   
116406 117022   /*
116407 117023   ** Merge all doclists in the TermSelect.aaOutput[] array into a single
116408 117024   ** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
116409 117025   ** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
116410 117026   **
................................................................................
116754 117370     Fts3SegFilter filter;           /* Segment term filter configuration */
116755 117371   
116756 117372     pSegcsr = pTok->pSegcsr;
116757 117373     memset(&tsc, 0, sizeof(TermSelect));
116758 117374   
116759 117375     filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
116760 117376           | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
       117377  +        | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
116761 117378           | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
116762 117379     filter.iCol = iColumn;
116763 117380     filter.zTerm = pTok->z;
116764 117381     filter.nTerm = pTok->n;
116765 117382   
116766 117383     rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
116767 117384     while( SQLITE_OK==rc
................................................................................
116894 117511       int iCol = idxNum-FTS3_FULLTEXT_SEARCH;
116895 117512       const char *zQuery = (const char *)sqlite3_value_text(apVal[0]);
116896 117513   
116897 117514       if( zQuery==0 && sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
116898 117515         return SQLITE_NOMEM;
116899 117516       }
116900 117517   
116901         -    rc = sqlite3Fts3ExprParse(p->pTokenizer, p->azColumn, p->nColumn, 
116902         -        iCol, zQuery, -1, &pCsr->pExpr
       117518  +    rc = sqlite3Fts3ExprParse(p->pTokenizer, p->azColumn, p->bHasStat, 
       117519  +        p->nColumn, iCol, zQuery, -1, &pCsr->pExpr
116903 117520       );
116904 117521       if( rc!=SQLITE_OK ){
116905 117522         if( rc==SQLITE_ERROR ){
116906 117523           static const char *zErr = "malformed MATCH expression: [%s]";
116907 117524           p->base.zErrMsg = sqlite3_mprintf(zErr, zQuery);
116908 117525         }
116909 117526         return rc;
................................................................................
116922 117539   
116923 117540     /* Compile a SELECT statement for this cursor. For a full-table-scan, the
116924 117541     ** statement loops through all rows of the %_content table. For a
116925 117542     ** full-text query or docid lookup, the statement retrieves a single
116926 117543     ** row by docid.
116927 117544     */
116928 117545     if( idxNum==FTS3_FULLSCAN_SEARCH ){
116929         -    const char *zSort = (pCsr->bDesc ? "DESC" : "ASC");
116930         -    const char *zTmpl = "SELECT %s FROM %Q.'%q_content' AS x ORDER BY docid %s";
116931         -    zSql = sqlite3_mprintf(zTmpl, p->zReadExprlist, p->zDb, p->zName, zSort);
116932         -  }else{
116933         -    const char *zTmpl = "SELECT %s FROM %Q.'%q_content' AS x WHERE docid = ?";
116934         -    zSql = sqlite3_mprintf(zTmpl, p->zReadExprlist, p->zDb, p->zName);
       117546  +    zSql = sqlite3_mprintf(
       117547  +        "SELECT %s ORDER BY rowid %s",
       117548  +        p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
       117549  +    );
       117550  +    if( zSql ){
       117551  +      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
       117552  +      sqlite3_free(zSql);
       117553  +    }else{
       117554  +      rc = SQLITE_NOMEM;
       117555  +    }
       117556  +  }else if( idxNum==FTS3_DOCID_SEARCH ){
       117557  +    rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
       117558  +    if( rc==SQLITE_OK ){
       117559  +      rc = sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
       117560  +    }
116935 117561     }
116936         -  if( !zSql ) return SQLITE_NOMEM;
116937         -  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
116938         -  sqlite3_free(zSql);
116939 117562     if( rc!=SQLITE_OK ) return rc;
116940 117563   
116941         -  if( idxNum==FTS3_DOCID_SEARCH ){
116942         -    rc = sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
116943         -    if( rc!=SQLITE_OK ) return rc;
116944         -  }
116945         -
116946 117564     return fts3NextMethod(pCursor);
116947 117565   }
116948 117566   
116949 117567   /* 
116950 117568   ** This is the xEof method of the virtual table. SQLite calls this 
116951 117569   ** routine to find out if it has reached the end of a result set.
116952 117570   */
................................................................................
116990 117608     }else if( iCol==p->nColumn ){
116991 117609       /* The extra column whose name is the same as the table.
116992 117610       ** Return a blob which is a pointer to the cursor.
116993 117611       */
116994 117612       sqlite3_result_blob(pContext, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT);
116995 117613     }else{
116996 117614       rc = fts3CursorSeek(0, pCsr);
116997         -    if( rc==SQLITE_OK ){
       117615  +    if( rc==SQLITE_OK && sqlite3_data_count(pCsr->pStmt)>(iCol+1) ){
116998 117616         sqlite3_result_value(pContext, sqlite3_column_value(pCsr->pStmt, iCol+1));
116999 117617       }
117000 117618     }
117001 117619   
117002 117620     assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
117003 117621     return rc;
117004 117622   }
................................................................................
117074 117692   ** When called, *ppPoslist must point to the byte immediately following the
117075 117693   ** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
117076 117694   ** moves *ppPoslist so that it instead points to the first byte of the
117077 117695   ** same position list.
117078 117696   */
117079 117697   static void fts3ReversePoslist(char *pStart, char **ppPoslist){
117080 117698     char *p = &(*ppPoslist)[-2];
117081         -  char c;
       117699  +  char c = 0;
117082 117700   
117083 117701     while( p>pStart && (c=*p--)==0 );
117084 117702     while( p>pStart && (*p & 0x80) | c ){ 
117085 117703       c = *p--; 
117086 117704     }
117087 117705     if( p>pStart ){ p = &p[2]; }
117088 117706     while( *p++&0x80 );
................................................................................
117283 117901     sqlite3_vtab *pVtab,            /* Virtual table handle */
117284 117902     const char *zName               /* New name of table */
117285 117903   ){
117286 117904     Fts3Table *p = (Fts3Table *)pVtab;
117287 117905     sqlite3 *db = p->db;            /* Database connection */
117288 117906     int rc;                         /* Return Code */
117289 117907   
       117908  +  /* As it happens, the pending terms table is always empty here. This is
       117909  +  ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction 
       117910  +  ** always opens a savepoint transaction. And the xSavepoint() method 
       117911  +  ** flushes the pending terms table. But leave the (no-op) call to
       117912  +  ** PendingTermsFlush() in in case that changes.
       117913  +  */
       117914  +  assert( p->nPendingData==0 );
117290 117915     rc = sqlite3Fts3PendingTermsFlush(p);
117291         -  if( rc!=SQLITE_OK ){
117292         -    return rc;
       117916  +
       117917  +  if( p->zContentTbl==0 ){
       117918  +    fts3DbExec(&rc, db,
       117919  +      "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';",
       117920  +      p->zDb, p->zName, zName
       117921  +    );
117293 117922     }
117294 117923   
117295         -  fts3DbExec(&rc, db,
117296         -    "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';",
117297         -    p->zDb, p->zName, zName
117298         -  );
117299 117924     if( p->bHasDocsize ){
117300 117925       fts3DbExec(&rc, db,
117301 117926         "ALTER TABLE %Q.'%q_docsize'  RENAME TO '%q_docsize';",
117302 117927         p->zDb, p->zName, zName
117303 117928       );
117304 117929     }
117305 117930     if( p->bHasStat ){
................................................................................
117650 118275   ** means that the phrase does not appear in the current row, doclist.pList
117651 118276   ** and doclist.nList are both zeroed.
117652 118277   **
117653 118278   ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
117654 118279   */
117655 118280   static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
117656 118281     int iToken;                     /* Used to iterate through phrase tokens */
117657         -  int rc = SQLITE_OK;             /* Return code */
117658 118282     char *aPoslist = 0;             /* Position list for deferred tokens */
117659 118283     int nPoslist = 0;               /* Number of bytes in aPoslist */
117660 118284     int iPrev = -1;                 /* Token number of previous deferred token */
117661 118285   
117662 118286     assert( pPhrase->doclist.bFreeList==0 );
117663 118287   
117664         -  for(iToken=0; rc==SQLITE_OK && iToken<pPhrase->nToken; iToken++){
       118288  +  for(iToken=0; iToken<pPhrase->nToken; iToken++){
117665 118289       Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
117666 118290       Fts3DeferredToken *pDeferred = pToken->pDeferred;
117667 118291   
117668 118292       if( pDeferred ){
117669 118293         char *pList;
117670 118294         int nList;
117671         -      rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
       118295  +      int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
117672 118296         if( rc!=SQLITE_OK ) return rc;
117673 118297   
117674 118298         if( pList==0 ){
117675 118299           sqlite3_free(aPoslist);
117676 118300           pPhrase->doclist.pList = 0;
117677 118301           pPhrase->doclist.nList = 0;
117678 118302           return SQLITE_OK;
................................................................................
117765 118389     Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
117766 118390   
117767 118391     if( pCsr->bDesc==pTab->bDescIdx 
117768 118392      && bOptOk==1 
117769 118393      && p->nToken==1 
117770 118394      && pFirst->pSegcsr 
117771 118395      && pFirst->pSegcsr->bLookup 
       118396  +   && pFirst->bFirst==0
117772 118397     ){
117773 118398       /* Use the incremental approach. */
117774 118399       int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
117775 118400       rc = sqlite3Fts3MsrIncrStart(
117776 118401           pTab, pFirst->pSegcsr, iCol, pFirst->z, pFirst->n);
117777 118402       p->bIncr = 1;
117778 118403   
................................................................................
117994 118619     Fts3Cursor *pCsr,               /* FTS Cursor handle */
117995 118620     Fts3Expr *pRoot,                /* Root of current AND/NEAR cluster */
117996 118621     Fts3Expr *pExpr,                /* Expression to consider */
117997 118622     Fts3TokenAndCost **ppTC,        /* Write new entries to *(*ppTC)++ */
117998 118623     Fts3Expr ***ppOr,               /* Write new OR root to *(*ppOr)++ */
117999 118624     int *pRc                        /* IN/OUT: Error code */
118000 118625   ){
118001         -  if( *pRc==SQLITE_OK && pExpr ){
       118626  +  if( *pRc==SQLITE_OK ){
118002 118627       if( pExpr->eType==FTSQUERY_PHRASE ){
118003 118628         Fts3Phrase *pPhrase = pExpr->pPhrase;
118004 118629         int i;
118005 118630         for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
118006 118631           Fts3TokenAndCost *pTC = (*ppTC)++;
118007 118632           pTC->pPhrase = pPhrase;
118008 118633           pTC->iToken = i;
118009 118634           pTC->pRoot = pRoot;
118010 118635           pTC->pToken = &pPhrase->aToken[i];
118011 118636           pTC->iCol = pPhrase->iColumn;
118012 118637           *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
118013 118638         }
118014 118639       }else if( pExpr->eType!=FTSQUERY_NOT ){
       118640  +      assert( pExpr->eType==FTSQUERY_OR
       118641  +           || pExpr->eType==FTSQUERY_AND
       118642  +           || pExpr->eType==FTSQUERY_NEAR
       118643  +      );
       118644  +      assert( pExpr->pLeft && pExpr->pRight );
118015 118645         if( pExpr->eType==FTSQUERY_OR ){
118016 118646           pRoot = pExpr->pLeft;
118017 118647           **ppOr = pRoot;
118018 118648           (*ppOr)++;
118019 118649         }
118020 118650         fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
118021 118651         if( pExpr->eType==FTSQUERY_OR ){
................................................................................
118068 118698       pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
118069 118699       a += sqlite3Fts3GetVarint(a, &nDoc);
118070 118700       while( a<pEnd ){
118071 118701         a += sqlite3Fts3GetVarint(a, &nByte);
118072 118702       }
118073 118703       if( nDoc==0 || nByte==0 ){
118074 118704         sqlite3_reset(pStmt);
118075         -      return SQLITE_CORRUPT_VTAB;
       118705  +      return FTS_CORRUPT_VTAB;
118076 118706       }
118077 118707   
118078 118708       pCsr->nDoc = nDoc;
118079 118709       pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
118080 118710       assert( pCsr->nRowAvg>0 ); 
118081 118711       rc = sqlite3_reset(pStmt);
118082 118712       if( rc!=SQLITE_OK ) return rc;
................................................................................
118111 118741     int rc = SQLITE_OK;             /* Return code */
118112 118742     int ii;                         /* Iterator variable for various purposes */
118113 118743     int nOvfl = 0;                  /* Total overflow pages used by doclists */
118114 118744     int nToken = 0;                 /* Total number of tokens in cluster */
118115 118745   
118116 118746     int nMinEst = 0;                /* The minimum count for any phrase so far. */
118117 118747     int nLoad4 = 1;                 /* (Phrases that will be loaded)^4. */
       118748  +
       118749  +  /* Tokens are never deferred for FTS tables created using the content=xxx
       118750  +  ** option. The reason being that it is not guaranteed that the content
       118751  +  ** table actually contains the same data as the index. To prevent this from
       118752  +  ** causing any problems, the deferred token optimization is completely
       118753  +  ** disabled for content=xxx tables. */
       118754  +  if( pTab->zContentTbl ){
       118755  +    return SQLITE_OK;
       118756  +  }
118118 118757   
118119 118758     /* Count the tokens in this AND/NEAR cluster. If none of the doclists
118120 118759     ** associated with the tokens spill onto overflow pages, or if there is
118121 118760     ** only 1 token, exit early. No tokens to defer in this case. */
118122 118761     for(ii=0; ii<nTC; ii++){
118123 118762       if( aTC[ii].pRoot==pRoot ){
118124 118763         nOvfl += aTC[ii].nOvfl;
................................................................................
118174 118813         ** that will be loaded if all subsequent tokens are deferred.
118175 118814         */
118176 118815         Fts3PhraseToken *pToken = pTC->pToken;
118177 118816         rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
118178 118817         fts3SegReaderCursorFree(pToken->pSegcsr);
118179 118818         pToken->pSegcsr = 0;
118180 118819       }else{
118181         -      nLoad4 = nLoad4*4;
       118820  +      /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
       118821  +      ** for-loop. Except, limit the value to 2^24 to prevent it from 
       118822  +      ** overflowing the 32-bit integer it is stored in. */
       118823  +      if( ii<12 ) nLoad4 = nLoad4*4;
       118824  +
118182 118825         if( ii==0 || pTC->pPhrase->nToken>1 ){
118183 118826           /* Either this is the cheapest token in the entire query, or it is
118184 118827           ** part of a multi-token phrase. Either way, the entire doclist will
118185 118828           ** (eventually) be loaded into memory. It may as well be now. */
118186 118829           Fts3PhraseToken *pToken = pTC->pToken;
118187 118830           int nList = 0;
118188 118831           char *pList = 0;
................................................................................
118544 119187           int nNear = p->nNear;
118545 119188           res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
118546 119189         }
118547 119190     
118548 119191         aPoslist = pExpr->pRight->pPhrase->doclist.pList;
118549 119192         nToken = pExpr->pRight->pPhrase->nToken;
118550 119193         for(p=pExpr->pLeft; p && res; p=p->pLeft){
118551         -        int nNear = p->pParent->nNear;
118552         -        Fts3Phrase *pPhrase = (
       119194  +        int nNear;
       119195  +        Fts3Phrase *pPhrase;
       119196  +        assert( p->pParent && p->pParent->pLeft==p );
       119197  +        nNear = p->pParent->nNear;
       119198  +        pPhrase = (
118553 119199               p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
118554 119200           );
118555 119201           res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
118556 119202         }
118557 119203       }
118558 119204   
118559 119205       sqlite3_free(aTmp);
................................................................................
119035 119681       for(i=0; i<pPhrase->nToken; i++){
119036 119682         fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
119037 119683         pPhrase->aToken[i].pSegcsr = 0;
119038 119684       }
119039 119685     }
119040 119686   }
119041 119687   
       119688  +/*
       119689  +** Return SQLITE_CORRUPT_VTAB.
       119690  +*/
       119691  +#ifdef SQLITE_DEBUG
       119692  +SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
       119693  +  return SQLITE_CORRUPT_VTAB;
       119694  +}
       119695  +#endif
       119696  +
119042 119697   #if !SQLITE_CORE
119043 119698   /*
119044 119699   ** Initialize API pointer table, if required.
119045 119700   */
119046 119701   SQLITE_API int sqlite3_extension_init(
119047 119702     sqlite3 *db, 
119048 119703     char **pzErrMsg,
................................................................................
119623 120278   **   FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
119624 120279   **   zero.
119625 120280   */
119626 120281   typedef struct ParseContext ParseContext;
119627 120282   struct ParseContext {
119628 120283     sqlite3_tokenizer *pTokenizer;      /* Tokenizer module */
119629 120284     const char **azCol;                 /* Array of column names for fts3 table */
       120285  +  int bFts4;                          /* True to allow FTS4-only syntax */
119630 120286     int nCol;                           /* Number of entries in azCol[] */
119631 120287     int iDefaultCol;                    /* Default column to query */
119632 120288     int isNot;                          /* True if getNextNode() sees a unary - */
119633 120289     sqlite3_context *pCtx;              /* Write error message here */
119634 120290     int nNest;                          /* Number of nested brackets */
119635 120291   };
119636 120292   
................................................................................
119710 120366           pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
119711 120367           memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
119712 120368   
119713 120369           if( iEnd<n && z[iEnd]=='*' ){
119714 120370             pRet->pPhrase->aToken[0].isPrefix = 1;
119715 120371             iEnd++;
119716 120372           }
119717         -        if( !sqlite3_fts3_enable_parentheses && iStart>0 && z[iStart-1]=='-' ){
119718         -          pParse->isNot = 1;
       120373  +
       120374  +        while( 1 ){
       120375  +          if( !sqlite3_fts3_enable_parentheses 
       120376  +           && iStart>0 && z[iStart-1]=='-' 
       120377  +          ){
       120378  +            pParse->isNot = 1;
       120379  +            iStart--;
       120380  +          }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
       120381  +            pRet->pPhrase->aToken[0].bFirst = 1;
       120382  +            iStart--;
       120383  +          }else{
       120384  +            break;
       120385  +          }
119719 120386           }
       120387  +
119720 120388         }
119721 120389         nConsumed = iEnd;
119722 120390       }
119723 120391   
119724 120392       pModule->xClose(pCursor);
119725 120393     }
119726 120394     
................................................................................
119811 120479           memset(pToken, 0, sizeof(Fts3PhraseToken));
119812 120480   
119813 120481           memcpy(&zTemp[nTemp], zByte, nByte);
119814 120482           nTemp += nByte;
119815 120483   
119816 120484           pToken->n = nByte;
119817 120485           pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
       120486  +        pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
119818 120487           nToken = ii+1;
119819 120488         }
119820 120489       }
119821 120490   
119822 120491       pModule->xClose(pCursor);
119823 120492       pCursor = 0;
119824 120493     }
................................................................................
119832 120501       memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
119833 120502       p->eType = FTSQUERY_PHRASE;
119834 120503       p->pPhrase = (Fts3Phrase *)&p[1];
119835 120504       p->pPhrase->iColumn = pParse->iDefaultCol;
119836 120505       p->pPhrase->nToken = nToken;
119837 120506   
119838 120507       zBuf = (char *)&p->pPhrase->aToken[nToken];
119839         -    memcpy(zBuf, zTemp, nTemp);
119840         -    sqlite3_free(zTemp);
       120508  +    if( zTemp ){
       120509  +      memcpy(zBuf, zTemp, nTemp);
       120510  +      sqlite3_free(zTemp);
       120511  +    }else{
       120512  +      assert( nTemp==0 );
       120513  +    }
119841 120514   
119842 120515       for(jj=0; jj<p->pPhrase->nToken; jj++){
119843 120516         p->pPhrase->aToken[jj].z = zBuf;
119844 120517         zBuf += p->pPhrase->aToken[jj].n;
119845 120518       }
119846 120519       rc = SQLITE_OK;
119847 120520     }
................................................................................
120258 120931   ** column to match against for tokens for which a column name is not explicitly
120259 120932   ** specified as part of the query string), or -1 if tokens may by default
120260 120933   ** match any table column.
120261 120934   */
120262 120935   SQLITE_PRIVATE int sqlite3Fts3ExprParse(
120263 120936     sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
120264 120937     char **azCol,                       /* Array of column names for fts3 table */
       120938  +  int bFts4,                          /* True to allow FTS4-only syntax */
120265 120939     int nCol,                           /* Number of entries in azCol[] */
120266 120940     int iDefaultCol,                    /* Default column to query */
120267 120941     const char *z, int n,               /* Text of MATCH query */
120268 120942     Fts3Expr **ppExpr                   /* OUT: Parsed query structure */
120269 120943   ){
120270 120944     int nParsed;
120271 120945     int rc;
120272 120946     ParseContext sParse;
120273 120947     sParse.pTokenizer = pTokenizer;
120274 120948     sParse.azCol = (const char **)azCol;
120275 120949     sParse.nCol = nCol;
120276 120950     sParse.iDefaultCol = iDefaultCol;
120277 120951     sParse.nNest = 0;
       120952  +  sParse.bFts4 = bFts4;
120278 120953     if( z==0 ){
120279 120954       *ppExpr = 0;
120280 120955       return SQLITE_OK;
120281 120956     }
120282 120957     if( n<0 ){
120283 120958       n = (int)strlen(z);
120284 120959     }
................................................................................
120460 121135       goto exprtest_out;
120461 121136     }
120462 121137     for(ii=0; ii<nCol; ii++){
120463 121138       azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
120464 121139     }
120465 121140   
120466 121141     rc = sqlite3Fts3ExprParse(
120467         -      pTokenizer, azCol, nCol, nCol, zExpr, nExpr, &pExpr
       121142  +      pTokenizer, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
120468 121143     );
120469 121144     if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
120470 121145       sqlite3_result_error(context, "Error parsing expression", -1);
120471 121146     }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
120472 121147       sqlite3_result_error_nomem(context);
120473 121148     }else{
120474 121149       sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
................................................................................
122507 123182   /* 0  */  "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
122508 123183   /* 1  */  "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
122509 123184   /* 2  */  "DELETE FROM %Q.'%q_content'",
122510 123185   /* 3  */  "DELETE FROM %Q.'%q_segments'",
122511 123186   /* 4  */  "DELETE FROM %Q.'%q_segdir'",
122512 123187   /* 5  */  "DELETE FROM %Q.'%q_docsize'",
122513 123188   /* 6  */  "DELETE FROM %Q.'%q_stat'",
122514         -/* 7  */  "SELECT %s FROM %Q.'%q_content' AS x WHERE rowid=?",
       123189  +/* 7  */  "SELECT %s WHERE rowid=?",
122515 123190   /* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
122516 123191   /* 9  */  "INSERT INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
122517 123192   /* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
122518 123193   /* 11 */  "INSERT INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
122519 123194   
122520 123195             /* Return segments in order from oldest to newest.*/ 
122521 123196   /* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
................................................................................
122549 123224     
122550 123225     pStmt = p->aStmt[eStmt];
122551 123226     if( !pStmt ){
122552 123227       char *zSql;
122553 123228       if( eStmt==SQL_CONTENT_INSERT ){
122554 123229         zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
122555 123230       }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
122556         -      zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist, p->zDb, p->zName);
       123231  +      zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
122557 123232       }else{
122558 123233         zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
122559 123234       }
122560 123235       if( !zSql ){
122561 123236         rc = SQLITE_NOMEM;
122562 123237       }else{
122563 123238         rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL);
................................................................................
122592 123267     if( rc==SQLITE_OK ){
122593 123268       if( eStmt==SQL_SELECT_DOCSIZE ){
122594 123269         sqlite3_bind_int64(pStmt, 1, iDocid);
122595 123270       }
122596 123271       rc = sqlite3_step(pStmt);
122597 123272       if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
122598 123273         rc = sqlite3_reset(pStmt);
122599         -      if( rc==SQLITE_OK ) rc = SQLITE_CORRUPT_VTAB;
       123274  +      if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
122600 123275         pStmt = 0;
122601 123276       }else{
122602 123277         rc = SQLITE_OK;
122603 123278       }
122604 123279     }
122605 123280   
122606 123281     *ppStmt = pStmt;
................................................................................
122660 123335   ** write-locks on the %_segments and %_segdir ** tables). 
122661 123336   **
122662 123337   ** We try to avoid this because if FTS3 returns any error when committing
122663 123338   ** a transaction, the whole transaction will be rolled back. And this is
122664 123339   ** not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. It can
122665 123340   ** still happen if the user reads data directly from the %_segments or
122666 123341   ** %_segdir tables instead of going through FTS3 though.
       123342  +**
       123343  +** This reasoning does not apply to a content=xxx table.
122667 123344   */
122668 123345   SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *p){
122669 123346     int rc;                         /* Return code */
122670 123347     sqlite3_stmt *pStmt;            /* Statement used to obtain lock */
122671 123348   
122672         -  rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0);
122673         -  if( rc==SQLITE_OK ){
122674         -    sqlite3_bind_null(pStmt, 1);
122675         -    sqlite3_step(pStmt);
122676         -    rc = sqlite3_reset(pStmt);
       123349  +  if( p->zContentTbl==0 ){
       123350  +    rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0);
       123351  +    if( rc==SQLITE_OK ){
       123352  +      sqlite3_bind_null(pStmt, 1);
       123353  +      sqlite3_step(pStmt);
       123354  +      rc = sqlite3_reset(pStmt);
       123355  +    }
       123356  +  }else{
       123357  +    rc = SQLITE_OK;
122677 123358     }
       123359  +
122678 123360     return rc;
122679 123361   }
122680 123362   
122681 123363   /*
122682 123364   ** Set *ppStmt to a statement handle that may be used to iterate through
122683 123365   ** all rows in the %_segdir table, from oldest to newest. If successful,
122684 123366   ** return SQLITE_OK. If an error occurs while preparing the statement, 
................................................................................
123030 123712   static int fts3InsertData(
123031 123713     Fts3Table *p,                   /* Full-text table */
123032 123714     sqlite3_value **apVal,          /* Array of values to insert */
123033 123715     sqlite3_int64 *piDocid          /* OUT: Docid for row just inserted */
123034 123716   ){
123035 123717     int rc;                         /* Return code */
123036 123718     sqlite3_stmt *pContentInsert;   /* INSERT INTO %_content VALUES(...) */
       123719  +
       123720  +  if( p->zContentTbl ){
       123721  +    sqlite3_value *pRowid = apVal[p->nColumn+3];
       123722  +    if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
       123723  +      pRowid = apVal[1];
       123724  +    }
       123725  +    if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
       123726  +      return SQLITE_CONSTRAINT;
       123727  +    }
       123728  +    *piDocid = sqlite3_value_int64(pRowid);
       123729  +    return SQLITE_OK;
       123730  +  }
123037 123731   
123038 123732     /* Locate the statement handle used to insert data into the %_content
123039 123733     ** table. The SQL for this statement is:
123040 123734     **
123041 123735     **   INSERT INTO %_content VALUES(?, ?, ?, ...)
123042 123736     **
123043 123737     ** The statement features N '?' variables, where N is the number of user
................................................................................
123081 123775   
123082 123776   
123083 123777   
123084 123778   /*
123085 123779   ** Remove all data from the FTS3 table. Clear the hash table containing
123086 123780   ** pending terms.
123087 123781   */
123088         -static int fts3DeleteAll(Fts3Table *p){
       123782  +static int fts3DeleteAll(Fts3Table *p, int bContent){
123089 123783     int rc = SQLITE_OK;             /* Return code */
123090 123784   
123091 123785     /* Discard the contents of the pending-terms hash table. */
123092 123786     sqlite3Fts3PendingTermsClear(p);
123093 123787   
123094         -  /* Delete everything from the %_content, %_segments and %_segdir tables. */
123095         -  fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
       123788  +  /* Delete everything from the shadow tables. Except, leave %_content as
       123789  +  ** is if bContent is false.  */
       123790  +  assert( p->zContentTbl==0 || bContent==0 );
       123791  +  if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
123096 123792     fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
123097 123793     fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
123098 123794     if( p->bHasDocsize ){
123099 123795       fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
123100 123796     }
123101 123797     if( p->bHasStat ){
123102 123798       fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
................................................................................
123396 124092     /* Because of the FTS3_NODE_PADDING bytes of padding, the following is 
123397 124093     ** safe (no risk of overread) even if the node data is corrupted. */
123398 124094     pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix);
123399 124095     pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix);
123400 124096     if( nPrefix<0 || nSuffix<=0 
123401 124097      || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] 
123402 124098     ){
123403         -    return SQLITE_CORRUPT_VTAB;
       124099  +    return FTS_CORRUPT_VTAB;
123404 124100     }
123405 124101   
123406 124102     if( nPrefix+nSuffix>pReader->nTermAlloc ){
123407 124103       int nNew = (nPrefix+nSuffix)*2;
123408 124104       char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
123409 124105       if( !zNew ){
123410 124106         return SQLITE_NOMEM;
................................................................................
123426 124122     /* Check that the doclist does not appear to extend past the end of the
123427 124123     ** b-tree node. And that the final byte of the doclist is 0x00. If either 
123428 124124     ** of these statements is untrue, then the data structure is corrupt.
123429 124125     */
123430 124126     if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] 
123431 124127      || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
123432 124128     ){
123433         -    return SQLITE_CORRUPT_VTAB;
       124129  +    return FTS_CORRUPT_VTAB;
123434 124130     }
123435 124131     return SQLITE_OK;
123436 124132   }
123437 124133   
123438 124134   /*
123439 124135   ** Set the SegReader to point to the first docid in the doclist associated
123440 124136   ** with the current term.
................................................................................
124376 125072   ** If successful, *pisEmpty is set to true if the table is empty except for
124377 125073   ** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
124378 125074   ** error occurs, an SQLite error code is returned.
124379 125075   */
124380 125076   static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
124381 125077     sqlite3_stmt *pStmt;
124382 125078     int rc;
124383         -  rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
124384         -  if( rc==SQLITE_OK ){
124385         -    if( SQLITE_ROW==sqlite3_step(pStmt) ){
124386         -      *pisEmpty = sqlite3_column_int(pStmt, 0);
       125079  +  if( p->zContentTbl ){
       125080  +    /* If using the content=xxx option, assume the table is never empty */
       125081  +    *pisEmpty = 0;
       125082  +    rc = SQLITE_OK;
       125083  +  }else{
       125084  +    rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
       125085  +    if( rc==SQLITE_OK ){
       125086  +      if( SQLITE_ROW==sqlite3_step(pStmt) ){
       125087  +        *pisEmpty = sqlite3_column_int(pStmt, 0);
       125088  +      }
       125089  +      rc = sqlite3_reset(pStmt);
124387 125090       }
124388         -    rc = sqlite3_reset(pStmt);
124389 125091     }
124390 125092     return rc;
124391 125093   }
124392 125094   
124393 125095   /*
124394 125096   ** Set *pnMax to the largest segment level in the database for the index
124395 125097   ** iIndex.
................................................................................
124733 125435     int rc = SQLITE_OK;
124734 125436   
124735 125437     int isIgnoreEmpty =  (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
124736 125438     int isRequirePos =   (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
124737 125439     int isColFilter =    (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
124738 125440     int isPrefix =       (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
124739 125441     int isScan =         (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
       125442  +  int isFirst =        (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
124740 125443   
124741 125444     Fts3SegReader **apSegment = pCsr->apSegment;
124742 125445     int nSegment = pCsr->nSegment;
124743 125446     Fts3SegFilter *pFilter = pCsr->pFilter;
124744 125447     int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
124745 125448       p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
124746 125449     );
................................................................................
124792 125495       ){
124793 125496         nMerge++;
124794 125497       }
124795 125498   
124796 125499       assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
124797 125500       if( nMerge==1 
124798 125501        && !isIgnoreEmpty 
       125502  +     && !isFirst 
124799 125503        && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
124800 125504       ){
124801 125505         pCsr->nDoclist = apSegment[0]->nDoclist;
124802 125506         if( fts3SegReaderIsPending(apSegment[0]) ){
124803 125507           rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
124804 125508           pCsr->aDoclist = pCsr->aBuffer;
124805 125509         }else{
................................................................................
124857 125561               pCsr->nBuffer = (nDoclist+nByte)*2;
124858 125562               aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
124859 125563               if( !aNew ){
124860 125564                 return SQLITE_NOMEM;
124861 125565               }
124862 125566               pCsr->aBuffer = aNew;
124863 125567             }
124864         -          nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
124865         -          iPrev = iDocid;
124866         -          if( isRequirePos ){
124867         -            memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
124868         -            nDoclist += nList;
124869         -            pCsr->aBuffer[nDoclist++] = '\0';
       125568  +
       125569  +          if( isFirst ){
       125570  +            char *a = &pCsr->aBuffer[nDoclist];
       125571  +            int nWrite;
       125572  +           
       125573  +            nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
       125574  +            if( nWrite ){
       125575  +              iPrev = iDocid;
       125576  +              nDoclist += nWrite;
       125577  +            }
       125578  +          }else{
       125579  +            nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
       125580  +            iPrev = iDocid;
       125581  +            if( isRequirePos ){
       125582  +              memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
       125583  +              nDoclist += nList;
       125584  +              pCsr->aBuffer[nDoclist++] = '\0';
       125585  +            }
124870 125586             }
124871 125587           }
124872 125588   
124873 125589           fts3SegReaderSort(apSegment, nMerge, j, xCmp);
124874 125590         }
124875 125591         if( nDoclist>0 ){
124876 125592           pCsr->aDoclist = pCsr->aBuffer;
................................................................................
125038 125754   
125039 125755   /*
125040 125756   ** Insert the sizes (in tokens) for each column of the document
125041 125757   ** with docid equal to p->iPrevDocid.  The sizes are encoded as
125042 125758   ** a blob of varints.
125043 125759   */
125044 125760   static void fts3InsertDocsize(
125045         -  int *pRC,         /* Result code */
125046         -  Fts3Table *p,     /* Table into which to insert */
125047         -  u32 *aSz          /* Sizes of each column */
       125761  +  int *pRC,                       /* Result code */
       125762  +  Fts3Table *p,                   /* Table into which to insert */
       125763  +  u32 *aSz                        /* Sizes of each column, in tokens */
125048 125764   ){
125049 125765     char *pBlob;             /* The BLOB encoding of the document size */
125050 125766     int nBlob;               /* Number of bytes in the BLOB */
125051 125767     sqlite3_stmt *pStmt;     /* Statement used to insert the encoding */
125052 125768     int rc;                  /* Result code from subfunctions */
125053 125769   
125054 125770     if( *pRC ) return;
................................................................................
125161 125877       }
125162 125878     }
125163 125879     sqlite3Fts3SegmentsClose(p);
125164 125880     sqlite3Fts3PendingTermsClear(p);
125165 125881   
125166 125882     return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
125167 125883   }
       125884  +
       125885  +/*
       125886  +** This function is called when the user executes the following statement:
       125887  +**
       125888  +**     INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
       125889  +**
       125890  +** The entire FTS index is discarded and rebuilt. If the table is one 
       125891  +** created using the content=xxx option, then the new index is based on
       125892  +** the current contents of the xxx table. Otherwise, it is rebuilt based
       125893  +** on the contents of the %_content table.
       125894  +*/
       125895  +static int fts3DoRebuild(Fts3Table *p){
       125896  +  int rc;                         /* Return Code */
       125897  +
       125898  +  rc = fts3DeleteAll(p, 0);
       125899  +  if( rc==SQLITE_OK ){
       125900  +    u32 *aSz = 0;
       125901  +    u32 *aSzIns = 0;
       125902  +    u32 *aSzDel = 0;
       125903  +    sqlite3_stmt *pStmt = 0;
       125904  +    int nEntry = 0;
       125905  +
       125906  +    /* Compose and prepare an SQL statement to loop through the content table */
       125907  +    char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
       125908  +    if( !zSql ){
       125909  +      rc = SQLITE_NOMEM;
       125910  +    }else{
       125911  +      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
       125912  +      sqlite3_free(zSql);
       125913  +    }
       125914  +
       125915  +    if( rc==SQLITE_OK ){
       125916  +      int nByte = sizeof(u32) * (p->nColumn+1)*3;
       125917  +      aSz = (u32 *)sqlite3_malloc(nByte);
       125918  +      if( aSz==0 ){
       125919  +        rc = SQLITE_NOMEM;
       125920  +      }else{
       125921  +        memset(aSz, 0, nByte);
       125922  +        aSzIns = &aSz[p->nColumn+1];
       125923  +        aSzDel = &aSzIns[p->nColumn+1];
       125924  +      }
       125925  +    }
       125926  +
       125927  +    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
       125928  +      int iCol;
       125929  +      rc = fts3PendingTermsDocid(p, sqlite3_column_int64(pStmt, 0));
       125930  +      aSz[p->nColumn] = 0;
       125931  +      for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
       125932  +        const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
       125933  +        rc = fts3PendingTermsAdd(p, z, iCol, &aSz[iCol]);
       125934  +        aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
       125935  +      }
       125936  +      if( p->bHasDocsize ){
       125937  +        fts3InsertDocsize(&rc, p, aSz);
       125938  +      }
       125939  +      if( rc!=SQLITE_OK ){
       125940  +        sqlite3_finalize(pStmt);
       125941  +        pStmt = 0;
       125942  +      }else{
       125943  +        nEntry++;
       125944  +        for(iCol=0; iCol<=p->nColumn; iCol++){
       125945  +          aSzIns[iCol] += aSz[iCol];
       125946  +        }
       125947  +      }
       125948  +    }
       125949  +    if( p->bHasStat ){
       125950  +      fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
       125951  +    }
       125952  +    sqlite3_free(aSz);
       125953  +
       125954  +    if( pStmt ){
       125955  +      int rc2 = sqlite3_finalize(pStmt);
       125956  +      if( rc==SQLITE_OK ){
       125957  +        rc = rc2;
       125958  +      }
       125959  +    }
       125960  +  }
       125961  +
       125962  +  return rc;
       125963  +}
125168 125964   
125169 125965   /*
125170 125966   ** Handle a 'special' INSERT of the form:
125171 125967   **
125172 125968   **   "INSERT INTO tbl(tbl) VALUES(<expr>)"
125173 125969   **
125174 125970   ** Argument pVal contains the result of <expr>. Currently the only 
................................................................................
125179 125975     const char *zVal = (const char *)sqlite3_value_text(pVal);
125180 125976     int nVal = sqlite3_value_bytes(pVal);
125181 125977   
125182 125978     if( !zVal ){
125183 125979       return SQLITE_NOMEM;
125184 125980     }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
125185 125981       rc = fts3DoOptimize(p, 0);
       125982  +  }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
       125983  +    rc = fts3DoRebuild(p);
125186 125984   #ifdef SQLITE_TEST
125187 125985     }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
125188 125986       p->nNodeSize = atoi(&zVal[9]);
125189 125987       rc = SQLITE_OK;
125190 125988     }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
125191 125989       p->nMaxPendingData = atoi(&zVal[11]);
125192 125990       rc = SQLITE_OK;
................................................................................
125259 126057           int iPos;                 /* Position of token in zText */
125260 126058     
125261 126059           pTC->pTokenizer = pT;
125262 126060           rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
125263 126061           for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
125264 126062             Fts3PhraseToken *pPT = pDef->pToken;
125265 126063             if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
       126064  +           && (pPT->bFirst==0 || iPos==0)
125266 126065              && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
125267 126066              && (0==memcmp(zToken, pPT->z, pPT->n))
125268 126067             ){
125269 126068               fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
125270 126069             }
125271 126070           }
125272 126071         }
................................................................................
125350 126149     int isEmpty = 0;
125351 126150     int rc = fts3IsEmpty(p, pRowid, &isEmpty);
125352 126151     if( rc==SQLITE_OK ){
125353 126152       if( isEmpty ){
125354 126153         /* Deleting this row means the whole table is empty. In this case
125355 126154         ** delete the contents of all three tables and throw away any
125356 126155         ** data in the pendingTerms hash table.  */
125357         -      rc = fts3DeleteAll(p);
       126156  +      rc = fts3DeleteAll(p, 1);
125358 126157         *pnDoc = *pnDoc - 1;
125359 126158       }else{
125360 126159         sqlite3_int64 iRemove = sqlite3_value_int64(pRowid);
125361 126160         rc = fts3PendingTermsDocid(p, iRemove);
125362 126161         fts3DeleteTerms(&rc, p, pRowid, aSzDel);
125363         -      fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
125364         -      if( sqlite3_changes(p->db) ) *pnDoc = *pnDoc - 1;
       126162  +      if( p->zContentTbl==0 ){
       126163  +        fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
       126164  +        if( sqlite3_changes(p->db) ) *pnDoc = *pnDoc - 1;
       126165  +      }else{
       126166  +        *pnDoc = *pnDoc - 1;
       126167  +      }
125365 126168         if( p->bHasDocsize ){
125366 126169           fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
125367 126170         }
125368 126171       }
125369 126172     }
125370 126173   
125371 126174     return rc;
................................................................................
125380 126183     int nArg,                       /* Size of argument array */
125381 126184     sqlite3_value **apVal,          /* Array of arguments */
125382 126185     sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
125383 126186   ){
125384 126187     Fts3Table *p = (Fts3Table *)pVtab;
125385 126188     int rc = SQLITE_OK;             /* Return Code */
125386 126189     int isRemove = 0;               /* True for an UPDATE or DELETE */
125387         -  sqlite3_int64 iRemove = 0;      /* Rowid removed by UPDATE or DELETE */
125388 126190     u32 *aSzIns = 0;                /* Sizes of inserted documents */
125389 126191     u32 *aSzDel;                    /* Sizes of deleted documents */
125390 126192     int nChng = 0;                  /* Net change in number of documents */
125391 126193     int bInsertDone = 0;
125392 126194   
125393 126195     assert( p->pSegments==0 );
125394 126196   
................................................................................
125418 126220     **
125419 126221     ** If the on-conflict mode is REPLACE, this means that the existing row
125420 126222     ** should be deleted from the database before inserting the new row. Or,
125421 126223     ** if the on-conflict mode is other than REPLACE, then this method must
125422 126224     ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
125423 126225     ** modify the database file.
125424 126226     */
125425         -  if( nArg>1 ){
       126227  +  if( nArg>1 && p->zContentTbl==0 ){
125426 126228       /* Find the value object that holds the new rowid value. */
125427 126229       sqlite3_value *pNewRowid = apVal[3+p->nColumn];
125428 126230       if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
125429 126231         pNewRowid = apVal[1];
125430 126232       }
125431 126233   
125432 126234       if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && ( 
................................................................................
125463 126265     }
125464 126266   
125465 126267     /* If this is a DELETE or UPDATE operation, remove the old record. */
125466 126268     if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
125467 126269       assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
125468 126270       rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
125469 126271       isRemove = 1;
125470         -    iRemove = sqlite3_value_int64(apVal[0]);
125471 126272     }
125472 126273     
125473 126274     /* If this is an INSERT or UPDATE operation, insert the new record. */
125474 126275     if( nArg>1 && rc==SQLITE_OK ){
125475 126276       if( bInsertDone==0 ){
125476 126277         rc = fts3InsertData(p, apVal, pRowid);
125477         -      if( rc==SQLITE_CONSTRAINT ) rc = SQLITE_CORRUPT_VTAB;
       126278  +      if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
       126279  +        rc = FTS_CORRUPT_VTAB;
       126280  +      }
125478 126281       }
125479         -    if( rc==SQLITE_OK && (!isRemove || *pRowid!=iRemove) ){
       126282  +    if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
125480 126283         rc = fts3PendingTermsDocid(p, *pRowid);
125481 126284       }
125482 126285       if( rc==SQLITE_OK ){
       126286  +      assert( p->iPrevDocid==*pRowid );
125483 126287         rc = fts3InsertTerms(p, apVal, aSzIns);
125484 126288       }
125485 126289       if( p->bHasDocsize ){
125486 126290         fts3InsertDocsize(&rc, p, aSzIns);
125487 126291       }
125488 126292       nChng++;
125489 126293     }
................................................................................
125889 126693     pPhrase->nToken = pExpr->pPhrase->nToken;
125890 126694   
125891 126695     pCsr = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol);
125892 126696     if( pCsr ){
125893 126697       int iFirst = 0;
125894 126698       pPhrase->pList = pCsr;
125895 126699       fts3GetDeltaPosition(&pCsr, &iFirst);
       126700  +    assert( iFirst>=0 );
125896 126701       pPhrase->pHead = pCsr;
125897 126702       pPhrase->pTail = pCsr;
125898 126703       pPhrase->iHead = iFirst;
125899 126704       pPhrase->iTail = iFirst;
125900 126705     }else{
125901 126706       assert( pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 );
125902 126707     }
................................................................................
126369 127174       if( rc!=SQLITE_OK ) return rc;
126370 127175     }
126371 127176     pStmt = *ppStmt;
126372 127177     assert( sqlite3_data_count(pStmt)==1 );
126373 127178   
126374 127179     a = sqlite3_column_blob(pStmt, 0);
126375 127180     a += sqlite3Fts3GetVarint(a, &nDoc);
126376         -  if( nDoc==0 ) return SQLITE_CORRUPT_VTAB;
       127181  +  if( nDoc==0 ) return FTS_CORRUPT_VTAB;
126377 127182     *pnDoc = (u32)nDoc;
126378 127183   
126379 127184     if( paLen ) *paLen = a;
126380 127185     return SQLITE_OK;
126381 127186   }
126382 127187   
126383 127188   /*
................................................................................
126930 127735             iMinPos = pT->iPos-pT->iOff;
126931 127736             pTerm = pT;
126932 127737           }
126933 127738         }
126934 127739   
126935 127740         if( !pTerm ){
126936 127741           /* All offsets for this column have been gathered. */
126937         -        break;
       127742  +        rc = SQLITE_DONE;
126938 127743         }else{
126939 127744           assert( iCurrent<=iMinPos );
126940 127745           if( 0==(0xFE&*pTerm->pList) ){
126941 127746             pTerm->pList = 0;
126942 127747           }else{
126943 127748             fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
126944 127749           }
................................................................................
126947 127752           }
126948 127753           if( rc==SQLITE_OK ){
126949 127754             char aBuffer[64];
126950 127755             sqlite3_snprintf(sizeof(aBuffer), aBuffer, 
126951 127756                 "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
126952 127757             );
126953 127758             rc = fts3StringAppend(&res, aBuffer, -1);
126954         -        }else if( rc==SQLITE_DONE ){
126955         -          rc = SQLITE_CORRUPT_VTAB;
       127759  +        }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
       127760  +          rc = FTS_CORRUPT_VTAB;
126956 127761           }
126957 127762         }
126958 127763       }
126959 127764       if( rc==SQLITE_DONE ){
126960 127765         rc = SQLITE_OK;
126961 127766       }
126962 127767   
................................................................................
128289 129094       if( argc>0 ){
128290 129095         pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
128291 129096         pCsr->nConstraint = argc;
128292 129097         if( !pCsr->aConstraint ){
128293 129098           rc = SQLITE_NOMEM;
128294 129099         }else{
128295 129100           memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
128296         -        assert( (idxStr==0 && argc==0) || (int)strlen(idxStr)==argc*2 );
       129101  +        assert( (idxStr==0 && argc==0)
       129102  +                || (idxStr && (int)strlen(idxStr)==argc*2) );
128297 129103           for(ii=0; ii<argc; ii++){
128298 129104             RtreeConstraint *p = &pCsr->aConstraint[ii];
128299 129105             p->op = idxStr[ii*2];
128300 129106             p->iCoord = idxStr[ii*2+1]-'a';
128301 129107             if( p->op==RTREE_MATCH ){
128302 129108               /* A MATCH operator. The right-hand-side must be a blob that
128303 129109               ** can be cast into an RtreeMatchArg object. One created using
................................................................................
128590 129396   
128591 129397     for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
128592 129398       int iCell;
128593 129399       sqlite3_int64 iBest = 0;
128594 129400   
128595 129401       float fMinGrowth = 0.0;
128596 129402       float fMinArea = 0.0;
       129403  +#if VARIANT_RSTARTREE_CHOOSESUBTREE
128597 129404       float fMinOverlap = 0.0;
       129405  +    float overlap;
       129406  +#endif
128598 129407   
128599 129408       int nCell = NCELL(pNode);
128600 129409       RtreeCell cell;
128601 129410       RtreeNode *pChild;
128602 129411   
128603 129412       RtreeCell *aCell = 0;
128604 129413   
................................................................................
128622 129431       ** is inserted into it. Resolve ties by choosing the entry with
128623 129432       ** the smallest area.
128624 129433       */
128625 129434       for(iCell=0; iCell<nCell; iCell++){
128626 129435         int bBest = 0;
128627 129436         float growth;
128628 129437         float area;
128629         -      float overlap = 0.0;
128630 129438         nodeGetCell(pRtree, pNode, iCell, &cell);
128631 129439         growth = cellGrowth(pRtree, &cell, pCell);
128632 129440         area = cellArea(pRtree, &cell);
128633 129441   
128634 129442   #if VARIANT_RSTARTREE_CHOOSESUBTREE
128635 129443         if( ii==(pRtree->iDepth-1) ){
128636 129444           overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
       129445  +      }else{
       129446  +        overlap = 0.0;
128637 129447         }
128638 129448         if( (iCell==0) 
128639 129449          || (overlap<fMinOverlap) 
128640 129450          || (overlap==fMinOverlap && growth<fMinGrowth)
128641 129451          || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
128642 129452         ){
128643 129453           bBest = 1;
       129454  +        fMinOverlap = overlap;
128644 129455         }
128645 129456   #else
128646 129457         if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
128647 129458           bBest = 1;
128648 129459         }
128649 129460   #endif
128650 129461         if( bBest ){
128651         -        fMinOverlap = overlap;
128652 129462           fMinGrowth = growth;
128653 129463           fMinArea = area;
128654 129464           iBest = cell.iRowid;
128655 129465         }
128656 129466       }
128657 129467   
128658 129468       sqlite3_free(aCell);

Changes to SQLite.Interop/src/core/sqlite3.h.

   103    103   ** string contains the date and time of the check-in (UTC) and an SHA1
   104    104   ** hash of the entire source tree.
   105    105   **
   106    106   ** See also: [sqlite3_libversion()],
   107    107   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   108    108   ** [sqlite_version()] and [sqlite_source_id()].
   109    109   */
   110         -#define SQLITE_VERSION        "3.7.8"
   111         -#define SQLITE_VERSION_NUMBER 3007008
   112         -#define SQLITE_SOURCE_ID      "2011-09-19 14:49:19 3e0da808d2f5b4d12046e05980ca04578f581177"
          110  +#define SQLITE_VERSION        "3.7.9"
          111  +#define SQLITE_VERSION_NUMBER 3007009
          112  +#define SQLITE_SOURCE_ID      "2011-11-01 00:52:41 c7c6050ef060877ebe77b41d959e9df13f8c9b5e"
   113    113   
   114    114   /*
   115    115   ** CAPI3REF: Run-Time Library Version Numbers
   116    116   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   117    117   **
   118    118   ** These interfaces provide the same information as the [SQLITE_VERSION],
   119    119   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
   742    742   ** opcode as doing so may disrupt the operation of the specialized VFSes
   743    743   ** that do require it.  
   744    744   **
   745    745   ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
   746    746   ** retry counts and intervals for certain disk I/O operations for the
   747    747   ** windows [VFS] in order to work to provide robustness against
   748    748   ** anti-virus programs.  By default, the windows VFS will retry file read,
   749         -** file write, and file delete opertions up to 10 times, with a delay
          749  +** file write, and file delete operations up to 10 times, with a delay
   750    750   ** of 25 milliseconds before the first retry and with the delay increasing
   751    751   ** by an additional 25 milliseconds with each subsequent retry.  This
   752    752   ** opcode allows those to values (10 retries and 25 milliseconds of delay)
   753    753   ** to be adjusted.  The values are changed for all database connections
   754    754   ** within the same process.  The argument is a pointer to an array of two
   755    755   ** integers where the first integer i the new retry count and the second
   756    756   ** integer is the delay.  If either integer is negative, then the setting
................................................................................
   767    767   ** have write permission on the directory containing the database file want
   768    768   ** to read the database file, as the WAL and shared memory files must exist
   769    769   ** in order for the database to be readable.  The fourth parameter to
   770    770   ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
   771    771   ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
   772    772   ** WAL mode.  If the integer is -1, then it is overwritten with the current
   773    773   ** WAL persistence setting.
   774         -** 
          774  +**
          775  +** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
          776  +** a write transaction to indicate that, unless it is rolled back for some
          777  +** reason, the entire database file will be overwritten by the current 
          778  +** transaction. This is used by VACUUM operations.
   775    779   */
   776    780   #define SQLITE_FCNTL_LOCKSTATE        1
   777    781   #define SQLITE_GET_LOCKPROXYFILE      2
   778    782   #define SQLITE_SET_LOCKPROXYFILE      3
   779    783   #define SQLITE_LAST_ERRNO             4
   780    784   #define SQLITE_FCNTL_SIZE_HINT        5
   781    785   #define SQLITE_FCNTL_CHUNK_SIZE       6
   782    786   #define SQLITE_FCNTL_FILE_POINTER     7
   783    787   #define SQLITE_FCNTL_SYNC_OMITTED     8
   784    788   #define SQLITE_FCNTL_WIN32_AV_RETRY   9
   785    789   #define SQLITE_FCNTL_PERSIST_WAL     10
          790  +#define SQLITE_FCNTL_OVERWRITE       11
   786    791   
   787    792   /*
   788    793   ** CAPI3REF: Mutex Handle
   789    794   **
   790    795   ** The mutex module within SQLite defines [sqlite3_mutex] to be an
   791    796   ** abstract type for a mutex object.  The SQLite core never looks
   792    797   ** at the internal representation of an [sqlite3_mutex].  It only
................................................................................
  1395   1400   ** to using its default memory allocator (the system malloc() implementation),
  1396   1401   ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
  1397   1402   ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
  1398   1403   ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
  1399   1404   ** allocator is engaged to handle all of SQLites memory allocation needs.
  1400   1405   ** The first pointer (the memory pointer) must be aligned to an 8-byte
  1401   1406   ** boundary or subsequent behavior of SQLite will be undefined.
  1402         -** The minimum allocation size is capped at 2^12. Reasonable values
  1403         -** for the minimum allocation size are 2^5 through 2^8.</dd>
         1407  +** The minimum allocation size is capped at 2**12. Reasonable values
         1408  +** for the minimum allocation size are 2**5 through 2**8.</dd>
  1404   1409   **
  1405   1410   ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
  1406   1411   ** <dd> ^(This option takes a single argument which is a pointer to an
  1407   1412   ** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
  1408   1413   ** alternative low-level mutex routines to be used in place
  1409   1414   ** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
  1410   1415   ** content of the [sqlite3_mutex_methods] structure before the call to
................................................................................
  2795   2800   ** first zero terminator. ^If nByte is non-negative, then it is the maximum
  2796   2801   ** number of  bytes read from zSql.  ^When nByte is non-negative, the
  2797   2802   ** zSql string ends at either the first '\000' or '\u0000' character or
  2798   2803   ** the nByte-th byte, whichever comes first. If the caller knows
  2799   2804   ** that the supplied string is nul-terminated, then there is a small
  2800   2805   ** performance advantage to be gained by passing an nByte parameter that
  2801   2806   ** is equal to the number of bytes in the input string <i>including</i>
  2802         -** the nul-terminator bytes.
         2807  +** the nul-terminator bytes as this saves SQLite from having to
         2808  +** make a copy of the input string.
  2803   2809   **
  2804   2810   ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
  2805   2811   ** past the end of the first SQL statement in zSql.  These routines only
  2806   2812   ** compile the first statement in zSql, so *pzTail is left pointing to
  2807   2813   ** what remains uncompiled.
  2808   2814   **
  2809   2815   ** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
................................................................................
  2846   2852   ** WHERE clause might influence the choice of query plan for a statement,
  2847   2853   ** then the statement will be automatically recompiled, as if there had been 
  2848   2854   ** a schema change, on the first  [sqlite3_step()] call following any change
  2849   2855   ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  2850   2856   ** ^The specific value of WHERE-clause [parameter] might influence the 
  2851   2857   ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  2852   2858   ** or [GLOB] operator or if the parameter is compared to an indexed column
  2853         -** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
         2859  +** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  2854   2860   ** the 
  2855   2861   ** </li>
  2856   2862   ** </ol>
  2857   2863   */
  2858   2864   SQLITE_API int sqlite3_prepare(
  2859   2865     sqlite3 *db,            /* Database handle */
  2860   2866     const char *zSql,       /* SQL statement, UTF-8 encoded */
................................................................................
  3016   3022   ** ^The third argument is the value to bind to the parameter.
  3017   3023   **
  3018   3024   ** ^(In those routines that have a fourth argument, its value is the
  3019   3025   ** number of bytes in the parameter.  To be clear: the value is the
  3020   3026   ** number of <u>bytes</u> in the value, not the number of characters.)^
  3021   3027   ** ^If the fourth parameter is negative, the length of the string is
  3022   3028   ** the number of bytes up to the first zero terminator.
         3029  +** If a non-negative fourth parameter is provided to sqlite3_bind_text()
         3030  +** or sqlite3_bind_text16() then that parameter must be the byte offset
         3031  +** where the NUL terminator would occur assuming the string were NUL
         3032  +** terminated.  If any NUL characters occur at byte offsets less than 
         3033  +** the value of the fourth parameter then the resulting string value will
         3034  +** contain embedded NULs.  The result of expressions involving strings
         3035  +** with embedded NULs is undefined.
  3023   3036   **
  3024   3037   ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
  3025   3038   ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
  3026   3039   ** string after SQLite has finished with it.  ^The destructor is called
  3027   3040   ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
  3028   3041   ** sqlite3_bind_text(), or sqlite3_bind_text16() fails.  
  3029   3042   ** ^If the fifth argument is
................................................................................
  3349   3362   **
  3350   3363   ** ^The sqlite3_data_count(P) interface returns the number of columns in the
  3351   3364   ** current row of the result set of [prepared statement] P.
  3352   3365   ** ^If prepared statement P does not have results ready to return
  3353   3366   ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
  3354   3367   ** interfaces) then sqlite3_data_count(P) returns 0.
  3355   3368   ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
         3369  +** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
         3370  +** [sqlite3_step](P) returned [SQLITE_DONE].  ^The sqlite3_data_count(P)
         3371  +** will return non-zero if previous call to [sqlite3_step](P) returned
         3372  +** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
         3373  +** where it always returns zero since each step of that multi-step
         3374  +** pragma returns 0 columns of data.
  3356   3375   **
  3357   3376   ** See also: [sqlite3_column_count()]
  3358   3377   */
  3359   3378   SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
  3360   3379   
  3361   3380   /*
  3362   3381   ** CAPI3REF: Fundamental Datatypes
................................................................................
  4028   4047   ** the 2nd parameter of the sqlite3_result_text* interfaces.
  4029   4048   ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  4030   4049   ** is negative, then SQLite takes result text from the 2nd parameter
  4031   4050   ** through the first zero character.
  4032   4051   ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  4033   4052   ** is non-negative, then as many bytes (not characters) of the text
  4034   4053   ** pointed to by the 2nd parameter are taken as the application-defined
  4035         -** function result.
         4054  +** function result.  If the 3rd parameter is non-negative, then it
         4055  +** must be the byte offset into the string where the NUL terminator would
         4056  +** appear if the string where NUL terminated.  If any NUL characters occur
         4057  +** in the string at a byte offset that is less than the value of the 3rd
         4058  +** parameter, then the resulting string will contain embedded NULs and the
         4059  +** result of expressions operating on strings with embedded NULs is undefined.
  4036   4060   ** ^If the 4th parameter to the sqlite3_result_text* interfaces
  4037   4061   ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
  4038   4062   ** function as the destructor on the text or BLOB result when it has
  4039   4063   ** finished using that result.
  4040   4064   ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
  4041   4065   ** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
  4042   4066   ** assumes that the text or BLOB result is in constant space and does not
................................................................................
  5811   5835   **
  5812   5836   ** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
  5813   5837   ** <dd>This parameter returns the approximate number of of bytes of heap
  5814   5838   ** and lookaside memory used by all prepared statements associated with
  5815   5839   ** the database connection.)^
  5816   5840   ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
  5817   5841   ** </dd>
         5842  +**
         5843  +** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
         5844  +** <dd>This parameter returns the number of pager cache hits that have
         5845  +** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT 
         5846  +** is always 0.
         5847  +** </dd>
         5848  +**
         5849  +** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
         5850  +** <dd>This parameter returns the number of pager cache misses that have
         5851  +** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS 
         5852  +** is always 0.
         5853  +** </dd>
  5818   5854   ** </dl>
  5819   5855   */
  5820   5856   #define SQLITE_DBSTATUS_LOOKASIDE_USED       0
  5821   5857   #define SQLITE_DBSTATUS_CACHE_USED           1
  5822   5858   #define SQLITE_DBSTATUS_SCHEMA_USED          2
  5823   5859   #define SQLITE_DBSTATUS_STMT_USED            3
  5824   5860   #define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
  5825   5861   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  5826   5862   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  5827         -#define SQLITE_DBSTATUS_MAX                  6   /* Largest defined DBSTATUS */
         5863  +#define SQLITE_DBSTATUS_CACHE_HIT            7
         5864  +#define SQLITE_DBSTATUS_CACHE_MISS           8
         5865  +#define SQLITE_DBSTATUS_MAX                  8   /* Largest defined DBSTATUS */
  5828   5866   
  5829   5867   
  5830   5868   /*
  5831   5869   ** CAPI3REF: Prepared Statement Status
  5832   5870   **
  5833   5871   ** ^(Each prepared statement maintains various
  5834   5872   ** [SQLITE_STMTSTATUS counters] that measure the number
................................................................................
  5874   5912   **
  5875   5913   ** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
  5876   5914   ** <dd>^This is the number of rows inserted into transient indices that
  5877   5915   ** were created automatically in order to help joins run faster.
  5878   5916   ** A non-zero value in this counter may indicate an opportunity to
  5879   5917   ** improvement performance by adding permanent indices that do not
  5880   5918   ** need to be reinitialized each time the statement is run.</dd>
  5881         -**
  5882   5919   ** </dl>
  5883   5920   */
  5884   5921   #define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
  5885   5922   #define SQLITE_STMTSTATUS_SORT              2
  5886   5923   #define SQLITE_STMTSTATUS_AUTOINDEX         3
  5887   5924   
  5888   5925   /*

Changes to SQLite.Interop/src/core/sqlite3ext.h.

    45     45     int  (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
    46     46     int  (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
    47     47     int  (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
    48     48     int  (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
    49     49     int  (*busy_timeout)(sqlite3*,int ms);
    50     50     int  (*changes)(sqlite3*);
    51     51     int  (*close)(sqlite3*);
    52         -  int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const char*));
    53         -  int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const void*));
           52  +  int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
           53  +                           int eTextRep,const char*));
           54  +  int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
           55  +                             int eTextRep,const void*));
    54     56     const void * (*column_blob)(sqlite3_stmt*,int iCol);
    55     57     int  (*column_bytes)(sqlite3_stmt*,int iCol);
    56     58     int  (*column_bytes16)(sqlite3_stmt*,int iCol);
    57     59     int  (*column_count)(sqlite3_stmt*pStmt);
    58     60     const char * (*column_database_name)(sqlite3_stmt*,int);
    59     61     const void * (*column_database_name16)(sqlite3_stmt*,int);
    60     62     const char * (*column_decltype)(sqlite3_stmt*,int i);
................................................................................
    71     73     const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
    72     74     const void * (*column_text16)(sqlite3_stmt*,int iCol);
    73     75     int  (*column_type)(sqlite3_stmt*,int iCol);
    74     76     sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
    75     77     void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
    76     78     int  (*complete)(const char*sql);
    77     79     int  (*complete16)(const void*sql);
    78         -  int  (*create_collation)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*));
    79         -  int  (*create_collation16)(sqlite3*,const void*,int,void*,int(*)(void*,int,const void*,int,const void*));
    80         -  int  (*create_function)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*));
    81         -  int  (*create_function16)(sqlite3*,const void*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*));
           80  +  int  (*create_collation)(sqlite3*,const char*,int,void*,
           81  +                           int(*)(void*,int,const void*,int,const void*));
           82  +  int  (*create_collation16)(sqlite3*,const void*,int,void*,
           83  +                             int(*)(void*,int,const void*,int,const void*));
           84  +  int  (*create_function)(sqlite3*,const char*,int,int,void*,
           85  +                          void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
           86  +                          void (*xStep)(sqlite3_context*,int,sqlite3_value**),
           87  +                          void (*xFinal)(sqlite3_context*));
           88  +  int  (*create_function16)(sqlite3*,const void*,int,int,void*,
           89  +                            void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
           90  +                            void (*xStep)(sqlite3_context*,int,sqlite3_value**),
           91  +                            void (*xFinal)(sqlite3_context*));
    82     92     int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
    83     93     int  (*data_count)(sqlite3_stmt*pStmt);
    84     94     sqlite3 * (*db_handle)(sqlite3_stmt*);
    85     95     int (*declare_vtab)(sqlite3*,const char*);
    86     96     int  (*enable_shared_cache)(int);
    87     97     int  (*errcode)(sqlite3*db);
    88     98     const char * (*errmsg)(sqlite3*);
................................................................................
   119    129     void  (*result_null)(sqlite3_context*);
   120    130     void  (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
   121    131     void  (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
   122    132     void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
   123    133     void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
   124    134     void  (*result_value)(sqlite3_context*,sqlite3_value*);
   125    135     void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
   126         -  int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,const char*,const char*),void*);
          136  +  int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
          137  +                         const char*,const char*),void*);
   127    138     void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
   128    139     char * (*snprintf)(int,char*,const char*,...);
   129    140     int  (*step)(sqlite3_stmt*);
   130         -  int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,char const**,char const**,int*,int*,int*);
          141  +  int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
          142  +                                char const**,char const**,int*,int*,int*);
   131    143     void  (*thread_cleanup)(void);
   132    144     int  (*total_changes)(sqlite3*);
   133    145     void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
   134    146     int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
   135         -  void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,sqlite_int64),void*);
          147  +  void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
          148  +                                         sqlite_int64),void*);
   136    149     void * (*user_data)(sqlite3_context*);
   137    150     const void * (*value_blob)(sqlite3_value*);
   138    151     int  (*value_bytes)(sqlite3_value*);
   139    152     int  (*value_bytes16)(sqlite3_value*);
   140    153     double  (*value_double)(sqlite3_value*);
   141    154     int  (*value_int)(sqlite3_value*);
   142    155     sqlite_int64  (*value_int64)(sqlite3_value*);
................................................................................
   150    163     /* Added ??? */
   151    164     int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
   152    165     /* Added by 3.3.13 */
   153    166     int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
   154    167     int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
   155    168     int (*clear_bindings)(sqlite3_stmt*);
   156    169     /* Added by 3.4.1 */
   157         -  int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,void (*xDestroy)(void *));
          170  +  int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
          171  +                          void (*xDestroy)(void *));
   158    172     /* Added by 3.5.0 */
   159    173     int (*bind_zeroblob)(sqlite3_stmt*,int,int);
   160    174     int (*blob_bytes)(sqlite3_blob*);
   161    175     int (*blob_close)(sqlite3_blob*);
   162         -  int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,int,sqlite3_blob**);
          176  +  int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
          177  +                   int,sqlite3_blob**);
   163    178     int (*blob_read)(sqlite3_blob*,void*,int,int);
   164    179     int (*blob_write)(sqlite3_blob*,const void*,int,int);
   165         -  int (*create_collation_v2)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*),void(*)(void*));
          180  +  int (*create_collation_v2)(sqlite3*,const char*,int,void*,
          181  +                             int(*)(void*,int,const void*,int,const void*),
          182  +                             void(*)(void*));
   166    183     int (*file_control)(sqlite3*,const char*,int,void*);
   167    184     sqlite3_int64 (*memory_highwater)(int);
   168    185     sqlite3_int64 (*memory_used)(void);
   169    186     sqlite3_mutex *(*mutex_alloc)(int);
   170    187     void (*mutex_enter)(sqlite3_mutex*);
   171    188     void (*mutex_free)(sqlite3_mutex*);
   172    189     void (*mutex_leave)(sqlite3_mutex*);
................................................................................
   194    211     int (*backup_finish)(sqlite3_backup*);
   195    212     sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
   196    213     int (*backup_pagecount)(sqlite3_backup*);
   197    214     int (*backup_remaining)(sqlite3_backup*);
   198    215     int (*backup_step)(sqlite3_backup*,int);
   199    216     const char *(*compileoption_get)(int);
   200    217     int (*compileoption_used)(const char*);
   201         -  int (*create_function_v2)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*),void(*xDestroy)(void*));
          218  +  int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
          219  +                            void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
          220  +                            void (*xStep)(sqlite3_context*,int,sqlite3_value**),
          221  +                            void (*xFinal)(sqlite3_context*),
          222  +                            void(*xDestroy)(void*));
   202    223     int (*db_config)(sqlite3*,int,...);
   203    224     sqlite3_mutex *(*db_mutex)(sqlite3*);
   204    225     int (*db_status)(sqlite3*,int,int*,int*,int);
   205    226     int (*extended_errcode)(sqlite3*);
   206    227     void (*log)(int,const char*,...);
   207    228     sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
   208    229     const char *(*sourceid)(void);