sqllogictest
Check-in [3f320d5068]
Not logged in

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

Overview
Comment:Check in the second 3.8.10 beta.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 3f320d5068540c88eadd984e2efcf04b7eabd6f3
User & Date: drh 2015-05-06 01:28:59
Context
2015-07-24
00:03
Update the built-in SQLite to the second 3.8.11 beta. check-in: ac688c398e user: drh tags: trunk
2015-05-06
01:28
Check in the second 3.8.10 beta. check-in: 3f320d5068 user: drh tags: trunk
2015-04-06
20:40
Update the built-in SQLite to the third 3.8.9 beta. check-in: 21890161c0 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.8.9.  By combining all the individual C code files into this 
            3  +** version 3.8.10.  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
................................................................................
    66     66   #define _MSVC_H_
    67     67   
    68     68   #if defined(_MSC_VER)
    69     69   #pragma warning(disable : 4054)
    70     70   #pragma warning(disable : 4055)
    71     71   #pragma warning(disable : 4100)
    72     72   #pragma warning(disable : 4127)
           73  +#pragma warning(disable : 4130)
    73     74   #pragma warning(disable : 4152)
    74     75   #pragma warning(disable : 4189)
    75     76   #pragma warning(disable : 4206)
    76     77   #pragma warning(disable : 4210)
    77     78   #pragma warning(disable : 4232)
    78     79   #pragma warning(disable : 4244)
    79     80   #pragma warning(disable : 4305)
................................................................................
   313    314   ** string contains the date and time of the check-in (UTC) and an SHA1
   314    315   ** hash of the entire source tree.
   315    316   **
   316    317   ** See also: [sqlite3_libversion()],
   317    318   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   318    319   ** [sqlite_version()] and [sqlite_source_id()].
   319    320   */
   320         -#define SQLITE_VERSION        "3.8.9"
   321         -#define SQLITE_VERSION_NUMBER 3008009
   322         -#define SQLITE_SOURCE_ID      "2015-04-06 11:04:51 3ad829e50faca538db3abb2afb898b5521550c5c"
          321  +#define SQLITE_VERSION        "3.8.10"
          322  +#define SQLITE_VERSION_NUMBER 3008010
          323  +#define SQLITE_SOURCE_ID      "2015-05-05 18:52:54 04afa3febee32854fbb09ef8d4ffffd432119716"
   323    324   
   324    325   /*
   325    326   ** CAPI3REF: Run-Time Library Version Numbers
   326    327   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   327    328   **
   328    329   ** These interfaces provide the same information as the [SQLITE_VERSION],
   329    330   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
   472    473   */
   473    474   #ifdef SQLITE_OMIT_FLOATING_POINT
   474    475   # define double sqlite3_int64
   475    476   #endif
   476    477   
   477    478   /*
   478    479   ** CAPI3REF: Closing A Database Connection
          480  +** DESTRUCTOR: sqlite3
   479    481   **
   480    482   ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
   481    483   ** for the [sqlite3] object.
   482    484   ** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
   483    485   ** the [sqlite3] object is successfully destroyed and all associated
   484    486   ** resources are deallocated.
   485    487   **
................................................................................
   523    525   ** This is legacy and deprecated.  It is included for historical
   524    526   ** compatibility and is not documented.
   525    527   */
   526    528   typedef int (*sqlite3_callback)(void*,int,char**, char**);
   527    529   
   528    530   /*
   529    531   ** CAPI3REF: One-Step Query Execution Interface
          532  +** METHOD: sqlite3
   530    533   **
   531    534   ** The sqlite3_exec() interface is a convenience wrapper around
   532    535   ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
   533    536   ** that allows an application to run multiple statements of SQL
   534    537   ** without having to use a lot of C code. 
   535    538   **
   536    539   ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
................................................................................
  1580   1583   ** ^If the option is unknown or SQLite is unable to set the option
  1581   1584   ** then this routine returns a non-zero [error code].
  1582   1585   */
  1583   1586   SQLITE_API int SQLITE_CDECL sqlite3_config(int, ...);
  1584   1587   
  1585   1588   /*
  1586   1589   ** CAPI3REF: Configure database connections
         1590  +** METHOD: sqlite3
  1587   1591   **
  1588   1592   ** The sqlite3_db_config() interface is used to make configuration
  1589   1593   ** changes to a [database connection].  The interface is similar to
  1590   1594   ** [sqlite3_config()] except that the changes apply to a single
  1591   1595   ** [database connection] (specified in the first argument).
  1592   1596   **
  1593   1597   ** The second argument to sqlite3_db_config(D,V,...)  is the
................................................................................
  2077   2081   #define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
  2078   2082   #define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
  2079   2083   #define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
  2080   2084   
  2081   2085   
  2082   2086   /*
  2083   2087   ** CAPI3REF: Enable Or Disable Extended Result Codes
         2088  +** METHOD: sqlite3
  2084   2089   **
  2085   2090   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  2086   2091   ** [extended result codes] feature of SQLite. ^The extended result
  2087   2092   ** codes are disabled by default for historical compatibility.
  2088   2093   */
  2089   2094   SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3*, int onoff);
  2090   2095   
  2091   2096   /*
  2092   2097   ** CAPI3REF: Last Insert Rowid
         2098  +** METHOD: sqlite3
  2093   2099   **
  2094   2100   ** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
  2095   2101   ** has a unique 64-bit signed
  2096   2102   ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
  2097   2103   ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
  2098   2104   ** names are not also used by explicitly declared columns. ^If
  2099   2105   ** the table has a column of type [INTEGER PRIMARY KEY] then that column
................................................................................
  2137   2143   ** unpredictable and might not equal either the old or the new
  2138   2144   ** last insert [rowid].
  2139   2145   */
  2140   2146   SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_last_insert_rowid(sqlite3*);
  2141   2147   
  2142   2148   /*
  2143   2149   ** CAPI3REF: Count The Number Of Rows Modified
         2150  +** METHOD: sqlite3
  2144   2151   **
  2145   2152   ** ^This function returns the number of rows modified, inserted or
  2146   2153   ** deleted by the most recently completed INSERT, UPDATE or DELETE
  2147   2154   ** statement on the database connection specified by the only parameter.
  2148   2155   ** ^Executing any other type of SQL statement does not modify the value
  2149   2156   ** returned by this function.
  2150   2157   **
................................................................................
  2189   2196   ** while [sqlite3_changes()] is running then the value returned
  2190   2197   ** is unpredictable and not meaningful.
  2191   2198   */
  2192   2199   SQLITE_API int SQLITE_STDCALL sqlite3_changes(sqlite3*);
  2193   2200   
  2194   2201   /*
  2195   2202   ** CAPI3REF: Total Number Of Rows Modified
         2203  +** METHOD: sqlite3
  2196   2204   **
  2197   2205   ** ^This function returns the total number of rows inserted, modified or
  2198   2206   ** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
  2199   2207   ** since the database connection was opened, including those executed as
  2200   2208   ** part of trigger programs. ^Executing any other type of SQL statement
  2201   2209   ** does not affect the value returned by sqlite3_total_changes().
  2202   2210   ** 
................................................................................
  2212   2220   ** while [sqlite3_total_changes()] is running then the value
  2213   2221   ** returned is unpredictable and not meaningful.
  2214   2222   */
  2215   2223   SQLITE_API int SQLITE_STDCALL sqlite3_total_changes(sqlite3*);
  2216   2224   
  2217   2225   /*
  2218   2226   ** CAPI3REF: Interrupt A Long-Running Query
         2227  +** METHOD: sqlite3
  2219   2228   **
  2220   2229   ** ^This function causes any pending database operation to abort and
  2221   2230   ** return at its earliest opportunity. This routine is typically
  2222   2231   ** called in response to a user action such as pressing "Cancel"
  2223   2232   ** or Ctrl-C where the user wants a long query operation to halt
  2224   2233   ** immediately.
  2225   2234   **
................................................................................
  2288   2297   */
  2289   2298   SQLITE_API int SQLITE_STDCALL sqlite3_complete(const char *sql);
  2290   2299   SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *sql);
  2291   2300   
  2292   2301   /*
  2293   2302   ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
  2294   2303   ** KEYWORDS: {busy-handler callback} {busy handler}
         2304  +** METHOD: sqlite3
  2295   2305   **
  2296   2306   ** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
  2297   2307   ** that might be invoked with argument P whenever
  2298   2308   ** an attempt is made to access a database table associated with
  2299   2309   ** [database connection] D when another thread
  2300   2310   ** or process has the table locked.
  2301   2311   ** The sqlite3_busy_handler() interface is used to implement
................................................................................
  2347   2357   ** A busy handler must not close the database connection
  2348   2358   ** or [prepared statement] that invoked the busy handler.
  2349   2359   */
  2350   2360   SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
  2351   2361   
  2352   2362   /*
  2353   2363   ** CAPI3REF: Set A Busy Timeout
         2364  +** METHOD: sqlite3
  2354   2365   **
  2355   2366   ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
  2356   2367   ** for a specified amount of time when a table is locked.  ^The handler
  2357   2368   ** will sleep multiple times until at least "ms" milliseconds of sleeping
  2358   2369   ** have accumulated.  ^After at least "ms" milliseconds of sleeping,
  2359   2370   ** the handler returns 0 which causes [sqlite3_step()] to return
  2360   2371   ** [SQLITE_BUSY].
................................................................................
  2369   2380   **
  2370   2381   ** See also:  [PRAGMA busy_timeout]
  2371   2382   */
  2372   2383   SQLITE_API int SQLITE_STDCALL sqlite3_busy_timeout(sqlite3*, int ms);
  2373   2384   
  2374   2385   /*
  2375   2386   ** CAPI3REF: Convenience Routines For Running Queries
         2387  +** METHOD: sqlite3
  2376   2388   **
  2377   2389   ** This is a legacy interface that is preserved for backwards compatibility.
  2378   2390   ** Use of this interface is not recommended.
  2379   2391   **
  2380   2392   ** Definition: A <b>result table</b> is memory data structure created by the
  2381   2393   ** [sqlite3_get_table()] interface.  A result table records the
  2382   2394   ** complete query results from one or more queries.
................................................................................
  2704   2716   ** internally and without recourse to the [sqlite3_vfs] xRandomness
  2705   2717   ** method.
  2706   2718   */
  2707   2719   SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
  2708   2720   
  2709   2721   /*
  2710   2722   ** CAPI3REF: Compile-Time Authorization Callbacks
         2723  +** METHOD: sqlite3
  2711   2724   **
  2712   2725   ** ^This routine registers an authorizer callback with a particular
  2713   2726   ** [database connection], supplied in the first argument.
  2714   2727   ** ^The authorizer callback is invoked as SQL statements are being compiled
  2715   2728   ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
  2716   2729   ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
  2717   2730   ** points during the compilation process, as logic is being created
................................................................................
  2860   2873   #define SQLITE_FUNCTION             31   /* NULL            Function Name   */
  2861   2874   #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
  2862   2875   #define SQLITE_COPY                  0   /* No longer used */
  2863   2876   #define SQLITE_RECURSIVE            33   /* NULL            NULL            */
  2864   2877   
  2865   2878   /*
  2866   2879   ** CAPI3REF: Tracing And Profiling Functions
         2880  +** METHOD: sqlite3
  2867   2881   **
  2868   2882   ** These routines register callback functions that can be used for
  2869   2883   ** tracing and profiling the execution of SQL statements.
  2870   2884   **
  2871   2885   ** ^The callback function registered by sqlite3_trace() is invoked at
  2872   2886   ** various times when an SQL statement is being run by [sqlite3_step()].
  2873   2887   ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
................................................................................
  2892   2906   */
  2893   2907   SQLITE_API void *SQLITE_STDCALL sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
  2894   2908   SQLITE_API SQLITE_EXPERIMENTAL void *SQLITE_STDCALL sqlite3_profile(sqlite3*,
  2895   2909      void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
  2896   2910   
  2897   2911   /*
  2898   2912   ** CAPI3REF: Query Progress Callbacks
         2913  +** METHOD: sqlite3
  2899   2914   **
  2900   2915   ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
  2901   2916   ** function X to be invoked periodically during long running calls to
  2902   2917   ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
  2903   2918   ** database connection D.  An example use for this
  2904   2919   ** interface is to keep a GUI updated during a large query.
  2905   2920   **
................................................................................
  2925   2940   ** database connections for the meaning of "modify" in this paragraph.
  2926   2941   **
  2927   2942   */
  2928   2943   SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
  2929   2944   
  2930   2945   /*
  2931   2946   ** CAPI3REF: Opening A New Database Connection
         2947  +** CONSTRUCTOR: sqlite3
  2932   2948   **
  2933   2949   ** ^These routines open an SQLite database file as specified by the 
  2934   2950   ** filename argument. ^The filename argument is interpreted as UTF-8 for
  2935   2951   ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
  2936   2952   ** order for sqlite3_open16(). ^(A [database connection] handle is usually
  2937   2953   ** returned in *ppDb, even if an error occurs.  The only exception is that
  2938   2954   ** if SQLite is unable to allocate memory to hold the [sqlite3] object,
................................................................................
  3210   3226   SQLITE_API const char *SQLITE_STDCALL sqlite3_uri_parameter(const char *zFilename, const char *zParam);
  3211   3227   SQLITE_API int SQLITE_STDCALL sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
  3212   3228   SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
  3213   3229   
  3214   3230   
  3215   3231   /*
  3216   3232   ** CAPI3REF: Error Codes And Messages
         3233  +** METHOD: sqlite3
  3217   3234   **
  3218   3235   ** ^If the most recent sqlite3_* API call associated with 
  3219   3236   ** [database connection] D failed, then the sqlite3_errcode(D) interface
  3220   3237   ** returns the numeric [result code] or [extended result code] for that
  3221   3238   ** API call.
  3222   3239   ** If the most recent API call was successful,
  3223   3240   ** then the return value from sqlite3_errcode() is undefined.
................................................................................
  3255   3272   SQLITE_API int SQLITE_STDCALL sqlite3_errcode(sqlite3 *db);
  3256   3273   SQLITE_API int SQLITE_STDCALL sqlite3_extended_errcode(sqlite3 *db);
  3257   3274   SQLITE_API const char *SQLITE_STDCALL sqlite3_errmsg(sqlite3*);
  3258   3275   SQLITE_API const void *SQLITE_STDCALL sqlite3_errmsg16(sqlite3*);
  3259   3276   SQLITE_API const char *SQLITE_STDCALL sqlite3_errstr(int);
  3260   3277   
  3261   3278   /*
  3262         -** CAPI3REF: SQL Statement Object
         3279  +** CAPI3REF: Prepared Statement Object
  3263   3280   ** KEYWORDS: {prepared statement} {prepared statements}
  3264   3281   **
  3265         -** An instance of this object represents a single SQL statement.
  3266         -** This object is variously known as a "prepared statement" or a
  3267         -** "compiled SQL statement" or simply as a "statement".
         3282  +** An instance of this object represents a single SQL statement that
         3283  +** has been compiled into binary form and is ready to be evaluated.
  3268   3284   **
  3269         -** The life of a statement object goes something like this:
         3285  +** Think of each SQL statement as a separate computer program.  The
         3286  +** original SQL text is source code.  A prepared statement object 
         3287  +** is the compiled object code.  All SQL must be converted into a
         3288  +** prepared statement before it can be run.
         3289  +**
         3290  +** The life-cycle of a prepared statement object usually goes like this:
  3270   3291   **
  3271   3292   ** <ol>
  3272         -** <li> Create the object using [sqlite3_prepare_v2()] or a related
  3273         -**      function.
  3274         -** <li> Bind values to [host parameters] using the sqlite3_bind_*()
         3293  +** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
         3294  +** <li> Bind values to [parameters] using the sqlite3_bind_*()
  3275   3295   **      interfaces.
  3276   3296   ** <li> Run the SQL by calling [sqlite3_step()] one or more times.
  3277         -** <li> Reset the statement using [sqlite3_reset()] then go back
         3297  +** <li> Reset the prepared statement using [sqlite3_reset()] then go back
  3278   3298   **      to step 2.  Do this zero or more times.
  3279   3299   ** <li> Destroy the object using [sqlite3_finalize()].
  3280   3300   ** </ol>
  3281         -**
  3282         -** Refer to documentation on individual methods above for additional
  3283         -** information.
  3284   3301   */
  3285   3302   typedef struct sqlite3_stmt sqlite3_stmt;
  3286   3303   
  3287   3304   /*
  3288   3305   ** CAPI3REF: Run-time Limits
         3306  +** METHOD: sqlite3
  3289   3307   **
  3290   3308   ** ^(This interface allows the size of various constructs to be limited
  3291   3309   ** on a connection by connection basis.  The first parameter is the
  3292   3310   ** [database connection] whose limit is to be set or queried.  The
  3293   3311   ** second parameter is one of the [limit categories] that define a
  3294   3312   ** class of constructs to be size limited.  The third parameter is the
  3295   3313   ** new limit for that construct.)^
................................................................................
  3393   3411   #define SQLITE_LIMIT_VARIABLE_NUMBER           9
  3394   3412   #define SQLITE_LIMIT_TRIGGER_DEPTH            10
  3395   3413   #define SQLITE_LIMIT_WORKER_THREADS           11
  3396   3414   
  3397   3415   /*
  3398   3416   ** CAPI3REF: Compiling An SQL Statement
  3399   3417   ** KEYWORDS: {SQL statement compiler}
         3418  +** METHOD: sqlite3
         3419  +** CONSTRUCTOR: sqlite3_stmt
  3400   3420   **
  3401   3421   ** To execute an SQL query, it must first be compiled into a byte-code
  3402   3422   ** program using one of these routines.
  3403   3423   **
  3404   3424   ** The first argument, "db", is a [database connection] obtained from a
  3405   3425   ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
  3406   3426   ** [sqlite3_open16()].  The database connection must not have been closed.
................................................................................
  3500   3520     int nByte,              /* Maximum length of zSql in bytes. */
  3501   3521     sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3502   3522     const void **pzTail     /* OUT: Pointer to unused portion of zSql */
  3503   3523   );
  3504   3524   
  3505   3525   /*
  3506   3526   ** CAPI3REF: Retrieving Statement SQL
         3527  +** METHOD: sqlite3_stmt
  3507   3528   **
  3508   3529   ** ^This interface can be used to retrieve a saved copy of the original
  3509   3530   ** SQL text used to create a [prepared statement] if that statement was
  3510   3531   ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
  3511   3532   */
  3512   3533   SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt);
  3513   3534   
  3514   3535   /*
  3515   3536   ** CAPI3REF: Determine If An SQL Statement Writes The Database
         3537  +** METHOD: sqlite3_stmt
  3516   3538   **
  3517   3539   ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  3518   3540   ** and only if the [prepared statement] X makes no direct changes to
  3519   3541   ** the content of the database file.
  3520   3542   **
  3521   3543   ** Note that [application-defined SQL functions] or
  3522   3544   ** [virtual tables] might change the database indirectly as a side effect.  
................................................................................
  3540   3562   ** change the configuration of a database connection, they do not make 
  3541   3563   ** changes to the content of the database files on disk.
  3542   3564   */
  3543   3565   SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
  3544   3566   
  3545   3567   /*
  3546   3568   ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
         3569  +** METHOD: sqlite3_stmt
  3547   3570   **
  3548   3571   ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
  3549   3572   ** [prepared statement] S has been stepped at least once using 
  3550   3573   ** [sqlite3_step(S)] but has not run to completion and/or has not 
  3551   3574   ** been reset using [sqlite3_reset(S)].  ^The sqlite3_stmt_busy(S)
  3552   3575   ** interface returns false if S is a NULL pointer.  If S is not a 
  3553   3576   ** NULL pointer and is not a pointer to a valid [prepared statement]
................................................................................
  3614   3637   */
  3615   3638   typedef struct sqlite3_context sqlite3_context;
  3616   3639   
  3617   3640   /*
  3618   3641   ** CAPI3REF: Binding Values To Prepared Statements
  3619   3642   ** KEYWORDS: {host parameter} {host parameters} {host parameter name}
  3620   3643   ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
         3644  +** METHOD: sqlite3_stmt
  3621   3645   **
  3622   3646   ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
  3623   3647   ** literals may be replaced by a [parameter] that matches one of following
  3624   3648   ** templates:
  3625   3649   **
  3626   3650   ** <ul>
  3627   3651   ** <li>  ?
................................................................................
  3732   3756   SQLITE_API int SQLITE_STDCALL sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
  3733   3757                            void(*)(void*), unsigned char encoding);
  3734   3758   SQLITE_API int SQLITE_STDCALL sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
  3735   3759   SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
  3736   3760   
  3737   3761   /*
  3738   3762   ** CAPI3REF: Number Of SQL Parameters
         3763  +** METHOD: sqlite3_stmt
  3739   3764   **
  3740   3765   ** ^This routine can be used to find the number of [SQL parameters]
  3741   3766   ** in a [prepared statement].  SQL parameters are tokens of the
  3742   3767   ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
  3743   3768   ** placeholders for values that are [sqlite3_bind_blob | bound]
  3744   3769   ** to the parameters at a later time.
  3745   3770   **
................................................................................
  3752   3777   ** [sqlite3_bind_parameter_name()], and
  3753   3778   ** [sqlite3_bind_parameter_index()].
  3754   3779   */
  3755   3780   SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt*);
  3756   3781   
  3757   3782   /*
  3758   3783   ** CAPI3REF: Name Of A Host Parameter
         3784  +** METHOD: sqlite3_stmt
  3759   3785   **
  3760   3786   ** ^The sqlite3_bind_parameter_name(P,N) interface returns
  3761   3787   ** the name of the N-th [SQL parameter] in the [prepared statement] P.
  3762   3788   ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
  3763   3789   ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
  3764   3790   ** respectively.
  3765   3791   ** In other words, the initial ":" or "$" or "@" or "?"
................................................................................
  3779   3805   ** [sqlite3_bind_parameter_count()], and
  3780   3806   ** [sqlite3_bind_parameter_index()].
  3781   3807   */
  3782   3808   SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt*, int);
  3783   3809   
  3784   3810   /*
  3785   3811   ** CAPI3REF: Index Of A Parameter With A Given Name
         3812  +** METHOD: sqlite3_stmt
  3786   3813   **
  3787   3814   ** ^Return the index of an SQL parameter given its name.  ^The
  3788   3815   ** index value returned is suitable for use as the second
  3789   3816   ** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
  3790   3817   ** is returned if no matching parameter is found.  ^The parameter
  3791   3818   ** name must be given in UTF-8 even if the original statement
  3792   3819   ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
................................................................................
  3795   3822   ** [sqlite3_bind_parameter_count()], and
  3796   3823   ** [sqlite3_bind_parameter_index()].
  3797   3824   */
  3798   3825   SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
  3799   3826   
  3800   3827   /*
  3801   3828   ** CAPI3REF: Reset All Bindings On A Prepared Statement
         3829  +** METHOD: sqlite3_stmt
  3802   3830   **
  3803   3831   ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
  3804   3832   ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
  3805   3833   ** ^Use this routine to reset all host parameters to NULL.
  3806   3834   */
  3807   3835   SQLITE_API int SQLITE_STDCALL sqlite3_clear_bindings(sqlite3_stmt*);
  3808   3836   
  3809   3837   /*
  3810   3838   ** CAPI3REF: Number Of Columns In A Result Set
         3839  +** METHOD: sqlite3_stmt
  3811   3840   **
  3812   3841   ** ^Return the number of columns in the result set returned by the
  3813   3842   ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
  3814   3843   ** statement that does not return data (for example an [UPDATE]).
  3815   3844   **
  3816   3845   ** See also: [sqlite3_data_count()]
  3817   3846   */
  3818   3847   SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt);
  3819   3848   
  3820   3849   /*
  3821   3850   ** CAPI3REF: Column Names In A Result Set
         3851  +** METHOD: sqlite3_stmt
  3822   3852   **
  3823   3853   ** ^These routines return the name assigned to a particular column
  3824   3854   ** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
  3825   3855   ** interface returns a pointer to a zero-terminated UTF-8 string
  3826   3856   ** and sqlite3_column_name16() returns a pointer to a zero-terminated
  3827   3857   ** UTF-16 string.  ^The first parameter is the [prepared statement]
  3828   3858   ** that implements the [SELECT] statement. ^The second parameter is the
................................................................................
  3844   3874   ** one release of SQLite to the next.
  3845   3875   */
  3846   3876   SQLITE_API const char *SQLITE_STDCALL sqlite3_column_name(sqlite3_stmt*, int N);
  3847   3877   SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt*, int N);
  3848   3878   
  3849   3879   /*
  3850   3880   ** CAPI3REF: Source Of Data In A Query Result
         3881  +** METHOD: sqlite3_stmt
  3851   3882   **
  3852   3883   ** ^These routines provide a means to determine the database, table, and
  3853   3884   ** table column that is the origin of a particular result column in
  3854   3885   ** [SELECT] statement.
  3855   3886   ** ^The name of the database or table or column can be returned as
  3856   3887   ** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
  3857   3888   ** the database name, the _table_ routines return the table name, and
................................................................................
  3896   3927   SQLITE_API const char *SQLITE_STDCALL sqlite3_column_table_name(sqlite3_stmt*,int);
  3897   3928   SQLITE_API const void *SQLITE_STDCALL sqlite3_column_table_name16(sqlite3_stmt*,int);
  3898   3929   SQLITE_API const char *SQLITE_STDCALL sqlite3_column_origin_name(sqlite3_stmt*,int);
  3899   3930   SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt*,int);
  3900   3931   
  3901   3932   /*
  3902   3933   ** CAPI3REF: Declared Datatype Of A Query Result
         3934  +** METHOD: sqlite3_stmt
  3903   3935   **
  3904   3936   ** ^(The first parameter is a [prepared statement].
  3905   3937   ** If this statement is a [SELECT] statement and the Nth column of the
  3906   3938   ** returned result set of that [SELECT] is a table column (not an
  3907   3939   ** expression or subquery) then the declared type of the table
  3908   3940   ** column is returned.)^  ^If the Nth column of the result set is an
  3909   3941   ** expression or subquery, then a NULL pointer is returned.
................................................................................
  3928   3960   ** used to hold those values.
  3929   3961   */
  3930   3962   SQLITE_API const char *SQLITE_STDCALL sqlite3_column_decltype(sqlite3_stmt*,int);
  3931   3963   SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt*,int);
  3932   3964   
  3933   3965   /*
  3934   3966   ** CAPI3REF: Evaluate An SQL Statement
         3967  +** METHOD: sqlite3_stmt
  3935   3968   **
  3936   3969   ** After a [prepared statement] has been prepared using either
  3937   3970   ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
  3938   3971   ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
  3939   3972   ** must be called one or more times to evaluate the statement.
  3940   3973   **
  3941   3974   ** The details of the behavior of the sqlite3_step() interface depend
................................................................................
  4007   4040   ** then the more specific [error codes] are returned directly
  4008   4041   ** by sqlite3_step().  The use of the "v2" interface is recommended.
  4009   4042   */
  4010   4043   SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt*);
  4011   4044   
  4012   4045   /*
  4013   4046   ** CAPI3REF: Number of columns in a result set
         4047  +** METHOD: sqlite3_stmt
  4014   4048   **
  4015   4049   ** ^The sqlite3_data_count(P) interface returns the number of columns in the
  4016   4050   ** current row of the result set of [prepared statement] P.
  4017   4051   ** ^If prepared statement P does not have results ready to return
  4018   4052   ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
  4019   4053   ** interfaces) then sqlite3_data_count(P) returns 0.
  4020   4054   ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
................................................................................
  4060   4094   # define SQLITE_TEXT     3
  4061   4095   #endif
  4062   4096   #define SQLITE3_TEXT     3
  4063   4097   
  4064   4098   /*
  4065   4099   ** CAPI3REF: Result Values From A Query
  4066   4100   ** KEYWORDS: {column access functions}
         4101  +** METHOD: sqlite3_stmt
  4067   4102   **
  4068   4103   ** These routines form the "result set" interface.
  4069   4104   **
  4070   4105   ** ^These routines return information about a single column of the current
  4071   4106   ** result row of a query.  ^In every case the first argument is a pointer
  4072   4107   ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
  4073   4108   ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
................................................................................
  4232   4267   SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_column_text(sqlite3_stmt*, int iCol);
  4233   4268   SQLITE_API const void *SQLITE_STDCALL sqlite3_column_text16(sqlite3_stmt*, int iCol);
  4234   4269   SQLITE_API int SQLITE_STDCALL sqlite3_column_type(sqlite3_stmt*, int iCol);
  4235   4270   SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_column_value(sqlite3_stmt*, int iCol);
  4236   4271   
  4237   4272   /*
  4238   4273   ** CAPI3REF: Destroy A Prepared Statement Object
         4274  +** DESTRUCTOR: sqlite3_stmt
  4239   4275   **
  4240   4276   ** ^The sqlite3_finalize() function is called to delete a [prepared statement].
  4241   4277   ** ^If the most recent evaluation of the statement encountered no errors
  4242   4278   ** or if the statement is never been evaluated, then sqlite3_finalize() returns
  4243   4279   ** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
  4244   4280   ** sqlite3_finalize(S) returns the appropriate [error code] or
  4245   4281   ** [extended error code].
................................................................................
  4259   4295   ** statement after it has been finalized can result in undefined and
  4260   4296   ** undesirable behavior such as segfaults and heap corruption.
  4261   4297   */
  4262   4298   SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt);
  4263   4299   
  4264   4300   /*
  4265   4301   ** CAPI3REF: Reset A Prepared Statement Object
         4302  +** METHOD: sqlite3_stmt
  4266   4303   **
  4267   4304   ** The sqlite3_reset() function is called to reset a [prepared statement]
  4268   4305   ** object back to its initial state, ready to be re-executed.
  4269   4306   ** ^Any SQL statement variables that had values bound to them using
  4270   4307   ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
  4271   4308   ** Use [sqlite3_clear_bindings()] to reset the bindings.
  4272   4309   **
................................................................................
  4288   4325   SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt);
  4289   4326   
  4290   4327   /*
  4291   4328   ** CAPI3REF: Create Or Redefine SQL Functions
  4292   4329   ** KEYWORDS: {function creation routines}
  4293   4330   ** KEYWORDS: {application-defined SQL function}
  4294   4331   ** KEYWORDS: {application-defined SQL functions}
         4332  +** METHOD: sqlite3
  4295   4333   **
  4296   4334   ** ^These functions (collectively known as "function creation routines")
  4297   4335   ** are used to add SQL functions or aggregates or to redefine the behavior
  4298   4336   ** of existing SQL functions or aggregates.  The only differences between
  4299   4337   ** these routines are the text encoding expected for
  4300   4338   ** the second parameter (the name of the function being created)
  4301   4339   ** and the presence or absence of a destructor callback for
................................................................................
  4457   4495   SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_thread_cleanup(void);
  4458   4496   SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
  4459   4497                         void*,sqlite3_int64);
  4460   4498   #endif
  4461   4499   
  4462   4500   /*
  4463   4501   ** CAPI3REF: Obtaining SQL Function Parameter Values
         4502  +** METHOD: sqlite3_value
  4464   4503   **
  4465   4504   ** The C-language implementation of SQL functions and aggregates uses
  4466   4505   ** this set of interface routines to access the parameter values on
  4467   4506   ** the function or aggregate.
  4468   4507   **
  4469   4508   ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
  4470   4509   ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
................................................................................
  4515   4554   SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value*);
  4516   4555   SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value*);
  4517   4556   SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value*);
  4518   4557   SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*);
  4519   4558   
  4520   4559   /*
  4521   4560   ** CAPI3REF: Obtain Aggregate Function Context
         4561  +** METHOD: sqlite3_context
  4522   4562   **
  4523   4563   ** Implementations of aggregate SQL functions use this
  4524   4564   ** routine to allocate memory for storing their state.
  4525   4565   **
  4526   4566   ** ^The first time the sqlite3_aggregate_context(C,N) routine is called 
  4527   4567   ** for a particular aggregate function, SQLite
  4528   4568   ** allocates N of memory, zeroes out that memory, and returns a pointer
................................................................................
  4559   4599   ** This routine must be called from the same thread in which
  4560   4600   ** the aggregate SQL function is running.
  4561   4601   */
  4562   4602   SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context*, int nBytes);
  4563   4603   
  4564   4604   /*
  4565   4605   ** CAPI3REF: User Data For Functions
         4606  +** METHOD: sqlite3_context
  4566   4607   **
  4567   4608   ** ^The sqlite3_user_data() interface returns a copy of
  4568   4609   ** the pointer that was the pUserData parameter (the 5th parameter)
  4569   4610   ** of the [sqlite3_create_function()]
  4570   4611   ** and [sqlite3_create_function16()] routines that originally
  4571   4612   ** registered the application defined function.
  4572   4613   **
................................................................................
  4573   4614   ** This routine must be called from the same thread in which
  4574   4615   ** the application-defined function is running.
  4575   4616   */
  4576   4617   SQLITE_API void *SQLITE_STDCALL sqlite3_user_data(sqlite3_context*);
  4577   4618   
  4578   4619   /*
  4579   4620   ** CAPI3REF: Database Connection For Functions
         4621  +** METHOD: sqlite3_context
  4580   4622   **
  4581   4623   ** ^The sqlite3_context_db_handle() interface returns a copy of
  4582   4624   ** the pointer to the [database connection] (the 1st parameter)
  4583   4625   ** of the [sqlite3_create_function()]
  4584   4626   ** and [sqlite3_create_function16()] routines that originally
  4585   4627   ** registered the application defined function.
  4586   4628   */
  4587   4629   SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context*);
  4588   4630   
  4589   4631   /*
  4590   4632   ** CAPI3REF: Function Auxiliary Data
         4633  +** METHOD: sqlite3_context
  4591   4634   **
  4592   4635   ** These functions may be used by (non-aggregate) SQL functions to
  4593   4636   ** associate metadata with argument values. If the same value is passed to
  4594   4637   ** multiple invocations of the same SQL function during query execution, under
  4595   4638   ** some circumstances the associated metadata may be preserved.  An example
  4596   4639   ** of where this might be useful is in a regular-expression matching
  4597   4640   ** function. The compiled version of the regular expression can be stored as
................................................................................
  4656   4699   */
  4657   4700   typedef void (*sqlite3_destructor_type)(void*);
  4658   4701   #define SQLITE_STATIC      ((sqlite3_destructor_type)0)
  4659   4702   #define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
  4660   4703   
  4661   4704   /*
  4662   4705   ** CAPI3REF: Setting The Result Of An SQL Function
         4706  +** METHOD: sqlite3_context
  4663   4707   **
  4664   4708   ** These routines are used by the xFunc or xFinal callbacks that
  4665   4709   ** implement SQL functions and aggregates.  See
  4666   4710   ** [sqlite3_create_function()] and [sqlite3_create_function16()]
  4667   4711   ** for additional information.
  4668   4712   **
  4669   4713   ** These functions work very much like the [parameter binding] family of
................................................................................
  4791   4835   SQLITE_API void SQLITE_STDCALL sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
  4792   4836   SQLITE_API void SQLITE_STDCALL sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
  4793   4837   SQLITE_API void SQLITE_STDCALL sqlite3_result_value(sqlite3_context*, sqlite3_value*);
  4794   4838   SQLITE_API void SQLITE_STDCALL sqlite3_result_zeroblob(sqlite3_context*, int n);
  4795   4839   
  4796   4840   /*
  4797   4841   ** CAPI3REF: Define New Collating Sequences
         4842  +** METHOD: sqlite3
  4798   4843   **
  4799   4844   ** ^These functions add, remove, or modify a [collation] associated
  4800   4845   ** with the [database connection] specified as the first argument.
  4801   4846   **
  4802   4847   ** ^The name of the collation is a UTF-8 string
  4803   4848   ** for sqlite3_create_collation() and sqlite3_create_collation_v2()
  4804   4849   ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
................................................................................
  4893   4938     int eTextRep, 
  4894   4939     void *pArg,
  4895   4940     int(*xCompare)(void*,int,const void*,int,const void*)
  4896   4941   );
  4897   4942   
  4898   4943   /*
  4899   4944   ** CAPI3REF: Collation Needed Callbacks
         4945  +** METHOD: sqlite3
  4900   4946   **
  4901   4947   ** ^To avoid having to register all collation sequences before a database
  4902   4948   ** can be used, a single callback function may be registered with the
  4903   4949   ** [database connection] to be invoked whenever an undefined collation
  4904   4950   ** sequence is required.
  4905   4951   **
  4906   4952   ** ^If the function is registered using the sqlite3_collation_needed() API,
................................................................................
  5100   5146   ** or else the use of the [data_store_directory pragma] should be avoided.
  5101   5147   */
  5102   5148   SQLITE_API char *sqlite3_data_directory;
  5103   5149   
  5104   5150   /*
  5105   5151   ** CAPI3REF: Test For Auto-Commit Mode
  5106   5152   ** KEYWORDS: {autocommit mode}
         5153  +** METHOD: sqlite3
  5107   5154   **
  5108   5155   ** ^The sqlite3_get_autocommit() interface returns non-zero or
  5109   5156   ** zero if the given database connection is or is not in autocommit mode,
  5110   5157   ** respectively.  ^Autocommit mode is on by default.
  5111   5158   ** ^Autocommit mode is disabled by a [BEGIN] statement.
  5112   5159   ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
  5113   5160   **
................................................................................
  5122   5169   ** connection while this routine is running, then the return value
  5123   5170   ** is undefined.
  5124   5171   */
  5125   5172   SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3*);
  5126   5173   
  5127   5174   /*
  5128   5175   ** CAPI3REF: Find The Database Handle Of A Prepared Statement
         5176  +** METHOD: sqlite3_stmt
  5129   5177   **
  5130   5178   ** ^The sqlite3_db_handle interface returns the [database connection] handle
  5131   5179   ** to which a [prepared statement] belongs.  ^The [database connection]
  5132   5180   ** returned by sqlite3_db_handle is the same [database connection]
  5133   5181   ** that was the first argument
  5134   5182   ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
  5135   5183   ** create the statement in the first place.
  5136   5184   */
  5137   5185   SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_db_handle(sqlite3_stmt*);
  5138   5186   
  5139   5187   /*
  5140   5188   ** CAPI3REF: Return The Filename For A Database Connection
         5189  +** METHOD: sqlite3
  5141   5190   **
  5142   5191   ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
  5143   5192   ** associated with database N of connection D.  ^The main database file
  5144   5193   ** has the name "main".  If there is no attached database N on the database
  5145   5194   ** connection D, or if database N is a temporary or in-memory database, then
  5146   5195   ** a NULL pointer is returned.
  5147   5196   **
................................................................................
  5150   5199   ** will be an absolute pathname, even if the filename used
  5151   5200   ** to open the database originally was a URI or relative pathname.
  5152   5201   */
  5153   5202   SQLITE_API const char *SQLITE_STDCALL sqlite3_db_filename(sqlite3 *db, const char *zDbName);
  5154   5203   
  5155   5204   /*
  5156   5205   ** CAPI3REF: Determine if a database is read-only
         5206  +** METHOD: sqlite3
  5157   5207   **
  5158   5208   ** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
  5159   5209   ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
  5160   5210   ** the name of a database on connection D.
  5161   5211   */
  5162   5212   SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
  5163   5213   
  5164   5214   /*
  5165   5215   ** CAPI3REF: Find the next prepared statement
         5216  +** METHOD: sqlite3
  5166   5217   **
  5167   5218   ** ^This interface returns a pointer to the next [prepared statement] after
  5168   5219   ** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
  5169   5220   ** then this interface returns a pointer to the first prepared statement
  5170   5221   ** associated with the database connection pDb.  ^If no prepared statement
  5171   5222   ** satisfies the conditions of this routine, it returns NULL.
  5172   5223   **
................................................................................
  5174   5225   ** [sqlite3_next_stmt(D,S)] must refer to an open database
  5175   5226   ** connection and in particular must not be a NULL pointer.
  5176   5227   */
  5177   5228   SQLITE_API sqlite3_stmt *SQLITE_STDCALL sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
  5178   5229   
  5179   5230   /*
  5180   5231   ** CAPI3REF: Commit And Rollback Notification Callbacks
         5232  +** METHOD: sqlite3
  5181   5233   **
  5182   5234   ** ^The sqlite3_commit_hook() interface registers a callback
  5183   5235   ** function to be invoked whenever a transaction is [COMMIT | committed].
  5184   5236   ** ^Any callback set by a previous call to sqlite3_commit_hook()
  5185   5237   ** for the same database connection is overridden.
  5186   5238   ** ^The sqlite3_rollback_hook() interface registers a callback
  5187   5239   ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
................................................................................
  5223   5275   ** See also the [sqlite3_update_hook()] interface.
  5224   5276   */
  5225   5277   SQLITE_API void *SQLITE_STDCALL sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
  5226   5278   SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
  5227   5279   
  5228   5280   /*
  5229   5281   ** CAPI3REF: Data Change Notification Callbacks
         5282  +** METHOD: sqlite3
  5230   5283   **
  5231   5284   ** ^The sqlite3_update_hook() interface registers a callback function
  5232   5285   ** with the [database connection] identified by the first argument
  5233   5286   ** to be invoked whenever a row is updated, inserted or deleted in
  5234   5287   ** a rowid table.
  5235   5288   ** ^Any callback set by a previous call to this function
  5236   5289   ** for the same database connection is overridden.
................................................................................
  5329   5382   **
  5330   5383   ** See also: [sqlite3_db_release_memory()]
  5331   5384   */
  5332   5385   SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int);
  5333   5386   
  5334   5387   /*
  5335   5388   ** CAPI3REF: Free Memory Used By A Database Connection
         5389  +** METHOD: sqlite3
  5336   5390   **
  5337   5391   ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
  5338   5392   ** memory as possible from database connection D. Unlike the
  5339   5393   ** [sqlite3_release_memory()] interface, this interface is in effect even
  5340   5394   ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
  5341   5395   ** omitted.
  5342   5396   **
................................................................................
  5406   5460   ** [sqlite3_soft_heap_limit64()] interface rather than this one.
  5407   5461   */
  5408   5462   SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_soft_heap_limit(int N);
  5409   5463   
  5410   5464   
  5411   5465   /*
  5412   5466   ** CAPI3REF: Extract Metadata About A Column Of A Table
         5467  +** METHOD: sqlite3
  5413   5468   **
  5414   5469   ** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
  5415   5470   ** information about column C of table T in database D
  5416   5471   ** on [database connection] X.)^  ^The sqlite3_table_column_metadata()
  5417   5472   ** interface returns SQLITE_OK and fills in the non-NULL pointers in
  5418   5473   ** the final five arguments with appropriate values if the specified
  5419   5474   ** column exists.  ^The sqlite3_table_column_metadata() interface returns
................................................................................
  5484   5539     int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
  5485   5540     int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
  5486   5541     int *pAutoinc               /* OUTPUT: True if column is auto-increment */
  5487   5542   );
  5488   5543   
  5489   5544   /*
  5490   5545   ** CAPI3REF: Load An Extension
         5546  +** METHOD: sqlite3
  5491   5547   **
  5492   5548   ** ^This interface loads an SQLite extension library from the named file.
  5493   5549   **
  5494   5550   ** ^The sqlite3_load_extension() interface attempts to load an
  5495   5551   ** [SQLite extension] library contained in the file zFile.  If
  5496   5552   ** the file cannot be loaded directly, attempts are made to load
  5497   5553   ** with various operating-system specific extensions added.
................................................................................
  5525   5581     const char *zFile,    /* Name of the shared library containing extension */
  5526   5582     const char *zProc,    /* Entry point.  Derived from zFile if 0 */
  5527   5583     char **pzErrMsg       /* Put error message here if not 0 */
  5528   5584   );
  5529   5585   
  5530   5586   /*
  5531   5587   ** CAPI3REF: Enable Or Disable Extension Loading
         5588  +** METHOD: sqlite3
  5532   5589   **
  5533   5590   ** ^So as not to open security holes in older applications that are
  5534   5591   ** unprepared to deal with [extension loading], and as a means of disabling
  5535   5592   ** [extension loading] while evaluating user-entered SQL, the following API
  5536   5593   ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
  5537   5594   **
  5538   5595   ** ^Extension loading is off by default.
................................................................................
  5774   5831   #define SQLITE_INDEX_CONSTRAINT_LE    8
  5775   5832   #define SQLITE_INDEX_CONSTRAINT_LT    16
  5776   5833   #define SQLITE_INDEX_CONSTRAINT_GE    32
  5777   5834   #define SQLITE_INDEX_CONSTRAINT_MATCH 64
  5778   5835   
  5779   5836   /*
  5780   5837   ** CAPI3REF: Register A Virtual Table Implementation
         5838  +** METHOD: sqlite3
  5781   5839   **
  5782   5840   ** ^These routines are used to register a new [virtual table module] name.
  5783   5841   ** ^Module names must be registered before
  5784   5842   ** creating a new [virtual table] using the module and before using a
  5785   5843   ** preexisting [virtual table] for the module.
  5786   5844   **
  5787   5845   ** ^The module name is registered on the [database connection] specified
................................................................................
  5870   5928   ** to declare the format (the names and datatypes of the columns) of
  5871   5929   ** the virtual tables they implement.
  5872   5930   */
  5873   5931   SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3*, const char *zSQL);
  5874   5932   
  5875   5933   /*
  5876   5934   ** CAPI3REF: Overload A Function For A Virtual Table
         5935  +** METHOD: sqlite3
  5877   5936   **
  5878   5937   ** ^(Virtual tables can provide alternative implementations of functions
  5879   5938   ** using the [xFindFunction] method of the [virtual table module].  
  5880   5939   ** But global versions of those functions
  5881   5940   ** must exist in order to be overloaded.)^
  5882   5941   **
  5883   5942   ** ^(This API makes sure a global version of a function with a particular
................................................................................
  5912   5971   ** can be used to read or write small subsections of the BLOB.
  5913   5972   ** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
  5914   5973   */
  5915   5974   typedef struct sqlite3_blob sqlite3_blob;
  5916   5975   
  5917   5976   /*
  5918   5977   ** CAPI3REF: Open A BLOB For Incremental I/O
         5978  +** METHOD: sqlite3
         5979  +** CONSTRUCTOR: sqlite3_blob
  5919   5980   **
  5920   5981   ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
  5921   5982   ** in row iRow, column zColumn, table zTable in database zDb;
  5922   5983   ** in other words, the same BLOB that would be selected by:
  5923   5984   **
  5924   5985   ** <pre>
  5925   5986   **     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
................................................................................
  5993   6054     sqlite3_int64 iRow,
  5994   6055     int flags,
  5995   6056     sqlite3_blob **ppBlob
  5996   6057   );
  5997   6058   
  5998   6059   /*
  5999   6060   ** CAPI3REF: Move a BLOB Handle to a New Row
         6061  +** METHOD: sqlite3_blob
  6000   6062   **
  6001   6063   ** ^This function is used to move an existing blob handle so that it points
  6002   6064   ** to a different row of the same database table. ^The new row is identified
  6003   6065   ** by the rowid value passed as the second argument. Only the row can be
  6004   6066   ** changed. ^The database, table and column on which the blob handle is open
  6005   6067   ** remain the same. Moving an existing blob handle to a new row can be
  6006   6068   ** faster than closing the existing handle and opening a new one.
................................................................................
  6017   6079   **
  6018   6080   ** ^This function sets the database handle error code and message.
  6019   6081   */
  6020   6082   SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
  6021   6083   
  6022   6084   /*
  6023   6085   ** CAPI3REF: Close A BLOB Handle
         6086  +** DESTRUCTOR: sqlite3_blob
  6024   6087   **
  6025   6088   ** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
  6026   6089   ** unconditionally.  Even if this routine returns an error code, the 
  6027   6090   ** handle is still closed.)^
  6028   6091   **
  6029   6092   ** ^If the blob handle being closed was opened for read-write access, and if
  6030   6093   ** the database is in auto-commit mode and there are no other open read-write
................................................................................
  6039   6102   ** is passed a valid open blob handle, the values returned by the 
  6040   6103   ** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
  6041   6104   */
  6042   6105   SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *);
  6043   6106   
  6044   6107   /*
  6045   6108   ** CAPI3REF: Return The Size Of An Open BLOB
         6109  +** METHOD: sqlite3_blob
  6046   6110   **
  6047   6111   ** ^Returns the size in bytes of the BLOB accessible via the 
  6048   6112   ** successfully opened [BLOB handle] in its only argument.  ^The
  6049   6113   ** incremental blob I/O routines can only read or overwriting existing
  6050   6114   ** blob content; they cannot change the size of a blob.
  6051   6115   **
  6052   6116   ** This routine only works on a [BLOB handle] which has been created
................................................................................
  6054   6118   ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
  6055   6119   ** to this routine results in undefined and probably undesirable behavior.
  6056   6120   */
  6057   6121   SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *);
  6058   6122   
  6059   6123   /*
  6060   6124   ** CAPI3REF: Read Data From A BLOB Incrementally
         6125  +** METHOD: sqlite3_blob
  6061   6126   **
  6062   6127   ** ^(This function is used to read data from an open [BLOB handle] into a
  6063   6128   ** caller-supplied buffer. N bytes of data are copied into buffer Z
  6064   6129   ** from the open BLOB, starting at offset iOffset.)^
  6065   6130   **
  6066   6131   ** ^If offset iOffset is less than N bytes from the end of the BLOB,
  6067   6132   ** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
................................................................................
  6082   6147   **
  6083   6148   ** See also: [sqlite3_blob_write()].
  6084   6149   */
  6085   6150   SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
  6086   6151   
  6087   6152   /*
  6088   6153   ** CAPI3REF: Write Data Into A BLOB Incrementally
         6154  +** METHOD: sqlite3_blob
  6089   6155   **
  6090   6156   ** ^(This function is used to write data into an open [BLOB handle] from a
  6091   6157   ** caller-supplied buffer. N bytes of data are copied from the buffer Z
  6092   6158   ** into the open BLOB, starting at offset iOffset.)^
  6093   6159   **
  6094   6160   ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
  6095   6161   ** Otherwise, an  [error code] or an [extended error code] is returned.)^
................................................................................
  6409   6475   #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
  6410   6476   #define SQLITE_MUTEX_STATIC_APP1      8  /* For use by application */
  6411   6477   #define SQLITE_MUTEX_STATIC_APP2      9  /* For use by application */
  6412   6478   #define SQLITE_MUTEX_STATIC_APP3     10  /* For use by application */
  6413   6479   
  6414   6480   /*
  6415   6481   ** CAPI3REF: Retrieve the mutex for a database connection
         6482  +** METHOD: sqlite3
  6416   6483   **
  6417   6484   ** ^This interface returns a pointer the [sqlite3_mutex] object that 
  6418   6485   ** serializes access to the [database connection] given in the argument
  6419   6486   ** when the [threading mode] is Serialized.
  6420   6487   ** ^If the [threading mode] is Single-thread or Multi-thread then this
  6421   6488   ** routine returns a NULL pointer.
  6422   6489   */
  6423   6490   SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3*);
  6424   6491   
  6425   6492   /*
  6426   6493   ** CAPI3REF: Low-Level Control Of Database Files
         6494  +** METHOD: sqlite3
  6427   6495   **
  6428   6496   ** ^The [sqlite3_file_control()] interface makes a direct call to the
  6429   6497   ** xFileControl method for the [sqlite3_io_methods] object associated
  6430   6498   ** with a particular database identified by the second argument. ^The
  6431   6499   ** name of the database is "main" for the main database or "temp" for the
  6432   6500   ** TEMP database, or the name that appears after the AS keyword for
  6433   6501   ** databases that are added using the [ATTACH] SQL command.
................................................................................
  6636   6704   #define SQLITE_STATUS_PARSER_STACK         6
  6637   6705   #define SQLITE_STATUS_PAGECACHE_SIZE       7
  6638   6706   #define SQLITE_STATUS_SCRATCH_SIZE         8
  6639   6707   #define SQLITE_STATUS_MALLOC_COUNT         9
  6640   6708   
  6641   6709   /*
  6642   6710   ** CAPI3REF: Database Connection Status
         6711  +** METHOD: sqlite3
  6643   6712   **
  6644   6713   ** ^This interface is used to retrieve runtime status information 
  6645   6714   ** about a single [database connection].  ^The first argument is the
  6646   6715   ** database connection object to be interrogated.  ^The second argument
  6647   6716   ** is an integer constant, taken from the set of
  6648   6717   ** [SQLITE_DBSTATUS options], that
  6649   6718   ** determines the parameter to interrogate.  The set of 
................................................................................
  6764   6833   #define SQLITE_DBSTATUS_CACHE_WRITE          9
  6765   6834   #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  6766   6835   #define SQLITE_DBSTATUS_MAX                 10   /* Largest defined DBSTATUS */
  6767   6836   
  6768   6837   
  6769   6838   /*
  6770   6839   ** CAPI3REF: Prepared Statement Status
         6840  +** METHOD: sqlite3_stmt
  6771   6841   **
  6772   6842   ** ^(Each prepared statement maintains various
  6773   6843   ** [SQLITE_STMTSTATUS counters] that measure the number
  6774   6844   ** of times it has performed specific operations.)^  These counters can
  6775   6845   ** be used to monitor the performance characteristics of the prepared
  6776   6846   ** statements.  For example, if the number of table steps greatly exceeds
  6777   6847   ** the number of table searches or result rows, that would tend to indicate
................................................................................
  7267   7337   SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage);
  7268   7338   SQLITE_API int SQLITE_STDCALL sqlite3_backup_finish(sqlite3_backup *p);
  7269   7339   SQLITE_API int SQLITE_STDCALL sqlite3_backup_remaining(sqlite3_backup *p);
  7270   7340   SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p);
  7271   7341   
  7272   7342   /*
  7273   7343   ** CAPI3REF: Unlock Notification
         7344  +** METHOD: sqlite3
  7274   7345   **
  7275   7346   ** ^When running in shared-cache mode, a database operation may fail with
  7276   7347   ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
  7277   7348   ** individual tables within the shared-cache cannot be obtained. See
  7278   7349   ** [SQLite Shared-Cache Mode] for a description of shared-cache locking. 
  7279   7350   ** ^This API may be used to register a callback that SQLite will invoke 
  7280   7351   ** when the connection currently holding the required lock relinquishes it.
................................................................................
  7437   7508   ** a few hundred characters, it will be truncated to the length of the
  7438   7509   ** buffer.
  7439   7510   */
  7440   7511   SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...);
  7441   7512   
  7442   7513   /*
  7443   7514   ** CAPI3REF: Write-Ahead Log Commit Hook
         7515  +** METHOD: sqlite3
  7444   7516   **
  7445   7517   ** ^The [sqlite3_wal_hook()] function is used to register a callback that
  7446   7518   ** is invoked each time data is committed to a database in wal mode.
  7447   7519   **
  7448   7520   ** ^(The callback is invoked by SQLite after the commit has taken place and 
  7449   7521   ** the associated write-lock on the database released)^, so the implementation 
  7450   7522   ** may read, write or [checkpoint] the database as required.
................................................................................
  7476   7548     sqlite3*, 
  7477   7549     int(*)(void *,sqlite3*,const char*,int),
  7478   7550     void*
  7479   7551   );
  7480   7552   
  7481   7553   /*
  7482   7554   ** CAPI3REF: Configure an auto-checkpoint
         7555  +** METHOD: sqlite3
  7483   7556   **
  7484   7557   ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
  7485   7558   ** [sqlite3_wal_hook()] that causes any database on [database connection] D
  7486   7559   ** to automatically [checkpoint]
  7487   7560   ** after committing a transaction if there are N or
  7488   7561   ** more frames in the [write-ahead log] file.  ^Passing zero or 
  7489   7562   ** a negative value as the nFrame parameter disables automatic
................................................................................
  7506   7579   ** is only necessary if the default setting is found to be suboptimal
  7507   7580   ** for a particular application.
  7508   7581   */
  7509   7582   SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
  7510   7583   
  7511   7584   /*
  7512   7585   ** CAPI3REF: Checkpoint a database
         7586  +** METHOD: sqlite3
  7513   7587   **
  7514   7588   ** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
  7515   7589   ** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
  7516   7590   **
  7517   7591   ** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the 
  7518   7592   ** [write-ahead log] for database X on [database connection] D to be
  7519   7593   ** transferred into the database file and for the write-ahead log to
................................................................................
  7527   7601   ** start a callback but which do not need the full power (and corresponding
  7528   7602   ** complication) of [sqlite3_wal_checkpoint_v2()].
  7529   7603   */
  7530   7604   SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
  7531   7605   
  7532   7606   /*
  7533   7607   ** CAPI3REF: Checkpoint a database
         7608  +** METHOD: sqlite3
  7534   7609   **
  7535   7610   ** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
  7536   7611   ** operation on database X of [database connection] D in mode M.  Status
  7537   7612   ** information is written back into integers pointed to by L and C.)^
  7538   7613   ** ^(The M parameter must be a valid [checkpoint mode]:)^
  7539   7614   **
  7540   7615   ** <dl>
................................................................................
  7781   7856   #define SQLITE_SCANSTAT_EST      2
  7782   7857   #define SQLITE_SCANSTAT_NAME     3
  7783   7858   #define SQLITE_SCANSTAT_EXPLAIN  4
  7784   7859   #define SQLITE_SCANSTAT_SELECTID 5
  7785   7860   
  7786   7861   /*
  7787   7862   ** CAPI3REF: Prepared Statement Scan Status
         7863  +** METHOD: sqlite3_stmt
  7788   7864   **
  7789   7865   ** This interface returns information about the predicted and measured
  7790   7866   ** performance for pStmt.  Advanced applications can use this
  7791   7867   ** interface to compare the predicted and the measured performance and
  7792   7868   ** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
  7793   7869   **
  7794   7870   ** Since this interface is expected to be rarely used, it is only
................................................................................
  7818   7894     int idx,                  /* Index of loop to report on */
  7819   7895     int iScanStatusOp,        /* Information desired.  SQLITE_SCANSTAT_* */
  7820   7896     void *pOut                /* Result written here */
  7821   7897   );     
  7822   7898   
  7823   7899   /*
  7824   7900   ** CAPI3REF: Zero Scan-Status Counters
         7901  +** METHOD: sqlite3_stmt
  7825   7902   **
  7826   7903   ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
  7827   7904   **
  7828   7905   ** This API is only available if the library is built with pre-processor
  7829   7906   ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
  7830   7907   */
  7831   7908   SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
................................................................................
  8428   8505   # define ALWAYS(X)      ((X)?1:(assert(0),0))
  8429   8506   # define NEVER(X)       ((X)?(assert(0),1):0)
  8430   8507   #else
  8431   8508   # define ALWAYS(X)      (X)
  8432   8509   # define NEVER(X)       (X)
  8433   8510   #endif
  8434   8511   
         8512  +/*
         8513  +** Declarations used for tracing the operating system interfaces.
         8514  +*/
         8515  +#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
         8516  +    (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
         8517  +  extern int sqlite3OSTrace;
         8518  +# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
         8519  +# define SQLITE_HAVE_OS_TRACE
         8520  +#else
         8521  +# define OSTRACE(X)
         8522  +# undef  SQLITE_HAVE_OS_TRACE
         8523  +#endif
         8524  +
         8525  +/*
         8526  +** Is the sqlite3ErrName() function needed in the build?  Currently,
         8527  +** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
         8528  +** OSTRACE is enabled), and by several "test*.c" files (which are
         8529  +** compiled using SQLITE_TEST).
         8530  +*/
         8531  +#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
         8532  +    (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
         8533  +# define SQLITE_NEED_ERR_NAME
         8534  +#else
         8535  +# undef  SQLITE_NEED_ERR_NAME
         8536  +#endif
         8537  +
  8435   8538   /*
  8436   8539   ** Return true (non-zero) if the input is an integer that is too large
  8437   8540   ** to fit in 32-bits.  This macro is used inside of various testcase()
  8438   8541   ** macros to verify that we have tested SQLite for large-file support.
  8439   8542   */
  8440   8543   #define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
  8441   8544   
................................................................................
  9839   9942   
  9840   9943   
  9841   9944   /* Properties such as "out2" or "jump" that are specified in
  9842   9945   ** comments following the "case" for each opcode in the vdbe.c
  9843   9946   ** are encoded into bitvectors as follows:
  9844   9947   */
  9845   9948   #define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
  9846         -#define OPFLG_OUT2_PRERELEASE 0x0002  /* out2-prerelease: */
  9847         -#define OPFLG_IN1             0x0004  /* in1:   P1 is an input */
  9848         -#define OPFLG_IN2             0x0008  /* in2:   P2 is an input */
  9849         -#define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
  9850         -#define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */
  9851         -#define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */
         9949  +#define OPFLG_IN1             0x0002  /* in1:   P1 is an input */
         9950  +#define OPFLG_IN2             0x0004  /* in2:   P2 is an input */
         9951  +#define OPFLG_IN3             0x0008  /* in3:   P3 is an input */
         9952  +#define OPFLG_OUT2            0x0010  /* out2:  P2 is an output */
         9953  +#define OPFLG_OUT3            0x0020  /* out3:  P3 is an output */
  9852   9954   #define OPFLG_INITIALIZER {\
  9853   9955   /*   0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,\
  9854         -/*   8 */ 0x01, 0x01, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00,\
  9855         -/*  16 */ 0x01, 0x01, 0x04, 0x24, 0x01, 0x04, 0x05, 0x10,\
  9856         -/*  24 */ 0x00, 0x02, 0x02, 0x02, 0x02, 0x00, 0x02, 0x02,\
  9857         -/*  32 */ 0x00, 0x00, 0x20, 0x00, 0x00, 0x04, 0x05, 0x04,\
  9858         -/*  40 */ 0x04, 0x00, 0x00, 0x01, 0x01, 0x05, 0x05, 0x00,\
  9859         -/*  48 */ 0x00, 0x00, 0x02, 0x02, 0x10, 0x00, 0x00, 0x00,\
  9860         -/*  56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\
  9861         -/*  64 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x4c,\
  9862         -/*  72 */ 0x4c, 0x02, 0x02, 0x00, 0x05, 0x05, 0x15, 0x15,\
  9863         -/*  80 */ 0x15, 0x15, 0x15, 0x15, 0x00, 0x4c, 0x4c, 0x4c,\
  9864         -/*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x00,\
  9865         -/*  96 */ 0x24, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,\
  9866         -/* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x08, 0x08, 0x00,\
  9867         -/* 112 */ 0x02, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00,\
  9868         -/* 120 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  9869         -/* 128 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x02, 0x00, 0x01,\
  9870         -/* 136 */ 0x08, 0x05, 0x05, 0x05, 0x05, 0x05, 0x00, 0x01,\
         9956  +/*   8 */ 0x01, 0x01, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\
         9957  +/*  16 */ 0x01, 0x01, 0x02, 0x12, 0x01, 0x02, 0x03, 0x08,\
         9958  +/*  24 */ 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x10,\
         9959  +/*  32 */ 0x00, 0x00, 0x10, 0x00, 0x00, 0x02, 0x03, 0x02,\
         9960  +/*  40 */ 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x00,\
         9961  +/*  48 */ 0x00, 0x00, 0x10, 0x10, 0x08, 0x00, 0x00, 0x00,\
         9962  +/*  56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09,\
         9963  +/*  64 */ 0x09, 0x09, 0x04, 0x09, 0x09, 0x09, 0x09, 0x26,\
         9964  +/*  72 */ 0x26, 0x10, 0x10, 0x00, 0x03, 0x03, 0x0b, 0x0b,\
         9965  +/*  80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x26, 0x26, 0x26,\
         9966  +/*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\
         9967  +/*  96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
         9968  +/* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x00,\
         9969  +/* 112 */ 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00, 0x00,\
         9970  +/* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
         9971  +/* 128 */ 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00, 0x01,\
         9972  +/* 136 */ 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x01,\
  9871   9973   /* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,\
  9872         -/* 152 */ 0x00, 0x02, 0x02, 0x01, 0x00, 0x00,}
         9974  +/* 152 */ 0x00, 0x10, 0x10, 0x01, 0x00, 0x00,}
  9873   9975   
  9874   9976   /************** End of opcodes.h *********************************************/
  9875   9977   /************** Continuing where we left off in vdbe.h ***********************/
  9876   9978   
  9877   9979   /*
  9878   9980   ** Prototypes for the VDBE interface.  See comments on the implementation
  9879   9981   ** for a description of what each of these routines does.
................................................................................
  9924  10026   #ifndef SQLITE_OMIT_TRACE
  9925  10027   SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
  9926  10028   #endif
  9927  10029   SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
  9928  10030   
  9929  10031   SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
  9930  10032   SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
        10033  +SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
  9931  10034   SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
  9932  10035   
  9933  10036   typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
  9934  10037   SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
  9935  10038   
  9936  10039   #ifndef SQLITE_OMIT_TRIGGER
  9937  10040   SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
................................................................................
 11061  11164   #define SQLITE_AutoIndex      0x00100000  /* Enable automatic indexes */
 11062  11165   #define SQLITE_PreferBuiltin  0x00200000  /* Preference to built-in funcs */
 11063  11166   #define SQLITE_LoadExtension  0x00400000  /* Enable load_extension */
 11064  11167   #define SQLITE_EnableTrigger  0x00800000  /* True to enable triggers */
 11065  11168   #define SQLITE_DeferFKs       0x01000000  /* Defer all FK constraints */
 11066  11169   #define SQLITE_QueryOnly      0x02000000  /* Disable database changes */
 11067  11170   #define SQLITE_VdbeEQP        0x04000000  /* Debug EXPLAIN QUERY PLAN */
        11171  +#define SQLITE_Vacuum         0x08000000  /* Currently in a VACUUM */
 11068  11172   
 11069  11173   
 11070  11174   /*
 11071  11175   ** Bits of the sqlite3.dbOptFlags field that are used by the
 11072  11176   ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
 11073  11177   ** selectively disable various optimizations.
 11074  11178   */
................................................................................
 11391  11495     int nRef;                 /* Number of pointers to this structure */
 11392  11496     u8 bConstraint;           /* True if constraints are supported */
 11393  11497     int iSavepoint;           /* Depth of the SAVEPOINT stack */
 11394  11498     VTable *pNext;            /* Next in linked list (see above) */
 11395  11499   };
 11396  11500   
 11397  11501   /*
 11398         -** Each SQL table is represented in memory by an instance of the
 11399         -** following structure.
 11400         -**
 11401         -** Table.zName is the name of the table.  The case of the original
 11402         -** CREATE TABLE statement is stored, but case is not significant for
 11403         -** comparisons.
 11404         -**
 11405         -** Table.nCol is the number of columns in this table.  Table.aCol is a
 11406         -** pointer to an array of Column structures, one for each column.
 11407         -**
 11408         -** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
 11409         -** the column that is that key.   Otherwise Table.iPKey is negative.  Note
 11410         -** that the datatype of the PRIMARY KEY must be INTEGER for this field to
 11411         -** be set.  An INTEGER PRIMARY KEY is used as the rowid for each row of
 11412         -** the table.  If a table has no INTEGER PRIMARY KEY, then a random rowid
 11413         -** is generated for each row of the table.  TF_HasPrimaryKey is set if
 11414         -** the table has any PRIMARY KEY, INTEGER or otherwise.
 11415         -**
 11416         -** Table.tnum is the page number for the root BTree page of the table in the
 11417         -** database file.  If Table.iDb is the index of the database table backend
 11418         -** in sqlite.aDb[].  0 is for the main database and 1 is for the file that
 11419         -** holds temporary tables and indices.  If TF_Ephemeral is set
 11420         -** then the table is stored in a file that is automatically deleted
 11421         -** when the VDBE cursor to the table is closed.  In this case Table.tnum 
 11422         -** refers VDBE cursor number that holds the table open, not to the root
 11423         -** page number.  Transient tables are used to hold the results of a
 11424         -** sub-query that appears instead of a real table name in the FROM clause 
 11425         -** of a SELECT statement.
        11502  +** The schema for each SQL table and view is represented in memory
        11503  +** by an instance of the following structure.
 11426  11504   */
 11427  11505   struct Table {
 11428  11506     char *zName;         /* Name of the table or view */
 11429  11507     Column *aCol;        /* Information about each column */
 11430  11508     Index *pIndex;       /* List of SQL indexes on this table. */
 11431  11509     Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
 11432  11510     FKey *pFKey;         /* Linked list of all foreign keys in this table */
 11433  11511     char *zColAff;       /* String defining the affinity of each column */
 11434  11512   #ifndef SQLITE_OMIT_CHECK
 11435  11513     ExprList *pCheck;    /* All CHECK constraints */
 11436  11514   #endif
 11437         -  LogEst nRowLogEst;   /* Estimated rows in table - from sqlite_stat1 table */
 11438         -  int tnum;            /* Root BTree node for this table (see note above) */
 11439         -  i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
        11515  +  int tnum;            /* Root BTree page for this table */
        11516  +  i16 iPKey;           /* If not negative, use aCol[iPKey] as the rowid */
 11440  11517     i16 nCol;            /* Number of columns in this table */
 11441  11518     u16 nRef;            /* Number of pointers to this Table */
        11519  +  LogEst nRowLogEst;   /* Estimated rows in table - from sqlite_stat1 table */
 11442  11520     LogEst szTabRow;     /* Estimated size of each table row in bytes */
 11443  11521   #ifdef SQLITE_ENABLE_COSTMULT
 11444  11522     LogEst costMult;     /* Cost multiplier for using this table */
 11445  11523   #endif
 11446  11524     u8 tabFlags;         /* Mask of TF_* values */
 11447  11525     u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
 11448  11526   #ifndef SQLITE_OMIT_ALTERTABLE
................................................................................
 11456  11534     Trigger *pTrigger;   /* List of triggers stored in pSchema */
 11457  11535     Schema *pSchema;     /* Schema that contains this table */
 11458  11536     Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
 11459  11537   };
 11460  11538   
 11461  11539   /*
 11462  11540   ** Allowed values for Table.tabFlags.
        11541  +**
        11542  +** TF_OOOHidden applies to virtual tables that have hidden columns that are
        11543  +** followed by non-hidden columns.  Example:  "CREATE VIRTUAL TABLE x USING
        11544  +** vtab1(a HIDDEN, b);".  Since "b" is a non-hidden column but "a" is hidden,
        11545  +** the TF_OOOHidden attribute would apply in this case.  Such tables require
        11546  +** special handling during INSERT processing.
 11463  11547   */
 11464  11548   #define TF_Readonly        0x01    /* Read-only system table */
 11465  11549   #define TF_Ephemeral       0x02    /* An ephemeral table */
 11466  11550   #define TF_HasPrimaryKey   0x04    /* Table has a primary key */
 11467  11551   #define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
 11468  11552   #define TF_Virtual         0x10    /* Is a virtual table */
 11469  11553   #define TF_WithoutRowid    0x20    /* No rowid used. PRIMARY KEY is the key */
        11554  +#define TF_OOOHidden       0x40    /* Out-of-Order hidden columns */
 11470  11555   
 11471  11556   
 11472  11557   /*
 11473  11558   ** Test to see whether or not a table is a virtual table.  This is
 11474  11559   ** done as a macro so that it will be optimized out when virtual
 11475  11560   ** table support is omitted from the build.
 11476  11561   */
................................................................................
 12219  12304   #define SF_Resolved        0x0002  /* Identifiers have been resolved */
 12220  12305   #define SF_Aggregate       0x0004  /* Contains aggregate functions */
 12221  12306   #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
 12222  12307   #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
 12223  12308   #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
 12224  12309   #define SF_Compound        0x0040  /* Part of a compound query */
 12225  12310   #define SF_Values          0x0080  /* Synthesized from VALUES clause */
 12226         -#define SF_AllValues       0x0100  /* All terms of compound are VALUES */
        12311  +#define SF_MultiValue      0x0100  /* Single VALUES term with multiple rows */
 12227  12312   #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
 12228  12313   #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
 12229  12314   #define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
 12230  12315   #define SF_MinMaxAgg       0x1000  /* Aggregate containing min() or max() */
 12231  12316   #define SF_Converted       0x2000  /* By convertCompoundSelectToSubquery() */
 12232  12317   
 12233  12318   
................................................................................
 12603  12688    * "SELECT" statement. The meanings of the other members is determined by the 
 12604  12689    * value of "op" as follows:
 12605  12690    *
 12606  12691    * (op == TK_INSERT)
 12607  12692    * orconf    -> stores the ON CONFLICT algorithm
 12608  12693    * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
 12609  12694    *              this stores a pointer to the SELECT statement. Otherwise NULL.
 12610         - * target    -> A token holding the quoted name of the table to insert into.
        12695  + * zTarget   -> Dequoted name of the table to insert into.
 12611  12696    * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
 12612  12697    *              this stores values to be inserted. Otherwise NULL.
 12613  12698    * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ... 
 12614  12699    *              statement, then this stores the column-names to be
 12615  12700    *              inserted into.
 12616  12701    *
 12617  12702    * (op == TK_DELETE)
 12618         - * target    -> A token holding the quoted name of the table to delete from.
        12703  + * zTarget   -> Dequoted name of the table to delete from.
 12619  12704    * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
 12620  12705    *              Otherwise NULL.
 12621  12706    * 
 12622  12707    * (op == TK_UPDATE)
 12623         - * target    -> A token holding the quoted name of the table to update rows of.
        12708  + * zTarget   -> Dequoted name of the table to update.
 12624  12709    * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
 12625  12710    *              Otherwise NULL.
 12626  12711    * pExprList -> A list of the columns to update and the expressions to update
 12627  12712    *              them to. See sqlite3Update() documentation of "pChanges"
 12628  12713    *              argument.
 12629  12714    * 
 12630  12715    */
 12631  12716   struct TriggerStep {
 12632  12717     u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
 12633  12718     u8 orconf;           /* OE_Rollback etc. */
 12634  12719     Trigger *pTrig;      /* The trigger that this step is a part of */
 12635         -  Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
 12636         -  Token target;        /* Target table for DELETE, UPDATE, INSERT */
        12720  +  Select *pSelect;     /* SELECT statement or RHS of INSERT INTO SELECT ... */
        12721  +  char *zTarget;       /* Target table for DELETE, UPDATE, INSERT */
 12637  12722     Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
 12638  12723     ExprList *pExprList; /* SET clause for UPDATE. */
 12639  12724     IdList *pIdList;     /* Column names for INSERT */
 12640  12725     TriggerStep *pNext;  /* Next in the link-list */
 12641  12726     TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
 12642  12727   };
 12643  12728   
................................................................................
 12662  12747   */
 12663  12748   struct StrAccum {
 12664  12749     sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
 12665  12750     char *zBase;         /* A base allocation.  Not from malloc. */
 12666  12751     char *zText;         /* The string collected so far */
 12667  12752     int  nChar;          /* Length of the string so far */
 12668  12753     int  nAlloc;         /* Amount of space allocated in zText */
 12669         -  int  mxAlloc;        /* Maximum allowed string length */
 12670         -  u8   useMalloc;      /* 0: none,  1: sqlite3DbMalloc,  2: sqlite3_malloc */
        12754  +  int  mxAlloc;        /* Maximum allowed allocation.  0 for no malloc usage */
 12671  12755     u8   accError;       /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
 12672  12756   };
 12673  12757   #define STRACCUM_NOMEM   1
 12674  12758   #define STRACCUM_TOOBIG  2
 12675  12759   
 12676  12760   /*
 12677  12761   ** A pointer to this structure is used to communicate information
................................................................................
 12980  13064   #define SQLITE_PRINTF_INTERNAL 0x01
 12981  13065   #define SQLITE_PRINTF_SQLFUNC  0x02
 12982  13066   SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
 12983  13067   SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
 12984  13068   SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
 12985  13069   SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
 12986  13070   SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
 12987         -#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
        13071  +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
 12988  13072   SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
 12989  13073   #endif
 12990  13074   #if defined(SQLITE_TEST)
 12991  13075   SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
 12992  13076   #endif
 12993  13077   
 12994  13078   #if defined(SQLITE_DEBUG)
................................................................................
 13327  13411   SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
 13328  13412   SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
 13329  13413   SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
 13330  13414   SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
 13331  13415   SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
 13332  13416   SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
 13333  13417   
 13334         -#if defined(SQLITE_TEST) 
        13418  +#if defined(SQLITE_NEED_ERR_NAME)
 13335  13419   SQLITE_PRIVATE const char *sqlite3ErrName(int);
 13336  13420   #endif
 13337  13421   
 13338  13422   SQLITE_PRIVATE const char *sqlite3ErrStr(int);
 13339  13423   SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
 13340  13424   SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
 13341  13425   SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
................................................................................
 13421  13505     void (*)(sqlite3_context*,int,sqlite3_value **),
 13422  13506     void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
 13423  13507     FuncDestructor *pDestructor
 13424  13508   );
 13425  13509   SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
 13426  13510   SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
 13427  13511   
 13428         -SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
        13512  +SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
 13429  13513   SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
 13430  13514   SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*);
 13431  13515   SQLITE_PRIVATE void sqlite3AppendChar(StrAccum*,int,char);
 13432  13516   SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
 13433  13517   SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
 13434  13518   SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
 13435  13519   SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
................................................................................
 19805  19889   ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
 19806  19890   ** switch.  The following code should catch this problem at compile-time.
 19807  19891   */
 19808  19892   #ifdef MEMORY_DEBUG
 19809  19893   # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
 19810  19894   #endif
 19811  19895   
 19812         -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 19813         -# ifndef SQLITE_DEBUG_OS_TRACE
 19814         -#   define SQLITE_DEBUG_OS_TRACE 0
 19815         -# endif
 19816         -  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
 19817         -# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
 19818         -#else
 19819         -# define OSTRACE(X)
 19820         -#endif
 19821         -
 19822  19896   /*
 19823  19897   ** Macros for performance tracing.  Normally turned off.  Only works
 19824  19898   ** on i486 hardware.
 19825  19899   */
 19826  19900   #ifdef SQLITE_PERFORMANCE_TRACE
 19827  19901   
 19828  19902   /* 
................................................................................
 21399  21473   }
 21400  21474   #endif /* SQLITE_OMIT_FLOATING_POINT */
 21401  21475   
 21402  21476   /*
 21403  21477   ** Set the StrAccum object to an error mode.
 21404  21478   */
 21405  21479   static void setStrAccumError(StrAccum *p, u8 eError){
        21480  +  assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG );
 21406  21481     p->accError = eError;
 21407  21482     p->nAlloc = 0;
 21408  21483   }
 21409  21484   
 21410  21485   /*
 21411  21486   ** Extra argument values from a PrintfArguments object
 21412  21487   */
................................................................................
 21513  21588           case '#':   flag_alternateform = 1;   break;
 21514  21589           case '!':   flag_altform2 = 1;        break;
 21515  21590           case '0':   flag_zeropad = 1;         break;
 21516  21591           default:    done = 1;                 break;
 21517  21592         }
 21518  21593       }while( !done && (c=(*++fmt))!=0 );
 21519  21594       /* Get the field width */
 21520         -    width = 0;
 21521  21595       if( c=='*' ){
 21522  21596         if( bArgList ){
 21523  21597           width = (int)getIntArg(pArgList);
 21524  21598         }else{
 21525  21599           width = va_arg(ap,int);
 21526  21600         }
 21527  21601         if( width<0 ){
 21528  21602           flag_leftjustify = 1;
 21529         -        width = -width;
        21603  +        width = width >= -2147483647 ? -width : 0;
 21530  21604         }
 21531  21605         c = *++fmt;
 21532  21606       }else{
        21607  +      unsigned wx = 0;
 21533  21608         while( c>='0' && c<='9' ){
 21534         -        width = width*10 + c - '0';
        21609  +        wx = wx*10 + c - '0';
 21535  21610           c = *++fmt;
 21536  21611         }
        21612  +      testcase( wx>0x7fffffff );
        21613  +      width = wx & 0x7fffffff;
 21537  21614       }
        21615  +
 21538  21616       /* Get the precision */
 21539  21617       if( c=='.' ){
 21540         -      precision = 0;
 21541  21618         c = *++fmt;
 21542  21619         if( c=='*' ){
 21543  21620           if( bArgList ){
 21544  21621             precision = (int)getIntArg(pArgList);
 21545  21622           }else{
 21546  21623             precision = va_arg(ap,int);
 21547  21624           }
 21548         -        if( precision<0 ) precision = -precision;
 21549  21625           c = *++fmt;
        21626  +        if( precision<0 ){
        21627  +          precision = precision >= -2147483647 ? -precision : -1;
        21628  +        }
 21550  21629         }else{
        21630  +        unsigned px = 0;
 21551  21631           while( c>='0' && c<='9' ){
 21552         -          precision = precision*10 + c - '0';
        21632  +          px = px*10 + c - '0';
 21553  21633             c = *++fmt;
 21554  21634           }
        21635  +        testcase( px>0x7fffffff );
        21636  +        precision = px & 0x7fffffff;
 21555  21637         }
 21556  21638       }else{
 21557  21639         precision = -1;
 21558  21640       }
 21559  21641       /* Get the conversion type modifier */
 21560  21642       if( c=='l' ){
 21561  21643         flag_long = 1;
................................................................................
 21711  21793             prefix = '-';
 21712  21794           }else{
 21713  21795             if( flag_plussign )          prefix = '+';
 21714  21796             else if( flag_blanksign )    prefix = ' ';
 21715  21797             else                         prefix = 0;
 21716  21798           }
 21717  21799           if( xtype==etGENERIC && precision>0 ) precision--;
 21718         -        for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
        21800  +        testcase( precision>0xfff );
        21801  +        for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){}
 21719  21802           if( xtype==etFLOAT ) realvalue += rounder;
 21720  21803           /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
 21721  21804           exp = 0;
 21722  21805           if( sqlite3IsNaN((double)realvalue) ){
 21723  21806             bufpt = "NaN";
 21724  21807             length = 3;
 21725  21808             break;
................................................................................
 21766  21849             flag_rtz = flag_altform2;
 21767  21850           }
 21768  21851           if( xtype==etEXP ){
 21769  21852             e2 = 0;
 21770  21853           }else{
 21771  21854             e2 = exp;
 21772  21855           }
 21773         -        if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
 21774         -          bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
        21856  +        if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){
        21857  +          bufpt = zExtra 
        21858  +              = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 );
 21775  21859             if( bufpt==0 ){
 21776  21860               setStrAccumError(pAccum, STRACCUM_NOMEM);
 21777  21861               return;
 21778  21862             }
 21779  21863           }
 21780  21864           zOut = bufpt;
 21781  21865           nsd = 16 + flag_altform2*10;
................................................................................
 21999  22083   ** able to accept at least N more bytes of text.
 22000  22084   **
 22001  22085   ** Return the number of bytes of text that StrAccum is able to accept
 22002  22086   ** after the attempted enlargement.  The value returned might be zero.
 22003  22087   */
 22004  22088   static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
 22005  22089     char *zNew;
 22006         -  assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */
        22090  +  assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
 22007  22091     if( p->accError ){
 22008  22092       testcase(p->accError==STRACCUM_TOOBIG);
 22009  22093       testcase(p->accError==STRACCUM_NOMEM);
 22010  22094       return 0;
 22011  22095     }
 22012         -  if( !p->useMalloc ){
        22096  +  if( p->mxAlloc==0 ){
 22013  22097       N = p->nAlloc - p->nChar - 1;
 22014  22098       setStrAccumError(p, STRACCUM_TOOBIG);
 22015  22099       return N;
 22016  22100     }else{
 22017  22101       char *zOld = (p->zText==p->zBase ? 0 : p->zText);
 22018  22102       i64 szNew = p->nChar;
 22019  22103       szNew += N + 1;
................................................................................
 22025  22109       if( szNew > p->mxAlloc ){
 22026  22110         sqlite3StrAccumReset(p);
 22027  22111         setStrAccumError(p, STRACCUM_TOOBIG);
 22028  22112         return 0;
 22029  22113       }else{
 22030  22114         p->nAlloc = (int)szNew;
 22031  22115       }
 22032         -    if( p->useMalloc==1 ){
        22116  +    if( p->db ){
 22033  22117         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
 22034  22118       }else{
 22035         -      zNew = sqlite3_realloc(zOld, p->nAlloc);
        22119  +      zNew = sqlite3_realloc64(zOld, p->nAlloc);
 22036  22120       }
 22037  22121       if( zNew ){
 22038  22122         assert( p->zText!=0 || p->nChar==0 );
 22039  22123         if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
 22040  22124         p->zText = zNew;
 22041  22125         p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
 22042  22126       }else{
................................................................................
 22048  22132     return N;
 22049  22133   }
 22050  22134   
 22051  22135   /*
 22052  22136   ** Append N copies of character c to the given string buffer.
 22053  22137   */
 22054  22138   SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){
 22055         -  if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return;
        22139  +  testcase( p->nChar + (i64)N > 0x7fffffff );
        22140  +  if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
        22141  +    return;
        22142  +  }
 22056  22143     while( (N--)>0 ) p->zText[p->nChar++] = c;
 22057  22144   }
 22058  22145   
 22059  22146   /*
 22060  22147   ** The StrAccum "p" is not large enough to accept N new bytes of z[].
 22061  22148   ** So enlarge if first, then do the append.
 22062  22149   **
................................................................................
 22073  22160   }
 22074  22161   
 22075  22162   /*
 22076  22163   ** Append N bytes of text from z to the StrAccum object.  Increase the
 22077  22164   ** size of the memory allocation for StrAccum if necessary.
 22078  22165   */
 22079  22166   SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
 22080         -  assert( z!=0 );
        22167  +  assert( z!=0 || N==0 );
 22081  22168     assert( p->zText!=0 || p->nChar==0 || p->accError );
 22082  22169     assert( N>=0 );
 22083  22170     assert( p->accError==0 || p->nAlloc==0 );
 22084  22171     if( p->nChar+N >= p->nAlloc ){
 22085  22172       enlargeAndAppend(p,z,N);
 22086  22173     }else{
 22087  22174       assert( p->zText );
................................................................................
 22102  22189   ** Finish off a string by making sure it is zero-terminated.
 22103  22190   ** Return a pointer to the resulting string.  Return a NULL
 22104  22191   ** pointer if any kind of error was encountered.
 22105  22192   */
 22106  22193   SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
 22107  22194     if( p->zText ){
 22108  22195       p->zText[p->nChar] = 0;
 22109         -    if( p->useMalloc && p->zText==p->zBase ){
 22110         -      if( p->useMalloc==1 ){
 22111         -        p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
 22112         -      }else{
 22113         -        p->zText = sqlite3_malloc(p->nChar+1);
 22114         -      }
        22196  +    if( p->mxAlloc>0 && p->zText==p->zBase ){
        22197  +      p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
 22115  22198         if( p->zText ){
 22116  22199           memcpy(p->zText, p->zBase, p->nChar+1);
 22117  22200         }else{
 22118  22201           setStrAccumError(p, STRACCUM_NOMEM);
 22119  22202         }
 22120  22203       }
 22121  22204     }
................................................................................
 22123  22206   }
 22124  22207   
 22125  22208   /*
 22126  22209   ** Reset an StrAccum string.  Reclaim all malloced memory.
 22127  22210   */
 22128  22211   SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
 22129  22212     if( p->zText!=p->zBase ){
 22130         -    if( p->useMalloc==1 ){
 22131         -      sqlite3DbFree(p->db, p->zText);
 22132         -    }else{
 22133         -      sqlite3_free(p->zText);
 22134         -    }
        22213  +    sqlite3DbFree(p->db, p->zText);
 22135  22214     }
 22136  22215     p->zText = 0;
 22137  22216   }
 22138  22217   
 22139  22218   /*
 22140         -** Initialize a string accumulator
        22219  +** Initialize a string accumulator.
        22220  +**
        22221  +** p:     The accumulator to be initialized.
        22222  +** db:    Pointer to a database connection.  May be NULL.  Lookaside
        22223  +**        memory is used if not NULL. db->mallocFailed is set appropriately
        22224  +**        when not NULL.
        22225  +** zBase: An initial buffer.  May be NULL in which case the initial buffer
        22226  +**        is malloced.
        22227  +** n:     Size of zBase in bytes.  If total space requirements never exceed
        22228  +**        n then no memory allocations ever occur.
        22229  +** mx:    Maximum number of bytes to accumulate.  If mx==0 then no memory
        22230  +**        allocations will ever occur.
 22141  22231   */
 22142         -SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
        22232  +SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
 22143  22233     p->zText = p->zBase = zBase;
 22144         -  p->db = 0;
        22234  +  p->db = db;
 22145  22235     p->nChar = 0;
 22146  22236     p->nAlloc = n;
 22147  22237     p->mxAlloc = mx;
 22148         -  p->useMalloc = 1;
 22149  22238     p->accError = 0;
 22150  22239   }
 22151  22240   
 22152  22241   /*
 22153  22242   ** Print into memory obtained from sqliteMalloc().  Use the internal
 22154  22243   ** %-conversion extensions.
 22155  22244   */
 22156  22245   SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
 22157  22246     char *z;
 22158  22247     char zBase[SQLITE_PRINT_BUF_SIZE];
 22159  22248     StrAccum acc;
 22160  22249     assert( db!=0 );
 22161         -  sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
        22250  +  sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
 22162  22251                         db->aLimit[SQLITE_LIMIT_LENGTH]);
 22163         -  acc.db = db;
 22164  22252     sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
 22165  22253     z = sqlite3StrAccumFinish(&acc);
 22166  22254     if( acc.accError==STRACCUM_NOMEM ){
 22167  22255       db->mallocFailed = 1;
 22168  22256     }
 22169  22257     return z;
 22170  22258   }
................................................................................
 22214  22302       (void)SQLITE_MISUSE_BKPT;
 22215  22303       return 0;
 22216  22304     }
 22217  22305   #endif
 22218  22306   #ifndef SQLITE_OMIT_AUTOINIT
 22219  22307     if( sqlite3_initialize() ) return 0;
 22220  22308   #endif
 22221         -  sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
 22222         -  acc.useMalloc = 2;
        22309  +  sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
 22223  22310     sqlite3VXPrintf(&acc, 0, zFormat, ap);
 22224  22311     z = sqlite3StrAccumFinish(&acc);
 22225  22312     return z;
 22226  22313   }
 22227  22314   
 22228  22315   /*
 22229  22316   ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
................................................................................
 22260  22347   #ifdef SQLITE_ENABLE_API_ARMOR
 22261  22348     if( zBuf==0 || zFormat==0 ) {
 22262  22349       (void)SQLITE_MISUSE_BKPT;
 22263  22350       if( zBuf ) zBuf[0] = 0;
 22264  22351       return zBuf;
 22265  22352     }
 22266  22353   #endif
 22267         -  sqlite3StrAccumInit(&acc, zBuf, n, 0);
 22268         -  acc.useMalloc = 0;
        22354  +  sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
 22269  22355     sqlite3VXPrintf(&acc, 0, zFormat, ap);
 22270  22356     return sqlite3StrAccumFinish(&acc);
 22271  22357   }
 22272  22358   SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
 22273  22359     char *z;
 22274  22360     va_list ap;
 22275  22361     va_start(ap,zFormat);
................................................................................
 22287  22373   ** allocate memory because it might be called while the memory allocator
 22288  22374   ** mutex is held.
 22289  22375   */
 22290  22376   static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
 22291  22377     StrAccum acc;                          /* String accumulator */
 22292  22378     char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
 22293  22379   
 22294         -  sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
 22295         -  acc.useMalloc = 0;
        22380  +  sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
 22296  22381     sqlite3VXPrintf(&acc, 0, zFormat, ap);
 22297  22382     sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
 22298  22383                              sqlite3StrAccumFinish(&acc));
 22299  22384   }
 22300  22385   
 22301  22386   /*
 22302  22387   ** Format and write a message to the log if logging is enabled.
................................................................................
 22306  22391     if( sqlite3GlobalConfig.xLog ){
 22307  22392       va_start(ap, zFormat);
 22308  22393       renderLogMsg(iErrCode, zFormat, ap);
 22309  22394       va_end(ap);
 22310  22395     }
 22311  22396   }
 22312  22397   
 22313         -#if defined(SQLITE_DEBUG)
        22398  +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
 22314  22399   /*
 22315  22400   ** A version of printf() that understands %lld.  Used for debugging.
 22316  22401   ** The printf() built into some versions of windows does not understand %lld
 22317  22402   ** and segfaults if you give it a long long int.
 22318  22403   */
 22319  22404   SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
 22320  22405     va_list ap;
 22321  22406     StrAccum acc;
 22322  22407     char zBuf[500];
 22323         -  sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
 22324         -  acc.useMalloc = 0;
        22408  +  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
 22325  22409     va_start(ap,zFormat);
 22326  22410     sqlite3VXPrintf(&acc, 0, zFormat, ap);
 22327  22411     va_end(ap);
 22328  22412     sqlite3StrAccumFinish(&acc);
 22329  22413     fprintf(stdout,"%s", zBuf);
 22330  22414     fflush(stdout);
 22331  22415   }
................................................................................
 22344  22428   ** a diagram of Expr, ExprList, and Select objects.
 22345  22429   **
 22346  22430   */
 22347  22431   /* Add a new subitem to the tree.  The moreToFollow flag indicates that this
 22348  22432   ** is not the last item in the tree. */
 22349  22433   SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
 22350  22434     if( p==0 ){
 22351         -    p = sqlite3_malloc( sizeof(*p) );
        22435  +    p = sqlite3_malloc64( sizeof(*p) );
 22352  22436       if( p==0 ) return 0;
 22353  22437       memset(p, 0, sizeof(*p));
 22354  22438     }else{
 22355  22439       p->iLevel++;
 22356  22440     }
 22357  22441     assert( moreToFollow==0 || moreToFollow==1 );
 22358  22442     if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
................................................................................
 22367  22451   /* Generate a single line of output for the tree, with a prefix that contains
 22368  22452   ** all the appropriate tree lines */
 22369  22453   SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
 22370  22454     va_list ap;
 22371  22455     int i;
 22372  22456     StrAccum acc;
 22373  22457     char zBuf[500];
 22374         -  sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
 22375         -  acc.useMalloc = 0;
        22458  +  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
 22376  22459     if( p ){
 22377  22460       for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
 22378  22461         sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|   " : "    ", 4);
 22379  22462       }
 22380  22463       sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
 22381  22464     }
 22382  22465     va_start(ap, zFormat);
................................................................................
 23991  24074         memcpy(pValue, &u, 4);
 23992  24075         return 1;
 23993  24076       }else{
 23994  24077         return 0;
 23995  24078       }
 23996  24079     }
 23997  24080   #endif
        24081  +  while( zNum[0]=='0' ) zNum++;
 23998  24082     for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
 23999  24083       v = v*10 + c;
 24000  24084     }
 24001  24085   
 24002  24086     /* The longest decimal representation of a 32 bit integer is 10 digits:
 24003  24087     **
 24004  24088     **             1234567890
................................................................................
 25245  25329   #endif
 25246  25330   
 25247  25331   #if SQLITE_ENABLE_LOCKING_STYLE
 25248  25332   # include <sys/ioctl.h>
 25249  25333   # include <sys/file.h>
 25250  25334   # include <sys/param.h>
 25251  25335   #endif /* SQLITE_ENABLE_LOCKING_STYLE */
        25336  +
        25337  +#if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
        25338  +                           (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
        25339  +#  if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
        25340  +       && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))
        25341  +#    define HAVE_GETHOSTUUID 1
        25342  +#  else
        25343  +#    warning "gethostuuid() is disabled."
        25344  +#  endif
        25345  +#endif
        25346  +
 25252  25347   
 25253  25348   #if OS_VXWORKS
 25254  25349   /* # include <sys/ioctl.h> */
 25255  25350   # include <semaphore.h>
 25256  25351   # include <limits.h>
 25257  25352   #endif /* OS_VXWORKS */
 25258  25353   
................................................................................
 25441  25536   ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
 25442  25537   ** switch.  The following code should catch this problem at compile-time.
 25443  25538   */
 25444  25539   #ifdef MEMORY_DEBUG
 25445  25540   # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
 25446  25541   #endif
 25447  25542   
 25448         -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 25449         -# ifndef SQLITE_DEBUG_OS_TRACE
 25450         -#   define SQLITE_DEBUG_OS_TRACE 0
 25451         -# endif
 25452         -  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
 25453         -# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
 25454         -#else
 25455         -# define OSTRACE(X)
 25456         -#endif
 25457         -
 25458  25543   /*
 25459  25544   ** Macros for performance tracing.  Normally turned off.  Only works
 25460  25545   ** on i486 hardware.
 25461  25546   */
 25462  25547   #ifdef SQLITE_PERFORMANCE_TRACE
 25463  25548   
 25464  25549   /* 
................................................................................
 25993  26078   #ifdef SQLITE_DEBUG
 25994  26079   static int unixMutexHeld(void) {
 25995  26080     return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 25996  26081   }
 25997  26082   #endif
 25998  26083   
 25999  26084   
 26000         -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
        26085  +#ifdef SQLITE_HAVE_OS_TRACE
 26001  26086   /*
 26002  26087   ** Helper function for printing out trace information from debugging
 26003  26088   ** binaries. This returns the string representation of the supplied
 26004  26089   ** integer lock-type.
 26005  26090   */
 26006  26091   static const char *azFileLock(int eFileLock){
 26007  26092     switch( eFileLock ){
................................................................................
 26256  26341   static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
 26257  26342     struct vxworksFileId *pNew;         /* search key and new file ID */
 26258  26343     struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
 26259  26344     int n;                              /* Length of zAbsoluteName string */
 26260  26345   
 26261  26346     assert( zAbsoluteName[0]=='/' );
 26262  26347     n = (int)strlen(zAbsoluteName);
 26263         -  pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
        26348  +  pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
 26264  26349     if( pNew==0 ) return 0;
 26265  26350     pNew->zCanonicalName = (char*)&pNew[1];
 26266  26351     memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
 26267  26352     n = vxworksSimplifyName(pNew->zCanonicalName, n);
 26268  26353   
 26269  26354     /* Search for an existing entry that matching the canonical name.
 26270  26355     ** If found, increment the reference count and return a pointer to
................................................................................
 26660  26745     fileId.ino = statbuf.st_ino;
 26661  26746   #endif
 26662  26747     pInode = inodeList;
 26663  26748     while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
 26664  26749       pInode = pInode->pNext;
 26665  26750     }
 26666  26751     if( pInode==0 ){
 26667         -    pInode = sqlite3_malloc( sizeof(*pInode) );
        26752  +    pInode = sqlite3_malloc64( sizeof(*pInode) );
 26668  26753       if( pInode==0 ){
 26669  26754         return SQLITE_NOMEM;
 26670  26755       }
 26671  26756       memset(pInode, 0, sizeof(*pInode));
 26672  26757       memcpy(&pInode->fileId, &fileId, sizeof(fileId));
 26673  26758       pInode->nRef = 1;
 26674  26759       pInode->pNext = inodeList;
................................................................................
 29181  29266         return SQLITE_OK;
 29182  29267       }
 29183  29268       case SQLITE_FCNTL_VFSNAME: {
 29184  29269         *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
 29185  29270         return SQLITE_OK;
 29186  29271       }
 29187  29272       case SQLITE_FCNTL_TEMPFILENAME: {
 29188         -      char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
        29273  +      char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
 29189  29274         if( zTFile ){
 29190  29275           unixGetTempname(pFile->pVfs->mxPathname, zTFile);
 29191  29276           *(char**)pArg = zTFile;
 29192  29277         }
 29193  29278         return SQLITE_OK;
 29194  29279       }
 29195  29280       case SQLITE_FCNTL_HAS_MOVED: {
................................................................................
 29622  29707     struct unixShmNode *pShmNode;   /* The underlying mmapped file */
 29623  29708     int rc;                         /* Result code */
 29624  29709     unixInodeInfo *pInode;          /* The inode of fd */
 29625  29710     char *zShmFilename;             /* Name of the file used for SHM */
 29626  29711     int nShmFilename;               /* Size of the SHM filename in bytes */
 29627  29712   
 29628  29713     /* Allocate space for the new unixShm object. */
 29629         -  p = sqlite3_malloc( sizeof(*p) );
        29714  +  p = sqlite3_malloc64( sizeof(*p) );
 29630  29715     if( p==0 ) return SQLITE_NOMEM;
 29631  29716     memset(p, 0, sizeof(*p));
 29632  29717     assert( pDbFd->pShm==0 );
 29633  29718   
 29634  29719     /* Check to see if a unixShmNode object already exists. Reuse an existing
 29635  29720     ** one if present. Create a new one if necessary.
 29636  29721     */
................................................................................
 29653  29738       }
 29654  29739   
 29655  29740   #ifdef SQLITE_SHM_DIRECTORY
 29656  29741       nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
 29657  29742   #else
 29658  29743       nShmFilename = 6 + (int)strlen(zBasePath);
 29659  29744   #endif
 29660         -    pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
        29745  +    pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
 29661  29746       if( pShmNode==0 ){
 29662  29747         rc = SQLITE_NOMEM;
 29663  29748         goto shm_open_err;
 29664  29749       }
 29665  29750       memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
 29666  29751       zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
 29667  29752   #ifdef SQLITE_SHM_DIRECTORY
................................................................................
 29863  29948               MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
 29864  29949           );
 29865  29950           if( pMem==MAP_FAILED ){
 29866  29951             rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
 29867  29952             goto shmpage_out;
 29868  29953           }
 29869  29954         }else{
 29870         -        pMem = sqlite3_malloc(szRegion);
        29955  +        pMem = sqlite3_malloc64(szRegion);
 29871  29956           if( pMem==0 ){
 29872  29957             rc = SQLITE_NOMEM;
 29873  29958             goto shmpage_out;
 29874  29959           }
 29875  29960           memset(pMem, 0, szRegion);
 29876  29961         }
 29877  29962   
................................................................................
 30700  30785   
 30701  30786   #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 30702  30787     else if( pLockingStyle == &afpIoMethods ){
 30703  30788       /* AFP locking uses the file path so it needs to be included in
 30704  30789       ** the afpLockingContext.
 30705  30790       */
 30706  30791       afpLockingContext *pCtx;
 30707         -    pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
        30792  +    pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
 30708  30793       if( pCtx==0 ){
 30709  30794         rc = SQLITE_NOMEM;
 30710  30795       }else{
 30711  30796         /* NB: zFilename exists and remains valid until the file is closed
 30712  30797         ** according to requirement F11141.  So we do not need to make a
 30713  30798         ** copy of the filename. */
 30714  30799         pCtx->dbPath = zFilename;
................................................................................
 30730  30815       /* Dotfile locking uses the file path so it needs to be included in
 30731  30816       ** the dotlockLockingContext 
 30732  30817       */
 30733  30818       char *zLockFile;
 30734  30819       int nFilename;
 30735  30820       assert( zFilename!=0 );
 30736  30821       nFilename = (int)strlen(zFilename) + 6;
 30737         -    zLockFile = (char *)sqlite3_malloc(nFilename);
        30822  +    zLockFile = (char *)sqlite3_malloc64(nFilename);
 30738  30823       if( zLockFile==0 ){
 30739  30824         rc = SQLITE_NOMEM;
 30740  30825       }else{
 30741  30826         sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
 30742  30827       }
 30743  30828       pNew->lockingContext = zLockFile;
 30744  30829     }
................................................................................
 31107  31192   
 31108  31193     if( eType==SQLITE_OPEN_MAIN_DB ){
 31109  31194       UnixUnusedFd *pUnused;
 31110  31195       pUnused = findReusableFd(zName, flags);
 31111  31196       if( pUnused ){
 31112  31197         fd = pUnused->fd;
 31113  31198       }else{
 31114         -      pUnused = sqlite3_malloc(sizeof(*pUnused));
        31199  +      pUnused = sqlite3_malloc64(sizeof(*pUnused));
 31115  31200         if( !pUnused ){
 31116  31201           return SQLITE_NOMEM;
 31117  31202         }
 31118  31203       }
 31119  31204       p->pUnused = pUnused;
 31120  31205   
 31121  31206       /* Database filenames are double-zero terminated if they are not
................................................................................
 31487  31572     **
 31488  31573     ** When testing, initializing zBuf[] to zero is all we do.  That means
 31489  31574     ** that we always use the same random number sequence.  This makes the
 31490  31575     ** tests repeatable.
 31491  31576     */
 31492  31577     memset(zBuf, 0, nBuf);
 31493  31578     randomnessPid = osGetpid(0);  
 31494         -#if !defined(SQLITE_TEST)
        31579  +#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
 31495  31580     {
 31496  31581       int fd, got;
 31497  31582       fd = robust_open("/dev/urandom", O_RDONLY, 0);
 31498  31583       if( fd<0 ){
 31499  31584         time_t t;
 31500  31585         time(&t);
 31501  31586         memcpy(zBuf, &t, sizeof(t));
................................................................................
 31899  31984     ** 3. if that fails, try to open the file read-only
 31900  31985     ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
 31901  31986     */
 31902  31987     pUnused = findReusableFd(path, openFlags);
 31903  31988     if( pUnused ){
 31904  31989       fd = pUnused->fd;
 31905  31990     }else{
 31906         -    pUnused = sqlite3_malloc(sizeof(*pUnused));
        31991  +    pUnused = sqlite3_malloc64(sizeof(*pUnused));
 31907  31992       if( !pUnused ){
 31908  31993         return SQLITE_NOMEM;
 31909  31994       }
 31910  31995     }
 31911  31996     if( fd<0 ){
 31912  31997       fd = robust_open(path, openFlags, 0);
 31913  31998       terrno = errno;
................................................................................
 31932  32017         case EIO: 
 31933  32018           return SQLITE_IOERR_LOCK; /* even though it is the conch */
 31934  32019         default:
 31935  32020           return SQLITE_CANTOPEN_BKPT;
 31936  32021       }
 31937  32022     }
 31938  32023     
 31939         -  pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
        32024  +  pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
 31940  32025     if( pNew==NULL ){
 31941  32026       rc = SQLITE_NOMEM;
 31942  32027       goto end_create_proxy;
 31943  32028     }
 31944  32029     memset(pNew, 0, sizeof(unixFile));
 31945  32030     pNew->openFlags = openFlags;
 31946  32031     memset(&dummyVfs, 0, sizeof(dummyVfs));
................................................................................
 31965  32050   #ifdef SQLITE_TEST
 31966  32051   /* simulate multiple hosts by creating unique hostid file paths */
 31967  32052   SQLITE_API int sqlite3_hostid_num = 0;
 31968  32053   #endif
 31969  32054   
 31970  32055   #define PROXY_HOSTIDLEN    16  /* conch file host id length */
 31971  32056   
        32057  +#ifdef HAVE_GETHOSTUUID
 31972  32058   /* Not always defined in the headers as it ought to be */
 31973  32059   extern int gethostuuid(uuid_t id, const struct timespec *wait);
        32060  +#endif
 31974  32061   
 31975  32062   /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN 
 31976  32063   ** bytes of writable memory.
 31977  32064   */
 31978  32065   static int proxyGetHostID(unsigned char *pHostID, int *pError){
 31979  32066     assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
 31980  32067     memset(pHostID, 0, PROXY_HOSTIDLEN);
 31981         -# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
 31982         -                            (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
        32068  +#ifdef HAVE_GETHOSTUUID
 31983  32069     {
 31984  32070       struct timespec timeout = {1, 0}; /* 1 sec timeout */
 31985  32071       if( gethostuuid(pHostID, &timeout) ){
 31986  32072         int err = errno;
 31987  32073         if( pError ){
 31988  32074           *pError = err;
 31989  32075         }
................................................................................
 32393  32479     OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
 32394  32480              (rc==SQLITE_OK ? "ok" : "failed")));
 32395  32481     return rc;
 32396  32482   }
 32397  32483   
 32398  32484   /*
 32399  32485   ** Given the name of a database file, compute the name of its conch file.
 32400         -** Store the conch filename in memory obtained from sqlite3_malloc().
        32486  +** Store the conch filename in memory obtained from sqlite3_malloc64().
 32401  32487   ** Make *pConchPath point to the new name.  Return SQLITE_OK on success
 32402  32488   ** or SQLITE_NOMEM if unable to obtain memory.
 32403  32489   **
 32404  32490   ** The caller is responsible for ensuring that the allocated memory
 32405  32491   ** space is eventually freed.
 32406  32492   **
 32407  32493   ** *pConchPath is set to NULL if a memory allocation error occurs.
................................................................................
 32409  32495   static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
 32410  32496     int i;                        /* Loop counter */
 32411  32497     int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
 32412  32498     char *conchPath;              /* buffer in which to construct conch name */
 32413  32499   
 32414  32500     /* Allocate space for the conch filename and initialize the name to
 32415  32501     ** the name of the original database file. */  
 32416         -  *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
        32502  +  *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
 32417  32503     if( conchPath==0 ){
 32418  32504       return SQLITE_NOMEM;
 32419  32505     }
 32420  32506     memcpy(conchPath, dbPath, len+1);
 32421  32507     
 32422  32508     /* now insert a "." before the last / character */
 32423  32509     for( i=(len-1); i>=0; i-- ){
................................................................................
 32525  32611     }else{
 32526  32612       lockPath=(char *)path;
 32527  32613     }
 32528  32614     
 32529  32615     OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
 32530  32616              (lockPath ? lockPath : ":auto:"), osGetpid(0)));
 32531  32617   
 32532         -  pCtx = sqlite3_malloc( sizeof(*pCtx) );
        32618  +  pCtx = sqlite3_malloc64( sizeof(*pCtx) );
 32533  32619     if( pCtx==0 ){
 32534  32620       return SQLITE_NOMEM;
 32535  32621     }
 32536  32622     memset(pCtx, 0, sizeof(*pCtx));
 32537  32623   
 32538  32624     rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
 32539  32625     if( rc==SQLITE_OK ){
................................................................................
 32969  33055   ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
 32970  33056   ** switch.  The following code should catch this problem at compile-time.
 32971  33057   */
 32972  33058   #ifdef MEMORY_DEBUG
 32973  33059   # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
 32974  33060   #endif
 32975  33061   
 32976         -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 32977         -# ifndef SQLITE_DEBUG_OS_TRACE
 32978         -#   define SQLITE_DEBUG_OS_TRACE 0
 32979         -# endif
 32980         -  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
 32981         -# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
 32982         -#else
 32983         -# define OSTRACE(X)
 32984         -#endif
 32985         -
 32986  33062   /*
 32987  33063   ** Macros for performance tracing.  Normally turned off.  Only works
 32988  33064   ** on i486 hardware.
 32989  33065   */
 32990  33066   #ifdef SQLITE_PERFORMANCE_TRACE
 32991  33067   
 32992  33068   /* 
................................................................................
 35882  35958   #ifndef SQLITE_NO_SYNC
 35883  35959     /*
 35884  35960     ** Used only when SQLITE_NO_SYNC is not defined.
 35885  35961      */
 35886  35962     BOOL rc;
 35887  35963   #endif
 35888  35964   #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
 35889         -    (defined(SQLITE_TEST) && defined(SQLITE_DEBUG))
        35965  +    defined(SQLITE_HAVE_OS_TRACE)
 35890  35966     /*
 35891  35967     ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
 35892  35968     ** OSTRACE() macros.
 35893  35969      */
 35894  35970     winFile *pFile = (winFile*)id;
 35895  35971   #else
 35896  35972     UNUSED_PARAMETER(id);
................................................................................
 36559  36635       void *pMap;
 36560  36636     } *aRegion;
 36561  36637     DWORD lastErrno;           /* The Windows errno from the last I/O error */
 36562  36638   
 36563  36639     int nRef;                  /* Number of winShm objects pointing to this */
 36564  36640     winShm *pFirst;            /* All winShm objects pointing to this */
 36565  36641     winShmNode *pNext;         /* Next in list of all winShmNode objects */
 36566         -#ifdef SQLITE_DEBUG
        36642  +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
 36567  36643     u8 nextShmId;              /* Next available winShm.id value */
 36568  36644   #endif
 36569  36645   };
 36570  36646   
 36571  36647   /*
 36572  36648   ** A global array of all winShmNode objects.
 36573  36649   **
................................................................................
 36590  36666   */
 36591  36667   struct winShm {
 36592  36668     winShmNode *pShmNode;      /* The underlying winShmNode object */
 36593  36669     winShm *pNext;             /* Next winShm with the same winShmNode */
 36594  36670     u8 hasMutex;               /* True if holding the winShmNode mutex */
 36595  36671     u16 sharedMask;            /* Mask of shared locks held */
 36596  36672     u16 exclMask;              /* Mask of exclusive locks held */
 36597         -#ifdef SQLITE_DEBUG
        36673  +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
 36598  36674     u8 id;                     /* Id of this connection with its winShmNode */
 36599  36675   #endif
 36600  36676   };
 36601  36677   
 36602  36678   /*
 36603  36679   ** Constants used for locking
 36604  36680   */
................................................................................
 36781  36857         rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
 36782  36858       }
 36783  36859       if( rc ) goto shm_open_err;
 36784  36860     }
 36785  36861   
 36786  36862     /* Make the new connection a child of the winShmNode */
 36787  36863     p->pShmNode = pShmNode;
 36788         -#ifdef SQLITE_DEBUG
        36864  +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
 36789  36865     p->id = pShmNode->nextShmId++;
 36790  36866   #endif
 36791  36867     pShmNode->nRef++;
 36792  36868     pDbFd->pShm = p;
 36793  36869     winShmLeaveMutex();
 36794  36870   
 36795  36871     /* The reference count on pShmNode has already been incremented under
................................................................................
 37050  37126           rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
 37051  37127                            "winShmMap2", pDbFd->zPath);
 37052  37128           goto shmpage_out;
 37053  37129         }
 37054  37130       }
 37055  37131   
 37056  37132       /* Map the requested memory region into this processes address space. */
 37057         -    apNew = (struct ShmRegion *)sqlite3_realloc(
        37133  +    apNew = (struct ShmRegion *)sqlite3_realloc64(
 37058  37134           pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
 37059  37135       );
 37060  37136       if( !apNew ){
 37061  37137         rc = SQLITE_IOERR_NOMEM;
 37062  37138         goto shmpage_out;
 37063  37139       }
 37064  37140       pShmNode->aRegion = apNew;
................................................................................
 38497  38573   
 38498  38574   /*
 38499  38575   ** Write up to nBuf bytes of randomness into zBuf.
 38500  38576   */
 38501  38577   static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
 38502  38578     int n = 0;
 38503  38579     UNUSED_PARAMETER(pVfs);
 38504         -#if defined(SQLITE_TEST)
        38580  +#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
 38505  38581     n = nBuf;
 38506  38582     memset(zBuf, 0, nBuf);
 38507  38583   #else
 38508  38584     if( sizeof(SYSTEMTIME)<=nBuf-n ){
 38509  38585       SYSTEMTIME x;
 38510  38586       osGetSystemTime(&x);
 38511  38587       memcpy(&zBuf[n], &x, sizeof(x));
................................................................................
 38531  38607   #endif
 38532  38608     if( sizeof(LARGE_INTEGER)<=nBuf-n ){
 38533  38609       LARGE_INTEGER i;
 38534  38610       osQueryPerformanceCounter(&i);
 38535  38611       memcpy(&zBuf[n], &i, sizeof(i));
 38536  38612       n += sizeof(i);
 38537  38613     }
 38538         -#endif
 38539  38614   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
 38540  38615     if( sizeof(UUID)<=nBuf-n ){
 38541  38616       UUID id;
 38542  38617       memset(&id, 0, sizeof(UUID));
 38543  38618       osUuidCreate(&id);
 38544  38619       memcpy(zBuf, &id, sizeof(UUID));
 38545  38620       n += sizeof(UUID);
................................................................................
 38548  38623       UUID id;
 38549  38624       memset(&id, 0, sizeof(UUID));
 38550  38625       osUuidCreateSequential(&id);
 38551  38626       memcpy(zBuf, &id, sizeof(UUID));
 38552  38627       n += sizeof(UUID);
 38553  38628     }
 38554  38629   #endif
        38630  +#endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */
 38555  38631     return n;
 38556  38632   }
 38557  38633   
 38558  38634   
 38559  38635   /*
 38560  38636   ** Sleep for a little while.  Return the amount of time slept.
 38561  38637   */
................................................................................
 39102  39178     int i, nx, pc, op;
 39103  39179     void *pTmpSpace;
 39104  39180   
 39105  39181     /* Allocate the Bitvec to be tested and a linear array of
 39106  39182     ** bits to act as the reference */
 39107  39183     pBitvec = sqlite3BitvecCreate( sz );
 39108  39184     pV = sqlite3MallocZero( (sz+7)/8 + 1 );
 39109         -  pTmpSpace = sqlite3_malloc(BITVEC_SZ);
        39185  +  pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
 39110  39186     if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
 39111  39187   
 39112  39188     /* NULL pBitvec tests */
 39113  39189     sqlite3BitvecSet(0, 1);
 39114  39190     sqlite3BitvecClear(0, 1, pTmpSpace);
 39115  39191   
 39116  39192     /* Run the program */
................................................................................
 44591  44667     Pager *pPager,                  /* Pager object */
 44592  44668     PgHdr *pList,                   /* List of frames to log */
 44593  44669     Pgno nTruncate,                 /* Database size after this commit */
 44594  44670     int isCommit                    /* True if this is a commit */
 44595  44671   ){
 44596  44672     int rc;                         /* Return code */
 44597  44673     int nList;                      /* Number of pages in pList */
 44598         -#if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
 44599  44674     PgHdr *p;                       /* For looping over pages */
 44600         -#endif
 44601  44675   
 44602  44676     assert( pPager->pWal );
 44603  44677     assert( pList );
 44604  44678   #ifdef SQLITE_DEBUG
 44605  44679     /* Verify that the page list is in accending order */
 44606  44680     for(p=pList; p && p->pDirty; p=p->pDirty){
 44607  44681       assert( p->pgno < p->pDirty->pgno );
................................................................................
 44610  44684   
 44611  44685     assert( pList->pDirty==0 || isCommit );
 44612  44686     if( isCommit ){
 44613  44687       /* If a WAL transaction is being committed, there is no point in writing
 44614  44688       ** any pages with page numbers greater than nTruncate into the WAL file.
 44615  44689       ** They will never be read by any client. So remove them from the pDirty
 44616  44690       ** list here. */
 44617         -    PgHdr *p;
 44618  44691       PgHdr **ppNext = &pList;
 44619  44692       nList = 0;
 44620  44693       for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
 44621  44694         if( p->pgno<=nTruncate ){
 44622  44695           ppNext = &p->pDirty;
 44623  44696           nList++;
 44624  44697         }
................................................................................
 44630  44703     pPager->aStat[PAGER_STAT_WRITE] += nList;
 44631  44704   
 44632  44705     if( pList->pgno==1 ) pager_write_changecounter(pList);
 44633  44706     rc = sqlite3WalFrames(pPager->pWal, 
 44634  44707         pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
 44635  44708     );
 44636  44709     if( rc==SQLITE_OK && pPager->pBackup ){
 44637         -    PgHdr *p;
 44638  44710       for(p=pList; p; p=p->pDirty){
 44639  44711         sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
 44640  44712       }
 44641  44713     }
 44642  44714   
 44643  44715   #ifdef SQLITE_CHECK_PAGES
 44644  44716     pList = sqlite3PcacheDirtyList(pPager->pPCache);
................................................................................
 48561  48633           if( rc==SQLITE_OK && state==PAGER_READER ){
 48562  48634             pagerUnlockDb(pPager, SHARED_LOCK);
 48563  48635           }else if( state==PAGER_OPEN ){
 48564  48636             pager_unlock(pPager);
 48565  48637           }
 48566  48638           assert( state==pPager->eState );
 48567  48639         }
        48640  +    }else if( eMode==PAGER_JOURNALMODE_OFF ){
        48641  +      sqlite3OsClose(pPager->jfd);
 48568  48642       }
 48569  48643     }
 48570  48644   
 48571  48645     /* Return the new journal mode */
 48572  48646     return (int)pPager->journalMode;
 48573  48647   }
 48574  48648   
................................................................................
 49343  49417   static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
 49344  49418     int rc = SQLITE_OK;
 49345  49419   
 49346  49420     /* Enlarge the pWal->apWiData[] array if required */
 49347  49421     if( pWal->nWiData<=iPage ){
 49348  49422       int nByte = sizeof(u32*)*(iPage+1);
 49349  49423       volatile u32 **apNew;
 49350         -    apNew = (volatile u32 **)sqlite3_realloc((void *)pWal->apWiData, nByte);
        49424  +    apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
 49351  49425       if( !apNew ){
 49352  49426         *ppPage = 0;
 49353  49427         return SQLITE_NOMEM;
 49354  49428       }
 49355  49429       memset((void*)&apNew[pWal->nWiData], 0,
 49356  49430              sizeof(u32*)*(iPage+1-pWal->nWiData));
 49357  49431       pWal->apWiData = apNew;
................................................................................
 49968  50042       if( version!=WAL_MAX_VERSION ){
 49969  50043         rc = SQLITE_CANTOPEN_BKPT;
 49970  50044         goto finished;
 49971  50045       }
 49972  50046   
 49973  50047       /* Malloc a buffer to read frames into. */
 49974  50048       szFrame = szPage + WAL_FRAME_HDRSIZE;
 49975         -    aFrame = (u8 *)sqlite3_malloc(szFrame);
        50049  +    aFrame = (u8 *)sqlite3_malloc64(szFrame);
 49976  50050       if( !aFrame ){
 49977  50051         rc = SQLITE_NOMEM;
 49978  50052         goto recovery_error;
 49979  50053       }
 49980  50054       aData = &aFrame[WAL_FRAME_HDRSIZE];
 49981  50055   
 49982  50056       /* Read all frames from the log file. */
................................................................................
 50361  50435     iLast = pWal->hdr.mxFrame;
 50362  50436   
 50363  50437     /* Allocate space for the WalIterator object. */
 50364  50438     nSegment = walFramePage(iLast) + 1;
 50365  50439     nByte = sizeof(WalIterator) 
 50366  50440           + (nSegment-1)*sizeof(struct WalSegment)
 50367  50441           + iLast*sizeof(ht_slot);
 50368         -  p = (WalIterator *)sqlite3_malloc(nByte);
        50442  +  p = (WalIterator *)sqlite3_malloc64(nByte);
 50369  50443     if( !p ){
 50370  50444       return SQLITE_NOMEM;
 50371  50445     }
 50372  50446     memset(p, 0, nByte);
 50373  50447     p->nSegment = nSegment;
 50374  50448   
 50375  50449     /* Allocate temporary space used by the merge-sort routine. This block
 50376  50450     ** of memory will be freed before this function returns.
 50377  50451     */
 50378         -  aTmp = (ht_slot *)sqlite3_malloc(
        50452  +  aTmp = (ht_slot *)sqlite3_malloc64(
 50379  50453         sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
 50380  50454     );
 50381  50455     if( !aTmp ){
 50382  50456       rc = SQLITE_NOMEM;
 50383  50457     }
 50384  50458   
 50385  50459     for(i=0; rc==SQLITE_OK && i<nSegment; i++){
................................................................................
 50551  50625       ** safe to write into the database.  Frames beyond mxSafeFrame might
 50552  50626       ** overwrite database pages that are in use by active readers and thus
 50553  50627       ** cannot be backfilled from the WAL.
 50554  50628       */
 50555  50629       mxSafeFrame = pWal->hdr.mxFrame;
 50556  50630       mxPage = pWal->hdr.nPage;
 50557  50631       for(i=1; i<WAL_NREADER; i++){
        50632  +      /* Thread-sanitizer reports that the following is an unsafe read,
        50633  +      ** as some other thread may be in the process of updating the value
        50634  +      ** of the aReadMark[] slot. The assumption here is that if that is
        50635  +      ** happening, the other client may only be increasing the value,
        50636  +      ** not decreasing it. So assuming either that either the "old" or
        50637  +      ** "new" version of the value is read, and not some arbitrary value
        50638  +      ** that would never be written by a real client, things are still 
        50639  +      ** safe.  */
 50558  50640         u32 y = pInfo->aReadMark[i];
 50559  50641         if( mxSafeFrame>y ){
 50560  50642           assert( y<=pWal->hdr.mxFrame );
 50561  50643           rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
 50562  50644           if( rc==SQLITE_OK ){
 50563  50645             pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
 50564  50646             walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
................................................................................
 55391  55473     if( nReserve<0 ){
 55392  55474       nReserve = pBt->pageSize - pBt->usableSize;
 55393  55475     }
 55394  55476     assert( nReserve>=0 && nReserve<=255 );
 55395  55477     if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
 55396  55478           ((pageSize-1)&pageSize)==0 ){
 55397  55479       assert( (pageSize & 7)==0 );
 55398         -    assert( !pBt->pPage1 && !pBt->pCursor );
        55480  +    assert( !pBt->pCursor );
 55399  55481       pBt->pageSize = (u32)pageSize;
 55400  55482       freeTempSpace(pBt);
 55401  55483     }
 55402  55484     rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
 55403  55485     pBt->usableSize = pBt->pageSize - (u16)nReserve;
 55404  55486     if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
 55405  55487     sqlite3BtreeLeave(p);
................................................................................
 57413  57495   ** page of the database.  The data might change or move the next time
 57414  57496   ** any btree routine is called.
 57415  57497   */
 57416  57498   static const void *fetchPayload(
 57417  57499     BtCursor *pCur,      /* Cursor pointing to entry to read from */
 57418  57500     u32 *pAmt            /* Write the number of available bytes here */
 57419  57501   ){
        57502  +  u32 amt;
 57420  57503     assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
 57421  57504     assert( pCur->eState==CURSOR_VALID );
 57422  57505     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 57423  57506     assert( cursorHoldsMutex(pCur) );
 57424  57507     assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 57425  57508     assert( pCur->info.nSize>0 );
 57426         -  *pAmt = pCur->info.nLocal;
        57509  +  assert( pCur->info.pPayload>pCur->apPage[pCur->iPage]->aData || CORRUPT_DB );
        57510  +  assert( pCur->info.pPayload<pCur->apPage[pCur->iPage]->aDataEnd ||CORRUPT_DB);
        57511  +  amt = (int)(pCur->apPage[pCur->iPage]->aDataEnd - pCur->info.pPayload);
        57512  +  if( pCur->info.nLocal<amt ) amt = pCur->info.nLocal;
        57513  +  *pAmt = amt;
 57427  57514     return (void*)pCur->info.pPayload;
 57428  57515   }
 57429  57516   
 57430  57517   
 57431  57518   /*
 57432  57519   ** For the entry that cursor pCur is point to, return as
 57433  57520   ** many bytes of the key or data as are available on the local
................................................................................
 59697  59784     }else{
 59698  59785       assert( bBulk==0 || bBulk==1 );
 59699  59786       if( iParentIdx==0 ){                 
 59700  59787         nxDiv = 0;
 59701  59788       }else if( iParentIdx==i ){
 59702  59789         nxDiv = i-2+bBulk;
 59703  59790       }else{
 59704         -      assert( bBulk==0 );
 59705  59791         nxDiv = iParentIdx-1;
 59706  59792       }
 59707  59793       i = 2-bBulk;
 59708  59794     }
 59709  59795     nOld = i+1;
 59710  59796     if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
 59711  59797       pRight = &pParent->aData[pParent->hdrOffset+8];
................................................................................
 61485  61571       }
 61486  61572   #endif
 61487  61573       iPage = get4byte(pOvflData);
 61488  61574       sqlite3PagerUnref(pOvflPage);
 61489  61575     }
 61490  61576   }
 61491  61577   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
        61578  +
        61579  +/*
        61580  +** An implementation of a min-heap.
        61581  +**
        61582  +** aHeap[0] is the number of elements on the heap.  aHeap[1] is the
        61583  +** root element.  The daughter nodes of aHeap[N] are aHeap[N*2]
        61584  +** and aHeap[N*2+1].
        61585  +**
        61586  +** The heap property is this:  Every node is less than or equal to both
        61587  +** of its daughter nodes.  A consequence of the heap property is that the
        61588  +** root node aHeap[1] is always the minimum value currently in the heap.
        61589  +**
        61590  +** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
        61591  +** the heap, preserving the heap property.  The btreeHeapPull() routine
        61592  +** removes the root element from the heap (the minimum value in the heap)
        61593  +** and then moves other nodes around as necessary to preserve the heap
        61594  +** property.
        61595  +**
        61596  +** This heap is used for cell overlap and coverage testing.  Each u32
        61597  +** entry represents the span of a cell or freeblock on a btree page.  
        61598  +** The upper 16 bits are the index of the first byte of a range and the
        61599  +** lower 16 bits are the index of the last byte of that range.
        61600  +*/
        61601  +static void btreeHeapInsert(u32 *aHeap, u32 x){
        61602  +  u32 j, i = ++aHeap[0];
        61603  +  aHeap[i] = x;
        61604  +  while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
        61605  +    x = aHeap[j];
        61606  +    aHeap[j] = aHeap[i];
        61607  +    aHeap[i] = x;
        61608  +    i = j;
        61609  +  }
        61610  +}
        61611  +static int btreeHeapPull(u32 *aHeap, u32 *pOut){
        61612  +  u32 j, i, x;
        61613  +  if( (x = aHeap[0])==0 ) return 0;
        61614  +  *pOut = aHeap[1];
        61615  +  aHeap[1] = aHeap[x];
        61616  +  aHeap[x] = 0xffffffff;
        61617  +  aHeap[0]--;
        61618  +  i = 1;
        61619  +  while( (j = i*2)<=aHeap[0] ){
        61620  +    if( aHeap[j]>aHeap[j+1] ) j++;
        61621  +    if( aHeap[i]<aHeap[j] ) break;
        61622  +    x = aHeap[i];
        61623  +    aHeap[i] = aHeap[j];
        61624  +    aHeap[j] = x;
        61625  +    i = j;
        61626  +  }
        61627  +  return 1;  
        61628  +}
 61492  61629   
 61493  61630   #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 61494  61631   /*
 61495  61632   ** Do various sanity checks on a single page of a tree.  Return
 61496  61633   ** the tree depth.  Root pages return 0.  Parents of root pages
 61497  61634   ** return 1, and so forth.
 61498  61635   ** 
................................................................................
 61518  61655     MemPage *pPage;
 61519  61656     int i, rc, depth, d2, pgno, cnt;
 61520  61657     int hdr, cellStart;
 61521  61658     int nCell;
 61522  61659     u8 *data;
 61523  61660     BtShared *pBt;
 61524  61661     int usableSize;
 61525         -  char *hit = 0;
        61662  +  u32 *heap = 0;
        61663  +  u32 x, prev = 0;
 61526  61664     i64 nMinKey = 0;
 61527  61665     i64 nMaxKey = 0;
 61528  61666     const char *saved_zPfx = pCheck->zPfx;
 61529  61667     int saved_v1 = pCheck->v1;
 61530  61668     int saved_v2 = pCheck->v2;
 61531  61669   
 61532  61670     /* Check that the page exists
................................................................................
 61663  61801       }
 61664  61802     }
 61665  61803   
 61666  61804     /* Check for complete coverage of the page
 61667  61805     */
 61668  61806     data = pPage->aData;
 61669  61807     hdr = pPage->hdrOffset;
 61670         -  hit = sqlite3PageMalloc( pBt->pageSize );
        61808  +  heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
 61671  61809     pCheck->zPfx = 0;
 61672         -  if( hit==0 ){
        61810  +  if( heap==0 ){
 61673  61811       pCheck->mallocFailed = 1;
 61674  61812     }else{
 61675  61813       int contentOffset = get2byteNotZero(&data[hdr+5]);
 61676  61814       assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
 61677         -    memset(hit+contentOffset, 0, usableSize-contentOffset);
 61678         -    memset(hit, 1, contentOffset);
        61815  +    heap[0] = 0;
        61816  +    btreeHeapInsert(heap, contentOffset-1);
 61679  61817       /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
 61680  61818       ** number of cells on the page. */
 61681  61819       nCell = get2byte(&data[hdr+3]);
 61682  61820       /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
 61683  61821       ** immediately follows the b-tree page header. */
 61684  61822       cellStart = hdr + 12 - 4*pPage->leaf;
 61685  61823       /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
 61686  61824       ** integer offsets to the cell contents. */
 61687  61825       for(i=0; i<nCell; i++){
 61688  61826         int pc = get2byte(&data[cellStart+i*2]);
 61689  61827         u32 size = 65536;
 61690         -      int j;
 61691  61828         if( pc<=usableSize-4 ){
 61692  61829           size = cellSizePtr(pPage, &data[pc]);
 61693  61830         }
 61694  61831         if( (int)(pc+size-1)>=usableSize ){
 61695  61832           pCheck->zPfx = 0;
 61696  61833           checkAppendMsg(pCheck,
 61697  61834               "Corruption detected in cell %d on page %d",i,iPage);
 61698  61835         }else{
 61699         -        for(j=pc+size-1; j>=pc; j--) hit[j]++;
        61836  +        btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
 61700  61837         }
 61701  61838       }
 61702  61839       /* EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
 61703  61840       ** is the offset of the first freeblock, or zero if there are no
 61704  61841       ** freeblocks on the page. */
 61705  61842       i = get2byte(&data[hdr+1]);
 61706  61843       while( i>0 ){
 61707  61844         int size, j;
 61708  61845         assert( i<=usableSize-4 );     /* Enforced by btreeInitPage() */
 61709  61846         size = get2byte(&data[i+2]);
 61710  61847         assert( i+size<=usableSize );  /* Enforced by btreeInitPage() */
 61711         -      for(j=i+size-1; j>=i; j--) hit[j]++;
        61848  +      btreeHeapInsert(heap, (i<<16)|(i+size-1));
 61712  61849         /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
 61713  61850         ** big-endian integer which is the offset in the b-tree page of the next
 61714  61851         ** freeblock in the chain, or zero if the freeblock is the last on the
 61715  61852         ** chain. */
 61716  61853         j = get2byte(&data[i]);
 61717  61854         /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
 61718  61855         ** increasing offset. */
 61719  61856         assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
 61720  61857         assert( j<=usableSize-4 );   /* Enforced by btreeInitPage() */
 61721  61858         i = j;
 61722  61859       }
 61723         -    for(i=cnt=0; i<usableSize; i++){
 61724         -      if( hit[i]==0 ){
 61725         -        cnt++;
 61726         -      }else if( hit[i]>1 ){
        61860  +    cnt = 0;
        61861  +    assert( heap[0]>0 );
        61862  +    assert( (heap[1]>>16)==0 );
        61863  +    btreeHeapPull(heap,&prev);
        61864  +    while( btreeHeapPull(heap,&x) ){
        61865  +      if( (prev&0xffff)+1>(x>>16) ){
 61727  61866           checkAppendMsg(pCheck,
 61728         -          "Multiple uses for byte %d of page %d", i, iPage);
        61867  +          "Multiple uses for byte %u of page %d", x>>16, iPage);
 61729  61868           break;
        61869  +      }else{
        61870  +        cnt += (x>>16) - (prev&0xffff) - 1;
        61871  +        prev = x;
 61730  61872         }
 61731  61873       }
        61874  +    cnt += usableSize - (prev&0xffff) - 1;
 61732  61875       /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
 61733  61876       ** is stored in the fifth field of the b-tree page header.
 61734  61877       ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
 61735  61878       ** number of fragmented free bytes within the cell content area.
 61736  61879       */
 61737         -    if( cnt!=data[hdr+7] ){
        61880  +    if( heap[0]==0 && cnt!=data[hdr+7] ){
 61738  61881         checkAppendMsg(pCheck,
 61739  61882             "Fragmentation of %d bytes reported as %d on page %d",
 61740  61883             cnt, data[hdr+7], iPage);
 61741  61884       }
 61742  61885     }
 61743         -  sqlite3PageFree(hit);
        61886  +  sqlite3PageFree(heap);
 61744  61887     releasePage(pPage);
 61745  61888   
 61746  61889   end_of_check:
 61747  61890     pCheck->zPfx = saved_zPfx;
 61748  61891     pCheck->v1 = saved_v1;
 61749  61892     pCheck->v2 = saved_v2;
 61750  61893     return depth+1;
................................................................................
 61800  61943     if( !sCheck.aPgRef ){
 61801  61944       *pnErr = 1;
 61802  61945       sqlite3BtreeLeave(p);
 61803  61946       return 0;
 61804  61947     }
 61805  61948     i = PENDING_BYTE_PAGE(pBt);
 61806  61949     if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
 61807         -  sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
 61808         -  sCheck.errMsg.useMalloc = 2;
        61950  +  sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
 61809  61951   
 61810  61952     /* Check the integrity of the freelist
 61811  61953     */
 61812  61954     sCheck.zPfx = "Main freelist: ";
 61813  61955     checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
 61814  61956               get4byte(&pBt->pPage1->aData[36]));
 61815  61957     sCheck.zPfx = 0;
................................................................................
 63137  63279     if( (f&(MEM_Str|MEM_Blob)) && (pMem->szMalloc==0 || pMem->z!=pMem->zMalloc) ){
 63138  63280       if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
 63139  63281         return SQLITE_NOMEM;
 63140  63282       }
 63141  63283       pMem->z[pMem->n] = 0;
 63142  63284       pMem->z[pMem->n+1] = 0;
 63143  63285       pMem->flags |= MEM_Term;
        63286  +  }
        63287  +  pMem->flags &= ~MEM_Ephem;
 63144  63288   #ifdef SQLITE_DEBUG
 63145         -    pMem->pScopyFrom = 0;
        63289  +  pMem->pScopyFrom = 0;
 63146  63290   #endif
 63147         -  }
 63148  63291   
 63149  63292     return SQLITE_OK;
 63150  63293   }
 63151  63294   
 63152  63295   /*
 63153  63296   ** If the given Mem* has a zero-filled tail, turn it into an ordinary
 63154  63297   ** blob stored in dynamically allocated space.
................................................................................
 64584  64727   SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
 64585  64728     if( pRec ){
 64586  64729       int i;
 64587  64730       int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
 64588  64731       Mem *aMem = pRec->aMem;
 64589  64732       sqlite3 *db = aMem[0].db;
 64590  64733       for(i=0; i<nCol; i++){
 64591         -      if( aMem[i].szMalloc ) sqlite3DbFree(db, aMem[i].zMalloc);
        64734  +      sqlite3VdbeMemRelease(&aMem[i]);
 64592  64735       }
 64593  64736       sqlite3KeyInfoUnref(pRec->pKeyInfo);
 64594  64737       sqlite3DbFree(db, pRec);
 64595  64738     }
 64596  64739   }
 64597  64740   #endif /* ifdef SQLITE_ENABLE_STAT4 */
 64598  64741   
................................................................................
 66420  66563       const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
 66421  66564       assert( pVtabCursor->pVtab->nRef>0 );
 66422  66565       pVtabCursor->pVtab->nRef--;
 66423  66566       pModule->xClose(pVtabCursor);
 66424  66567     }
 66425  66568   #endif
 66426  66569   }
        66570  +
        66571  +/*
        66572  +** Close all cursors in the current frame.
        66573  +*/
        66574  +static void closeCursorsInFrame(Vdbe *p){
        66575  +  if( p->apCsr ){
        66576  +    int i;
        66577  +    for(i=0; i<p->nCursor; i++){
        66578  +      VdbeCursor *pC = p->apCsr[i];
        66579  +      if( pC ){
        66580  +        sqlite3VdbeFreeCursor(p, pC);
        66581  +        p->apCsr[i] = 0;
        66582  +      }
        66583  +    }
        66584  +  }
        66585  +}
 66427  66586   
 66428  66587   /*
 66429  66588   ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
 66430  66589   ** is used, for example, when a trigger sub-program is halted to restore
 66431  66590   ** control to the main program.
 66432  66591   */
 66433  66592   SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
 66434  66593     Vdbe *v = pFrame->v;
        66594  +  closeCursorsInFrame(v);
 66435  66595   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 66436  66596     v->anExec = pFrame->anExec;
 66437  66597   #endif
 66438  66598     v->aOnceFlag = pFrame->aOnceFlag;
 66439  66599     v->nOnceFlag = pFrame->nOnceFlag;
 66440  66600     v->aOp = pFrame->aOp;
 66441  66601     v->nOp = pFrame->nOp;
................................................................................
 66462  66622       VdbeFrame *pFrame;
 66463  66623       for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
 66464  66624       sqlite3VdbeFrameRestore(pFrame);
 66465  66625       p->pFrame = 0;
 66466  66626       p->nFrame = 0;
 66467  66627     }
 66468  66628     assert( p->nFrame==0 );
 66469         -
 66470         -  if( p->apCsr ){
 66471         -    int i;
 66472         -    for(i=0; i<p->nCursor; i++){
 66473         -      VdbeCursor *pC = p->apCsr[i];
 66474         -      if( pC ){
 66475         -        sqlite3VdbeFreeCursor(p, pC);
 66476         -        p->apCsr[i] = 0;
 66477         -      }
 66478         -    }
 66479         -  }
        66629  +  closeCursorsInFrame(p);
 66480  66630     if( p->aMem ){
 66481  66631       releaseMemArray(&p->aMem[1], p->nMem);
 66482  66632     }
 66483  66633     while( p->pDelFrame ){
 66484  66634       VdbeFrame *pDel = p->pDelFrame;
 66485  66635       p->pDelFrame = pDel->pParent;
 66486  66636       sqlite3VdbeFrameDelete(pDel);
................................................................................
 68217  68367   ** returned.
 68218  68368   **
 68219  68369   ** If database corruption is discovered, set pPKey2->errCode to 
 68220  68370   ** SQLITE_CORRUPT and return 0. If an OOM error is encountered, 
 68221  68371   ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
 68222  68372   ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
 68223  68373   */
 68224         -static int vdbeRecordCompareWithSkip(
        68374  +SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
 68225  68375     int nKey1, const void *pKey1,   /* Left key */
 68226  68376     UnpackedRecord *pPKey2,         /* Right key */
 68227  68377     int bSkip                       /* If true, skip the first field */
 68228  68378   ){
 68229  68379     u32 d1;                         /* Offset into aKey[] of next data element */
 68230  68380     int i;                          /* Index of next field to compare */
 68231  68381     u32 szHdr1;                     /* Size of record header in bytes */
................................................................................
 68403  68553     );
 68404  68554     return pPKey2->default_rc;
 68405  68555   }
 68406  68556   SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
 68407  68557     int nKey1, const void *pKey1,   /* Left key */
 68408  68558     UnpackedRecord *pPKey2          /* Right key */
 68409  68559   ){
 68410         -  return vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
        68560  +  return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
 68411  68561   }
 68412  68562   
 68413  68563   
 68414  68564   /*
 68415  68565   ** This function is an optimized version of sqlite3VdbeRecordCompare() 
 68416  68566   ** that (a) the first field of pPKey2 is an integer, and (b) the 
 68417  68567   ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
................................................................................
 68491  68641     if( v>lhs ){
 68492  68642       res = pPKey2->r1;
 68493  68643     }else if( v<lhs ){
 68494  68644       res = pPKey2->r2;
 68495  68645     }else if( pPKey2->nField>1 ){
 68496  68646       /* The first fields of the two keys are equal. Compare the trailing 
 68497  68647       ** fields.  */
 68498         -    res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
        68648  +    res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
 68499  68649     }else{
 68500  68650       /* The first fields of the two keys are equal and there are no trailing
 68501  68651       ** fields. Return pPKey2->default_rc in this case. */
 68502  68652       res = pPKey2->default_rc;
 68503  68653     }
 68504  68654   
 68505  68655     assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
................................................................................
 68539  68689       nCmp = MIN( pPKey2->aMem[0].n, nStr );
 68540  68690       res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
 68541  68691   
 68542  68692       if( res==0 ){
 68543  68693         res = nStr - pPKey2->aMem[0].n;
 68544  68694         if( res==0 ){
 68545  68695           if( pPKey2->nField>1 ){
 68546         -          res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
        68696  +          res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
 68547  68697           }else{
 68548  68698             res = pPKey2->default_rc;
 68549  68699           }
 68550  68700         }else if( res>0 ){
 68551  68701           res = pPKey2->r2;
 68552  68702         }else{
 68553  68703           res = pPKey2->r1;
................................................................................
 70481  70631     int nToken;              /* Length of the parameter token */
 70482  70632     int i;                   /* Loop counter */
 70483  70633     Mem *pVar;               /* Value of a host parameter */
 70484  70634     StrAccum out;            /* Accumulate the output here */
 70485  70635     char zBase[100];         /* Initial working space */
 70486  70636   
 70487  70637     db = p->db;
 70488         -  sqlite3StrAccumInit(&out, zBase, sizeof(zBase), 
        70638  +  sqlite3StrAccumInit(&out, db, zBase, sizeof(zBase), 
 70489  70639                         db->aLimit[SQLITE_LIMIT_LENGTH]);
 70490         -  out.db = db;
 70491  70640     if( db->nVdbeExec>1 ){
 70492  70641       while( *zRawSql ){
 70493  70642         const char *zStart = zRawSql;
 70494  70643         while( *(zRawSql++)!='\n' && *zRawSql );
 70495  70644         sqlite3StrAccumAppend(&out, "-- ", 3);
 70496  70645         assert( (zRawSql - zStart) > 0 );
 70497  70646         sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
 70498  70647       }
        70648  +  }else if( p->nVar==0 ){
        70649  +    sqlite3StrAccumAppend(&out, zRawSql, sqlite3Strlen30(zRawSql));
 70499  70650     }else{
 70500  70651       while( zRawSql[0] ){
 70501  70652         n = findNextHostParameter(zRawSql, &nToken);
 70502  70653         assert( n>0 );
 70503  70654         sqlite3StrAccumAppend(&out, zRawSql, n);
 70504  70655         zRawSql += n;
 70505  70656         assert( zRawSql[0] || nToken==0 );
................................................................................
 70508  70659           if( nToken>1 ){
 70509  70660             assert( sqlite3Isdigit(zRawSql[1]) );
 70510  70661             sqlite3GetInt32(&zRawSql[1], &idx);
 70511  70662           }else{
 70512  70663             idx = nextIndex;
 70513  70664           }
 70514  70665         }else{
 70515         -        assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
        70666  +        assert( zRawSql[0]==':' || zRawSql[0]=='$' ||
        70667  +                zRawSql[0]=='@' || zRawSql[0]=='#' );
 70516  70668           testcase( zRawSql[0]==':' );
 70517  70669           testcase( zRawSql[0]=='$' );
 70518  70670           testcase( zRawSql[0]=='@' );
        70671  +        testcase( zRawSql[0]=='#' );
 70519  70672           idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
 70520  70673           assert( idx>0 );
 70521  70674         }
 70522  70675         zRawSql += nToken;
 70523  70676         nextIndex = idx + 1;
 70524  70677         assert( idx>0 && idx<=p->nVar );
 70525  70678         pVar = &p->aVar[idx-1];
................................................................................
 71186  71339     Savepoint *p;
 71187  71340     for(p=db->pSavepoint; p; p=p->pNext) n++;
 71188  71341     assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
 71189  71342     return 1;
 71190  71343   }
 71191  71344   #endif
 71192  71345   
        71346  +/*
        71347  +** Return the register of pOp->p2 after first preparing it to be
        71348  +** overwritten with an integer value.
        71349  +*/ 
        71350  +static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
        71351  +  Mem *pOut;
        71352  +  assert( pOp->p2>0 );
        71353  +  assert( pOp->p2<=(p->nMem-p->nCursor) );
        71354  +  pOut = &p->aMem[pOp->p2];
        71355  +  memAboutToChange(p, pOut);
        71356  +  if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
        71357  +  pOut->flags = MEM_Int;
        71358  +  return pOut;
        71359  +}
        71360  +
 71193  71361   
 71194  71362   /*
 71195  71363   ** Execute as much of a VDBE program as we can.
 71196  71364   ** This is the core of sqlite3_step().  
 71197  71365   */
 71198  71366   SQLITE_PRIVATE int sqlite3VdbeExec(
 71199  71367     Vdbe *p                    /* The VDBE */
 71200  71368   ){
 71201         -  int pc=0;                  /* The program counter */
 71202  71369     Op *aOp = p->aOp;          /* Copy of p->aOp */
 71203         -  Op *pOp;                   /* Current operation */
        71370  +  Op *pOp = aOp;             /* Current operation */
        71371  +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
        71372  +  Op *pOrigOp;               /* Value of pOp at the top of the loop */
        71373  +#endif
 71204  71374     int rc = SQLITE_OK;        /* Value to return */
 71205  71375     sqlite3 *db = p->db;       /* The database */
 71206  71376     u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
 71207  71377     u8 encoding = ENC(db);     /* The database encoding */
 71208  71378     int iCompare = 0;          /* Result of last OP_Compare operation */
 71209  71379     unsigned nVmStep = 0;      /* Number of virtual machine steps */
 71210  71380   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
................................................................................
 71272  71442           }
 71273  71443         }
 71274  71444       }
 71275  71445       if( p->db->flags & SQLITE_VdbeTrace )  printf("VDBE Trace:\n");
 71276  71446     }
 71277  71447     sqlite3EndBenignMalloc();
 71278  71448   #endif
 71279         -  for(pc=p->pc; rc==SQLITE_OK; pc++){
 71280         -    assert( pc>=0 && pc<p->nOp );
        71449  +  for(pOp=&aOp[p->pc]; rc==SQLITE_OK; pOp++){
        71450  +    assert( pOp>=aOp && pOp<&aOp[p->nOp]);
 71281  71451       if( db->mallocFailed ) goto no_mem;
 71282  71452   #ifdef VDBE_PROFILE
 71283  71453       start = sqlite3Hwtime();
 71284  71454   #endif
 71285  71455       nVmStep++;
 71286         -    pOp = &aOp[pc];
 71287  71456   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 71288         -    if( p->anExec ) p->anExec[pc]++;
        71457  +    if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
 71289  71458   #endif
 71290  71459   
 71291  71460       /* Only allow tracing if SQLITE_DEBUG is defined.
 71292  71461       */
 71293  71462   #ifdef SQLITE_DEBUG
 71294  71463       if( db->flags & SQLITE_VdbeTrace ){
 71295         -      sqlite3VdbePrintOp(stdout, pc, pOp);
        71464  +      sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
 71296  71465       }
 71297  71466   #endif
 71298  71467         
 71299  71468   
 71300  71469       /* Check to see if we need to simulate an interrupt.  This only happens
 71301  71470       ** if we have a special test build.
 71302  71471       */
................................................................................
 71305  71474         sqlite3_interrupt_count--;
 71306  71475         if( sqlite3_interrupt_count==0 ){
 71307  71476           sqlite3_interrupt(db);
 71308  71477         }
 71309  71478       }
 71310  71479   #endif
 71311  71480   
 71312         -    /* On any opcode with the "out2-prerelease" tag, free any
 71313         -    ** external allocations out of mem[p2] and set mem[p2] to be
 71314         -    ** an undefined integer.  Opcodes will either fill in the integer
 71315         -    ** value or convert mem[p2] to a different type.
 71316         -    */
 71317         -    assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
 71318         -    if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
 71319         -      assert( pOp->p2>0 );
 71320         -      assert( pOp->p2<=(p->nMem-p->nCursor) );
 71321         -      pOut = &aMem[pOp->p2];
 71322         -      memAboutToChange(p, pOut);
 71323         -      if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
 71324         -      pOut->flags = MEM_Int;
 71325         -    }
 71326         -
 71327  71481       /* Sanity checking on other operands */
 71328  71482   #ifdef SQLITE_DEBUG
        71483  +    assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
 71329  71484       if( (pOp->opflags & OPFLG_IN1)!=0 ){
 71330  71485         assert( pOp->p1>0 );
 71331  71486         assert( pOp->p1<=(p->nMem-p->nCursor) );
 71332  71487         assert( memIsValid(&aMem[pOp->p1]) );
 71333  71488         assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
 71334  71489         REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
 71335  71490       }
................................................................................
 71354  71509       }
 71355  71510       if( (pOp->opflags & OPFLG_OUT3)!=0 ){
 71356  71511         assert( pOp->p3>0 );
 71357  71512         assert( pOp->p3<=(p->nMem-p->nCursor) );
 71358  71513         memAboutToChange(p, &aMem[pOp->p3]);
 71359  71514       }
 71360  71515   #endif
        71516  +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
        71517  +    pOrigOp = pOp;
        71518  +#endif
 71361  71519     
 71362  71520       switch( pOp->opcode ){
 71363  71521   
 71364  71522   /*****************************************************************************
 71365  71523   ** What follows is a massive switch statement where each case implements a
 71366  71524   ** separate instruction in the virtual machine.  If we follow the usual
 71367  71525   ** indentation conventions, each case should be indented by 6 spaces.  But
................................................................................
 71377  71535   ** opcode and the opcodes.c file is filled with an array of strings where
 71378  71536   ** each string is the symbolic name for the corresponding opcode.  If the
 71379  71537   ** case statement is followed by a comment of the form "/# same as ... #/"
 71380  71538   ** that comment is used to determine the particular value of the opcode.
 71381  71539   **
 71382  71540   ** Other keywords in the comment that follows each case are used to
 71383  71541   ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
 71384         -** Keywords include: in1, in2, in3, out2_prerelease, out2, out3.  See
        71542  +** Keywords include: in1, in2, in3, out2, out3.  See
 71385  71543   ** the mkopcodeh.awk script for additional information.
 71386  71544   **
 71387  71545   ** Documentation about VDBE opcodes is generated by scanning this file
 71388  71546   ** for lines of that contain "Opcode:".  That line and all subsequent
 71389  71547   ** comment lines are used in the generation of the opcode.html documentation
 71390  71548   ** file.
 71391  71549   **
................................................................................
 71405  71563   **
 71406  71564   ** The P1 parameter is not actually used by this opcode.  However, it
 71407  71565   ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
 71408  71566   ** that this Goto is the bottom of a loop and that the lines from P2 down
 71409  71567   ** to the current line should be indented for EXPLAIN output.
 71410  71568   */
 71411  71569   case OP_Goto: {             /* jump */
 71412         -  pc = pOp->p2 - 1;
        71570  +jump_to_p2_and_check_for_interrupt:
        71571  +  pOp = &aOp[pOp->p2 - 1];
 71413  71572   
 71414  71573     /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
 71415  71574     ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
 71416  71575     ** completion.  Check to see if sqlite3_interrupt() has been called
 71417  71576     ** or if the progress callback needs to be invoked. 
 71418  71577     **
 71419  71578     ** This code uses unstructured "goto" statements and does not look clean.
................................................................................
 71450  71609   */
 71451  71610   case OP_Gosub: {            /* jump */
 71452  71611     assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
 71453  71612     pIn1 = &aMem[pOp->p1];
 71454  71613     assert( VdbeMemDynamic(pIn1)==0 );
 71455  71614     memAboutToChange(p, pIn1);
 71456  71615     pIn1->flags = MEM_Int;
 71457         -  pIn1->u.i = pc;
        71616  +  pIn1->u.i = (int)(pOp-aOp);
 71458  71617     REGISTER_TRACE(pOp->p1, pIn1);
 71459         -  pc = pOp->p2 - 1;
        71618  +
        71619  +  /* Most jump operations do a goto to this spot in order to update
        71620  +  ** the pOp pointer. */
        71621  +jump_to_p2:
        71622  +  pOp = &aOp[pOp->p2 - 1];
 71460  71623     break;
 71461  71624   }
 71462  71625   
 71463  71626   /* Opcode:  Return P1 * * * *
 71464  71627   **
 71465  71628   ** Jump to the next instruction after the address in register P1.  After
 71466  71629   ** the jump, register P1 becomes undefined.
 71467  71630   */
 71468  71631   case OP_Return: {           /* in1 */
 71469  71632     pIn1 = &aMem[pOp->p1];
 71470  71633     assert( pIn1->flags==MEM_Int );
 71471         -  pc = (int)pIn1->u.i;
        71634  +  pOp = &aOp[pIn1->u.i];
 71472  71635     pIn1->flags = MEM_Undefined;
 71473  71636     break;
 71474  71637   }
 71475  71638   
 71476  71639   /* Opcode: InitCoroutine P1 P2 P3 * *
 71477  71640   **
 71478  71641   ** Set up register P1 so that it will Yield to the coroutine
................................................................................
 71488  71651     assert( pOp->p1>0 &&  pOp->p1<=(p->nMem-p->nCursor) );
 71489  71652     assert( pOp->p2>=0 && pOp->p2<p->nOp );
 71490  71653     assert( pOp->p3>=0 && pOp->p3<p->nOp );
 71491  71654     pOut = &aMem[pOp->p1];
 71492  71655     assert( !VdbeMemDynamic(pOut) );
 71493  71656     pOut->u.i = pOp->p3 - 1;
 71494  71657     pOut->flags = MEM_Int;
 71495         -  if( pOp->p2 ) pc = pOp->p2 - 1;
        71658  +  if( pOp->p2 ) goto jump_to_p2;
 71496  71659     break;
 71497  71660   }
 71498  71661   
 71499  71662   /* Opcode:  EndCoroutine P1 * * * *
 71500  71663   **
 71501  71664   ** The instruction at the address in register P1 is a Yield.
 71502  71665   ** Jump to the P2 parameter of that Yield.
................................................................................
 71508  71671     VdbeOp *pCaller;
 71509  71672     pIn1 = &aMem[pOp->p1];
 71510  71673     assert( pIn1->flags==MEM_Int );
 71511  71674     assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
 71512  71675     pCaller = &aOp[pIn1->u.i];
 71513  71676     assert( pCaller->opcode==OP_Yield );
 71514  71677     assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
 71515         -  pc = pCaller->p2 - 1;
        71678  +  pOp = &aOp[pCaller->p2 - 1];
 71516  71679     pIn1->flags = MEM_Undefined;
 71517  71680     break;
 71518  71681   }
 71519  71682   
 71520  71683   /* Opcode:  Yield P1 P2 * * *
 71521  71684   **
 71522  71685   ** Swap the program counter with the value in register P1.  This
................................................................................
 71532  71695   */
 71533  71696   case OP_Yield: {            /* in1, jump */
 71534  71697     int pcDest;
 71535  71698     pIn1 = &aMem[pOp->p1];
 71536  71699     assert( VdbeMemDynamic(pIn1)==0 );
 71537  71700     pIn1->flags = MEM_Int;
 71538  71701     pcDest = (int)pIn1->u.i;
 71539         -  pIn1->u.i = pc;
        71702  +  pIn1->u.i = (int)(pOp - aOp);
 71540  71703     REGISTER_TRACE(pOp->p1, pIn1);
 71541         -  pc = pcDest;
        71704  +  pOp = &aOp[pcDest];
 71542  71705     break;
 71543  71706   }
 71544  71707   
 71545  71708   /* Opcode:  HaltIfNull  P1 P2 P3 P4 P5
 71546  71709   ** Synopsis:  if r[P3]=null halt
 71547  71710   **
 71548  71711   ** Check the value in register P3.  If it is NULL then Halt using
................................................................................
 71585  71748   ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
 71586  71749   ** every program.  So a jump past the last instruction of the program
 71587  71750   ** is the same as executing Halt.
 71588  71751   */
 71589  71752   case OP_Halt: {
 71590  71753     const char *zType;
 71591  71754     const char *zLogFmt;
        71755  +  VdbeFrame *pFrame;
        71756  +  int pcx;
 71592  71757   
        71758  +  pcx = (int)(pOp - aOp);
 71593  71759     if( pOp->p1==SQLITE_OK && p->pFrame ){
 71594  71760       /* Halt the sub-program. Return control to the parent frame. */
 71595         -    VdbeFrame *pFrame = p->pFrame;
        71761  +    pFrame = p->pFrame;
 71596  71762       p->pFrame = pFrame->pParent;
 71597  71763       p->nFrame--;
 71598  71764       sqlite3VdbeSetChanges(db, p->nChange);
 71599         -    pc = sqlite3VdbeFrameRestore(pFrame);
        71765  +    pcx = sqlite3VdbeFrameRestore(pFrame);
 71600  71766       lastRowid = db->lastRowid;
 71601  71767       if( pOp->p2==OE_Ignore ){
 71602         -      /* Instruction pc is the OP_Program that invoked the sub-program 
        71768  +      /* Instruction pcx is the OP_Program that invoked the sub-program 
 71603  71769         ** currently being halted. If the p2 instruction of this OP_Halt
 71604  71770         ** instruction is set to OE_Ignore, then the sub-program is throwing
 71605  71771         ** an IGNORE exception. In this case jump to the address specified
 71606  71772         ** as the p2 of the calling OP_Program.  */
 71607         -      pc = p->aOp[pc].p2-1;
        71773  +      pcx = p->aOp[pcx].p2-1;
 71608  71774       }
 71609  71775       aOp = p->aOp;
 71610  71776       aMem = p->aMem;
        71777  +    pOp = &aOp[pcx];
 71611  71778       break;
 71612  71779     }
 71613  71780     p->rc = pOp->p1;
 71614  71781     p->errorAction = (u8)pOp->p2;
 71615         -  p->pc = pc;
        71782  +  p->pc = pcx;
 71616  71783     if( p->rc ){
 71617  71784       if( pOp->p5 ){
 71618  71785         static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
 71619  71786                                                "FOREIGN KEY" };
 71620  71787         assert( pOp->p5>=1 && pOp->p5<=4 );
 71621  71788         testcase( pOp->p5==1 );
 71622  71789         testcase( pOp->p5==2 );
................................................................................
 71632  71799         sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s", 
 71633  71800                          zType, pOp->p4.z);
 71634  71801       }else if( pOp->p4.z ){
 71635  71802         sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
 71636  71803       }else{
 71637  71804         sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType);
 71638  71805       }
 71639         -    sqlite3_log(pOp->p1, zLogFmt, pc, p->zSql, p->zErrMsg);
        71806  +    sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg);
 71640  71807     }
 71641  71808     rc = sqlite3VdbeHalt(p);
 71642  71809     assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
 71643  71810     if( rc==SQLITE_BUSY ){
 71644  71811       p->rc = rc = SQLITE_BUSY;
 71645  71812     }else{
 71646  71813       assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
................................................................................
 71651  71818   }
 71652  71819   
 71653  71820   /* Opcode: Integer P1 P2 * * *
 71654  71821   ** Synopsis: r[P2]=P1
 71655  71822   **
 71656  71823   ** The 32-bit integer value P1 is written into register P2.
 71657  71824   */
 71658         -case OP_Integer: {         /* out2-prerelease */
        71825  +case OP_Integer: {         /* out2 */
        71826  +  pOut = out2Prerelease(p, pOp);
 71659  71827     pOut->u.i = pOp->p1;
 71660  71828     break;
 71661  71829   }
 71662  71830   
 71663  71831   /* Opcode: Int64 * P2 * P4 *
 71664  71832   ** Synopsis: r[P2]=P4
 71665  71833   **
 71666  71834   ** P4 is a pointer to a 64-bit integer value.
 71667  71835   ** Write that value into register P2.
 71668  71836   */
 71669         -case OP_Int64: {           /* out2-prerelease */
        71837  +case OP_Int64: {           /* out2 */
        71838  +  pOut = out2Prerelease(p, pOp);
 71670  71839     assert( pOp->p4.pI64!=0 );
 71671  71840     pOut->u.i = *pOp->p4.pI64;
 71672  71841     break;
 71673  71842   }
 71674  71843   
 71675  71844   #ifndef SQLITE_OMIT_FLOATING_POINT
 71676  71845   /* Opcode: Real * P2 * P4 *
 71677  71846   ** Synopsis: r[P2]=P4
 71678  71847   **
 71679  71848   ** P4 is a pointer to a 64-bit floating point value.
 71680  71849   ** Write that value into register P2.
 71681  71850   */
 71682         -case OP_Real: {            /* same as TK_FLOAT, out2-prerelease */
        71851  +case OP_Real: {            /* same as TK_FLOAT, out2 */
        71852  +  pOut = out2Prerelease(p, pOp);
 71683  71853     pOut->flags = MEM_Real;
 71684  71854     assert( !sqlite3IsNaN(*pOp->p4.pReal) );
 71685  71855     pOut->u.r = *pOp->p4.pReal;
 71686  71856     break;
 71687  71857   }
 71688  71858   #endif
 71689  71859   
................................................................................
 71691  71861   ** Synopsis: r[P2]='P4'
 71692  71862   **
 71693  71863   ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 
 71694  71864   ** into a String opcode before it is executed for the first time.  During
 71695  71865   ** this transformation, the length of string P4 is computed and stored
 71696  71866   ** as the P1 parameter.
 71697  71867   */
 71698         -case OP_String8: {         /* same as TK_STRING, out2-prerelease */
        71868  +case OP_String8: {         /* same as TK_STRING, out2 */
 71699  71869     assert( pOp->p4.z!=0 );
        71870  +  pOut = out2Prerelease(p, pOp);
 71700  71871     pOp->opcode = OP_String;
 71701  71872     pOp->p1 = sqlite3Strlen30(pOp->p4.z);
 71702  71873   
 71703  71874   #ifndef SQLITE_OMIT_UTF16
 71704  71875     if( encoding!=SQLITE_UTF8 ){
 71705  71876       rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
 71706  71877       if( rc==SQLITE_TOOBIG ) goto too_big;
................................................................................
 71729  71900   ** The string value P4 of length P1 (bytes) is stored in register P2.
 71730  71901   **
 71731  71902   ** If P5!=0 and the content of register P3 is greater than zero, then
 71732  71903   ** the datatype of the register P2 is converted to BLOB.  The content is
 71733  71904   ** the same sequence of bytes, it is merely interpreted as a BLOB instead
 71734  71905   ** of a string, as if it had been CAST.
 71735  71906   */
 71736         -case OP_String: {          /* out2-prerelease */
        71907  +case OP_String: {          /* out2 */
 71737  71908     assert( pOp->p4.z!=0 );
        71909  +  pOut = out2Prerelease(p, pOp);
 71738  71910     pOut->flags = MEM_Str|MEM_Static|MEM_Term;
 71739  71911     pOut->z = pOp->p4.z;
 71740  71912     pOut->n = pOp->p1;
 71741  71913     pOut->enc = encoding;
 71742  71914     UPDATE_MAX_BLOBSIZE(pOut);
 71743  71915     if( pOp->p5 ){
 71744  71916       assert( pOp->p3>0 );
................................................................................
 71758  71930   ** is less than P2 (typically P3 is zero) then only register P2 is
 71759  71931   ** set to NULL.
 71760  71932   **
 71761  71933   ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
 71762  71934   ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
 71763  71935   ** OP_Ne or OP_Eq.
 71764  71936   */
 71765         -case OP_Null: {           /* out2-prerelease */
        71937  +case OP_Null: {           /* out2 */
 71766  71938     int cnt;
 71767  71939     u16 nullFlag;
        71940  +  pOut = out2Prerelease(p, pOp);
 71768  71941     cnt = pOp->p3-pOp->p2;
 71769  71942     assert( pOp->p3<=(p->nMem-p->nCursor) );
 71770  71943     pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
 71771  71944     while( cnt>0 ){
 71772  71945       pOut++;
 71773  71946       memAboutToChange(p, pOut);
 71774  71947       sqlite3VdbeMemSetNull(pOut);
................................................................................
 71795  71968   
 71796  71969   /* Opcode: Blob P1 P2 * P4 *
 71797  71970   ** Synopsis: r[P2]=P4 (len=P1)
 71798  71971   **
 71799  71972   ** P4 points to a blob of data P1 bytes long.  Store this
 71800  71973   ** blob in register P2.
 71801  71974   */
 71802         -case OP_Blob: {                /* out2-prerelease */
        71975  +case OP_Blob: {                /* out2 */
 71803  71976     assert( pOp->p1 <= SQLITE_MAX_LENGTH );
        71977  +  pOut = out2Prerelease(p, pOp);
 71804  71978     sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
 71805  71979     pOut->enc = encoding;
 71806  71980     UPDATE_MAX_BLOBSIZE(pOut);
 71807  71981     break;
 71808  71982   }
 71809  71983   
 71810  71984   /* Opcode: Variable P1 P2 * P4 *
................................................................................
 71811  71985   ** Synopsis: r[P2]=parameter(P1,P4)
 71812  71986   **
 71813  71987   ** Transfer the values of bound parameter P1 into register P2
 71814  71988   **
 71815  71989   ** If the parameter is named, then its name appears in P4.
 71816  71990   ** The P4 value is used by sqlite3_bind_parameter_name().
 71817  71991   */
 71818         -case OP_Variable: {            /* out2-prerelease */
        71992  +case OP_Variable: {            /* out2 */
 71819  71993     Mem *pVar;       /* Value being transferred */
 71820  71994   
 71821  71995     assert( pOp->p1>0 && pOp->p1<=p->nVar );
 71822  71996     assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
 71823  71997     pVar = &p->aVar[pOp->p1 - 1];
 71824  71998     if( sqlite3VdbeMemTooBig(pVar) ){
 71825  71999       goto too_big;
 71826  72000     }
        72001  +  pOut = out2Prerelease(p, pOp);
 71827  72002     sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
 71828  72003     UPDATE_MAX_BLOBSIZE(pOut);
 71829  72004     break;
 71830  72005   }
 71831  72006   
 71832  72007   /* Opcode: Move P1 P2 P3 * *
 71833  72008   ** Synopsis:  r[P2@P3]=r[P1@P3]
................................................................................
 71854  72029     do{
 71855  72030       assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
 71856  72031       assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
 71857  72032       assert( memIsValid(pIn1) );
 71858  72033       memAboutToChange(p, pOut);
 71859  72034       sqlite3VdbeMemMove(pOut, pIn1);
 71860  72035   #ifdef SQLITE_DEBUG
 71861         -    if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
 71862         -      pOut->pScopyFrom += p1 - pOp->p2;
        72036  +    if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
        72037  +      pOut->pScopyFrom += pOp->p2 - p1;
 71863  72038       }
 71864  72039   #endif
        72040  +    Deephemeralize(pOut);
 71865  72041       REGISTER_TRACE(p2++, pOut);
 71866  72042       pIn1++;
 71867  72043       pOut++;
 71868  72044     }while( --n );
 71869  72045     break;
 71870  72046   }
 71871  72047   
................................................................................
 71996  72172       sqlite3VdbeMemNulTerminate(&pMem[i]);
 71997  72173       REGISTER_TRACE(pOp->p1+i, &pMem[i]);
 71998  72174     }
 71999  72175     if( db->mallocFailed ) goto no_mem;
 72000  72176   
 72001  72177     /* Return SQLITE_ROW
 72002  72178     */
 72003         -  p->pc = pc + 1;
        72179  +  p->pc = (int)(pOp - aOp) + 1;
 72004  72180     rc = SQLITE_ROW;
 72005  72181     goto vdbe_return;
 72006  72182   }
 72007  72183   
 72008  72184   /* Opcode: Concat P1 P2 P3 * *
 72009  72185   ** Synopsis: r[P3]=r[P2]+r[P1]
 72010  72186   **
................................................................................
 72242  72418       apVal[i] = pArg;
 72243  72419       Deephemeralize(pArg);
 72244  72420       REGISTER_TRACE(pOp->p2+i, pArg);
 72245  72421     }
 72246  72422   
 72247  72423     assert( pOp->p4type==P4_FUNCDEF );
 72248  72424     ctx.pFunc = pOp->p4.pFunc;
 72249         -  ctx.iOp = pc;
        72425  +  ctx.iOp = (int)(pOp - aOp);
 72250  72426     ctx.pVdbe = p;
 72251  72427     MemSetTypeFlag(ctx.pOut, MEM_Null);
 72252  72428     ctx.fErrorOrAux = 0;
 72253  72429     db->lastRowid = lastRowid;
 72254  72430     (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
 72255  72431     lastRowid = db->lastRowid;  /* Remember rowid changes made by xFunc */
 72256  72432   
 72257  72433     /* If the function returned an error, throw an exception */
 72258  72434     if( ctx.fErrorOrAux ){
 72259  72435       if( ctx.isError ){
 72260  72436         sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut));
 72261  72437         rc = ctx.isError;
 72262  72438       }
 72263         -    sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
        72439  +    sqlite3VdbeDeleteAuxData(p, (int)(pOp - aOp), pOp->p1);
 72264  72440     }
 72265  72441   
 72266  72442     /* Copy the result of the function into register P3 */
 72267  72443     sqlite3VdbeChangeEncoding(ctx.pOut, encoding);
 72268  72444     if( sqlite3VdbeMemTooBig(ctx.pOut) ){
 72269  72445       goto too_big;
 72270  72446     }
................................................................................
 72385  72561       applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
 72386  72562       VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
 72387  72563       if( (pIn1->flags & MEM_Int)==0 ){
 72388  72564         if( pOp->p2==0 ){
 72389  72565           rc = SQLITE_MISMATCH;
 72390  72566           goto abort_due_to_error;
 72391  72567         }else{
 72392         -        pc = pOp->p2 - 1;
 72393         -        break;
        72568  +        goto jump_to_p2;
 72394  72569         }
 72395  72570       }
 72396  72571     }
 72397  72572     MemSetTypeFlag(pIn1, MEM_Int);
 72398  72573     break;
 72399  72574   }
 72400  72575   
................................................................................
 72572  72747         if( pOp->p5 & SQLITE_STOREP2 ){
 72573  72748           pOut = &aMem[pOp->p2];
 72574  72749           MemSetTypeFlag(pOut, MEM_Null);
 72575  72750           REGISTER_TRACE(pOp->p2, pOut);
 72576  72751         }else{
 72577  72752           VdbeBranchTaken(2,3);
 72578  72753           if( pOp->p5 & SQLITE_JUMPIFNULL ){
 72579         -          pc = pOp->p2-1;
        72754  +          goto jump_to_p2;
 72580  72755           }
 72581  72756         }
 72582  72757         break;
 72583  72758       }
 72584  72759     }else{
 72585  72760       /* Neither operand is NULL.  Do a comparison. */
 72586  72761       affinity = pOp->p5 & SQLITE_AFF_MASK;
................................................................................
 72623  72798       case OP_Eq:    res = res==0;     break;
 72624  72799       case OP_Ne:    res = res!=0;     break;
 72625  72800       case OP_Lt:    res = res<0;      break;
 72626  72801       case OP_Le:    res = res<=0;     break;
 72627  72802       case OP_Gt:    res = res>0;      break;
 72628  72803       default:       res = res>=0;     break;
 72629  72804     }
        72805  +
        72806  +  /* Undo any changes made by applyAffinity() to the input registers. */
        72807  +  assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
        72808  +  pIn1->flags = flags1;
        72809  +  assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
        72810  +  pIn3->flags = flags3;
 72630  72811   
 72631  72812     if( pOp->p5 & SQLITE_STOREP2 ){
 72632  72813       pOut = &aMem[pOp->p2];
 72633  72814       memAboutToChange(p, pOut);
 72634  72815       MemSetTypeFlag(pOut, MEM_Int);
 72635  72816       pOut->u.i = res;
 72636  72817       REGISTER_TRACE(pOp->p2, pOut);
 72637  72818     }else{
 72638  72819       VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
 72639  72820       if( res ){
 72640         -      pc = pOp->p2-1;
        72821  +      goto jump_to_p2;
 72641  72822       }
 72642  72823     }
 72643         -  /* Undo any changes made by applyAffinity() to the input registers. */
 72644         -  assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
 72645         -  pIn1->flags = flags1;
 72646         -  assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
 72647         -  pIn3->flags = flags3;
 72648  72824     break;
 72649  72825   }
 72650  72826   
 72651  72827   /* Opcode: Permutation * * * P4 *
 72652  72828   **
 72653  72829   ** Set the permutation used by the OP_Compare operator to be the array
 72654  72830   ** of integers in P4.
................................................................................
 72735  72911   **
 72736  72912   ** Jump to the instruction at address P1, P2, or P3 depending on whether
 72737  72913   ** in the most recent OP_Compare instruction the P1 vector was less than
 72738  72914   ** equal to, or greater than the P2 vector, respectively.
 72739  72915   */
 72740  72916   case OP_Jump: {             /* jump */
 72741  72917     if( iCompare<0 ){
 72742         -    pc = pOp->p1 - 1;  VdbeBranchTaken(0,3);
        72918  +    VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1];
 72743  72919     }else if( iCompare==0 ){
 72744         -    pc = pOp->p2 - 1;  VdbeBranchTaken(1,3);
        72920  +    VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1];
 72745  72921     }else{
 72746         -    pc = pOp->p3 - 1;  VdbeBranchTaken(2,3);
        72922  +    VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1];
 72747  72923     }
 72748  72924     break;
 72749  72925   }
 72750  72926   
 72751  72927   /* Opcode: And P1 P2 P3 * *
 72752  72928   ** Synopsis: r[P3]=(r[P1] && r[P2])
 72753  72929   **
................................................................................
 72849  73025   ** All "once" flags are initially cleared whenever a prepared statement
 72850  73026   ** first begins to run.
 72851  73027   */
 72852  73028   case OP_Once: {             /* jump */
 72853  73029     assert( pOp->p1<p->nOnceFlag );
 72854  73030     VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
 72855  73031     if( p->aOnceFlag[pOp->p1] ){
 72856         -    pc = pOp->p2-1;
        73032  +    goto jump_to_p2;
 72857  73033     }else{
 72858  73034       p->aOnceFlag[pOp->p1] = 1;
 72859  73035     }
 72860  73036     break;
 72861  73037   }
 72862  73038   
 72863  73039   /* Opcode: If P1 P2 P3 * *
................................................................................
 72884  73060   #else
 72885  73061       c = sqlite3VdbeRealValue(pIn1)!=0.0;
 72886  73062   #endif
 72887  73063       if( pOp->opcode==OP_IfNot ) c = !c;
 72888  73064     }
 72889  73065     VdbeBranchTaken(c!=0, 2);
 72890  73066     if( c ){
 72891         -    pc = pOp->p2-1;
        73067  +    goto jump_to_p2;
 72892  73068     }
 72893  73069     break;
 72894  73070   }
 72895  73071   
 72896  73072   /* Opcode: IsNull P1 P2 * * *
 72897  73073   ** Synopsis:  if r[P1]==NULL goto P2
 72898  73074   **
 72899  73075   ** Jump to P2 if the value in register P1 is NULL.
 72900  73076   */
 72901  73077   case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
 72902  73078     pIn1 = &aMem[pOp->p1];
 72903  73079     VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
 72904  73080     if( (pIn1->flags & MEM_Null)!=0 ){
 72905         -    pc = pOp->p2 - 1;
        73081  +    goto jump_to_p2;
 72906  73082     }
 72907  73083     break;
 72908  73084   }
 72909  73085   
 72910  73086   /* Opcode: NotNull P1 P2 * * *
 72911  73087   ** Synopsis: if r[P1]!=NULL goto P2
 72912  73088   **
 72913  73089   ** Jump to P2 if the value in register P1 is not NULL.  
 72914  73090   */
 72915  73091   case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
 72916  73092     pIn1 = &aMem[pOp->p1];
 72917  73093     VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
 72918  73094     if( (pIn1->flags & MEM_Null)==0 ){
 72919         -    pc = pOp->p2 - 1;
        73095  +    goto jump_to_p2;
 72920  73096     }
 72921  73097     break;
 72922  73098   }
 72923  73099   
 72924  73100   /* Opcode: Column P1 P2 P3 P4 P5
 72925  73101   ** Synopsis:  r[P3]=PX
 72926  73102   **
................................................................................
 73126  73302          || (offset > pC->payloadSize)
 73127  73303         ){
 73128  73304           rc = SQLITE_CORRUPT_BKPT;
 73129  73305           goto op_column_error;
 73130  73306         }
 73131  73307       }
 73132  73308   
 73133         -    /* If after trying to extra new entries from the header, nHdrParsed is
        73309  +    /* If after trying to extract new entries from the header, nHdrParsed is
 73134  73310       ** still not up to p2, that means that the record has fewer than p2
 73135  73311       ** columns.  So the result will be either the default value or a NULL.
 73136  73312       */
 73137  73313       if( pC->nHdrParsed<=p2 ){
 73138  73314         if( pOp->p4type==P4_MEM ){
 73139  73315           sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
 73140  73316         }else{
................................................................................
 73250  73426   */
 73251  73427   case OP_MakeRecord: {
 73252  73428     u8 *zNewRecord;        /* A buffer to hold the data for the new record */
 73253  73429     Mem *pRec;             /* The new record */
 73254  73430     u64 nData;             /* Number of bytes of data space */
 73255  73431     int nHdr;              /* Number of bytes of header space */
 73256  73432     i64 nByte;             /* Data space required for this record */
 73257         -  int nZero;             /* Number of zero bytes at the end of the record */
        73433  +  i64 nZero;             /* Number of zero bytes at the end of the record */
 73258  73434     int nVarint;           /* Number of bytes in a varint */
 73259  73435     u32 serial_type;       /* Type field */
 73260  73436     Mem *pData0;           /* First field to be combined into the record */
 73261  73437     Mem *pLast;            /* Last field of the record */
 73262  73438     int nField;            /* Number of fields in the record */
 73263  73439     char *zAffinity;       /* The affinity string for the record */
 73264  73440     int file_format;       /* File format to use for encoding */
................................................................................
 73342  73518     }else{
 73343  73519       /* Rare case of a really large header */
 73344  73520       nVarint = sqlite3VarintLen(nHdr);
 73345  73521       nHdr += nVarint;
 73346  73522       if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
 73347  73523     }
 73348  73524     nByte = nHdr+nData;
 73349         -  if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
        73525  +  if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 73350  73526       goto too_big;
 73351  73527     }
 73352  73528   
 73353  73529     /* Make sure the output register has a buffer large enough to store 
 73354  73530     ** the new record. The output register (pOp->p3) is not allowed to
 73355  73531     ** be one of the input registers (because the following call to
 73356  73532     ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
................................................................................
 73393  73569   /* Opcode: Count P1 P2 * * *
 73394  73570   ** Synopsis: r[P2]=count()
 73395  73571   **
 73396  73572   ** Store the number of entries (an integer value) in the table or index 
 73397  73573   ** opened by cursor P1 in register P2
 73398  73574   */
 73399  73575   #ifndef SQLITE_OMIT_BTREECOUNT
 73400         -case OP_Count: {         /* out2-prerelease */
        73576  +case OP_Count: {         /* out2 */
 73401  73577     i64 nEntry;
 73402  73578     BtCursor *pCrsr;
 73403  73579   
 73404  73580     pCrsr = p->apCsr[pOp->p1]->pCursor;
 73405  73581     assert( pCrsr );
 73406  73582     nEntry = 0;  /* Not needed.  Only used to silence a warning. */
 73407  73583     rc = sqlite3BtreeCount(pCrsr, &nEntry);
        73584  +  pOut = out2Prerelease(p, pOp);
 73408  73585     pOut->u.i = nEntry;
 73409  73586     break;
 73410  73587   }
 73411  73588   #endif
 73412  73589   
 73413  73590   /* Opcode: Savepoint P1 * * P4 *
 73414  73591   **
................................................................................
 73514  73691         int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
 73515  73692         if( isTransaction && p1==SAVEPOINT_RELEASE ){
 73516  73693           if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
 73517  73694             goto vdbe_return;
 73518  73695           }
 73519  73696           db->autoCommit = 1;
 73520  73697           if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
 73521         -          p->pc = pc;
        73698  +          p->pc = (int)(pOp - aOp);
 73522  73699             db->autoCommit = 0;
 73523  73700             p->rc = rc = SQLITE_BUSY;
 73524  73701             goto vdbe_return;
 73525  73702           }
 73526  73703           db->isTransactionSavepoint = 0;
 73527  73704           rc = p->rc;
 73528  73705         }else{
................................................................................
 73573  73750             db->nSavepoint--;
 73574  73751           }
 73575  73752         }else{
 73576  73753           db->nDeferredCons = pSavepoint->nDeferredCons;
 73577  73754           db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
 73578  73755         }
 73579  73756   
 73580         -      if( !isTransaction ){
        73757  +      if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
 73581  73758           rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
 73582  73759           if( rc!=SQLITE_OK ) goto abort_due_to_error;
 73583  73760         }
 73584  73761       }
 73585  73762     }
 73586  73763   
 73587  73764     break;
................................................................................
 73633  73810         sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
 73634  73811         db->autoCommit = 1;
 73635  73812       }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
 73636  73813         goto vdbe_return;
 73637  73814       }else{
 73638  73815         db->autoCommit = (u8)desiredAutoCommit;
 73639  73816         if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
 73640         -        p->pc = pc;
        73817  +        p->pc = (int)(pOp - aOp);
 73641  73818           db->autoCommit = (u8)(1-desiredAutoCommit);
 73642  73819           p->rc = rc = SQLITE_BUSY;
 73643  73820           goto vdbe_return;
 73644  73821         }
 73645  73822       }
 73646  73823       assert( db->nStatement==0 );
 73647  73824       sqlite3CloseSavepoints(db);
................................................................................
 73710  73887       goto abort_due_to_error;
 73711  73888     }
 73712  73889     pBt = db->aDb[pOp->p1].pBt;
 73713  73890   
 73714  73891     if( pBt ){
 73715  73892       rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
 73716  73893       if( rc==SQLITE_BUSY ){
 73717         -      p->pc = pc;
        73894  +      p->pc = (int)(pOp - aOp);
 73718  73895         p->rc = rc = SQLITE_BUSY;
 73719  73896         goto vdbe_return;
 73720  73897       }
 73721  73898       if( rc!=SQLITE_OK ){
 73722  73899         goto abort_due_to_error;
 73723  73900       }
 73724  73901   
................................................................................
 73789  73966   ** the main database file and P1==1 is the database file used to store
 73790  73967   ** temporary tables.
 73791  73968   **
 73792  73969   ** There must be a read-lock on the database (either a transaction
 73793  73970   ** must be started or there must be an open cursor) before
 73794  73971   ** executing this instruction.
 73795  73972   */
 73796         -case OP_ReadCookie: {               /* out2-prerelease */
        73973  +case OP_ReadCookie: {               /* out2 */
 73797  73974     int iMeta;
 73798  73975     int iDb;
 73799  73976     int iCookie;
 73800  73977   
 73801  73978     assert( p->bIsReader );
 73802  73979     iDb = pOp->p1;
 73803  73980     iCookie = pOp->p3;
 73804  73981     assert( pOp->p3<SQLITE_N_BTREE_META );
 73805  73982     assert( iDb>=0 && iDb<db->nDb );
 73806  73983     assert( db->aDb[iDb].pBt!=0 );
 73807  73984     assert( DbMaskTest(p->btreeMask, iDb) );
 73808  73985   
 73809  73986     sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
        73987  +  pOut = out2Prerelease(p, pOp);
 73810  73988     pOut->u.i = iMeta;
 73811  73989     break;
 73812  73990   }
 73813  73991   
 73814  73992   /* Opcode: SetCookie P1 P2 P3 * *
 73815  73993   **
 73816  73994   ** Write the content of register P3 (interpreted as an integer)
................................................................................
 74124  74302   */
 74125  74303   case OP_SequenceTest: {
 74126  74304     VdbeCursor *pC;
 74127  74305     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 74128  74306     pC = p->apCsr[pOp->p1];
 74129  74307     assert( pC->pSorter );
 74130  74308     if( (pC->seqCount++)==0 ){
 74131         -    pc = pOp->p2 - 1;
        74309  +    goto jump_to_p2;
 74132  74310     }
 74133  74311     break;
 74134  74312   }
 74135  74313   
 74136  74314   /* Opcode: OpenPseudo P1 P2 P3 * *
 74137  74315   ** Synopsis: P3 columns in r[P2]
 74138  74316   **
................................................................................
 74301  74479   
 74302  74480       /* If the P3 value could not be converted into an integer without
 74303  74481       ** loss of information, then special processing is required... */
 74304  74482       if( (pIn3->flags & MEM_Int)==0 ){
 74305  74483         if( (pIn3->flags & MEM_Real)==0 ){
 74306  74484           /* If the P3 value cannot be converted into any kind of a number,
 74307  74485           ** then the seek is not possible, so jump to P2 */
 74308         -        pc = pOp->p2 - 1;  VdbeBranchTaken(1,2);
        74486  +        VdbeBranchTaken(1,2); goto jump_to_p2;
 74309  74487           break;
 74310  74488         }
 74311  74489   
 74312  74490         /* If the approximation iKey is larger than the actual real search
 74313  74491         ** term, substitute >= for > and < for <=. e.g. if the search term
 74314  74492         ** is 4.9 and the integer approximation 5:
 74315  74493         **
................................................................................
 74392  74570         */
 74393  74571         res = sqlite3BtreeEof(pC->pCursor);
 74394  74572       }
 74395  74573     }
 74396  74574     assert( pOp->p2>0 );
 74397  74575     VdbeBranchTaken(res!=0,2);
 74398  74576     if( res ){
 74399         -    pc = pOp->p2 - 1;
        74577  +    goto jump_to_p2;
 74400  74578     }
 74401  74579     break;
 74402  74580   }
 74403  74581   
 74404  74582   /* Opcode: Seek P1 P2 * * *
 74405  74583   ** Synopsis:  intkey=r[P2]
 74406  74584   **
................................................................................
 74486  74664   **
 74487  74665   ** See also: NotFound, Found, NotExists
 74488  74666   */
 74489  74667   case OP_NoConflict:     /* jump, in3 */
 74490  74668   case OP_NotFound:       /* jump, in3 */
 74491  74669   case OP_Found: {        /* jump, in3 */
 74492  74670     int alreadyExists;
        74671  +  int takeJump;
 74493  74672     int ii;
 74494  74673     VdbeCursor *pC;
 74495  74674     int res;
 74496  74675     char *pFree;
 74497  74676     UnpackedRecord *pIdxKey;
 74498  74677     UnpackedRecord r;
 74499  74678     char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
................................................................................
 74508  74687     assert( pC!=0 );
 74509  74688   #ifdef SQLITE_DEBUG
 74510  74689     pC->seekOp = pOp->opcode;
 74511  74690   #endif
 74512  74691     pIn3 = &aMem[pOp->p3];
 74513  74692     assert( pC->pCursor!=0 );
 74514  74693     assert( pC->isTable==0 );
 74515         -  pFree = 0;  /* Not needed.  Only used to suppress a compiler warning. */
        74694  +  pFree = 0;
 74516  74695     if( pOp->p4.i>0 ){
 74517  74696       r.pKeyInfo = pC->pKeyInfo;
 74518  74697       r.nField = (u16)pOp->p4.i;
 74519  74698       r.aMem = pIn3;
 74520  74699       for(ii=0; ii<r.nField; ii++){
 74521  74700         assert( memIsValid(&r.aMem[ii]) );
 74522  74701         ExpandBlob(&r.aMem[ii]);
................................................................................
 74531  74710       );
 74532  74711       if( pIdxKey==0 ) goto no_mem;
 74533  74712       assert( pIn3->flags & MEM_Blob );
 74534  74713       ExpandBlob(pIn3);
 74535  74714       sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
 74536  74715     }
 74537  74716     pIdxKey->default_rc = 0;
        74717  +  takeJump = 0;
 74538  74718     if( pOp->opcode==OP_NoConflict ){
 74539  74719       /* For the OP_NoConflict opcode, take the jump if any of the
 74540  74720       ** input fields are NULL, since any key with a NULL will not
 74541  74721       ** conflict */
 74542  74722       for(ii=0; ii<pIdxKey->nField; ii++){
 74543  74723         if( pIdxKey->aMem[ii].flags & MEM_Null ){
 74544         -        pc = pOp->p2 - 1; VdbeBranchTaken(1,2);
        74724  +        takeJump = 1;
 74545  74725           break;
 74546  74726         }
 74547  74727       }
 74548  74728     }
 74549  74729     rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
 74550         -  if( pOp->p4.i==0 ){
 74551         -    sqlite3DbFree(db, pFree);
 74552         -  }
        74730  +  sqlite3DbFree(db, pFree);
 74553  74731     if( rc!=SQLITE_OK ){
 74554  74732       break;
 74555  74733     }
 74556  74734     pC->seekResult = res;
 74557  74735     alreadyExists = (res==0);
 74558  74736     pC->nullRow = 1-alreadyExists;
 74559  74737     pC->deferredMoveto = 0;
 74560  74738     pC->cacheStatus = CACHE_STALE;
 74561  74739     if( pOp->opcode==OP_Found ){
 74562  74740       VdbeBranchTaken(alreadyExists!=0,2);
 74563         -    if( alreadyExists ) pc = pOp->p2 - 1;
        74741  +    if( alreadyExists ) goto jump_to_p2;
 74564  74742     }else{
 74565         -    VdbeBranchTaken(alreadyExists==0,2);
 74566         -    if( !alreadyExists ) pc = pOp->p2 - 1;
        74743  +    VdbeBranchTaken(takeJump||alreadyExists==0,2);
        74744  +    if( takeJump || !alreadyExists ) goto jump_to_p2;
 74567  74745     }
 74568  74746     break;
 74569  74747   }
 74570  74748   
 74571  74749   /* Opcode: NotExists P1 P2 P3 * *
 74572  74750   ** Synopsis: intkey=r[P3]
 74573  74751   **
................................................................................
 74608  74786     iKey = pIn3->u.i;
 74609  74787     rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
 74610  74788     pC->movetoTarget = iKey;  /* Used by OP_Delete */
 74611  74789     pC->nullRow = 0;
 74612  74790     pC->cacheStatus = CACHE_STALE;
 74613  74791     pC->deferredMoveto = 0;
 74614  74792     VdbeBranchTaken(res!=0,2);
 74615         -  if( res!=0 ){
 74616         -    pc = pOp->p2 - 1;
 74617         -  }
 74618  74793     pC->seekResult = res;
        74794  +  if( res!=0 ) goto jump_to_p2;
 74619  74795     break;
 74620  74796   }
 74621  74797   
 74622  74798   /* Opcode: Sequence P1 P2 * * *
 74623  74799   ** Synopsis: r[P2]=cursor[P1].ctr++
 74624  74800   **
 74625  74801   ** Find the next available sequence number for cursor P1.
 74626  74802   ** Write the sequence number into register P2.
 74627  74803   ** The sequence number on the cursor is incremented after this
 74628  74804   ** instruction.  
 74629  74805   */
 74630         -case OP_Sequence: {           /* out2-prerelease */
        74806  +case OP_Sequence: {           /* out2 */
 74631  74807     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 74632  74808     assert( p->apCsr[pOp->p1]!=0 );
        74809  +  pOut = out2Prerelease(p, pOp);
 74633  74810     pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
 74634  74811     break;
 74635  74812   }
 74636  74813   
 74637  74814   
 74638  74815   /* Opcode: NewRowid P1 P2 P3 * *
 74639  74816   ** Synopsis: r[P2]=rowid
................................................................................
 74646  74823   ** If P3>0 then P3 is a register in the root frame of this VDBE that holds 
 74647  74824   ** the largest previously generated record number. No new record numbers are
 74648  74825   ** allowed to be less than this value. When this value reaches its maximum, 
 74649  74826   ** an SQLITE_FULL error is generated. The P3 register is updated with the '
 74650  74827   ** generated record number. This P3 mechanism is used to help implement the
 74651  74828   ** AUTOINCREMENT feature.
 74652  74829   */
 74653         -case OP_NewRowid: {           /* out2-prerelease */
        74830  +case OP_NewRowid: {           /* out2 */
 74654  74831     i64 v;                 /* The new rowid */
 74655  74832     VdbeCursor *pC;        /* Cursor of table to get the new rowid */
 74656  74833     int res;               /* Result of an sqlite3BtreeLast() */
 74657  74834     int cnt;               /* Counter to limit the number of searches */
 74658  74835     Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
 74659  74836     VdbeFrame *pFrame;     /* Root frame of VDBE */
 74660  74837   
 74661  74838     v = 0;
 74662  74839     res = 0;
        74840  +  pOut = out2Prerelease(p, pOp);
 74663  74841     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 74664  74842     pC = p->apCsr[pOp->p1];
 74665  74843     assert( pC!=0 );
 74666  74844     if( NEVER(pC->pCursor==0) ){
 74667  74845       /* The zero initialization above is all that is needed */
 74668  74846     }else{
 74669  74847       /* The next rowid or record number (different terms for the same
................................................................................
 74969  75147     assert( isSorter(pC) );
 74970  75148     assert( pOp->p4type==P4_INT32 );
 74971  75149     pIn3 = &aMem[pOp->p3];
 74972  75150     nKeyCol = pOp->p4.i;
 74973  75151     res = 0;
 74974  75152     rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
 74975  75153     VdbeBranchTaken(res!=0,2);
 74976         -  if( res ){
 74977         -    pc = pOp->p2-1;
 74978         -  }
        75154  +  if( res ) goto jump_to_p2;
 74979  75155     break;
 74980  75156   };
 74981  75157   
 74982  75158   /* Opcode: SorterData P1 P2 P3 * *
 74983  75159   ** Synopsis: r[P2]=data
 74984  75160   **
 74985  75161   ** Write into register P2 the current sorter data for sorter cursor P1.
................................................................................
 75100  75276   ** Store in register P2 an integer which is the key of the table entry that
 75101  75277   ** P1 is currently point to.
 75102  75278   **
 75103  75279   ** P1 can be either an ordinary table or a virtual table.  There used to
 75104  75280   ** be a separate OP_VRowid opcode for use with virtual tables, but this
 75105  75281   ** one opcode now works for both table types.
 75106  75282   */
 75107         -case OP_Rowid: {                 /* out2-prerelease */
        75283  +case OP_Rowid: {                 /* out2 */
 75108  75284     VdbeCursor *pC;
 75109  75285     i64 v;
 75110  75286     sqlite3_vtab *pVtab;
 75111  75287     const sqlite3_module *pModule;
 75112  75288   
        75289  +  pOut = out2Prerelease(p, pOp);
 75113  75290     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 75114  75291     pC = p->apCsr[pOp->p1];
 75115  75292     assert( pC!=0 );
 75116  75293     assert( pC->pseudoTableReg==0 || pC->nullRow );
 75117  75294     if( pC->nullRow ){
 75118  75295       pOut->flags = MEM_Null;
 75119  75296       break;
................................................................................
 75158  75335     pC->cacheStatus = CACHE_STALE;
 75159  75336     if( pC->pCursor ){
 75160  75337       sqlite3BtreeClearCursor(pC->pCursor);
 75161  75338     }
 75162  75339     break;
 75163  75340   }
 75164  75341   
 75165         -/* Opcode: Last P1 P2 * * *
        75342  +/* Opcode: Last P1 P2 P3 * *
 75166  75343   **
 75167  75344   ** The next use of the Rowid or Column or Prev instruction for P1 
 75168  75345   ** will refer to the last entry in the database table or index.
 75169  75346   ** If the table or index is empty and P2>0, then jump immediately to P2.
 75170  75347   ** If P2 is 0 or if the table or index is not empty, fall through
 75171  75348   ** to the following instruction.
 75172  75349   **
................................................................................
 75185  75362     pCrsr = pC->pCursor;
 75186  75363     res = 0;
 75187  75364     assert( pCrsr!=0 );
 75188  75365     rc = sqlite3BtreeLast(pCrsr, &res);
 75189  75366     pC->nullRow = (u8)res;
 75190  75367     pC->deferredMoveto = 0;
 75191  75368     pC->cacheStatus = CACHE_STALE;
        75369  +  pC->seekResult = pOp->p3;
 75192  75370   #ifdef SQLITE_DEBUG
 75193  75371     pC->seekOp = OP_Last;
 75194  75372   #endif
 75195  75373     if( pOp->p2>0 ){
 75196  75374       VdbeBranchTaken(res!=0,2);
 75197         -    if( res ) pc = pOp->p2 - 1;
        75375  +    if( res ) goto jump_to_p2;
 75198  75376     }
 75199  75377     break;
 75200  75378   }
 75201  75379   
 75202  75380   
 75203  75381   /* Opcode: Sort P1 P2 * * *
 75204  75382   **
................................................................................
 75254  75432       rc = sqlite3BtreeFirst(pCrsr, &res);
 75255  75433       pC->deferredMoveto = 0;
 75256  75434       pC->cacheStatus = CACHE_STALE;
 75257  75435     }
 75258  75436     pC->nullRow = (u8)res;
 75259  75437     assert( pOp->p2>0 && pOp->p2<p->nOp );
 75260  75438     VdbeBranchTaken(res!=0,2);
 75261         -  if( res ){
 75262         -    pc = pOp->p2 - 1;
 75263         -  }
        75439  +  if( res ) goto jump_to_p2;
 75264  75440     break;
 75265  75441   }
 75266  75442   
 75267  75443   /* Opcode: Next P1 P2 P3 P4 P5
 75268  75444   **
 75269  75445   ** Advance cursor P1 so that it points to the next key/data pair in its
 75270  75446   ** table or index.  If there are no more key/value pairs then fall through
................................................................................
 75367  75543   
 75368  75544     rc = pOp->p4.xAdvance(pC->pCursor, &res);
 75369  75545   next_tail:
 75370  75546     pC->cacheStatus = CACHE_STALE;
 75371  75547     VdbeBranchTaken(res==0,2);
 75372  75548     if( res==0 ){
 75373  75549       pC->nullRow = 0;
 75374         -    pc = pOp->p2 - 1;
 75375  75550       p->aCounter[pOp->p5]++;
 75376  75551   #ifdef SQLITE_TEST
 75377  75552       sqlite3_search_count++;
 75378  75553   #endif
        75554  +    goto jump_to_p2_and_check_for_interrupt;
 75379  75555     }else{
 75380  75556       pC->nullRow = 1;
 75381  75557     }
 75382  75558     goto check_for_interrupt;
 75383  75559   }
 75384  75560   
 75385  75561   /* Opcode: IdxInsert P1 P2 P3 * P5
................................................................................
 75479  75655   **
 75480  75656   ** Write into register P2 an integer which is the last entry in the record at
 75481  75657   ** the end of the index key pointed to by cursor P1.  This integer should be
 75482  75658   ** the rowid of the table entry to which this index entry points.
 75483  75659   **
 75484  75660   ** See also: Rowid, MakeRecord.
 75485  75661   */
 75486         -case OP_IdxRowid: {              /* out2-prerelease */
        75662  +case OP_IdxRowid: {              /* out2 */
 75487  75663     BtCursor *pCrsr;
 75488  75664     VdbeCursor *pC;
 75489  75665     i64 rowid;
 75490  75666   
        75667  +  pOut = out2Prerelease(p, pOp);
 75491  75668     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 75492  75669     pC = p->apCsr[pOp->p1];
 75493  75670     assert( pC!=0 );
 75494  75671     pCrsr = pC->pCursor;
 75495  75672     assert( pCrsr!=0 );
 75496  75673     pOut->flags = MEM_Null;
 75497  75674     assert( pC->isTable==0 );
................................................................................
 75596  75773       assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
 75597  75774       res = -res;
 75598  75775     }else{
 75599  75776       assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
 75600  75777       res++;
 75601  75778     }
 75602  75779     VdbeBranchTaken(res>0,2);
 75603         -  if( res>0 ){
 75604         -    pc = pOp->p2 - 1 ;
 75605         -  }
        75780  +  if( res>0 ) goto jump_to_p2;
 75606  75781     break;
 75607  75782   }
 75608  75783   
 75609  75784   /* Opcode: Destroy P1 P2 P3 * *
 75610  75785   **
 75611  75786   ** Delete an entire database table or index whose root page in the database
 75612  75787   ** file is given by P1.
................................................................................
 75622  75797   ** is stored in register P2.  If no page 
 75623  75798   ** movement was required (because the table being dropped was already 
 75624  75799   ** the last one in the database) then a zero is stored in register P2.
 75625  75800   ** If AUTOVACUUM is disabled then a zero is stored in register P2.
 75626  75801   **
 75627  75802   ** See also: Clear
 75628  75803   */
 75629         -case OP_Destroy: {     /* out2-prerelease */
        75804  +case OP_Destroy: {     /* out2 */
 75630  75805     int iMoved;
 75631  75806     int iDb;
 75632  75807   
 75633  75808     assert( p->readOnly==0 );
        75809  +  pOut = out2Prerelease(p, pOp);
 75634  75810     pOut->flags = MEM_Null;
 75635  75811     if( db->nVdbeRead > db->nVDestroy+1 ){
 75636  75812       rc = SQLITE_LOCKED;
 75637  75813       p->errorAction = OE_Abort;
 75638  75814     }else{
 75639  75815       iDb = pOp->p3;
 75640  75816       assert( DbMaskTest(p->btreeMask, iDb) );
................................................................................
 75735  75911   ** Allocate a new index in the main database file if P1==0 or in the
 75736  75912   ** auxiliary database file if P1==1 or in an attached database if
 75737  75913   ** P1>1.  Write the root page number of the new table into
 75738  75914   ** register P2.
 75739  75915   **
 75740  75916   ** See documentation on OP_CreateTable for additional information.
 75741  75917   */
 75742         -case OP_CreateIndex:            /* out2-prerelease */
 75743         -case OP_CreateTable: {          /* out2-prerelease */
        75918  +case OP_CreateIndex:            /* out2 */
        75919  +case OP_CreateTable: {          /* out2 */
 75744  75920     int pgno;
 75745  75921     int flags;
 75746  75922     Db *pDb;
 75747  75923   
        75924  +  pOut = out2Prerelease(p, pOp);
 75748  75925     pgno = 0;
 75749  75926     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 75750  75927     assert( DbMaskTest(p->btreeMask, pOp->p1) );
 75751  75928     assert( p->readOnly==0 );
 75752  75929     pDb = &db->aDb[pOp->p1];
 75753  75930     assert( pDb->pBt!=0 );
 75754  75931     if( pOp->opcode==OP_CreateTable ){
................................................................................
 75966  76143   
 75967  76144     pIn1 = &aMem[pOp->p1];
 75968  76145     if( (pIn1->flags & MEM_RowSet)==0 
 75969  76146      || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
 75970  76147     ){
 75971  76148       /* The boolean index is empty */
 75972  76149       sqlite3VdbeMemSetNull(pIn1);
 75973         -    pc = pOp->p2 - 1;
 75974  76150       VdbeBranchTaken(1,2);
        76151  +    goto jump_to_p2_and_check_for_interrupt;
 75975  76152     }else{
 75976  76153       /* A value was pulled from the index */
 75977         -    sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
 75978  76154       VdbeBranchTaken(0,2);
        76155  +    sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
 75979  76156     }
 75980  76157     goto check_for_interrupt;
 75981  76158   }
 75982  76159   
 75983  76160   /* Opcode: RowSetTest P1 P2 P3 P4
 75984  76161   ** Synopsis: if r[P3] in rowset(P1) goto P2
 75985  76162   **
................................................................................
 76022  76199     }
 76023  76200   
 76024  76201     assert( pOp->p4type==P4_INT32 );
 76025  76202     assert( iSet==-1 || iSet>=0 );
 76026  76203     if( iSet ){
 76027  76204       exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
 76028  76205       VdbeBranchTaken(exists!=0,2);
 76029         -    if( exists ){
 76030         -      pc = pOp->p2 - 1;
 76031         -      break;
 76032         -    }
        76206  +    if( exists ) goto jump_to_p2;
 76033  76207     }
 76034  76208     if( iSet>=0 ){
 76035  76209       sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
 76036  76210     }
 76037  76211     break;
 76038  76212   }
 76039  76213   
................................................................................
 76114  76288       sqlite3VdbeMemRelease(pRt);
 76115  76289       pRt->flags = MEM_Frame;
 76116  76290       pRt->u.pFrame = pFrame;
 76117  76291   
 76118  76292       pFrame->v = p;
 76119  76293       pFrame->nChildMem = nMem;
 76120  76294       pFrame->nChildCsr = pProgram->nCsr;
 76121         -    pFrame->pc = pc;
        76295  +    pFrame->pc = (int)(pOp - aOp);
 76122  76296       pFrame->aMem = p->aMem;
 76123  76297       pFrame->nMem = p->nMem;
 76124  76298       pFrame->apCsr = p->apCsr;
 76125  76299       pFrame->nCursor = p->nCursor;
 76126  76300       pFrame->aOp = p->aOp;
 76127  76301       pFrame->nOp = p->nOp;
 76128  76302       pFrame->token = pProgram->token;
................................................................................
 76137  76311         pMem->flags = MEM_Undefined;
 76138  76312         pMem->db = db;
 76139  76313       }
 76140  76314     }else{
 76141  76315       pFrame = pRt->u.pFrame;
 76142  76316       assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
 76143  76317       assert( pProgram->nCsr==pFrame->nChildCsr );
 76144         -    assert( pc==pFrame->pc );
        76318  +    assert( (int)(pOp - aOp)==pFrame->pc );
 76145  76319     }
 76146  76320   
 76147  76321     p->nFrame++;
 76148  76322     pFrame->pParent = p->pFrame;
 76149  76323     pFrame->lastRowid = lastRowid;
 76150  76324     pFrame->nChange = p->nChange;
 76151  76325     pFrame->nDbChange = p->db->nChange;
................................................................................
 76158  76332     p->aOp = aOp = pProgram->aOp;
 76159  76333     p->nOp = pProgram->nOp;
 76160  76334     p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
 76161  76335     p->nOnceFlag = pProgram->nOnce;
 76162  76336   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 76163  76337     p->anExec = 0;
 76164  76338   #endif
 76165         -  pc = -1;
        76339  +  pOp = &aOp[-1];
 76166  76340     memset(p->aOnceFlag, 0, p->nOnceFlag);
 76167  76341   
 76168  76342     break;
 76169  76343   }
 76170  76344   
 76171  76345   /* Opcode: Param P1 P2 * * *
 76172  76346   **
................................................................................
 76176  76350   ** address space. This is used by trigger programs to access the new.* 
 76177  76351   ** and old.* values.
 76178  76352   **
 76179  76353   ** The address of the cell in the parent frame is determined by adding
 76180  76354   ** the value of the P1 argument to the value of the P1 argument to the
 76181  76355   ** calling OP_Program instruction.
 76182  76356   */
 76183         -case OP_Param: {           /* out2-prerelease */
        76357  +case OP_Param: {           /* out2 */
 76184  76358     VdbeFrame *pFrame;
 76185  76359     Mem *pIn;
        76360  +  pOut = out2Prerelease(p, pOp);
 76186  76361     pFrame = p->pFrame;
 76187  76362     pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];   
 76188  76363     sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
 76189  76364     break;
 76190  76365   }
 76191  76366   
 76192  76367   #endif /* #ifndef SQLITE_OMIT_TRIGGER */
................................................................................
 76222  76397   ** is zero (the one that counts deferred constraint violations). If P1 is
 76223  76398   ** zero, the jump is taken if the statement constraint-counter is zero
 76224  76399   ** (immediate foreign key constraint violations).
 76225  76400   */
 76226  76401   case OP_FkIfZero: {         /* jump */
 76227  76402     if( pOp->p1 ){
 76228  76403       VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
 76229         -    if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
        76404  +    if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
 76230  76405     }else{
 76231  76406       VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
 76232         -    if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
        76407  +    if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
 76233  76408     }
 76234  76409     break;
 76235  76410   }
 76236  76411   #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
 76237  76412   
 76238  76413   #ifndef SQLITE_OMIT_AUTOINCREMENT
 76239  76414   /* Opcode: MemMax P1 P2 * * *
................................................................................
 76276  76451   ** If the initial value of register P1 is less than 1, then the
 76277  76452   ** value is unchanged and control passes through to the next instruction.
 76278  76453   */
 76279  76454   case OP_IfPos: {        /* jump, in1 */
 76280  76455     pIn1 = &aMem[pOp->p1];
 76281  76456     assert( pIn1->flags&MEM_Int );
 76282  76457     VdbeBranchTaken( pIn1->u.i>0, 2);
 76283         -  if( pIn1->u.i>0 ){
 76284         -     pc = pOp->p2 - 1;
 76285         -  }
        76458  +  if( pIn1->u.i>0 ) goto jump_to_p2;
 76286  76459     break;
 76287  76460   }
 76288  76461   
 76289  76462   /* Opcode: IfNeg P1 P2 P3 * *
 76290  76463   ** Synopsis: r[P1]+=P3, if r[P1]<0 goto P2
 76291  76464   **
 76292  76465   ** Register P1 must contain an integer.  Add literal P3 to the value in
................................................................................
 76293  76466   ** register P1 then if the value of register P1 is less than zero, jump to P2. 
 76294  76467   */
 76295  76468   case OP_IfNeg: {        /* jump, in1 */
 76296  76469     pIn1 = &aMem[pOp->p1];
 76297  76470     assert( pIn1->flags&MEM_Int );
 76298  76471     pIn1->u.i += pOp->p3;
 76299  76472     VdbeBranchTaken(pIn1->u.i<0, 2);
 76300         -  if( pIn1->u.i<0 ){
 76301         -     pc = pOp->p2 - 1;
 76302         -  }
        76473  +  if( pIn1->u.i<0 ) goto jump_to_p2;
 76303  76474     break;
 76304  76475   }
 76305  76476   
 76306  76477   /* Opcode: IfNotZero P1 P2 P3 * *
 76307  76478   ** Synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2
 76308  76479   **
 76309  76480   ** Register P1 must contain an integer.  If the content of register P1 is
................................................................................
 76312  76483   */
 76313  76484   case OP_IfNotZero: {        /* jump, in1 */
 76314  76485     pIn1 = &aMem[pOp->p1];
 76315  76486     assert( pIn1->flags&MEM_Int );
 76316  76487     VdbeBranchTaken(pIn1->u.i<0, 2);
 76317  76488     if( pIn1->u.i ){
 76318  76489        pIn1->u.i += pOp->p3;
 76319         -     pc = pOp->p2 - 1;
        76490  +     goto jump_to_p2;
 76320  76491     }
 76321  76492     break;
 76322  76493   }
 76323  76494   
 76324  76495   /* Opcode: DecrJumpZero P1 P2 * * *
 76325  76496   ** Synopsis: if (--r[P1])==0 goto P2
 76326  76497   **
................................................................................
 76328  76499   ** then jump to P2 if the new value is exactly zero.
 76329  76500   */
 76330  76501   case OP_DecrJumpZero: {      /* jump, in1 */
 76331  76502     pIn1 = &aMem[pOp->p1];
 76332  76503     assert( pIn1->flags&MEM_Int );
 76333  76504     pIn1->u.i--;
 76334  76505     VdbeBranchTaken(pIn1->u.i==0, 2);
 76335         -  if( pIn1->u.i==0 ){
 76336         -     pc = pOp->p2 - 1;
 76337         -  }
        76506  +  if( pIn1->u.i==0 ) goto jump_to_p2;
 76338  76507     break;
 76339  76508   }
 76340  76509   
 76341  76510   
 76342  76511   /* Opcode: JumpZeroIncr P1 P2 * * *
 76343  76512   ** Synopsis: if (r[P1]++)==0 ) goto P2
 76344  76513   **
................................................................................
 76346  76515   ** zero, then jump to P2.  Increment register P1 regardless of whether or
 76347  76516   ** not the jump is taken.
 76348  76517   */
 76349  76518   case OP_JumpZeroIncr: {        /* jump, in1 */
 76350  76519     pIn1 = &aMem[pOp->p1];
 76351  76520     assert( pIn1->flags&MEM_Int );
 76352  76521     VdbeBranchTaken(pIn1->u.i==0, 2);
 76353         -  if( (pIn1->u.i++)==0 ){
 76354         -     pc = pOp->p2 - 1;
 76355         -  }
        76522  +  if( (pIn1->u.i++)==0 ) goto jump_to_p2;
 76356  76523     break;
 76357  76524   }
 76358  76525   
 76359  76526   /* Opcode: AggStep * P2 P3 P4 P5
 76360  76527   ** Synopsis: accum=r[P3] step(r[P2@P5])
 76361  76528   **
 76362  76529   ** Execute the step function for an aggregate.  The
................................................................................
 76390  76557     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
 76391  76558     ctx.pMem = pMem = &aMem[pOp->p3];
 76392  76559     pMem->n++;
 76393  76560     sqlite3VdbeMemInit(&t, db, MEM_Null);
 76394  76561     ctx.pOut = &t;
 76395  76562     ctx.isError = 0;
 76396  76563     ctx.pVdbe = p;
 76397         -  ctx.iOp = pc;
        76564  +  ctx.iOp = (int)(pOp - aOp);
 76398  76565     ctx.skipFlag = 0;
 76399  76566     (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
 76400  76567     if( ctx.isError ){
 76401  76568       sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t));
 76402  76569       rc = ctx.isError;
 76403  76570     }
 76404  76571     if( ctx.skipFlag ){
................................................................................
 76485  76652   ** modes (delete, truncate, persist, off and memory), this is a simple
 76486  76653   ** operation. No IO is required.
 76487  76654   **
 76488  76655   ** If changing into or out of WAL mode the procedure is more complicated.
 76489  76656   **
 76490  76657   ** Write a string containing the final journal-mode to register P2.
 76491  76658   */
 76492         -case OP_JournalMode: {    /* out2-prerelease */
        76659  +case OP_JournalMode: {    /* out2 */
 76493  76660     Btree *pBt;                     /* Btree to change journal mode of */
 76494  76661     Pager *pPager;                  /* Pager associated with pBt */
 76495  76662     int eNew;                       /* New journal mode */
 76496  76663     int eOld;                       /* The old journal mode */
 76497  76664   #ifndef SQLITE_OMIT_WAL
 76498  76665     const char *zFilename;          /* Name of database file for pPager */
 76499  76666   #endif
 76500  76667   
        76668  +  pOut = out2Prerelease(p, pOp);
 76501  76669     eNew = pOp->p3;
 76502  76670     assert( eNew==PAGER_JOURNALMODE_DELETE 
 76503  76671          || eNew==PAGER_JOURNALMODE_TRUNCATE 
 76504  76672          || eNew==PAGER_JOURNALMODE_PERSIST 
 76505  76673          || eNew==PAGER_JOURNALMODE_OFF
 76506  76674          || eNew==PAGER_JOURNALMODE_MEMORY
 76507  76675          || eNew==PAGER_JOURNALMODE_WAL
................................................................................
 76569  76737   #endif /* ifndef SQLITE_OMIT_WAL */
 76570  76738   
 76571  76739     if( rc ){
 76572  76740       eNew = eOld;
 76573  76741     }
 76574  76742     eNew = sqlite3PagerSetJournalMode(pPager, eNew);
 76575  76743   
 76576         -  pOut = &aMem[pOp->p2];
 76577  76744     pOut->flags = MEM_Str|MEM_Static|MEM_Term;
 76578  76745     pOut->z = (char *)sqlite3JournalModename(eNew);
 76579  76746     pOut->n = sqlite3Strlen30(pOut->z);
 76580  76747     pOut->enc = SQLITE_UTF8;
 76581  76748     sqlite3VdbeChangeEncoding(pOut, encoding);
 76582  76749     break;
 76583  76750   };
................................................................................
 76610  76777     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 76611  76778     assert( DbMaskTest(p->btreeMask, pOp->p1) );
 76612  76779     assert( p->readOnly==0 );
 76613  76780     pBt = db->aDb[pOp->p1].pBt;
 76614  76781     rc = sqlite3BtreeIncrVacuum(pBt);
 76615  76782     VdbeBranchTaken(rc==SQLITE_DONE,2);
 76616  76783     if( rc==SQLITE_DONE ){
 76617         -    pc = pOp->p2 - 1;
 76618  76784       rc = SQLITE_OK;
        76785  +    goto jump_to_p2;
 76619  76786     }
 76620  76787     break;
 76621  76788   }
 76622  76789   #endif
 76623  76790   
 76624  76791   /* Opcode: Expire P1 * * * *
 76625  76792   **
................................................................................
 76764  76931   
 76765  76932       /* Initialize vdbe cursor object */
 76766  76933       pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
 76767  76934       if( pCur ){
 76768  76935         pCur->pVtabCursor = pVtabCursor;
 76769  76936         pVtab->nRef++;
 76770  76937       }else{
 76771         -      db->mallocFailed = 1;
        76938  +      assert( db->mallocFailed );
 76772  76939         pModule->xClose(pVtabCursor);
        76940  +      goto no_mem;
 76773  76941       }
 76774  76942     }
 76775  76943     break;
 76776  76944   }
 76777  76945   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 76778  76946   
 76779  76947   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
 76821  76989   
 76822  76990     /* Grab the index number and argc parameters */
 76823  76991     assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
 76824  76992     nArg = (int)pArgc->u.i;
 76825  76993     iQuery = (int)pQuery->u.i;
 76826  76994   
 76827  76995     /* Invoke the xFilter method */
 76828         -  {
 76829         -    res = 0;
 76830         -    apArg = p->apArg;
 76831         -    for(i = 0; i<nArg; i++){
 76832         -      apArg[i] = &pArgc[i+1];
 76833         -    }
 76834         -
 76835         -    rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
 76836         -    sqlite3VtabImportErrmsg(p, pVtab);
 76837         -    if( rc==SQLITE_OK ){
 76838         -      res = pModule->xEof(pVtabCursor);
 76839         -    }
 76840         -    VdbeBranchTaken(res!=0,2);
 76841         -    if( res ){
 76842         -      pc = pOp->p2 - 1;
 76843         -    }
        76996  +  res = 0;
        76997  +  apArg = p->apArg;
        76998  +  for(i = 0; i<nArg; i++){
        76999  +    apArg[i] = &pArgc[i+1];
        77000  +  }
        77001  +  rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
        77002  +  sqlite3VtabImportErrmsg(p, pVtab);
        77003  +  if( rc==SQLITE_OK ){
        77004  +    res = pModule->xEof(pVtabCursor);
 76844  77005     }
 76845  77006     pCur->nullRow = 0;
 76846         -
        77007  +  VdbeBranchTaken(res!=0,2);
        77008  +  if( res ) goto jump_to_p2;
 76847  77009     break;
 76848  77010   }
 76849  77011   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 76850  77012   
 76851  77013   #ifndef SQLITE_OMIT_VIRTUALTABLE
 76852  77014   /* Opcode: VColumn P1 P2 P3 * *
 76853  77015   ** Synopsis: r[P3]=vcolumn(P2)
................................................................................
 76926  77088     sqlite3VtabImportErrmsg(p, pVtab);
 76927  77089     if( rc==SQLITE_OK ){
 76928  77090       res = pModule->xEof(pCur->pVtabCursor);
 76929  77091     }
 76930  77092     VdbeBranchTaken(!res,2);
 76931  77093     if( !res ){
 76932  77094       /* If there is data, jump to P2 */
 76933         -    pc = pOp->p2 - 1;
        77095  +    goto jump_to_p2_and_check_for_interrupt;
 76934  77096     }
 76935  77097     goto check_for_interrupt;
 76936  77098   }
 76937  77099   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 76938  77100   
 76939  77101   #ifndef SQLITE_OMIT_VIRTUALTABLE
 76940  77102   /* Opcode: VRename P1 * * P4 *
................................................................................
 77049  77211   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 77050  77212   
 77051  77213   #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
 77052  77214   /* Opcode: Pagecount P1 P2 * * *
 77053  77215   **
 77054  77216   ** Write the current number of pages in database P1 to memory cell P2.
 77055  77217   */
 77056         -case OP_Pagecount: {            /* out2-prerelease */
        77218  +case OP_Pagecount: {            /* out2 */
        77219  +  pOut = out2Prerelease(p, pOp);
 77057  77220     pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
 77058  77221     break;
 77059  77222   }
 77060  77223   #endif
 77061  77224   
 77062  77225   
 77063  77226   #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
................................................................................
 77065  77228   **
 77066  77229   ** Try to set the maximum page count for database P1 to the value in P3.
 77067  77230   ** Do not let the maximum page count fall below the current page count and
 77068  77231   ** do not change the maximum page count value if P3==0.
 77069  77232   **
 77070  77233   ** Store the maximum page count after the change in register P2.
 77071  77234   */
 77072         -case OP_MaxPgcnt: {            /* out2-prerelease */
        77235  +case OP_MaxPgcnt: {            /* out2 */
 77073  77236     unsigned int newMax;
 77074  77237     Btree *pBt;
 77075  77238   
        77239  +  pOut = out2Prerelease(p, pOp);
 77076  77240     pBt = db->aDb[pOp->p1].pBt;
 77077  77241     newMax = 0;
 77078  77242     if( pOp->p3 ){
 77079  77243       newMax = sqlite3BtreeLastPage(pBt);
 77080  77244       if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
 77081  77245     }
 77082  77246     pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
................................................................................
 77097  77261   **
 77098  77262   ** If P2 is not zero, jump to instruction P2.
 77099  77263   */
 77100  77264   case OP_Init: {          /* jump */
 77101  77265     char *zTrace;
 77102  77266     char *z;
 77103  77267   
 77104         -  if( pOp->p2 ){
 77105         -    pc = pOp->p2 - 1;
 77106         -  }
 77107  77268   #ifndef SQLITE_OMIT_TRACE
 77108  77269     if( db->xTrace
 77109  77270      && !p->doingRerun
 77110  77271      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 77111  77272     ){
 77112  77273       z = sqlite3VdbeExpandSql(p, zTrace);
 77113  77274       db->xTrace(db->pTraceArg, z);
................................................................................
 77127  77288     if( (db->flags & SQLITE_SqlTrace)!=0
 77128  77289      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 77129  77290     ){
 77130  77291       sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
 77131  77292     }
 77132  77293   #endif /* SQLITE_DEBUG */
 77133  77294   #endif /* SQLITE_OMIT_TRACE */
        77295  +  if( pOp->p2 ) goto jump_to_p2;
 77134  77296     break;
 77135  77297   }
 77136  77298   
 77137  77299   
 77138  77300   /* Opcode: Noop * * * * *
 77139  77301   **
 77140  77302   ** Do nothing.  This instruction is often useful as a jump
................................................................................
 77158  77320   ** restored.
 77159  77321   *****************************************************************************/
 77160  77322       }
 77161  77323   
 77162  77324   #ifdef VDBE_PROFILE
 77163  77325       {
 77164  77326         u64 endTime = sqlite3Hwtime();
 77165         -      if( endTime>start ) pOp->cycles += endTime - start;
 77166         -      pOp->cnt++;
        77327  +      if( endTime>start ) pOrigOp->cycles += endTime - start;
        77328  +      pOrigOp->cnt++;
 77167  77329       }
 77168  77330   #endif
 77169  77331   
 77170  77332       /* The following code adds nothing to the actual functionality
 77171  77333       ** of the program.  It is only here for testing and debugging.
 77172  77334       ** On the other hand, it does burn CPU cycles every time through
 77173  77335       ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
 77174  77336       */
 77175  77337   #ifndef NDEBUG
 77176         -    assert( pc>=-1 && pc<p->nOp );
        77338  +    assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
 77177  77339   
 77178  77340   #ifdef SQLITE_DEBUG
 77179  77341       if( db->flags & SQLITE_VdbeTrace ){
 77180  77342         if( rc!=0 ) printf("rc=%d\n",rc);
 77181         -      if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
 77182         -        registerTrace(pOp->p2, &aMem[pOp->p2]);
        77343  +      if( pOrigOp->opflags & (OPFLG_OUT2) ){
        77344  +        registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
 77183  77345         }
 77184         -      if( pOp->opflags & OPFLG_OUT3 ){
 77185         -        registerTrace(pOp->p3, &aMem[pOp->p3]);
        77346  +      if( pOrigOp->opflags & OPFLG_OUT3 ){
        77347  +        registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
 77186  77348         }
 77187  77349       }
 77188  77350   #endif  /* SQLITE_DEBUG */
 77189  77351   #endif  /* NDEBUG */
 77190  77352     }  /* The end of the for(;;) loop the loops through opcodes */
 77191  77353   
 77192  77354     /* If we reach this point, it means that execution is finished with
................................................................................
 77193  77355     ** an error of some kind.
 77194  77356     */
 77195  77357   vdbe_error_halt:
 77196  77358     assert( rc );
 77197  77359     p->rc = rc;
 77198  77360     testcase( sqlite3GlobalConfig.xLog!=0 );
 77199  77361     sqlite3_log(rc, "statement aborts at %d: [%s] %s", 
 77200         -                   pc, p->zSql, p->zErrMsg);
        77362  +                   (int)(pOp - aOp), p->zSql, p->zErrMsg);
 77201  77363     sqlite3VdbeHalt(p);
 77202  77364     if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
 77203  77365     rc = SQLITE_ERROR;
 77204  77366     if( resetSchemaOnFault>0 ){
 77205  77367       sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
 77206  77368     }
 77207  77369   
................................................................................
 78019  78181   **      to sqlite3ThreadJoin() is likely to block. Cases that are likely to
 78020  78182   **      block provoke debugging output.
 78021  78183   **
 78022  78184   ** In both cases, the effects of the main thread seeing (bDone==0) even
 78023  78185   ** after the thread has finished are not dire. So we don't worry about
 78024  78186   ** memory barriers and such here.
 78025  78187   */
        78188  +typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
 78026  78189   struct SortSubtask {
 78027  78190     SQLiteThread *pThread;          /* Background thread, if any */
 78028  78191     int bDone;                      /* Set if thread is finished but not joined */
 78029  78192     VdbeSorter *pSorter;            /* Sorter that owns this sub-task */
 78030  78193     UnpackedRecord *pUnpacked;      /* Space to unpack a record */
 78031  78194     SorterList list;                /* List for thread to write to a PMA */
 78032  78195     int nPMA;                       /* Number of PMAs currently in file */
        78196  +  SorterCompare xCompare;         /* Compare function to use */
 78033  78197     SorterFile file;                /* Temp file for level-0 PMAs */
 78034  78198     SorterFile file2;               /* Space for other PMAs */
 78035  78199   };
        78200  +
 78036  78201   
 78037  78202   /*
 78038  78203   ** Main sorter structure. A single instance of this is allocated for each 
 78039  78204   ** sorter cursor created by the VDBE.
 78040  78205   **
 78041  78206   ** mxKeysize:
 78042  78207   **   As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
................................................................................
 78056  78221     SorterList list;                /* List of in-memory records */
 78057  78222     int iMemory;                    /* Offset of free space in list.aMemory */
 78058  78223     int nMemory;                    /* Size of list.aMemory allocation in bytes */
 78059  78224     u8 bUsePMA;                     /* True if one or more PMAs created */
 78060  78225     u8 bUseThreads;                 /* True to use background threads */
 78061  78226     u8 iPrev;                       /* Previous thread used to flush PMA */
 78062  78227     u8 nTask;                       /* Size of aTask[] array */
        78228  +  u8 typeMask;
 78063  78229     SortSubtask aTask[1];           /* One or more subtasks */
 78064  78230   };
        78231  +
        78232  +#define SORTER_TYPE_INTEGER 0x01
        78233  +#define SORTER_TYPE_TEXT    0x02
 78065  78234   
 78066  78235   /*
 78067  78236   ** An instance of the following object is used to read records out of a
 78068  78237   ** PMA, in sorted order.  The next key to be read is cached in nKey/aKey.
 78069  78238   ** aKey might point into aMap or into aBuffer.  If neither of those locations
 78070  78239   ** contain a contiguous representation of the key, then aAlloc is allocated
 78071  78240   ** and the key is copied into aAlloc and aKey is made to poitn to aAlloc.
................................................................................
 78470  78639   
 78471  78640     if( rc==SQLITE_OK ){
 78472  78641       rc = vdbePmaReaderNext(pReadr);
 78473  78642     }
 78474  78643     return rc;
 78475  78644   }
 78476  78645   
        78646  +/*
        78647  +** A version of vdbeSorterCompare() that assumes that it has already been
        78648  +** determined that the first field of key1 is equal to the first field of 
        78649  +** key2.
        78650  +*/
        78651  +static int vdbeSorterCompareTail(
        78652  +  SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
        78653  +  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
        78654  +  const void *pKey1, int nKey1,   /* Left side of comparison */
        78655  +  const void *pKey2, int nKey2    /* Right side of comparison */
        78656  +){
        78657  +  UnpackedRecord *r2 = pTask->pUnpacked;
        78658  +  if( *pbKey2Cached==0 ){
        78659  +    sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
        78660  +    *pbKey2Cached = 1;
        78661  +  }
        78662  +  return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1);
        78663  +}
 78477  78664   
 78478  78665   /*
 78479  78666   ** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, 
 78480  78667   ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
 78481  78668   ** used by the comparison. Return the result of the comparison.
 78482  78669   **
 78483         -** Before returning, object (pTask->pUnpacked) is populated with the
 78484         -** unpacked version of key2. Or, if pKey2 is passed a NULL pointer, then it 
 78485         -** is assumed that the (pTask->pUnpacked) structure already contains the 
 78486         -** unpacked key to use as key2.
        78670  +** If IN/OUT parameter *pbKey2Cached is true when this function is called,
        78671  +** it is assumed that (pTask->pUnpacked) contains the unpacked version
        78672  +** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
        78673  +** version of key2 and *pbKey2Cached set to true before returning.
 78487  78674   **
 78488  78675   ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
 78489  78676   ** to SQLITE_NOMEM.
 78490  78677   */
 78491  78678   static int vdbeSorterCompare(
 78492  78679     SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
        78680  +  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
 78493  78681     const void *pKey1, int nKey1,   /* Left side of comparison */
 78494  78682     const void *pKey2, int nKey2    /* Right side of comparison */
 78495  78683   ){
 78496  78684     UnpackedRecord *r2 = pTask->pUnpacked;
 78497         -  if( pKey2 ){
        78685  +  if( !*pbKey2Cached ){
 78498  78686       sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
        78687  +    *pbKey2Cached = 1;
 78499  78688     }
 78500  78689     return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
 78501  78690   }
        78691  +
        78692  +/*
        78693  +** A specially optimized version of vdbeSorterCompare() that assumes that
        78694  +** the first field of each key is a TEXT value and that the collation
        78695  +** sequence to compare them with is BINARY.
        78696  +*/
        78697  +static int vdbeSorterCompareText(
        78698  +  SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
        78699  +  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
        78700  +  const void *pKey1, int nKey1,   /* Left side of comparison */
        78701  +  const void *pKey2, int nKey2    /* Right side of comparison */
        78702  +){
        78703  +  const u8 * const p1 = (const u8 * const)pKey1;
        78704  +  const u8 * const p2 = (const u8 * const)pKey2;
        78705  +  const u8 * const v1 = &p1[ p1[0] ];   /* Pointer to value 1 */
        78706  +  const u8 * const v2 = &p2[ p2[0] ];   /* Pointer to value 2 */
        78707  +
        78708  +  int n1;
        78709  +  int n2;
        78710  +  int res;
        78711  +
        78712  +  getVarint32(&p1[1], n1); n1 = (n1 - 13) / 2;
        78713  +  getVarint32(&p2[1], n2); n2 = (n2 - 13) / 2;
        78714  +  res = memcmp(v1, v2, MIN(n1, n2));
        78715  +  if( res==0 ){
        78716  +    res = n1 - n2;
        78717  +  }
        78718  +
        78719  +  if( res==0 ){
        78720  +    if( pTask->pSorter->pKeyInfo->nField>1 ){
        78721  +      res = vdbeSorterCompareTail(
        78722  +          pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
        78723  +      );
        78724  +    }
        78725  +  }else{
        78726  +    if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
        78727  +      res = res * -1;
        78728  +    }
        78729  +  }
        78730  +
        78731  +  return res;
        78732  +}
        78733  +
        78734  +/*
        78735  +** A specially optimized version of vdbeSorterCompare() that assumes that
        78736  +** the first field of each key is an INTEGER value.
        78737  +*/
        78738  +static int vdbeSorterCompareInt(
        78739  +  SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
        78740  +  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
        78741  +  const void *pKey1, int nKey1,   /* Left side of comparison */
        78742  +  const void *pKey2, int nKey2    /* Right side of comparison */
        78743  +){
        78744  +  const u8 * const p1 = (const u8 * const)pKey1;
        78745  +  const u8 * const p2 = (const u8 * const)pKey2;
        78746  +  const int s1 = p1[1];                 /* Left hand serial type */
        78747  +  const int s2 = p2[1];                 /* Right hand serial type */
        78748  +  const u8 * const v1 = &p1[ p1[0] ];   /* Pointer to value 1 */
        78749  +  const u8 * const v2 = &p2[ p2[0] ];   /* Pointer to value 2 */
        78750  +  int res;                              /* Return value */
        78751  +
        78752  +  assert( (s1>0 && s1<7) || s1==8 || s1==9 );
        78753  +  assert( (s2>0 && s2<7) || s2==8 || s2==9 );
        78754  +
        78755  +  if( s1>7 && s2>7 ){
        78756  +    res = s1 - s2;
        78757  +  }else{
        78758  +    if( s1==s2 ){
        78759  +      if( (*v1 ^ *v2) & 0x80 ){
        78760  +        /* The two values have different signs */
        78761  +        res = (*v1 & 0x80) ? -1 : +1;
        78762  +      }else{
        78763  +        /* The two values have the same sign. Compare using memcmp(). */
        78764  +        static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8 };
        78765  +        int i;
        78766  +        res = 0;
        78767  +        for(i=0; i<aLen[s1]; i++){
        78768  +          if( (res = v1[i] - v2[i]) ) break;
        78769  +        }
        78770  +      }
        78771  +    }else{
        78772  +      if( s2>7 ){
        78773  +        res = +1;
        78774  +      }else if( s1>7 ){
        78775  +        res = -1;
        78776  +      }else{
        78777  +        res = s1 - s2;
        78778  +      }
        78779  +      assert( res!=0 );
        78780  +
        78781  +      if( res>0 ){
        78782  +        if( *v1 & 0x80 ) res = -1;
        78783  +      }else{
        78784  +        if( *v2 & 0x80 ) res = +1;
        78785  +      }
        78786  +    }
        78787  +  }
        78788  +
        78789  +  if( res==0 ){
        78790  +    if( pTask->pSorter->pKeyInfo->nField>1 ){
        78791  +      res = vdbeSorterCompareTail(
        78792  +          pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
        78793  +      );
        78794  +    }
        78795  +  }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
        78796  +    res = res * -1;
        78797  +  }
        78798  +
        78799  +  return res;
        78800  +}
 78502  78801   
 78503  78802   /*
 78504  78803   ** Initialize the temporary index cursor just opened as a sorter cursor.
 78505  78804   **
 78506  78805   ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nField)
 78507  78806   ** to determine the number of fields that should be compared from the
 78508  78807   ** records being sorted. However, if the value passed as argument nField
................................................................................
 78563  78862     pCsr->pSorter = pSorter;
 78564  78863     if( pSorter==0 ){
 78565  78864       rc = SQLITE_NOMEM;
 78566  78865     }else{
 78567  78866       pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
 78568  78867       memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
 78569  78868       pKeyInfo->db = 0;
 78570         -    if( nField && nWorker==0 ) pKeyInfo->nField = nField;
        78869  +    if( nField && nWorker==0 ){
        78870  +      pKeyInfo->nXField += (pKeyInfo->nField - nField);
        78871  +      pKeyInfo->nField = nField;
        78872  +    }
 78571  78873       pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
 78572  78874       pSorter->nTask = nWorker + 1;
        78875  +    pSorter->iPrev = nWorker-1;
 78573  78876       pSorter->bUseThreads = (pSorter->nTask>1);
 78574  78877       pSorter->db = db;
 78575  78878       for(i=0; i<pSorter->nTask; i++){
 78576  78879         SortSubtask *pTask = &pSorter->aTask[i];
 78577  78880         pTask->pSorter = pSorter;
 78578  78881       }
 78579  78882   
................................................................................
 78591  78894         if( sqlite3GlobalConfig.pScratch==0 ){
 78592  78895           assert( pSorter->iMemory==0 );
 78593  78896           pSorter->nMemory = pgsz;
 78594  78897           pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
 78595  78898           if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM;
 78596  78899         }
 78597  78900       }
        78901  +
        78902  +    if( (pKeyInfo->nField+pKeyInfo->nXField)<13 
        78903  +     && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
        78904  +    ){
        78905  +      pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
        78906  +    }
 78598  78907     }
 78599  78908   
 78600  78909     return rc;
 78601  78910   }
 78602  78911   #undef nWorker   /* Defined at the top of this function */
 78603  78912   
 78604  78913   /*
................................................................................
 78615  78924   
 78616  78925   /*
 78617  78926   ** Free all resources owned by the object indicated by argument pTask. All 
 78618  78927   ** fields of *pTask are zeroed before returning.
 78619  78928   */
 78620  78929   static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
 78621  78930     sqlite3DbFree(db, pTask->pUnpacked);
 78622         -  pTask->pUnpacked = 0;
 78623  78931   #if SQLITE_MAX_WORKER_THREADS>0
 78624  78932     /* pTask->list.aMemory can only be non-zero if it was handed memory
 78625  78933     ** from the main thread.  That only occurs SQLITE_MAX_WORKER_THREADS>0 */
 78626  78934     if( pTask->list.aMemory ){
 78627  78935       sqlite3_free(pTask->list.aMemory);
 78628         -    pTask->list.aMemory = 0;
 78629  78936     }else
 78630  78937   #endif
 78631  78938     {
 78632  78939       assert( pTask->list.aMemory==0 );
 78633  78940       vdbeSorterRecordFree(0, pTask->list.pList);
 78634  78941     }
 78635         -  pTask->list.pList = 0;
 78636  78942     if( pTask->file.pFd ){
 78637  78943       sqlite3OsCloseFree(pTask->file.pFd);
 78638         -    pTask->file.pFd = 0;
 78639         -    pTask->file.iEof = 0;
 78640  78944     }
 78641  78945     if( pTask->file2.pFd ){
 78642  78946       sqlite3OsCloseFree(pTask->file2.pFd);
 78643         -    pTask->file2.pFd = 0;
 78644         -    pTask->file2.iEof = 0;
 78645  78947     }
        78948  +  memset(pTask, 0, sizeof(SortSubtask));
 78646  78949   }
 78647  78950   
 78648  78951   #ifdef SQLITE_DEBUG_SORTER_THREADS
 78649  78952   static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
 78650  78953     i64 t;
 78651  78954     int iTask = (pTask - pTask->pSorter->aTask);
 78652  78955     sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
................................................................................
 78818  79121     }
 78819  79122   #endif
 78820  79123     vdbeMergeEngineFree(pSorter->pMerger);
 78821  79124     pSorter->pMerger = 0;
 78822  79125     for(i=0; i<pSorter->nTask; i++){
 78823  79126       SortSubtask *pTask = &pSorter->aTask[i];
 78824  79127       vdbeSortSubtaskCleanup(db, pTask);
        79128  +    pTask->pSorter = pSorter;
 78825  79129     }
 78826  79130     if( pSorter->list.aMemory==0 ){
 78827  79131       vdbeSorterRecordFree(0, pSorter->list.pList);
 78828  79132     }
 78829  79133     pSorter->list.pList = 0;
 78830  79134     pSorter->list.szPMA = 0;
 78831  79135     pSorter->bUsePMA = 0;
................................................................................
 78927  79231     SortSubtask *pTask,             /* Calling thread context */
 78928  79232     SorterRecord *p1,               /* First list to merge */
 78929  79233     SorterRecord *p2,               /* Second list to merge */
 78930  79234     SorterRecord **ppOut            /* OUT: Head of merged list */
 78931  79235   ){
 78932  79236     SorterRecord *pFinal = 0;
 78933  79237     SorterRecord **pp = &pFinal;
 78934         -  void *pVal2 = p2 ? SRVAL(p2) : 0;
        79238  +  int bCached = 0;
 78935  79239   
 78936  79240     while( p1 && p2 ){
 78937  79241       int res;
 78938         -    res = vdbeSorterCompare(pTask, SRVAL(p1), p1->nVal, pVal2, p2->nVal);
        79242  +    res = pTask->xCompare(
        79243  +        pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
        79244  +    );
        79245  +
 78939  79246       if( res<=0 ){
 78940  79247         *pp = p1;
 78941  79248         pp = &p1->u.pNext;
 78942  79249         p1 = p1->u.pNext;
 78943         -      pVal2 = 0;
 78944  79250       }else{
 78945  79251         *pp = p2;
 78946         -       pp = &p2->u.pNext;
        79252  +      pp = &p2->u.pNext;
 78947  79253         p2 = p2->u.pNext;
 78948         -      if( p2==0 ) break;
 78949         -      pVal2 = SRVAL(p2);
        79254  +      bCached = 0;
 78950  79255       }
 78951  79256     }
 78952  79257     *pp = p1 ? p1 : p2;
 78953  79258     *ppOut = pFinal;
 78954  79259   }
        79260  +
        79261  +/*
        79262  +** Return the SorterCompare function to compare values collected by the
        79263  +** sorter object passed as the only argument.
        79264  +*/
        79265  +static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
        79266  +  if( p->typeMask==SORTER_TYPE_INTEGER ){
        79267  +    return vdbeSorterCompareInt;
        79268  +  }else if( p->typeMask==SORTER_TYPE_TEXT ){
        79269  +    return vdbeSorterCompareText; 
        79270  +  }
        79271  +  return vdbeSorterCompare;
        79272  +}
 78955  79273   
 78956  79274   /*
 78957  79275   ** Sort the linked list of records headed at pTask->pList. Return 
 78958  79276   ** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if 
 78959  79277   ** an error occurs.
 78960  79278   */
 78961  79279   static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
................................................................................
 78963  79281     SorterRecord **aSlot;
 78964  79282     SorterRecord *p;
 78965  79283     int rc;
 78966  79284   
 78967  79285     rc = vdbeSortAllocUnpacked(pTask);
 78968  79286     if( rc!=SQLITE_OK ) return rc;
 78969  79287   
        79288  +  p = pList->pList;
        79289  +  pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
        79290  +
 78970  79291     aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
 78971  79292     if( !aSlot ){
 78972  79293       return SQLITE_NOMEM;
 78973  79294     }
 78974  79295   
 78975         -  p = pList->pList;
 78976  79296     while( p ){
 78977  79297       SorterRecord *pNext;
 78978  79298       if( pList->aMemory ){
 78979  79299         if( (u8*)p==pList->aMemory ){
 78980  79300           pNext = 0;
 78981  79301         }else{
 78982  79302           assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
................................................................................
 79182  79502     rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
 79183  79503   
 79184  79504     /* Update contents of aTree[] */
 79185  79505     if( rc==SQLITE_OK ){
 79186  79506       int i;                      /* Index of aTree[] to recalculate */
 79187  79507       PmaReader *pReadr1;         /* First PmaReader to compare */
 79188  79508       PmaReader *pReadr2;         /* Second PmaReader to compare */
 79189         -    u8 *pKey2;                  /* To pReadr2->aKey, or 0 if record cached */
        79509  +    int bCached = 0;
 79190  79510   
 79191  79511       /* Find the first two PmaReaders to compare. The one that was just
 79192  79512       ** advanced (iPrev) and the one next to it in the array.  */
 79193  79513       pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
 79194  79514       pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
 79195         -    pKey2 = pReadr2->aKey;
 79196  79515   
 79197  79516       for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
 79198  79517         /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
 79199  79518         int iRes;
 79200  79519         if( pReadr1->pFd==0 ){
 79201  79520           iRes = +1;
 79202  79521         }else if( pReadr2->pFd==0 ){
 79203  79522           iRes = -1;
 79204  79523         }else{
 79205         -        iRes = vdbeSorterCompare(pTask, 
 79206         -            pReadr1->aKey, pReadr1->nKey, pKey2, pReadr2->nKey
        79524  +        iRes = pTask->xCompare(pTask, &bCached,
        79525  +            pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
 79207  79526           );
 79208  79527         }
 79209  79528   
 79210  79529         /* If pReadr1 contained the smaller value, set aTree[i] to its index.
 79211  79530         ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
 79212  79531         ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
 79213  79532         ** pKey2 to point to the record belonging to pReadr2.
................................................................................
 79221  79540         ** If the two values were equal, then the value from the oldest
 79222  79541         ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
 79223  79542         ** is sorted from oldest to newest, so pReadr1 contains older values
 79224  79543         ** than pReadr2 iff (pReadr1<pReadr2).  */
 79225  79544         if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
 79226  79545           pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
 79227  79546           pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
 79228         -        pKey2 = pReadr2->aKey;
        79547  +        bCached = 0;
 79229  79548         }else{
 79230         -        if( pReadr1->pFd ) pKey2 = 0;
        79549  +        if( pReadr1->pFd ) bCached = 0;
 79231  79550           pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
 79232  79551           pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
 79233  79552         }
 79234  79553       }
 79235  79554       *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
 79236  79555     }
 79237  79556   
................................................................................
 79330  79649     VdbeSorter *pSorter = pCsr->pSorter;
 79331  79650     int rc = SQLITE_OK;             /* Return Code */
 79332  79651     SorterRecord *pNew;             /* New list element */
 79333  79652   
 79334  79653     int bFlush;                     /* True to flush contents of memory to PMA */
 79335  79654     int nReq;                       /* Bytes of memory required */
 79336  79655     int nPMA;                       /* Bytes of PMA space required */
        79656  +  int t;                          /* serial type of first record field */
        79657  +
        79658  +  getVarint32((const u8*)&pVal->z[1], t);
        79659  +  if( t>0 && t<10 && t!=7 ){
        79660  +    pSorter->typeMask &= SORTER_TYPE_INTEGER;
        79661  +  }else if( t>10 && (t & 0x01) ){
        79662  +    pSorter->typeMask &= SORTER_TYPE_TEXT;
        79663  +  }else{
        79664  +    pSorter->typeMask = 0;
        79665  +  }
 79337  79666   
 79338  79667     assert( pSorter );
 79339  79668   
 79340  79669     /* Figure out whether or not the current contents of memory should be
 79341  79670     ** flushed to a PMA before continuing. If so, do so.
 79342  79671     **
 79343  79672     ** If using the single large allocation mode (pSorter->aMemory!=0), then
................................................................................
 79595  79924     p2 = &pMerger->aReadr[i2];
 79596  79925   
 79597  79926     if( p1->pFd==0 ){
 79598  79927       iRes = i2;
 79599  79928     }else if( p2->pFd==0 ){
 79600  79929       iRes = i1;
 79601  79930     }else{
        79931  +    SortSubtask *pTask = pMerger->pTask;
        79932  +    int bCached = 0;
 79602  79933       int res;
 79603         -    assert( pMerger->pTask->pUnpacked!=0 );  /* from vdbeSortSubtaskMain() */
 79604         -    res = vdbeSorterCompare(
 79605         -        pMerger->pTask, p1->aKey, p1->nKey, p2->aKey, p2->nKey
        79934  +    assert( pTask->pUnpacked!=0 );  /* from vdbeSortSubtaskMain() */
        79935  +    res = pTask->xCompare(
        79936  +        pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
 79606  79937       );
 79607  79938       if( res<=0 ){
 79608  79939         iRes = i1;
 79609  79940       }else{
 79610  79941         iRes = i2;
 79611  79942       }
 79612  79943     }
................................................................................
 79622  79953   ** SQLITE_MAX_WORKER_THREADS==0).  The other values are only used
 79623  79954   ** when there exists one or more separate worker threads.
 79624  79955   */
 79625  79956   #define INCRINIT_NORMAL 0
 79626  79957   #define INCRINIT_TASK   1
 79627  79958   #define INCRINIT_ROOT   2
 79628  79959   
 79629         -/* Forward reference.
 79630         -** The vdbeIncrMergeInit() and vdbePmaReaderIncrMergeInit() routines call each
 79631         -** other (when building a merge tree).
        79960  +/* 
        79961  +** Forward reference required as the vdbeIncrMergeInit() and
        79962  +** vdbePmaReaderIncrInit() routines are called mutually recursively when
        79963  +** building a merge tree.
 79632  79964   */
 79633         -static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode);
        79965  +static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
 79634  79966   
 79635  79967   /*
 79636  79968   ** Initialize the MergeEngine object passed as the second argument. Once this
 79637  79969   ** function returns, the first key of merged data may be read from the 
 79638  79970   ** MergeEngine object in the usual fashion.
 79639  79971   **
 79640  79972   ** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
................................................................................
 79673  80005         ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
 79674  80006         ** in use it will block the vdbePmaReaderNext() call while it uses
 79675  80007         ** the main thread to fill its buffer. So calling PmaReaderNext()
 79676  80008         ** on this PmaReader before any of the multi-threaded PmaReaders takes
 79677  80009         ** better advantage of multi-processor hardware. */
 79678  80010         rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
 79679  80011       }else{
 79680         -      rc = vdbePmaReaderIncrMergeInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
        80012  +      rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
 79681  80013       }
 79682  80014       if( rc!=SQLITE_OK ) return rc;
 79683  80015     }
 79684  80016   
 79685  80017     for(i=pMerger->nTree-1; i>0; i--){
 79686  80018       vdbeMergeEngineCompare(pMerger, i);
 79687  80019     }
 79688  80020     return pTask->pUnpacked->errCode;
 79689  80021   }
 79690  80022   
 79691  80023   /*
 79692         -** Initialize the IncrMerge field of a PmaReader.
 79693         -**
 79694         -** If the PmaReader passed as the first argument is not an incremental-reader
 79695         -** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it serves
 79696         -** to open and/or initialize the temp file related fields of the IncrMerge
        80024  +** The PmaReader passed as the first argument is guaranteed to be an
        80025  +** incremental-reader (pReadr->pIncr!=0). This function serves to open
        80026  +** and/or initialize the temp file related fields of the IncrMerge
 79697  80027   ** object at (pReadr->pIncr).
 79698  80028   **
 79699  80029   ** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
 79700         -** in the sub-tree headed by pReadr are also initialized. Data is then loaded
 79701         -** into the buffers belonging to pReadr and it is set to
 79702         -** point to the first key in its range.
        80030  +** in the sub-tree headed by pReadr are also initialized. Data is then 
        80031  +** loaded into the buffers belonging to pReadr and it is set to point to 
        80032  +** the first key in its range.
 79703  80033   **
 79704  80034   ** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
 79705  80035   ** to be a multi-threaded PmaReader and this function is being called in a
 79706  80036   ** background thread. In this case all PmaReaders in the sub-tree are 
 79707  80037   ** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
 79708  80038   ** pReadr is populated. However, pReadr itself is not set up to point
 79709  80039   ** to its first key. A call to vdbePmaReaderNext() is still required to do
................................................................................
 79722  80052   ** the current PmaReader set to point to the first key in its range.
 79723  80053   **
 79724  80054   ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
 79725  80055   */
 79726  80056   static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
 79727  80057     int rc = SQLITE_OK;
 79728  80058     IncrMerger *pIncr = pReadr->pIncr;
        80059  +  SortSubtask *pTask = pIncr->pTask;
        80060  +  sqlite3 *db = pTask->pSorter->db;
 79729  80061   
 79730  80062     /* eMode is always INCRINIT_NORMAL in single-threaded mode */
 79731  80063     assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
 79732  80064   
 79733         -  if( pIncr ){
 79734         -    SortSubtask *pTask = pIncr->pTask;
 79735         -    sqlite3 *db = pTask->pSorter->db;
        80065  +  rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
 79736  80066   
 79737         -    rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
 79738         -
 79739         -    /* Set up the required files for pIncr. A multi-theaded IncrMerge object
 79740         -    ** requires two temp files to itself, whereas a single-threaded object
 79741         -    ** only requires a region of pTask->file2. */
 79742         -    if( rc==SQLITE_OK ){
 79743         -      int mxSz = pIncr->mxSz;
        80067  +  /* Set up the required files for pIncr. A multi-theaded IncrMerge object
        80068  +  ** requires two temp files to itself, whereas a single-threaded object
        80069  +  ** only requires a region of pTask->file2. */
        80070  +  if( rc==SQLITE_OK ){
        80071  +    int mxSz = pIncr->mxSz;
 79744  80072   #if SQLITE_MAX_WORKER_THREADS>0
 79745         -      if( pIncr->bUseThread ){
 79746         -        rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
 79747         -        if( rc==SQLITE_OK ){
 79748         -          rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
 79749         -        }
 79750         -      }else
 79751         -#endif
 79752         -      /*if( !pIncr->bUseThread )*/{
 79753         -        if( pTask->file2.pFd==0 ){
 79754         -          assert( pTask->file2.iEof>0 );
 79755         -          rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
 79756         -          pTask->file2.iEof = 0;
 79757         -        }
 79758         -        if( rc==SQLITE_OK ){
 79759         -          pIncr->aFile[1].pFd = pTask->file2.pFd;
 79760         -          pIncr->iStartOff = pTask->file2.iEof;
 79761         -          pTask->file2.iEof += mxSz;
 79762         -        }
 79763         -      }
 79764         -    }
        80073  +    if( pIncr->bUseThread ){
        80074  +      rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
        80075  +      if( rc==SQLITE_OK ){
        80076  +        rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
        80077  +      }
        80078  +    }else
        80079  +#endif
        80080  +    /*if( !pIncr->bUseThread )*/{
        80081  +      if( pTask->file2.pFd==0 ){
        80082  +        assert( pTask->file2.iEof>0 );
        80083  +        rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
        80084  +        pTask->file2.iEof = 0;
        80085  +      }
        80086  +      if( rc==SQLITE_OK ){
        80087  +        pIncr->aFile[1].pFd = pTask->file2.pFd;
        80088  +        pIncr->iStartOff = pTask->file2.iEof;
        80089  +        pTask->file2.iEof += mxSz;
        80090  +      }
        80091  +    }
        80092  +  }
 79765  80093   
 79766  80094   #if SQLITE_MAX_WORKER_THREADS>0
 79767         -    if( rc==SQLITE_OK && pIncr->bUseThread ){
 79768         -      /* Use the current thread to populate aFile[1], even though this
 79769         -      ** PmaReader is multi-threaded. The reason being that this function
 79770         -      ** is already running in background thread pIncr->pTask->thread. */
 79771         -      assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK );
 79772         -      rc = vdbeIncrPopulate(pIncr);
 79773         -    }
        80095  +  if( rc==SQLITE_OK && pIncr->bUseThread ){
        80096  +    /* Use the current thread to populate aFile[1], even though this
        80097  +    ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
        80098  +    ** then this function is already running in background thread 
        80099  +    ** pIncr->pTask->thread. 
        80100  +    **
        80101  +    ** If this is the INCRINIT_ROOT object, then it is running in the 
        80102  +    ** main VDBE thread. But that is Ok, as that thread cannot return
        80103  +    ** control to the VDBE or proceed with anything useful until the 
        80104  +    ** first results are ready from this merger object anyway.
        80105  +    */
        80106  +    assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK );
        80107  +    rc = vdbeIncrPopulate(pIncr);
        80108  +  }
 79774  80109   #endif
 79775  80110   
 79776         -    if( rc==SQLITE_OK
 79777         -     && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK)
 79778         -    ){
 79779         -      rc = vdbePmaReaderNext(pReadr);
 79780         -    }
        80111  +  if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){
        80112  +    rc = vdbePmaReaderNext(pReadr);
 79781  80113     }
        80114  +
 79782  80115     return rc;
 79783  80116   }
 79784  80117   
 79785  80118   #if SQLITE_MAX_WORKER_THREADS>0
 79786  80119   /*
 79787  80120   ** The main routine for vdbePmaReaderIncrMergeInit() operations run in 
 79788  80121   ** background threads.
 79789  80122   */
 79790         -static void *vdbePmaReaderBgInit(void *pCtx){
        80123  +static void *vdbePmaReaderBgIncrInit(void *pCtx){
 79791  80124     PmaReader *pReader = (PmaReader*)pCtx;
 79792  80125     void *pRet = SQLITE_INT_TO_PTR(
 79793  80126                     vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)
 79794  80127                  );
 79795  80128     pReader->pIncr->pTask->bDone = 1;
 79796  80129     return pRet;
 79797  80130   }
 79798         -
 79799         -/*
 79800         -** Use a background thread to invoke vdbePmaReaderIncrMergeInit(INCRINIT_TASK) 
 79801         -** on the PmaReader object passed as the first argument.
 79802         -**
 79803         -** This call will initialize the various fields of the pReadr->pIncr 
 79804         -** structure and, if it is a multi-threaded IncrMerger, launch a 
 79805         -** background thread to populate aFile[1].
 79806         -*/
 79807         -static int vdbePmaReaderBgIncrInit(PmaReader *pReadr){
 79808         -  void *pCtx = (void*)pReadr;
 79809         -  return vdbeSorterCreateThread(pReadr->pIncr->pTask, vdbePmaReaderBgInit, pCtx);
 79810         -}
 79811         -#endif
        80131  +#endif
        80132  +
        80133  +/*
        80134  +** If the PmaReader passed as the first argument is not an incremental-reader
        80135  +** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
        80136  +** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
        80137  +** this routine to initialize the incremental merge.
        80138  +** 
        80139  +** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1), 
        80140  +** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
        80141  +** Or, if the IncrMerger is single threaded, the same function is called
        80142  +** using the current thread.
        80143  +*/
        80144  +static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
        80145  +  IncrMerger *pIncr = pReadr->pIncr;   /* Incremental merger */
        80146  +  int rc = SQLITE_OK;                  /* Return code */
        80147  +  if( pIncr ){
        80148  +#if SQLITE_MAX_WORKER_THREADS>0
        80149  +    assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
        80150  +    if( pIncr->bUseThread ){
        80151  +      void *pCtx = (void*)pReadr;
        80152  +      rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
        80153  +    }else
        80154  +#endif
        80155  +    {
        80156  +      rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
        80157  +    }
        80158  +  }
        80159  +  return rc;
        80160  +}
 79812  80161   
 79813  80162   /*
 79814  80163   ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
 79815  80164   ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
 79816  80165   ** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
 79817  80166   ** to NULL and return an SQLite error code.
 79818  80167   **
................................................................................
 80016  80365   */
 80017  80366   static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
 80018  80367     int rc;                         /* Return code */
 80019  80368     SortSubtask *pTask0 = &pSorter->aTask[0];
 80020  80369     MergeEngine *pMain = 0;
 80021  80370   #if SQLITE_MAX_WORKER_THREADS
 80022  80371     sqlite3 *db = pTask0->pSorter->db;
        80372  +  int i;
        80373  +  SorterCompare xCompare = vdbeSorterGetCompare(pSorter);
        80374  +  for(i=0; i<pSorter->nTask; i++){
        80375  +    pSorter->aTask[i].xCompare = xCompare;
        80376  +  }
 80023  80377   #endif
 80024  80378   
 80025  80379     rc = vdbeSorterMergeTreeBuild(pSorter, &pMain);
 80026  80380     if( rc==SQLITE_OK ){
 80027  80381   #if SQLITE_MAX_WORKER_THREADS
 80028  80382       assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
 80029  80383       if( pSorter->bUseThreads ){
................................................................................
 80044  80398               IncrMerger *pIncr;
 80045  80399               if( (pIncr = pMain->aReadr[iTask].pIncr) ){
 80046  80400                 vdbeIncrMergerSetThreads(pIncr);
 80047  80401                 assert( pIncr->pTask!=pLast );
 80048  80402               }
 80049  80403             }
 80050  80404             for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
        80405  +            /* Check that:
        80406  +            **   
        80407  +            **   a) The incremental merge object is configured to use the
        80408  +            **      right task, and
        80409  +            **   b) If it is using task (nTask-1), it is configured to run
        80410  +            **      in single-threaded mode. This is important, as the
        80411  +            **      root merge (INCRINIT_ROOT) will be using the same task
        80412  +            **      object.
        80413  +            */
 80051  80414               PmaReader *p = &pMain->aReadr[iTask];
 80052         -            assert( p->pIncr==0 || p->pIncr->pTask==&pSorter->aTask[iTask] );
 80053         -            if( p->pIncr ){ 
 80054         -              if( iTask==pSorter->nTask-1 ){
 80055         -                rc = vdbePmaReaderIncrMergeInit(p, INCRINIT_TASK);
 80056         -              }else{
 80057         -                rc = vdbePmaReaderBgIncrInit(p);
 80058         -              }
 80059         -            }
        80415  +            assert( p->pIncr==0 || (
        80416  +                (p->pIncr->pTask==&pSorter->aTask[iTask])             /* a */
        80417  +             && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0)  /* b */
        80418  +            ));
        80419  +            rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK);
 80060  80420             }
 80061  80421           }
 80062  80422           pMain = 0;
 80063  80423         }
 80064  80424         if( rc==SQLITE_OK ){
 80065  80425           rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT);
 80066  80426         }
................................................................................
 81007  81367   **     SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
 81008  81368   **
 81009  81369   ** Should be transformed into:
 81010  81370   **
 81011  81371   **     SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
 81012  81372   **
 81013  81373   ** The nSubquery parameter specifies how many levels of subquery the
 81014         -** alias is removed from the original expression.  The usually value is
        81374  +** alias is removed from the original expression.  The usual value is
 81015  81375   ** zero but it might be more if the alias is contained within a subquery
 81016  81376   ** of the original expression.  The Expr.op2 field of TK_AGG_FUNCTION
 81017  81377   ** structures must be increased by the nSubquery amount.
 81018  81378   */
 81019  81379   static void resolveAlias(
 81020  81380     Parse *pParse,         /* Parsing context */
 81021  81381     ExprList *pEList,      /* A result set */
................................................................................
 81027  81387     Expr *pOrig;           /* The iCol-th column of the result set */
 81028  81388     Expr *pDup;            /* Copy of pOrig */
 81029  81389     sqlite3 *db;           /* The database connection */
 81030  81390   
 81031  81391     assert( iCol>=0 && iCol<pEList->nExpr );
 81032  81392     pOrig = pEList->a[iCol].pExpr;
 81033  81393     assert( pOrig!=0 );
 81034         -  assert( pOrig->flags & EP_Resolved );
        81394  +  assert( (pOrig->flags & EP_Resolved)!=0 || zType[0]==0 );
 81035  81395     db = pParse->db;
 81036  81396     pDup = sqlite3ExprDup(db, pOrig, 0);
 81037  81397     if( pDup==0 ) return;
 81038  81398     if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
 81039  81399       incrAggFunctionDepth(pDup, nSubquery);
 81040  81400       pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
 81041  81401       if( pDup==0 ) return;
................................................................................
 81921  82281           Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
 81922  82282           if( pNew==0 ) return 1;
 81923  82283           pNew->flags |= EP_IntValue;
 81924  82284           pNew->u.iValue = iCol;
 81925  82285           if( pItem->pExpr==pE ){
 81926  82286             pItem->pExpr = pNew;
 81927  82287           }else{
 81928         -          assert( pItem->pExpr->op==TK_COLLATE );
 81929         -          assert( pItem->pExpr->pLeft==pE );
 81930         -          pItem->pExpr->pLeft = pNew;
        82288  +          Expr *pParent = pItem->pExpr;
        82289  +          assert( pParent->op==TK_COLLATE );
        82290  +          while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
        82291  +          assert( pParent->pLeft==pE );
        82292  +          pParent->pLeft = pNew;
 81931  82293           }
 81932  82294           sqlite3ExprDelete(db, pE);
 81933  82295           pItem->u.x.iOrderByCol = (u16)iCol;
 81934  82296           pItem->done = 1;
 81935  82297         }else{
 81936  82298           moreToDo = 1;
 81937  82299         }
................................................................................
 82123  82485       ** was created by the convertCompoundSelectToSubquery() function.
 82124  82486       ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
 82125  82487       ** as if it were part of the sub-query, not the parent. This block
 82126  82488       ** moves the pOrderBy down to the sub-query. It will be moved back
 82127  82489       ** after the names have been resolved.  */
 82128  82490       if( p->selFlags & SF_Converted ){
 82129  82491         Select *pSub = p->pSrc->a[0].pSelect;
 82130         -      assert( p->pSrc->nSrc==1 && isCompound==0 && p->pOrderBy );
        82492  +      assert( p->pSrc->nSrc==1 && p->pOrderBy );
 82131  82493         assert( pSub->pPrior && pSub->pOrderBy==0 );
 82132  82494         pSub->pOrderBy = p->pOrderBy;
 82133  82495         p->pOrderBy = 0;
 82134  82496       }
 82135  82497     
 82136  82498       /* Recursively resolve names in all subqueries
 82137  82499       */
................................................................................
 82225  82587         pSub->pOrderBy = 0;
 82226  82588       }
 82227  82589   
 82228  82590       /* Process the ORDER BY clause for singleton SELECT statements.
 82229  82591       ** The ORDER BY clause for compounds SELECT statements is handled
 82230  82592       ** below, after all of the result-sets for all of the elements of
 82231  82593       ** the compound have been resolved.
        82594  +    **
        82595  +    ** If there is an ORDER BY clause on a term of a compound-select other
        82596  +    ** than the right-most term, then that is a syntax error.  But the error
        82597  +    ** is not detected until much later, and so we need to go ahead and
        82598  +    ** resolve those symbols on the incorrect ORDER BY for consistency.
 82232  82599       */
 82233         -    if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
        82600  +    if( isCompound<=nCompound  /* Defer right-most ORDER BY of a compound */
        82601  +     && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
        82602  +    ){
 82234  82603         return WRC_Abort;
 82235  82604       }
 82236  82605       if( db->mallocFailed ){
 82237  82606         return WRC_Abort;
 82238  82607       }
 82239  82608     
 82240  82609       /* Resolve the GROUP BY clause.  At the same time, make sure 
................................................................................
 83678  84047   ** ExprList.
 83679  84048   */
 83680  84049   SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){
 83681  84050     int i;
 83682  84051     u32 m = 0;
 83683  84052     if( pList ){
 83684  84053       for(i=0; i<pList->nExpr; i++){
 83685         -       m |= pList->a[i].pExpr->flags;
        84054  +       Expr *pExpr = pList->a[i].pExpr;
        84055  +       if( ALWAYS(pExpr) ) m |= pExpr->flags;
 83686  84056       }
 83687  84057     }
 83688  84058     return m;
 83689  84059   }
 83690  84060   
 83691  84061   /*
 83692  84062   ** These routines are Walker callbacks used to check expressions to
................................................................................
 84118  84488     mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
 84119  84489   
 84120  84490     /* Check to see if an existing table or index can be used to
 84121  84491     ** satisfy the query.  This is preferable to generating a new 
 84122  84492     ** ephemeral table.
 84123  84493     */
 84124  84494     p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
 84125         -  if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
        84495  +  if( pParse->nErr==0 && isCandidateForInOpt(p) ){
 84126  84496       sqlite3 *db = pParse->db;              /* Database connection */
 84127  84497       Table *pTab;                           /* Table <table>. */
 84128  84498       Expr *pExpr;                           /* Expression <column> */
 84129  84499       i16 iCol;                              /* Index of column <column> */
 84130  84500       i16 iDb;                               /* Database idx for pTab */
 84131  84501   
 84132  84502       assert( p );                        /* Because of isCandidateForInOpt(p) */
................................................................................
 84443  84813           sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
 84444  84814           VdbeComment((v, "Init EXISTS result"));
 84445  84815         }
 84446  84816         sqlite3ExprDelete(pParse->db, pSel->pLimit);
 84447  84817         pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
 84448  84818                                     &sqlite3IntTokens[1]);
 84449  84819         pSel->iLimit = 0;
        84820  +      pSel->selFlags &= ~SF_MultiValue;
 84450  84821         if( sqlite3Select(pParse, pSel, &dest) ){
 84451  84822           return 0;
 84452  84823         }
 84453  84824         rReg = dest.iSDParm;
 84454  84825         ExprSetVVAProperty(pExpr, EP_NoReduce);
 84455  84826         break;
 84456  84827       }
................................................................................
 85808  86179       }
 85809  86180       case TK_AS: {
 85810  86181         sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
 85811  86182         sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
 85812  86183         break;
 85813  86184       }
 85814  86185       case TK_ID: {
 85815         -      sqlite3TreeViewLine(pView,"ID %Q", pExpr->u.zToken);
        86186  +      sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
 85816  86187         break;
 85817  86188       }
 85818  86189   #ifndef SQLITE_OMIT_CAST
 85819  86190       case TK_CAST: {
 85820  86191         /* Expressions of the form:   CAST(pLeft AS token) */
 85821  86192         sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
 85822  86193         sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
................................................................................
 86443  86814     }
 86444  86815     if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
 86445  86816     if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
 86446  86817       if( combinedFlags & EP_xIsSelect ) return 2;
 86447  86818       if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
 86448  86819       if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
 86449  86820       if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
 86450         -    if( ALWAYS((combinedFlags & EP_Reduced)==0) ){
        86821  +    if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){
 86451  86822         if( pA->iColumn!=pB->iColumn ) return 2;
 86452  86823         if( pA->iTable!=pB->iTable 
 86453  86824          && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
 86454  86825       }
 86455  86826     }
 86456  86827     return 0;
 86457  86828   }
................................................................................
 86975  87346       if( token==TK_REFERENCES ){
 86976  87347         char *zParent;
 86977  87348         do {
 86978  87349           z += n;
 86979  87350           n = sqlite3GetToken(z, &token);
 86980  87351         }while( token==TK_SPACE );
 86981  87352   
        87353  +      if( token==TK_ILLEGAL ) break;
 86982  87354         zParent = sqlite3DbStrNDup(db, (const char *)z, n);
 86983  87355         if( zParent==0 ) break;
 86984  87356         sqlite3Dequote(zParent);
 86985  87357         if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
 86986  87358           char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", 
 86987  87359               (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew
 86988  87360           );
................................................................................
 89201  89573       pIndex = sqlite3PrimaryKeyIndex(pTable);
 89202  89574     }else{
 89203  89575       pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
 89204  89576     }
 89205  89577     z = argv[2];
 89206  89578   
 89207  89579     if( pIndex ){
        89580  +    tRowcnt *aiRowEst = 0;
 89208  89581       int nCol = pIndex->nKeyCol+1;
 89209  89582   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 89210         -    tRowcnt * const aiRowEst = pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(
 89211         -        sizeof(tRowcnt) * nCol
 89212         -    );
 89213         -    if( aiRowEst==0 ) pInfo->db->mallocFailed = 1;
 89214         -#else
 89215         -    tRowcnt * const aiRowEst = 0;
        89583  +    /* Index.aiRowEst may already be set here if there are duplicate 
        89584  +    ** sqlite_stat1 entries for this index. In that case just clobber
        89585  +    ** the old data with the new instead of allocating a new array.  */
        89586  +    if( pIndex->aiRowEst==0 ){
        89587  +      pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
        89588  +      if( pIndex->aiRowEst==0 ) pInfo->db->mallocFailed = 1;
        89589  +    }
        89590  +    aiRowEst = pIndex->aiRowEst;
 89216  89591   #endif
 89217  89592       pIndex->bUnordered = 0;
 89218  89593       decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
 89219  89594       if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
 89220  89595     }else{
 89221  89596       Index fakeIdx;
 89222  89597       fakeIdx.szIdxRow = pTable->szTabRow;
................................................................................
 89871  90246       sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
 89872  90247       goto detach_error;
 89873  90248     }
 89874  90249   
 89875  90250     sqlite3BtreeClose(pDb->pBt);
 89876  90251     pDb->pBt = 0;
 89877  90252     pDb->pSchema = 0;
 89878         -  sqlite3ResetAllSchemasOfConnection(db);
        90253  +  sqlite3CollapseDatabaseArray(db);
 89879  90254     return;
 89880  90255   
 89881  90256   detach_error:
 89882  90257     sqlite3_result_error(context, zErr, -1);
 89883  90258   }
 89884  90259   
 89885  90260   /*
................................................................................
 89905  90280     sName.pParse = pParse;
 89906  90281   
 89907  90282     if( 
 89908  90283         SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
 89909  90284         SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
 89910  90285         SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
 89911  90286     ){
 89912         -    pParse->nErr++;
 89913  90287       goto attach_end;
 89914  90288     }
 89915  90289   
 89916  90290   #ifndef SQLITE_OMIT_AUTHORIZATION
 89917  90291     if( pAuthArg ){
 89918  90292       char *zAuthArg;
 89919  90293       if( pAuthArg->op==TK_STRING ){
................................................................................
 90564  90938   */
 90565  90939   SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
 90566  90940     sqlite3 *db;
 90567  90941     Vdbe *v;
 90568  90942   
 90569  90943     assert( pParse->pToplevel==0 );
 90570  90944     db = pParse->db;
 90571         -  if( db->mallocFailed ) return;
 90572  90945     if( pParse->nested ) return;
 90573         -  if( pParse->nErr ) return;
        90946  +  if( db->mallocFailed || pParse->nErr ){
        90947  +    if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR;
        90948  +    return;
        90949  +  }
 90574  90950   
 90575  90951     /* Begin by generating some termination code at the end of the
 90576  90952     ** vdbe program
 90577  90953     */
 90578  90954     v = sqlite3GetVdbe(pParse);
 90579  90955     assert( !pParse->isMultiWrite 
 90580  90956          || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
................................................................................
 90648  91024         sqlite3VdbeAddOp2(v, OP_Goto, 0, 1);
 90649  91025       }
 90650  91026     }
 90651  91027   
 90652  91028   
 90653  91029     /* Get the VDBE program ready for execution
 90654  91030     */
 90655         -  if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
        91031  +  if( v && pParse->nErr==0 && !db->mallocFailed ){
 90656  91032       assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
 90657  91033       /* A minimum of one cursor is required if autoincrement is used
 90658  91034       *  See ticket [a696379c1f08866] */
 90659  91035       if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
 90660  91036       sqlite3VdbeMakeReady(v, pParse);
 90661  91037       pParse->rc = SQLITE_DONE;
 90662  91038       pParse->colNamesSet = 0;
................................................................................
 91183  91559   ){
 91184  91560     int iDb;                    /* Database holding the object */
 91185  91561     sqlite3 *db = pParse->db;
 91186  91562   
 91187  91563     if( ALWAYS(pName2!=0) && pName2->n>0 ){
 91188  91564       if( db->init.busy ) {
 91189  91565         sqlite3ErrorMsg(pParse, "corrupt database");
 91190         -      pParse->nErr++;
 91191  91566         return -1;
 91192  91567       }
 91193  91568       *pUnqual = pName2;
 91194  91569       iDb = sqlite3FindDb(db, pName1);
 91195  91570       if( iDb<0 ){
 91196  91571         sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
 91197         -      pParse->nErr++;
 91198  91572         return -1;
 91199  91573       }
 91200  91574     }else{
 91201  91575       assert( db->init.iDb==0 || db->init.busy );
 91202  91576       iDb = db->init.iDb;
 91203  91577       *pUnqual = pName1;
 91204  91578     }
................................................................................
 91349  91723         goto begin_table_error;
 91350  91724       }
 91351  91725       pTable = sqlite3FindTable(db, zName, zDb);
 91352  91726       if( pTable ){
 91353  91727         if( !noErr ){
 91354  91728           sqlite3ErrorMsg(pParse, "table %T already exists", pName);
 91355  91729         }else{
 91356         -        assert( !db->init.busy );
        91730  +        assert( !db->init.busy || CORRUPT_DB );
 91357  91731           sqlite3CodeVerifySchema(pParse, iDb);
 91358  91732         }
 91359  91733         goto begin_table_error;
 91360  91734       }
 91361  91735       if( sqlite3FindIndex(db, zName, zDb)!=0 ){
 91362  91736         sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
 91363  91737         goto begin_table_error;
................................................................................
 91638  92012   SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
 91639  92013     Table *p;
 91640  92014     Column *pCol;
 91641  92015   
 91642  92016     p = pParse->pNewTable;
 91643  92017     if( p==0 || NEVER(p->nCol<1) ) return;
 91644  92018     pCol = &p->aCol[p->nCol-1];
 91645         -  assert( pCol->zType==0 );
        92019  +  assert( pCol->zType==0 || CORRUPT_DB );
        92020  +  sqlite3DbFree(pParse->db, pCol->zType);
 91646  92021     pCol->zType = sqlite3NameFromToken(pParse->db, pType);
 91647  92022     pCol->affinity = sqlite3AffinityType(pCol->zType, &pCol->szEst);
 91648  92023   }
 91649  92024   
 91650  92025   /*
 91651  92026   ** The expression is the default value for the most recently added column
 91652  92027   ** of the table currently under construction.
................................................................................
 92872  93247     int iDb;
 92873  93248   
 92874  93249     if( db->mallocFailed ){
 92875  93250       goto exit_drop_table;
 92876  93251     }
 92877  93252     assert( pParse->nErr==0 );
 92878  93253     assert( pName->nSrc==1 );
        93254  +  if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
 92879  93255     if( noErr ) db->suppressErr++;
 92880  93256     pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
 92881  93257     if( noErr ) db->suppressErr--;
 92882  93258   
 92883  93259     if( pTab==0 ){
 92884  93260       if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
 92885  93261       goto exit_drop_table;
................................................................................
 93185  93561       sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
 93186  93562                            pIndex->nKeyCol); VdbeCoverage(v);
 93187  93563       sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
 93188  93564     }else{
 93189  93565       addr2 = sqlite3VdbeCurrentAddr(v);
 93190  93566     }
 93191  93567     sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
 93192         -  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
        93568  +  sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1);
        93569  +  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
 93193  93570     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 93194  93571     sqlite3ReleaseTempReg(pParse, regRecord);
 93195  93572     sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
 93196  93573     sqlite3VdbeJumpHere(v, addr1);
 93197  93574   
 93198  93575     sqlite3VdbeAddOp1(v, OP_Close, iTab);
 93199  93576     sqlite3VdbeAddOp1(v, OP_Close, iIdx);
................................................................................
 93278  93655     struct ExprList_item *pListItem; /* For looping over pList */
 93279  93656     const Column *pTabCol;           /* A column in the table */
 93280  93657     int nExtra = 0;                  /* Space allocated for zExtra[] */
 93281  93658     int nExtraCol;                   /* Number of extra columns needed */
 93282  93659     char *zExtra = 0;                /* Extra space after the Index object */
 93283  93660     Index *pPk = 0;      /* PRIMARY KEY index for WITHOUT ROWID tables */
 93284  93661   
 93285         -  assert( pParse->nErr==0 );      /* Never called with prior errors */
 93286         -  if( db->mallocFailed || IN_DECLARE_VTAB ){
        93662  +  if( db->mallocFailed || IN_DECLARE_VTAB || pParse->nErr>0 ){
 93287  93663       goto exit_create_index;
 93288  93664     }
 93289  93665     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 93290  93666       goto exit_create_index;
 93291  93667     }
 93292  93668   
 93293  93669     /*
................................................................................
 94198  94574   ** The operator is "natural cross join".  The A and B operands are stored
 94199  94575   ** in p->a[0] and p->a[1], respectively.  The parser initially stores the
 94200  94576   ** operator with A.  This routine shifts that operator over to B.
 94201  94577   */
 94202  94578   SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
 94203  94579     if( p ){
 94204  94580       int i;
 94205         -    assert( p->a || p->nSrc==0 );
 94206  94581       for(i=p->nSrc-1; i>0; i--){
 94207  94582         p->a[i].jointype = p->a[i-1].jointype;
 94208  94583       }
 94209  94584       p->a[0].jointype = 0;
 94210  94585     }
 94211  94586   }
 94212  94587   
................................................................................
 94445  94820     Index *pIdx       /* The index that triggers the constraint */
 94446  94821   ){
 94447  94822     char *zErr;
 94448  94823     int j;
 94449  94824     StrAccum errMsg;
 94450  94825     Table *pTab = pIdx->pTable;
 94451  94826   
 94452         -  sqlite3StrAccumInit(&errMsg, 0, 0, 200);
 94453         -  errMsg.db = pParse->db;
        94827  +  sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
 94454  94828     for(j=0; j<pIdx->nKeyCol; j++){
 94455  94829       char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
 94456  94830       if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
 94457  94831       sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
 94458  94832       sqlite3StrAccumAppend(&errMsg, ".", 1);
 94459  94833       sqlite3StrAccumAppendAll(&errMsg, zCol);
 94460  94834     }
................................................................................
 96275  96649     int argc,
 96276  96650     sqlite3_value **argv
 96277  96651   ){
 96278  96652     PrintfArguments x;
 96279  96653     StrAccum str;
 96280  96654     const char *zFormat;
 96281  96655     int n;
        96656  +  sqlite3 *db = sqlite3_context_db_handle(context);
 96282  96657   
 96283  96658     if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
 96284  96659       x.nArg = argc-1;
 96285  96660       x.nUsed = 0;
 96286  96661       x.apArg = argv+1;
 96287         -    sqlite3StrAccumInit(&str, 0, 0, SQLITE_MAX_LENGTH);
 96288         -    str.db = sqlite3_context_db_handle(context);
        96662  +    sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
 96289  96663       sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x);
 96290  96664       n = str.nChar;
 96291  96665       sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
 96292  96666                           SQLITE_DYNAMIC);
 96293  96667     }
 96294  96668   }
 96295  96669   
................................................................................
 96431  96805       sqlite3_free(zBuf);
 96432  96806     }
 96433  96807     sqlite3_result_double(context, r);
 96434  96808   }
 96435  96809   #endif
 96436  96810   
 96437  96811   /*
 96438         -** Allocate nByte bytes of space using sqlite3_malloc(). If the
        96812  +** Allocate nByte bytes of space using sqlite3Malloc(). If the
 96439  96813   ** allocation fails, call sqlite3_result_error_nomem() to notify
 96440  96814   ** the database handle that malloc() has failed and return NULL.
 96441  96815   ** If nByte is larger than the maximum string or blob length, then
 96442  96816   ** raise an SQLITE_TOOBIG exception and return NULL.
 96443  96817   */
 96444  96818   static void *contextMalloc(sqlite3_context *context, i64 nByte){
 96445  96819     char *z;
................................................................................
 97100  97474   static void charFunc(
 97101  97475     sqlite3_context *context,
 97102  97476     int argc,
 97103  97477     sqlite3_value **argv
 97104  97478   ){
 97105  97479     unsigned char *z, *zOut;
 97106  97480     int i;
 97107         -  zOut = z = sqlite3_malloc( argc*4+1 );
        97481  +  zOut = z = sqlite3_malloc64( argc*4+1 );
 97108  97482     if( z==0 ){
 97109  97483       sqlite3_result_error_nomem(context);
 97110  97484       return;
 97111  97485     }
 97112  97486     for(i=0; i<argc; i++){
 97113  97487       sqlite3_int64 x;
 97114  97488       unsigned c;
................................................................................
 97248  97622         testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
 97249  97623         if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 97250  97624           sqlite3_result_error_toobig(context);
 97251  97625           sqlite3_free(zOut);
 97252  97626           return;
 97253  97627         }
 97254  97628         zOld = zOut;
 97255         -      zOut = sqlite3_realloc(zOut, (int)nOut);
        97629  +      zOut = sqlite3_realloc64(zOut, (int)nOut);
 97256  97630         if( zOut==0 ){
 97257  97631           sqlite3_result_error_nomem(context);
 97258  97632           sqlite3_free(zOld);
 97259  97633           return;
 97260  97634         }
 97261  97635         memcpy(&zOut[j], zRep, nRep);
 97262  97636         j += nRep;
................................................................................
 97610  97984     int nVal, nSep;
 97611  97985     assert( argc==1 || argc==2 );
 97612  97986     if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
 97613  97987     pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
 97614  97988   
 97615  97989     if( pAccum ){
 97616  97990       sqlite3 *db = sqlite3_context_db_handle(context);
 97617         -    int firstTerm = pAccum->useMalloc==0;
 97618         -    pAccum->useMalloc = 2;
        97991  +    int firstTerm = pAccum->mxAlloc==0;
 97619  97992       pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
 97620  97993       if( !firstTerm ){
 97621  97994         if( argc==2 ){
 97622  97995           zSep = (char*)sqlite3_value_text(argv[1]);
 97623  97996           nSep = sqlite3_value_bytes(argv[1]);
 97624  97997         }else{
 97625  97998           zSep = ",";
................................................................................
 99031  99404         Token tFromCol;             /* Name of column in child table */
 99032  99405         Token tToCol;               /* Name of column in parent table */
 99033  99406         int iFromCol;               /* Idx of column in child table */
 99034  99407         Expr *pEq;                  /* tFromCol = OLD.tToCol */
 99035  99408   
 99036  99409         iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
 99037  99410         assert( iFromCol>=0 );
 99038         -      tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid";
        99411  +      assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
        99412  +      tToCol.z = pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName;
 99039  99413         tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
 99040  99414   
 99041  99415         tToCol.n = sqlite3Strlen30(tToCol.z);
 99042  99416         tFromCol.n = sqlite3Strlen30(tFromCol.z);
 99043  99417   
 99044  99418         /* Create the expression "OLD.zToCol = zFromCol". It is important
 99045  99419         ** that the "OLD.zToCol" term is on the LHS of the = operator, so
 99046  99420         ** that the affinity and collation sequence associated with the
 99047  99421         ** parent table are used for the comparison. */
 99048  99422         pEq = sqlite3PExpr(pParse, TK_EQ,
 99049  99423             sqlite3PExpr(pParse, TK_DOT, 
 99050         -            sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
 99051         -            sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
        99424  +            sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
        99425  +            sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)
 99052  99426             , 0),
 99053         -          sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol)
        99427  +          sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0)
 99054  99428         , 0);
 99055  99429         pWhere = sqlite3ExprAnd(db, pWhere, pEq);
 99056  99430   
 99057  99431         /* For ON UPDATE, construct the next term of the WHEN clause.
 99058  99432         ** The final WHEN clause will be like this:
 99059  99433         **
 99060  99434         **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
 99061  99435         */
 99062  99436         if( pChanges ){
 99063  99437           pEq = sqlite3PExpr(pParse, TK_IS,
 99064  99438               sqlite3PExpr(pParse, TK_DOT, 
 99065         -              sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
 99066         -              sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
        99439  +              sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
        99440  +              sqlite3ExprAlloc(db, TK_ID, &tToCol, 0),
 99067  99441                 0),
 99068  99442               sqlite3PExpr(pParse, TK_DOT, 
 99069         -              sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
 99070         -              sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
        99443  +              sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
        99444  +              sqlite3ExprAlloc(db, TK_ID, &tToCol, 0),
 99071  99445                 0),
 99072  99446               0);
 99073  99447           pWhen = sqlite3ExprAnd(db, pWhen, pEq);
 99074  99448         }
 99075  99449     
 99076  99450         if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
 99077  99451           Expr *pNew;
 99078  99452           if( action==OE_Cascade ){
 99079  99453             pNew = sqlite3PExpr(pParse, TK_DOT, 
 99080         -            sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
 99081         -            sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
        99454  +            sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
        99455  +            sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)
 99082  99456             , 0);
 99083  99457           }else if( action==OE_SetDflt ){
 99084  99458             Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
 99085  99459             if( pDflt ){
 99086  99460               pNew = sqlite3ExprDup(db, pDflt, 0);
 99087  99461             }else{
 99088  99462               pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
................................................................................
 99121  99495       /* Disable lookaside memory allocation */
 99122  99496       enableLookaside = db->lookaside.bEnabled;
 99123  99497       db->lookaside.bEnabled = 0;
 99124  99498   
 99125  99499       pTrigger = (Trigger *)sqlite3DbMallocZero(db, 
 99126  99500           sizeof(Trigger) +         /* struct Trigger */
 99127  99501           sizeof(TriggerStep) +     /* Single step in trigger program */
 99128         -        nFrom + 1                 /* Space for pStep->target.z */
        99502  +        nFrom + 1                 /* Space for pStep->zTarget */
 99129  99503       );
 99130  99504       if( pTrigger ){
 99131  99505         pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
 99132         -      pStep->target.z = (char *)&pStep[1];
 99133         -      pStep->target.n = nFrom;
 99134         -      memcpy((char *)pStep->target.z, zFrom, nFrom);
        99506  +      pStep->zTarget = (char *)&pStep[1];
        99507  +      memcpy((char *)pStep->zTarget, zFrom, nFrom);
 99135  99508     
 99136  99509         pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
 99137  99510         pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
 99138  99511         pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
 99139  99512         if( pWhen ){
 99140  99513           pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
 99141  99514           pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
................................................................................
 99592  99965     int onError,          /* How to handle constraint errors */
 99593  99966     int iDbDest           /* The database of pDest */
 99594  99967   );
 99595  99968   
 99596  99969   /*
 99597  99970   ** This routine is called to handle SQL of the following forms:
 99598  99971   **
 99599         -**    insert into TABLE (IDLIST) values(EXPRLIST)
        99972  +**    insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
 99600  99973   **    insert into TABLE (IDLIST) select
        99974  +**    insert into TABLE (IDLIST) default values
 99601  99975   **
 99602  99976   ** The IDLIST following the table name is always optional.  If omitted,
 99603         -** then a list of all columns for the table is substituted.  The IDLIST
 99604         -** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
        99977  +** then a list of all (non-hidden) columns for the table is substituted.
        99978  +** The IDLIST appears in the pColumn parameter.  pColumn is NULL if IDLIST
        99979  +** is omitted.
 99605  99980   **
 99606         -** The pList parameter holds EXPRLIST in the first form of the INSERT
 99607         -** statement above, and pSelect is NULL.  For the second form, pList is
 99608         -** NULL and pSelect is a pointer to the select statement used to generate
 99609         -** data for the insert.
        99981  +** For the pSelect parameter holds the values to be inserted for the
        99982  +** first two forms shown above.  A VALUES clause is really just short-hand
        99983  +** for a SELECT statement that omits the FROM clause and everything else
        99984  +** that follows.  If the pSelect parameter is NULL, that means that the
        99985  +** DEFAULT VALUES form of the INSERT statement is intended.
 99610  99986   **
 99611  99987   ** The code generated follows one of four templates.  For a simple
 99612         -** insert with data coming from a VALUES clause, the code executes
        99988  +** insert with data coming from a single-row VALUES clause, the code executes
 99613  99989   ** once straight down through.  Pseudo-code follows (we call this
 99614  99990   ** the "1st template"):
 99615  99991   **
 99616  99992   **         open write cursor to <table> and its indices
 99617  99993   **         put VALUES clause expressions into registers
 99618  99994   **         write the resulting record into <table>
 99619  99995   **         cleanup
................................................................................
 99712 100088     int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
 99713 100089     SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
 99714 100090     int iDb;              /* Index of database holding TABLE */
 99715 100091     Db *pDb;              /* The database containing table being inserted into */
 99716 100092     u8 useTempTable = 0;  /* Store SELECT results in intermediate table */
 99717 100093     u8 appendFlag = 0;    /* True if the insert is likely to be an append */
 99718 100094     u8 withoutRowid;      /* 0 for normal table.  1 for WITHOUT ROWID table */
 99719         -  u8 bIdListInOrder = 1; /* True if IDLIST is in table order */
       100095  +  u8 bIdListInOrder;    /* True if IDLIST is in table order */
 99720 100096     ExprList *pList = 0;  /* List of VALUES() to be inserted  */
 99721 100097   
 99722 100098     /* Register allocations */
 99723 100099     int regFromSelect = 0;/* Base register for data coming from SELECT */
 99724 100100     int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
 99725 100101     int regRowCount = 0;  /* Memory cell used for the row counter */
 99726 100102     int regIns;           /* Block of regs holding rowid+data being inserted */
................................................................................
 99737 100113     db = pParse->db;
 99738 100114     memset(&dest, 0, sizeof(dest));
 99739 100115     if( pParse->nErr || db->mallocFailed ){
 99740 100116       goto insert_cleanup;
 99741 100117     }
 99742 100118   
 99743 100119     /* If the Select object is really just a simple VALUES() list with a
 99744         -  ** single row values (the common case) then keep that one row of values
 99745         -  ** and go ahead and discard the Select object
       100120  +  ** single row (the common case) then keep that one row of values
       100121  +  ** and discard the other (unused) parts of the pSelect object
 99746 100122     */
 99747 100123     if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
 99748 100124       pList = pSelect->pEList;
 99749 100125       pSelect->pEList = 0;
 99750 100126       sqlite3SelectDelete(db, pSelect);
 99751 100127       pSelect = 0;
 99752 100128     }
................................................................................
 99846 100222     ** If the table has an INTEGER PRIMARY KEY column and that column
 99847 100223     ** is named in the IDLIST, then record in the ipkColumn variable
 99848 100224     ** the index into IDLIST of the primary key column.  ipkColumn is
 99849 100225     ** the index of the primary key as it appears in IDLIST, not as
 99850 100226     ** is appears in the original table.  (The index of the INTEGER
 99851 100227     ** PRIMARY KEY in the original table is pTab->iPKey.)
 99852 100228     */
       100229  +  bIdListInOrder = (pTab->tabFlags & TF_OOOHidden)==0;
 99853 100230     if( pColumn ){
 99854 100231       for(i=0; i<pColumn->nId; i++){
 99855 100232         pColumn->a[i].idx = -1;
 99856 100233       }
 99857 100234       for(i=0; i<pColumn->nId; i++){
 99858 100235         for(j=0; j<pTab->nCol; j++){
 99859 100236           if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
................................................................................
 99881 100258   
 99882 100259     /* Figure out how many columns of data are supplied.  If the data
 99883 100260     ** is coming from a SELECT statement, then generate a co-routine that
 99884 100261     ** produces a single row of the SELECT on each invocation.  The
 99885 100262     ** co-routine is the common header to the 3rd and 4th templates.
 99886 100263     */
 99887 100264     if( pSelect ){
 99888         -    /* Data is coming from a SELECT.  Generate a co-routine to run the SELECT */
       100265  +    /* Data is coming from a SELECT or from a multi-row VALUES clause.
       100266  +    ** Generate a co-routine to run the SELECT. */
 99889 100267       int regYield;       /* Register holding co-routine entry-point */
 99890 100268       int addrTop;        /* Top of the co-routine */
 99891 100269       int rc;             /* Result code */
 99892 100270   
 99893 100271       regYield = ++pParse->nMem;
 99894 100272       addrTop = sqlite3VdbeCurrentAddr(v) + 1;
 99895 100273       sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
 99896 100274       sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
 99897 100275       dest.iSdst = bIdListInOrder ? regData : 0;
 99898 100276       dest.nSdst = pTab->nCol;
 99899 100277       rc = sqlite3Select(pParse, pSelect, &dest);
 99900 100278       regFromSelect = dest.iSdst;
 99901         -    assert( pParse->nErr==0 || rc );
 99902         -    if( rc || db->mallocFailed ) goto insert_cleanup;
       100279  +    if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
 99903 100280       sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
 99904 100281       sqlite3VdbeJumpHere(v, addrTop - 1);                       /* label B: */
 99905 100282       assert( pSelect->pEList );
 99906 100283       nColumn = pSelect->pEList->nExpr;
 99907 100284   
 99908 100285       /* Set useTempTable to TRUE if the result of the SELECT statement
 99909 100286       ** should be written into a temporary table (template 4).  Set to
................................................................................
 99943 100320         sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
 99944 100321         sqlite3VdbeAddOp2(v, OP_Goto, 0, addrL);
 99945 100322         sqlite3VdbeJumpHere(v, addrL);
 99946 100323         sqlite3ReleaseTempReg(pParse, regRec);
 99947 100324         sqlite3ReleaseTempReg(pParse, regTempRowid);
 99948 100325       }
 99949 100326     }else{
 99950         -    /* This is the case if the data for the INSERT is coming from a VALUES
 99951         -    ** clause
       100327  +    /* This is the case if the data for the INSERT is coming from a 
       100328  +    ** single-row VALUES clause
 99952 100329       */
 99953 100330       NameContext sNC;
 99954 100331       memset(&sNC, 0, sizeof(sNC));
 99955 100332       sNC.pParse = pParse;
 99956 100333       srcTab = -1;
 99957 100334       assert( useTempTable==0 );
 99958 100335       nColumn = pList ? pList->nExpr : 0;
................................................................................
101015 101392   static int xferOptimization(
101016 101393     Parse *pParse,        /* Parser context */
101017 101394     Table *pDest,         /* The table we are inserting into */
101018 101395     Select *pSelect,      /* A SELECT statement to use as the data source */
101019 101396     int onError,          /* How to handle constraint errors */
101020 101397     int iDbDest           /* The database of pDest */
101021 101398   ){
       101399  +  sqlite3 *db = pParse->db;
101022 101400     ExprList *pEList;                /* The result set of the SELECT */
101023 101401     Table *pSrc;                     /* The table in the FROM clause of SELECT */
101024 101402     Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
101025 101403     struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
101026 101404     int i;                           /* Loop counter */
101027 101405     int iDbSrc;                      /* The database of pSrc */
101028 101406     int iSrc, iDest;                 /* Cursors from source and destination */
................................................................................
101162 101540     /* Disallow the transfer optimization if the destination table constains
101163 101541     ** any foreign key constraints.  This is more restrictive than necessary.
101164 101542     ** But the main beneficiary of the transfer optimization is the VACUUM 
101165 101543     ** command, and the VACUUM command disables foreign key constraints.  So
101166 101544     ** the extra complication to make this rule less restrictive is probably
101167 101545     ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
101168 101546     */
101169         -  if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
       101547  +  if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
101170 101548       return 0;
101171 101549     }
101172 101550   #endif
101173         -  if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
       101551  +  if( (db->flags & SQLITE_CountRows)!=0 ){
101174 101552       return 0;  /* xfer opt does not play well with PRAGMA count_changes */
101175 101553     }
101176 101554   
101177 101555     /* If we get this far, it means that the xfer optimization is at
101178 101556     ** least a possibility, though it might only work if the destination
101179 101557     ** table (tab1) is initially empty.
101180 101558     */
101181 101559   #ifdef SQLITE_TEST
101182 101560     sqlite3_xferopt_count++;
101183 101561   #endif
101184         -  iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
       101562  +  iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
101185 101563     v = sqlite3GetVdbe(pParse);
101186 101564     sqlite3CodeVerifySchema(pParse, iDbSrc);
101187 101565     iSrc = pParse->nTab++;
101188 101566     iDest = pParse->nTab++;
101189 101567     regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
101190 101568     regData = sqlite3GetTempReg(pParse);
101191 101569     regRowid = sqlite3GetTempReg(pParse);
101192 101570     sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
101193 101571     assert( HasRowid(pDest) || destHasUniqueIdx );
101194         -  if( (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
       101572  +  if( (db->flags & SQLITE_Vacuum)==0 && (
       101573  +      (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
101195 101574      || destHasUniqueIdx                              /* (2) */
101196 101575      || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
101197         -  ){
       101576  +  )){
101198 101577       /* In some circumstances, we are able to run the xfer optimization
101199         -    ** only if the destination table is initially empty.  This code makes
101200         -    ** that determination.  Conditions under which the destination must
101201         -    ** be empty:
       101578  +    ** only if the destination table is initially empty. Unless the
       101579  +    ** SQLITE_Vacuum flag is set, this block generates code to make
       101580  +    ** that determination. If SQLITE_Vacuum is set, then the destination
       101581  +    ** table is always empty.
       101582  +    **
       101583  +    ** Conditions under which the destination must be empty:
101202 101584       **
101203 101585       ** (1) There is no INTEGER PRIMARY KEY but there are indices.
101204 101586       **     (If the destination is not initially empty, the rowid fields
101205 101587       **     of index entries might need to change.)
101206 101588       **
101207 101589       ** (2) The destination has a unique index.  (The xfer optimization 
101208 101590       **     is unable to test uniqueness.)
................................................................................
101237 101619       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
101238 101620       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
101239 101621     }else{
101240 101622       sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
101241 101623       sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
101242 101624     }
101243 101625     for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
       101626  +    u8 useSeekResult = 0;
101244 101627       for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
101245 101628         if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
101246 101629       }
101247 101630       assert( pSrcIdx );
101248 101631       sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
101249 101632       sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
101250 101633       VdbeComment((v, "%s", pSrcIdx->zName));
101251 101634       sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
101252 101635       sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
101253 101636       sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
101254 101637       VdbeComment((v, "%s", pDestIdx->zName));
101255 101638       addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
101256 101639       sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
       101640  +    if( db->flags & SQLITE_Vacuum ){
       101641  +      /* This INSERT command is part of a VACUUM operation, which guarantees
       101642  +      ** that the destination table is empty. If all indexed columns use
       101643  +      ** collation sequence BINARY, then it can also be assumed that the
       101644  +      ** index will be populated by inserting keys in strictly sorted 
       101645  +      ** order. In this case, instead of seeking within the b-tree as part
       101646  +      ** of every OP_IdxInsert opcode, an OP_Last is added before the
       101647  +      ** OP_IdxInsert to seek to the point within the b-tree where each key 
       101648  +      ** should be inserted. This is faster.
       101649  +      **
       101650  +      ** If any of the indexed columns use a collation sequence other than
       101651  +      ** BINARY, this optimization is disabled. This is because the user 
       101652  +      ** might change the definition of a collation sequence and then run
       101653  +      ** a VACUUM command. In that case keys may not be written in strictly
       101654  +      ** sorted order.  */
       101655  +      for(i=0; i<pSrcIdx->nColumn; i++){
       101656  +        char *zColl = pSrcIdx->azColl[i];
       101657  +        assert( zColl!=0 );
       101658  +        if( sqlite3_stricmp("BINARY", zColl) ) break;
       101659  +      }
       101660  +      if( i==pSrcIdx->nColumn ){
       101661  +        useSeekResult = OPFLAG_USESEEKRESULT;
       101662  +        sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
       101663  +      }
       101664  +    }
101257 101665       sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
       101666  +    sqlite3VdbeChangeP5(v, useSeekResult);
101258 101667       sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
101259 101668       sqlite3VdbeJumpHere(v, addr1);
101260 101669       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
101261 101670       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
101262 101671     }
101263 101672     if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
101264 101673     sqlite3ReleaseTempReg(pParse, regRowid);
................................................................................
102369 102778     sqlite3_vfs *pVfs = db->pVfs;
102370 102779     void *handle;
102371 102780     int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
102372 102781     char *zErrmsg = 0;
102373 102782     const char *zEntry;
102374 102783     char *zAltEntry = 0;
102375 102784     void **aHandle;
102376         -  int nMsg = 300 + sqlite3Strlen30(zFile);
       102785  +  u64 nMsg = 300 + sqlite3Strlen30(zFile);
102377 102786     int ii;
102378 102787   
102379 102788     /* Shared library endings to try if zFile cannot be loaded as written */
102380 102789     static const char *azEndings[] = {
102381 102790   #if SQLITE_OS_WIN
102382 102791        "dll"   
102383 102792   #elif defined(__APPLE__)
................................................................................
102412 102821       if( zAltFile==0 ) return SQLITE_NOMEM;
102413 102822       handle = sqlite3OsDlOpen(pVfs, zAltFile);
102414 102823       sqlite3_free(zAltFile);
102415 102824     }
102416 102825   #endif
102417 102826     if( handle==0 ){
102418 102827       if( pzErrMsg ){
102419         -      *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
       102828  +      *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
102420 102829         if( zErrmsg ){
102421 102830           sqlite3_snprintf(nMsg, zErrmsg, 
102422 102831               "unable to open shared library [%s]", zFile);
102423 102832           sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
102424 102833         }
102425 102834       }
102426 102835       return SQLITE_ERROR;
................................................................................
102438 102847     **
102439 102848     **    /usr/local/lib/libExample5.4.3.so ==>  sqlite3_example_init
102440 102849     **    C:/lib/mathfuncs.dll              ==>  sqlite3_mathfuncs_init
102441 102850     */
102442 102851     if( xInit==0 && zProc==0 ){
102443 102852       int iFile, iEntry, c;
102444 102853       int ncFile = sqlite3Strlen30(zFile);
102445         -    zAltEntry = sqlite3_malloc(ncFile+30);
       102854  +    zAltEntry = sqlite3_malloc64(ncFile+30);
102446 102855       if( zAltEntry==0 ){
102447 102856         sqlite3OsDlClose(pVfs, handle);
102448 102857         return SQLITE_NOMEM;
102449 102858       }
102450 102859       memcpy(zAltEntry, "sqlite3_", 8);
102451 102860       for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
102452 102861       iFile++;
................................................................................
102460 102869       zEntry = zAltEntry;
102461 102870       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
102462 102871                        sqlite3OsDlSym(pVfs, handle, zEntry);
102463 102872     }
102464 102873     if( xInit==0 ){
102465 102874       if( pzErrMsg ){
102466 102875         nMsg += sqlite3Strlen30(zEntry);
102467         -      *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
       102876  +      *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
102468 102877         if( zErrmsg ){
102469 102878           sqlite3_snprintf(nMsg, zErrmsg,
102470 102879               "no entry point [%s] in shared library [%s]", zEntry, zFile);
102471 102880           sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
102472 102881         }
102473 102882       }
102474 102883       sqlite3OsDlClose(pVfs, handle);
................................................................................
102559 102968   ** extensions.
102560 102969   **
102561 102970   ** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
102562 102971   ** mutex must be held while accessing this list.
102563 102972   */
102564 102973   typedef struct sqlite3AutoExtList sqlite3AutoExtList;
102565 102974   static SQLITE_WSD struct sqlite3AutoExtList {
102566         -  int nExt;              /* Number of entries in aExt[] */          
       102975  +  u32 nExt;              /* Number of entries in aExt[] */          
102567 102976     void (**aExt)(void);   /* Pointers to the extension init functions */
102568 102977   } sqlite3Autoext = { 0, 0 };
102569 102978   
102570 102979   /* The "wsdAutoext" macro will resolve to the autoextension
102571 102980   ** state vector.  If writable static data is unsupported on the target,
102572 102981   ** we have to locate the state vector at run-time.  In the more common
102573 102982   ** case where writable static data is supported, wsdStat can refer directly
................................................................................
102592 103001   #ifndef SQLITE_OMIT_AUTOINIT
102593 103002     rc = sqlite3_initialize();
102594 103003     if( rc ){
102595 103004       return rc;
102596 103005     }else
102597 103006   #endif
102598 103007     {
102599         -    int i;
       103008  +    u32 i;
102600 103009   #if SQLITE_THREADSAFE
102601 103010       sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
102602 103011   #endif
102603 103012       wsdAutoextInit;
102604 103013       sqlite3_mutex_enter(mutex);
102605 103014       for(i=0; i<wsdAutoext.nExt; i++){
102606 103015         if( wsdAutoext.aExt[i]==xInit ) break;
102607 103016       }
102608 103017       if( i==wsdAutoext.nExt ){
102609         -      int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
       103018  +      u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
102610 103019         void (**aNew)(void);
102611         -      aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
       103020  +      aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte);
102612 103021         if( aNew==0 ){
102613 103022           rc = SQLITE_NOMEM;
102614 103023         }else{
102615 103024           wsdAutoext.aExt = aNew;
102616 103025           wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
102617 103026           wsdAutoext.nExt++;
102618 103027         }
................................................................................
102636 103045   #if SQLITE_THREADSAFE
102637 103046     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
102638 103047   #endif
102639 103048     int i;
102640 103049     int n = 0;
102641 103050     wsdAutoextInit;
102642 103051     sqlite3_mutex_enter(mutex);
102643         -  for(i=wsdAutoext.nExt-1; i>=0; i--){
       103052  +  for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
102644 103053       if( wsdAutoext.aExt[i]==xInit ){
102645 103054         wsdAutoext.nExt--;
102646 103055         wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
102647 103056         n++;
102648 103057         break;
102649 103058       }
102650 103059     }
................................................................................
102674 103083   
102675 103084   /*
102676 103085   ** Load all automatic extensions.
102677 103086   **
102678 103087   ** If anything goes wrong, set an error in the database connection.
102679 103088   */
102680 103089   SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
102681         -  int i;
       103090  +  u32 i;
102682 103091     int go = 1;
102683 103092     int rc;
102684 103093     int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
102685 103094   
102686 103095     wsdAutoextInit;
102687 103096     if( wsdAutoext.nExt==0 ){
102688 103097       /* Common case: early out without every having to acquire a mutex */
................................................................................
103338 103747   #endif /* SQLITE_PAGER_PRAGMAS */
103339 103748   
103340 103749   /*
103341 103750   ** Generate code to return a single integer value.
103342 103751   */
103343 103752   static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
103344 103753     Vdbe *v = sqlite3GetVdbe(pParse);
103345         -  int mem = ++pParse->nMem;
       103754  +  int nMem = ++pParse->nMem;
103346 103755     i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
103347 103756     if( pI64 ){
103348 103757       memcpy(pI64, &value, sizeof(value));
103349 103758     }
103350         -  sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
       103759  +  sqlite3VdbeAddOp4(v, OP_Int64, 0, nMem, 0, (char*)pI64, P4_INT64);
103351 103760     sqlite3VdbeSetNumCols(v, 1);
103352 103761     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
103353         -  sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
       103762  +  sqlite3VdbeAddOp2(v, OP_ResultRow, nMem, 1);
103354 103763   }
103355 103764   
103356 103765   
103357 103766   /*
103358 103767   ** Set the safety_level and pager flags for pager iDb.  Or if iDb<0
103359 103768   ** set these values for all pagers.
103360 103769   */
................................................................................
103511 103920     aFcntl[1] = zLeft;
103512 103921     aFcntl[2] = zRight;
103513 103922     aFcntl[3] = 0;
103514 103923     db->busyHandler.nBusy = 0;
103515 103924     rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
103516 103925     if( rc==SQLITE_OK ){
103517 103926       if( aFcntl[0] ){
103518         -      int mem = ++pParse->nMem;
103519         -      sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
       103927  +      int nMem = ++pParse->nMem;
       103928  +      sqlite3VdbeAddOp4(v, OP_String8, 0, nMem, 0, aFcntl[0], 0);
103520 103929         sqlite3VdbeSetNumCols(v, 1);
103521 103930         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
103522         -      sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
       103931  +      sqlite3VdbeAddOp2(v, OP_ResultRow, nMem, 1);
103523 103932         sqlite3_free(aFcntl[0]);
103524 103933       }
103525 103934       goto pragma_out;
103526 103935     }
103527 103936     if( rc!=SQLITE_NOTFOUND ){
103528 103937       if( aFcntl[0] ){
103529 103938         sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
................................................................................
104120 104529       if( !zRight ){
104121 104530         returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
104122 104531       }else{
104123 104532         if( !db->autoCommit ){
104124 104533           sqlite3ErrorMsg(pParse, 
104125 104534               "Safety level may not be changed inside a transaction");
104126 104535         }else{
104127         -        pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
       104536  +        int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
       104537  +        if( iLevel==0 ) iLevel = 1;
       104538  +        pDb->safety_level = iLevel;
104128 104539           setAllPagerFlags(db);
104129 104540         }
104130 104541       }
104131 104542       break;
104132 104543     }
104133 104544   #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
104134 104545   
................................................................................
104215 104626             sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
104216 104627           }
104217 104628           if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
104218 104629             k = 0;
104219 104630           }else if( pPk==0 ){
104220 104631             k = 1;
104221 104632           }else{
104222         -          for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
       104633  +          for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
104223 104634           }
104224 104635           sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
104225 104636           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
104226 104637         }
104227 104638       }
104228 104639     }
104229 104640     break;
................................................................................
105221 105632       return 1;
105222 105633     }
105223 105634   
105224 105635     assert( iDb>=0 && iDb<db->nDb );
105225 105636     if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
105226 105637     if( argv[1]==0 ){
105227 105638       corruptSchema(pData, argv[0], 0);
105228         -  }else if( argv[2] && argv[2][0] ){
       105639  +  }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){
105229 105640       /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
105230 105641       ** But because db->init.busy is set to 1, no VDBE code is generated
105231 105642       ** or executed.  All the parser does is build the internal data
105232 105643       ** structures that describe the table, index, or view.
105233 105644       */
105234 105645       int rc;
105235 105646       sqlite3_stmt *pStmt;
................................................................................
105252 105663             db->mallocFailed = 1;
105253 105664           }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
105254 105665             corruptSchema(pData, argv[0], sqlite3_errmsg(db));
105255 105666           }
105256 105667         }
105257 105668       }
105258 105669       sqlite3_finalize(pStmt);
105259         -  }else if( argv[0]==0 ){
105260         -    corruptSchema(pData, 0, 0);
       105670  +  }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){
       105671  +    corruptSchema(pData, argv[0], 0);
105261 105672     }else{
105262 105673       /* If the SQL column is blank it means this is an index that
105263 105674       ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
105264 105675       ** constraint for a CREATE TABLE.  The index should have already
105265 105676       ** been created when we processed the CREATE TABLE.  All we have
105266 105677       ** to do here is record the root page number for that index.
105267 105678       */
................................................................................
106160 106571     Expr *pLimit,         /* LIMIT value.  NULL means not used */
106161 106572     Expr *pOffset         /* OFFSET value.  NULL means no offset */
106162 106573   ){
106163 106574     Select *pNew;
106164 106575     Select standin;
106165 106576     sqlite3 *db = pParse->db;
106166 106577     pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
106167         -  assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
106168 106578     if( pNew==0 ){
106169 106579       assert( db->mallocFailed );
106170 106580       pNew = &standin;
106171 106581       memset(pNew, 0, sizeof(*pNew));
106172 106582     }
106173 106583     if( pEList==0 ){
106174 106584       pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
................................................................................
106180 106590     pNew->pGroupBy = pGroupBy;
106181 106591     pNew->pHaving = pHaving;
106182 106592     pNew->pOrderBy = pOrderBy;
106183 106593     pNew->selFlags = selFlags;
106184 106594     pNew->op = TK_SELECT;
106185 106595     pNew->pLimit = pLimit;
106186 106596     pNew->pOffset = pOffset;
106187         -  assert( pOffset==0 || pLimit!=0 );
       106597  +  assert( pOffset==0 || pLimit!=0 || pParse->nErr>0 || db->mallocFailed!=0 );
106188 106598     pNew->addrOpenEphm[0] = -1;
106189 106599     pNew->addrOpenEphm[1] = -1;
106190 106600     if( db->mallocFailed ) {
106191 106601       clearSelect(db, pNew, pNew!=&standin);
106192 106602       pNew = 0;
106193 106603     }else{
106194 106604       assert( pNew->pSrc!=0 || pParse->nErr>0 );
................................................................................
107430 107840   
107431 107841         assert( pTab && pExpr->pTab==pTab );
107432 107842         if( pS ){
107433 107843           /* The "table" is actually a sub-select or a view in the FROM clause
107434 107844           ** of the SELECT statement. Return the declaration type and origin
107435 107845           ** data for the result-set column of the sub-select.
107436 107846           */
107437         -        if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
       107847  +        if( iCol>=0 && iCol<pS->pEList->nExpr ){
107438 107848             /* If iCol is less than zero, then the expression requests the
107439 107849             ** rowid of the sub-select or view. This expression is legal (see 
107440 107850             ** test case misc2.2.2) - it always evaluates to NULL.
107441 107851             */
107442 107852             NameContext sNC;
107443 107853             Expr *p = pS->pEList->a[iCol].pExpr;
107444 107854             sNC.pSrcList = pS->pSrc;
................................................................................
107750 108160     assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
107751 108161     if( db->mallocFailed ) return;
107752 108162     memset(&sNC, 0, sizeof(sNC));
107753 108163     sNC.pSrcList = pSelect->pSrc;
107754 108164     a = pSelect->pEList->a;
107755 108165     for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
107756 108166       p = a[i].pExpr;
107757         -    pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst));
       108167  +    if( pCol->zType==0 ){
       108168  +      pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst));
       108169  +    }
107758 108170       szAll += pCol->szEst;
107759 108171       pCol->affinity = sqlite3ExprAffinity(p);
107760 108172       if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
107761 108173       pColl = sqlite3ExprCollSeq(pParse, p);
107762         -    if( pColl ){
       108174  +    if( pColl && pCol->zColl==0 ){
107763 108175         pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
107764 108176       }
107765 108177     }
107766 108178     pTab->szTabRow = sqlite3LogEst(szAll*4);
107767 108179   }
107768 108180   
107769 108181   /*
................................................................................
108157 108569     Select *p,            /* The right-most of SELECTs to be coded */
108158 108570     SelectDest *pDest     /* What to do with query results */
108159 108571   ){
108160 108572     Select *pPrior;
108161 108573     int nExpr = p->pEList->nExpr;
108162 108574     int nRow = 1;
108163 108575     int rc = 0;
108164         -  assert( p->pNext==0 );
108165         -  assert( p->selFlags & SF_AllValues );
       108576  +  assert( p->selFlags & SF_MultiValue );
108166 108577     do{
108167 108578       assert( p->selFlags & SF_Values );
108168 108579       assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
108169 108580       assert( p->pLimit==0 );
108170 108581       assert( p->pOffset==0 );
108171 108582       if( p->pEList->nExpr!=nExpr ){
108172 108583         selectWrongNumTermsError(pParse, p);
................................................................................
108267 108678       sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
108268 108679       sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
108269 108680       dest.eDest = SRT_Table;
108270 108681     }
108271 108682   
108272 108683     /* Special handling for a compound-select that originates as a VALUES clause.
108273 108684     */
108274         -  if( p->selFlags & SF_AllValues ){
       108685  +  if( p->selFlags & SF_MultiValue ){
108275 108686       rc = multiSelectValues(pParse, p, &dest);
108276 108687       goto multi_select_end;
108277 108688     }
108278 108689   
108279 108690     /* Make sure all SELECTs in the statement have the same number of elements
108280 108691     ** in their result sets.
108281 108692     */
................................................................................
108652 109063   #ifndef SQLITE_OMIT_SUBQUERY
108653 109064       /* If we are creating a set for an "expr IN (SELECT ...)" construct,
108654 109065       ** then there should be a single item on the stack.  Write this
108655 109066       ** item into the set table with bogus data.
108656 109067       */
108657 109068       case SRT_Set: {
108658 109069         int r1;
108659         -      assert( pIn->nSdst==1 );
       109070  +      assert( pIn->nSdst==1 || pParse->nErr>0 );
108660 109071         pDest->affSdst = 
108661 109072            sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
108662 109073         r1 = sqlite3GetTempReg(pParse);
108663 109074         sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
108664 109075         sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
108665 109076         sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
108666 109077         sqlite3ReleaseTempReg(pParse, r1);
................................................................................
108678 109089   #endif
108679 109090   
108680 109091       /* If this is a scalar select that is part of an expression, then
108681 109092       ** store the results in the appropriate memory cell and break out
108682 109093       ** of the scan loop.
108683 109094       */
108684 109095       case SRT_Mem: {
108685         -      assert( pIn->nSdst==1 );
       109096  +      assert( pIn->nSdst==1 || pParse->nErr>0 );  testcase( pIn->nSdst!=1 );
108686 109097         sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
108687 109098         /* The LIMIT clause will jump out of the loop for us */
108688 109099         break;
108689 109100       }
108690 109101   #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
108691 109102   
108692 109103       /* The results are stored in a sequence of registers
................................................................................
108693 109104       ** starting at pDest->iSdst.  Then the co-routine yields.
108694 109105       */
108695 109106       case SRT_Coroutine: {
108696 109107         if( pDest->iSdst==0 ){
108697 109108           pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
108698 109109           pDest->nSdst = pIn->nSdst;
108699 109110         }
108700         -      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pDest->nSdst);
       109111  +      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
108701 109112         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
108702 109113         break;
108703 109114       }
108704 109115   
108705 109116       /* If none of the above, then the result destination must be
108706 109117       ** SRT_Output.  This routine is never called with any other
108707 109118       ** destination other than the ones handled above or SRT_Output.
................................................................................
108909 109320     ** to the right and the left are evaluated, they use the correct
108910 109321     ** collation.
108911 109322     */
108912 109323     aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
108913 109324     if( aPermute ){
108914 109325       struct ExprList_item *pItem;
108915 109326       for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
108916         -      assert( pItem->u.x.iOrderByCol>0
108917         -          && pItem->u.x.iOrderByCol<=p->pEList->nExpr );
       109327  +      assert( pItem->u.x.iOrderByCol>0 );
       109328  +      /* assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr ) is also true
       109329  +      ** but only for well-formed SELECT statements. */
       109330  +      testcase( pItem->u.x.iOrderByCol > p->pEList->nExpr );
108918 109331         aPermute[i] = pItem->u.x.iOrderByCol - 1;
108919 109332       }
108920 109333       pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
108921 109334     }else{
108922 109335       pKeyMerge = 0;
108923 109336     }
108924 109337   
................................................................................
109120 109533     }
109121 109534     p->pPrior = pPrior;
109122 109535     pPrior->pNext = p;
109123 109536   
109124 109537     /*** TBD:  Insert subroutine calls to close cursors on incomplete
109125 109538     **** subqueries ****/
109126 109539     explainComposite(pParse, p->op, iSub1, iSub2, 0);
109127         -  return SQLITE_OK;
       109540  +  return pParse->nErr!=0;
109128 109541   }
109129 109542   #endif
109130 109543   
109131 109544   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
109132 109545   /* Forward Declarations */
109133 109546   static void substExprList(sqlite3*, ExprList*, int, ExprList*);
109134 109547   static void substSelect(sqlite3*, Select *, int, ExprList *);
................................................................................
109932 110345     p->op = TK_SELECT;
109933 110346     p->pWhere = 0;
109934 110347     pNew->pGroupBy = 0;
109935 110348     pNew->pHaving = 0;
109936 110349     pNew->pOrderBy = 0;
109937 110350     p->pPrior = 0;
109938 110351     p->pNext = 0;
       110352  +  p->pWith = 0;
109939 110353     p->selFlags &= ~SF_Compound;
109940 110354     assert( (p->selFlags & SF_Converted)==0 );
109941 110355     p->selFlags |= SF_Converted;
109942 110356     assert( pNew->pPrior!=0 );
109943 110357     pNew->pPrior->pNext = pNew;
109944 110358     pNew->pLimit = 0;
109945 110359     pNew->pOffset = 0;
................................................................................
110470 110884     w.xExprCallback = exprWalkNoop;
110471 110885     w.pParse = pParse;
110472 110886     if( pParse->hasCompound ){
110473 110887       w.xSelectCallback = convertCompoundSelectToSubquery;
110474 110888       sqlite3WalkSelect(&w, pSelect);
110475 110889     }
110476 110890     w.xSelectCallback = selectExpander;
110477         -  if( (pSelect->selFlags & SF_AllValues)==0 ){
       110891  +  if( (pSelect->selFlags & SF_MultiValue)==0 ){
110478 110892       w.xSelectCallback2 = selectPopWith;
110479 110893     }
110480 110894     sqlite3WalkSelect(&w, pSelect);
110481 110895   }
110482 110896   
110483 110897   
110484 110898   #ifndef SQLITE_OMIT_SUBQUERY
................................................................................
110656 111070         sqlite3ExprCodeExprList(pParse, pList, regAgg, SQLITE_ECEL_DUP);
110657 111071       }else{
110658 111072         nArg = 0;
110659 111073         regAgg = 0;
110660 111074       }
110661 111075       if( pF->iDistinct>=0 ){
110662 111076         addrNext = sqlite3VdbeMakeLabel(v);
110663         -      assert( nArg==1 );
       111077  +      testcase( nArg==0 );  /* Error condition */
       111078  +      testcase( nArg>1 );   /* Also an error */
110664 111079         codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
110665 111080       }
110666 111081       if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
110667 111082         CollSeq *pColl = 0;
110668 111083         struct ExprList_item *pItem;
110669 111084         int j;
110670 111085         assert( pList!=0 );  /* pList!=0 if pF->pFunc has NEEDCOLL */
................................................................................
111531 111946       generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
111532 111947     }
111533 111948   
111534 111949     /* Jump here to skip this query
111535 111950     */
111536 111951     sqlite3VdbeResolveLabel(v, iEnd);
111537 111952   
111538         -  /* The SELECT was successfully coded.   Set the return code to 0
111539         -  ** to indicate no errors.
111540         -  */
111541         -  rc = 0;
       111953  +  /* The SELECT has been coded. If there is an error in the Parse structure,
       111954  +  ** set the return code to 1. Otherwise 0. */
       111955  +  rc = (pParse->nErr>0);
111542 111956   
111543 111957     /* Control jumps to here if an error is encountered above, or upon
111544 111958     ** successful coding of the SELECT.
111545 111959     */
111546 111960   select_end:
111547 111961     explainSetInteger(pParse->iSelectId, iRestoreSelectId);
111548 111962   
................................................................................
111585 111999       int i;
111586 112000       pView = sqlite3TreeViewPush(pView, (n--)>0);
111587 112001       sqlite3TreeViewLine(pView, "FROM");
111588 112002       for(i=0; i<p->pSrc->nSrc; i++){
111589 112003         struct SrcList_item *pItem = &p->pSrc->a[i];
111590 112004         StrAccum x;
111591 112005         char zLine[100];
111592         -      sqlite3StrAccumInit(&x, zLine, sizeof(zLine), 0);
       112006  +      sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
111593 112007         sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
111594 112008         if( pItem->zDatabase ){
111595 112009           sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
111596 112010         }else if( pItem->zName ){
111597 112011           sqlite3XPrintf(&x, 0, " %s", pItem->zName);
111598 112012         }
111599 112013         if( pItem->pTab ){
................................................................................
111744 112158     */
111745 112159     if( argv!=0 ){
111746 112160       for(i=0; i<nCol; i++){
111747 112161         if( argv[i]==0 ){
111748 112162           z = 0;
111749 112163         }else{
111750 112164           int n = sqlite3Strlen30(argv[i])+1;
111751         -        z = sqlite3_malloc( n );
       112165  +        z = sqlite3_malloc64( n );
111752 112166           if( z==0 ) goto malloc_failed;
111753 112167           memcpy(z, argv[i], n);
111754 112168         }
111755 112169         p->azResult[p->nData++] = z;
111756 112170       }
111757 112171       p->nRow++;
111758 112172     }
................................................................................
111793 112207     if( pzErrMsg ) *pzErrMsg = 0;
111794 112208     res.zErrMsg = 0;
111795 112209     res.nRow = 0;
111796 112210     res.nColumn = 0;
111797 112211     res.nData = 1;
111798 112212     res.nAlloc = 20;
111799 112213     res.rc = SQLITE_OK;
111800         -  res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
       112214  +  res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
111801 112215     if( res.azResult==0 ){
111802 112216        db->errCode = SQLITE_NOMEM;
111803 112217        return SQLITE_NOMEM;
111804 112218     }
111805 112219     res.azResult[0] = 0;
111806 112220     rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
111807 112221     assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
................................................................................
111821 112235     sqlite3_free(res.zErrMsg);
111822 112236     if( rc!=SQLITE_OK ){
111823 112237       sqlite3_free_table(&res.azResult[1]);
111824 112238       return rc;
111825 112239     }
111826 112240     if( res.nAlloc>res.nData ){
111827 112241       char **azNew;
111828         -    azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
       112242  +    azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData );
111829 112243       if( azNew==0 ){
111830 112244         sqlite3_free_table(&res.azResult[1]);
111831 112245         db->errCode = SQLITE_NOMEM;
111832 112246         return SQLITE_NOMEM;
111833 112247       }
111834 112248       res.azResult = azNew;
111835 112249     }
................................................................................
112049 112463       }
112050 112464       goto trigger_cleanup;
112051 112465     }
112052 112466   
112053 112467     /* Do not create a trigger on a system table */
112054 112468     if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
112055 112469       sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
112056         -    pParse->nErr++;
112057 112470       goto trigger_cleanup;
112058 112471     }
112059 112472   
112060 112473     /* INSTEAD of triggers are only for views and views only support INSTEAD
112061 112474     ** of triggers.
112062 112475     */
112063 112476     if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
................................................................................
112229 112642   static TriggerStep *triggerStepAllocate(
112230 112643     sqlite3 *db,                /* Database connection */
112231 112644     u8 op,                      /* Trigger opcode */
112232 112645     Token *pName                /* The target name */
112233 112646   ){
112234 112647     TriggerStep *pTriggerStep;
112235 112648   
112236         -  pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
       112649  +  pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
112237 112650     if( pTriggerStep ){
112238 112651       char *z = (char*)&pTriggerStep[1];
112239 112652       memcpy(z, pName->z, pName->n);
112240         -    pTriggerStep->target.z = z;
112241         -    pTriggerStep->target.n = pName->n;
       112653  +    sqlite3Dequote(z);
       112654  +    pTriggerStep->zTarget = z;
112242 112655       pTriggerStep->op = op;
112243 112656     }
112244 112657     return pTriggerStep;
112245 112658   }
112246 112659   
112247 112660   /*
112248 112661   ** Build a trigger step out of an INSERT statement.  Return a pointer
................................................................................
112517 112930     if( pMask ){
112518 112931       *pMask = mask;
112519 112932     }
112520 112933     return (mask ? pList : 0);
112521 112934   }
112522 112935   
112523 112936   /*
112524         -** Convert the pStep->target token into a SrcList and return a pointer
       112937  +** Convert the pStep->zTarget string into a SrcList and return a pointer
112525 112938   ** to that SrcList.
112526 112939   **
112527 112940   ** This routine adds a specific database name, if needed, to the target when
112528 112941   ** forming the SrcList.  This prevents a trigger in one database from
112529 112942   ** referring to a target in another database.  An exception is when the
112530 112943   ** trigger is in TEMP in which case it can refer to any other database it
112531 112944   ** wants.
112532 112945   */
112533 112946   static SrcList *targetSrcList(
112534 112947     Parse *pParse,       /* The parsing context */
112535 112948     TriggerStep *pStep   /* The trigger containing the target token */
112536 112949   ){
       112950  +  sqlite3 *db = pParse->db;
112537 112951     int iDb;             /* Index of the database to use */
112538 112952     SrcList *pSrc;       /* SrcList to be returned */
112539 112953   
112540         -  pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
       112954  +  pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
112541 112955     if( pSrc ){
112542 112956       assert( pSrc->nSrc>0 );
112543         -    assert( pSrc->a!=0 );
112544         -    iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
       112957  +    pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget);
       112958  +    iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema);
112545 112959       if( iDb==0 || iDb>=2 ){
112546         -      sqlite3 *db = pParse->db;
112547         -      assert( iDb<pParse->db->nDb );
       112960  +      assert( iDb<db->nDb );
112548 112961         pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
112549 112962       }
112550 112963     }
112551 112964     return pSrc;
112552 112965   }
112553 112966   
112554 112967   /*
................................................................................
112652 113065   */
112653 113066   static void transferParseError(Parse *pTo, Parse *pFrom){
112654 113067     assert( pFrom->zErrMsg==0 || pFrom->nErr );
112655 113068     assert( pTo->zErrMsg==0 || pTo->nErr );
112656 113069     if( pTo->nErr==0 ){
112657 113070       pTo->zErrMsg = pFrom->zErrMsg;
112658 113071       pTo->nErr = pFrom->nErr;
       113072  +    pTo->rc = pFrom->rc;
112659 113073     }else{
112660 113074       sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
112661 113075     }
112662 113076   }
112663 113077   
112664 113078   /*
112665 113079   ** Create and populate a new TriggerPrg object with a sub-program 
................................................................................
114002 114416         "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
114003 114417     if( rc!=SQLITE_OK ) goto end_of_vacuum;
114004 114418   
114005 114419     /* Loop through the tables in the main database. For each, do
114006 114420     ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
114007 114421     ** the contents to the temporary database.
114008 114422     */
       114423  +  assert( (db->flags & SQLITE_Vacuum)==0 );
       114424  +  db->flags |= SQLITE_Vacuum;
114009 114425     rc = execExecSql(db, pzErrMsg,
114010 114426         "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
114011 114427         "|| ' SELECT * FROM main.' || quote(name) || ';'"
114012 114428         "FROM main.sqlite_master "
114013 114429         "WHERE type = 'table' AND name!='sqlite_sequence' "
114014 114430         "  AND coalesce(rootpage,1)>0"
114015 114431     );
       114432  +  assert( (db->flags & SQLITE_Vacuum)!=0 );
       114433  +  db->flags &= ~SQLITE_Vacuum;
114016 114434     if( rc!=SQLITE_OK ) goto end_of_vacuum;
114017 114435   
114018 114436     /* Copy over the sequence table
114019 114437     */
114020 114438     rc = execExecSql(db, pzErrMsg,
114021 114439         "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
114022 114440         "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
................................................................................
114147 114565   ** this struct allocated on the stack. It is used by the implementation of 
114148 114566   ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
114149 114567   ** are invoked only from within xCreate and xConnect methods.
114150 114568   */
114151 114569   struct VtabCtx {
114152 114570     VTable *pVTable;    /* The virtual table being constructed */
114153 114571     Table *pTab;        /* The Table object to which the virtual table belongs */
       114572  +  VtabCtx *pPrior;    /* Parent context (if any) */
       114573  +  int bDeclared;      /* True after sqlite3_declare_vtab() is called */
114154 114574   };
114155 114575   
114156 114576   /*
114157 114577   ** The actual function that does the work of creating a new module.
114158 114578   ** This function implements the sqlite3_create_module() and
114159 114579   ** sqlite3_create_module_v2() interfaces.
114160 114580   */
................................................................................
114593 115013   */
114594 115014   SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
114595 115015     Token *pArg = &pParse->sArg;
114596 115016     if( pArg->z==0 ){
114597 115017       pArg->z = p->z;
114598 115018       pArg->n = p->n;
114599 115019     }else{
114600         -    assert(pArg->z < p->z);
       115020  +    assert(pArg->z <= p->z);
114601 115021       pArg->n = (int)(&p->z[p->n] - pArg->z);
114602 115022     }
114603 115023   }
114604 115024   
114605 115025   /*
114606 115026   ** Invoke a virtual table constructor (either xCreate or xConnect). The
114607 115027   ** pointer to the function to invoke is passed as the fourth parameter
................................................................................
114610 115030   static int vtabCallConstructor(
114611 115031     sqlite3 *db, 
114612 115032     Table *pTab,
114613 115033     Module *pMod,
114614 115034     int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
114615 115035     char **pzErr
114616 115036   ){
114617         -  VtabCtx sCtx, *pPriorCtx;
       115037  +  VtabCtx sCtx;
114618 115038     VTable *pVTable;
114619 115039     int rc;
114620 115040     const char *const*azArg = (const char *const*)pTab->azModuleArg;
114621 115041     int nArg = pTab->nModuleArg;
114622 115042     char *zErr = 0;
114623         -  char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
       115043  +  char *zModuleName;
114624 115044     int iDb;
       115045  +  VtabCtx *pCtx;
114625 115046   
       115047  +  /* Check that the virtual-table is not already being initialized */
       115048  +  for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
       115049  +    if( pCtx->pTab==pTab ){
       115050  +      *pzErr = sqlite3MPrintf(db, 
       115051  +          "vtable constructor called recursively: %s", pTab->zName
       115052  +      );
       115053  +      return SQLITE_LOCKED;
       115054  +    }
       115055  +  }
       115056  +
       115057  +  zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
114626 115058     if( !zModuleName ){
114627 115059       return SQLITE_NOMEM;
114628 115060     }
114629 115061   
114630 115062     pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
114631 115063     if( !pVTable ){
114632 115064       sqlite3DbFree(db, zModuleName);
................................................................................
114639 115071     pTab->azModuleArg[1] = db->aDb[iDb].zName;
114640 115072   
114641 115073     /* Invoke the virtual table constructor */
114642 115074     assert( &db->pVtabCtx );
114643 115075     assert( xConstruct );
114644 115076     sCtx.pTab = pTab;
114645 115077     sCtx.pVTable = pVTable;
114646         -  pPriorCtx = db->pVtabCtx;
       115078  +  sCtx.pPrior = db->pVtabCtx;
       115079  +  sCtx.bDeclared = 0;
114647 115080     db->pVtabCtx = &sCtx;
114648 115081     rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
114649         -  db->pVtabCtx = pPriorCtx;
       115082  +  db->pVtabCtx = sCtx.pPrior;
114650 115083     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
       115084  +  assert( sCtx.pTab==pTab );
114651 115085   
114652 115086     if( SQLITE_OK!=rc ){
114653 115087       if( zErr==0 ){
114654 115088         *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
114655 115089       }else {
114656 115090         *pzErr = sqlite3MPrintf(db, "%s", zErr);
114657 115091         sqlite3_free(zErr);
................................................................................
114659 115093       sqlite3DbFree(db, pVTable);
114660 115094     }else if( ALWAYS(pVTable->pVtab) ){
114661 115095       /* Justification of ALWAYS():  A correct vtab constructor must allocate
114662 115096       ** the sqlite3_vtab object if successful.  */
114663 115097       memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
114664 115098       pVTable->pVtab->pModule = pMod->pModule;
114665 115099       pVTable->nRef = 1;
114666         -    if( sCtx.pTab ){
       115100  +    if( sCtx.bDeclared==0 ){
114667 115101         const char *zFormat = "vtable constructor did not declare schema: %s";
114668 115102         *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
114669 115103         sqlite3VtabUnlock(pVTable);
114670 115104         rc = SQLITE_ERROR;
114671 115105       }else{
114672 115106         int iCol;
       115107  +      u8 oooHidden = 0;
114673 115108         /* If everything went according to plan, link the new VTable structure
114674 115109         ** into the linked list headed by pTab->pVTable. Then loop through the 
114675 115110         ** columns of the table to see if any of them contain the token "hidden".
114676 115111         ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
114677 115112         ** the type string.  */
114678 115113         pVTable->pNext = pTab->pVTable;
114679 115114         pTab->pVTable = pVTable;
114680 115115   
114681 115116         for(iCol=0; iCol<pTab->nCol; iCol++){
114682 115117           char *zType = pTab->aCol[iCol].zType;
114683 115118           int nType;
114684 115119           int i = 0;
114685         -        if( !zType ) continue;
       115120  +        if( !zType ){
       115121  +          pTab->tabFlags |= oooHidden;
       115122  +          continue;
       115123  +        }
114686 115124           nType = sqlite3Strlen30(zType);
114687 115125           if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
114688 115126             for(i=0; i<nType; i++){
114689 115127               if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
114690 115128                && (zType[i+7]=='\0' || zType[i+7]==' ')
114691 115129               ){
114692 115130                 i++;
................................................................................
114701 115139               zType[j] = zType[j+nDel];
114702 115140             }
114703 115141             if( zType[i]=='\0' && i>0 ){
114704 115142               assert(zType[i-1]==' ');
114705 115143               zType[i-1] = '\0';
114706 115144             }
114707 115145             pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
       115146  +          oooHidden = TF_OOOHidden;
       115147  +        }else{
       115148  +          pTab->tabFlags |= oooHidden;
114708 115149           }
114709 115150         }
114710 115151       }
114711 115152     }
114712 115153   
114713 115154     sqlite3DbFree(db, zModuleName);
114714 115155     return rc;
................................................................................
114829 115270   
114830 115271   /*
114831 115272   ** This function is used to set the schema of a virtual table.  It is only
114832 115273   ** valid to call this function from within the xCreate() or xConnect() of a
114833 115274   ** virtual table module.
114834 115275   */
114835 115276   SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
       115277  +  VtabCtx *pCtx;
114836 115278     Parse *pParse;
114837         -
114838 115279     int rc = SQLITE_OK;
114839 115280     Table *pTab;
114840 115281     char *zErr = 0;
114841 115282   
114842 115283   #ifdef SQLITE_ENABLE_API_ARMOR
114843 115284     if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
114844 115285       return SQLITE_MISUSE_BKPT;
114845 115286     }
114846 115287   #endif
114847 115288     sqlite3_mutex_enter(db->mutex);
114848         -  if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
       115289  +  pCtx = db->pVtabCtx;
       115290  +  if( !pCtx || pCtx->bDeclared ){
114849 115291       sqlite3Error(db, SQLITE_MISUSE);
114850 115292       sqlite3_mutex_leave(db->mutex);
114851 115293       return SQLITE_MISUSE_BKPT;
114852 115294     }
       115295  +  pTab = pCtx->pTab;
114853 115296     assert( (pTab->tabFlags & TF_Virtual)!=0 );
114854 115297   
114855 115298     pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
114856 115299     if( pParse==0 ){
114857 115300       rc = SQLITE_NOMEM;
114858 115301     }else{
114859 115302       pParse->declareVtab = 1;
................................................................................
114868 115311       ){
114869 115312         if( !pTab->aCol ){
114870 115313           pTab->aCol = pParse->pNewTable->aCol;
114871 115314           pTab->nCol = pParse->pNewTable->nCol;
114872 115315           pParse->pNewTable->nCol = 0;
114873 115316           pParse->pNewTable->aCol = 0;
114874 115317         }
114875         -      db->pVtabCtx->pTab = 0;
       115318  +      pCtx->bDeclared = 1;
114876 115319       }else{
114877 115320         sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
114878 115321         sqlite3DbFree(db, zErr);
114879 115322         rc = SQLITE_ERROR;
114880 115323       }
114881 115324       pParse->declareVtab = 0;
114882 115325     
................................................................................
115062 115505   ** function immediately. If all calls to virtual table methods are successful,
115063 115506   ** SQLITE_OK is returned.
115064 115507   */
115065 115508   SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
115066 115509     int rc = SQLITE_OK;
115067 115510   
115068 115511     assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
115069         -  assert( iSavepoint>=0 );
       115512  +  assert( iSavepoint>=-1 );
115070 115513     if( db->aVTrans ){
115071 115514       int i;
115072 115515       for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
115073 115516         VTable *pVTab = db->aVTrans[i];
115074 115517         const sqlite3_module *pMod = pVTab->pMod->pModule;
115075 115518         if( pVTab->pVtab && pMod->iVersion>=2 ){
115076 115519           int (*xMethod)(sqlite3_vtab *, int);
................................................................................
115180 115623     Table **apVtabLock;
115181 115624   
115182 115625     assert( IsVirtual(pTab) );
115183 115626     for(i=0; i<pToplevel->nVtabLock; i++){
115184 115627       if( pTab==pToplevel->apVtabLock[i] ) return;
115185 115628     }
115186 115629     n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
115187         -  apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
       115630  +  apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n);
115188 115631     if( apVtabLock ){
115189 115632       pToplevel->apVtabLock = apVtabLock;
115190 115633       pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
115191 115634     }else{
115192 115635       pToplevel->db->mallocFailed = 1;
115193 115636     }
115194 115637   }
................................................................................
115979 116422   ** does is make slot[] entries point to substructure within pExpr.
115980 116423   **
115981 116424   ** In the previous sentence and in the diagram, "slot[]" refers to
115982 116425   ** the WhereClause.a[] array.  The slot[] array grows as needed to contain
115983 116426   ** all terms of the WHERE clause.
115984 116427   */
115985 116428   static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
       116429  +  Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
115986 116430     pWC->op = op;
115987         -  if( pExpr==0 ) return;
115988         -  if( pExpr->op!=op ){
       116431  +  if( pE2==0 ) return;
       116432  +  if( pE2->op!=op ){
115989 116433       whereClauseInsert(pWC, pExpr, 0);
115990 116434     }else{
115991         -    whereSplit(pWC, pExpr->pLeft, op);
115992         -    whereSplit(pWC, pExpr->pRight, op);
       116435  +    whereSplit(pWC, pE2->pLeft, op);
       116436  +    whereSplit(pWC, pE2->pRight, op);
115993 116437     }
115994 116438   }
115995 116439   
115996 116440   /*
115997 116441   ** Initialize a WhereMaskSet object
115998 116442   */
115999 116443   #define initMaskSet(P)  (P)->n=0
................................................................................
117256 117700     for(i=0; i<pList->nExpr; i++){
117257 117701       Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
117258 117702       if( p->op==TK_COLUMN
117259 117703        && p->iColumn==pIdx->aiColumn[iCol]
117260 117704        && p->iTable==iBase
117261 117705       ){
117262 117706         CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
117263         -      if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
       117707  +      if( pColl && 0==sqlite3StrICmp(pColl->zName, zColl) ){
117264 117708           return i;
117265 117709         }
117266 117710       }
117267 117711     }
117268 117712   
117269 117713     return -1;
117270 117714   }
................................................................................
117530 117974         testcase( iCol==BMS-1 );
117531 117975         testcase( iCol==BMS );
117532 117976         if( (idxCols & cMask)==0 ){
117533 117977           Expr *pX = pTerm->pExpr;
117534 117978           idxCols |= cMask;
117535 117979           pIdx->aiColumn[n] = pTerm->u.leftColumn;
117536 117980           pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
117537         -        pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
       117981  +        pIdx->azColl[n] = pColl ? pColl->zName : "BINARY";
117538 117982           n++;
117539 117983         }
117540 117984       }
117541 117985     }
117542 117986     assert( (u32)n==pLoop->u.btree.nEq );
117543 117987   
117544 117988     /* Add additional columns needed to make the automatic index into
................................................................................
118826 119270       flags = pLoop->wsFlags;
118827 119271       if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
118828 119272   
118829 119273       isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
118830 119274               || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
118831 119275               || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
118832 119276   
118833         -    sqlite3StrAccumInit(&str, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
118834         -    str.db = db;
       119277  +    sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
118835 119278       sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
118836 119279       if( pItem->pSelect ){
118837 119280         sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
118838 119281       }else{
118839 119282         sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
118840 119283       }
118841 119284   
................................................................................
120026 120469   }
120027 120470   
120028 120471   /*
120029 120472   ** Free a WhereInfo structure
120030 120473   */
120031 120474   static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
120032 120475     if( ALWAYS(pWInfo) ){
       120476  +    int i;
       120477  +    for(i=0; i<pWInfo->nLevel; i++){
       120478  +      WhereLevel *pLevel = &pWInfo->a[i];
       120479  +      if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
       120480  +        sqlite3DbFree(db, pLevel->u.in.aInLoop);
       120481  +      }
       120482  +    }
120033 120483       whereClauseClear(&pWInfo->sWC);
120034 120484       while( pWInfo->pLoops ){
120035 120485         WhereLoop *p = pWInfo->pLoops;
120036 120486         pWInfo->pLoops = p->pNextLoop;
120037 120487         whereLoopDelete(db, p);
120038 120488       }
120039 120489       sqlite3DbFree(db, pWInfo);
................................................................................
120505 120955         }
120506 120956         assert( nIn>0 );  /* RHS always has 2 or more terms...  The parser
120507 120957                           ** changes "x IN (?)" into "x=?". */
120508 120958   
120509 120959       }else if( eOp & (WO_EQ) ){
120510 120960         pNew->wsFlags |= WHERE_COLUMN_EQ;
120511 120961         if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
120512         -        if( iCol>=0 && !IsUniqueIndex(pProbe) ){
       120962  +        if( iCol>=0 && pProbe->uniqNotNull==0 ){
120513 120963             pNew->wsFlags |= WHERE_UNQ_WANTED;
120514 120964           }else{
120515 120965             pNew->wsFlags |= WHERE_ONEROW;
120516 120966           }
120517 120967         }
120518 120968       }else if( eOp & WO_ISNULL ){
120519 120969         pNew->wsFlags |= WHERE_COLUMN_NULL;
................................................................................
121965 122415         }
121966 122416       }else{
121967 122417         pWInfo->nOBSat = pFrom->isOrdered;
121968 122418         if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0;
121969 122419         pWInfo->revMask = pFrom->revLoop;
121970 122420       }
121971 122421       if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
121972         -        && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr
       122422  +        && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
121973 122423       ){
121974 122424         Bitmask revMask = 0;
121975 122425         int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, 
121976 122426             pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
121977 122427         );
121978 122428         assert( pWInfo->sorted==0 );
121979 122429         if( nOrder==pWInfo->pOrderBy->nExpr ){
................................................................................
122370 122820        pWInfo->revMask = (Bitmask)(-1);
122371 122821     }
122372 122822     if( pParse->nErr || NEVER(db->mallocFailed) ){
122373 122823       goto whereBeginError;
122374 122824     }
122375 122825   #ifdef WHERETRACE_ENABLED /* !=0 */
122376 122826     if( sqlite3WhereTrace ){
122377         -    int ii;
122378 122827       sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
122379 122828       if( pWInfo->nOBSat>0 ){
122380 122829         sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
122381 122830       }
122382 122831       switch( pWInfo->eDistinct ){
122383 122832         case WHERE_DISTINCT_UNIQUE: {
122384 122833           sqlite3DebugPrintf("  DISTINCT=unique");
................................................................................
122623 123072           sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
122624 123073           sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
122625 123074           VdbeCoverage(v);
122626 123075           VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen);
122627 123076           VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen);
122628 123077           sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
122629 123078         }
122630         -      sqlite3DbFree(db, pLevel->u.in.aInLoop);
122631 123079       }
122632 123080       sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
122633 123081       if( pLevel->addrSkip ){
122634 123082         sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip);
122635 123083         VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
122636 123084         sqlite3VdbeJumpHere(v, pLevel->addrSkip);
122637 123085         sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
................................................................................
122834 123282   struct TrigEvent { int a; IdList * b; };
122835 123283   
122836 123284   /*
122837 123285   ** An instance of this structure holds the ATTACH key and the key type.
122838 123286   */
122839 123287   struct AttachKey { int type;  Token key; };
122840 123288   
       123289  +
       123290  +  /*
       123291  +  ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
       123292  +  ** all elements in the list.  And make sure list length does not exceed
       123293  +  ** SQLITE_LIMIT_COMPOUND_SELECT.
       123294  +  */
       123295  +  static void parserDoubleLinkSelect(Parse *pParse, Select *p){
       123296  +    if( p->pPrior ){
       123297  +      Select *pNext = 0, *pLoop;
       123298  +      int mxSelect, cnt = 0;
       123299  +      for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
       123300  +        pLoop->pNext = pNext;
       123301  +        pLoop->selFlags |= SF_Compound;
       123302  +      }
       123303  +      if( (p->selFlags & SF_MultiValue)==0 && 
       123304  +        (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
       123305  +        cnt>mxSelect
       123306  +      ){
       123307  +        sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
       123308  +      }
       123309  +    }
       123310  +  }
122841 123311   
122842 123312     /* This is a utility routine used to set the ExprSpan.zStart and
122843 123313     ** ExprSpan.zEnd values of pOut so that the span covers the complete
122844 123314     ** range of text beginning with pStart and going to the end of pEnd.
122845 123315     */
122846 123316     static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
122847 123317       pOut->zStart = pStart->z;
................................................................................
125151 125621     SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
125152 125622     sqlite3Select(pParse, yymsp[0].minor.yy3, &dest);
125153 125623     sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
125154 125624   }
125155 125625           break;
125156 125626         case 112: /* select ::= with selectnowith */
125157 125627   {
125158         -  Select *p = yymsp[0].minor.yy3, *pNext, *pLoop;
       125628  +  Select *p = yymsp[0].minor.yy3;
125159 125629     if( p ){
125160         -    int cnt = 0, mxSelect;
125161 125630       p->pWith = yymsp[-1].minor.yy59;
125162         -    if( p->pPrior ){
125163         -      u16 allValues = SF_Values;
125164         -      pNext = 0;
125165         -      for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
125166         -        pLoop->pNext = pNext;
125167         -        pLoop->selFlags |= SF_Compound;
125168         -        allValues &= pLoop->selFlags;
125169         -      }
125170         -      if( allValues ){
125171         -        p->selFlags |= SF_AllValues;
125172         -      }else if(
125173         -        (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0
125174         -        && cnt>mxSelect
125175         -      ){
125176         -        sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
125177         -      }
125178         -    }
       125631  +    parserDoubleLinkSelect(pParse, p);
125179 125632     }else{
125180 125633       sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy59);
125181 125634     }
125182 125635     yygotominor.yy3 = p;
125183 125636   }
125184 125637           break;
125185 125638         case 113: /* selectnowith ::= oneselect */
................................................................................
125189 125642         case 114: /* selectnowith ::= selectnowith multiselect_op oneselect */
125190 125643   {
125191 125644     Select *pRhs = yymsp[0].minor.yy3;
125192 125645     if( pRhs && pRhs->pPrior ){
125193 125646       SrcList *pFrom;
125194 125647       Token x;
125195 125648       x.n = 0;
       125649  +    parserDoubleLinkSelect(pParse, pRhs);
125196 125650       pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
125197 125651       pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0);
125198 125652     }
125199 125653     if( pRhs ){
125200 125654       pRhs->op = (u8)yymsp[-1].minor.yy328;
125201 125655       pRhs->pPrior = yymsp[-2].minor.yy3;
       125656  +    pRhs->selFlags &= ~SF_MultiValue;
125202 125657       if( yymsp[-1].minor.yy328!=TK_ALL ) pParse->hasCompound = 1;
125203 125658     }else{
125204 125659       sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy3);
125205 125660     }
125206 125661     yygotominor.yy3 = pRhs;
125207 125662   }
125208 125663           break;
................................................................................
125241 125696         case 120: /* values ::= VALUES LP nexprlist RP */
125242 125697   {
125243 125698     yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0);
125244 125699   }
125245 125700           break;
125246 125701         case 121: /* values ::= values COMMA LP exprlist RP */
125247 125702   {
125248         -  Select *pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0);
       125703  +  Select *pRight, *pLeft = yymsp[-4].minor.yy3;
       125704  +  pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values|SF_MultiValue,0,0);
       125705  +  if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
125249 125706     if( pRight ){
125250 125707       pRight->op = TK_ALL;
125251         -    pRight->pPrior = yymsp[-4].minor.yy3;
       125708  +    pLeft = yymsp[-4].minor.yy3;
       125709  +    pRight->pPrior = pLeft;
125252 125710       yygotominor.yy3 = pRight;
125253 125711     }else{
125254         -    yygotominor.yy3 = yymsp[-4].minor.yy3;
       125712  +    yygotominor.yy3 = pLeft;
125255 125713     }
125256 125714   }
125257 125715           break;
125258 125716         case 122: /* distinct ::= DISTINCT */
125259 125717   {yygotominor.yy381 = SF_Distinct;}
125260 125718           break;
125261 125719         case 123: /* distinct ::= ALL */
................................................................................
127051 127509             sqlite3ErrorMsg(pParse, "interrupt");
127052 127510             pParse->rc = SQLITE_INTERRUPT;
127053 127511             goto abort_parse;
127054 127512           }
127055 127513           break;
127056 127514         }
127057 127515         case TK_ILLEGAL: {
127058         -        sqlite3DbFree(db, *pzErrMsg);
127059         -        *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
       127516  +        sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
127060 127517                           &pParse->sLastToken);
127061         -        nErr++;
127062 127518           goto abort_parse;
127063 127519         }
127064 127520         case TK_SEMI: {
127065 127521           pParse->zTail = &zSql[i];
127066 127522           /* Fall thru into the default case */
127067 127523         }
127068 127524         default: {
................................................................................
127072 127528             goto abort_parse;
127073 127529           }
127074 127530           break;
127075 127531         }
127076 127532       }
127077 127533     }
127078 127534   abort_parse:
127079         -  if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
       127535  +  assert( nErr==0 );
       127536  +  if( zSql[i]==0 && pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
127080 127537       if( lastTokenParsed!=TK_SEMI ){
127081 127538         sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
127082 127539         pParse->zTail = &zSql[i];
127083 127540       }
127084         -    sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
       127541  +    if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
       127542  +      sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
       127543  +    }
127085 127544     }
127086 127545   #ifdef YYTRACKMAXSTACKDEPTH
127087 127546     sqlite3_mutex_enter(sqlite3MallocMutex());
127088 127547     sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
127089 127548         sqlite3ParserStackPeak(pEngine)
127090 127549     );
127091 127550     sqlite3_mutex_leave(sqlite3MallocMutex());
................................................................................
127138 127597       sqlite3DbFree(db, p);
127139 127598     }
127140 127599     while( pParse->pZombieTab ){
127141 127600       Table *p = pParse->pZombieTab;
127142 127601       pParse->pZombieTab = p->pNextZombie;
127143 127602       sqlite3DeleteTable(db, p);
127144 127603     }
127145         -  if( nErr>0 && pParse->rc==SQLITE_OK ){
127146         -    pParse->rc = SQLITE_ERROR;
127147         -  }
       127604  +  assert( nErr==0 || pParse->rc!=SQLITE_OK );
127148 127605     return nErr;
127149 127606   }
127150 127607   
127151 127608   /************** End of tokenize.c ********************************************/
127152 127609   /************** Begin file complete.c ****************************************/
127153 127610   /*
127154 127611   ** 2001 September 15
................................................................................
127416 127873   ** This routine is the same as the sqlite3_complete() routine described
127417 127874   ** above, except that the parameter is required to be UTF-16 encoded, not
127418 127875   ** UTF-8.
127419 127876   */
127420 127877   SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *zSql){
127421 127878     sqlite3_value *pVal;
127422 127879     char const *zSql8;
127423         -  int rc = SQLITE_NOMEM;
       127880  +  int rc;
127424 127881   
127425 127882   #ifndef SQLITE_OMIT_AUTOINIT
127426 127883     rc = sqlite3_initialize();
127427 127884     if( rc ) return rc;
127428 127885   #endif
127429 127886     pVal = sqlite3ValueNew(0);
127430 127887     sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
................................................................................
127582 128039   
127583 128040   /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
127584 128041   ** zero if and only if SQLite was compiled with mutexing code omitted due to
127585 128042   ** the SQLITE_THREADSAFE compile-time option being set to 0.
127586 128043   */
127587 128044   SQLITE_API int SQLITE_STDCALL sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
127588 128045   
       128046  +/*
       128047  +** When compiling the test fixture or with debugging enabled (on Win32),
       128048  +** this variable being set to non-zero will cause OSTRACE macros to emit
       128049  +** extra diagnostic information.
       128050  +*/
       128051  +#ifdef SQLITE_HAVE_OS_TRACE
       128052  +# ifndef SQLITE_DEBUG_OS_TRACE
       128053  +#   define SQLITE_DEBUG_OS_TRACE 0
       128054  +# endif
       128055  +  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
       128056  +#endif
       128057  +
127589 128058   #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
127590 128059   /*
127591 128060   ** If the following function pointer is not NULL and if
127592 128061   ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
127593 128062   ** I/O active are written using this function.  These messages
127594 128063   ** are intended for debugging activity only.
127595 128064   */
................................................................................
128721 129190     }
128722 129191   }
128723 129192   
128724 129193   /*
128725 129194   ** Return a static string containing the name corresponding to the error code
128726 129195   ** specified in the argument.
128727 129196   */
128728         -#if (defined(SQLITE_DEBUG) && SQLITE_OS_WIN) || defined(SQLITE_TEST)
       129197  +#if defined(SQLITE_NEED_ERR_NAME)
128729 129198   SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
128730 129199     const char *zName = 0;
128731 129200     int i, origRc = rc;
128732 129201     for(i=0; i<2 && zName==0; i++, rc &= 0xff){
128733 129202       switch( rc ){
128734 129203         case SQLITE_OK:                 zName = "SQLITE_OK";                break;
128735 129204         case SQLITE_ERROR:              zName = "SQLITE_ERROR";             break;
................................................................................
129946 130415               || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
129947 130416      && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
129948 130417     ){
129949 130418       char *zOpt;
129950 130419       int eState;                   /* Parser state when parsing URI */
129951 130420       int iIn;                      /* Input character index */
129952 130421       int iOut = 0;                 /* Output character index */
129953         -    int nByte = nUri+2;           /* Bytes of space to allocate */
       130422  +    u64 nByte = nUri+2;           /* Bytes of space to allocate */
129954 130423   
129955 130424       /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen 
129956 130425       ** method that there may be extra parameters following the file-name.  */
129957 130426       flags |= SQLITE_OPEN_URI;
129958 130427   
129959 130428       for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
129960         -    zFile = sqlite3_malloc(nByte);
       130429  +    zFile = sqlite3_malloc64(nByte);
129961 130430       if( !zFile ) return SQLITE_NOMEM;
129962 130431   
129963 130432       iIn = 5;
129964 130433   #ifdef SQLITE_ALLOW_URI_AUTHORITY
129965 130434       if( strncmp(zUri+5, "///", 3)==0 ){
129966 130435         iIn = 7;
129967 130436         /* The following condition causes URIs with five leading / characters
................................................................................
130119 130588           }
130120 130589         }
130121 130590   
130122 130591         zOpt = &zVal[nVal+1];
130123 130592       }
130124 130593   
130125 130594     }else{
130126         -    zFile = sqlite3_malloc(nUri+2);
       130595  +    zFile = sqlite3_malloc64(nUri+2);
130127 130596       if( !zFile ) return SQLITE_NOMEM;
130128 130597       memcpy(zFile, zUri, nUri);
130129 130598       zFile[nUri] = '\0';
130130 130599       zFile[nUri+1] = '\0';
130131 130600       flags &= ~SQLITE_OPEN_URI;
130132 130601     }
130133 130602   
................................................................................
130390 130859   #endif
130391 130860   
130392 130861   #ifdef SQLITE_ENABLE_RTREE
130393 130862     if( !db->mallocFailed && rc==SQLITE_OK){
130394 130863       rc = sqlite3RtreeInit(db);
130395 130864     }
130396 130865   #endif
       130866  +
       130867  +#ifdef SQLITE_ENABLE_DBSTAT_VTAB
       130868  +  if( !db->mallocFailed && rc==SQLITE_OK){
       130869  +    int sqlite3_dbstat_register(sqlite3*);
       130870  +    rc = sqlite3_dbstat_register(db);
       130871  +  }
       130872  +#endif
130397 130873   
130398 130874     /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
130399 130875     ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
130400 130876     ** mode.  Doing nothing at all also makes NORMAL the default.
130401 130877     */
130402 130878   #ifdef SQLITE_DEFAULT_LOCKING_MODE
130403 130879     db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
................................................................................
132328 132804   /*
132329 132805   ** Macros indicating that conditional expressions are always true or
132330 132806   ** false.
132331 132807   */
132332 132808   #ifdef SQLITE_COVERAGE_TEST
132333 132809   # define ALWAYS(x) (1)
132334 132810   # define NEVER(X)  (0)
       132811  +#elif defined(SQLITE_DEBUG)
       132812  +# define ALWAYS(x) sqlite3Fts3Always((x)!=0)
       132813  +# define NEVER(x) sqlite3Fts3Never((x)!=0)
       132814  +SQLITE_PRIVATE int sqlite3Fts3Always(int b);
       132815  +SQLITE_PRIVATE int sqlite3Fts3Never(int b);
132335 132816   #else
132336 132817   # define ALWAYS(x) (x)
132337 132818   # define NEVER(x)  (x)
132338 132819   #endif
132339 132820   
132340 132821   /*
132341 132822   ** Internal types used by SQLite.
................................................................................
132728 133209   SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
132729 133210   
132730 133211   #define fts3GetVarint32(p, piVal) (                                           \
132731 133212     (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
132732 133213   )
132733 133214   
132734 133215   /* fts3.c */
       133216  +SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char**,const char*,...);
132735 133217   SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
132736 133218   SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
132737 133219   SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
132738 133220   SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
132739 133221   SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
132740 133222   SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
132741 133223   SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
................................................................................
132816 133298     SQLITE_EXTENSION_INIT1
132817 133299   #endif
132818 133300   
132819 133301   static int fts3EvalNext(Fts3Cursor *pCsr);
132820 133302   static int fts3EvalStart(Fts3Cursor *pCsr);
132821 133303   static int fts3TermSegReaderCursor(
132822 133304       Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
       133305  +
       133306  +#ifndef SQLITE_AMALGAMATION
       133307  +# if defined(SQLITE_DEBUG)
       133308  +SQLITE_PRIVATE int sqlite3Fts3Always(int b) { assert( b ); return b; }
       133309  +SQLITE_PRIVATE int sqlite3Fts3Never(int b)  { assert( !b ); return b; }
       133310  +# endif
       133311  +#endif
132823 133312   
132824 133313   /* 
132825 133314   ** Write a 64-bit variable-length integer to memory starting at p[0].
132826 133315   ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
132827 133316   ** The number of bytes written is returned.
132828 133317   */
132829 133318   SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
................................................................................
132926 133415     if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
132927 133416       int iIn = 1;                  /* Index of next byte to read from input */
132928 133417       int iOut = 0;                 /* Index of next byte to write to output */
132929 133418   
132930 133419       /* If the first byte was a '[', then the close-quote character is a ']' */
132931 133420       if( quote=='[' ) quote = ']';  
132932 133421   
132933         -    while( ALWAYS(z[iIn]) ){
       133422  +    while( z[iIn] ){
132934 133423         if( z[iIn]==quote ){
132935 133424           if( z[iIn+1]!=quote ) break;
132936 133425           z[iOut++] = quote;
132937 133426           iIn += 2;
132938 133427         }else{
132939 133428           z[iOut++] = z[iIn++];
132940 133429         }
................................................................................
133004 133493   
133005 133494     /* Invoke the tokenizer destructor to free the tokenizer. */
133006 133495     p->pTokenizer->pModule->xDestroy(p->pTokenizer);
133007 133496   
133008 133497     sqlite3_free(p);
133009 133498     return SQLITE_OK;
133010 133499   }
       133500  +
       133501  +/*
       133502  +** Write an error message into *pzErr
       133503  +*/
       133504  +SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){
       133505  +  va_list ap;
       133506  +  sqlite3_free(*pzErr);
       133507  +  va_start(ap, zFormat);
       133508  +  *pzErr = sqlite3_vmprintf(zFormat, ap);
       133509  +  va_end(ap);
       133510  +}
133011 133511   
133012 133512   /*
133013 133513   ** Construct one or more SQL statements from the format string given
133014 133514   ** and then evaluate those statements. The success code is written
133015 133515   ** into *pRc.
133016 133516   **
133017 133517   ** If *pRc is initially non-zero then this routine is a no-op.
................................................................................
133523 134023   */
133524 134024   static int fts3ContentColumns(
133525 134025     sqlite3 *db,                    /* Database handle */
133526 134026     const char *zDb,                /* Name of db (i.e. "main", "temp" etc.) */
133527 134027     const char *zTbl,               /* Name of content table */
133528 134028     const char ***pazCol,           /* OUT: Malloc'd array of column names */
133529 134029     int *pnCol,                     /* OUT: Size of array *pazCol */
133530         -  int *pnStr                      /* OUT: Bytes of string content */
       134030  +  int *pnStr,                     /* OUT: Bytes of string content */
       134031  +  char **pzErr                    /* OUT: error message */
133531 134032   ){
133532 134033     int rc = SQLITE_OK;             /* Return code */
133533 134034     char *zSql;                     /* "SELECT *" statement on zTbl */  
133534 134035     sqlite3_stmt *pStmt = 0;        /* Compiled version of zSql */
133535 134036   
133536 134037     zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
133537 134038     if( !zSql ){
133538 134039       rc = SQLITE_NOMEM;
133539 134040     }else{
133540 134041       rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
       134042  +    if( rc!=SQLITE_OK ){
       134043  +      sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db));
       134044  +    }
133541 134045     }
133542 134046     sqlite3_free(zSql);
133543 134047   
133544 134048     if( rc==SQLITE_OK ){
133545 134049       const char **azCol;           /* Output array */
133546 134050       int nStr = 0;                 /* Size of all column names (incl. 0x00) */
133547 134051       int nCol;                     /* Number of table columns */
................................................................................
133700 134204           for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
133701 134205             struct Fts4Option *pOp = &aFts4Opt[iOpt];
133702 134206             if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
133703 134207               break;
133704 134208             }
133705 134209           }
133706 134210           if( iOpt==SizeofArray(aFts4Opt) ){
133707         -          *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
       134211  +          sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z);
133708 134212             rc = SQLITE_ERROR;
133709 134213           }else{
133710 134214             switch( iOpt ){
133711 134215               case 0:               /* MATCHINFO */
133712 134216                 if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
133713         -                *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
       134217  +                sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal);
133714 134218                   rc = SQLITE_ERROR;
133715 134219                 }
133716 134220                 bNoDocsize = 1;
133717 134221                 break;
133718 134222   
133719 134223               case 1:               /* PREFIX */
133720 134224                 sqlite3_free(zPrefix);
................................................................................
133734 134238                 zVal = 0;
133735 134239                 break;
133736 134240   
133737 134241               case 4:               /* ORDER */
133738 134242                 if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) 
133739 134243                  && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) 
133740 134244                 ){
133741         -                *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal);
       134245  +                sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal);
133742 134246                   rc = SQLITE_ERROR;
133743 134247                 }
133744 134248                 bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
133745 134249                 break;
133746 134250   
133747 134251               case 5:              /* CONTENT */
133748 134252                 sqlite3_free(zContent);
................................................................................
133785 134289       sqlite3_free(zCompress); 
133786 134290       sqlite3_free(zUncompress); 
133787 134291       zCompress = 0;
133788 134292       zUncompress = 0;
133789 134293       if( nCol==0 ){
133790 134294         sqlite3_free((void*)aCol); 
133791 134295         aCol = 0;
133792         -      rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString);
       134296  +      rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr);
133793 134297   
133794 134298         /* If a languageid= option was specified, remove the language id
133795 134299         ** column from the aCol[] array. */ 
133796 134300         if( rc==SQLITE_OK && zLanguageid ){
133797 134301           int j;
133798 134302           for(j=0; j<nCol; j++){
133799 134303             if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
................................................................................
133820 134324       if( rc!=SQLITE_OK ) goto fts3_init_out;
133821 134325     }
133822 134326     assert( pTokenizer );
133823 134327   
133824 134328     rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
133825 134329     if( rc==SQLITE_ERROR ){
133826 134330       assert( zPrefix );
133827         -    *pzErr = sqlite3_mprintf("error parsing prefix parameter: %s", zPrefix);
       134331  +    sqlite3Fts3ErrMsg(pzErr, "error parsing prefix parameter: %s", zPrefix);
133828 134332     }
133829 134333     if( rc!=SQLITE_OK ) goto fts3_init_out;
133830 134334   
133831 134335     /* Allocate and populate the Fts3Table structure. */
133832 134336     nByte = sizeof(Fts3Table) +                  /* Fts3Table */
133833 134337             nCol * sizeof(char *) +              /* azColumn */
133834 134338             nIndex * sizeof(struct Fts3Index) +  /* aIndex */
................................................................................
133902 134406           sqlite3_free(zNot);
133903 134407           azNotindexed[i] = 0;
133904 134408         }
133905 134409       }
133906 134410     }
133907 134411     for(i=0; i<nNotindexed; i++){
133908 134412       if( azNotindexed[i] ){
133909         -      *pzErr = sqlite3_mprintf("no such column: %s", azNotindexed[i]);
       134413  +      sqlite3Fts3ErrMsg(pzErr, "no such column: %s", azNotindexed[i]);
133910 134414         rc = SQLITE_ERROR;
133911 134415       }
133912 134416     }
133913 134417   
133914 134418     if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){
133915 134419       char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
133916 134420       rc = SQLITE_ERROR;
133917         -    *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
       134421  +    sqlite3Fts3ErrMsg(pzErr, "missing %s parameter in fts4 constructor", zMiss);
133918 134422     }
133919 134423     p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
133920 134424     p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
133921 134425     if( rc!=SQLITE_OK ) goto fts3_init_out;
133922 134426   
133923 134427     /* If this is an xCreate call, create the underlying tables in the 
133924 134428     ** database. TODO: For xConnect(), it could verify that said tables exist.
................................................................................
135303 135807     ** for the pending-terms. If this is a scan, then this call must be being
135304 135808     ** made by an fts4aux module, not an FTS table. In this case calling
135305 135809     ** Fts3SegReaderPending might segfault, as the data structures used by 
135306 135810     ** fts4aux are not completely populated. So it's easiest to filter these
135307 135811     ** calls out here.  */
135308 135812     if( iLevel<0 && p->aIndex ){
135309 135813       Fts3SegReader *pSeg = 0;
135310         -    rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix, &pSeg);
       135814  +    rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg);
135311 135815       if( rc==SQLITE_OK && pSeg ){
135312 135816         rc = fts3SegReaderCursorAppend(pCsr, pSeg);
135313 135817       }
135314 135818     }
135315 135819   
135316 135820     if( iLevel!=FTS3_SEGCURSOR_PENDING ){
135317 135821       if( rc==SQLITE_OK ){
................................................................................
135952 136456   ** moves *ppPoslist so that it instead points to the first byte of the
135953 136457   ** same position list.
135954 136458   */
135955 136459   static void fts3ReversePoslist(char *pStart, char **ppPoslist){
135956 136460     char *p = &(*ppPoslist)[-2];
135957 136461     char c = 0;
135958 136462   
       136463  +  /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */
135959 136464     while( p>pStart && (c=*p--)==0 );
       136465  +
       136466  +  /* Search backwards for a varint with value zero (the end of the previous 
       136467  +  ** poslist). This is an 0x00 byte preceded by some byte that does not
       136468  +  ** have the 0x80 bit set.  */
135960 136469     while( p>pStart && (*p & 0x80) | c ){ 
135961 136470       c = *p--; 
135962 136471     }
135963         -  if( p>pStart ){ p = &p[2]; }
       136472  +  assert( p==pStart || c==0 );
       136473  +
       136474  +  /* At this point p points to that preceding byte without the 0x80 bit
       136475  +  ** set. So to find the start of the poslist, skip forward 2 bytes then
       136476  +  ** over a varint. 
       136477  +  **
       136478  +  ** Normally. The other case is that p==pStart and the poslist to return
       136479  +  ** is the first in the doclist. In this case do not skip forward 2 bytes.
       136480  +  ** The second part of the if condition (c==0 && *ppPoslist>&p[2])
       136481  +  ** is required for cases where the first byte of a doclist and the
       136482  +  ** doclist is empty. For example, if the first docid is 10, a doclist
       136483  +  ** that begins with:
       136484  +  **
       136485  +  **   0x0A 0x00 <next docid delta varint>
       136486  +  */
       136487  +  if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; }
135964 136488     while( *p++&0x80 );
135965 136489     *ppPoslist = p;
135966 136490   }
135967 136491   
135968 136492   /*
135969 136493   ** Helper function used by the implementation of the overloaded snippet(),
135970 136494   ** offsets() and optimize() SQL functions.
................................................................................
136027 136551       case 5: iCol = sqlite3_value_int(apVal[4]);
136028 136552       case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
136029 136553       case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
136030 136554       case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
136031 136555     }
136032 136556     if( !zEllipsis || !zEnd || !zStart ){
136033 136557       sqlite3_result_error_nomem(pContext);
       136558  +  }else if( nToken==0 ){
       136559  +    sqlite3_result_text(pContext, "", -1, SQLITE_STATIC);
136034 136560     }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
136035 136561       sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
136036 136562     }
136037 136563   }
136038 136564   
136039 136565   /*
136040 136566   ** Implementation of the offsets() function for FTS3
................................................................................
137088 137614   static void fts3EvalStartReaders(
137089 137615     Fts3Cursor *pCsr,               /* FTS Cursor handle */
137090 137616     Fts3Expr *pExpr,                /* Expression to initialize phrases in */
137091 137617     int *pRc                        /* IN/OUT: Error code */
137092 137618   ){
137093 137619     if( pExpr && SQLITE_OK==*pRc ){
137094 137620       if( pExpr->eType==FTSQUERY_PHRASE ){
137095         -      int i;
137096 137621         int nToken = pExpr->pPhrase->nToken;
137097         -      for(i=0; i<nToken; i++){
137098         -        if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
       137622  +      if( nToken ){
       137623  +        int i;
       137624  +        for(i=0; i<nToken; i++){
       137625  +          if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
       137626  +        }
       137627  +        pExpr->bDeferred = (i==nToken);
137099 137628         }
137100         -      pExpr->bDeferred = (i==nToken);
137101 137629         *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
137102 137630       }else{
137103 137631         fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
137104 137632         fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
137105 137633         pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
137106 137634       }
137107 137635     }
................................................................................
138256 138784         }
138257 138785       }
138258 138786       if( rc!=SQLITE_OK ) return rc;
138259 138787   
138260 138788       pIter = pPhrase->pOrPoslist;
138261 138789       iDocid = pPhrase->iOrDocid;
138262 138790       if( pCsr->bDesc==bDescDoclist ){
138263         -      bEof = (pIter >= (pPhrase->doclist.aAll + pPhrase->doclist.nAll));
       138791  +      bEof = !pPhrase->doclist.nAll ||
       138792  +                 (pIter >= (pPhrase->doclist.aAll + pPhrase->doclist.nAll));
138264 138793         while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
138265 138794           sqlite3Fts3DoclistNext(
138266 138795               bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, 
138267 138796               &pIter, &iDocid, &bEof
138268 138797           );
138269 138798         }
138270 138799       }else{
................................................................................
138468 138997     memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
138469 138998     sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
138470 138999   
138471 139000     *ppVtab = (sqlite3_vtab *)p;
138472 139001     return SQLITE_OK;
138473 139002   
138474 139003    bad_args:
138475         -  *pzErr = sqlite3_mprintf("invalid arguments to fts4aux constructor");
       139004  +  sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor");
138476 139005     return SQLITE_ERROR;
138477 139006   }
138478 139007   
138479 139008   /*
138480 139009   ** This function does the work for both the xDisconnect and xDestroy methods.
138481 139010   ** These tables have no persistent representation of their own, so xDisconnect
138482 139011   ** and xDestroy are identical operations.
................................................................................
139926 140455       }
139927 140456     }
139928 140457   
139929 140458     if( rc!=SQLITE_OK ){
139930 140459       sqlite3Fts3ExprFree(*ppExpr);
139931 140460       *ppExpr = 0;
139932 140461       if( rc==SQLITE_TOOBIG ){
139933         -      *pzErr = sqlite3_mprintf(
       140462  +      sqlite3Fts3ErrMsg(pzErr,
139934 140463             "FTS expression tree is too large (maximum depth %d)", 
139935 140464             SQLITE_FTS3_MAX_EXPR_DEPTH
139936 140465         );
139937 140466         rc = SQLITE_ERROR;
139938 140467       }else if( rc==SQLITE_ERROR ){
139939         -      *pzErr = sqlite3_mprintf("malformed MATCH expression: [%s]", z);
       140468  +      sqlite3Fts3ErrMsg(pzErr, "malformed MATCH expression: [%s]", z);
139940 140469       }
139941 140470     }
139942 140471   
139943 140472     return rc;
139944 140473   }
139945 140474   
139946 140475   /*
................................................................................
141408 141937       z = zCopy;
141409 141938     }
141410 141939     z[n] = '\0';
141411 141940     sqlite3Fts3Dequote(z);
141412 141941   
141413 141942     m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
141414 141943     if( !m ){
141415         -    *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z);
       141944  +    sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", z);
141416 141945       rc = SQLITE_ERROR;
141417 141946     }else{
141418 141947       char const **aArg = 0;
141419 141948       int iArg = 0;
141420 141949       z = &z[n+1];
141421 141950       while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
141422 141951         int nNew = sizeof(char *)*(iArg+1);
................................................................................
141431 141960         z[n] = '\0';
141432 141961         sqlite3Fts3Dequote(z);
141433 141962         z = &z[n+1];
141434 141963       }
141435 141964       rc = m->xCreate(iArg, aArg, ppTok);
141436 141965       assert( rc!=SQLITE_OK || *ppTok );
141437 141966       if( rc!=SQLITE_OK ){
141438         -      *pzErr = sqlite3_mprintf("unknown tokenizer");
       141967  +      sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer");
141439 141968       }else{
141440 141969         (*ppTok)->pModule = m; 
141441 141970       }
141442 141971       sqlite3_free((void *)aArg);
141443 141972     }
141444 141973   
141445 141974     sqlite3_free(zCopy);
................................................................................
141515 142044     nInput = sqlite3_value_bytes(argv[argc-1]);
141516 142045     zInput = (const char *)sqlite3_value_text(argv[argc-1]);
141517 142046   
141518 142047     pHash = (Fts3Hash *)sqlite3_user_data(context);
141519 142048     p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
141520 142049   
141521 142050     if( !p ){
141522         -    char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
141523         -    sqlite3_result_error(context, zErr, -1);
141524         -    sqlite3_free(zErr);
       142051  +    char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName);
       142052  +    sqlite3_result_error(context, zErr2, -1);
       142053  +    sqlite3_free(zErr2);
141525 142054       return;
141526 142055     }
141527 142056   
141528 142057     pRet = Tcl_NewObj();
141529 142058     Tcl_IncrRefCount(pRet);
141530 142059   
141531 142060     for(i=1; i<argc-1; i++){
................................................................................
142052 142581     char **pzErr
142053 142582   ){
142054 142583     sqlite3_tokenizer_module *p;
142055 142584     int nName = (int)strlen(zName);
142056 142585   
142057 142586     p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
142058 142587     if( !p ){
142059         -    *pzErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
       142588  +    sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", zName);
142060 142589       return SQLITE_ERROR;
142061 142590     }
142062 142591   
142063 142592     *pp = p;
142064 142593     return SQLITE_OK;
142065 142594   }
142066 142595   
................................................................................
142749 143278   /* 21 */  "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
142750 143279   /* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=?",
142751 143280   /* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
142752 143281   /* 24 */  "",
142753 143282   /* 25 */  "",
142754 143283   
142755 143284   /* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
142756         -/* 27 */ "SELECT DISTINCT level / (1024 * ?) FROM %Q.'%q_segdir'",
       143285  +/* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'",
142757 143286   
142758 143287   /* This statement is used to determine which level to read the input from
142759 143288   ** when performing an incremental merge. It returns the absolute level number
142760 143289   ** of the oldest level in the db that contains at least ? segments. Or,
142761 143290   ** if no level in the FTS index contains more than ? segments, the statement
142762 143291   ** returns zero rows.  */
142763 143292   /* 28 */ "SELECT level FROM %Q.'%q_segdir' GROUP BY level HAVING count(*)>=?"
................................................................................
145867 146396     int bSeenDone = 0;
145868 146397     int rc;
145869 146398     sqlite3_stmt *pAllLangid = 0;
145870 146399   
145871 146400     rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
145872 146401     if( rc==SQLITE_OK ){
145873 146402       int rc2;
145874         -    sqlite3_bind_int(pAllLangid, 1, p->nIndex);
       146403  +    sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
       146404  +    sqlite3_bind_int(pAllLangid, 2, p->nIndex);
145875 146405       while( sqlite3_step(pAllLangid)==SQLITE_ROW ){
145876 146406         int i;
145877 146407         int iLangid = sqlite3_column_int(pAllLangid, 0);
145878 146408         for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
145879 146409           rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL);
145880 146410           if( rc==SQLITE_DONE ){
145881 146411             bSeenDone = 1;
................................................................................
147199 147729     i = pHint->n-2;
147200 147730     while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
147201 147731     while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
147202 147732   
147203 147733     pHint->n = i;
147204 147734     i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
147205 147735     i += fts3GetVarint32(&pHint->a[i], pnInput);
147206         -  if( i!=nHint ) return SQLITE_CORRUPT_VTAB;
       147736  +  if( i!=nHint ) return FTS_CORRUPT_VTAB;
147207 147737   
147208 147738     return SQLITE_OK;
147209 147739   }
147210 147740   
147211 147741   
147212 147742   /*
147213 147743   ** Attempt an incremental merge that writes nMerge leaf blocks.
................................................................................
147567 148097     u64 cksum2 = 0;                 /* Checksum based on %_content contents */
147568 148098     sqlite3_stmt *pAllLangid = 0;   /* Statement to return all language-ids */
147569 148099   
147570 148100     /* This block calculates the checksum according to the FTS index. */
147571 148101     rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
147572 148102     if( rc==SQLITE_OK ){
147573 148103       int rc2;
147574         -    sqlite3_bind_int(pAllLangid, 1, p->nIndex);
       148104  +    sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
       148105  +    sqlite3_bind_int(pAllLangid, 2, p->nIndex);
147575 148106       while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
147576 148107         int iLangid = sqlite3_column_int(pAllLangid, 0);
147577 148108         int i;
147578 148109         for(i=0; i<p->nIndex; i++){
147579 148110           cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
147580 148111         }
147581 148112       }
147582 148113       rc2 = sqlite3_reset(pAllLangid);
147583 148114       if( rc==SQLITE_OK ) rc = rc2;
147584 148115     }
147585 148116   
147586 148117     /* This block calculates the checksum according to the %_content table */
147587         -  rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
147588 148118     if( rc==SQLITE_OK ){
147589 148119       sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
147590 148120       sqlite3_stmt *pStmt = 0;
147591 148121       char *zSql;
147592 148122      
147593 148123       zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
147594 148124       if( !zSql ){
................................................................................
147677 148207   */
147678 148208   static int fts3DoIntegrityCheck(
147679 148209     Fts3Table *p                    /* FTS3 table handle */
147680 148210   ){
147681 148211     int rc;
147682 148212     int bOk = 0;
147683 148213     rc = fts3IntegrityCheck(p, &bOk);
147684         -  if( rc==SQLITE_OK && bOk==0 ) rc = SQLITE_CORRUPT_VTAB;
       148214  +  if( rc==SQLITE_OK && bOk==0 ) rc = FTS_CORRUPT_VTAB;
147685 148215     return rc;
147686 148216   }
147687 148217   
147688 148218   /*
147689 148219   ** Handle a 'special' INSERT of the form:
147690 148220   **
147691 148221   **   "INSERT INTO tbl(tbl) VALUES(<expr>)"
................................................................................
148115 148645   #define FTS3_MATCHINFO_NPHRASE   'p'        /* 1 value */
148116 148646   #define FTS3_MATCHINFO_NCOL      'c'        /* 1 value */
148117 148647   #define FTS3_MATCHINFO_NDOC      'n'        /* 1 value */
148118 148648   #define FTS3_MATCHINFO_AVGLENGTH 'a'        /* nCol values */
148119 148649   #define FTS3_MATCHINFO_LENGTH    'l'        /* nCol values */
148120 148650   #define FTS3_MATCHINFO_LCS       's'        /* nCol values */
148121 148651   #define FTS3_MATCHINFO_HITS      'x'        /* 3*nCol*nPhrase values */
       148652  +#define FTS3_MATCHINFO_LHITS     'y'        /* nCol*nPhrase values */
148122 148653   
148123 148654   /*
148124 148655   ** The default value for the second argument to matchinfo(). 
148125 148656   */
148126 148657   #define FTS3_MATCHINFO_DEFAULT   "pcx"
148127 148658   
148128 148659   
................................................................................
148896 149427       }else{
148897 149428         p->aMatchinfo[iStart+i*3] = 0;
148898 149429       }
148899 149430     }
148900 149431   
148901 149432     return rc;
148902 149433   }
       149434  +
       149435  +/*
       149436  +** fts3ExprIterate() callback used to gather information for the matchinfo
       149437  +** directive 'y'.
       149438  +*/
       149439  +static int fts3ExprLHitsCb(
       149440  +  Fts3Expr *pExpr,                /* Phrase expression node */
       149441  +  int iPhrase,                    /* Phrase number */
       149442  +  void *pCtx                      /* Pointer to MatchInfo structure */
       149443  +){
       149444  +  MatchInfo *p = (MatchInfo *)pCtx;
       149445  +  Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
       149446  +  int rc = SQLITE_OK;
       149447  +  int iStart = iPhrase * p->nCol;
       149448  +  Fts3Expr *pEof;                 /* Ancestor node already at EOF */
       149449  +  
       149450  +  /* This must be a phrase */
       149451  +  assert( pExpr->pPhrase );
       149452  +
       149453  +  /* Initialize all output integers to zero. */
       149454  +  memset(&p->aMatchinfo[iStart], 0, sizeof(u32) * p->nCol);
       149455  +
       149456  +  /* Check if this or any parent node is at EOF. If so, then all output
       149457  +  ** values are zero.  */
       149458  +  for(pEof=pExpr; pEof && pEof->bEof==0; pEof=pEof->pParent);
       149459  +
       149460  +  if( pEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
       149461  +    Fts3Phrase *pPhrase = pExpr->pPhrase;
       149462  +    char *pIter = pPhrase->doclist.pList;
       149463  +    int iCol = 0;
       149464  +
       149465  +    while( 1 ){
       149466  +      int nHit = fts3ColumnlistCount(&pIter);
       149467  +      if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
       149468  +        p->aMatchinfo[iStart + iCol] = (u32)nHit;
       149469  +      }
       149470  +      assert( *pIter==0x00 || *pIter==0x01 );
       149471  +      if( *pIter!=0x01 ) break;
       149472  +      pIter++;
       149473  +      pIter += fts3GetVarint32(pIter, &iCol);
       149474  +    }
       149475  +  }
       149476  +
       149477  +  return rc;
       149478  +}
148903 149479   
148904 149480   static int fts3MatchinfoCheck(
148905 149481     Fts3Table *pTab, 
148906 149482     char cArg,
148907 149483     char **pzErr
148908 149484   ){
148909 149485     if( (cArg==FTS3_MATCHINFO_NPHRASE)
148910 149486      || (cArg==FTS3_MATCHINFO_NCOL)
148911 149487      || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
148912 149488      || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
148913 149489      || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
148914 149490      || (cArg==FTS3_MATCHINFO_LCS)
148915 149491      || (cArg==FTS3_MATCHINFO_HITS)
       149492  +   || (cArg==FTS3_MATCHINFO_LHITS)
148916 149493     ){
148917 149494       return SQLITE_OK;
148918 149495     }
148919         -  *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
       149496  +  sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
148920 149497     return SQLITE_ERROR;
148921 149498   }
148922 149499   
148923 149500   static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
148924 149501     int nVal;                       /* Number of integers output by cArg */
148925 149502   
148926 149503     switch( cArg ){
................................................................................
148931 149508         break;
148932 149509   
148933 149510       case FTS3_MATCHINFO_AVGLENGTH:
148934 149511       case FTS3_MATCHINFO_LENGTH:
148935 149512       case FTS3_MATCHINFO_LCS:
148936 149513         nVal = pInfo->nCol;
148937 149514         break;
       149515  +
       149516  +    case FTS3_MATCHINFO_LHITS:
       149517  +      nVal = pInfo->nCol * pInfo->nPhrase;
       149518  +      break;
148938 149519   
148939 149520       default:
148940 149521         assert( cArg==FTS3_MATCHINFO_HITS );
148941 149522         nVal = pInfo->nCol * pInfo->nPhrase * 3;
148942 149523         break;
148943 149524     }
148944 149525   
................................................................................
149185 149766   
149186 149767         case FTS3_MATCHINFO_LCS:
149187 149768           rc = fts3ExprLoadDoclists(pCsr, 0, 0);
149188 149769           if( rc==SQLITE_OK ){
149189 149770             rc = fts3MatchinfoLcs(pCsr, pInfo);
149190 149771           }
149191 149772           break;
       149773  +
       149774  +      case FTS3_MATCHINFO_LHITS:
       149775  +        (void)fts3ExprIterate(pCsr->pExpr, fts3ExprLHitsCb, (void*)pInfo);
       149776  +        break;
149192 149777   
149193 149778         default: {
149194 149779           Fts3Expr *pExpr;
149195 149780           assert( zArg[i]==FTS3_MATCHINFO_HITS );
149196 149781           pExpr = pCsr->pExpr;
149197 149782           rc = fts3ExprLoadDoclists(pCsr, 0, 0);
149198 149783           if( rc!=SQLITE_OK ) break;
................................................................................
153198 153783     ** the conflicting row can be removed before proceeding. In the second
153199 153784     ** case, SQLITE_CONSTRAINT must be returned regardless of the
153200 153785     ** conflict-handling mode specified by the user.
153201 153786     */
153202 153787     if( nData>1 ){
153203 153788       int ii;
153204 153789   
153205         -    /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
153206         -    assert( nData==(pRtree->nDim*2 + 3) );
       153790  +    /* Populate the cell.aCoord[] array. The first coordinate is azData[3].
       153791  +    **
       153792  +    ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
       153793  +    ** with "column" that are interpreted as table constraints.
       153794  +    ** Example:  CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
       153795  +    ** This problem was discovered after years of use, so we silently ignore
       153796  +    ** these kinds of misdeclared tables to avoid breaking any legacy.
       153797  +    */
       153798  +    assert( nData<=(pRtree->nDim*2 + 3) );
       153799  +
153207 153800   #ifndef SQLITE_RTREE_INT_ONLY
153208 153801       if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
153209         -      for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       153802  +      for(ii=0; ii<nData-4; ii+=2){
153210 153803           cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
153211 153804           cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
153212 153805           if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
153213 153806             rc = SQLITE_CONSTRAINT;
153214 153807             goto constraint;
153215 153808           }
153216 153809         }
153217 153810       }else
153218 153811   #endif
153219 153812       {
153220         -      for(ii=0; ii<(pRtree->nDim*2); ii+=2){
       153813  +      for(ii=0; ii<nData-4; ii+=2){
153221 153814           cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
153222 153815           cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
153223 153816           if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
153224 153817             rc = SQLITE_CONSTRAINT;
153225 153818             goto constraint;
153226 153819           }
153227 153820         }
................................................................................
154613 155206     *ppModule = &icuTokenizerModule;
154614 155207   }
154615 155208   
154616 155209   #endif /* defined(SQLITE_ENABLE_ICU) */
154617 155210   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
154618 155211   
154619 155212   /************** End of fts3_icu.c ********************************************/
       155213  +/************** Begin file dbstat.c ******************************************/
       155214  +/*
       155215  +** 2010 July 12
       155216  +**
       155217  +** The author disclaims copyright to this source code.  In place of
       155218  +** a legal notice, here is a blessing:
       155219  +**
       155220  +**    May you do good and not evil.
       155221  +**    May you find forgiveness for yourself and forgive others.
       155222  +**    May you share freely, never taking more than you give.
       155223  +**
       155224  +******************************************************************************
       155225  +**
       155226  +** This file contains an implementation of the "dbstat" virtual table.
       155227  +**
       155228  +** The dbstat virtual table is used to extract low-level formatting
       155229  +** information from an SQLite database in order to implement the
       155230  +** "sqlite3_analyzer" utility.  See the ../tool/spaceanal.tcl script
       155231  +** for an example implementation.
       155232  +*/
       155233  +
       155234  +#if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
       155235  +    && !defined(SQLITE_OMIT_VIRTUALTABLE)
       155236  +
       155237  +/*
       155238  +** Page paths:
       155239  +** 
       155240  +**   The value of the 'path' column describes the path taken from the 
       155241  +**   root-node of the b-tree structure to each page. The value of the 
       155242  +**   root-node path is '/'.
       155243  +**
       155244  +**   The value of the path for the left-most child page of the root of
       155245  +**   a b-tree is '/000/'. (Btrees store content ordered from left to right
       155246  +**   so the pages to the left have smaller keys than the pages to the right.)
       155247  +**   The next to left-most child of the root page is
       155248  +**   '/001', and so on, each sibling page identified by a 3-digit hex 
       155249  +**   value. The children of the 451st left-most sibling have paths such
       155250  +**   as '/1c2/000/, '/1c2/001/' etc.
       155251  +**
       155252  +**   Overflow pages are specified by appending a '+' character and a 
       155253  +**   six-digit hexadecimal value to the path to the cell they are linked
       155254  +**   from. For example, the three overflow pages in a chain linked from 
       155255  +**   the left-most cell of the 450th child of the root page are identified
       155256  +**   by the paths:
       155257  +**
       155258  +**      '/1c2/000+000000'         // First page in overflow chain
       155259  +**      '/1c2/000+000001'         // Second page in overflow chain
       155260  +**      '/1c2/000+000002'         // Third page in overflow chain
       155261  +**
       155262  +**   If the paths are sorted using the BINARY collation sequence, then
       155263  +**   the overflow pages associated with a cell will appear earlier in the
       155264  +**   sort-order than its child page:
       155265  +**
       155266  +**      '/1c2/000/'               // Left-most child of 451st child of root
       155267  +*/
       155268  +#define VTAB_SCHEMA                                                         \
       155269  +  "CREATE TABLE xx( "                                                       \
       155270  +  "  name       STRING,           /* Name of table or index */"             \
       155271  +  "  path       INTEGER,          /* Path to page from root */"             \
       155272  +  "  pageno     INTEGER,          /* Page number */"                        \
       155273  +  "  pagetype   STRING,           /* 'internal', 'leaf' or 'overflow' */"   \
       155274  +  "  ncell      INTEGER,          /* Cells on page (0 for overflow) */"     \
       155275  +  "  payload    INTEGER,          /* Bytes of payload on this page */"      \
       155276  +  "  unused     INTEGER,          /* Bytes of unused space on this page */" \
       155277  +  "  mx_payload INTEGER,          /* Largest payload size of all cells */"  \
       155278  +  "  pgoffset   INTEGER,          /* Offset of page in file */"             \
       155279  +  "  pgsize     INTEGER           /* Size of the page */"                   \
       155280  +  ");"
       155281  +
       155282  +
       155283  +typedef struct StatTable StatTable;
       155284  +typedef struct StatCursor StatCursor;
       155285  +typedef struct StatPage StatPage;
       155286  +typedef struct StatCell StatCell;
       155287  +
       155288  +struct StatCell {
       155289  +  int nLocal;                     /* Bytes of local payload */
       155290  +  u32 iChildPg;                   /* Child node (or 0 if this is a leaf) */
       155291  +  int nOvfl;                      /* Entries in aOvfl[] */
       155292  +  u32 *aOvfl;                     /* Array of overflow page numbers */
       155293  +  int nLastOvfl;                  /* Bytes of payload on final overflow page */
       155294  +  int iOvfl;                      /* Iterates through aOvfl[] */
       155295  +};
       155296  +
       155297  +struct StatPage {
       155298  +  u32 iPgno;
       155299  +  DbPage *pPg;
       155300  +  int iCell;
       155301  +
       155302  +  char *zPath;                    /* Path to this page */
       155303  +
       155304  +  /* Variables populated by statDecodePage(): */
       155305  +  u8 flags;                       /* Copy of flags byte */
       155306  +  int nCell;                      /* Number of cells on page */
       155307  +  int nUnused;                    /* Number of unused bytes on page */
       155308  +  StatCell *aCell;                /* Array of parsed cells */
       155309  +  u32 iRightChildPg;              /* Right-child page number (or 0) */
       155310  +  int nMxPayload;                 /* Largest payload of any cell on this page */
       155311  +};
       155312  +
       155313  +struct StatCursor {
       155314  +  sqlite3_vtab_cursor base;
       155315  +  sqlite3_stmt *pStmt;            /* Iterates through set of root pages */
       155316  +  int isEof;                      /* After pStmt has returned SQLITE_DONE */
       155317  +
       155318  +  StatPage aPage[32];
       155319  +  int iPage;                      /* Current entry in aPage[] */
       155320  +
       155321  +  /* Values to return. */
       155322  +  char *zName;                    /* Value of 'name' column */
       155323  +  char *zPath;                    /* Value of 'path' column */
       155324  +  u32 iPageno;                    /* Value of 'pageno' column */
       155325  +  char *zPagetype;                /* Value of 'pagetype' column */
       155326  +  int nCell;                      /* Value of 'ncell' column */
       155327  +  int nPayload;                   /* Value of 'payload' column */
       155328  +  int nUnused;                    /* Value of 'unused' column */
       155329  +  int nMxPayload;                 /* Value of 'mx_payload' column */
       155330  +  i64 iOffset;                    /* Value of 'pgOffset' column */
       155331  +  int szPage;                     /* Value of 'pgSize' column */
       155332  +};
       155333  +
       155334  +struct StatTable {
       155335  +  sqlite3_vtab base;
       155336  +  sqlite3 *db;
       155337  +};
       155338  +
       155339  +#ifndef get2byte
       155340  +# define get2byte(x)   ((x)[0]<<8 | (x)[1])
       155341  +#endif
       155342  +
       155343  +/*
       155344  +** Connect to or create a statvfs virtual table.
       155345  +*/
       155346  +static int statConnect(
       155347  +  sqlite3 *db,
       155348  +  void *pAux,
       155349  +  int argc, const char *const*argv,
       155350  +  sqlite3_vtab **ppVtab,
       155351  +  char **pzErr
       155352  +){
       155353  +  StatTable *pTab = 0;
       155354  +  int rc = SQLITE_OK;
       155355  +
       155356  +  rc = sqlite3_declare_vtab(db, VTAB_SCHEMA);
       155357  +  if( rc==SQLITE_OK ){
       155358  +    pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
       155359  +    if( pTab==0 ) rc = SQLITE_NOMEM;
       155360  +  }
       155361  +
       155362  +  assert( rc==SQLITE_OK || pTab==0 );
       155363  +  if( rc==SQLITE_OK ){
       155364  +    memset(pTab, 0, sizeof(StatTable));
       155365  +    pTab->db = db;
       155366  +  }
       155367  +
       155368  +  *ppVtab = (sqlite3_vtab*)pTab;
       155369  +  return rc;
       155370  +}
       155371  +
       155372  +/*
       155373  +** Disconnect from or destroy a statvfs virtual table.
       155374  +*/
       155375  +static int statDisconnect(sqlite3_vtab *pVtab){
       155376  +  sqlite3_free(pVtab);
       155377  +  return SQLITE_OK;
       155378  +}
       155379  +
       155380  +/*
       155381  +** There is no "best-index". This virtual table always does a linear
       155382  +** scan of the binary VFS log file.
       155383  +*/
       155384  +static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
       155385  +
       155386  +  /* Records are always returned in ascending order of (name, path). 
       155387  +  ** If this will satisfy the client, set the orderByConsumed flag so that 
       155388  +  ** SQLite does not do an external sort.
       155389  +  */
       155390  +  if( ( pIdxInfo->nOrderBy==1
       155391  +     && pIdxInfo->aOrderBy[0].iColumn==0
       155392  +     && pIdxInfo->aOrderBy[0].desc==0
       155393  +     ) ||
       155394  +      ( pIdxInfo->nOrderBy==2
       155395  +     && pIdxInfo->aOrderBy[0].iColumn==0
       155396  +     && pIdxInfo->aOrderBy[0].desc==0
       155397  +     && pIdxInfo->aOrderBy[1].iColumn==1
       155398  +     && pIdxInfo->aOrderBy[1].desc==0
       155399  +     )
       155400  +  ){
       155401  +    pIdxInfo->orderByConsumed = 1;
       155402  +  }
       155403  +
       155404  +  pIdxInfo->estimatedCost = 10.0;
       155405  +  return SQLITE_OK;
       155406  +}
       155407  +
       155408  +/*
       155409  +** Open a new statvfs cursor.
       155410  +*/
       155411  +static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
       155412  +  StatTable *pTab = (StatTable *)pVTab;
       155413  +  StatCursor *pCsr;
       155414  +  int rc;
       155415  +
       155416  +  pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
       155417  +  if( pCsr==0 ){
       155418  +    rc = SQLITE_NOMEM;
       155419  +  }else{
       155420  +    memset(pCsr, 0, sizeof(StatCursor));
       155421  +    pCsr->base.pVtab = pVTab;
       155422  +
       155423  +    rc = sqlite3_prepare_v2(pTab->db, 
       155424  +        "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type"
       155425  +        "  UNION ALL  "
       155426  +        "SELECT name, rootpage, type FROM sqlite_master WHERE rootpage!=0"
       155427  +        "  ORDER BY name", -1,
       155428  +        &pCsr->pStmt, 0
       155429  +        );
       155430  +    if( rc!=SQLITE_OK ){
       155431  +      sqlite3_free(pCsr);
       155432  +      pCsr = 0;
       155433  +    }
       155434  +  }
       155435  +
       155436  +  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
       155437  +  return rc;
       155438  +}
       155439  +
       155440  +static void statClearPage(StatPage *p){
       155441  +  int i;
       155442  +  if( p->aCell ){
       155443  +    for(i=0; i<p->nCell; i++){
       155444  +      sqlite3_free(p->aCell[i].aOvfl);
       155445  +    }
       155446  +    sqlite3_free(p->aCell);
       155447  +  }
       155448  +  sqlite3PagerUnref(p->pPg);
       155449  +  sqlite3_free(p->zPath);
       155450  +  memset(p, 0, sizeof(StatPage));
       155451  +}
       155452  +
       155453  +static void statResetCsr(StatCursor *pCsr){
       155454  +  int i;
       155455  +  sqlite3_reset(pCsr->pStmt);
       155456  +  for(i=0; i<ArraySize(pCsr->aPage); i++){
       155457  +    statClearPage(&pCsr->aPage[i]);
       155458  +  }
       155459  +  pCsr->iPage = 0;
       155460  +  sqlite3_free(pCsr->zPath);
       155461  +  pCsr->zPath = 0;
       155462  +}
       155463  +
       155464  +/*
       155465  +** Close a statvfs cursor.
       155466  +*/
       155467  +static int statClose(sqlite3_vtab_cursor *pCursor){
       155468  +  StatCursor *pCsr = (StatCursor *)pCursor;
       155469  +  statResetCsr(pCsr);
       155470  +  sqlite3_finalize(pCsr->pStmt);
       155471  +  sqlite3_free(pCsr);
       155472  +  return SQLITE_OK;
       155473  +}
       155474  +
       155475  +static void getLocalPayload(
       155476  +  int nUsable,                    /* Usable bytes per page */
       155477  +  u8 flags,                       /* Page flags */
       155478  +  int nTotal,                     /* Total record (payload) size */
       155479  +  int *pnLocal                    /* OUT: Bytes stored locally */
       155480  +){
       155481  +  int nLocal;
       155482  +  int nMinLocal;
       155483  +  int nMaxLocal;
       155484  + 
       155485  +  if( flags==0x0D ){              /* Table leaf node */
       155486  +    nMinLocal = (nUsable - 12) * 32 / 255 - 23;
       155487  +    nMaxLocal = nUsable - 35;
       155488  +  }else{                          /* Index interior and leaf nodes */
       155489  +    nMinLocal = (nUsable - 12) * 32 / 255 - 23;
       155490  +    nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
       155491  +  }
       155492  +
       155493  +  nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
       155494  +  if( nLocal>nMaxLocal ) nLocal = nMinLocal;
       155495  +  *pnLocal = nLocal;
       155496  +}
       155497  +
       155498  +static int statDecodePage(Btree *pBt, StatPage *p){
       155499  +  int nUnused;
       155500  +  int iOff;
       155501  +  int nHdr;
       155502  +  int isLeaf;
       155503  +  int szPage;
       155504  +
       155505  +  u8 *aData = sqlite3PagerGetData(p->pPg);
       155506  +  u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
       155507  +
       155508  +  p->flags = aHdr[0];
       155509  +  p->nCell = get2byte(&aHdr[3]);
       155510  +  p->nMxPayload = 0;
       155511  +
       155512  +  isLeaf = (p->flags==0x0A || p->flags==0x0D);
       155513  +  nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100;
       155514  +
       155515  +  nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
       155516  +  nUnused += (int)aHdr[7];
       155517  +  iOff = get2byte(&aHdr[1]);
       155518  +  while( iOff ){
       155519  +    nUnused += get2byte(&aData[iOff+2]);
       155520  +    iOff = get2byte(&aData[iOff]);
       155521  +  }
       155522  +  p->nUnused = nUnused;
       155523  +  p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
       155524  +  szPage = sqlite3BtreeGetPageSize(pBt);
       155525  +
       155526  +  if( p->nCell ){
       155527  +    int i;                        /* Used to iterate through cells */
       155528  +    int nUsable;                  /* Usable bytes per page */
       155529  +
       155530  +    sqlite3BtreeEnter(pBt);
       155531  +    nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
       155532  +    sqlite3BtreeLeave(pBt);
       155533  +    p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
       155534  +    if( p->aCell==0 ) return SQLITE_NOMEM;
       155535  +    memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
       155536  +
       155537  +    for(i=0; i<p->nCell; i++){
       155538  +      StatCell *pCell = &p->aCell[i];
       155539  +
       155540  +      iOff = get2byte(&aData[nHdr+i*2]);
       155541  +      if( !isLeaf ){
       155542  +        pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
       155543  +        iOff += 4;
       155544  +      }
       155545  +      if( p->flags==0x05 ){
       155546  +        /* A table interior node. nPayload==0. */
       155547  +      }else{
       155548  +        u32 nPayload;             /* Bytes of payload total (local+overflow) */
       155549  +        int nLocal;               /* Bytes of payload stored locally */
       155550  +        iOff += getVarint32(&aData[iOff], nPayload);
       155551  +        if( p->flags==0x0D ){
       155552  +          u64 dummy;
       155553  +          iOff += sqlite3GetVarint(&aData[iOff], &dummy);
       155554  +        }
       155555  +        if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
       155556  +        getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
       155557  +        pCell->nLocal = nLocal;
       155558  +        assert( nLocal>=0 );
       155559  +        assert( nPayload>=(u32)nLocal );
       155560  +        assert( nLocal<=(nUsable-35) );
       155561  +        if( nPayload>(u32)nLocal ){
       155562  +          int j;
       155563  +          int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
       155564  +          pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
       155565  +          pCell->nOvfl = nOvfl;
       155566  +          pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
       155567  +          if( pCell->aOvfl==0 ) return SQLITE_NOMEM;
       155568  +          pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
       155569  +          for(j=1; j<nOvfl; j++){
       155570  +            int rc;
       155571  +            u32 iPrev = pCell->aOvfl[j-1];
       155572  +            DbPage *pPg = 0;
       155573  +            rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg);
       155574  +            if( rc!=SQLITE_OK ){
       155575  +              assert( pPg==0 );
       155576  +              return rc;
       155577  +            } 
       155578  +            pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
       155579  +            sqlite3PagerUnref(pPg);
       155580  +          }
       155581  +        }
       155582  +      }
       155583  +    }
       155584  +  }
       155585  +
       155586  +  return SQLITE_OK;
       155587  +}
       155588  +
       155589  +/*
       155590  +** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
       155591  +** the current value of pCsr->iPageno.
       155592  +*/
       155593  +static void statSizeAndOffset(StatCursor *pCsr){
       155594  +  StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
       155595  +  Btree *pBt = pTab->db->aDb[0].pBt;
       155596  +  Pager *pPager = sqlite3BtreePager(pBt);
       155597  +  sqlite3_file *fd;
       155598  +  sqlite3_int64 x[2];
       155599  +
       155600  +  /* The default page size and offset */
       155601  +  pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
       155602  +  pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
       155603  +
       155604  +  /* If connected to a ZIPVFS backend, override the page size and
       155605  +  ** offset with actual values obtained from ZIPVFS.
       155606  +  */
       155607  +  fd = sqlite3PagerFile(pPager);
       155608  +  x[0] = pCsr->iPageno;
       155609  +  if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
       155610  +    pCsr->iOffset = x[0];
       155611  +    pCsr->szPage = (int)x[1];
       155612  +  }
       155613  +}
       155614  +
       155615  +/*
       155616  +** Move a statvfs cursor to the next entry in the file.
       155617  +*/
       155618  +static int statNext(sqlite3_vtab_cursor *pCursor){
       155619  +  int rc;
       155620  +  int nPayload;
       155621  +  StatCursor *pCsr = (StatCursor *)pCursor;
       155622  +  StatTable *pTab = (StatTable *)pCursor->pVtab;
       155623  +  Btree *pBt = pTab->db->aDb[0].pBt;
       155624  +  Pager *pPager = sqlite3BtreePager(pBt);
       155625  +
       155626  +  sqlite3_free(pCsr->zPath);
       155627  +  pCsr->zPath = 0;
       155628  +
       155629  +statNextRestart:
       155630  +  if( pCsr->aPage[0].pPg==0 ){
       155631  +    rc = sqlite3_step(pCsr->pStmt);
       155632  +    if( rc==SQLITE_ROW ){
       155633  +      int nPage;
       155634  +      u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
       155635  +      sqlite3PagerPagecount(pPager, &nPage);
       155636  +      if( nPage==0 ){
       155637  +        pCsr->isEof = 1;
       155638  +        return sqlite3_reset(pCsr->pStmt);
       155639  +      }
       155640  +      rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg);
       155641  +      pCsr->aPage[0].iPgno = iRoot;
       155642  +      pCsr->aPage[0].iCell = 0;
       155643  +      pCsr->aPage[0].zPath = sqlite3_mprintf("/");
       155644  +      pCsr->iPage = 0;
       155645  +    }else{
       155646  +      pCsr->isEof = 1;
       155647  +      return sqlite3_reset(pCsr->pStmt);
       155648  +    }
       155649  +  }else{
       155650  +
       155651  +    /* Page p itself has already been visited. */
       155652  +    StatPage *p = &pCsr->aPage[pCsr->iPage];
       155653  +
       155654  +    while( p->iCell<p->nCell ){
       155655  +      StatCell *pCell = &p->aCell[p->iCell];
       155656  +      if( pCell->iOvfl<pCell->nOvfl ){
       155657  +        int nUsable;
       155658  +        sqlite3BtreeEnter(pBt);
       155659  +        nUsable = sqlite3BtreeGetPageSize(pBt) - 
       155660  +                        sqlite3BtreeGetReserveNoMutex(pBt);
       155661  +        sqlite3BtreeLeave(pBt);
       155662  +        pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
       155663  +        pCsr->iPageno = pCell->aOvfl[pCell->iOvfl];
       155664  +        pCsr->zPagetype = "overflow";
       155665  +        pCsr->nCell = 0;
       155666  +        pCsr->nMxPayload = 0;
       155667  +        pCsr->zPath = sqlite3_mprintf(
       155668  +            "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl
       155669  +        );
       155670  +        if( pCell->iOvfl<pCell->nOvfl-1 ){
       155671  +          pCsr->nUnused = 0;
       155672  +          pCsr->nPayload = nUsable - 4;
       155673  +        }else{
       155674  +          pCsr->nPayload = pCell->nLastOvfl;
       155675  +          pCsr->nUnused = nUsable - 4 - pCsr->nPayload;
       155676  +        }
       155677  +        pCell->iOvfl++;
       155678  +        statSizeAndOffset(pCsr);
       155679  +        return SQLITE_OK;
       155680  +      }
       155681  +      if( p->iRightChildPg ) break;
       155682  +      p->iCell++;
       155683  +    }
       155684  +
       155685  +    if( !p->iRightChildPg || p->iCell>p->nCell ){
       155686  +      statClearPage(p);
       155687  +      if( pCsr->iPage==0 ) return statNext(pCursor);
       155688  +      pCsr->iPage--;
       155689  +      goto statNextRestart; /* Tail recursion */
       155690  +    }
       155691  +    pCsr->iPage++;
       155692  +    assert( p==&pCsr->aPage[pCsr->iPage-1] );
       155693  +
       155694  +    if( p->iCell==p->nCell ){
       155695  +      p[1].iPgno = p->iRightChildPg;
       155696  +    }else{
       155697  +      p[1].iPgno = p->aCell[p->iCell].iChildPg;
       155698  +    }
       155699  +    rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg);
       155700  +    p[1].iCell = 0;
       155701  +    p[1].zPath = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
       155702  +    p->iCell++;
       155703  +  }
       155704  +
       155705  +
       155706  +  /* Populate the StatCursor fields with the values to be returned
       155707  +  ** by the xColumn() and xRowid() methods.
       155708  +  */
       155709  +  if( rc==SQLITE_OK ){
       155710  +    int i;
       155711  +    StatPage *p = &pCsr->aPage[pCsr->iPage];
       155712  +    pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
       155713  +    pCsr->iPageno = p->iPgno;
       155714  +
       155715  +    rc = statDecodePage(pBt, p);
       155716  +    if( rc==SQLITE_OK ){
       155717  +      statSizeAndOffset(pCsr);
       155718  +
       155719  +      switch( p->flags ){
       155720  +        case 0x05:             /* table internal */
       155721  +        case 0x02:             /* index internal */
       155722  +          pCsr->zPagetype = "internal";
       155723  +          break;
       155724  +        case 0x0D:             /* table leaf */
       155725  +        case 0x0A:             /* index leaf */
       155726  +          pCsr->zPagetype = "leaf";
       155727  +          break;
       155728  +        default:
       155729  +          pCsr->zPagetype = "corrupted";
       155730  +          break;
       155731  +      }
       155732  +      pCsr->nCell = p->nCell;
       155733  +      pCsr->nUnused = p->nUnused;
       155734  +      pCsr->nMxPayload = p->nMxPayload;
       155735  +      pCsr->zPath = sqlite3_mprintf("%s", p->zPath);
       155736  +      nPayload = 0;
       155737  +      for(i=0; i<p->nCell; i++){
       155738  +        nPayload += p->aCell[i].nLocal;
       155739  +      }
       155740  +      pCsr->nPayload = nPayload;
       155741  +    }
       155742  +  }
       155743  +
       155744  +  return rc;
       155745  +}
       155746  +
       155747  +static int statEof(sqlite3_vtab_cursor *pCursor){
       155748  +  StatCursor *pCsr = (StatCursor *)pCursor;
       155749  +  return pCsr->isEof;
       155750  +}
       155751  +
       155752  +static int statFilter(
       155753  +  sqlite3_vtab_cursor *pCursor, 
       155754  +  int idxNum, const char *idxStr,
       155755  +  int argc, sqlite3_value **argv
       155756  +){
       155757  +  StatCursor *pCsr = (StatCursor *)pCursor;
       155758  +
       155759  +  statResetCsr(pCsr);
       155760  +  return statNext(pCursor);
       155761  +}
       155762  +
       155763  +static int statColumn(
       155764  +  sqlite3_vtab_cursor *pCursor, 
       155765  +  sqlite3_context *ctx, 
       155766  +  int i
       155767  +){
       155768  +  StatCursor *pCsr = (StatCursor *)pCursor;
       155769  +  switch( i ){
       155770  +    case 0:            /* name */
       155771  +      sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_STATIC);
       155772  +      break;
       155773  +    case 1:            /* path */
       155774  +      sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
       155775  +      break;
       155776  +    case 2:            /* pageno */
       155777  +      sqlite3_result_int64(ctx, pCsr->iPageno);
       155778  +      break;
       155779  +    case 3:            /* pagetype */
       155780  +      sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
       155781  +      break;
       155782  +    case 4:            /* ncell */
       155783  +      sqlite3_result_int(ctx, pCsr->nCell);
       155784  +      break;
       155785  +    case 5:            /* payload */
       155786  +      sqlite3_result_int(ctx, pCsr->nPayload);
       155787  +      break;
       155788  +    case 6:            /* unused */
       155789  +      sqlite3_result_int(ctx, pCsr->nUnused);
       155790  +      break;
       155791  +    case 7:            /* mx_payload */
       155792  +      sqlite3_result_int(ctx, pCsr->nMxPayload);
       155793  +      break;
       155794  +    case 8:            /* pgoffset */
       155795  +      sqlite3_result_int64(ctx, pCsr->iOffset);
       155796  +      break;
       155797  +    case 9:            /* pgsize */
       155798  +      sqlite3_result_int(ctx, pCsr->szPage);
       155799  +      break;
       155800  +  }
       155801  +  return SQLITE_OK;
       155802  +}
       155803  +
       155804  +static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
       155805  +  StatCursor *pCsr = (StatCursor *)pCursor;
       155806  +  *pRowid = pCsr->iPageno;
       155807  +  return SQLITE_OK;
       155808  +}
       155809  +
       155810  +/*
       155811  +** Invoke this routine to register the "dbstat" virtual table module
       155812  +*/
       155813  +SQLITE_API int SQLITE_STDCALL sqlite3_dbstat_register(sqlite3 *db){
       155814  +  static sqlite3_module dbstat_module = {
       155815  +    0,                            /* iVersion */
       155816  +    statConnect,                  /* xCreate */
       155817  +    statConnect,                  /* xConnect */
       155818  +    statBestIndex,                /* xBestIndex */
       155819  +    statDisconnect,               /* xDisconnect */
       155820  +    statDisconnect,               /* xDestroy */
       155821  +    statOpen,                     /* xOpen - open a cursor */
       155822  +    statClose,                    /* xClose - close a cursor */
       155823  +    statFilter,                   /* xFilter - configure scan constraints */
       155824  +    statNext,                     /* xNext - advance a cursor */
       155825  +    statEof,                      /* xEof - check for end of scan */
       155826  +    statColumn,                   /* xColumn - read data */
       155827  +    statRowid,                    /* xRowid - read data */
       155828  +    0,                            /* xUpdate */
       155829  +    0,                            /* xBegin */
       155830  +    0,                            /* xSync */
       155831  +    0,                            /* xCommit */
       155832  +    0,                            /* xRollback */
       155833  +    0,                            /* xFindMethod */
       155834  +    0,                            /* xRename */
       155835  +  };
       155836  +  return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
       155837  +}
       155838  +#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
       155839  +
       155840  +/************** End of dbstat.c **********************************************/

Changes to src/sqlite3.h.

   107    107   ** string contains the date and time of the check-in (UTC) and an SHA1
   108    108   ** hash of the entire source tree.
   109    109   **
   110    110   ** See also: [sqlite3_libversion()],
   111    111   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   112    112   ** [sqlite_version()] and [sqlite_source_id()].
   113    113   */
   114         -#define SQLITE_VERSION        "3.8.9"
   115         -#define SQLITE_VERSION_NUMBER 3008009
   116         -#define SQLITE_SOURCE_ID      "2015-04-06 11:04:51 3ad829e50faca538db3abb2afb898b5521550c5c"
          114  +#define SQLITE_VERSION        "3.8.10"
          115  +#define SQLITE_VERSION_NUMBER 3008010
          116  +#define SQLITE_SOURCE_ID      "2015-05-05 18:52:54 04afa3febee32854fbb09ef8d4ffffd432119716"
   117    117   
   118    118   /*
   119    119   ** CAPI3REF: Run-Time Library Version Numbers
   120    120   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   121    121   **
   122    122   ** These interfaces provide the same information as the [SQLITE_VERSION],
   123    123   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
   266    266   */
   267    267   #ifdef SQLITE_OMIT_FLOATING_POINT
   268    268   # define double sqlite3_int64
   269    269   #endif
   270    270   
   271    271   /*
   272    272   ** CAPI3REF: Closing A Database Connection
          273  +** DESTRUCTOR: sqlite3
   273    274   **
   274    275   ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
   275    276   ** for the [sqlite3] object.
   276    277   ** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
   277    278   ** the [sqlite3] object is successfully destroyed and all associated
   278    279   ** resources are deallocated.
   279    280   **
................................................................................
   317    318   ** This is legacy and deprecated.  It is included for historical
   318    319   ** compatibility and is not documented.
   319    320   */
   320    321   typedef int (*sqlite3_callback)(void*,int,char**, char**);
   321    322   
   322    323   /*
   323    324   ** CAPI3REF: One-Step Query Execution Interface
          325  +** METHOD: sqlite3
   324    326   **
   325    327   ** The sqlite3_exec() interface is a convenience wrapper around
   326    328   ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
   327    329   ** that allows an application to run multiple statements of SQL
   328    330   ** without having to use a lot of C code. 
   329    331   **
   330    332   ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
................................................................................
  1374   1376   ** ^If the option is unknown or SQLite is unable to set the option
  1375   1377   ** then this routine returns a non-zero [error code].
  1376   1378   */
  1377   1379   SQLITE_API int SQLITE_CDECL sqlite3_config(int, ...);
  1378   1380   
  1379   1381   /*
  1380   1382   ** CAPI3REF: Configure database connections
         1383  +** METHOD: sqlite3
  1381   1384   **
  1382   1385   ** The sqlite3_db_config() interface is used to make configuration
  1383   1386   ** changes to a [database connection].  The interface is similar to
  1384   1387   ** [sqlite3_config()] except that the changes apply to a single
  1385   1388   ** [database connection] (specified in the first argument).
  1386   1389   **
  1387   1390   ** The second argument to sqlite3_db_config(D,V,...)  is the
................................................................................
  1871   1874   #define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
  1872   1875   #define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
  1873   1876   #define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
  1874   1877   
  1875   1878   
  1876   1879   /*
  1877   1880   ** CAPI3REF: Enable Or Disable Extended Result Codes
         1881  +** METHOD: sqlite3
  1878   1882   **
  1879   1883   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  1880   1884   ** [extended result codes] feature of SQLite. ^The extended result
  1881   1885   ** codes are disabled by default for historical compatibility.
  1882   1886   */
  1883   1887   SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3*, int onoff);
  1884   1888   
  1885   1889   /*
  1886   1890   ** CAPI3REF: Last Insert Rowid
         1891  +** METHOD: sqlite3
  1887   1892   **
  1888   1893   ** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
  1889   1894   ** has a unique 64-bit signed
  1890   1895   ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
  1891   1896   ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
  1892   1897   ** names are not also used by explicitly declared columns. ^If
  1893   1898   ** the table has a column of type [INTEGER PRIMARY KEY] then that column
................................................................................
  1931   1936   ** unpredictable and might not equal either the old or the new
  1932   1937   ** last insert [rowid].
  1933   1938   */
  1934   1939   SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_last_insert_rowid(sqlite3*);
  1935   1940   
  1936   1941   /*
  1937   1942   ** CAPI3REF: Count The Number Of Rows Modified
         1943  +** METHOD: sqlite3
  1938   1944   **
  1939   1945   ** ^This function returns the number of rows modified, inserted or
  1940   1946   ** deleted by the most recently completed INSERT, UPDATE or DELETE
  1941   1947   ** statement on the database connection specified by the only parameter.
  1942   1948   ** ^Executing any other type of SQL statement does not modify the value
  1943   1949   ** returned by this function.
  1944   1950   **
................................................................................
  1983   1989   ** while [sqlite3_changes()] is running then the value returned
  1984   1990   ** is unpredictable and not meaningful.
  1985   1991   */
  1986   1992   SQLITE_API int SQLITE_STDCALL sqlite3_changes(sqlite3*);
  1987   1993   
  1988   1994   /*
  1989   1995   ** CAPI3REF: Total Number Of Rows Modified
         1996  +** METHOD: sqlite3
  1990   1997   **
  1991   1998   ** ^This function returns the total number of rows inserted, modified or
  1992   1999   ** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
  1993   2000   ** since the database connection was opened, including those executed as
  1994   2001   ** part of trigger programs. ^Executing any other type of SQL statement
  1995   2002   ** does not affect the value returned by sqlite3_total_changes().
  1996   2003   ** 
................................................................................
  2006   2013   ** while [sqlite3_total_changes()] is running then the value
  2007   2014   ** returned is unpredictable and not meaningful.
  2008   2015   */
  2009   2016   SQLITE_API int SQLITE_STDCALL sqlite3_total_changes(sqlite3*);
  2010   2017   
  2011   2018   /*
  2012   2019   ** CAPI3REF: Interrupt A Long-Running Query
         2020  +** METHOD: sqlite3
  2013   2021   **
  2014   2022   ** ^This function causes any pending database operation to abort and
  2015   2023   ** return at its earliest opportunity. This routine is typically
  2016   2024   ** called in response to a user action such as pressing "Cancel"
  2017   2025   ** or Ctrl-C where the user wants a long query operation to halt
  2018   2026   ** immediately.
  2019   2027   **
................................................................................
  2082   2090   */
  2083   2091   SQLITE_API int SQLITE_STDCALL sqlite3_complete(const char *sql);
  2084   2092   SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *sql);
  2085   2093   
  2086   2094   /*
  2087   2095   ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
  2088   2096   ** KEYWORDS: {busy-handler callback} {busy handler}
         2097  +** METHOD: sqlite3
  2089   2098   **
  2090   2099   ** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
  2091   2100   ** that might be invoked with argument P whenever
  2092   2101   ** an attempt is made to access a database table associated with
  2093   2102   ** [database connection] D when another thread
  2094   2103   ** or process has the table locked.
  2095   2104   ** The sqlite3_busy_handler() interface is used to implement
................................................................................
  2141   2150   ** A busy handler must not close the database connection
  2142   2151   ** or [prepared statement] that invoked the busy handler.
  2143   2152   */
  2144   2153   SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
  2145   2154   
  2146   2155   /*
  2147   2156   ** CAPI3REF: Set A Busy Timeout
         2157  +** METHOD: sqlite3
  2148   2158   **
  2149   2159   ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
  2150   2160   ** for a specified amount of time when a table is locked.  ^The handler
  2151   2161   ** will sleep multiple times until at least "ms" milliseconds of sleeping
  2152   2162   ** have accumulated.  ^After at least "ms" milliseconds of sleeping,
  2153   2163   ** the handler returns 0 which causes [sqlite3_step()] to return
  2154   2164   ** [SQLITE_BUSY].
................................................................................
  2163   2173   **
  2164   2174   ** See also:  [PRAGMA busy_timeout]
  2165   2175   */
  2166   2176   SQLITE_API int SQLITE_STDCALL sqlite3_busy_timeout(sqlite3*, int ms);
  2167   2177   
  2168   2178   /*
  2169   2179   ** CAPI3REF: Convenience Routines For Running Queries
         2180  +** METHOD: sqlite3
  2170   2181   **
  2171   2182   ** This is a legacy interface that is preserved for backwards compatibility.
  2172   2183   ** Use of this interface is not recommended.
  2173   2184   **
  2174   2185   ** Definition: A <b>result table</b> is memory data structure created by the
  2175   2186   ** [sqlite3_get_table()] interface.  A result table records the
  2176   2187   ** complete query results from one or more queries.
................................................................................
  2498   2509   ** internally and without recourse to the [sqlite3_vfs] xRandomness
  2499   2510   ** method.
  2500   2511   */
  2501   2512   SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
  2502   2513   
  2503   2514   /*
  2504   2515   ** CAPI3REF: Compile-Time Authorization Callbacks
         2516  +** METHOD: sqlite3
  2505   2517   **
  2506   2518   ** ^This routine registers an authorizer callback with a particular
  2507   2519   ** [database connection], supplied in the first argument.
  2508   2520   ** ^The authorizer callback is invoked as SQL statements are being compiled
  2509   2521   ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
  2510   2522   ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
  2511   2523   ** points during the compilation process, as logic is being created
................................................................................
  2654   2666   #define SQLITE_FUNCTION             31   /* NULL            Function Name   */
  2655   2667   #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
  2656   2668   #define SQLITE_COPY                  0   /* No longer used */
  2657   2669   #define SQLITE_RECURSIVE            33   /* NULL            NULL            */
  2658   2670   
  2659   2671   /*
  2660   2672   ** CAPI3REF: Tracing And Profiling Functions
         2673  +** METHOD: sqlite3
  2661   2674   **
  2662   2675   ** These routines register callback functions that can be used for
  2663   2676   ** tracing and profiling the execution of SQL statements.
  2664   2677   **
  2665   2678   ** ^The callback function registered by sqlite3_trace() is invoked at
  2666   2679   ** various times when an SQL statement is being run by [sqlite3_step()].
  2667   2680   ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
................................................................................
  2686   2699   */
  2687   2700   SQLITE_API void *SQLITE_STDCALL sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
  2688   2701   SQLITE_API SQLITE_EXPERIMENTAL void *SQLITE_STDCALL sqlite3_profile(sqlite3*,
  2689   2702      void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
  2690   2703   
  2691   2704   /*
  2692   2705   ** CAPI3REF: Query Progress Callbacks
         2706  +** METHOD: sqlite3
  2693   2707   **
  2694   2708   ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
  2695   2709   ** function X to be invoked periodically during long running calls to
  2696   2710   ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
  2697   2711   ** database connection D.  An example use for this
  2698   2712   ** interface is to keep a GUI updated during a large query.
  2699   2713   **
................................................................................
  2719   2733   ** database connections for the meaning of "modify" in this paragraph.
  2720   2734   **
  2721   2735   */
  2722   2736   SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
  2723   2737   
  2724   2738   /*
  2725   2739   ** CAPI3REF: Opening A New Database Connection
         2740  +** CONSTRUCTOR: sqlite3
  2726   2741   **
  2727   2742   ** ^These routines open an SQLite database file as specified by the 
  2728   2743   ** filename argument. ^The filename argument is interpreted as UTF-8 for
  2729   2744   ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
  2730   2745   ** order for sqlite3_open16(). ^(A [database connection] handle is usually
  2731   2746   ** returned in *ppDb, even if an error occurs.  The only exception is that
  2732   2747   ** if SQLite is unable to allocate memory to hold the [sqlite3] object,
................................................................................
  3004   3019   SQLITE_API const char *SQLITE_STDCALL sqlite3_uri_parameter(const char *zFilename, const char *zParam);
  3005   3020   SQLITE_API int SQLITE_STDCALL sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
  3006   3021   SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
  3007   3022   
  3008   3023   
  3009   3024   /*
  3010   3025   ** CAPI3REF: Error Codes And Messages
         3026  +** METHOD: sqlite3
  3011   3027   **
  3012   3028   ** ^If the most recent sqlite3_* API call associated with 
  3013   3029   ** [database connection] D failed, then the sqlite3_errcode(D) interface
  3014   3030   ** returns the numeric [result code] or [extended result code] for that
  3015   3031   ** API call.
  3016   3032   ** If the most recent API call was successful,
  3017   3033   ** then the return value from sqlite3_errcode() is undefined.
................................................................................
  3049   3065   SQLITE_API int SQLITE_STDCALL sqlite3_errcode(sqlite3 *db);
  3050   3066   SQLITE_API int SQLITE_STDCALL sqlite3_extended_errcode(sqlite3 *db);
  3051   3067   SQLITE_API const char *SQLITE_STDCALL sqlite3_errmsg(sqlite3*);
  3052   3068   SQLITE_API const void *SQLITE_STDCALL sqlite3_errmsg16(sqlite3*);
  3053   3069   SQLITE_API const char *SQLITE_STDCALL sqlite3_errstr(int);
  3054   3070   
  3055   3071   /*
  3056         -** CAPI3REF: SQL Statement Object
         3072  +** CAPI3REF: Prepared Statement Object
  3057   3073   ** KEYWORDS: {prepared statement} {prepared statements}
  3058   3074   **
  3059         -** An instance of this object represents a single SQL statement.
  3060         -** This object is variously known as a "prepared statement" or a
  3061         -** "compiled SQL statement" or simply as a "statement".
         3075  +** An instance of this object represents a single SQL statement that
         3076  +** has been compiled into binary form and is ready to be evaluated.
  3062   3077   **
  3063         -** The life of a statement object goes something like this:
         3078  +** Think of each SQL statement as a separate computer program.  The
         3079  +** original SQL text is source code.  A prepared statement object 
         3080  +** is the compiled object code.  All SQL must be converted into a
         3081  +** prepared statement before it can be run.
         3082  +**
         3083  +** The life-cycle of a prepared statement object usually goes like this:
  3064   3084   **
  3065   3085   ** <ol>
  3066         -** <li> Create the object using [sqlite3_prepare_v2()] or a related
  3067         -**      function.
  3068         -** <li> Bind values to [host parameters] using the sqlite3_bind_*()
         3086  +** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
         3087  +** <li> Bind values to [parameters] using the sqlite3_bind_*()
  3069   3088   **      interfaces.
  3070   3089   ** <li> Run the SQL by calling [sqlite3_step()] one or more times.
  3071         -** <li> Reset the statement using [sqlite3_reset()] then go back
         3090  +** <li> Reset the prepared statement using [sqlite3_reset()] then go back
  3072   3091   **      to step 2.  Do this zero or more times.
  3073   3092   ** <li> Destroy the object using [sqlite3_finalize()].
  3074   3093   ** </ol>
  3075         -**
  3076         -** Refer to documentation on individual methods above for additional
  3077         -** information.
  3078   3094   */
  3079   3095   typedef struct sqlite3_stmt sqlite3_stmt;
  3080   3096   
  3081   3097   /*
  3082   3098   ** CAPI3REF: Run-time Limits
         3099  +** METHOD: sqlite3
  3083   3100   **
  3084   3101   ** ^(This interface allows the size of various constructs to be limited
  3085   3102   ** on a connection by connection basis.  The first parameter is the
  3086   3103   ** [database connection] whose limit is to be set or queried.  The
  3087   3104   ** second parameter is one of the [limit categories] that define a
  3088   3105   ** class of constructs to be size limited.  The third parameter is the
  3089   3106   ** new limit for that construct.)^
................................................................................
  3187   3204   #define SQLITE_LIMIT_VARIABLE_NUMBER           9
  3188   3205   #define SQLITE_LIMIT_TRIGGER_DEPTH            10
  3189   3206   #define SQLITE_LIMIT_WORKER_THREADS           11
  3190   3207   
  3191   3208   /*
  3192   3209   ** CAPI3REF: Compiling An SQL Statement
  3193   3210   ** KEYWORDS: {SQL statement compiler}
         3211  +** METHOD: sqlite3
         3212  +** CONSTRUCTOR: sqlite3_stmt
  3194   3213   **
  3195   3214   ** To execute an SQL query, it must first be compiled into a byte-code
  3196   3215   ** program using one of these routines.
  3197   3216   **
  3198   3217   ** The first argument, "db", is a [database connection] obtained from a
  3199   3218   ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
  3200   3219   ** [sqlite3_open16()].  The database connection must not have been closed.
................................................................................
  3294   3313     int nByte,              /* Maximum length of zSql in bytes. */
  3295   3314     sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3296   3315     const void **pzTail     /* OUT: Pointer to unused portion of zSql */
  3297   3316   );
  3298   3317   
  3299   3318   /*
  3300   3319   ** CAPI3REF: Retrieving Statement SQL
         3320  +** METHOD: sqlite3_stmt
  3301   3321   **
  3302   3322   ** ^This interface can be used to retrieve a saved copy of the original
  3303   3323   ** SQL text used to create a [prepared statement] if that statement was
  3304   3324   ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
  3305   3325   */
  3306   3326   SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt);
  3307   3327   
  3308   3328   /*
  3309   3329   ** CAPI3REF: Determine If An SQL Statement Writes The Database
         3330  +** METHOD: sqlite3_stmt
  3310   3331   **
  3311   3332   ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  3312   3333   ** and only if the [prepared statement] X makes no direct changes to
  3313   3334   ** the content of the database file.
  3314   3335   **
  3315   3336   ** Note that [application-defined SQL functions] or
  3316   3337   ** [virtual tables] might change the database indirectly as a side effect.  
................................................................................
  3334   3355   ** change the configuration of a database connection, they do not make 
  3335   3356   ** changes to the content of the database files on disk.
  3336   3357   */
  3337   3358   SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
  3338   3359   
  3339   3360   /*
  3340   3361   ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
         3362  +** METHOD: sqlite3_stmt
  3341   3363   **
  3342   3364   ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
  3343   3365   ** [prepared statement] S has been stepped at least once using 
  3344   3366   ** [sqlite3_step(S)] but has not run to completion and/or has not 
  3345   3367   ** been reset using [sqlite3_reset(S)].  ^The sqlite3_stmt_busy(S)
  3346   3368   ** interface returns false if S is a NULL pointer.  If S is not a 
  3347   3369   ** NULL pointer and is not a pointer to a valid [prepared statement]
................................................................................
  3408   3430   */
  3409   3431   typedef struct sqlite3_context sqlite3_context;
  3410   3432   
  3411   3433   /*
  3412   3434   ** CAPI3REF: Binding Values To Prepared Statements
  3413   3435   ** KEYWORDS: {host parameter} {host parameters} {host parameter name}
  3414   3436   ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
         3437  +** METHOD: sqlite3_stmt
  3415   3438   **
  3416   3439   ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
  3417   3440   ** literals may be replaced by a [parameter] that matches one of following
  3418   3441   ** templates:
  3419   3442   **
  3420   3443   ** <ul>
  3421   3444   ** <li>  ?
................................................................................
  3526   3549   SQLITE_API int SQLITE_STDCALL sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
  3527   3550                            void(*)(void*), unsigned char encoding);
  3528   3551   SQLITE_API int SQLITE_STDCALL sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
  3529   3552   SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
  3530   3553   
  3531   3554   /*
  3532   3555   ** CAPI3REF: Number Of SQL Parameters
         3556  +** METHOD: sqlite3_stmt
  3533   3557   **
  3534   3558   ** ^This routine can be used to find the number of [SQL parameters]
  3535   3559   ** in a [prepared statement].  SQL parameters are tokens of the
  3536   3560   ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
  3537   3561   ** placeholders for values that are [sqlite3_bind_blob | bound]
  3538   3562   ** to the parameters at a later time.
  3539   3563   **
................................................................................
  3546   3570   ** [sqlite3_bind_parameter_name()], and
  3547   3571   ** [sqlite3_bind_parameter_index()].
  3548   3572   */
  3549   3573   SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt*);
  3550   3574   
  3551   3575   /*
  3552   3576   ** CAPI3REF: Name Of A Host Parameter
         3577  +** METHOD: sqlite3_stmt
  3553   3578   **
  3554   3579   ** ^The sqlite3_bind_parameter_name(P,N) interface returns
  3555   3580   ** the name of the N-th [SQL parameter] in the [prepared statement] P.
  3556   3581   ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
  3557   3582   ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
  3558   3583   ** respectively.
  3559   3584   ** In other words, the initial ":" or "$" or "@" or "?"
................................................................................
  3573   3598   ** [sqlite3_bind_parameter_count()], and
  3574   3599   ** [sqlite3_bind_parameter_index()].
  3575   3600   */
  3576   3601   SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt*, int);
  3577   3602   
  3578   3603   /*
  3579   3604   ** CAPI3REF: Index Of A Parameter With A Given Name
         3605  +** METHOD: sqlite3_stmt
  3580   3606   **
  3581   3607   ** ^Return the index of an SQL parameter given its name.  ^The
  3582   3608   ** index value returned is suitable for use as the second
  3583   3609   ** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
  3584   3610   ** is returned if no matching parameter is found.  ^The parameter
  3585   3611   ** name must be given in UTF-8 even if the original statement
  3586   3612   ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
................................................................................
  3589   3615   ** [sqlite3_bind_parameter_count()], and
  3590   3616   ** [sqlite3_bind_parameter_index()].
  3591   3617   */
  3592   3618   SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
  3593   3619   
  3594   3620   /*
  3595   3621   ** CAPI3REF: Reset All Bindings On A Prepared Statement
         3622  +** METHOD: sqlite3_stmt
  3596   3623   **
  3597   3624   ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
  3598   3625   ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
  3599   3626   ** ^Use this routine to reset all host parameters to NULL.
  3600   3627   */
  3601   3628   SQLITE_API int SQLITE_STDCALL sqlite3_clear_bindings(sqlite3_stmt*);
  3602   3629   
  3603   3630   /*
  3604   3631   ** CAPI3REF: Number Of Columns In A Result Set
         3632  +** METHOD: sqlite3_stmt
  3605   3633   **
  3606   3634   ** ^Return the number of columns in the result set returned by the
  3607   3635   ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
  3608   3636   ** statement that does not return data (for example an [UPDATE]).
  3609   3637   **
  3610   3638   ** See also: [sqlite3_data_count()]
  3611   3639   */
  3612   3640   SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt);
  3613   3641   
  3614   3642   /*
  3615   3643   ** CAPI3REF: Column Names In A Result Set
         3644  +** METHOD: sqlite3_stmt
  3616   3645   **
  3617   3646   ** ^These routines return the name assigned to a particular column
  3618   3647   ** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
  3619   3648   ** interface returns a pointer to a zero-terminated UTF-8 string
  3620   3649   ** and sqlite3_column_name16() returns a pointer to a zero-terminated
  3621   3650   ** UTF-16 string.  ^The first parameter is the [prepared statement]
  3622   3651   ** that implements the [SELECT] statement. ^The second parameter is the
................................................................................
  3638   3667   ** one release of SQLite to the next.
  3639   3668   */
  3640   3669   SQLITE_API const char *SQLITE_STDCALL sqlite3_column_name(sqlite3_stmt*, int N);
  3641   3670   SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt*, int N);
  3642   3671   
  3643   3672   /*
  3644   3673   ** CAPI3REF: Source Of Data In A Query Result
         3674  +** METHOD: sqlite3_stmt
  3645   3675   **
  3646   3676   ** ^These routines provide a means to determine the database, table, and
  3647   3677   ** table column that is the origin of a particular result column in
  3648   3678   ** [SELECT] statement.
  3649   3679   ** ^The name of the database or table or column can be returned as
  3650   3680   ** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
  3651   3681   ** the database name, the _table_ routines return the table name, and
................................................................................
  3690   3720   SQLITE_API const char *SQLITE_STDCALL sqlite3_column_table_name(sqlite3_stmt*,int);
  3691   3721   SQLITE_API const void *SQLITE_STDCALL sqlite3_column_table_name16(sqlite3_stmt*,int);
  3692   3722   SQLITE_API const char *SQLITE_STDCALL sqlite3_column_origin_name(sqlite3_stmt*,int);
  3693   3723   SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt*,int);
  3694   3724   
  3695   3725   /*
  3696   3726   ** CAPI3REF: Declared Datatype Of A Query Result
         3727  +** METHOD: sqlite3_stmt
  3697   3728   **
  3698   3729   ** ^(The first parameter is a [prepared statement].
  3699   3730   ** If this statement is a [SELECT] statement and the Nth column of the
  3700   3731   ** returned result set of that [SELECT] is a table column (not an
  3701   3732   ** expression or subquery) then the declared type of the table
  3702   3733   ** column is returned.)^  ^If the Nth column of the result set is an
  3703   3734   ** expression or subquery, then a NULL pointer is returned.
................................................................................
  3722   3753   ** used to hold those values.
  3723   3754   */
  3724   3755   SQLITE_API const char *SQLITE_STDCALL sqlite3_column_decltype(sqlite3_stmt*,int);
  3725   3756   SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt*,int);
  3726   3757   
  3727   3758   /*
  3728   3759   ** CAPI3REF: Evaluate An SQL Statement
         3760  +** METHOD: sqlite3_stmt
  3729   3761   **
  3730   3762   ** After a [prepared statement] has been prepared using either
  3731   3763   ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
  3732   3764   ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
  3733   3765   ** must be called one or more times to evaluate the statement.
  3734   3766   **
  3735   3767   ** The details of the behavior of the sqlite3_step() interface depend
................................................................................
  3801   3833   ** then the more specific [error codes] are returned directly
  3802   3834   ** by sqlite3_step().  The use of the "v2" interface is recommended.
  3803   3835   */
  3804   3836   SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt*);
  3805   3837   
  3806   3838   /*
  3807   3839   ** CAPI3REF: Number of columns in a result set
         3840  +** METHOD: sqlite3_stmt
  3808   3841   **
  3809   3842   ** ^The sqlite3_data_count(P) interface returns the number of columns in the
  3810   3843   ** current row of the result set of [prepared statement] P.
  3811   3844   ** ^If prepared statement P does not have results ready to return
  3812   3845   ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
  3813   3846   ** interfaces) then sqlite3_data_count(P) returns 0.
  3814   3847   ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
................................................................................
  3854   3887   # define SQLITE_TEXT     3
  3855   3888   #endif
  3856   3889   #define SQLITE3_TEXT     3
  3857   3890   
  3858   3891   /*
  3859   3892   ** CAPI3REF: Result Values From A Query
  3860   3893   ** KEYWORDS: {column access functions}
         3894  +** METHOD: sqlite3_stmt
  3861   3895   **
  3862   3896   ** These routines form the "result set" interface.
  3863   3897   **
  3864   3898   ** ^These routines return information about a single column of the current
  3865   3899   ** result row of a query.  ^In every case the first argument is a pointer
  3866   3900   ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
  3867   3901   ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
................................................................................
  4026   4060   SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_column_text(sqlite3_stmt*, int iCol);
  4027   4061   SQLITE_API const void *SQLITE_STDCALL sqlite3_column_text16(sqlite3_stmt*, int iCol);
  4028   4062   SQLITE_API int SQLITE_STDCALL sqlite3_column_type(sqlite3_stmt*, int iCol);
  4029   4063   SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_column_value(sqlite3_stmt*, int iCol);
  4030   4064   
  4031   4065   /*
  4032   4066   ** CAPI3REF: Destroy A Prepared Statement Object
         4067  +** DESTRUCTOR: sqlite3_stmt
  4033   4068   **
  4034   4069   ** ^The sqlite3_finalize() function is called to delete a [prepared statement].
  4035   4070   ** ^If the most recent evaluation of the statement encountered no errors
  4036   4071   ** or if the statement is never been evaluated, then sqlite3_finalize() returns
  4037   4072   ** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
  4038   4073   ** sqlite3_finalize(S) returns the appropriate [error code] or
  4039   4074   ** [extended error code].
................................................................................
  4053   4088   ** statement after it has been finalized can result in undefined and
  4054   4089   ** undesirable behavior such as segfaults and heap corruption.
  4055   4090   */
  4056   4091   SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt);
  4057   4092   
  4058   4093   /*
  4059   4094   ** CAPI3REF: Reset A Prepared Statement Object
         4095  +** METHOD: sqlite3_stmt
  4060   4096   **
  4061   4097   ** The sqlite3_reset() function is called to reset a [prepared statement]
  4062   4098   ** object back to its initial state, ready to be re-executed.
  4063   4099   ** ^Any SQL statement variables that had values bound to them using
  4064   4100   ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
  4065   4101   ** Use [sqlite3_clear_bindings()] to reset the bindings.
  4066   4102   **
................................................................................
  4082   4118   SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt);
  4083   4119   
  4084   4120   /*
  4085   4121   ** CAPI3REF: Create Or Redefine SQL Functions
  4086   4122   ** KEYWORDS: {function creation routines}
  4087   4123   ** KEYWORDS: {application-defined SQL function}
  4088   4124   ** KEYWORDS: {application-defined SQL functions}
         4125  +** METHOD: sqlite3
  4089   4126   **
  4090   4127   ** ^These functions (collectively known as "function creation routines")
  4091   4128   ** are used to add SQL functions or aggregates or to redefine the behavior
  4092   4129   ** of existing SQL functions or aggregates.  The only differences between
  4093   4130   ** these routines are the text encoding expected for
  4094   4131   ** the second parameter (the name of the function being created)
  4095   4132   ** and the presence or absence of a destructor callback for
................................................................................
  4251   4288   SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_thread_cleanup(void);
  4252   4289   SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
  4253   4290                         void*,sqlite3_int64);
  4254   4291   #endif
  4255   4292   
  4256   4293   /*
  4257   4294   ** CAPI3REF: Obtaining SQL Function Parameter Values
         4295  +** METHOD: sqlite3_value
  4258   4296   **
  4259   4297   ** The C-language implementation of SQL functions and aggregates uses
  4260   4298   ** this set of interface routines to access the parameter values on
  4261   4299   ** the function or aggregate.
  4262   4300   **
  4263   4301   ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
  4264   4302   ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
................................................................................
  4309   4347   SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value*);
  4310   4348   SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value*);
  4311   4349   SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value*);
  4312   4350   SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*);
  4313   4351   
  4314   4352   /*
  4315   4353   ** CAPI3REF: Obtain Aggregate Function Context
         4354  +** METHOD: sqlite3_context
  4316   4355   **
  4317   4356   ** Implementations of aggregate SQL functions use this
  4318   4357   ** routine to allocate memory for storing their state.
  4319   4358   **
  4320   4359   ** ^The first time the sqlite3_aggregate_context(C,N) routine is called 
  4321   4360   ** for a particular aggregate function, SQLite
  4322   4361   ** allocates N of memory, zeroes out that memory, and returns a pointer
................................................................................
  4353   4392   ** This routine must be called from the same thread in which
  4354   4393   ** the aggregate SQL function is running.
  4355   4394   */
  4356   4395   SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context*, int nBytes);
  4357   4396   
  4358   4397   /*
  4359   4398   ** CAPI3REF: User Data For Functions
         4399  +** METHOD: sqlite3_context
  4360   4400   **
  4361   4401   ** ^The sqlite3_user_data() interface returns a copy of
  4362   4402   ** the pointer that was the pUserData parameter (the 5th parameter)
  4363   4403   ** of the [sqlite3_create_function()]
  4364   4404   ** and [sqlite3_create_function16()] routines that originally
  4365   4405   ** registered the application defined function.
  4366   4406   **
................................................................................
  4367   4407   ** This routine must be called from the same thread in which
  4368   4408   ** the application-defined function is running.
  4369   4409   */
  4370   4410   SQLITE_API void *SQLITE_STDCALL sqlite3_user_data(sqlite3_context*);
  4371   4411   
  4372   4412   /*
  4373   4413   ** CAPI3REF: Database Connection For Functions
         4414  +** METHOD: sqlite3_context
  4374   4415   **
  4375   4416   ** ^The sqlite3_context_db_handle() interface returns a copy of
  4376   4417   ** the pointer to the [database connection] (the 1st parameter)
  4377   4418   ** of the [sqlite3_create_function()]
  4378   4419   ** and [sqlite3_create_function16()] routines that originally
  4379   4420   ** registered the application defined function.
  4380   4421   */
  4381   4422   SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context*);
  4382   4423   
  4383   4424   /*
  4384   4425   ** CAPI3REF: Function Auxiliary Data
         4426  +** METHOD: sqlite3_context
  4385   4427   **
  4386   4428   ** These functions may be used by (non-aggregate) SQL functions to
  4387   4429   ** associate metadata with argument values. If the same value is passed to
  4388   4430   ** multiple invocations of the same SQL function during query execution, under
  4389   4431   ** some circumstances the associated metadata may be preserved.  An example
  4390   4432   ** of where this might be useful is in a regular-expression matching
  4391   4433   ** function. The compiled version of the regular expression can be stored as
................................................................................
  4450   4492   */
  4451   4493   typedef void (*sqlite3_destructor_type)(void*);
  4452   4494   #define SQLITE_STATIC      ((sqlite3_destructor_type)0)
  4453   4495   #define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
  4454   4496   
  4455   4497   /*
  4456   4498   ** CAPI3REF: Setting The Result Of An SQL Function
         4499  +** METHOD: sqlite3_context
  4457   4500   **
  4458   4501   ** These routines are used by the xFunc or xFinal callbacks that
  4459   4502   ** implement SQL functions and aggregates.  See
  4460   4503   ** [sqlite3_create_function()] and [sqlite3_create_function16()]
  4461   4504   ** for additional information.
  4462   4505   **
  4463   4506   ** These functions work very much like the [parameter binding] family of
................................................................................
  4585   4628   SQLITE_API void SQLITE_STDCALL sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
  4586   4629   SQLITE_API void SQLITE_STDCALL sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
  4587   4630   SQLITE_API void SQLITE_STDCALL sqlite3_result_value(sqlite3_context*, sqlite3_value*);
  4588   4631   SQLITE_API void SQLITE_STDCALL sqlite3_result_zeroblob(sqlite3_context*, int n);
  4589   4632   
  4590   4633   /*
  4591   4634   ** CAPI3REF: Define New Collating Sequences
         4635  +** METHOD: sqlite3
  4592   4636   **
  4593   4637   ** ^These functions add, remove, or modify a [collation] associated
  4594   4638   ** with the [database connection] specified as the first argument.
  4595   4639   **
  4596   4640   ** ^The name of the collation is a UTF-8 string
  4597   4641   ** for sqlite3_create_collation() and sqlite3_create_collation_v2()
  4598   4642   ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
................................................................................
  4687   4731     int eTextRep, 
  4688   4732     void *pArg,
  4689   4733     int(*xCompare)(void*,int,const void*,int,const void*)
  4690   4734   );
  4691   4735   
  4692   4736   /*
  4693   4737   ** CAPI3REF: Collation Needed Callbacks
         4738  +** METHOD: sqlite3
  4694   4739   **
  4695   4740   ** ^To avoid having to register all collation sequences before a database
  4696   4741   ** can be used, a single callback function may be registered with the
  4697   4742   ** [database connection] to be invoked whenever an undefined collation
  4698   4743   ** sequence is required.
  4699   4744   **
  4700   4745   ** ^If the function is registered using the sqlite3_collation_needed() API,
................................................................................
  4894   4939   ** or else the use of the [data_store_directory pragma] should be avoided.
  4895   4940   */
  4896   4941   SQLITE_API SQLITE_EXTERN char *sqlite3_data_directory;
  4897   4942   
  4898   4943   /*
  4899   4944   ** CAPI3REF: Test For Auto-Commit Mode
  4900   4945   ** KEYWORDS: {autocommit mode}
         4946  +** METHOD: sqlite3
  4901   4947   **
  4902   4948   ** ^The sqlite3_get_autocommit() interface returns non-zero or
  4903   4949   ** zero if the given database connection is or is not in autocommit mode,
  4904   4950   ** respectively.  ^Autocommit mode is on by default.
  4905   4951   ** ^Autocommit mode is disabled by a [BEGIN] statement.
  4906   4952   ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
  4907   4953   **
................................................................................
  4916   4962   ** connection while this routine is running, then the return value
  4917   4963   ** is undefined.
  4918   4964   */
  4919   4965   SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3*);
  4920   4966   
  4921   4967   /*
  4922   4968   ** CAPI3REF: Find The Database Handle Of A Prepared Statement
         4969  +** METHOD: sqlite3_stmt
  4923   4970   **
  4924   4971   ** ^The sqlite3_db_handle interface returns the [database connection] handle
  4925   4972   ** to which a [prepared statement] belongs.  ^The [database connection]
  4926   4973   ** returned by sqlite3_db_handle is the same [database connection]
  4927   4974   ** that was the first argument
  4928   4975   ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
  4929   4976   ** create the statement in the first place.
  4930   4977   */
  4931   4978   SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_db_handle(sqlite3_stmt*);
  4932   4979   
  4933   4980   /*
  4934   4981   ** CAPI3REF: Return The Filename For A Database Connection
         4982  +** METHOD: sqlite3
  4935   4983   **
  4936   4984   ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
  4937   4985   ** associated with database N of connection D.  ^The main database file
  4938   4986   ** has the name "main".  If there is no attached database N on the database
  4939   4987   ** connection D, or if database N is a temporary or in-memory database, then
  4940   4988   ** a NULL pointer is returned.
  4941   4989   **
................................................................................
  4944   4992   ** will be an absolute pathname, even if the filename used
  4945   4993   ** to open the database originally was a URI or relative pathname.
  4946   4994   */
  4947   4995   SQLITE_API const char *SQLITE_STDCALL sqlite3_db_filename(sqlite3 *db, const char *zDbName);
  4948   4996   
  4949   4997   /*
  4950   4998   ** CAPI3REF: Determine if a database is read-only
         4999  +** METHOD: sqlite3
  4951   5000   **
  4952   5001   ** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
  4953   5002   ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
  4954   5003   ** the name of a database on connection D.
  4955   5004   */
  4956   5005   SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
  4957   5006   
  4958   5007   /*
  4959   5008   ** CAPI3REF: Find the next prepared statement
         5009  +** METHOD: sqlite3
  4960   5010   **
  4961   5011   ** ^This interface returns a pointer to the next [prepared statement] after
  4962   5012   ** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
  4963   5013   ** then this interface returns a pointer to the first prepared statement
  4964   5014   ** associated with the database connection pDb.  ^If no prepared statement
  4965   5015   ** satisfies the conditions of this routine, it returns NULL.
  4966   5016   **
................................................................................
  4968   5018   ** [sqlite3_next_stmt(D,S)] must refer to an open database
  4969   5019   ** connection and in particular must not be a NULL pointer.
  4970   5020   */
  4971   5021   SQLITE_API sqlite3_stmt *SQLITE_STDCALL sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
  4972   5022   
  4973   5023   /*
  4974   5024   ** CAPI3REF: Commit And Rollback Notification Callbacks
         5025  +** METHOD: sqlite3
  4975   5026   **
  4976   5027   ** ^The sqlite3_commit_hook() interface registers a callback
  4977   5028   ** function to be invoked whenever a transaction is [COMMIT | committed].
  4978   5029   ** ^Any callback set by a previous call to sqlite3_commit_hook()
  4979   5030   ** for the same database connection is overridden.
  4980   5031   ** ^The sqlite3_rollback_hook() interface registers a callback
  4981   5032   ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
................................................................................
  5017   5068   ** See also the [sqlite3_update_hook()] interface.
  5018   5069   */
  5019   5070   SQLITE_API void *SQLITE_STDCALL sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
  5020   5071   SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
  5021   5072   
  5022   5073   /*
  5023   5074   ** CAPI3REF: Data Change Notification Callbacks
         5075  +** METHOD: sqlite3
  5024   5076   **
  5025   5077   ** ^The sqlite3_update_hook() interface registers a callback function
  5026   5078   ** with the [database connection] identified by the first argument
  5027   5079   ** to be invoked whenever a row is updated, inserted or deleted in
  5028   5080   ** a rowid table.
  5029   5081   ** ^Any callback set by a previous call to this function
  5030   5082   ** for the same database connection is overridden.
................................................................................
  5123   5175   **
  5124   5176   ** See also: [sqlite3_db_release_memory()]
  5125   5177   */
  5126   5178   SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int);
  5127   5179   
  5128   5180   /*
  5129   5181   ** CAPI3REF: Free Memory Used By A Database Connection
         5182  +** METHOD: sqlite3
  5130   5183   **
  5131   5184   ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
  5132   5185   ** memory as possible from database connection D. Unlike the
  5133   5186   ** [sqlite3_release_memory()] interface, this interface is in effect even
  5134   5187   ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
  5135   5188   ** omitted.
  5136   5189   **
................................................................................
  5200   5253   ** [sqlite3_soft_heap_limit64()] interface rather than this one.
  5201   5254   */
  5202   5255   SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_soft_heap_limit(int N);
  5203   5256   
  5204   5257   
  5205   5258   /*
  5206   5259   ** CAPI3REF: Extract Metadata About A Column Of A Table
         5260  +** METHOD: sqlite3
  5207   5261   **
  5208   5262   ** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
  5209   5263   ** information about column C of table T in database D
  5210   5264   ** on [database connection] X.)^  ^The sqlite3_table_column_metadata()
  5211   5265   ** interface returns SQLITE_OK and fills in the non-NULL pointers in
  5212   5266   ** the final five arguments with appropriate values if the specified
  5213   5267   ** column exists.  ^The sqlite3_table_column_metadata() interface returns
................................................................................
  5278   5332     int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
  5279   5333     int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
  5280   5334     int *pAutoinc               /* OUTPUT: True if column is auto-increment */
  5281   5335   );
  5282   5336   
  5283   5337   /*
  5284   5338   ** CAPI3REF: Load An Extension
         5339  +** METHOD: sqlite3
  5285   5340   **
  5286   5341   ** ^This interface loads an SQLite extension library from the named file.
  5287   5342   **
  5288   5343   ** ^The sqlite3_load_extension() interface attempts to load an
  5289   5344   ** [SQLite extension] library contained in the file zFile.  If
  5290   5345   ** the file cannot be loaded directly, attempts are made to load
  5291   5346   ** with various operating-system specific extensions added.
................................................................................
  5319   5374     const char *zFile,    /* Name of the shared library containing extension */
  5320   5375     const char *zProc,    /* Entry point.  Derived from zFile if 0 */
  5321   5376     char **pzErrMsg       /* Put error message here if not 0 */
  5322   5377   );
  5323   5378   
  5324   5379   /*
  5325   5380   ** CAPI3REF: Enable Or Disable Extension Loading
         5381  +** METHOD: sqlite3
  5326   5382   **
  5327   5383   ** ^So as not to open security holes in older applications that are
  5328   5384   ** unprepared to deal with [extension loading], and as a means of disabling
  5329   5385   ** [extension loading] while evaluating user-entered SQL, the following API
  5330   5386   ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
  5331   5387   **
  5332   5388   ** ^Extension loading is off by default.
................................................................................
  5568   5624   #define SQLITE_INDEX_CONSTRAINT_LE    8
  5569   5625   #define SQLITE_INDEX_CONSTRAINT_LT    16
  5570   5626   #define SQLITE_INDEX_CONSTRAINT_GE    32
  5571   5627   #define SQLITE_INDEX_CONSTRAINT_MATCH 64
  5572   5628   
  5573   5629   /*
  5574   5630   ** CAPI3REF: Register A Virtual Table Implementation
         5631  +** METHOD: sqlite3
  5575   5632   **
  5576   5633   ** ^These routines are used to register a new [virtual table module] name.
  5577   5634   ** ^Module names must be registered before
  5578   5635   ** creating a new [virtual table] using the module and before using a
  5579   5636   ** preexisting [virtual table] for the module.
  5580   5637   **
  5581   5638   ** ^The module name is registered on the [database connection] specified
................................................................................
  5664   5721   ** to declare the format (the names and datatypes of the columns) of
  5665   5722   ** the virtual tables they implement.
  5666   5723   */
  5667   5724   SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3*, const char *zSQL);
  5668   5725   
  5669   5726   /*
  5670   5727   ** CAPI3REF: Overload A Function For A Virtual Table
         5728  +** METHOD: sqlite3
  5671   5729   **
  5672   5730   ** ^(Virtual tables can provide alternative implementations of functions
  5673   5731   ** using the [xFindFunction] method of the [virtual table module].  
  5674   5732   ** But global versions of those functions
  5675   5733   ** must exist in order to be overloaded.)^
  5676   5734   **
  5677   5735   ** ^(This API makes sure a global version of a function with a particular
................................................................................
  5706   5764   ** can be used to read or write small subsections of the BLOB.
  5707   5765   ** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
  5708   5766   */
  5709   5767   typedef struct sqlite3_blob sqlite3_blob;
  5710   5768   
  5711   5769   /*
  5712   5770   ** CAPI3REF: Open A BLOB For Incremental I/O
         5771  +** METHOD: sqlite3
         5772  +** CONSTRUCTOR: sqlite3_blob
  5713   5773   **
  5714   5774   ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
  5715   5775   ** in row iRow, column zColumn, table zTable in database zDb;
  5716   5776   ** in other words, the same BLOB that would be selected by:
  5717   5777   **
  5718   5778   ** <pre>
  5719   5779   **     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
................................................................................
  5787   5847     sqlite3_int64 iRow,
  5788   5848     int flags,
  5789   5849     sqlite3_blob **ppBlob
  5790   5850   );
  5791   5851   
  5792   5852   /*
  5793   5853   ** CAPI3REF: Move a BLOB Handle to a New Row
         5854  +** METHOD: sqlite3_blob
  5794   5855   **
  5795   5856   ** ^This function is used to move an existing blob handle so that it points
  5796   5857   ** to a different row of the same database table. ^The new row is identified
  5797   5858   ** by the rowid value passed as the second argument. Only the row can be
  5798   5859   ** changed. ^The database, table and column on which the blob handle is open
  5799   5860   ** remain the same. Moving an existing blob handle to a new row can be
  5800   5861   ** faster than closing the existing handle and opening a new one.
................................................................................
  5811   5872   **
  5812   5873   ** ^This function sets the database handle error code and message.
  5813   5874   */
  5814   5875   SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
  5815   5876   
  5816   5877   /*
  5817   5878   ** CAPI3REF: Close A BLOB Handle
         5879  +** DESTRUCTOR: sqlite3_blob
  5818   5880   **
  5819   5881   ** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
  5820   5882   ** unconditionally.  Even if this routine returns an error code, the 
  5821   5883   ** handle is still closed.)^
  5822   5884   **
  5823   5885   ** ^If the blob handle being closed was opened for read-write access, and if
  5824   5886   ** the database is in auto-commit mode and there are no other open read-write
................................................................................
  5833   5895   ** is passed a valid open blob handle, the values returned by the 
  5834   5896   ** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
  5835   5897   */
  5836   5898   SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *);
  5837   5899   
  5838   5900   /*
  5839   5901   ** CAPI3REF: Return The Size Of An Open BLOB
         5902  +** METHOD: sqlite3_blob
  5840   5903   **
  5841   5904   ** ^Returns the size in bytes of the BLOB accessible via the 
  5842   5905   ** successfully opened [BLOB handle] in its only argument.  ^The
  5843   5906   ** incremental blob I/O routines can only read or overwriting existing
  5844   5907   ** blob content; they cannot change the size of a blob.
  5845   5908   **
  5846   5909   ** This routine only works on a [BLOB handle] which has been created
................................................................................
  5848   5911   ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
  5849   5912   ** to this routine results in undefined and probably undesirable behavior.
  5850   5913   */
  5851   5914   SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *);
  5852   5915   
  5853   5916   /*
  5854   5917   ** CAPI3REF: Read Data From A BLOB Incrementally
         5918  +** METHOD: sqlite3_blob
  5855   5919   **
  5856   5920   ** ^(This function is used to read data from an open [BLOB handle] into a
  5857   5921   ** caller-supplied buffer. N bytes of data are copied into buffer Z
  5858   5922   ** from the open BLOB, starting at offset iOffset.)^
  5859   5923   **
  5860   5924   ** ^If offset iOffset is less than N bytes from the end of the BLOB,
  5861   5925   ** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
................................................................................
  5876   5940   **
  5877   5941   ** See also: [sqlite3_blob_write()].
  5878   5942   */
  5879   5943   SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
  5880   5944   
  5881   5945   /*
  5882   5946   ** CAPI3REF: Write Data Into A BLOB Incrementally
         5947  +** METHOD: sqlite3_blob
  5883   5948   **
  5884   5949   ** ^(This function is used to write data into an open [BLOB handle] from a
  5885   5950   ** caller-supplied buffer. N bytes of data are copied from the buffer Z
  5886   5951   ** into the open BLOB, starting at offset iOffset.)^
  5887   5952   **
  5888   5953   ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
  5889   5954   ** Otherwise, an  [error code] or an [extended error code] is returned.)^
................................................................................
  6203   6268   #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
  6204   6269   #define SQLITE_MUTEX_STATIC_APP1      8  /* For use by application */
  6205   6270   #define SQLITE_MUTEX_STATIC_APP2      9  /* For use by application */
  6206   6271   #define SQLITE_MUTEX_STATIC_APP3     10  /* For use by application */
  6207   6272   
  6208   6273   /*
  6209   6274   ** CAPI3REF: Retrieve the mutex for a database connection
         6275  +** METHOD: sqlite3
  6210   6276   **
  6211   6277   ** ^This interface returns a pointer the [sqlite3_mutex] object that 
  6212   6278   ** serializes access to the [database connection] given in the argument
  6213   6279   ** when the [threading mode] is Serialized.
  6214   6280   ** ^If the [threading mode] is Single-thread or Multi-thread then this
  6215   6281   ** routine returns a NULL pointer.
  6216   6282   */
  6217   6283   SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3*);
  6218   6284   
  6219   6285   /*
  6220   6286   ** CAPI3REF: Low-Level Control Of Database Files
         6287  +** METHOD: sqlite3
  6221   6288   **
  6222   6289   ** ^The [sqlite3_file_control()] interface makes a direct call to the
  6223   6290   ** xFileControl method for the [sqlite3_io_methods] object associated
  6224   6291   ** with a particular database identified by the second argument. ^The
  6225   6292   ** name of the database is "main" for the main database or "temp" for the
  6226   6293   ** TEMP database, or the name that appears after the AS keyword for
  6227   6294   ** databases that are added using the [ATTACH] SQL command.
................................................................................
  6430   6497   #define SQLITE_STATUS_PARSER_STACK         6
  6431   6498   #define SQLITE_STATUS_PAGECACHE_SIZE       7
  6432   6499   #define SQLITE_STATUS_SCRATCH_SIZE         8
  6433   6500   #define SQLITE_STATUS_MALLOC_COUNT         9
  6434   6501   
  6435   6502   /*
  6436   6503   ** CAPI3REF: Database Connection Status
         6504  +** METHOD: sqlite3
  6437   6505   **
  6438   6506   ** ^This interface is used to retrieve runtime status information 
  6439   6507   ** about a single [database connection].  ^The first argument is the
  6440   6508   ** database connection object to be interrogated.  ^The second argument
  6441   6509   ** is an integer constant, taken from the set of
  6442   6510   ** [SQLITE_DBSTATUS options], that
  6443   6511   ** determines the parameter to interrogate.  The set of 
................................................................................
  6558   6626   #define SQLITE_DBSTATUS_CACHE_WRITE          9
  6559   6627   #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  6560   6628   #define SQLITE_DBSTATUS_MAX                 10   /* Largest defined DBSTATUS */
  6561   6629   
  6562   6630   
  6563   6631   /*
  6564   6632   ** CAPI3REF: Prepared Statement Status
         6633  +** METHOD: sqlite3_stmt
  6565   6634   **
  6566   6635   ** ^(Each prepared statement maintains various
  6567   6636   ** [SQLITE_STMTSTATUS counters] that measure the number
  6568   6637   ** of times it has performed specific operations.)^  These counters can
  6569   6638   ** be used to monitor the performance characteristics of the prepared
  6570   6639   ** statements.  For example, if the number of table steps greatly exceeds
  6571   6640   ** the number of table searches or result rows, that would tend to indicate
................................................................................
  7061   7130   SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage);
  7062   7131   SQLITE_API int SQLITE_STDCALL sqlite3_backup_finish(sqlite3_backup *p);
  7063   7132   SQLITE_API int SQLITE_STDCALL sqlite3_backup_remaining(sqlite3_backup *p);
  7064   7133   SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p);
  7065   7134   
  7066   7135   /*
  7067   7136   ** CAPI3REF: Unlock Notification
         7137  +** METHOD: sqlite3
  7068   7138   **
  7069   7139   ** ^When running in shared-cache mode, a database operation may fail with
  7070   7140   ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
  7071   7141   ** individual tables within the shared-cache cannot be obtained. See
  7072   7142   ** [SQLite Shared-Cache Mode] for a description of shared-cache locking. 
  7073   7143   ** ^This API may be used to register a callback that SQLite will invoke 
  7074   7144   ** when the connection currently holding the required lock relinquishes it.
................................................................................
  7231   7301   ** a few hundred characters, it will be truncated to the length of the
  7232   7302   ** buffer.
  7233   7303   */
  7234   7304   SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...);
  7235   7305   
  7236   7306   /*
  7237   7307   ** CAPI3REF: Write-Ahead Log Commit Hook
         7308  +** METHOD: sqlite3
  7238   7309   **
  7239   7310   ** ^The [sqlite3_wal_hook()] function is used to register a callback that
  7240   7311   ** is invoked each time data is committed to a database in wal mode.
  7241   7312   **
  7242   7313   ** ^(The callback is invoked by SQLite after the commit has taken place and 
  7243   7314   ** the associated write-lock on the database released)^, so the implementation 
  7244   7315   ** may read, write or [checkpoint] the database as required.
................................................................................
  7270   7341     sqlite3*, 
  7271   7342     int(*)(void *,sqlite3*,const char*,int),
  7272   7343     void*
  7273   7344   );
  7274   7345   
  7275   7346   /*
  7276   7347   ** CAPI3REF: Configure an auto-checkpoint
         7348  +** METHOD: sqlite3
  7277   7349   **
  7278   7350   ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
  7279   7351   ** [sqlite3_wal_hook()] that causes any database on [database connection] D
  7280   7352   ** to automatically [checkpoint]
  7281   7353   ** after committing a transaction if there are N or
  7282   7354   ** more frames in the [write-ahead log] file.  ^Passing zero or 
  7283   7355   ** a negative value as the nFrame parameter disables automatic
................................................................................
  7300   7372   ** is only necessary if the default setting is found to be suboptimal
  7301   7373   ** for a particular application.
  7302   7374   */
  7303   7375   SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
  7304   7376   
  7305   7377   /*
  7306   7378   ** CAPI3REF: Checkpoint a database
         7379  +** METHOD: sqlite3
  7307   7380   **
  7308   7381   ** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
  7309   7382   ** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
  7310   7383   **
  7311   7384   ** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the 
  7312   7385   ** [write-ahead log] for database X on [database connection] D to be
  7313   7386   ** transferred into the database file and for the write-ahead log to
................................................................................
  7321   7394   ** start a callback but which do not need the full power (and corresponding
  7322   7395   ** complication) of [sqlite3_wal_checkpoint_v2()].
  7323   7396   */
  7324   7397   SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
  7325   7398   
  7326   7399   /*
  7327   7400   ** CAPI3REF: Checkpoint a database
         7401  +** METHOD: sqlite3
  7328   7402   **
  7329   7403   ** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
  7330   7404   ** operation on database X of [database connection] D in mode M.  Status
  7331   7405   ** information is written back into integers pointed to by L and C.)^
  7332   7406   ** ^(The M parameter must be a valid [checkpoint mode]:)^
  7333   7407   **
  7334   7408   ** <dl>
................................................................................
  7575   7649   #define SQLITE_SCANSTAT_EST      2
  7576   7650   #define SQLITE_SCANSTAT_NAME     3
  7577   7651   #define SQLITE_SCANSTAT_EXPLAIN  4
  7578   7652   #define SQLITE_SCANSTAT_SELECTID 5
  7579   7653   
  7580   7654   /*
  7581   7655   ** CAPI3REF: Prepared Statement Scan Status
         7656  +** METHOD: sqlite3_stmt
  7582   7657   **
  7583   7658   ** This interface returns information about the predicted and measured
  7584   7659   ** performance for pStmt.  Advanced applications can use this
  7585   7660   ** interface to compare the predicted and the measured performance and
  7586   7661   ** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
  7587   7662   **
  7588   7663   ** Since this interface is expected to be rarely used, it is only
................................................................................
  7612   7687     int idx,                  /* Index of loop to report on */
  7613   7688     int iScanStatusOp,        /* Information desired.  SQLITE_SCANSTAT_* */
  7614   7689     void *pOut                /* Result written here */
  7615   7690   );     
  7616   7691   
  7617   7692   /*
  7618   7693   ** CAPI3REF: Zero Scan-Status Counters
         7694  +** METHOD: sqlite3_stmt
  7619   7695   **
  7620   7696   ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
  7621   7697   **
  7622   7698   ** This API is only available if the library is built with pre-processor
  7623   7699   ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
  7624   7700   */
  7625   7701   SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);